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