usb.c 13 KB

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