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