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