ath3k.c 13 KB

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