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