2 * Intel Wireless UWB Link 1480
3 * USB SKU firmware upload implementation
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
23 * This driver will prepare the i1480 device to behave as a real
24 * Wireless USB HWA adaptor by uploading the firmware.
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.
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().
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"
48 #include <linux/uwb/debug.h>
53 struct usb_device *usb_dev;
54 struct usb_interface *usb_iface;
55 struct urb *neep_urb; /* URB for reading from EP1 */
60 void i1480_usb_init(struct i1480_usb *i1480_usb)
62 i1480_init(&i1480_usb->i1480);
67 int i1480_usb_create(struct i1480_usb *i1480_usb, struct usb_interface *iface)
69 struct usb_device *usb_dev = interface_to_usbdev(iface);
72 i1480_usb->usb_dev = usb_get_dev(usb_dev); /* bind the USB device */
73 i1480_usb->usb_iface = usb_get_intf(iface);
74 usb_set_intfdata(iface, i1480_usb); /* Bind the driver to iface0 */
75 i1480_usb->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
76 if (i1480_usb->neep_urb == NULL)
81 usb_set_intfdata(iface, NULL);
89 void i1480_usb_destroy(struct i1480_usb *i1480_usb)
91 usb_kill_urb(i1480_usb->neep_urb);
92 usb_free_urb(i1480_usb->neep_urb);
93 usb_set_intfdata(i1480_usb->usb_iface, NULL);
94 usb_put_intf(i1480_usb->usb_iface);
95 usb_put_dev(i1480_usb->usb_dev);
100 * Write a buffer to a memory address in the i1480 device
102 * @i1480: i1480 instance
104 * Address where to write the data buffer to.
105 * @buffer: Buffer to the data
106 * @size: Size of the buffer [has to be < 512].
107 * @returns: 0 if ok, < 0 errno code on error.
109 * Data buffers to USB cannot be on the stack or in vmalloc'ed areas,
110 * so we copy it to the local i1480 buffer before proceeding. In any
111 * case, we have a max size we can send, soooo.
114 int i1480_usb_write(struct i1480 *i1480, u32 memory_address,
115 const void *buffer, size_t size)
118 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
119 size_t buffer_size, itr = 0;
121 d_fnstart(3, i1480->dev, "(%p, 0x%08x, %p, %zu)\n",
122 i1480, memory_address, buffer, size);
123 BUG_ON(size & 0x3); /* Needs to be a multiple of 4 */
125 buffer_size = size < i1480->buf_size ? size : i1480->buf_size;
126 memcpy(i1480->cmd_buf, buffer + itr, buffer_size);
127 result = usb_control_msg(
128 i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
129 0xf0, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
130 cpu_to_le16(memory_address & 0xffff),
131 cpu_to_le16((memory_address >> 16) & 0xffff),
132 i1480->cmd_buf, buffer_size, 100 /* FIXME: arbitrary */);
135 d_printf(3, i1480->dev,
136 "wrote @ 0x%08x %u bytes (of %zu bytes requested)\n",
137 memory_address, result, buffer_size);
138 d_dump(4, i1480->dev, i1480->cmd_buf, result);
140 memory_address += result;
143 d_fnend(3, i1480->dev, "(%p, 0x%08x, %p, %zu) = %d\n",
144 i1480, memory_address, buffer, size, result);
150 * Read a block [max size 512] of the device's memory to @i1480's buffer.
152 * @i1480: i1480 instance
154 * Address where to read from.
155 * @size: Size to read. Smaller than or equal to 512.
156 * @returns: >= 0 number of bytes written if ok, < 0 errno code on error.
158 * NOTE: if the memory address or block is incorrect, you might get a
159 * stall or a different memory read. Caller has to verify the
160 * memory address and size passed back in the @neh structure.
163 int i1480_usb_read(struct i1480 *i1480, u32 addr, size_t size)
165 ssize_t result = 0, bytes = 0;
166 size_t itr, read_size = i1480->buf_size;
167 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
169 d_fnstart(3, i1480->dev, "(%p, 0x%08x, %zu)\n",
171 BUG_ON(size > i1480->buf_size);
172 BUG_ON(size & 0x3); /* Needs to be a multiple of 4 */
173 BUG_ON(read_size > 512);
175 if (addr >= 0x8000d200 && addr < 0x8000d400) /* Yeah, HW quirk */
178 for (itr = 0; itr < size; itr += read_size) {
179 size_t itr_addr = addr + itr;
180 size_t itr_size = min(read_size, size - itr);
181 result = usb_control_msg(
182 i1480_usb->usb_dev, usb_rcvctrlpipe(i1480_usb->usb_dev, 0),
183 0xf0, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184 cpu_to_le16(itr_addr & 0xffff),
185 cpu_to_le16((itr_addr >> 16) & 0xffff),
186 i1480->cmd_buf + itr, itr_size,
187 100 /* FIXME: arbitrary */);
189 dev_err(i1480->dev, "%s: USB read error: %zd\n",
193 if (result != itr_size) {
196 "%s: partial read got only %zu bytes vs %zu expected\n",
197 __func__, result, itr_size);
204 d_fnend(3, i1480->dev, "(%p, 0x%08x, %zu) = %zd\n",
205 i1480, addr, size, result);
207 d_dump(4, i1480->dev, i1480->cmd_buf, result);
213 * Callback for reads on the notification/event endpoint
215 * Just enables the completion read handler.
218 void i1480_usb_neep_cb(struct urb *urb)
220 struct i1480 *i1480 = urb->context;
221 struct device *dev = i1480->dev;
223 switch (urb->status) {
226 case -ECONNRESET: /* Not an error, but a controlled situation; */
227 case -ENOENT: /* (we killed the URB)...so, no broadcast */
228 dev_dbg(dev, "NEEP: reset/noent %d\n", urb->status);
230 case -ESHUTDOWN: /* going away! */
231 dev_dbg(dev, "NEEP: down %d\n", urb->status);
234 dev_err(dev, "NEEP: unknown status %d\n", urb->status);
237 i1480->evt_result = urb->actual_length;
238 complete(&i1480->evt_complete);
244 * Wait for the MAC FW to initialize
246 * MAC FW sends a 0xfd/0101/00 notification to EP1 when done
247 * initializing. Get that notification into i1480->evt_buf; upper layer
250 * Set i1480->evt_result with the result of getting the event or its
251 * size (if succesful).
253 * Delivers the data directly to i1480->evt_buf
256 int i1480_usb_wait_init_done(struct i1480 *i1480)
259 struct device *dev = i1480->dev;
260 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
261 struct usb_endpoint_descriptor *epd;
263 d_fnstart(3, dev, "(%p)\n", i1480);
264 init_completion(&i1480->evt_complete);
265 i1480->evt_result = -EINPROGRESS;
266 epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
267 usb_fill_int_urb(i1480_usb->neep_urb, i1480_usb->usb_dev,
268 usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
269 i1480->evt_buf, i1480->buf_size,
270 i1480_usb_neep_cb, i1480, epd->bInterval);
271 result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
273 dev_err(dev, "init done: cannot submit NEEP read: %d\n",
277 /* Wait for the USB callback to get the data */
278 result = wait_for_completion_interruptible_timeout(
279 &i1480->evt_complete, HZ);
281 result = result == 0 ? -ETIMEDOUT : result;
284 usb_kill_urb(i1480_usb->neep_urb);
285 d_fnend(3, dev, "(%p) = 0\n", i1480);
289 usb_kill_urb(i1480_usb->neep_urb);
291 i1480->evt_result = result;
292 d_fnend(3, dev, "(%p) = %d\n", i1480, result);
298 * Generic function for issuing commands to the i1480
300 * @i1480: i1480 instance
301 * @cmd_name: Name of the command (for error messages)
302 * @cmd: Pointer to command buffer
303 * @cmd_size: Size of the command buffer
304 * @reply: Buffer for the reply event
305 * @reply_size: Expected size back (including RCEB); the reply buffer
306 * is assumed to be as big as this.
307 * @returns: >= 0 size of the returned event data if ok,
308 * < 0 errno code on error.
310 * Arms the NE handle, issues the command to the device and checks the
311 * basics of the reply event.
314 int i1480_usb_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size)
317 struct device *dev = i1480->dev;
318 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
319 struct usb_endpoint_descriptor *epd;
320 struct uwb_rccb *cmd = i1480->cmd_buf;
323 d_fnstart(3, dev, "(%p, %s, %zu)\n", i1480, cmd_name, cmd_size);
324 /* Post a read on the notification & event endpoint */
325 iface_no = i1480_usb->usb_iface->cur_altsetting->desc.bInterfaceNumber;
326 epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
328 i1480_usb->neep_urb, i1480_usb->usb_dev,
329 usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
330 i1480->evt_buf, i1480->buf_size,
331 i1480_usb_neep_cb, i1480, epd->bInterval);
332 result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
334 dev_err(dev, "%s: cannot submit NEEP read: %d\n",
336 goto error_submit_ep1;
338 /* Now post the command on EP0 */
339 result = usb_control_msg(
340 i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
342 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
345 100 /* FIXME: this is totally arbitrary */);
347 dev_err(dev, "%s: control request failed: %d\n",
349 goto error_submit_ep0;
351 d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
352 i1480, cmd_name, cmd_size, result);
356 usb_kill_urb(i1480_usb->neep_urb);
358 d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
359 i1480, cmd_name, cmd_size, result);
365 * Probe a i1480 device for uploading firmware.
367 * We attach only to interface #0, which is the radio control interface.
370 int i1480_usb_probe(struct usb_interface *iface, const struct usb_device_id *id)
372 struct i1480_usb *i1480_usb;
374 struct device *dev = &iface->dev;
378 if (iface->cur_altsetting->desc.bInterfaceNumber != 0) {
379 dev_dbg(dev, "not attaching to iface %d\n",
380 iface->cur_altsetting->desc.bInterfaceNumber);
383 if (iface->num_altsetting > 1
384 && interface_to_usbdev(iface)->descriptor.idProduct == 0xbabe) {
385 /* Need altsetting #1 [HW QUIRK] or EP1 won't work */
386 result = usb_set_interface(interface_to_usbdev(iface), 0, 1);
389 "can't set altsetting 1 on iface 0: %d\n",
394 i1480_usb = kzalloc(sizeof(*i1480_usb), GFP_KERNEL);
395 if (i1480_usb == NULL) {
396 dev_err(dev, "Unable to allocate instance\n");
399 i1480_usb_init(i1480_usb);
401 i1480 = &i1480_usb->i1480;
402 i1480->buf_size = 512;
403 i1480->cmd_buf = kmalloc(2 * i1480->buf_size, GFP_KERNEL);
404 if (i1480->cmd_buf == NULL) {
405 dev_err(dev, "Cannot allocate transfer buffers\n");
407 goto error_buf_alloc;
409 i1480->evt_buf = i1480->cmd_buf + i1480->buf_size;
411 result = i1480_usb_create(i1480_usb, iface);
413 dev_err(dev, "Cannot create instance: %d\n", result);
417 /* setup the fops and upload the firmare */
418 i1480->pre_fw_name = "i1480-pre-phy-0.0.bin";
419 i1480->mac_fw_name = "i1480-usb-0.0.bin";
420 i1480->mac_fw_name_deprecate = "ptc-0.0.bin";
421 i1480->phy_fw_name = "i1480-phy-0.0.bin";
422 i1480->dev = &iface->dev;
423 i1480->write = i1480_usb_write;
424 i1480->read = i1480_usb_read;
425 i1480->rc_setup = NULL;
426 i1480->wait_init_done = i1480_usb_wait_init_done;
427 i1480->cmd = i1480_usb_cmd;
429 result = i1480_fw_upload(&i1480_usb->i1480); /* the real thing */
431 usb_reset_device(i1480_usb->usb_dev);
432 result = -ENODEV; /* we don't want to bind to the iface */
434 i1480_usb_destroy(i1480_usb);
436 kfree(i1480->cmd_buf);
443 #define i1480_USB_DEV(v, p) \
445 .match_flags = USB_DEVICE_ID_MATCH_DEVICE \
446 | USB_DEVICE_ID_MATCH_DEV_INFO \
447 | USB_DEVICE_ID_MATCH_INT_INFO, \
450 .bDeviceClass = 0xff, \
451 .bDeviceSubClass = 0xff, \
452 .bDeviceProtocol = 0xff, \
453 .bInterfaceClass = 0xff, \
454 .bInterfaceSubClass = 0xff, \
455 .bInterfaceProtocol = 0xff, \
459 /** USB device ID's that we handle */
460 static struct usb_device_id i1480_usb_id_table[] = {
461 i1480_USB_DEV(0x8086, 0xdf3b),
462 i1480_USB_DEV(0x15a9, 0x0005),
463 i1480_USB_DEV(0x07d1, 0x3802),
464 i1480_USB_DEV(0x050d, 0x305a),
465 i1480_USB_DEV(0x3495, 0x3007),
468 MODULE_DEVICE_TABLE(usb, i1480_usb_id_table);
471 static struct usb_driver i1480_dfu_driver = {
472 .name = "i1480-dfu-usb",
473 .id_table = i1480_usb_id_table,
474 .probe = i1480_usb_probe,
480 * Initialize the i1480 DFU driver.
482 * We also need to register our function for guessing event sizes.
484 static int __init i1480_dfu_driver_init(void)
486 return usb_register(&i1480_dfu_driver);
488 module_init(i1480_dfu_driver_init);
491 static void __exit i1480_dfu_driver_exit(void)
493 usb_deregister(&i1480_dfu_driver);
495 module_exit(i1480_dfu_driver_exit);
498 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
499 MODULE_DESCRIPTION("Intel Wireless UWB Link 1480 firmware uploader for USB");
500 MODULE_LICENSE("GPL");