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