spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / net / wireless / zd1211rw / zd_usb.c
blob785bdbe38f2a0cb6f67d6ad56a3c0ad2d5980490
1 /* ZD1211 USB-WLAN driver for Linux
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/firmware.h>
25 #include <linux/device.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/skbuff.h>
29 #include <linux/usb.h>
30 #include <linux/workqueue.h>
31 #include <linux/module.h>
32 #include <net/mac80211.h>
33 #include <asm/unaligned.h>
35 #include "zd_def.h"
36 #include "zd_mac.h"
37 #include "zd_usb.h"
39 static struct usb_device_id usb_ids[] = {
40 /* ZD1211 */
41 { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
55 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
57 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
58 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
59 { USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 },
60 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
61 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
62 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
63 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
64 { USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 },
65 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
66 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
67 /* ZD1211B */
68 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B },
70 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
71 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
73 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
80 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
81 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B },
82 { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B },
83 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
84 { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B },
85 { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
86 { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
87 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
88 { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
89 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
90 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
91 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
92 { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
93 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
94 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
95 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
96 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
97 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
98 { USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B },
99 /* "Driverless" devices that need ejecting */
100 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
101 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
105 MODULE_LICENSE("GPL");
106 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
107 MODULE_AUTHOR("Ulrich Kunitz");
108 MODULE_AUTHOR("Daniel Drake");
109 MODULE_VERSION("1.0");
110 MODULE_DEVICE_TABLE(usb, usb_ids);
112 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
113 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
115 static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
116 unsigned int count);
118 /* USB device initialization */
119 static void int_urb_complete(struct urb *urb);
121 static int request_fw_file(
122 const struct firmware **fw, const char *name, struct device *device)
124 int r;
126 dev_dbg_f(device, "fw name %s\n", name);
128 r = request_firmware(fw, name, device);
129 if (r)
130 dev_err(device,
131 "Could not load firmware file %s. Error number %d\n",
132 name, r);
133 return r;
136 static inline u16 get_bcdDevice(const struct usb_device *udev)
138 return le16_to_cpu(udev->descriptor.bcdDevice);
141 enum upload_code_flags {
142 REBOOT = 1,
145 /* Ensures that MAX_TRANSFER_SIZE is even. */
146 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
148 static int upload_code(struct usb_device *udev,
149 const u8 *data, size_t size, u16 code_offset, int flags)
151 u8 *p;
152 int r;
154 /* USB request blocks need "kmalloced" buffers.
156 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
157 if (!p) {
158 dev_err(&udev->dev, "out of memory\n");
159 r = -ENOMEM;
160 goto error;
163 size &= ~1;
164 while (size > 0) {
165 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
166 size : MAX_TRANSFER_SIZE;
168 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
170 memcpy(p, data, transfer_size);
171 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
172 USB_REQ_FIRMWARE_DOWNLOAD,
173 USB_DIR_OUT | USB_TYPE_VENDOR,
174 code_offset, 0, p, transfer_size, 1000 /* ms */);
175 if (r < 0) {
176 dev_err(&udev->dev,
177 "USB control request for firmware upload"
178 " failed. Error number %d\n", r);
179 goto error;
181 transfer_size = r & ~1;
183 size -= transfer_size;
184 data += transfer_size;
185 code_offset += transfer_size/sizeof(u16);
188 if (flags & REBOOT) {
189 u8 ret;
191 /* Use "DMA-aware" buffer. */
192 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
193 USB_REQ_FIRMWARE_CONFIRM,
194 USB_DIR_IN | USB_TYPE_VENDOR,
195 0, 0, p, sizeof(ret), 5000 /* ms */);
196 if (r != sizeof(ret)) {
197 dev_err(&udev->dev,
198 "control request firmeware confirmation failed."
199 " Return value %d\n", r);
200 if (r >= 0)
201 r = -ENODEV;
202 goto error;
204 ret = p[0];
205 if (ret & 0x80) {
206 dev_err(&udev->dev,
207 "Internal error while downloading."
208 " Firmware confirm return value %#04x\n",
209 (unsigned int)ret);
210 r = -ENODEV;
211 goto error;
213 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
214 (unsigned int)ret);
217 r = 0;
218 error:
219 kfree(p);
220 return r;
223 static u16 get_word(const void *data, u16 offset)
225 const __le16 *p = data;
226 return le16_to_cpu(p[offset]);
229 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
230 const char* postfix)
232 scnprintf(buffer, size, "%s%s",
233 usb->is_zd1211b ?
234 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
235 postfix);
236 return buffer;
239 static int handle_version_mismatch(struct zd_usb *usb,
240 const struct firmware *ub_fw)
242 struct usb_device *udev = zd_usb_to_usbdev(usb);
243 const struct firmware *ur_fw = NULL;
244 int offset;
245 int r = 0;
246 char fw_name[128];
248 r = request_fw_file(&ur_fw,
249 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
250 &udev->dev);
251 if (r)
252 goto error;
254 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
255 if (r)
256 goto error;
258 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
259 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
260 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
262 /* At this point, the vendor driver downloads the whole firmware
263 * image, hacks around with version IDs, and uploads it again,
264 * completely overwriting the boot code. We do not do this here as
265 * it is not required on any tested devices, and it is suspected to
266 * cause problems. */
267 error:
268 release_firmware(ur_fw);
269 return r;
272 static int upload_firmware(struct zd_usb *usb)
274 int r;
275 u16 fw_bcdDevice;
276 u16 bcdDevice;
277 struct usb_device *udev = zd_usb_to_usbdev(usb);
278 const struct firmware *ub_fw = NULL;
279 const struct firmware *uph_fw = NULL;
280 char fw_name[128];
282 bcdDevice = get_bcdDevice(udev);
284 r = request_fw_file(&ub_fw,
285 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
286 &udev->dev);
287 if (r)
288 goto error;
290 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
292 if (fw_bcdDevice != bcdDevice) {
293 dev_info(&udev->dev,
294 "firmware version %#06x and device bootcode version "
295 "%#06x differ\n", fw_bcdDevice, bcdDevice);
296 if (bcdDevice <= 0x4313)
297 dev_warn(&udev->dev, "device has old bootcode, please "
298 "report success or failure\n");
300 r = handle_version_mismatch(usb, ub_fw);
301 if (r)
302 goto error;
303 } else {
304 dev_dbg_f(&udev->dev,
305 "firmware device id %#06x is equal to the "
306 "actual device id\n", fw_bcdDevice);
310 r = request_fw_file(&uph_fw,
311 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
312 &udev->dev);
313 if (r)
314 goto error;
316 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
317 if (r) {
318 dev_err(&udev->dev,
319 "Could not upload firmware code uph. Error number %d\n",
323 /* FALL-THROUGH */
324 error:
325 release_firmware(ub_fw);
326 release_firmware(uph_fw);
327 return r;
330 MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur");
331 MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur");
332 MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub");
333 MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub");
334 MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr");
335 MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr");
337 /* Read data from device address space using "firmware interface" which does
338 * not require firmware to be loaded. */
339 int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
341 int r;
342 struct usb_device *udev = zd_usb_to_usbdev(usb);
343 u8 *buf;
345 /* Use "DMA-aware" buffer. */
346 buf = kmalloc(len, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
349 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
350 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
351 buf, len, 5000);
352 if (r < 0) {
353 dev_err(&udev->dev,
354 "read over firmware interface failed: %d\n", r);
355 goto exit;
356 } else if (r != len) {
357 dev_err(&udev->dev,
358 "incomplete read over firmware interface: %d/%d\n",
359 r, len);
360 r = -EIO;
361 goto exit;
363 r = 0;
364 memcpy(data, buf, len);
365 exit:
366 kfree(buf);
367 return r;
370 #define urb_dev(urb) (&(urb)->dev->dev)
372 static inline void handle_regs_int_override(struct urb *urb)
374 struct zd_usb *usb = urb->context;
375 struct zd_usb_interrupt *intr = &usb->intr;
377 spin_lock(&intr->lock);
378 if (atomic_read(&intr->read_regs_enabled)) {
379 atomic_set(&intr->read_regs_enabled, 0);
380 intr->read_regs_int_overridden = 1;
381 complete(&intr->read_regs.completion);
383 spin_unlock(&intr->lock);
386 static inline void handle_regs_int(struct urb *urb)
388 struct zd_usb *usb = urb->context;
389 struct zd_usb_interrupt *intr = &usb->intr;
390 int len;
391 u16 int_num;
393 ZD_ASSERT(in_interrupt());
394 spin_lock(&intr->lock);
396 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
397 if (int_num == CR_INTERRUPT) {
398 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
399 spin_lock(&mac->lock);
400 memcpy(&mac->intr_buffer, urb->transfer_buffer,
401 USB_MAX_EP_INT_BUFFER);
402 spin_unlock(&mac->lock);
403 schedule_work(&mac->process_intr);
404 } else if (atomic_read(&intr->read_regs_enabled)) {
405 len = urb->actual_length;
406 intr->read_regs.length = urb->actual_length;
407 if (len > sizeof(intr->read_regs.buffer))
408 len = sizeof(intr->read_regs.buffer);
410 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
412 /* Sometimes USB_INT_ID_REGS is not overridden, but comes after
413 * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this
414 * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of
415 * retry unhandled. Next read-reg command then might catch
416 * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads.
418 if (!check_read_regs(usb, intr->read_regs.req,
419 intr->read_regs.req_count))
420 goto out;
422 atomic_set(&intr->read_regs_enabled, 0);
423 intr->read_regs_int_overridden = 0;
424 complete(&intr->read_regs.completion);
426 goto out;
429 out:
430 spin_unlock(&intr->lock);
432 /* CR_INTERRUPT might override read_reg too. */
433 if (int_num == CR_INTERRUPT && atomic_read(&intr->read_regs_enabled))
434 handle_regs_int_override(urb);
437 static void int_urb_complete(struct urb *urb)
439 int r;
440 struct usb_int_header *hdr;
441 struct zd_usb *usb;
442 struct zd_usb_interrupt *intr;
444 switch (urb->status) {
445 case 0:
446 break;
447 case -ESHUTDOWN:
448 case -EINVAL:
449 case -ENODEV:
450 case -ENOENT:
451 case -ECONNRESET:
452 case -EPIPE:
453 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
454 return;
455 default:
456 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
457 goto resubmit;
460 if (urb->actual_length < sizeof(hdr)) {
461 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
462 goto resubmit;
465 hdr = urb->transfer_buffer;
466 if (hdr->type != USB_INT_TYPE) {
467 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
468 goto resubmit;
471 /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override
472 * pending USB_INT_ID_REGS causing read command timeout.
474 usb = urb->context;
475 intr = &usb->intr;
476 if (hdr->id != USB_INT_ID_REGS && atomic_read(&intr->read_regs_enabled))
477 handle_regs_int_override(urb);
479 switch (hdr->id) {
480 case USB_INT_ID_REGS:
481 handle_regs_int(urb);
482 break;
483 case USB_INT_ID_RETRY_FAILED:
484 zd_mac_tx_failed(urb);
485 break;
486 default:
487 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
488 (unsigned int)hdr->id);
489 goto resubmit;
492 resubmit:
493 r = usb_submit_urb(urb, GFP_ATOMIC);
494 if (r) {
495 dev_dbg_f(urb_dev(urb), "error: resubmit urb %p err code %d\n",
496 urb, r);
497 /* TODO: add worker to reset intr->urb */
499 return;
502 static inline int int_urb_interval(struct usb_device *udev)
504 switch (udev->speed) {
505 case USB_SPEED_HIGH:
506 return 4;
507 case USB_SPEED_LOW:
508 return 10;
509 case USB_SPEED_FULL:
510 default:
511 return 1;
515 static inline int usb_int_enabled(struct zd_usb *usb)
517 unsigned long flags;
518 struct zd_usb_interrupt *intr = &usb->intr;
519 struct urb *urb;
521 spin_lock_irqsave(&intr->lock, flags);
522 urb = intr->urb;
523 spin_unlock_irqrestore(&intr->lock, flags);
524 return urb != NULL;
527 int zd_usb_enable_int(struct zd_usb *usb)
529 int r;
530 struct usb_device *udev = zd_usb_to_usbdev(usb);
531 struct zd_usb_interrupt *intr = &usb->intr;
532 struct urb *urb;
534 dev_dbg_f(zd_usb_dev(usb), "\n");
536 urb = usb_alloc_urb(0, GFP_KERNEL);
537 if (!urb) {
538 r = -ENOMEM;
539 goto out;
542 ZD_ASSERT(!irqs_disabled());
543 spin_lock_irq(&intr->lock);
544 if (intr->urb) {
545 spin_unlock_irq(&intr->lock);
546 r = 0;
547 goto error_free_urb;
549 intr->urb = urb;
550 spin_unlock_irq(&intr->lock);
552 r = -ENOMEM;
553 intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER,
554 GFP_KERNEL, &intr->buffer_dma);
555 if (!intr->buffer) {
556 dev_dbg_f(zd_usb_dev(usb),
557 "couldn't allocate transfer_buffer\n");
558 goto error_set_urb_null;
561 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
562 intr->buffer, USB_MAX_EP_INT_BUFFER,
563 int_urb_complete, usb,
564 intr->interval);
565 urb->transfer_dma = intr->buffer_dma;
566 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
568 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
569 r = usb_submit_urb(urb, GFP_KERNEL);
570 if (r) {
571 dev_dbg_f(zd_usb_dev(usb),
572 "Couldn't submit urb. Error number %d\n", r);
573 goto error;
576 return 0;
577 error:
578 usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
579 intr->buffer, intr->buffer_dma);
580 error_set_urb_null:
581 spin_lock_irq(&intr->lock);
582 intr->urb = NULL;
583 spin_unlock_irq(&intr->lock);
584 error_free_urb:
585 usb_free_urb(urb);
586 out:
587 return r;
590 void zd_usb_disable_int(struct zd_usb *usb)
592 unsigned long flags;
593 struct usb_device *udev = zd_usb_to_usbdev(usb);
594 struct zd_usb_interrupt *intr = &usb->intr;
595 struct urb *urb;
596 void *buffer;
597 dma_addr_t buffer_dma;
599 spin_lock_irqsave(&intr->lock, flags);
600 urb = intr->urb;
601 if (!urb) {
602 spin_unlock_irqrestore(&intr->lock, flags);
603 return;
605 intr->urb = NULL;
606 buffer = intr->buffer;
607 buffer_dma = intr->buffer_dma;
608 intr->buffer = NULL;
609 spin_unlock_irqrestore(&intr->lock, flags);
611 usb_kill_urb(urb);
612 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
613 usb_free_urb(urb);
615 if (buffer)
616 usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
617 buffer, buffer_dma);
620 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
621 unsigned int length)
623 int i;
624 const struct rx_length_info *length_info;
626 if (length < sizeof(struct rx_length_info)) {
627 /* It's not a complete packet anyhow. */
628 dev_dbg_f(zd_usb_dev(usb), "invalid, small RX packet : %d\n",
629 length);
630 return;
632 length_info = (struct rx_length_info *)
633 (buffer + length - sizeof(struct rx_length_info));
635 /* It might be that three frames are merged into a single URB
636 * transaction. We have to check for the length info tag.
638 * While testing we discovered that length_info might be unaligned,
639 * because if USB transactions are merged, the last packet will not
640 * be padded. Unaligned access might also happen if the length_info
641 * structure is not present.
643 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
645 unsigned int l, k, n;
646 for (i = 0, l = 0;; i++) {
647 k = get_unaligned_le16(&length_info->length[i]);
648 if (k == 0)
649 return;
650 n = l+k;
651 if (n > length)
652 return;
653 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
654 if (i >= 2)
655 return;
656 l = (n+3) & ~3;
658 } else {
659 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
663 static void rx_urb_complete(struct urb *urb)
665 int r;
666 struct zd_usb *usb;
667 struct zd_usb_rx *rx;
668 const u8 *buffer;
669 unsigned int length;
671 switch (urb->status) {
672 case 0:
673 break;
674 case -ESHUTDOWN:
675 case -EINVAL:
676 case -ENODEV:
677 case -ENOENT:
678 case -ECONNRESET:
679 case -EPIPE:
680 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
681 return;
682 default:
683 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
684 goto resubmit;
687 buffer = urb->transfer_buffer;
688 length = urb->actual_length;
689 usb = urb->context;
690 rx = &usb->rx;
692 tasklet_schedule(&rx->reset_timer_tasklet);
694 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
695 /* If there is an old first fragment, we don't care. */
696 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
697 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
698 spin_lock(&rx->lock);
699 memcpy(rx->fragment, buffer, length);
700 rx->fragment_length = length;
701 spin_unlock(&rx->lock);
702 goto resubmit;
705 spin_lock(&rx->lock);
706 if (rx->fragment_length > 0) {
707 /* We are on a second fragment, we believe */
708 ZD_ASSERT(length + rx->fragment_length <=
709 ARRAY_SIZE(rx->fragment));
710 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
711 memcpy(rx->fragment+rx->fragment_length, buffer, length);
712 handle_rx_packet(usb, rx->fragment,
713 rx->fragment_length + length);
714 rx->fragment_length = 0;
715 spin_unlock(&rx->lock);
716 } else {
717 spin_unlock(&rx->lock);
718 handle_rx_packet(usb, buffer, length);
721 resubmit:
722 r = usb_submit_urb(urb, GFP_ATOMIC);
723 if (r)
724 dev_dbg_f(urb_dev(urb), "urb %p resubmit error %d\n", urb, r);
727 static struct urb *alloc_rx_urb(struct zd_usb *usb)
729 struct usb_device *udev = zd_usb_to_usbdev(usb);
730 struct urb *urb;
731 void *buffer;
733 urb = usb_alloc_urb(0, GFP_KERNEL);
734 if (!urb)
735 return NULL;
736 buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
737 &urb->transfer_dma);
738 if (!buffer) {
739 usb_free_urb(urb);
740 return NULL;
743 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
744 buffer, USB_MAX_RX_SIZE,
745 rx_urb_complete, usb);
746 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
748 return urb;
751 static void free_rx_urb(struct urb *urb)
753 if (!urb)
754 return;
755 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
756 urb->transfer_buffer, urb->transfer_dma);
757 usb_free_urb(urb);
760 static int __zd_usb_enable_rx(struct zd_usb *usb)
762 int i, r;
763 struct zd_usb_rx *rx = &usb->rx;
764 struct urb **urbs;
766 dev_dbg_f(zd_usb_dev(usb), "\n");
768 r = -ENOMEM;
769 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
770 if (!urbs)
771 goto error;
772 for (i = 0; i < RX_URBS_COUNT; i++) {
773 urbs[i] = alloc_rx_urb(usb);
774 if (!urbs[i])
775 goto error;
778 ZD_ASSERT(!irqs_disabled());
779 spin_lock_irq(&rx->lock);
780 if (rx->urbs) {
781 spin_unlock_irq(&rx->lock);
782 r = 0;
783 goto error;
785 rx->urbs = urbs;
786 rx->urbs_count = RX_URBS_COUNT;
787 spin_unlock_irq(&rx->lock);
789 for (i = 0; i < RX_URBS_COUNT; i++) {
790 r = usb_submit_urb(urbs[i], GFP_KERNEL);
791 if (r)
792 goto error_submit;
795 return 0;
796 error_submit:
797 for (i = 0; i < RX_URBS_COUNT; i++) {
798 usb_kill_urb(urbs[i]);
800 spin_lock_irq(&rx->lock);
801 rx->urbs = NULL;
802 rx->urbs_count = 0;
803 spin_unlock_irq(&rx->lock);
804 error:
805 if (urbs) {
806 for (i = 0; i < RX_URBS_COUNT; i++)
807 free_rx_urb(urbs[i]);
809 return r;
812 int zd_usb_enable_rx(struct zd_usb *usb)
814 int r;
815 struct zd_usb_rx *rx = &usb->rx;
817 mutex_lock(&rx->setup_mutex);
818 r = __zd_usb_enable_rx(usb);
819 mutex_unlock(&rx->setup_mutex);
821 zd_usb_reset_rx_idle_timer(usb);
823 return r;
826 static void __zd_usb_disable_rx(struct zd_usb *usb)
828 int i;
829 unsigned long flags;
830 struct urb **urbs;
831 unsigned int count;
832 struct zd_usb_rx *rx = &usb->rx;
834 spin_lock_irqsave(&rx->lock, flags);
835 urbs = rx->urbs;
836 count = rx->urbs_count;
837 spin_unlock_irqrestore(&rx->lock, flags);
838 if (!urbs)
839 return;
841 for (i = 0; i < count; i++) {
842 usb_kill_urb(urbs[i]);
843 free_rx_urb(urbs[i]);
845 kfree(urbs);
847 spin_lock_irqsave(&rx->lock, flags);
848 rx->urbs = NULL;
849 rx->urbs_count = 0;
850 spin_unlock_irqrestore(&rx->lock, flags);
853 void zd_usb_disable_rx(struct zd_usb *usb)
855 struct zd_usb_rx *rx = &usb->rx;
857 mutex_lock(&rx->setup_mutex);
858 __zd_usb_disable_rx(usb);
859 mutex_unlock(&rx->setup_mutex);
861 tasklet_kill(&rx->reset_timer_tasklet);
862 cancel_delayed_work_sync(&rx->idle_work);
865 static void zd_usb_reset_rx(struct zd_usb *usb)
867 bool do_reset;
868 struct zd_usb_rx *rx = &usb->rx;
869 unsigned long flags;
871 mutex_lock(&rx->setup_mutex);
873 spin_lock_irqsave(&rx->lock, flags);
874 do_reset = rx->urbs != NULL;
875 spin_unlock_irqrestore(&rx->lock, flags);
877 if (do_reset) {
878 __zd_usb_disable_rx(usb);
879 __zd_usb_enable_rx(usb);
882 mutex_unlock(&rx->setup_mutex);
884 if (do_reset)
885 zd_usb_reset_rx_idle_timer(usb);
889 * zd_usb_disable_tx - disable transmission
890 * @usb: the zd1211rw-private USB structure
892 * Frees all URBs in the free list and marks the transmission as disabled.
894 void zd_usb_disable_tx(struct zd_usb *usb)
896 struct zd_usb_tx *tx = &usb->tx;
897 unsigned long flags;
899 atomic_set(&tx->enabled, 0);
901 /* kill all submitted tx-urbs */
902 usb_kill_anchored_urbs(&tx->submitted);
904 spin_lock_irqsave(&tx->lock, flags);
905 WARN_ON(!skb_queue_empty(&tx->submitted_skbs));
906 WARN_ON(tx->submitted_urbs != 0);
907 tx->submitted_urbs = 0;
908 spin_unlock_irqrestore(&tx->lock, flags);
910 /* The stopped state is ignored, relying on ieee80211_wake_queues()
911 * in a potentionally following zd_usb_enable_tx().
916 * zd_usb_enable_tx - enables transmission
917 * @usb: a &struct zd_usb pointer
919 * This function enables transmission and prepares the &zd_usb_tx data
920 * structure.
922 void zd_usb_enable_tx(struct zd_usb *usb)
924 unsigned long flags;
925 struct zd_usb_tx *tx = &usb->tx;
927 spin_lock_irqsave(&tx->lock, flags);
928 atomic_set(&tx->enabled, 1);
929 tx->submitted_urbs = 0;
930 ieee80211_wake_queues(zd_usb_to_hw(usb));
931 tx->stopped = 0;
932 spin_unlock_irqrestore(&tx->lock, flags);
935 static void tx_dec_submitted_urbs(struct zd_usb *usb)
937 struct zd_usb_tx *tx = &usb->tx;
938 unsigned long flags;
940 spin_lock_irqsave(&tx->lock, flags);
941 --tx->submitted_urbs;
942 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
943 ieee80211_wake_queues(zd_usb_to_hw(usb));
944 tx->stopped = 0;
946 spin_unlock_irqrestore(&tx->lock, flags);
949 static void tx_inc_submitted_urbs(struct zd_usb *usb)
951 struct zd_usb_tx *tx = &usb->tx;
952 unsigned long flags;
954 spin_lock_irqsave(&tx->lock, flags);
955 ++tx->submitted_urbs;
956 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
957 ieee80211_stop_queues(zd_usb_to_hw(usb));
958 tx->stopped = 1;
960 spin_unlock_irqrestore(&tx->lock, flags);
964 * tx_urb_complete - completes the execution of an URB
965 * @urb: a URB
967 * This function is called if the URB has been transferred to a device or an
968 * error has happened.
970 static void tx_urb_complete(struct urb *urb)
972 int r;
973 struct sk_buff *skb;
974 struct ieee80211_tx_info *info;
975 struct zd_usb *usb;
976 struct zd_usb_tx *tx;
978 skb = (struct sk_buff *)urb->context;
979 info = IEEE80211_SKB_CB(skb);
981 * grab 'usb' pointer before handing off the skb (since
982 * it might be freed by zd_mac_tx_to_dev or mac80211)
984 usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb;
985 tx = &usb->tx;
987 switch (urb->status) {
988 case 0:
989 break;
990 case -ESHUTDOWN:
991 case -EINVAL:
992 case -ENODEV:
993 case -ENOENT:
994 case -ECONNRESET:
995 case -EPIPE:
996 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
997 break;
998 default:
999 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
1000 goto resubmit;
1002 free_urb:
1003 skb_unlink(skb, &usb->tx.submitted_skbs);
1004 zd_mac_tx_to_dev(skb, urb->status);
1005 usb_free_urb(urb);
1006 tx_dec_submitted_urbs(usb);
1007 return;
1008 resubmit:
1009 usb_anchor_urb(urb, &tx->submitted);
1010 r = usb_submit_urb(urb, GFP_ATOMIC);
1011 if (r) {
1012 usb_unanchor_urb(urb);
1013 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
1014 goto free_urb;
1019 * zd_usb_tx: initiates transfer of a frame of the device
1021 * @usb: the zd1211rw-private USB structure
1022 * @skb: a &struct sk_buff pointer
1024 * This function tranmits a frame to the device. It doesn't wait for
1025 * completion. The frame must contain the control set and have all the
1026 * control set information available.
1028 * The function returns 0 if the transfer has been successfully initiated.
1030 int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
1032 int r;
1033 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1034 struct usb_device *udev = zd_usb_to_usbdev(usb);
1035 struct urb *urb;
1036 struct zd_usb_tx *tx = &usb->tx;
1038 if (!atomic_read(&tx->enabled)) {
1039 r = -ENOENT;
1040 goto out;
1043 urb = usb_alloc_urb(0, GFP_ATOMIC);
1044 if (!urb) {
1045 r = -ENOMEM;
1046 goto out;
1049 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
1050 skb->data, skb->len, tx_urb_complete, skb);
1052 info->rate_driver_data[1] = (void *)jiffies;
1053 skb_queue_tail(&tx->submitted_skbs, skb);
1054 usb_anchor_urb(urb, &tx->submitted);
1056 r = usb_submit_urb(urb, GFP_ATOMIC);
1057 if (r) {
1058 dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r);
1059 usb_unanchor_urb(urb);
1060 skb_unlink(skb, &tx->submitted_skbs);
1061 goto error;
1063 tx_inc_submitted_urbs(usb);
1064 return 0;
1065 error:
1066 usb_free_urb(urb);
1067 out:
1068 return r;
1071 static bool zd_tx_timeout(struct zd_usb *usb)
1073 struct zd_usb_tx *tx = &usb->tx;
1074 struct sk_buff_head *q = &tx->submitted_skbs;
1075 struct sk_buff *skb, *skbnext;
1076 struct ieee80211_tx_info *info;
1077 unsigned long flags, trans_start;
1078 bool have_timedout = false;
1080 spin_lock_irqsave(&q->lock, flags);
1081 skb_queue_walk_safe(q, skb, skbnext) {
1082 info = IEEE80211_SKB_CB(skb);
1083 trans_start = (unsigned long)info->rate_driver_data[1];
1085 if (time_is_before_jiffies(trans_start + ZD_TX_TIMEOUT)) {
1086 have_timedout = true;
1087 break;
1090 spin_unlock_irqrestore(&q->lock, flags);
1092 return have_timedout;
1095 static void zd_tx_watchdog_handler(struct work_struct *work)
1097 struct zd_usb *usb =
1098 container_of(work, struct zd_usb, tx.watchdog_work.work);
1099 struct zd_usb_tx *tx = &usb->tx;
1101 if (!atomic_read(&tx->enabled) || !tx->watchdog_enabled)
1102 goto out;
1103 if (!zd_tx_timeout(usb))
1104 goto out;
1106 /* TX halted, try reset */
1107 dev_warn(zd_usb_dev(usb), "TX-stall detected, reseting device...");
1109 usb_queue_reset_device(usb->intf);
1111 /* reset will stop this worker, don't rearm */
1112 return;
1113 out:
1114 queue_delayed_work(zd_workqueue, &tx->watchdog_work,
1115 ZD_TX_WATCHDOG_INTERVAL);
1118 void zd_tx_watchdog_enable(struct zd_usb *usb)
1120 struct zd_usb_tx *tx = &usb->tx;
1122 if (!tx->watchdog_enabled) {
1123 dev_dbg_f(zd_usb_dev(usb), "\n");
1124 queue_delayed_work(zd_workqueue, &tx->watchdog_work,
1125 ZD_TX_WATCHDOG_INTERVAL);
1126 tx->watchdog_enabled = 1;
1130 void zd_tx_watchdog_disable(struct zd_usb *usb)
1132 struct zd_usb_tx *tx = &usb->tx;
1134 if (tx->watchdog_enabled) {
1135 dev_dbg_f(zd_usb_dev(usb), "\n");
1136 tx->watchdog_enabled = 0;
1137 cancel_delayed_work_sync(&tx->watchdog_work);
1141 static void zd_rx_idle_timer_handler(struct work_struct *work)
1143 struct zd_usb *usb =
1144 container_of(work, struct zd_usb, rx.idle_work.work);
1145 struct zd_mac *mac = zd_usb_to_mac(usb);
1147 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1148 return;
1150 dev_dbg_f(zd_usb_dev(usb), "\n");
1152 /* 30 seconds since last rx, reset rx */
1153 zd_usb_reset_rx(usb);
1156 static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param)
1158 struct zd_usb *usb = (struct zd_usb *)param;
1160 zd_usb_reset_rx_idle_timer(usb);
1163 void zd_usb_reset_rx_idle_timer(struct zd_usb *usb)
1165 struct zd_usb_rx *rx = &usb->rx;
1167 cancel_delayed_work(&rx->idle_work);
1168 queue_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL);
1171 static inline void init_usb_interrupt(struct zd_usb *usb)
1173 struct zd_usb_interrupt *intr = &usb->intr;
1175 spin_lock_init(&intr->lock);
1176 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
1177 init_completion(&intr->read_regs.completion);
1178 atomic_set(&intr->read_regs_enabled, 0);
1179 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
1182 static inline void init_usb_rx(struct zd_usb *usb)
1184 struct zd_usb_rx *rx = &usb->rx;
1186 spin_lock_init(&rx->lock);
1187 mutex_init(&rx->setup_mutex);
1188 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
1189 rx->usb_packet_size = 512;
1190 } else {
1191 rx->usb_packet_size = 64;
1193 ZD_ASSERT(rx->fragment_length == 0);
1194 INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler);
1195 rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet;
1196 rx->reset_timer_tasklet.data = (unsigned long)usb;
1199 static inline void init_usb_tx(struct zd_usb *usb)
1201 struct zd_usb_tx *tx = &usb->tx;
1203 spin_lock_init(&tx->lock);
1204 atomic_set(&tx->enabled, 0);
1205 tx->stopped = 0;
1206 skb_queue_head_init(&tx->submitted_skbs);
1207 init_usb_anchor(&tx->submitted);
1208 tx->submitted_urbs = 0;
1209 tx->watchdog_enabled = 0;
1210 INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler);
1213 void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
1214 struct usb_interface *intf)
1216 memset(usb, 0, sizeof(*usb));
1217 usb->intf = usb_get_intf(intf);
1218 usb_set_intfdata(usb->intf, hw);
1219 init_usb_anchor(&usb->submitted_cmds);
1220 init_usb_interrupt(usb);
1221 init_usb_tx(usb);
1222 init_usb_rx(usb);
1225 void zd_usb_clear(struct zd_usb *usb)
1227 usb_set_intfdata(usb->intf, NULL);
1228 usb_put_intf(usb->intf);
1229 ZD_MEMCLEAR(usb, sizeof(*usb));
1230 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1233 static const char *speed(enum usb_device_speed speed)
1235 switch (speed) {
1236 case USB_SPEED_LOW:
1237 return "low";
1238 case USB_SPEED_FULL:
1239 return "full";
1240 case USB_SPEED_HIGH:
1241 return "high";
1242 default:
1243 return "unknown speed";
1247 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1249 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1250 le16_to_cpu(udev->descriptor.idVendor),
1251 le16_to_cpu(udev->descriptor.idProduct),
1252 get_bcdDevice(udev),
1253 speed(udev->speed));
1256 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1258 struct usb_device *udev = interface_to_usbdev(usb->intf);
1259 return scnprint_id(udev, buffer, size);
1262 #ifdef DEBUG
1263 static void print_id(struct usb_device *udev)
1265 char buffer[40];
1267 scnprint_id(udev, buffer, sizeof(buffer));
1268 buffer[sizeof(buffer)-1] = 0;
1269 dev_dbg_f(&udev->dev, "%s\n", buffer);
1271 #else
1272 #define print_id(udev) do { } while (0)
1273 #endif
1275 static int eject_installer(struct usb_interface *intf)
1277 struct usb_device *udev = interface_to_usbdev(intf);
1278 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1279 struct usb_endpoint_descriptor *endpoint;
1280 unsigned char *cmd;
1281 u8 bulk_out_ep;
1282 int r;
1284 /* Find bulk out endpoint */
1285 for (r = 1; r >= 0; r--) {
1286 endpoint = &iface_desc->endpoint[r].desc;
1287 if (usb_endpoint_dir_out(endpoint) &&
1288 usb_endpoint_xfer_bulk(endpoint)) {
1289 bulk_out_ep = endpoint->bEndpointAddress;
1290 break;
1293 if (r == -1) {
1294 dev_err(&udev->dev,
1295 "zd1211rw: Could not find bulk out endpoint\n");
1296 return -ENODEV;
1299 cmd = kzalloc(31, GFP_KERNEL);
1300 if (cmd == NULL)
1301 return -ENODEV;
1303 /* USB bulk command block */
1304 cmd[0] = 0x55; /* bulk command signature */
1305 cmd[1] = 0x53; /* bulk command signature */
1306 cmd[2] = 0x42; /* bulk command signature */
1307 cmd[3] = 0x43; /* bulk command signature */
1308 cmd[14] = 6; /* command length */
1310 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1311 cmd[19] = 0x2; /* eject disc */
1313 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1314 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1315 cmd, 31, NULL, 2000);
1316 kfree(cmd);
1317 if (r)
1318 return r;
1320 /* At this point, the device disconnects and reconnects with the real
1321 * ID numbers. */
1323 usb_set_intfdata(intf, NULL);
1324 return 0;
1327 int zd_usb_init_hw(struct zd_usb *usb)
1329 int r;
1330 struct zd_mac *mac = zd_usb_to_mac(usb);
1332 dev_dbg_f(zd_usb_dev(usb), "\n");
1334 r = upload_firmware(usb);
1335 if (r) {
1336 dev_err(zd_usb_dev(usb),
1337 "couldn't load firmware. Error number %d\n", r);
1338 return r;
1341 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1342 if (r) {
1343 dev_dbg_f(zd_usb_dev(usb),
1344 "couldn't reset configuration. Error number %d\n", r);
1345 return r;
1348 r = zd_mac_init_hw(mac->hw);
1349 if (r) {
1350 dev_dbg_f(zd_usb_dev(usb),
1351 "couldn't initialize mac. Error number %d\n", r);
1352 return r;
1355 usb->initialized = 1;
1356 return 0;
1359 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1361 int r;
1362 struct usb_device *udev = interface_to_usbdev(intf);
1363 struct zd_usb *usb;
1364 struct ieee80211_hw *hw = NULL;
1366 print_id(udev);
1368 if (id->driver_info & DEVICE_INSTALLER)
1369 return eject_installer(intf);
1371 switch (udev->speed) {
1372 case USB_SPEED_LOW:
1373 case USB_SPEED_FULL:
1374 case USB_SPEED_HIGH:
1375 break;
1376 default:
1377 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1378 r = -ENODEV;
1379 goto error;
1382 r = usb_reset_device(udev);
1383 if (r) {
1384 dev_err(&intf->dev,
1385 "couldn't reset usb device. Error number %d\n", r);
1386 goto error;
1389 hw = zd_mac_alloc_hw(intf);
1390 if (hw == NULL) {
1391 r = -ENOMEM;
1392 goto error;
1395 usb = &zd_hw_mac(hw)->chip.usb;
1396 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1398 r = zd_mac_preinit_hw(hw);
1399 if (r) {
1400 dev_dbg_f(&intf->dev,
1401 "couldn't initialize mac. Error number %d\n", r);
1402 goto error;
1405 r = ieee80211_register_hw(hw);
1406 if (r) {
1407 dev_dbg_f(&intf->dev,
1408 "couldn't register device. Error number %d\n", r);
1409 goto error;
1412 dev_dbg_f(&intf->dev, "successful\n");
1413 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
1414 return 0;
1415 error:
1416 usb_reset_device(interface_to_usbdev(intf));
1417 if (hw) {
1418 zd_mac_clear(zd_hw_mac(hw));
1419 ieee80211_free_hw(hw);
1421 return r;
1424 static void disconnect(struct usb_interface *intf)
1426 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
1427 struct zd_mac *mac;
1428 struct zd_usb *usb;
1430 /* Either something really bad happened, or we're just dealing with
1431 * a DEVICE_INSTALLER. */
1432 if (hw == NULL)
1433 return;
1435 mac = zd_hw_mac(hw);
1436 usb = &mac->chip.usb;
1438 dev_dbg_f(zd_usb_dev(usb), "\n");
1440 ieee80211_unregister_hw(hw);
1442 /* Just in case something has gone wrong! */
1443 zd_usb_disable_tx(usb);
1444 zd_usb_disable_rx(usb);
1445 zd_usb_disable_int(usb);
1447 /* If the disconnect has been caused by a removal of the
1448 * driver module, the reset allows reloading of the driver. If the
1449 * reset will not be executed here, the upload of the firmware in the
1450 * probe function caused by the reloading of the driver will fail.
1452 usb_reset_device(interface_to_usbdev(intf));
1454 zd_mac_clear(mac);
1455 ieee80211_free_hw(hw);
1456 dev_dbg(&intf->dev, "disconnected\n");
1459 static void zd_usb_resume(struct zd_usb *usb)
1461 struct zd_mac *mac = zd_usb_to_mac(usb);
1462 int r;
1464 dev_dbg_f(zd_usb_dev(usb), "\n");
1466 r = zd_op_start(zd_usb_to_hw(usb));
1467 if (r < 0) {
1468 dev_warn(zd_usb_dev(usb), "Device resume failed "
1469 "with error code %d. Retrying...\n", r);
1470 if (usb->was_running)
1471 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
1472 usb_queue_reset_device(usb->intf);
1473 return;
1476 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) {
1477 r = zd_restore_settings(mac);
1478 if (r < 0) {
1479 dev_dbg(zd_usb_dev(usb),
1480 "failed to restore settings, %d\n", r);
1481 return;
1486 static void zd_usb_stop(struct zd_usb *usb)
1488 dev_dbg_f(zd_usb_dev(usb), "\n");
1490 zd_op_stop(zd_usb_to_hw(usb));
1492 zd_usb_disable_tx(usb);
1493 zd_usb_disable_rx(usb);
1494 zd_usb_disable_int(usb);
1496 usb->initialized = 0;
1499 static int pre_reset(struct usb_interface *intf)
1501 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1502 struct zd_mac *mac;
1503 struct zd_usb *usb;
1505 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1506 return 0;
1508 mac = zd_hw_mac(hw);
1509 usb = &mac->chip.usb;
1511 usb->was_running = test_bit(ZD_DEVICE_RUNNING, &mac->flags);
1513 zd_usb_stop(usb);
1515 mutex_lock(&mac->chip.mutex);
1516 return 0;
1519 static int post_reset(struct usb_interface *intf)
1521 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1522 struct zd_mac *mac;
1523 struct zd_usb *usb;
1525 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1526 return 0;
1528 mac = zd_hw_mac(hw);
1529 usb = &mac->chip.usb;
1531 mutex_unlock(&mac->chip.mutex);
1533 if (usb->was_running)
1534 zd_usb_resume(usb);
1535 return 0;
1538 static struct usb_driver driver = {
1539 .name = KBUILD_MODNAME,
1540 .id_table = usb_ids,
1541 .probe = probe,
1542 .disconnect = disconnect,
1543 .pre_reset = pre_reset,
1544 .post_reset = post_reset,
1547 struct workqueue_struct *zd_workqueue;
1549 static int __init usb_init(void)
1551 int r;
1553 pr_debug("%s usb_init()\n", driver.name);
1555 zd_workqueue = create_singlethread_workqueue(driver.name);
1556 if (zd_workqueue == NULL) {
1557 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1558 return -ENOMEM;
1561 r = usb_register(&driver);
1562 if (r) {
1563 destroy_workqueue(zd_workqueue);
1564 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1565 driver.name, r);
1566 return r;
1569 pr_debug("%s initialized\n", driver.name);
1570 return 0;
1573 static void __exit usb_exit(void)
1575 pr_debug("%s usb_exit()\n", driver.name);
1576 usb_deregister(&driver);
1577 destroy_workqueue(zd_workqueue);
1580 module_init(usb_init);
1581 module_exit(usb_exit);
1583 static int zd_ep_regs_out_msg(struct usb_device *udev, void *data, int len,
1584 int *actual_length, int timeout)
1586 /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in
1587 * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint
1588 * descriptor.
1590 struct usb_host_endpoint *ep;
1591 unsigned int pipe;
1593 pipe = usb_sndintpipe(udev, EP_REGS_OUT);
1594 ep = usb_pipe_endpoint(udev, pipe);
1595 if (!ep)
1596 return -EINVAL;
1598 if (usb_endpoint_xfer_int(&ep->desc)) {
1599 return usb_interrupt_msg(udev, pipe, data, len,
1600 actual_length, timeout);
1601 } else {
1602 pipe = usb_sndbulkpipe(udev, EP_REGS_OUT);
1603 return usb_bulk_msg(udev, pipe, data, len, actual_length,
1604 timeout);
1608 static int usb_int_regs_length(unsigned int count)
1610 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1613 static void prepare_read_regs_int(struct zd_usb *usb,
1614 struct usb_req_read_regs *req,
1615 unsigned int count)
1617 struct zd_usb_interrupt *intr = &usb->intr;
1619 spin_lock_irq(&intr->lock);
1620 atomic_set(&intr->read_regs_enabled, 1);
1621 intr->read_regs.req = req;
1622 intr->read_regs.req_count = count;
1623 INIT_COMPLETION(intr->read_regs.completion);
1624 spin_unlock_irq(&intr->lock);
1627 static void disable_read_regs_int(struct zd_usb *usb)
1629 struct zd_usb_interrupt *intr = &usb->intr;
1631 spin_lock_irq(&intr->lock);
1632 atomic_set(&intr->read_regs_enabled, 0);
1633 spin_unlock_irq(&intr->lock);
1636 static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
1637 unsigned int count)
1639 int i;
1640 struct zd_usb_interrupt *intr = &usb->intr;
1641 struct read_regs_int *rr = &intr->read_regs;
1642 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1644 /* The created block size seems to be larger than expected.
1645 * However results appear to be correct.
1647 if (rr->length < usb_int_regs_length(count)) {
1648 dev_dbg_f(zd_usb_dev(usb),
1649 "error: actual length %d less than expected %d\n",
1650 rr->length, usb_int_regs_length(count));
1651 return false;
1654 if (rr->length > sizeof(rr->buffer)) {
1655 dev_dbg_f(zd_usb_dev(usb),
1656 "error: actual length %d exceeds buffer size %zu\n",
1657 rr->length, sizeof(rr->buffer));
1658 return false;
1661 for (i = 0; i < count; i++) {
1662 struct reg_data *rd = &regs->regs[i];
1663 if (rd->addr != req->addr[i]) {
1664 dev_dbg_f(zd_usb_dev(usb),
1665 "rd[%d] addr %#06hx expected %#06hx\n", i,
1666 le16_to_cpu(rd->addr),
1667 le16_to_cpu(req->addr[i]));
1668 return false;
1672 return true;
1675 static int get_results(struct zd_usb *usb, u16 *values,
1676 struct usb_req_read_regs *req, unsigned int count,
1677 bool *retry)
1679 int r;
1680 int i;
1681 struct zd_usb_interrupt *intr = &usb->intr;
1682 struct read_regs_int *rr = &intr->read_regs;
1683 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1685 spin_lock_irq(&intr->lock);
1687 r = -EIO;
1689 /* Read failed because firmware bug? */
1690 *retry = !!intr->read_regs_int_overridden;
1691 if (*retry)
1692 goto error_unlock;
1694 if (!check_read_regs(usb, req, count)) {
1695 dev_dbg_f(zd_usb_dev(usb), "error: invalid read regs\n");
1696 goto error_unlock;
1699 for (i = 0; i < count; i++) {
1700 struct reg_data *rd = &regs->regs[i];
1701 values[i] = le16_to_cpu(rd->value);
1704 r = 0;
1705 error_unlock:
1706 spin_unlock_irq(&intr->lock);
1707 return r;
1710 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1711 const zd_addr_t *addresses, unsigned int count)
1713 int r, i, req_len, actual_req_len, try_count = 0;
1714 struct usb_device *udev;
1715 struct usb_req_read_regs *req = NULL;
1716 unsigned long timeout;
1717 bool retry = false;
1719 if (count < 1) {
1720 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1721 return -EINVAL;
1723 if (count > USB_MAX_IOREAD16_COUNT) {
1724 dev_dbg_f(zd_usb_dev(usb),
1725 "error: count %u exceeds possible max %u\n",
1726 count, USB_MAX_IOREAD16_COUNT);
1727 return -EINVAL;
1729 if (in_atomic()) {
1730 dev_dbg_f(zd_usb_dev(usb),
1731 "error: io in atomic context not supported\n");
1732 return -EWOULDBLOCK;
1734 if (!usb_int_enabled(usb)) {
1735 dev_dbg_f(zd_usb_dev(usb),
1736 "error: usb interrupt not enabled\n");
1737 return -EWOULDBLOCK;
1740 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1741 BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT *
1742 sizeof(__le16) > sizeof(usb->req_buf));
1743 BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) >
1744 sizeof(usb->req_buf));
1746 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1747 req = (void *)usb->req_buf;
1749 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1750 for (i = 0; i < count; i++)
1751 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1753 retry_read:
1754 try_count++;
1755 udev = zd_usb_to_usbdev(usb);
1756 prepare_read_regs_int(usb, req, count);
1757 r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
1758 if (r) {
1759 dev_dbg_f(zd_usb_dev(usb),
1760 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
1761 goto error;
1763 if (req_len != actual_req_len) {
1764 dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()\n"
1765 " req_len %d != actual_req_len %d\n",
1766 req_len, actual_req_len);
1767 r = -EIO;
1768 goto error;
1771 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1772 msecs_to_jiffies(50));
1773 if (!timeout) {
1774 disable_read_regs_int(usb);
1775 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1776 r = -ETIMEDOUT;
1777 goto error;
1780 r = get_results(usb, values, req, count, &retry);
1781 if (retry && try_count < 20) {
1782 dev_dbg_f(zd_usb_dev(usb), "read retry, tries so far: %d\n",
1783 try_count);
1784 goto retry_read;
1786 error:
1787 return r;
1790 static void iowrite16v_urb_complete(struct urb *urb)
1792 struct zd_usb *usb = urb->context;
1794 if (urb->status && !usb->cmd_error)
1795 usb->cmd_error = urb->status;
1797 if (!usb->cmd_error &&
1798 urb->actual_length != urb->transfer_buffer_length)
1799 usb->cmd_error = -EIO;
1802 static int zd_submit_waiting_urb(struct zd_usb *usb, bool last)
1804 int r = 0;
1805 struct urb *urb = usb->urb_async_waiting;
1807 if (!urb)
1808 return 0;
1810 usb->urb_async_waiting = NULL;
1812 if (!last)
1813 urb->transfer_flags |= URB_NO_INTERRUPT;
1815 usb_anchor_urb(urb, &usb->submitted_cmds);
1816 r = usb_submit_urb(urb, GFP_KERNEL);
1817 if (r) {
1818 usb_unanchor_urb(urb);
1819 dev_dbg_f(zd_usb_dev(usb),
1820 "error in usb_submit_urb(). Error number %d\n", r);
1821 goto error;
1824 /* fall-through with r == 0 */
1825 error:
1826 usb_free_urb(urb);
1827 return r;
1830 void zd_usb_iowrite16v_async_start(struct zd_usb *usb)
1832 ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds));
1833 ZD_ASSERT(usb->urb_async_waiting == NULL);
1834 ZD_ASSERT(!usb->in_async);
1836 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1838 usb->in_async = 1;
1839 usb->cmd_error = 0;
1840 usb->urb_async_waiting = NULL;
1843 int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout)
1845 int r;
1847 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1848 ZD_ASSERT(usb->in_async);
1850 /* Submit last iowrite16v URB */
1851 r = zd_submit_waiting_urb(usb, true);
1852 if (r) {
1853 dev_dbg_f(zd_usb_dev(usb),
1854 "error in zd_submit_waiting_usb(). "
1855 "Error number %d\n", r);
1857 usb_kill_anchored_urbs(&usb->submitted_cmds);
1858 goto error;
1861 if (timeout)
1862 timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds,
1863 timeout);
1864 if (!timeout) {
1865 usb_kill_anchored_urbs(&usb->submitted_cmds);
1866 if (usb->cmd_error == -ENOENT) {
1867 dev_dbg_f(zd_usb_dev(usb), "timed out");
1868 r = -ETIMEDOUT;
1869 goto error;
1873 r = usb->cmd_error;
1874 error:
1875 usb->in_async = 0;
1876 return r;
1879 int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1880 unsigned int count)
1882 int r;
1883 struct usb_device *udev;
1884 struct usb_req_write_regs *req = NULL;
1885 int i, req_len;
1886 struct urb *urb;
1887 struct usb_host_endpoint *ep;
1889 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1890 ZD_ASSERT(usb->in_async);
1892 if (count == 0)
1893 return 0;
1894 if (count > USB_MAX_IOWRITE16_COUNT) {
1895 dev_dbg_f(zd_usb_dev(usb),
1896 "error: count %u exceeds possible max %u\n",
1897 count, USB_MAX_IOWRITE16_COUNT);
1898 return -EINVAL;
1900 if (in_atomic()) {
1901 dev_dbg_f(zd_usb_dev(usb),
1902 "error: io in atomic context not supported\n");
1903 return -EWOULDBLOCK;
1906 udev = zd_usb_to_usbdev(usb);
1908 ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT));
1909 if (!ep)
1910 return -ENOENT;
1912 urb = usb_alloc_urb(0, GFP_KERNEL);
1913 if (!urb)
1914 return -ENOMEM;
1916 req_len = sizeof(struct usb_req_write_regs) +
1917 count * sizeof(struct reg_data);
1918 req = kmalloc(req_len, GFP_KERNEL);
1919 if (!req) {
1920 r = -ENOMEM;
1921 goto error;
1924 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1925 for (i = 0; i < count; i++) {
1926 struct reg_data *rw = &req->reg_writes[i];
1927 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1928 rw->value = cpu_to_le16(ioreqs[i].value);
1931 /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
1932 * endpoint is bulk. Select correct type URB by endpoint descriptor.
1934 if (usb_endpoint_xfer_int(&ep->desc))
1935 usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT),
1936 req, req_len, iowrite16v_urb_complete, usb,
1937 ep->desc.bInterval);
1938 else
1939 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1940 req, req_len, iowrite16v_urb_complete, usb);
1942 urb->transfer_flags |= URB_FREE_BUFFER;
1944 /* Submit previous URB */
1945 r = zd_submit_waiting_urb(usb, false);
1946 if (r) {
1947 dev_dbg_f(zd_usb_dev(usb),
1948 "error in zd_submit_waiting_usb(). "
1949 "Error number %d\n", r);
1950 goto error;
1953 /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1954 * of currect batch except for very last.
1956 usb->urb_async_waiting = urb;
1957 return 0;
1958 error:
1959 usb_free_urb(urb);
1960 return r;
1963 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1964 unsigned int count)
1966 int r;
1968 zd_usb_iowrite16v_async_start(usb);
1969 r = zd_usb_iowrite16v_async(usb, ioreqs, count);
1970 if (r) {
1971 zd_usb_iowrite16v_async_end(usb, 0);
1972 return r;
1974 return zd_usb_iowrite16v_async_end(usb, 50 /* ms */);
1977 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1979 int r;
1980 struct usb_device *udev;
1981 struct usb_req_rfwrite *req = NULL;
1982 int i, req_len, actual_req_len;
1983 u16 bit_value_template;
1985 if (in_atomic()) {
1986 dev_dbg_f(zd_usb_dev(usb),
1987 "error: io in atomic context not supported\n");
1988 return -EWOULDBLOCK;
1990 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1991 dev_dbg_f(zd_usb_dev(usb),
1992 "error: bits %d are smaller than"
1993 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1994 bits, USB_MIN_RFWRITE_BIT_COUNT);
1995 return -EINVAL;
1997 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1998 dev_dbg_f(zd_usb_dev(usb),
1999 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
2000 bits, USB_MAX_RFWRITE_BIT_COUNT);
2001 return -EINVAL;
2003 #ifdef DEBUG
2004 if (value & (~0UL << bits)) {
2005 dev_dbg_f(zd_usb_dev(usb),
2006 "error: value %#09x has bits >= %d set\n",
2007 value, bits);
2008 return -EINVAL;
2010 #endif /* DEBUG */
2012 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
2014 r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203);
2015 if (r) {
2016 dev_dbg_f(zd_usb_dev(usb),
2017 "error %d: Couldn't read ZD_CR203\n", r);
2018 return r;
2020 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
2022 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
2023 BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) +
2024 USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) >
2025 sizeof(usb->req_buf));
2026 BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) >
2027 sizeof(usb->req_buf));
2029 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
2030 req = (void *)usb->req_buf;
2032 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
2033 /* 1: 3683a, but not used in ZYDAS driver */
2034 req->value = cpu_to_le16(2);
2035 req->bits = cpu_to_le16(bits);
2037 for (i = 0; i < bits; i++) {
2038 u16 bv = bit_value_template;
2039 if (value & (1 << (bits-1-i)))
2040 bv |= RF_DATA;
2041 req->bit_values[i] = cpu_to_le16(bv);
2044 udev = zd_usb_to_usbdev(usb);
2045 r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
2046 if (r) {
2047 dev_dbg_f(zd_usb_dev(usb),
2048 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
2049 goto out;
2051 if (req_len != actual_req_len) {
2052 dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()"
2053 " req_len %d != actual_req_len %d\n",
2054 req_len, actual_req_len);
2055 r = -EIO;
2056 goto out;
2059 /* FALL-THROUGH with r == 0 */
2060 out:
2061 return r;