ath3k.c 12 KB

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