usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Intel Wireless UWB Link 1480
  3. * USB SKU firmware upload implementation
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * This driver will prepare the i1480 device to behave as a real
  24. * Wireless USB HWA adaptor by uploading the firmware.
  25. *
  26. * When the device is connected or driver is loaded, i1480_usb_probe()
  27. * is called--this will allocate and initialize the device structure,
  28. * fill in the pointers to the common functions (read, write,
  29. * wait_init_done and cmd for HWA command execution) and once that is
  30. * done, call the common firmware uploading routine. Then clean up and
  31. * return -ENODEV, as we don't attach to the device.
  32. *
  33. * The rest are the basic ops we implement that the fw upload code
  34. * uses to do its job. All the ops in the common code are i1480->NAME,
  35. * the functions are i1480_usb_NAME().
  36. */
  37. #include <linux/module.h>
  38. #include <linux/version.h>
  39. #include <linux/usb.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uwb.h>
  43. #include <linux/usb/wusb.h>
  44. #include <linux/usb/wusb-wa.h>
  45. #include "i1480-dfu.h"
  46. #define D_LOCAL 0
  47. #include <linux/uwb/debug.h>
  48. struct i1480_usb {
  49. struct i1480 i1480;
  50. struct usb_device *usb_dev;
  51. struct usb_interface *usb_iface;
  52. struct urb *neep_urb; /* URB for reading from EP1 */
  53. };
  54. static
  55. void i1480_usb_init(struct i1480_usb *i1480_usb)
  56. {
  57. i1480_init(&i1480_usb->i1480);
  58. }
  59. static
  60. int i1480_usb_create(struct i1480_usb *i1480_usb, struct usb_interface *iface)
  61. {
  62. struct usb_device *usb_dev = interface_to_usbdev(iface);
  63. int result = -ENOMEM;
  64. i1480_usb->usb_dev = usb_get_dev(usb_dev); /* bind the USB device */
  65. i1480_usb->usb_iface = usb_get_intf(iface);
  66. usb_set_intfdata(iface, i1480_usb); /* Bind the driver to iface0 */
  67. i1480_usb->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
  68. if (i1480_usb->neep_urb == NULL)
  69. goto error;
  70. return 0;
  71. error:
  72. usb_set_intfdata(iface, NULL);
  73. usb_put_intf(iface);
  74. usb_put_dev(usb_dev);
  75. return result;
  76. }
  77. static
  78. void i1480_usb_destroy(struct i1480_usb *i1480_usb)
  79. {
  80. usb_kill_urb(i1480_usb->neep_urb);
  81. usb_free_urb(i1480_usb->neep_urb);
  82. usb_set_intfdata(i1480_usb->usb_iface, NULL);
  83. usb_put_intf(i1480_usb->usb_iface);
  84. usb_put_dev(i1480_usb->usb_dev);
  85. }
  86. /**
  87. * Write a buffer to a memory address in the i1480 device
  88. *
  89. * @i1480: i1480 instance
  90. * @memory_address:
  91. * Address where to write the data buffer to.
  92. * @buffer: Buffer to the data
  93. * @size: Size of the buffer [has to be < 512].
  94. * @returns: 0 if ok, < 0 errno code on error.
  95. *
  96. * Data buffers to USB cannot be on the stack or in vmalloc'ed areas,
  97. * so we copy it to the local i1480 buffer before proceeding. In any
  98. * case, we have a max size we can send, soooo.
  99. */
  100. static
  101. int i1480_usb_write(struct i1480 *i1480, u32 memory_address,
  102. const void *buffer, size_t size)
  103. {
  104. int result = 0;
  105. struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
  106. size_t buffer_size, itr = 0;
  107. d_fnstart(3, i1480->dev, "(%p, 0x%08x, %p, %zu)\n",
  108. i1480, memory_address, buffer, size);
  109. BUG_ON(size & 0x3); /* Needs to be a multiple of 4 */
  110. while (size > 0) {
  111. buffer_size = size < i1480->buf_size ? size : i1480->buf_size;
  112. memcpy(i1480->cmd_buf, buffer + itr, buffer_size);
  113. result = usb_control_msg(
  114. i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
  115. 0xf0, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  116. cpu_to_le16(memory_address & 0xffff),
  117. cpu_to_le16((memory_address >> 16) & 0xffff),
  118. i1480->cmd_buf, buffer_size, 100 /* FIXME: arbitrary */);
  119. if (result < 0)
  120. break;
  121. d_printf(3, i1480->dev,
  122. "wrote @ 0x%08x %u bytes (of %zu bytes requested)\n",
  123. memory_address, result, buffer_size);
  124. d_dump(4, i1480->dev, i1480->cmd_buf, result);
  125. itr += result;
  126. memory_address += result;
  127. size -= result;
  128. }
  129. d_fnend(3, i1480->dev, "(%p, 0x%08x, %p, %zu) = %d\n",
  130. i1480, memory_address, buffer, size, result);
  131. return result;
  132. }
  133. /**
  134. * Read a block [max size 512] of the device's memory to @i1480's buffer.
  135. *
  136. * @i1480: i1480 instance
  137. * @memory_address:
  138. * Address where to read from.
  139. * @size: Size to read. Smaller than or equal to 512.
  140. * @returns: >= 0 number of bytes written if ok, < 0 errno code on error.
  141. *
  142. * NOTE: if the memory address or block is incorrect, you might get a
  143. * stall or a different memory read. Caller has to verify the
  144. * memory address and size passed back in the @neh structure.
  145. */
  146. static
  147. int i1480_usb_read(struct i1480 *i1480, u32 addr, size_t size)
  148. {
  149. ssize_t result = 0, bytes = 0;
  150. size_t itr, read_size = i1480->buf_size;
  151. struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
  152. d_fnstart(3, i1480->dev, "(%p, 0x%08x, %zu)\n",
  153. i1480, addr, size);
  154. BUG_ON(size > i1480->buf_size);
  155. BUG_ON(size & 0x3); /* Needs to be a multiple of 4 */
  156. BUG_ON(read_size > 512);
  157. if (addr >= 0x8000d200 && addr < 0x8000d400) /* Yeah, HW quirk */
  158. read_size = 4;
  159. for (itr = 0; itr < size; itr += read_size) {
  160. size_t itr_addr = addr + itr;
  161. size_t itr_size = min(read_size, size - itr);
  162. result = usb_control_msg(
  163. i1480_usb->usb_dev, usb_rcvctrlpipe(i1480_usb->usb_dev, 0),
  164. 0xf0, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  165. cpu_to_le16(itr_addr & 0xffff),
  166. cpu_to_le16((itr_addr >> 16) & 0xffff),
  167. i1480->cmd_buf + itr, itr_size,
  168. 100 /* FIXME: arbitrary */);
  169. if (result < 0) {
  170. dev_err(i1480->dev, "%s: USB read error: %zd\n",
  171. __func__, result);
  172. goto out;
  173. }
  174. if (result != itr_size) {
  175. result = -EIO;
  176. dev_err(i1480->dev,
  177. "%s: partial read got only %zu bytes vs %zu expected\n",
  178. __func__, result, itr_size);
  179. goto out;
  180. }
  181. bytes += result;
  182. }
  183. result = bytes;
  184. out:
  185. d_fnend(3, i1480->dev, "(%p, 0x%08x, %zu) = %zd\n",
  186. i1480, addr, size, result);
  187. if (result > 0)
  188. d_dump(4, i1480->dev, i1480->cmd_buf, result);
  189. return result;
  190. }
  191. /**
  192. * Callback for reads on the notification/event endpoint
  193. *
  194. * Just enables the completion read handler.
  195. */
  196. static
  197. void i1480_usb_neep_cb(struct urb *urb)
  198. {
  199. struct i1480 *i1480 = urb->context;
  200. struct device *dev = i1480->dev;
  201. switch (urb->status) {
  202. case 0:
  203. break;
  204. case -ECONNRESET: /* Not an error, but a controlled situation; */
  205. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  206. dev_dbg(dev, "NEEP: reset/noent %d\n", urb->status);
  207. break;
  208. case -ESHUTDOWN: /* going away! */
  209. dev_dbg(dev, "NEEP: down %d\n", urb->status);
  210. break;
  211. default:
  212. dev_err(dev, "NEEP: unknown status %d\n", urb->status);
  213. break;
  214. }
  215. i1480->evt_result = urb->actual_length;
  216. complete(&i1480->evt_complete);
  217. return;
  218. }
  219. /**
  220. * Wait for the MAC FW to initialize
  221. *
  222. * MAC FW sends a 0xfd/0101/00 notification to EP1 when done
  223. * initializing. Get that notification into i1480->evt_buf; upper layer
  224. * will verify it.
  225. *
  226. * Set i1480->evt_result with the result of getting the event or its
  227. * size (if succesful).
  228. *
  229. * Delivers the data directly to i1480->evt_buf
  230. */
  231. static
  232. int i1480_usb_wait_init_done(struct i1480 *i1480)
  233. {
  234. int result;
  235. struct device *dev = i1480->dev;
  236. struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
  237. struct usb_endpoint_descriptor *epd;
  238. d_fnstart(3, dev, "(%p)\n", i1480);
  239. init_completion(&i1480->evt_complete);
  240. i1480->evt_result = -EINPROGRESS;
  241. epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
  242. usb_fill_int_urb(i1480_usb->neep_urb, i1480_usb->usb_dev,
  243. usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
  244. i1480->evt_buf, i1480->buf_size,
  245. i1480_usb_neep_cb, i1480, epd->bInterval);
  246. result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
  247. if (result < 0) {
  248. dev_err(dev, "init done: cannot submit NEEP read: %d\n",
  249. result);
  250. goto error_submit;
  251. }
  252. /* Wait for the USB callback to get the data */
  253. result = wait_for_completion_interruptible_timeout(
  254. &i1480->evt_complete, HZ);
  255. if (result <= 0) {
  256. result = result == 0 ? -ETIMEDOUT : result;
  257. goto error_wait;
  258. }
  259. usb_kill_urb(i1480_usb->neep_urb);
  260. d_fnend(3, dev, "(%p) = 0\n", i1480);
  261. return 0;
  262. error_wait:
  263. usb_kill_urb(i1480_usb->neep_urb);
  264. error_submit:
  265. i1480->evt_result = result;
  266. d_fnend(3, dev, "(%p) = %d\n", i1480, result);
  267. return result;
  268. }
  269. /**
  270. * Generic function for issuing commands to the i1480
  271. *
  272. * @i1480: i1480 instance
  273. * @cmd_name: Name of the command (for error messages)
  274. * @cmd: Pointer to command buffer
  275. * @cmd_size: Size of the command buffer
  276. * @reply: Buffer for the reply event
  277. * @reply_size: Expected size back (including RCEB); the reply buffer
  278. * is assumed to be as big as this.
  279. * @returns: >= 0 size of the returned event data if ok,
  280. * < 0 errno code on error.
  281. *
  282. * Arms the NE handle, issues the command to the device and checks the
  283. * basics of the reply event.
  284. */
  285. static
  286. int i1480_usb_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size)
  287. {
  288. int result;
  289. struct device *dev = i1480->dev;
  290. struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
  291. struct usb_endpoint_descriptor *epd;
  292. struct uwb_rccb *cmd = i1480->cmd_buf;
  293. u8 iface_no;
  294. d_fnstart(3, dev, "(%p, %s, %zu)\n", i1480, cmd_name, cmd_size);
  295. /* Post a read on the notification & event endpoint */
  296. iface_no = i1480_usb->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  297. epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
  298. usb_fill_int_urb(
  299. i1480_usb->neep_urb, i1480_usb->usb_dev,
  300. usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
  301. i1480->evt_buf, i1480->buf_size,
  302. i1480_usb_neep_cb, i1480, epd->bInterval);
  303. result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
  304. if (result < 0) {
  305. dev_err(dev, "%s: cannot submit NEEP read: %d\n",
  306. cmd_name, result);
  307. goto error_submit_ep1;
  308. }
  309. /* Now post the command on EP0 */
  310. result = usb_control_msg(
  311. i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
  312. WA_EXEC_RC_CMD,
  313. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  314. 0, iface_no,
  315. cmd, cmd_size,
  316. 100 /* FIXME: this is totally arbitrary */);
  317. if (result < 0) {
  318. dev_err(dev, "%s: control request failed: %d\n",
  319. cmd_name, result);
  320. goto error_submit_ep0;
  321. }
  322. d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
  323. i1480, cmd_name, cmd_size, result);
  324. return result;
  325. error_submit_ep0:
  326. usb_kill_urb(i1480_usb->neep_urb);
  327. error_submit_ep1:
  328. d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
  329. i1480, cmd_name, cmd_size, result);
  330. return result;
  331. }
  332. /*
  333. * Probe a i1480 device for uploading firmware.
  334. *
  335. * We attach only to interface #0, which is the radio control interface.
  336. */
  337. static
  338. int i1480_usb_probe(struct usb_interface *iface, const struct usb_device_id *id)
  339. {
  340. struct i1480_usb *i1480_usb;
  341. struct i1480 *i1480;
  342. struct device *dev = &iface->dev;
  343. int result;
  344. result = -ENODEV;
  345. if (iface->cur_altsetting->desc.bInterfaceNumber != 0) {
  346. dev_dbg(dev, "not attaching to iface %d\n",
  347. iface->cur_altsetting->desc.bInterfaceNumber);
  348. goto error;
  349. }
  350. if (iface->num_altsetting > 1
  351. && interface_to_usbdev(iface)->descriptor.idProduct == 0xbabe) {
  352. /* Need altsetting #1 [HW QUIRK] or EP1 won't work */
  353. result = usb_set_interface(interface_to_usbdev(iface), 0, 1);
  354. if (result < 0)
  355. dev_warn(dev,
  356. "can't set altsetting 1 on iface 0: %d\n",
  357. result);
  358. }
  359. result = -ENOMEM;
  360. i1480_usb = kzalloc(sizeof(*i1480_usb), GFP_KERNEL);
  361. if (i1480_usb == NULL) {
  362. dev_err(dev, "Unable to allocate instance\n");
  363. goto error;
  364. }
  365. i1480_usb_init(i1480_usb);
  366. i1480 = &i1480_usb->i1480;
  367. i1480->buf_size = 512;
  368. i1480->cmd_buf = kmalloc(2 * i1480->buf_size, GFP_KERNEL);
  369. if (i1480->cmd_buf == NULL) {
  370. dev_err(dev, "Cannot allocate transfer buffers\n");
  371. result = -ENOMEM;
  372. goto error_buf_alloc;
  373. }
  374. i1480->evt_buf = i1480->cmd_buf + i1480->buf_size;
  375. result = i1480_usb_create(i1480_usb, iface);
  376. if (result < 0) {
  377. dev_err(dev, "Cannot create instance: %d\n", result);
  378. goto error_create;
  379. }
  380. /* setup the fops and upload the firmare */
  381. i1480->pre_fw_name = "i1480-pre-phy-0.0.bin";
  382. i1480->mac_fw_name = "i1480-usb-0.0.bin";
  383. i1480->mac_fw_name_deprecate = "ptc-0.0.bin";
  384. i1480->phy_fw_name = "i1480-phy-0.0.bin";
  385. i1480->dev = &iface->dev;
  386. i1480->write = i1480_usb_write;
  387. i1480->read = i1480_usb_read;
  388. i1480->rc_setup = NULL;
  389. i1480->wait_init_done = i1480_usb_wait_init_done;
  390. i1480->cmd = i1480_usb_cmd;
  391. result = i1480_fw_upload(&i1480_usb->i1480); /* the real thing */
  392. if (result >= 0) {
  393. usb_reset_device(i1480_usb->usb_dev);
  394. result = -ENODEV; /* we don't want to bind to the iface */
  395. }
  396. i1480_usb_destroy(i1480_usb);
  397. error_create:
  398. kfree(i1480->cmd_buf);
  399. error_buf_alloc:
  400. kfree(i1480_usb);
  401. error:
  402. return result;
  403. }
  404. #define i1480_USB_DEV(v, p) \
  405. { \
  406. .match_flags = USB_DEVICE_ID_MATCH_DEVICE \
  407. | USB_DEVICE_ID_MATCH_DEV_INFO \
  408. | USB_DEVICE_ID_MATCH_INT_INFO, \
  409. .idVendor = (v), \
  410. .idProduct = (p), \
  411. .bDeviceClass = 0xff, \
  412. .bDeviceSubClass = 0xff, \
  413. .bDeviceProtocol = 0xff, \
  414. .bInterfaceClass = 0xff, \
  415. .bInterfaceSubClass = 0xff, \
  416. .bInterfaceProtocol = 0xff, \
  417. }
  418. /** USB device ID's that we handle */
  419. static struct usb_device_id i1480_usb_id_table[] = {
  420. i1480_USB_DEV(0x8086, 0xdf3b),
  421. i1480_USB_DEV(0x15a9, 0x0005),
  422. i1480_USB_DEV(0x07d1, 0x3802),
  423. i1480_USB_DEV(0x050d, 0x305a),
  424. i1480_USB_DEV(0x3495, 0x3007),
  425. {},
  426. };
  427. MODULE_DEVICE_TABLE(usb, i1480_usb_id_table);
  428. static struct usb_driver i1480_dfu_driver = {
  429. .name = "i1480-dfu-usb",
  430. .id_table = i1480_usb_id_table,
  431. .probe = i1480_usb_probe,
  432. .disconnect = NULL,
  433. };
  434. /*
  435. * Initialize the i1480 DFU driver.
  436. *
  437. * We also need to register our function for guessing event sizes.
  438. */
  439. static int __init i1480_dfu_driver_init(void)
  440. {
  441. return usb_register(&i1480_dfu_driver);
  442. }
  443. module_init(i1480_dfu_driver_init);
  444. static void __exit i1480_dfu_driver_exit(void)
  445. {
  446. usb_deregister(&i1480_dfu_driver);
  447. }
  448. module_exit(i1480_dfu_driver_exit);
  449. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  450. MODULE_DESCRIPTION("Intel Wireless UWB Link 1480 firmware uploader for USB");
  451. MODULE_LICENSE("GPL");