ath3k.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/device.h>
  26. #include <linux/firmware.h>
  27. #include <linux/usb.h>
  28. #include <net/bluetooth/bluetooth.h>
  29. #define VERSION "1.0"
  30. #define ATH3K_FIRMWARE "ath3k-1.fw"
  31. #define ATH3K_DNLOAD 0x01
  32. #define ATH3K_GETSTATE 0x05
  33. #define ATH3K_SET_NORMAL_MODE 0x07
  34. #define ATH3K_GETVERSION 0x09
  35. #define USB_REG_SWITCH_VID_PID 0x0a
  36. #define ATH3K_MODE_MASK 0x3F
  37. #define ATH3K_NORMAL_MODE 0x0E
  38. #define ATH3K_PATCH_UPDATE 0x80
  39. #define ATH3K_SYSCFG_UPDATE 0x40
  40. #define ATH3K_XTAL_FREQ_26M 0x00
  41. #define ATH3K_XTAL_FREQ_40M 0x01
  42. #define ATH3K_XTAL_FREQ_19P2 0x02
  43. #define ATH3K_NAME_LEN 0xFF
  44. struct ath3k_version {
  45. unsigned int rom_version;
  46. unsigned int build_version;
  47. unsigned int ram_version;
  48. unsigned char ref_clock;
  49. unsigned char reserved[0x07];
  50. };
  51. static const struct usb_device_id ath3k_table[] = {
  52. /* Atheros AR3011 */
  53. { USB_DEVICE(0x0CF3, 0x3000) },
  54. /* Atheros AR3011 with sflash firmware*/
  55. { USB_DEVICE(0x0489, 0xE027) },
  56. { USB_DEVICE(0x0489, 0xE03D) },
  57. { USB_DEVICE(0x0930, 0x0215) },
  58. { USB_DEVICE(0x0CF3, 0x3002) },
  59. { USB_DEVICE(0x0CF3, 0xE019) },
  60. { USB_DEVICE(0x13d3, 0x3304) },
  61. /* Atheros AR9285 Malbec with sflash firmware */
  62. { USB_DEVICE(0x03F0, 0x311D) },
  63. /* Atheros AR3012 with sflash firmware*/
  64. { USB_DEVICE(0x0489, 0xe04d) },
  65. { USB_DEVICE(0x0489, 0xe04e) },
  66. { USB_DEVICE(0x0489, 0xe057) },
  67. { USB_DEVICE(0x0489, 0xe056) },
  68. { USB_DEVICE(0x0489, 0xe05f) },
  69. { USB_DEVICE(0x04c5, 0x1330) },
  70. { USB_DEVICE(0x04CA, 0x3004) },
  71. { USB_DEVICE(0x04CA, 0x3005) },
  72. { USB_DEVICE(0x04CA, 0x3006) },
  73. { USB_DEVICE(0x04CA, 0x3007) },
  74. { USB_DEVICE(0x04CA, 0x3008) },
  75. { USB_DEVICE(0x04CA, 0x300b) },
  76. { USB_DEVICE(0x0930, 0x0219) },
  77. { USB_DEVICE(0x0930, 0x0220) },
  78. { USB_DEVICE(0x0b05, 0x17d0) },
  79. { USB_DEVICE(0x0CF3, 0x0036) },
  80. { USB_DEVICE(0x0CF3, 0x3004) },
  81. { USB_DEVICE(0x0CF3, 0x3008) },
  82. { USB_DEVICE(0x0CF3, 0x311D) },
  83. { USB_DEVICE(0x0CF3, 0x311E) },
  84. { USB_DEVICE(0x0CF3, 0x311F) },
  85. { USB_DEVICE(0x0cf3, 0x3121) },
  86. { USB_DEVICE(0x0CF3, 0x817a) },
  87. { USB_DEVICE(0x0cf3, 0xe003) },
  88. { USB_DEVICE(0x0CF3, 0xE004) },
  89. { USB_DEVICE(0x0CF3, 0xE005) },
  90. { USB_DEVICE(0x13d3, 0x3362) },
  91. { USB_DEVICE(0x13d3, 0x3375) },
  92. { USB_DEVICE(0x13d3, 0x3393) },
  93. { USB_DEVICE(0x13d3, 0x3402) },
  94. /* Atheros AR5BBU12 with sflash firmware */
  95. { USB_DEVICE(0x0489, 0xE02C) },
  96. /* Atheros AR5BBU22 with sflash firmware */
  97. { USB_DEVICE(0x0489, 0xE036) },
  98. { USB_DEVICE(0x0489, 0xE03C) },
  99. { } /* Terminating entry */
  100. };
  101. MODULE_DEVICE_TABLE(usb, ath3k_table);
  102. #define BTUSB_ATH3012 0x80
  103. /* This table is to load patch and sysconfig files
  104. * for AR3012 */
  105. static const struct usb_device_id ath3k_blist_tbl[] = {
  106. /* Atheros AR3012 with sflash firmware*/
  107. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  108. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  109. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  110. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  111. { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
  112. { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
  113. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  114. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  115. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  116. { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
  117. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  118. { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
  119. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  120. { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
  121. { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
  122. { USB_DEVICE(0x0CF3, 0x0036), .driver_info = BTUSB_ATH3012 },
  123. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  124. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  125. { USB_DEVICE(0x0cf3, 0x311D), .driver_info = BTUSB_ATH3012 },
  126. { USB_DEVICE(0x0cf3, 0x311E), .driver_info = BTUSB_ATH3012 },
  127. { USB_DEVICE(0x0cf3, 0x311F), .driver_info = BTUSB_ATH3012 },
  128. { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
  129. { USB_DEVICE(0x0CF3, 0x817a), .driver_info = BTUSB_ATH3012 },
  130. { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
  131. { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
  132. { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
  133. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  134. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  135. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  136. { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
  137. /* Atheros AR5BBU22 with sflash firmware */
  138. { USB_DEVICE(0x0489, 0xE036), .driver_info = BTUSB_ATH3012 },
  139. { USB_DEVICE(0x0489, 0xE03C), .driver_info = BTUSB_ATH3012 },
  140. { } /* Terminating entry */
  141. };
  142. #define USB_REQ_DFU_DNLOAD 1
  143. #define BULK_SIZE 4096
  144. #define FW_HDR_SIZE 20
  145. static int ath3k_load_firmware(struct usb_device *udev,
  146. const struct firmware *firmware)
  147. {
  148. u8 *send_buf;
  149. int err, pipe, len, size, sent = 0;
  150. int count = firmware->size;
  151. BT_DBG("udev %p", udev);
  152. pipe = usb_sndctrlpipe(udev, 0);
  153. send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
  154. if (!send_buf) {
  155. BT_ERR("Can't allocate memory chunk for firmware");
  156. return -ENOMEM;
  157. }
  158. memcpy(send_buf, firmware->data, 20);
  159. err = usb_control_msg(udev, pipe, USB_REQ_DFU_DNLOAD, USB_TYPE_VENDOR,
  160. 0, 0, send_buf, 20, USB_CTRL_SET_TIMEOUT);
  161. if (err < 0) {
  162. BT_ERR("Can't change to loading configuration err");
  163. goto error;
  164. }
  165. sent += 20;
  166. count -= 20;
  167. pipe = usb_sndbulkpipe(udev, 0x02);
  168. while (count) {
  169. size = min_t(uint, count, BULK_SIZE);
  170. memcpy(send_buf, firmware->data + sent, size);
  171. err = usb_bulk_msg(udev, pipe, send_buf, size,
  172. &len, 3000);
  173. if (err || (len != size)) {
  174. BT_ERR("Error in firmware loading err = %d,"
  175. "len = %d, size = %d", err, len, size);
  176. goto error;
  177. }
  178. sent += size;
  179. count -= size;
  180. }
  181. error:
  182. kfree(send_buf);
  183. return err;
  184. }
  185. static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
  186. {
  187. int ret, pipe = 0;
  188. char *buf;
  189. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  190. if (!buf)
  191. return -ENOMEM;
  192. pipe = usb_rcvctrlpipe(udev, 0);
  193. ret = usb_control_msg(udev, pipe, ATH3K_GETSTATE,
  194. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  195. buf, sizeof(*buf), USB_CTRL_SET_TIMEOUT);
  196. *state = *buf;
  197. kfree(buf);
  198. return ret;
  199. }
  200. static int ath3k_get_version(struct usb_device *udev,
  201. struct ath3k_version *version)
  202. {
  203. int ret, pipe = 0;
  204. struct ath3k_version *buf;
  205. const int size = sizeof(*buf);
  206. buf = kmalloc(size, GFP_KERNEL);
  207. if (!buf)
  208. return -ENOMEM;
  209. pipe = usb_rcvctrlpipe(udev, 0);
  210. ret = usb_control_msg(udev, pipe, ATH3K_GETVERSION,
  211. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  212. buf, size, USB_CTRL_SET_TIMEOUT);
  213. memcpy(version, buf, size);
  214. kfree(buf);
  215. return ret;
  216. }
  217. static int ath3k_load_fwfile(struct usb_device *udev,
  218. const struct firmware *firmware)
  219. {
  220. u8 *send_buf;
  221. int err, pipe, len, size, count, sent = 0;
  222. int ret;
  223. count = firmware->size;
  224. send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
  225. if (!send_buf) {
  226. BT_ERR("Can't allocate memory chunk for firmware");
  227. return -ENOMEM;
  228. }
  229. size = min_t(uint, count, FW_HDR_SIZE);
  230. memcpy(send_buf, firmware->data, size);
  231. pipe = usb_sndctrlpipe(udev, 0);
  232. ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD,
  233. USB_TYPE_VENDOR, 0, 0, send_buf,
  234. size, USB_CTRL_SET_TIMEOUT);
  235. if (ret < 0) {
  236. BT_ERR("Can't change to loading configuration err");
  237. kfree(send_buf);
  238. return ret;
  239. }
  240. sent += size;
  241. count -= size;
  242. while (count) {
  243. size = min_t(uint, count, BULK_SIZE);
  244. pipe = usb_sndbulkpipe(udev, 0x02);
  245. memcpy(send_buf, firmware->data + sent, size);
  246. err = usb_bulk_msg(udev, pipe, send_buf, size,
  247. &len, 3000);
  248. if (err || (len != size)) {
  249. BT_ERR("Error in firmware loading err = %d,"
  250. "len = %d, size = %d", err, len, size);
  251. kfree(send_buf);
  252. return err;
  253. }
  254. sent += size;
  255. count -= size;
  256. }
  257. kfree(send_buf);
  258. return 0;
  259. }
  260. static int ath3k_switch_pid(struct usb_device *udev)
  261. {
  262. int pipe = 0;
  263. pipe = usb_sndctrlpipe(udev, 0);
  264. return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID,
  265. USB_TYPE_VENDOR, 0, 0,
  266. NULL, 0, USB_CTRL_SET_TIMEOUT);
  267. }
  268. static int ath3k_set_normal_mode(struct usb_device *udev)
  269. {
  270. unsigned char fw_state;
  271. int pipe = 0, ret;
  272. ret = ath3k_get_state(udev, &fw_state);
  273. if (ret < 0) {
  274. BT_ERR("Can't get state to change to normal mode err");
  275. return ret;
  276. }
  277. if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
  278. BT_DBG("firmware was already in normal mode");
  279. return 0;
  280. }
  281. pipe = usb_sndctrlpipe(udev, 0);
  282. return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE,
  283. USB_TYPE_VENDOR, 0, 0,
  284. NULL, 0, USB_CTRL_SET_TIMEOUT);
  285. }
  286. static int ath3k_load_patch(struct usb_device *udev)
  287. {
  288. unsigned char fw_state;
  289. char filename[ATH3K_NAME_LEN] = {0};
  290. const struct firmware *firmware;
  291. struct ath3k_version fw_version, pt_version;
  292. int ret;
  293. ret = ath3k_get_state(udev, &fw_state);
  294. if (ret < 0) {
  295. BT_ERR("Can't get state to change to load ram patch err");
  296. return ret;
  297. }
  298. if (fw_state & ATH3K_PATCH_UPDATE) {
  299. BT_DBG("Patch was already downloaded");
  300. return 0;
  301. }
  302. ret = ath3k_get_version(udev, &fw_version);
  303. if (ret < 0) {
  304. BT_ERR("Can't get version to change to load ram patch err");
  305. return ret;
  306. }
  307. snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
  308. le32_to_cpu(fw_version.rom_version));
  309. ret = request_firmware(&firmware, filename, &udev->dev);
  310. if (ret < 0) {
  311. BT_ERR("Patch file not found %s", filename);
  312. return ret;
  313. }
  314. pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8);
  315. pt_version.build_version = *(int *)
  316. (firmware->data + firmware->size - 4);
  317. if ((pt_version.rom_version != fw_version.rom_version) ||
  318. (pt_version.build_version <= fw_version.build_version)) {
  319. BT_ERR("Patch file version did not match with firmware");
  320. release_firmware(firmware);
  321. return -EINVAL;
  322. }
  323. ret = ath3k_load_fwfile(udev, firmware);
  324. release_firmware(firmware);
  325. return ret;
  326. }
  327. static int ath3k_load_syscfg(struct usb_device *udev)
  328. {
  329. unsigned char fw_state;
  330. char filename[ATH3K_NAME_LEN] = {0};
  331. const struct firmware *firmware;
  332. struct ath3k_version fw_version;
  333. int clk_value, ret;
  334. ret = ath3k_get_state(udev, &fw_state);
  335. if (ret < 0) {
  336. BT_ERR("Can't get state to change to load configuration err");
  337. return -EBUSY;
  338. }
  339. ret = ath3k_get_version(udev, &fw_version);
  340. if (ret < 0) {
  341. BT_ERR("Can't get version to change to load ram patch err");
  342. return ret;
  343. }
  344. switch (fw_version.ref_clock) {
  345. case ATH3K_XTAL_FREQ_26M:
  346. clk_value = 26;
  347. break;
  348. case ATH3K_XTAL_FREQ_40M:
  349. clk_value = 40;
  350. break;
  351. case ATH3K_XTAL_FREQ_19P2:
  352. clk_value = 19;
  353. break;
  354. default:
  355. clk_value = 0;
  356. break;
  357. }
  358. snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
  359. le32_to_cpu(fw_version.rom_version), clk_value, ".dfu");
  360. ret = request_firmware(&firmware, filename, &udev->dev);
  361. if (ret < 0) {
  362. BT_ERR("Configuration file not found %s", filename);
  363. return ret;
  364. }
  365. ret = ath3k_load_fwfile(udev, firmware);
  366. release_firmware(firmware);
  367. return ret;
  368. }
  369. static int ath3k_probe(struct usb_interface *intf,
  370. const struct usb_device_id *id)
  371. {
  372. const struct firmware *firmware;
  373. struct usb_device *udev = interface_to_usbdev(intf);
  374. int ret;
  375. BT_DBG("intf %p id %p", intf, id);
  376. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  377. return -ENODEV;
  378. /* match device ID in ath3k blacklist table */
  379. if (!id->driver_info) {
  380. const struct usb_device_id *match;
  381. match = usb_match_id(intf, ath3k_blist_tbl);
  382. if (match)
  383. id = match;
  384. }
  385. /* load patch and sysconfig files for AR3012 */
  386. if (id->driver_info & BTUSB_ATH3012) {
  387. /* New firmware with patch and sysconfig files already loaded */
  388. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
  389. return -ENODEV;
  390. ret = ath3k_load_patch(udev);
  391. if (ret < 0) {
  392. BT_ERR("Loading patch file failed");
  393. return ret;
  394. }
  395. ret = ath3k_load_syscfg(udev);
  396. if (ret < 0) {
  397. BT_ERR("Loading sysconfig file failed");
  398. return ret;
  399. }
  400. ret = ath3k_set_normal_mode(udev);
  401. if (ret < 0) {
  402. BT_ERR("Set normal mode failed");
  403. return ret;
  404. }
  405. ath3k_switch_pid(udev);
  406. return 0;
  407. }
  408. ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev);
  409. if (ret < 0) {
  410. if (ret == -ENOENT)
  411. BT_ERR("Firmware file \"%s\" not found",
  412. ATH3K_FIRMWARE);
  413. else
  414. BT_ERR("Firmware file \"%s\" request failed (err=%d)",
  415. ATH3K_FIRMWARE, ret);
  416. return ret;
  417. }
  418. ret = ath3k_load_firmware(udev, firmware);
  419. release_firmware(firmware);
  420. return ret;
  421. }
  422. static void ath3k_disconnect(struct usb_interface *intf)
  423. {
  424. BT_DBG("ath3k_disconnect intf %p", intf);
  425. }
  426. static struct usb_driver ath3k_driver = {
  427. .name = "ath3k",
  428. .probe = ath3k_probe,
  429. .disconnect = ath3k_disconnect,
  430. .id_table = ath3k_table,
  431. .disable_hub_initiated_lpm = 1,
  432. };
  433. module_usb_driver(ath3k_driver);
  434. MODULE_AUTHOR("Atheros Communications");
  435. MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
  436. MODULE_VERSION(VERSION);
  437. MODULE_LICENSE("GPL");
  438. MODULE_FIRMWARE(ATH3K_FIRMWARE);