ath3k.c 11 KB

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