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