ath3k.c 10 KB

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