usb.c 13 KB

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