uwb: add the i1480 DFU driver
[safe/jmp/linux-2.6] / drivers / uwb / i1480 / dfu / usb.c
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/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"
46
47 #define D_LOCAL 0
48 #include <linux/uwb/debug.h>
49
50
51 struct i1480_usb {
52         struct i1480 i1480;
53         struct usb_device *usb_dev;
54         struct usb_interface *usb_iface;
55         struct urb *neep_urb;   /* URB for reading from EP1 */
56 };
57
58
59 static
60 void i1480_usb_init(struct i1480_usb *i1480_usb)
61 {
62         i1480_init(&i1480_usb->i1480);
63 }
64
65
66 static
67 int i1480_usb_create(struct i1480_usb *i1480_usb, struct usb_interface *iface)
68 {
69         struct usb_device *usb_dev = interface_to_usbdev(iface);
70         int result = -ENOMEM;
71
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)
77                 goto error;
78         return 0;
79
80 error:
81         usb_set_intfdata(iface, NULL);
82         usb_put_intf(iface);
83         usb_put_dev(usb_dev);
84         return result;
85 }
86
87
88 static
89 void i1480_usb_destroy(struct i1480_usb *i1480_usb)
90 {
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);
96 }
97
98
99 /**
100  * Write a buffer to a memory address in the i1480 device
101  *
102  * @i1480:  i1480 instance
103  * @memory_address:
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.
108  *
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.
112  */
113 static
114 int i1480_usb_write(struct i1480 *i1480, u32 memory_address,
115                     const void *buffer, size_t size)
116 {
117         int result = 0;
118         struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
119         size_t buffer_size, itr = 0;
120
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 */
124         while (size > 0) {
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 */);
133                 if (result < 0)
134                         break;
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);
139                 itr += result;
140                 memory_address += result;
141                 size -= result;
142         }
143         d_fnend(3, i1480->dev, "(%p, 0x%08x, %p, %zu) = %d\n",
144                 i1480, memory_address, buffer, size, result);
145         return result;
146 }
147
148
149 /**
150  * Read a block [max size 512] of the device's memory to @i1480's buffer.
151  *
152  * @i1480: i1480 instance
153  * @memory_address:
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.
157  *
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.
161  */
162 static
163 int i1480_usb_read(struct i1480 *i1480, u32 addr, size_t size)
164 {
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);
168
169         d_fnstart(3, i1480->dev, "(%p, 0x%08x, %zu)\n",
170                   i1480, addr, size);
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);
174
175         if (addr >= 0x8000d200 && addr < 0x8000d400)    /* Yeah, HW quirk */
176                 read_size = 4;
177
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 */);
188                 if (result < 0) {
189                         dev_err(i1480->dev, "%s: USB read error: %zd\n",
190                                 __func__, result);
191                         goto out;
192                 }
193                 if (result != itr_size) {
194                         result = -EIO;
195                         dev_err(i1480->dev,
196                                 "%s: partial read got only %zu bytes vs %zu expected\n",
197                                 __func__, result, itr_size);
198                         goto out;
199                 }
200                 bytes += result;
201         }
202         result = bytes;
203 out:
204         d_fnend(3, i1480->dev, "(%p, 0x%08x, %zu) = %zd\n",
205                 i1480, addr, size, result);
206         if (result > 0)
207                 d_dump(4, i1480->dev, i1480->cmd_buf, result);
208         return result;
209 }
210
211
212 /**
213  * Callback for reads on the notification/event endpoint
214  *
215  * Just enables the completion read handler.
216  */
217 static
218 void i1480_usb_neep_cb(struct urb *urb)
219 {
220         struct i1480 *i1480 = urb->context;
221         struct device *dev = i1480->dev;
222
223         switch (urb->status) {
224         case 0:
225                 break;
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);
229                 break;
230         case -ESHUTDOWN:        /* going away! */
231                 dev_dbg(dev, "NEEP: down %d\n", urb->status);
232                 break;
233         default:
234                 dev_err(dev, "NEEP: unknown status %d\n", urb->status);
235                 break;
236         }
237         i1480->evt_result = urb->actual_length;
238         complete(&i1480->evt_complete);
239         return;
240 }
241
242
243 /**
244  * Wait for the MAC FW to initialize
245  *
246  * MAC FW sends a 0xfd/0101/00 notification to EP1 when done
247  * initializing. Get that notification into i1480->evt_buf; upper layer
248  * will verify it.
249  *
250  * Set i1480->evt_result with the result of getting the event or its
251  * size (if succesful).
252  *
253  * Delivers the data directly to i1480->evt_buf
254  */
255 static
256 int i1480_usb_wait_init_done(struct i1480 *i1480)
257 {
258         int result;
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;
262
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);
272         if (result < 0) {
273                 dev_err(dev, "init done: cannot submit NEEP read: %d\n",
274                         result);
275                 goto error_submit;
276         }
277         /* Wait for the USB callback to get the data */
278         result = wait_for_completion_interruptible_timeout(
279                 &i1480->evt_complete, HZ);
280         if (result <= 0) {
281                 result = result == 0 ? -ETIMEDOUT : result;
282                 goto error_wait;
283         }
284         usb_kill_urb(i1480_usb->neep_urb);
285         d_fnend(3, dev, "(%p) = 0\n", i1480);
286         return 0;
287
288 error_wait:
289         usb_kill_urb(i1480_usb->neep_urb);
290 error_submit:
291         i1480->evt_result = result;
292         d_fnend(3, dev, "(%p) = %d\n", i1480, result);
293         return result;
294 }
295
296
297 /**
298  * Generic function for issuing commands to the i1480
299  *
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.
309  *
310  * Arms the NE handle, issues the command to the device and checks the
311  * basics of the reply event.
312  */
313 static
314 int i1480_usb_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size)
315 {
316         int result;
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;
321         u8 iface_no;
322
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;
327         usb_fill_int_urb(
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);
333         if (result < 0) {
334                 dev_err(dev, "%s: cannot submit NEEP read: %d\n",
335                         cmd_name, result);
336                         goto error_submit_ep1;
337         }
338         /* Now post the command on EP0 */
339         result = usb_control_msg(
340                 i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
341                 WA_EXEC_RC_CMD,
342                 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
343                 0, iface_no,
344                 cmd, cmd_size,
345                 100 /* FIXME: this is totally arbitrary */);
346         if (result < 0) {
347                 dev_err(dev, "%s: control request failed: %d\n",
348                         cmd_name, result);
349                 goto error_submit_ep0;
350         }
351         d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
352                 i1480, cmd_name, cmd_size, result);
353         return result;
354
355 error_submit_ep0:
356         usb_kill_urb(i1480_usb->neep_urb);
357 error_submit_ep1:
358         d_fnend(3, dev, "(%p, %s, %zu) = %d\n",
359                 i1480, cmd_name, cmd_size, result);
360         return result;
361 }
362
363
364 /*
365  * Probe a i1480 device for uploading firmware.
366  *
367  * We attach only to interface #0, which is the radio control interface.
368  */
369 static
370 int i1480_usb_probe(struct usb_interface *iface, const struct usb_device_id *id)
371 {
372         struct i1480_usb *i1480_usb;
373         struct i1480 *i1480;
374         struct device *dev = &iface->dev;
375         int result;
376
377         result = -ENODEV;
378         if (iface->cur_altsetting->desc.bInterfaceNumber != 0) {
379                 dev_dbg(dev, "not attaching to iface %d\n",
380                         iface->cur_altsetting->desc.bInterfaceNumber);
381                 goto error;
382         }
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);
387                 if (result < 0)
388                         dev_warn(dev,
389                                  "can't set altsetting 1 on iface 0: %d\n",
390                                  result);
391         }
392
393         result = -ENOMEM;
394         i1480_usb = kzalloc(sizeof(*i1480_usb), GFP_KERNEL);
395         if (i1480_usb == NULL) {
396                 dev_err(dev, "Unable to allocate instance\n");
397                 goto error;
398         }
399         i1480_usb_init(i1480_usb);
400
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");
406                 result = -ENOMEM;
407                 goto error_buf_alloc;
408         }
409         i1480->evt_buf = i1480->cmd_buf + i1480->buf_size;
410
411         result = i1480_usb_create(i1480_usb, iface);
412         if (result < 0) {
413                 dev_err(dev, "Cannot create instance: %d\n", result);
414                 goto error_create;
415         }
416
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;
428
429         result = i1480_fw_upload(&i1480_usb->i1480);    /* the real thing */
430         if (result >= 0) {
431                 usb_reset_device(i1480_usb->usb_dev);
432                 result = -ENODEV;       /* we don't want to bind to the iface */
433         }
434         i1480_usb_destroy(i1480_usb);
435 error_create:
436         kfree(i1480->cmd_buf);
437 error_buf_alloc:
438         kfree(i1480_usb);
439 error:
440         return result;
441 }
442
443 #define i1480_USB_DEV(v, p)                             \
444 {                                                       \
445         .match_flags = USB_DEVICE_ID_MATCH_DEVICE       \
446                  | USB_DEVICE_ID_MATCH_DEV_INFO         \
447                  | USB_DEVICE_ID_MATCH_INT_INFO,        \
448         .idVendor = (v),                                \
449         .idProduct = (p),                               \
450         .bDeviceClass = 0xff,                           \
451         .bDeviceSubClass = 0xff,                        \
452         .bDeviceProtocol = 0xff,                        \
453         .bInterfaceClass = 0xff,                        \
454         .bInterfaceSubClass = 0xff,                     \
455         .bInterfaceProtocol = 0xff,                     \
456 }
457
458
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),
466         {},
467 };
468 MODULE_DEVICE_TABLE(usb, i1480_usb_id_table);
469
470
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,
475         .disconnect =   NULL,
476 };
477
478
479 /*
480  * Initialize the i1480 DFU driver.
481  *
482  * We also need to register our function for guessing event sizes.
483  */
484 static int __init i1480_dfu_driver_init(void)
485 {
486         return usb_register(&i1480_dfu_driver);
487 }
488 module_init(i1480_dfu_driver_init);
489
490
491 static void __exit i1480_dfu_driver_exit(void)
492 {
493         usb_deregister(&i1480_dfu_driver);
494 }
495 module_exit(i1480_dfu_driver_exit);
496
497
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");