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