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