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