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