1b8ea880efcfc8a3b4ee4dd73a52e61a29000e83
[linux-3.10.git] / drivers / net / wireless / zd1211rw / zd_usb.c
1 /* zd_usb.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <asm/unaligned.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/firmware.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/skbuff.h>
26 #include <linux/usb.h>
27 #include <linux/workqueue.h>
28 #include <net/ieee80211.h>
29
30 #include "zd_def.h"
31 #include "zd_netdev.h"
32 #include "zd_mac.h"
33 #include "zd_usb.h"
34 #include "zd_util.h"
35
36 static struct usb_device_id usb_ids[] = {
37         /* ZD1211 */
38         { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
39         { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
40         { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
41         { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
42         { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
43         { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
44         { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
45         { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
46         { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
47         { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
48         { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
49         { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
50         { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
51         { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
52         { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
53         { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
54         { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
55         /* ZD1211B */
56         { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
57         { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
58         { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
59         { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
60         { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
61         { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
62         { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
63         { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
64         /* "Driverless" devices that need ejecting */
65         { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
66         {}
67 };
68
69 MODULE_LICENSE("GPL");
70 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
71 MODULE_AUTHOR("Ulrich Kunitz");
72 MODULE_AUTHOR("Daniel Drake");
73 MODULE_VERSION("1.0");
74 MODULE_DEVICE_TABLE(usb, usb_ids);
75
76 #define FW_ZD1211_PREFIX        "zd1211/zd1211_"
77 #define FW_ZD1211B_PREFIX       "zd1211/zd1211b_"
78
79 /* USB device initialization */
80
81 static int request_fw_file(
82         const struct firmware **fw, const char *name, struct device *device)
83 {
84         int r;
85
86         dev_dbg_f(device, "fw name %s\n", name);
87
88         r = request_firmware(fw, name, device);
89         if (r)
90                 dev_err(device,
91                        "Could not load firmware file %s. Error number %d\n",
92                        name, r);
93         return r;
94 }
95
96 static inline u16 get_bcdDevice(const struct usb_device *udev)
97 {
98         return le16_to_cpu(udev->descriptor.bcdDevice);
99 }
100
101 enum upload_code_flags {
102         REBOOT = 1,
103 };
104
105 /* Ensures that MAX_TRANSFER_SIZE is even. */
106 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
107
108 static int upload_code(struct usb_device *udev,
109         const u8 *data, size_t size, u16 code_offset, int flags)
110 {
111         u8 *p;
112         int r;
113
114         /* USB request blocks need "kmalloced" buffers.
115          */
116         p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
117         if (!p) {
118                 dev_err(&udev->dev, "out of memory\n");
119                 r = -ENOMEM;
120                 goto error;
121         }
122
123         size &= ~1;
124         while (size > 0) {
125                 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
126                         size : MAX_TRANSFER_SIZE;
127
128                 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
129
130                 memcpy(p, data, transfer_size);
131                 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
132                         USB_REQ_FIRMWARE_DOWNLOAD,
133                         USB_DIR_OUT | USB_TYPE_VENDOR,
134                         code_offset, 0, p, transfer_size, 1000 /* ms */);
135                 if (r < 0) {
136                         dev_err(&udev->dev,
137                                "USB control request for firmware upload"
138                                " failed. Error number %d\n", r);
139                         goto error;
140                 }
141                 transfer_size = r & ~1;
142
143                 size -= transfer_size;
144                 data += transfer_size;
145                 code_offset += transfer_size/sizeof(u16);
146         }
147
148         if (flags & REBOOT) {
149                 u8 ret;
150
151                 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
152                         USB_REQ_FIRMWARE_CONFIRM,
153                         USB_DIR_IN | USB_TYPE_VENDOR,
154                         0, 0, &ret, sizeof(ret), 5000 /* ms */);
155                 if (r != sizeof(ret)) {
156                         dev_err(&udev->dev,
157                                 "control request firmeware confirmation failed."
158                                 " Return value %d\n", r);
159                         if (r >= 0)
160                                 r = -ENODEV;
161                         goto error;
162                 }
163                 if (ret & 0x80) {
164                         dev_err(&udev->dev,
165                                 "Internal error while downloading."
166                                 " Firmware confirm return value %#04x\n",
167                                 (unsigned int)ret);
168                         r = -ENODEV;
169                         goto error;
170                 }
171                 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
172                         (unsigned int)ret);
173         }
174
175         r = 0;
176 error:
177         kfree(p);
178         return r;
179 }
180
181 static u16 get_word(const void *data, u16 offset)
182 {
183         const __le16 *p = data;
184         return le16_to_cpu(p[offset]);
185 }
186
187 static char *get_fw_name(char *buffer, size_t size, u8 device_type,
188                        const char* postfix)
189 {
190         scnprintf(buffer, size, "%s%s",
191                 device_type == DEVICE_ZD1211B ?
192                         FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
193                 postfix);
194         return buffer;
195 }
196
197 static int handle_version_mismatch(struct usb_device *udev, u8 device_type,
198         const struct firmware *ub_fw)
199 {
200         const struct firmware *ur_fw = NULL;
201         int offset;
202         int r = 0;
203         char fw_name[128];
204
205         r = request_fw_file(&ur_fw,
206                 get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"),
207                 &udev->dev);
208         if (r)
209                 goto error;
210
211         r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
212         if (r)
213                 goto error;
214
215         offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
216         r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
217                 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
218
219         /* At this point, the vendor driver downloads the whole firmware
220          * image, hacks around with version IDs, and uploads it again,
221          * completely overwriting the boot code. We do not do this here as
222          * it is not required on any tested devices, and it is suspected to
223          * cause problems. */
224 error:
225         release_firmware(ur_fw);
226         return r;
227 }
228
229 static int upload_firmware(struct usb_device *udev, u8 device_type)
230 {
231         int r;
232         u16 fw_bcdDevice;
233         u16 bcdDevice;
234         const struct firmware *ub_fw = NULL;
235         const struct firmware *uph_fw = NULL;
236         char fw_name[128];
237
238         bcdDevice = get_bcdDevice(udev);
239
240         r = request_fw_file(&ub_fw,
241                 get_fw_name(fw_name, sizeof(fw_name), device_type,  "ub"),
242                 &udev->dev);
243         if (r)
244                 goto error;
245
246         fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
247
248         if (fw_bcdDevice != bcdDevice) {
249                 dev_info(&udev->dev,
250                         "firmware version %#06x and device bootcode version "
251                         "%#06x differ\n", fw_bcdDevice, bcdDevice);
252                 if (bcdDevice <= 0x4313)
253                         dev_warn(&udev->dev, "device has old bootcode, please "
254                                 "report success or failure\n");
255
256                 r = handle_version_mismatch(udev, device_type, ub_fw);
257                 if (r)
258                         goto error;
259         } else {
260                 dev_dbg_f(&udev->dev,
261                         "firmware device id %#06x is equal to the "
262                         "actual device id\n", fw_bcdDevice);
263         }
264
265
266         r = request_fw_file(&uph_fw,
267                 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
268                 &udev->dev);
269         if (r)
270                 goto error;
271
272         r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
273         if (r) {
274                 dev_err(&udev->dev,
275                         "Could not upload firmware code uph. Error number %d\n",
276                         r);
277         }
278
279         /* FALL-THROUGH */
280 error:
281         release_firmware(ub_fw);
282         release_firmware(uph_fw);
283         return r;
284 }
285
286 #define urb_dev(urb) (&(urb)->dev->dev)
287
288 static inline void handle_regs_int(struct urb *urb)
289 {
290         struct zd_usb *usb = urb->context;
291         struct zd_usb_interrupt *intr = &usb->intr;
292         int len;
293
294         ZD_ASSERT(in_interrupt());
295         spin_lock(&intr->lock);
296
297         if (intr->read_regs_enabled) {
298                 intr->read_regs.length = len = urb->actual_length;
299
300                 if (len > sizeof(intr->read_regs.buffer))
301                         len = sizeof(intr->read_regs.buffer);
302                 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
303                 intr->read_regs_enabled = 0;
304                 complete(&intr->read_regs.completion);
305                 goto out;
306         }
307
308         dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
309 out:
310         spin_unlock(&intr->lock);
311 }
312
313 static inline void handle_retry_failed_int(struct urb *urb)
314 {
315         dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
316 }
317
318
319 static void int_urb_complete(struct urb *urb)
320 {
321         int r;
322         struct usb_int_header *hdr;
323
324         switch (urb->status) {
325         case 0:
326                 break;
327         case -ESHUTDOWN:
328         case -EINVAL:
329         case -ENODEV:
330         case -ENOENT:
331         case -ECONNRESET:
332         case -EPIPE:
333                 goto kfree;
334         default:
335                 goto resubmit;
336         }
337
338         if (urb->actual_length < sizeof(hdr)) {
339                 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
340                 goto resubmit;
341         }
342
343         hdr = urb->transfer_buffer;
344         if (hdr->type != USB_INT_TYPE) {
345                 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
346                 goto resubmit;
347         }
348
349         switch (hdr->id) {
350         case USB_INT_ID_REGS:
351                 handle_regs_int(urb);
352                 break;
353         case USB_INT_ID_RETRY_FAILED:
354                 handle_retry_failed_int(urb);
355                 break;
356         default:
357                 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
358                         (unsigned int)hdr->id);
359                 goto resubmit;
360         }
361
362 resubmit:
363         r = usb_submit_urb(urb, GFP_ATOMIC);
364         if (r) {
365                 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
366                 goto kfree;
367         }
368         return;
369 kfree:
370         kfree(urb->transfer_buffer);
371 }
372
373 static inline int int_urb_interval(struct usb_device *udev)
374 {
375         switch (udev->speed) {
376         case USB_SPEED_HIGH:
377                 return 4;
378         case USB_SPEED_LOW:
379                 return 10;
380         case USB_SPEED_FULL:
381         default:
382                 return 1;
383         }
384 }
385
386 static inline int usb_int_enabled(struct zd_usb *usb)
387 {
388         unsigned long flags;
389         struct zd_usb_interrupt *intr = &usb->intr;
390         struct urb *urb;
391
392         spin_lock_irqsave(&intr->lock, flags);
393         urb = intr->urb;
394         spin_unlock_irqrestore(&intr->lock, flags);
395         return urb != NULL;
396 }
397
398 int zd_usb_enable_int(struct zd_usb *usb)
399 {
400         int r;
401         struct usb_device *udev;
402         struct zd_usb_interrupt *intr = &usb->intr;
403         void *transfer_buffer = NULL;
404         struct urb *urb;
405
406         dev_dbg_f(zd_usb_dev(usb), "\n");
407
408         urb = usb_alloc_urb(0, GFP_NOFS);
409         if (!urb) {
410                 r = -ENOMEM;
411                 goto out;
412         }
413
414         ZD_ASSERT(!irqs_disabled());
415         spin_lock_irq(&intr->lock);
416         if (intr->urb) {
417                 spin_unlock_irq(&intr->lock);
418                 r = 0;
419                 goto error_free_urb;
420         }
421         intr->urb = urb;
422         spin_unlock_irq(&intr->lock);
423
424         /* TODO: make it a DMA buffer */
425         r = -ENOMEM;
426         transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
427         if (!transfer_buffer) {
428                 dev_dbg_f(zd_usb_dev(usb),
429                         "couldn't allocate transfer_buffer\n");
430                 goto error_set_urb_null;
431         }
432
433         udev = zd_usb_to_usbdev(usb);
434         usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
435                          transfer_buffer, USB_MAX_EP_INT_BUFFER,
436                          int_urb_complete, usb,
437                          intr->interval);
438
439         dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
440         r = usb_submit_urb(urb, GFP_NOFS);
441         if (r) {
442                 dev_dbg_f(zd_usb_dev(usb),
443                          "Couldn't submit urb. Error number %d\n", r);
444                 goto error;
445         }
446
447         return 0;
448 error:
449         kfree(transfer_buffer);
450 error_set_urb_null:
451         spin_lock_irq(&intr->lock);
452         intr->urb = NULL;
453         spin_unlock_irq(&intr->lock);
454 error_free_urb:
455         usb_free_urb(urb);
456 out:
457         return r;
458 }
459
460 void zd_usb_disable_int(struct zd_usb *usb)
461 {
462         unsigned long flags;
463         struct zd_usb_interrupt *intr = &usb->intr;
464         struct urb *urb;
465
466         spin_lock_irqsave(&intr->lock, flags);
467         urb = intr->urb;
468         if (!urb) {
469                 spin_unlock_irqrestore(&intr->lock, flags);
470                 return;
471         }
472         intr->urb = NULL;
473         spin_unlock_irqrestore(&intr->lock, flags);
474
475         usb_kill_urb(urb);
476         dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
477         usb_free_urb(urb);
478 }
479
480 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
481                              unsigned int length)
482 {
483         int i;
484         struct zd_mac *mac = zd_usb_to_mac(usb);
485         const struct rx_length_info *length_info;
486
487         if (length < sizeof(struct rx_length_info)) {
488                 /* It's not a complete packet anyhow. */
489                 return;
490         }
491         length_info = (struct rx_length_info *)
492                 (buffer + length - sizeof(struct rx_length_info));
493
494         /* It might be that three frames are merged into a single URB
495          * transaction. We have to check for the length info tag.
496          *
497          * While testing we discovered that length_info might be unaligned,
498          * because if USB transactions are merged, the last packet will not
499          * be padded. Unaligned access might also happen if the length_info
500          * structure is not present.
501          */
502         if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
503         {
504                 unsigned int l, k, n;
505                 for (i = 0, l = 0;; i++) {
506                         k = le16_to_cpu(get_unaligned(&length_info->length[i]));
507                         if (k == 0)
508                                 return;
509                         n = l+k;
510                         if (n > length)
511                                 return;
512                         zd_mac_rx_irq(mac, buffer+l, k);
513                         if (i >= 2)
514                                 return;
515                         l = (n+3) & ~3;
516                 }
517         } else {
518                 zd_mac_rx_irq(mac, buffer, length);
519         }
520 }
521
522 static void rx_urb_complete(struct urb *urb)
523 {
524         struct zd_usb *usb;
525         struct zd_usb_rx *rx;
526         const u8 *buffer;
527         unsigned int length;
528
529         switch (urb->status) {
530         case 0:
531                 break;
532         case -ESHUTDOWN:
533         case -EINVAL:
534         case -ENODEV:
535         case -ENOENT:
536         case -ECONNRESET:
537         case -EPIPE:
538                 return;
539         default:
540                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
541                 goto resubmit;
542         }
543
544         buffer = urb->transfer_buffer;
545         length = urb->actual_length;
546         usb = urb->context;
547         rx = &usb->rx;
548
549         if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
550                 /* If there is an old first fragment, we don't care. */
551                 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
552                 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
553                 spin_lock(&rx->lock);
554                 memcpy(rx->fragment, buffer, length);
555                 rx->fragment_length = length;
556                 spin_unlock(&rx->lock);
557                 goto resubmit;
558         }
559
560         spin_lock(&rx->lock);
561         if (rx->fragment_length > 0) {
562                 /* We are on a second fragment, we believe */
563                 ZD_ASSERT(length + rx->fragment_length <=
564                           ARRAY_SIZE(rx->fragment));
565                 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
566                 memcpy(rx->fragment+rx->fragment_length, buffer, length);
567                 handle_rx_packet(usb, rx->fragment,
568                                  rx->fragment_length + length);
569                 rx->fragment_length = 0;
570                 spin_unlock(&rx->lock);
571         } else {
572                 spin_unlock(&rx->lock);
573                 handle_rx_packet(usb, buffer, length);
574         }
575
576 resubmit:
577         usb_submit_urb(urb, GFP_ATOMIC);
578 }
579
580 static struct urb *alloc_urb(struct zd_usb *usb)
581 {
582         struct usb_device *udev = zd_usb_to_usbdev(usb);
583         struct urb *urb;
584         void *buffer;
585
586         urb = usb_alloc_urb(0, GFP_NOFS);
587         if (!urb)
588                 return NULL;
589         buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS,
590                                   &urb->transfer_dma);
591         if (!buffer) {
592                 usb_free_urb(urb);
593                 return NULL;
594         }
595
596         usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
597                           buffer, USB_MAX_RX_SIZE,
598                           rx_urb_complete, usb);
599         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
600
601         return urb;
602 }
603
604 static void free_urb(struct urb *urb)
605 {
606         if (!urb)
607                 return;
608         usb_buffer_free(urb->dev, urb->transfer_buffer_length,
609                         urb->transfer_buffer, urb->transfer_dma);
610         usb_free_urb(urb);
611 }
612
613 int zd_usb_enable_rx(struct zd_usb *usb)
614 {
615         int i, r;
616         struct zd_usb_rx *rx = &usb->rx;
617         struct urb **urbs;
618
619         dev_dbg_f(zd_usb_dev(usb), "\n");
620
621         r = -ENOMEM;
622         urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS);
623         if (!urbs)
624                 goto error;
625         for (i = 0; i < URBS_COUNT; i++) {
626                 urbs[i] = alloc_urb(usb);
627                 if (!urbs[i])
628                         goto error;
629         }
630
631         ZD_ASSERT(!irqs_disabled());
632         spin_lock_irq(&rx->lock);
633         if (rx->urbs) {
634                 spin_unlock_irq(&rx->lock);
635                 r = 0;
636                 goto error;
637         }
638         rx->urbs = urbs;
639         rx->urbs_count = URBS_COUNT;
640         spin_unlock_irq(&rx->lock);
641
642         for (i = 0; i < URBS_COUNT; i++) {
643                 r = usb_submit_urb(urbs[i], GFP_NOFS);
644                 if (r)
645                         goto error_submit;
646         }
647
648         return 0;
649 error_submit:
650         for (i = 0; i < URBS_COUNT; i++) {
651                 usb_kill_urb(urbs[i]);
652         }
653         spin_lock_irq(&rx->lock);
654         rx->urbs = NULL;
655         rx->urbs_count = 0;
656         spin_unlock_irq(&rx->lock);
657 error:
658         if (urbs) {
659                 for (i = 0; i < URBS_COUNT; i++)
660                         free_urb(urbs[i]);
661         }
662         return r;
663 }
664
665 void zd_usb_disable_rx(struct zd_usb *usb)
666 {
667         int i;
668         unsigned long flags;
669         struct urb **urbs;
670         unsigned int count;
671         struct zd_usb_rx *rx = &usb->rx;
672
673         spin_lock_irqsave(&rx->lock, flags);
674         urbs = rx->urbs;
675         count = rx->urbs_count;
676         spin_unlock_irqrestore(&rx->lock, flags);
677         if (!urbs)
678                 return;
679
680         for (i = 0; i < count; i++) {
681                 usb_kill_urb(urbs[i]);
682                 free_urb(urbs[i]);
683         }
684         kfree(urbs);
685
686         spin_lock_irqsave(&rx->lock, flags);
687         rx->urbs = NULL;
688         rx->urbs_count = 0;
689         spin_unlock_irqrestore(&rx->lock, flags);
690 }
691
692 static void tx_urb_complete(struct urb *urb)
693 {
694         int r;
695
696         switch (urb->status) {
697         case 0:
698                 break;
699         case -ESHUTDOWN:
700         case -EINVAL:
701         case -ENODEV:
702         case -ENOENT:
703         case -ECONNRESET:
704         case -EPIPE:
705                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
706                 break;
707         default:
708                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
709                 goto resubmit;
710         }
711 free_urb:
712         usb_buffer_free(urb->dev, urb->transfer_buffer_length,
713                         urb->transfer_buffer, urb->transfer_dma);
714         usb_free_urb(urb);
715         return;
716 resubmit:
717         r = usb_submit_urb(urb, GFP_ATOMIC);
718         if (r) {
719                 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
720                 goto free_urb;
721         }
722 }
723
724 /* Puts the frame on the USB endpoint. It doesn't wait for
725  * completion. The frame must contain the control set.
726  */
727 int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
728 {
729         int r;
730         struct usb_device *udev = zd_usb_to_usbdev(usb);
731         struct urb *urb;
732         void *buffer;
733
734         urb = usb_alloc_urb(0, GFP_ATOMIC);
735         if (!urb) {
736                 r = -ENOMEM;
737                 goto out;
738         }
739
740         buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
741                                   &urb->transfer_dma);
742         if (!buffer) {
743                 r = -ENOMEM;
744                 goto error_free_urb;
745         }
746         memcpy(buffer, frame, length);
747
748         usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
749                           buffer, length, tx_urb_complete, NULL);
750         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
751
752         r = usb_submit_urb(urb, GFP_ATOMIC);
753         if (r)
754                 goto error;
755         return 0;
756 error:
757         usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
758                         urb->transfer_dma);
759 error_free_urb:
760         usb_free_urb(urb);
761 out:
762         return r;
763 }
764
765 static inline void init_usb_interrupt(struct zd_usb *usb)
766 {
767         struct zd_usb_interrupt *intr = &usb->intr;
768
769         spin_lock_init(&intr->lock);
770         intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
771         init_completion(&intr->read_regs.completion);
772         intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
773 }
774
775 static inline void init_usb_rx(struct zd_usb *usb)
776 {
777         struct zd_usb_rx *rx = &usb->rx;
778         spin_lock_init(&rx->lock);
779         if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
780                 rx->usb_packet_size = 512;
781         } else {
782                 rx->usb_packet_size = 64;
783         }
784         ZD_ASSERT(rx->fragment_length == 0);
785 }
786
787 static inline void init_usb_tx(struct zd_usb *usb)
788 {
789         /* FIXME: at this point we will allocate a fixed number of urb's for
790          * use in a cyclic scheme */
791 }
792
793 void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
794                  struct usb_interface *intf)
795 {
796         memset(usb, 0, sizeof(*usb));
797         usb->intf = usb_get_intf(intf);
798         usb_set_intfdata(usb->intf, netdev);
799         init_usb_interrupt(usb);
800         init_usb_tx(usb);
801         init_usb_rx(usb);
802 }
803
804 void zd_usb_clear(struct zd_usb *usb)
805 {
806         usb_set_intfdata(usb->intf, NULL);
807         usb_put_intf(usb->intf);
808         ZD_MEMCLEAR(usb, sizeof(*usb));
809         /* FIXME: usb_interrupt, usb_tx, usb_rx? */
810 }
811
812 static const char *speed(enum usb_device_speed speed)
813 {
814         switch (speed) {
815         case USB_SPEED_LOW:
816                 return "low";
817         case USB_SPEED_FULL:
818                 return "full";
819         case USB_SPEED_HIGH:
820                 return "high";
821         default:
822                 return "unknown speed";
823         }
824 }
825
826 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
827 {
828         return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
829                 le16_to_cpu(udev->descriptor.idVendor),
830                 le16_to_cpu(udev->descriptor.idProduct),
831                 get_bcdDevice(udev),
832                 speed(udev->speed));
833 }
834
835 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
836 {
837         struct usb_device *udev = interface_to_usbdev(usb->intf);
838         return scnprint_id(udev, buffer, size);
839 }
840
841 #ifdef DEBUG
842 static void print_id(struct usb_device *udev)
843 {
844         char buffer[40];
845
846         scnprint_id(udev, buffer, sizeof(buffer));
847         buffer[sizeof(buffer)-1] = 0;
848         dev_dbg_f(&udev->dev, "%s\n", buffer);
849 }
850 #else
851 #define print_id(udev) do { } while (0)
852 #endif
853
854 static int eject_installer(struct usb_interface *intf)
855 {
856         struct usb_device *udev = interface_to_usbdev(intf);
857         struct usb_host_interface *iface_desc = &intf->altsetting[0];
858         struct usb_endpoint_descriptor *endpoint;
859         unsigned char *cmd;
860         u8 bulk_out_ep;
861         int r;
862
863         /* Find bulk out endpoint */
864         endpoint = &iface_desc->endpoint[1].desc;
865         if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
866             (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
867             USB_ENDPOINT_XFER_BULK) {
868                 bulk_out_ep = endpoint->bEndpointAddress;
869         } else {
870                 dev_err(&udev->dev,
871                         "zd1211rw: Could not find bulk out endpoint\n");
872                 return -ENODEV;
873         }
874
875         cmd = kzalloc(31, GFP_KERNEL);
876         if (cmd == NULL)
877                 return -ENODEV;
878
879         /* USB bulk command block */
880         cmd[0] = 0x55;  /* bulk command signature */
881         cmd[1] = 0x53;  /* bulk command signature */
882         cmd[2] = 0x42;  /* bulk command signature */
883         cmd[3] = 0x43;  /* bulk command signature */
884         cmd[14] = 6;    /* command length */
885
886         cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
887         cmd[19] = 0x2;  /* eject disc */
888
889         dev_info(&udev->dev, "Ejecting virtual installer media...\n");
890         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
891                 cmd, 31, NULL, 2000);
892         kfree(cmd);
893         if (r)
894                 return r;
895
896         /* At this point, the device disconnects and reconnects with the real
897          * ID numbers. */
898
899         usb_set_intfdata(intf, NULL);
900         return 0;
901 }
902
903 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
904 {
905         int r;
906         struct usb_device *udev = interface_to_usbdev(intf);
907         struct net_device *netdev = NULL;
908
909         print_id(udev);
910
911         if (id->driver_info & DEVICE_INSTALLER)
912                 return eject_installer(intf);
913
914         switch (udev->speed) {
915         case USB_SPEED_LOW:
916         case USB_SPEED_FULL:
917         case USB_SPEED_HIGH:
918                 break;
919         default:
920                 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
921                 r = -ENODEV;
922                 goto error;
923         }
924
925         netdev = zd_netdev_alloc(intf);
926         if (netdev == NULL) {
927                 r = -ENOMEM;
928                 goto error;
929         }
930
931         r = upload_firmware(udev, id->driver_info);
932         if (r) {
933                 dev_err(&intf->dev,
934                        "couldn't load firmware. Error number %d\n", r);
935                 goto error;
936         }
937
938         r = usb_reset_configuration(udev);
939         if (r) {
940                 dev_dbg_f(&intf->dev,
941                         "couldn't reset configuration. Error number %d\n", r);
942                 goto error;
943         }
944
945         /* At this point the interrupt endpoint is not generally enabled. We
946          * save the USB bandwidth until the network device is opened. But
947          * notify that the initialization of the MAC will require the
948          * interrupts to be temporary enabled.
949          */
950         r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info);
951         if (r) {
952                 dev_dbg_f(&intf->dev,
953                          "couldn't initialize mac. Error number %d\n", r);
954                 goto error;
955         }
956
957         r = register_netdev(netdev);
958         if (r) {
959                 dev_dbg_f(&intf->dev,
960                          "couldn't register netdev. Error number %d\n", r);
961                 goto error;
962         }
963
964         dev_dbg_f(&intf->dev, "successful\n");
965         dev_info(&intf->dev,"%s\n", netdev->name);
966         return 0;
967 error:
968         usb_reset_device(interface_to_usbdev(intf));
969         zd_netdev_free(netdev);
970         return r;
971 }
972
973 static void disconnect(struct usb_interface *intf)
974 {
975         struct net_device *netdev = zd_intf_to_netdev(intf);
976         struct zd_mac *mac = zd_netdev_mac(netdev);
977         struct zd_usb *usb = &mac->chip.usb;
978
979         /* Either something really bad happened, or we're just dealing with
980          * a DEVICE_INSTALLER. */
981         if (netdev == NULL)
982                 return;
983
984         dev_dbg_f(zd_usb_dev(usb), "\n");
985
986         zd_netdev_disconnect(netdev);
987
988         /* Just in case something has gone wrong! */
989         zd_usb_disable_rx(usb);
990         zd_usb_disable_int(usb);
991
992         /* If the disconnect has been caused by a removal of the
993          * driver module, the reset allows reloading of the driver. If the
994          * reset will not be executed here, the upload of the firmware in the
995          * probe function caused by the reloading of the driver will fail.
996          */
997         usb_reset_device(interface_to_usbdev(intf));
998
999         zd_netdev_free(netdev);
1000         dev_dbg(&intf->dev, "disconnected\n");
1001 }
1002
1003 static struct usb_driver driver = {
1004         .name           = "zd1211rw",
1005         .id_table       = usb_ids,
1006         .probe          = probe,
1007         .disconnect     = disconnect,
1008 };
1009
1010 struct workqueue_struct *zd_workqueue;
1011
1012 static int __init usb_init(void)
1013 {
1014         int r;
1015
1016         pr_debug("%s usb_init()\n", driver.name);
1017
1018         zd_workqueue = create_singlethread_workqueue(driver.name);
1019         if (zd_workqueue == NULL) {
1020                 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1021                 return -ENOMEM;
1022         }
1023
1024         r = usb_register(&driver);
1025         if (r) {
1026                 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1027                        driver.name, r);
1028                 return r;
1029         }
1030
1031         pr_debug("%s initialized\n", driver.name);
1032         return 0;
1033 }
1034
1035 static void __exit usb_exit(void)
1036 {
1037         pr_debug("%s usb_exit()\n", driver.name);
1038         usb_deregister(&driver);
1039         destroy_workqueue(zd_workqueue);
1040 }
1041
1042 module_init(usb_init);
1043 module_exit(usb_exit);
1044
1045 static int usb_int_regs_length(unsigned int count)
1046 {
1047         return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1048 }
1049
1050 static void prepare_read_regs_int(struct zd_usb *usb)
1051 {
1052         struct zd_usb_interrupt *intr = &usb->intr;
1053
1054         spin_lock_irq(&intr->lock);
1055         intr->read_regs_enabled = 1;
1056         INIT_COMPLETION(intr->read_regs.completion);
1057         spin_unlock_irq(&intr->lock);
1058 }
1059
1060 static void disable_read_regs_int(struct zd_usb *usb)
1061 {
1062         struct zd_usb_interrupt *intr = &usb->intr;
1063
1064         spin_lock_irq(&intr->lock);
1065         intr->read_regs_enabled = 0;
1066         spin_unlock_irq(&intr->lock);
1067 }
1068
1069 static int get_results(struct zd_usb *usb, u16 *values,
1070                        struct usb_req_read_regs *req, unsigned int count)
1071 {
1072         int r;
1073         int i;
1074         struct zd_usb_interrupt *intr = &usb->intr;
1075         struct read_regs_int *rr = &intr->read_regs;
1076         struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1077
1078         spin_lock_irq(&intr->lock);
1079
1080         r = -EIO;
1081         /* The created block size seems to be larger than expected.
1082          * However results appear to be correct.
1083          */
1084         if (rr->length < usb_int_regs_length(count)) {
1085                 dev_dbg_f(zd_usb_dev(usb),
1086                          "error: actual length %d less than expected %d\n",
1087                          rr->length, usb_int_regs_length(count));
1088                 goto error_unlock;
1089         }
1090         if (rr->length > sizeof(rr->buffer)) {
1091                 dev_dbg_f(zd_usb_dev(usb),
1092                          "error: actual length %d exceeds buffer size %zu\n",
1093                          rr->length, sizeof(rr->buffer));
1094                 goto error_unlock;
1095         }
1096
1097         for (i = 0; i < count; i++) {
1098                 struct reg_data *rd = &regs->regs[i];
1099                 if (rd->addr != req->addr[i]) {
1100                         dev_dbg_f(zd_usb_dev(usb),
1101                                  "rd[%d] addr %#06hx expected %#06hx\n", i,
1102                                  le16_to_cpu(rd->addr),
1103                                  le16_to_cpu(req->addr[i]));
1104                         goto error_unlock;
1105                 }
1106                 values[i] = le16_to_cpu(rd->value);
1107         }
1108
1109         r = 0;
1110 error_unlock:
1111         spin_unlock_irq(&intr->lock);
1112         return r;
1113 }
1114
1115 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1116                      const zd_addr_t *addresses, unsigned int count)
1117 {
1118         int r;
1119         int i, req_len, actual_req_len;
1120         struct usb_device *udev;
1121         struct usb_req_read_regs *req = NULL;
1122         unsigned long timeout;
1123
1124         if (count < 1) {
1125                 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1126                 return -EINVAL;
1127         }
1128         if (count > USB_MAX_IOREAD16_COUNT) {
1129                 dev_dbg_f(zd_usb_dev(usb),
1130                          "error: count %u exceeds possible max %u\n",
1131                          count, USB_MAX_IOREAD16_COUNT);
1132                 return -EINVAL;
1133         }
1134         if (in_atomic()) {
1135                 dev_dbg_f(zd_usb_dev(usb),
1136                          "error: io in atomic context not supported\n");
1137                 return -EWOULDBLOCK;
1138         }
1139         if (!usb_int_enabled(usb)) {
1140                  dev_dbg_f(zd_usb_dev(usb),
1141                           "error: usb interrupt not enabled\n");
1142                 return -EWOULDBLOCK;
1143         }
1144
1145         req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1146         req = kmalloc(req_len, GFP_NOFS);
1147         if (!req)
1148                 return -ENOMEM;
1149         req->id = cpu_to_le16(USB_REQ_READ_REGS);
1150         for (i = 0; i < count; i++)
1151                 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1152
1153         udev = zd_usb_to_usbdev(usb);
1154         prepare_read_regs_int(usb);
1155         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1156                          req, req_len, &actual_req_len, 1000 /* ms */);
1157         if (r) {
1158                 dev_dbg_f(zd_usb_dev(usb),
1159                         "error in usb_bulk_msg(). Error number %d\n", r);
1160                 goto error;
1161         }
1162         if (req_len != actual_req_len) {
1163                 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1164                         " req_len %d != actual_req_len %d\n",
1165                         req_len, actual_req_len);
1166                 r = -EIO;
1167                 goto error;
1168         }
1169
1170         timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1171                                               msecs_to_jiffies(1000));
1172         if (!timeout) {
1173                 disable_read_regs_int(usb);
1174                 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1175                 r = -ETIMEDOUT;
1176                 goto error;
1177         }
1178
1179         r = get_results(usb, values, req, count);
1180 error:
1181         kfree(req);
1182         return r;
1183 }
1184
1185 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1186                       unsigned int count)
1187 {
1188         int r;
1189         struct usb_device *udev;
1190         struct usb_req_write_regs *req = NULL;
1191         int i, req_len, actual_req_len;
1192
1193         if (count == 0)
1194                 return 0;
1195         if (count > USB_MAX_IOWRITE16_COUNT) {
1196                 dev_dbg_f(zd_usb_dev(usb),
1197                         "error: count %u exceeds possible max %u\n",
1198                         count, USB_MAX_IOWRITE16_COUNT);
1199                 return -EINVAL;
1200         }
1201         if (in_atomic()) {
1202                 dev_dbg_f(zd_usb_dev(usb),
1203                         "error: io in atomic context not supported\n");
1204                 return -EWOULDBLOCK;
1205         }
1206
1207         req_len = sizeof(struct usb_req_write_regs) +
1208                   count * sizeof(struct reg_data);
1209         req = kmalloc(req_len, GFP_NOFS);
1210         if (!req)
1211                 return -ENOMEM;
1212
1213         req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1214         for (i = 0; i < count; i++) {
1215                 struct reg_data *rw  = &req->reg_writes[i];
1216                 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1217                 rw->value = cpu_to_le16(ioreqs[i].value);
1218         }
1219
1220         udev = zd_usb_to_usbdev(usb);
1221         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1222                          req, req_len, &actual_req_len, 1000 /* ms */);
1223         if (r) {
1224                 dev_dbg_f(zd_usb_dev(usb),
1225                         "error in usb_bulk_msg(). Error number %d\n", r);
1226                 goto error;
1227         }
1228         if (req_len != actual_req_len) {
1229                 dev_dbg_f(zd_usb_dev(usb),
1230                         "error in usb_bulk_msg()"
1231                         " req_len %d != actual_req_len %d\n",
1232                         req_len, actual_req_len);
1233                 r = -EIO;
1234                 goto error;
1235         }
1236
1237         /* FALL-THROUGH with r == 0 */
1238 error:
1239         kfree(req);
1240         return r;
1241 }
1242
1243 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1244 {
1245         int r;
1246         struct usb_device *udev;
1247         struct usb_req_rfwrite *req = NULL;
1248         int i, req_len, actual_req_len;
1249         u16 bit_value_template;
1250
1251         if (in_atomic()) {
1252                 dev_dbg_f(zd_usb_dev(usb),
1253                         "error: io in atomic context not supported\n");
1254                 return -EWOULDBLOCK;
1255         }
1256         if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1257                 dev_dbg_f(zd_usb_dev(usb),
1258                         "error: bits %d are smaller than"
1259                         " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1260                         bits, USB_MIN_RFWRITE_BIT_COUNT);
1261                 return -EINVAL;
1262         }
1263         if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1264                 dev_dbg_f(zd_usb_dev(usb),
1265                         "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1266                         bits, USB_MAX_RFWRITE_BIT_COUNT);
1267                 return -EINVAL;
1268         }
1269 #ifdef DEBUG
1270         if (value & (~0UL << bits)) {
1271                 dev_dbg_f(zd_usb_dev(usb),
1272                         "error: value %#09x has bits >= %d set\n",
1273                         value, bits);
1274                 return -EINVAL;
1275         }
1276 #endif /* DEBUG */
1277
1278         dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1279
1280         r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1281         if (r) {
1282                 dev_dbg_f(zd_usb_dev(usb),
1283                         "error %d: Couldn't read CR203\n", r);
1284                 goto out;
1285         }
1286         bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1287
1288         req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1289         req = kmalloc(req_len, GFP_NOFS);
1290         if (!req)
1291                 return -ENOMEM;
1292
1293         req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1294         /* 1: 3683a, but not used in ZYDAS driver */
1295         req->value = cpu_to_le16(2);
1296         req->bits = cpu_to_le16(bits);
1297
1298         for (i = 0; i < bits; i++) {
1299                 u16 bv = bit_value_template;
1300                 if (value & (1 << (bits-1-i)))
1301                         bv |= RF_DATA;
1302                 req->bit_values[i] = cpu_to_le16(bv);
1303         }
1304
1305         udev = zd_usb_to_usbdev(usb);
1306         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1307                          req, req_len, &actual_req_len, 1000 /* ms */);
1308         if (r) {
1309                 dev_dbg_f(zd_usb_dev(usb),
1310                         "error in usb_bulk_msg(). Error number %d\n", r);
1311                 goto out;
1312         }
1313         if (req_len != actual_req_len) {
1314                 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1315                         " req_len %d != actual_req_len %d\n",
1316                         req_len, actual_req_len);
1317                 r = -EIO;
1318                 goto out;
1319         }
1320
1321         /* FALL-THROUGH with r == 0 */
1322 out:
1323         kfree(req);
1324         return r;
1325 }