usb.c 14 KB

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