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