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