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