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