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