ath3k.c 13 KB

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