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