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