ath3k.c 14 KB

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