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