ath3k.c 10 KB

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