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