ath3k.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_DNLOAD 0x01
  31. #define ATH3K_GETSTATE 0x05
  32. #define ATH3K_SET_NORMAL_MODE 0x07
  33. #define ATH3K_GETVERSION 0x09
  34. #define USB_REG_SWITCH_VID_PID 0x0a
  35. #define ATH3K_MODE_MASK 0x3F
  36. #define ATH3K_NORMAL_MODE 0x0E
  37. #define ATH3K_PATCH_UPDATE 0x80
  38. #define ATH3K_SYSCFG_UPDATE 0x40
  39. #define ATH3K_XTAL_FREQ_26M 0x00
  40. #define ATH3K_XTAL_FREQ_40M 0x01
  41. #define ATH3K_XTAL_FREQ_19P2 0x02
  42. #define ATH3K_NAME_LEN 0xFF
  43. struct ath3k_version {
  44. unsigned int rom_version;
  45. unsigned int build_version;
  46. unsigned int ram_version;
  47. unsigned char ref_clock;
  48. unsigned char reserved[0x07];
  49. };
  50. static struct usb_device_id ath3k_table[] = {
  51. /* Atheros AR3011 */
  52. { USB_DEVICE(0x0CF3, 0x3000) },
  53. /* Atheros AR3011 with sflash firmware*/
  54. { USB_DEVICE(0x0489, 0xE027) },
  55. { USB_DEVICE(0x0489, 0xE03D) },
  56. { USB_DEVICE(0x04F2, 0xAFF1) },
  57. { USB_DEVICE(0x0930, 0x0215) },
  58. { USB_DEVICE(0x0CF3, 0x3002) },
  59. { USB_DEVICE(0x0CF3, 0xE019) },
  60. /* Atheros AR9285 Malbec with sflash firmware */
  61. { USB_DEVICE(0x03F0, 0x311D) },
  62. /* Atheros AR3012 with sflash firmware*/
  63. { USB_DEVICE(0x0489, 0xe04d) },
  64. { USB_DEVICE(0x0489, 0xe04e) },
  65. { USB_DEVICE(0x0489, 0xe057) },
  66. { USB_DEVICE(0x0489, 0xe056) },
  67. { USB_DEVICE(0x0489, 0xe078) },
  68. { USB_DEVICE(0x04c5, 0x1330) },
  69. { USB_DEVICE(0x04CA, 0x3004) },
  70. { USB_DEVICE(0x04CA, 0x3005) },
  71. { USB_DEVICE(0x04CA, 0x3006) },
  72. { USB_DEVICE(0x04CA, 0x3008) },
  73. { USB_DEVICE(0x04CA, 0x3010) },
  74. { USB_DEVICE(0x0930, 0x0219) },
  75. { USB_DEVICE(0x0930, 0x0227) },
  76. { USB_DEVICE(0x0b05, 0x17d0) },
  77. { USB_DEVICE(0x0CF3, 0x0036) },
  78. { USB_DEVICE(0x0CF3, 0x3004) },
  79. { USB_DEVICE(0x0CF3, 0x817a) },
  80. { USB_DEVICE(0x0CF3, 0x817b) },
  81. { USB_DEVICE(0x0CF3, 0x3008) },
  82. { USB_DEVICE(0x0cf3, 0xe003) },
  83. { USB_DEVICE(0x0CF3, 0xE005) },
  84. { USB_DEVICE(0x13d3, 0x3362) },
  85. { USB_DEVICE(0x13d3, 0x3375) },
  86. { USB_DEVICE(0x13d3, 0x3393) },
  87. { USB_DEVICE(0x13d3, 0x3402) },
  88. { USB_DEVICE(0x13d3, 0x3408) },
  89. { USB_DEVICE(0x13d3, 0x3423) },
  90. { USB_DEVICE(0x13d3, 0x3432) },
  91. { USB_DEVICE(0x13d3, 0x3474) },
  92. /* Atheros AR5BBU12 with sflash firmware */
  93. { USB_DEVICE(0x0489, 0xE02C) },
  94. /* Atheros AR5BBU22 with sflash firmware */
  95. { USB_DEVICE(0x0489, 0xE036) },
  96. { USB_DEVICE(0x0489, 0xE03C) },
  97. { } /* Terminating entry */
  98. };
  99. MODULE_DEVICE_TABLE(usb, ath3k_table);
  100. #define BTUSB_ATH3012 0x80
  101. /* This table is to load patch and sysconfig files
  102. * for AR3012 */
  103. static struct usb_device_id ath3k_blist_tbl[] = {
  104. /* Atheros AR3012 with sflash firmware*/
  105. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  106. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  107. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  108. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  109. { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
  110. { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
  111. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  112. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  113. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  114. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  115. { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
  116. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  117. { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
  118. { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
  119. { USB_DEVICE(0x0CF3, 0x0036), .driver_info = BTUSB_ATH3012 },
  120. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  121. { USB_DEVICE(0x0CF3, 0x817a), .driver_info = BTUSB_ATH3012 },
  122. { USB_DEVICE(0x0CF3, 0x817b), .driver_info = BTUSB_ATH3012 },
  123. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  124. { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
  125. { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
  126. { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
  127. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  128. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  129. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  130. { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
  131. { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
  132. { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
  133. { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
  134. { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
  135. /* Atheros AR5BBU22 with sflash firmware */
  136. { USB_DEVICE(0x0489, 0xE03C), .driver_info = BTUSB_ATH3012 },
  137. { USB_DEVICE(0x0489, 0xE036), .driver_info = BTUSB_ATH3012 },
  138. { } /* Terminating entry */
  139. };
  140. #define USB_REQ_DFU_DNLOAD 1
  141. #define BULK_SIZE 4096
  142. #define FW_HDR_SIZE 20
  143. #define TIMEGAP_USEC_MIN 50
  144. #define TIMEGAP_USEC_MAX 100
  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_ATOMIC);
  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. if ((err = usb_control_msg(udev, pipe,
  160. USB_REQ_DFU_DNLOAD,
  161. USB_TYPE_VENDOR, 0, 0,
  162. send_buf, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
  163. BT_ERR("Can't change to loading configuration err");
  164. goto error;
  165. }
  166. sent += 20;
  167. count -= 20;
  168. while (count) {
  169. /* workaround the compatibility issue with xHCI controller*/
  170. usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
  171. size = min_t(uint, count, BULK_SIZE);
  172. pipe = usb_sndbulkpipe(udev, 0x02);
  173. memcpy(send_buf, firmware->data + sent, size);
  174. err = usb_bulk_msg(udev, pipe, send_buf, size,
  175. &len, 3000);
  176. if (err || (len != size)) {
  177. BT_ERR("Error in firmware loading err = %d,"
  178. "len = %d, size = %d", err, len, size);
  179. goto error;
  180. }
  181. sent += size;
  182. count -= size;
  183. }
  184. error:
  185. kfree(send_buf);
  186. return err;
  187. }
  188. static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
  189. {
  190. int pipe = 0;
  191. pipe = usb_rcvctrlpipe(udev, 0);
  192. return usb_control_msg(udev, pipe, ATH3K_GETSTATE,
  193. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  194. state, 0x01, USB_CTRL_SET_TIMEOUT);
  195. }
  196. static int ath3k_get_version(struct usb_device *udev,
  197. struct ath3k_version *version)
  198. {
  199. int pipe = 0;
  200. pipe = usb_rcvctrlpipe(udev, 0);
  201. return usb_control_msg(udev, pipe, ATH3K_GETVERSION,
  202. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, version,
  203. sizeof(struct ath3k_version),
  204. USB_CTRL_SET_TIMEOUT);
  205. }
  206. static int ath3k_load_fwfile(struct usb_device *udev,
  207. const struct firmware *firmware)
  208. {
  209. u8 *send_buf;
  210. int err, pipe, len, size, count, sent = 0;
  211. int ret;
  212. count = firmware->size;
  213. send_buf = kmalloc(BULK_SIZE, GFP_ATOMIC);
  214. if (!send_buf) {
  215. BT_ERR("Can't allocate memory chunk for firmware");
  216. return -ENOMEM;
  217. }
  218. size = min_t(uint, count, FW_HDR_SIZE);
  219. memcpy(send_buf, firmware->data, size);
  220. pipe = usb_sndctrlpipe(udev, 0);
  221. ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD,
  222. USB_TYPE_VENDOR, 0, 0, send_buf,
  223. size, USB_CTRL_SET_TIMEOUT);
  224. if (ret < 0) {
  225. BT_ERR("Can't change to loading configuration err");
  226. kfree(send_buf);
  227. return ret;
  228. }
  229. sent += size;
  230. count -= size;
  231. while (count) {
  232. /* workaround the compatibility issue with xHCI controller*/
  233. usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
  234. size = min_t(uint, count, BULK_SIZE);
  235. pipe = usb_sndbulkpipe(udev, 0x02);
  236. memcpy(send_buf, firmware->data + sent, size);
  237. err = usb_bulk_msg(udev, pipe, send_buf, size,
  238. &len, 3000);
  239. if (err || (len != size)) {
  240. BT_ERR("Error in firmware loading err = %d,"
  241. "len = %d, size = %d", err, len, size);
  242. kfree(send_buf);
  243. return err;
  244. }
  245. sent += size;
  246. count -= size;
  247. }
  248. kfree(send_buf);
  249. return 0;
  250. }
  251. static int ath3k_switch_pid(struct usb_device *udev)
  252. {
  253. int pipe = 0;
  254. pipe = usb_sndctrlpipe(udev, 0);
  255. return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID,
  256. USB_TYPE_VENDOR, 0, 0,
  257. NULL, 0, USB_CTRL_SET_TIMEOUT);
  258. }
  259. static int ath3k_set_normal_mode(struct usb_device *udev)
  260. {
  261. unsigned char fw_state;
  262. int pipe = 0, ret;
  263. ret = ath3k_get_state(udev, &fw_state);
  264. if (ret < 0) {
  265. BT_ERR("Can't get state to change to normal mode err");
  266. return ret;
  267. }
  268. if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
  269. BT_DBG("firmware was already in normal mode");
  270. return 0;
  271. }
  272. pipe = usb_sndctrlpipe(udev, 0);
  273. return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE,
  274. USB_TYPE_VENDOR, 0, 0,
  275. NULL, 0, USB_CTRL_SET_TIMEOUT);
  276. }
  277. static int ath3k_load_patch(struct usb_device *udev)
  278. {
  279. unsigned char fw_state;
  280. char filename[ATH3K_NAME_LEN] = {0};
  281. const struct firmware *firmware;
  282. struct ath3k_version fw_version, pt_version;
  283. int ret;
  284. ret = ath3k_get_state(udev, &fw_state);
  285. if (ret < 0) {
  286. BT_ERR("Can't get state to change to load ram patch err");
  287. return ret;
  288. }
  289. if (fw_state & ATH3K_PATCH_UPDATE) {
  290. BT_DBG("Patch was already downloaded");
  291. return 0;
  292. }
  293. ret = ath3k_get_version(udev, &fw_version);
  294. if (ret < 0) {
  295. BT_ERR("Can't get version to change to load ram patch err");
  296. return ret;
  297. }
  298. snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
  299. fw_version.rom_version);
  300. ret = request_firmware(&firmware, filename, &udev->dev);
  301. if (ret < 0) {
  302. BT_ERR("Patch file not found %s", filename);
  303. return ret;
  304. }
  305. pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8);
  306. pt_version.build_version = *(int *)
  307. (firmware->data + firmware->size - 4);
  308. if ((pt_version.rom_version != fw_version.rom_version) ||
  309. (pt_version.build_version <= fw_version.build_version)) {
  310. BT_ERR("Patch file version did not match with firmware");
  311. release_firmware(firmware);
  312. return -EINVAL;
  313. }
  314. ret = ath3k_load_fwfile(udev, firmware);
  315. release_firmware(firmware);
  316. return ret;
  317. }
  318. static int ath3k_load_syscfg(struct usb_device *udev)
  319. {
  320. unsigned char fw_state;
  321. char filename[ATH3K_NAME_LEN] = {0};
  322. const struct firmware *firmware;
  323. struct ath3k_version fw_version;
  324. int clk_value, ret;
  325. ret = ath3k_get_state(udev, &fw_state);
  326. if (ret < 0) {
  327. BT_ERR("Can't get state to change to load configration err");
  328. return -EBUSY;
  329. }
  330. ret = ath3k_get_version(udev, &fw_version);
  331. if (ret < 0) {
  332. BT_ERR("Can't get version to change to load ram patch err");
  333. return ret;
  334. }
  335. switch (fw_version.ref_clock) {
  336. case ATH3K_XTAL_FREQ_26M:
  337. clk_value = 26;
  338. break;
  339. case ATH3K_XTAL_FREQ_40M:
  340. clk_value = 40;
  341. break;
  342. case ATH3K_XTAL_FREQ_19P2:
  343. clk_value = 19;
  344. break;
  345. default:
  346. clk_value = 0;
  347. break;
  348. }
  349. snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
  350. fw_version.rom_version, clk_value, ".dfu");
  351. ret = request_firmware(&firmware, filename, &udev->dev);
  352. if (ret < 0) {
  353. BT_ERR("Configuration file not found %s", filename);
  354. return ret;
  355. }
  356. ret = ath3k_load_fwfile(udev, firmware);
  357. release_firmware(firmware);
  358. return ret;
  359. }
  360. static int ath3k_probe(struct usb_interface *intf,
  361. const struct usb_device_id *id)
  362. {
  363. const struct firmware *firmware;
  364. struct usb_device *udev = interface_to_usbdev(intf);
  365. int ret;
  366. BT_DBG("intf %p id %p", intf, id);
  367. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  368. return -ENODEV;
  369. /* match device ID in ath3k blacklist table */
  370. if (!id->driver_info) {
  371. const struct usb_device_id *match;
  372. match = usb_match_id(intf, ath3k_blist_tbl);
  373. if (match)
  374. id = match;
  375. }
  376. /* load patch and sysconfig files for AR3012 */
  377. if (id->driver_info & BTUSB_ATH3012) {
  378. /* New firmware with patch and sysconfig files already loaded */
  379. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
  380. return -ENODEV;
  381. ret = ath3k_load_patch(udev);
  382. if (ret < 0) {
  383. BT_ERR("Loading patch file failed");
  384. return ret;
  385. }
  386. ret = ath3k_load_syscfg(udev);
  387. if (ret < 0) {
  388. BT_ERR("Loading sysconfig file failed");
  389. return ret;
  390. }
  391. ret = ath3k_set_normal_mode(udev);
  392. if (ret < 0) {
  393. BT_ERR("Set normal mode failed");
  394. return ret;
  395. }
  396. ath3k_switch_pid(udev);
  397. return 0;
  398. }
  399. if (request_firmware(&firmware, "ath3k-1.fw", &udev->dev) < 0) {
  400. BT_ERR("Error loading firmware");
  401. return -EIO;
  402. }
  403. ret = ath3k_load_firmware(udev, firmware);
  404. release_firmware(firmware);
  405. return ret;
  406. }
  407. static void ath3k_disconnect(struct usb_interface *intf)
  408. {
  409. BT_DBG("ath3k_disconnect intf %p", intf);
  410. }
  411. static struct usb_driver ath3k_driver = {
  412. .name = "ath3k",
  413. .probe = ath3k_probe,
  414. .disconnect = ath3k_disconnect,
  415. .id_table = ath3k_table,
  416. };
  417. static int __init ath3k_init(void)
  418. {
  419. BT_INFO("Atheros AR30xx firmware driver ver %s", VERSION);
  420. return usb_register(&ath3k_driver);
  421. }
  422. static void __exit ath3k_exit(void)
  423. {
  424. usb_deregister(&ath3k_driver);
  425. }
  426. module_init(ath3k_init);
  427. module_exit(ath3k_exit);
  428. MODULE_AUTHOR("Atheros Communications");
  429. MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
  430. MODULE_VERSION(VERSION);
  431. MODULE_LICENSE("GPL");
  432. MODULE_FIRMWARE("ath3k-1.fw");