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