[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wireless / zd1211rw / zd_usb.c
blob23a6a6d4863bd8c3468ac2b21276125201419faa
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/skbuff.h>
28 #include <linux/usb.h>
29 #include <linux/workqueue.h>
30 #include <net/mac80211.h>
31 #include <asm/unaligned.h>
33 #include "zd_def.h"
34 #include "zd_mac.h"
35 #include "zd_usb.h"
37 static struct usb_device_id usb_ids[] = {
38 /* ZD1211 */
39 { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
55 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
57 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
58 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
59 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
60 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
61 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
62 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
63 /* ZD1211B */
64 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
66 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
67 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B },
70 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
71 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
73 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B },
80 { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
81 { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
82 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
83 { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
84 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
85 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
86 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
87 { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
88 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
89 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
90 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
91 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
92 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
93 /* "Driverless" devices that need ejecting */
94 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
95 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
99 MODULE_LICENSE("GPL");
100 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
101 MODULE_AUTHOR("Ulrich Kunitz");
102 MODULE_AUTHOR("Daniel Drake");
103 MODULE_VERSION("1.0");
104 MODULE_DEVICE_TABLE(usb, usb_ids);
106 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
107 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
109 /* USB device initialization */
110 static void int_urb_complete(struct urb *urb);
112 static int request_fw_file(
113 const struct firmware **fw, const char *name, struct device *device)
115 int r;
117 dev_dbg_f(device, "fw name %s\n", name);
119 r = request_firmware(fw, name, device);
120 if (r)
121 dev_err(device,
122 "Could not load firmware file %s. Error number %d\n",
123 name, r);
124 return r;
127 static inline u16 get_bcdDevice(const struct usb_device *udev)
129 return le16_to_cpu(udev->descriptor.bcdDevice);
132 enum upload_code_flags {
133 REBOOT = 1,
136 /* Ensures that MAX_TRANSFER_SIZE is even. */
137 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
139 static int upload_code(struct usb_device *udev,
140 const u8 *data, size_t size, u16 code_offset, int flags)
142 u8 *p;
143 int r;
145 /* USB request blocks need "kmalloced" buffers.
147 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
148 if (!p) {
149 dev_err(&udev->dev, "out of memory\n");
150 r = -ENOMEM;
151 goto error;
154 size &= ~1;
155 while (size > 0) {
156 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
157 size : MAX_TRANSFER_SIZE;
159 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
161 memcpy(p, data, transfer_size);
162 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
163 USB_REQ_FIRMWARE_DOWNLOAD,
164 USB_DIR_OUT | USB_TYPE_VENDOR,
165 code_offset, 0, p, transfer_size, 1000 /* ms */);
166 if (r < 0) {
167 dev_err(&udev->dev,
168 "USB control request for firmware upload"
169 " failed. Error number %d\n", r);
170 goto error;
172 transfer_size = r & ~1;
174 size -= transfer_size;
175 data += transfer_size;
176 code_offset += transfer_size/sizeof(u16);
179 if (flags & REBOOT) {
180 u8 ret;
182 /* Use "DMA-aware" buffer. */
183 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
184 USB_REQ_FIRMWARE_CONFIRM,
185 USB_DIR_IN | USB_TYPE_VENDOR,
186 0, 0, p, sizeof(ret), 5000 /* ms */);
187 if (r != sizeof(ret)) {
188 dev_err(&udev->dev,
189 "control request firmeware confirmation failed."
190 " Return value %d\n", r);
191 if (r >= 0)
192 r = -ENODEV;
193 goto error;
195 ret = p[0];
196 if (ret & 0x80) {
197 dev_err(&udev->dev,
198 "Internal error while downloading."
199 " Firmware confirm return value %#04x\n",
200 (unsigned int)ret);
201 r = -ENODEV;
202 goto error;
204 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
205 (unsigned int)ret);
208 r = 0;
209 error:
210 kfree(p);
211 return r;
214 static u16 get_word(const void *data, u16 offset)
216 const __le16 *p = data;
217 return le16_to_cpu(p[offset]);
220 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
221 const char* postfix)
223 scnprintf(buffer, size, "%s%s",
224 usb->is_zd1211b ?
225 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
226 postfix);
227 return buffer;
230 static int handle_version_mismatch(struct zd_usb *usb,
231 const struct firmware *ub_fw)
233 struct usb_device *udev = zd_usb_to_usbdev(usb);
234 const struct firmware *ur_fw = NULL;
235 int offset;
236 int r = 0;
237 char fw_name[128];
239 r = request_fw_file(&ur_fw,
240 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
241 &udev->dev);
242 if (r)
243 goto error;
245 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
246 if (r)
247 goto error;
249 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
250 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
251 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
253 /* At this point, the vendor driver downloads the whole firmware
254 * image, hacks around with version IDs, and uploads it again,
255 * completely overwriting the boot code. We do not do this here as
256 * it is not required on any tested devices, and it is suspected to
257 * cause problems. */
258 error:
259 release_firmware(ur_fw);
260 return r;
263 static int upload_firmware(struct zd_usb *usb)
265 int r;
266 u16 fw_bcdDevice;
267 u16 bcdDevice;
268 struct usb_device *udev = zd_usb_to_usbdev(usb);
269 const struct firmware *ub_fw = NULL;
270 const struct firmware *uph_fw = NULL;
271 char fw_name[128];
273 bcdDevice = get_bcdDevice(udev);
275 r = request_fw_file(&ub_fw,
276 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
277 &udev->dev);
278 if (r)
279 goto error;
281 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
283 if (fw_bcdDevice != bcdDevice) {
284 dev_info(&udev->dev,
285 "firmware version %#06x and device bootcode version "
286 "%#06x differ\n", fw_bcdDevice, bcdDevice);
287 if (bcdDevice <= 0x4313)
288 dev_warn(&udev->dev, "device has old bootcode, please "
289 "report success or failure\n");
291 r = handle_version_mismatch(usb, ub_fw);
292 if (r)
293 goto error;
294 } else {
295 dev_dbg_f(&udev->dev,
296 "firmware device id %#06x is equal to the "
297 "actual device id\n", fw_bcdDevice);
301 r = request_fw_file(&uph_fw,
302 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
303 &udev->dev);
304 if (r)
305 goto error;
307 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
308 if (r) {
309 dev_err(&udev->dev,
310 "Could not upload firmware code uph. Error number %d\n",
314 /* FALL-THROUGH */
315 error:
316 release_firmware(ub_fw);
317 release_firmware(uph_fw);
318 return r;
321 /* Read data from device address space using "firmware interface" which does
322 * not require firmware to be loaded. */
323 int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
325 int r;
326 struct usb_device *udev = zd_usb_to_usbdev(usb);
327 u8 *buf;
329 /* Use "DMA-aware" buffer. */
330 buf = kmalloc(len, GFP_KERNEL);
331 if (!buf)
332 return -ENOMEM;
333 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
334 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
335 buf, len, 5000);
336 if (r < 0) {
337 dev_err(&udev->dev,
338 "read over firmware interface failed: %d\n", r);
339 goto exit;
340 } else if (r != len) {
341 dev_err(&udev->dev,
342 "incomplete read over firmware interface: %d/%d\n",
343 r, len);
344 r = -EIO;
345 goto exit;
347 r = 0;
348 memcpy(data, buf, len);
349 exit:
350 kfree(buf);
351 return r;
354 #define urb_dev(urb) (&(urb)->dev->dev)
356 static inline void handle_regs_int(struct urb *urb)
358 struct zd_usb *usb = urb->context;
359 struct zd_usb_interrupt *intr = &usb->intr;
360 int len;
361 u16 int_num;
363 ZD_ASSERT(in_interrupt());
364 spin_lock(&intr->lock);
366 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
367 if (int_num == CR_INTERRUPT) {
368 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
369 memcpy(&mac->intr_buffer, urb->transfer_buffer,
370 USB_MAX_EP_INT_BUFFER);
371 schedule_work(&mac->process_intr);
372 } else if (intr->read_regs_enabled) {
373 intr->read_regs.length = len = urb->actual_length;
375 if (len > sizeof(intr->read_regs.buffer))
376 len = sizeof(intr->read_regs.buffer);
377 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
378 intr->read_regs_enabled = 0;
379 complete(&intr->read_regs.completion);
380 goto out;
383 out:
384 spin_unlock(&intr->lock);
387 static void int_urb_complete(struct urb *urb)
389 int r;
390 struct usb_int_header *hdr;
392 switch (urb->status) {
393 case 0:
394 break;
395 case -ESHUTDOWN:
396 case -EINVAL:
397 case -ENODEV:
398 case -ENOENT:
399 case -ECONNRESET:
400 case -EPIPE:
401 goto kfree;
402 default:
403 goto resubmit;
406 if (urb->actual_length < sizeof(hdr)) {
407 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
408 goto resubmit;
411 hdr = urb->transfer_buffer;
412 if (hdr->type != USB_INT_TYPE) {
413 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
414 goto resubmit;
417 switch (hdr->id) {
418 case USB_INT_ID_REGS:
419 handle_regs_int(urb);
420 break;
421 case USB_INT_ID_RETRY_FAILED:
422 zd_mac_tx_failed(zd_usb_to_hw(urb->context));
423 break;
424 default:
425 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
426 (unsigned int)hdr->id);
427 goto resubmit;
430 resubmit:
431 r = usb_submit_urb(urb, GFP_ATOMIC);
432 if (r) {
433 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
434 goto kfree;
436 return;
437 kfree:
438 kfree(urb->transfer_buffer);
441 static inline int int_urb_interval(struct usb_device *udev)
443 switch (udev->speed) {
444 case USB_SPEED_HIGH:
445 return 4;
446 case USB_SPEED_LOW:
447 return 10;
448 case USB_SPEED_FULL:
449 default:
450 return 1;
454 static inline int usb_int_enabled(struct zd_usb *usb)
456 unsigned long flags;
457 struct zd_usb_interrupt *intr = &usb->intr;
458 struct urb *urb;
460 spin_lock_irqsave(&intr->lock, flags);
461 urb = intr->urb;
462 spin_unlock_irqrestore(&intr->lock, flags);
463 return urb != NULL;
466 int zd_usb_enable_int(struct zd_usb *usb)
468 int r;
469 struct usb_device *udev;
470 struct zd_usb_interrupt *intr = &usb->intr;
471 void *transfer_buffer = NULL;
472 struct urb *urb;
474 dev_dbg_f(zd_usb_dev(usb), "\n");
476 urb = usb_alloc_urb(0, GFP_KERNEL);
477 if (!urb) {
478 r = -ENOMEM;
479 goto out;
482 ZD_ASSERT(!irqs_disabled());
483 spin_lock_irq(&intr->lock);
484 if (intr->urb) {
485 spin_unlock_irq(&intr->lock);
486 r = 0;
487 goto error_free_urb;
489 intr->urb = urb;
490 spin_unlock_irq(&intr->lock);
492 /* TODO: make it a DMA buffer */
493 r = -ENOMEM;
494 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
495 if (!transfer_buffer) {
496 dev_dbg_f(zd_usb_dev(usb),
497 "couldn't allocate transfer_buffer\n");
498 goto error_set_urb_null;
501 udev = zd_usb_to_usbdev(usb);
502 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
503 transfer_buffer, USB_MAX_EP_INT_BUFFER,
504 int_urb_complete, usb,
505 intr->interval);
507 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
508 r = usb_submit_urb(urb, GFP_KERNEL);
509 if (r) {
510 dev_dbg_f(zd_usb_dev(usb),
511 "Couldn't submit urb. Error number %d\n", r);
512 goto error;
515 return 0;
516 error:
517 kfree(transfer_buffer);
518 error_set_urb_null:
519 spin_lock_irq(&intr->lock);
520 intr->urb = NULL;
521 spin_unlock_irq(&intr->lock);
522 error_free_urb:
523 usb_free_urb(urb);
524 out:
525 return r;
528 void zd_usb_disable_int(struct zd_usb *usb)
530 unsigned long flags;
531 struct zd_usb_interrupt *intr = &usb->intr;
532 struct urb *urb;
534 spin_lock_irqsave(&intr->lock, flags);
535 urb = intr->urb;
536 if (!urb) {
537 spin_unlock_irqrestore(&intr->lock, flags);
538 return;
540 intr->urb = NULL;
541 spin_unlock_irqrestore(&intr->lock, flags);
543 usb_kill_urb(urb);
544 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
545 usb_free_urb(urb);
548 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
549 unsigned int length)
551 int i;
552 const struct rx_length_info *length_info;
554 if (length < sizeof(struct rx_length_info)) {
555 /* It's not a complete packet anyhow. */
556 return;
558 length_info = (struct rx_length_info *)
559 (buffer + length - sizeof(struct rx_length_info));
561 /* It might be that three frames are merged into a single URB
562 * transaction. We have to check for the length info tag.
564 * While testing we discovered that length_info might be unaligned,
565 * because if USB transactions are merged, the last packet will not
566 * be padded. Unaligned access might also happen if the length_info
567 * structure is not present.
569 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
571 unsigned int l, k, n;
572 for (i = 0, l = 0;; i++) {
573 k = get_unaligned_le16(&length_info->length[i]);
574 if (k == 0)
575 return;
576 n = l+k;
577 if (n > length)
578 return;
579 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
580 if (i >= 2)
581 return;
582 l = (n+3) & ~3;
584 } else {
585 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
589 static void rx_urb_complete(struct urb *urb)
591 struct zd_usb *usb;
592 struct zd_usb_rx *rx;
593 const u8 *buffer;
594 unsigned int length;
596 switch (urb->status) {
597 case 0:
598 break;
599 case -ESHUTDOWN:
600 case -EINVAL:
601 case -ENODEV:
602 case -ENOENT:
603 case -ECONNRESET:
604 case -EPIPE:
605 return;
606 default:
607 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
608 goto resubmit;
611 buffer = urb->transfer_buffer;
612 length = urb->actual_length;
613 usb = urb->context;
614 rx = &usb->rx;
616 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
617 /* If there is an old first fragment, we don't care. */
618 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
619 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
620 spin_lock(&rx->lock);
621 memcpy(rx->fragment, buffer, length);
622 rx->fragment_length = length;
623 spin_unlock(&rx->lock);
624 goto resubmit;
627 spin_lock(&rx->lock);
628 if (rx->fragment_length > 0) {
629 /* We are on a second fragment, we believe */
630 ZD_ASSERT(length + rx->fragment_length <=
631 ARRAY_SIZE(rx->fragment));
632 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
633 memcpy(rx->fragment+rx->fragment_length, buffer, length);
634 handle_rx_packet(usb, rx->fragment,
635 rx->fragment_length + length);
636 rx->fragment_length = 0;
637 spin_unlock(&rx->lock);
638 } else {
639 spin_unlock(&rx->lock);
640 handle_rx_packet(usb, buffer, length);
643 resubmit:
644 usb_submit_urb(urb, GFP_ATOMIC);
647 static struct urb *alloc_rx_urb(struct zd_usb *usb)
649 struct usb_device *udev = zd_usb_to_usbdev(usb);
650 struct urb *urb;
651 void *buffer;
653 urb = usb_alloc_urb(0, GFP_KERNEL);
654 if (!urb)
655 return NULL;
656 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
657 &urb->transfer_dma);
658 if (!buffer) {
659 usb_free_urb(urb);
660 return NULL;
663 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
664 buffer, USB_MAX_RX_SIZE,
665 rx_urb_complete, usb);
666 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
668 return urb;
671 static void free_rx_urb(struct urb *urb)
673 if (!urb)
674 return;
675 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
676 urb->transfer_buffer, urb->transfer_dma);
677 usb_free_urb(urb);
680 int zd_usb_enable_rx(struct zd_usb *usb)
682 int i, r;
683 struct zd_usb_rx *rx = &usb->rx;
684 struct urb **urbs;
686 dev_dbg_f(zd_usb_dev(usb), "\n");
688 r = -ENOMEM;
689 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
690 if (!urbs)
691 goto error;
692 for (i = 0; i < RX_URBS_COUNT; i++) {
693 urbs[i] = alloc_rx_urb(usb);
694 if (!urbs[i])
695 goto error;
698 ZD_ASSERT(!irqs_disabled());
699 spin_lock_irq(&rx->lock);
700 if (rx->urbs) {
701 spin_unlock_irq(&rx->lock);
702 r = 0;
703 goto error;
705 rx->urbs = urbs;
706 rx->urbs_count = RX_URBS_COUNT;
707 spin_unlock_irq(&rx->lock);
709 for (i = 0; i < RX_URBS_COUNT; i++) {
710 r = usb_submit_urb(urbs[i], GFP_KERNEL);
711 if (r)
712 goto error_submit;
715 return 0;
716 error_submit:
717 for (i = 0; i < RX_URBS_COUNT; i++) {
718 usb_kill_urb(urbs[i]);
720 spin_lock_irq(&rx->lock);
721 rx->urbs = NULL;
722 rx->urbs_count = 0;
723 spin_unlock_irq(&rx->lock);
724 error:
725 if (urbs) {
726 for (i = 0; i < RX_URBS_COUNT; i++)
727 free_rx_urb(urbs[i]);
729 return r;
732 void zd_usb_disable_rx(struct zd_usb *usb)
734 int i;
735 unsigned long flags;
736 struct urb **urbs;
737 unsigned int count;
738 struct zd_usb_rx *rx = &usb->rx;
740 spin_lock_irqsave(&rx->lock, flags);
741 urbs = rx->urbs;
742 count = rx->urbs_count;
743 spin_unlock_irqrestore(&rx->lock, flags);
744 if (!urbs)
745 return;
747 for (i = 0; i < count; i++) {
748 usb_kill_urb(urbs[i]);
749 free_rx_urb(urbs[i]);
751 kfree(urbs);
753 spin_lock_irqsave(&rx->lock, flags);
754 rx->urbs = NULL;
755 rx->urbs_count = 0;
756 spin_unlock_irqrestore(&rx->lock, flags);
760 * zd_usb_disable_tx - disable transmission
761 * @usb: the zd1211rw-private USB structure
763 * Frees all URBs in the free list and marks the transmission as disabled.
765 void zd_usb_disable_tx(struct zd_usb *usb)
767 struct zd_usb_tx *tx = &usb->tx;
768 unsigned long flags;
769 struct list_head *pos, *n;
771 spin_lock_irqsave(&tx->lock, flags);
772 list_for_each_safe(pos, n, &tx->free_urb_list) {
773 list_del(pos);
774 usb_free_urb(list_entry(pos, struct urb, urb_list));
776 tx->enabled = 0;
777 tx->submitted_urbs = 0;
778 /* The stopped state is ignored, relying on ieee80211_wake_queues()
779 * in a potentionally following zd_usb_enable_tx().
781 spin_unlock_irqrestore(&tx->lock, flags);
785 * zd_usb_enable_tx - enables transmission
786 * @usb: a &struct zd_usb pointer
788 * This function enables transmission and prepares the &zd_usb_tx data
789 * structure.
791 void zd_usb_enable_tx(struct zd_usb *usb)
793 unsigned long flags;
794 struct zd_usb_tx *tx = &usb->tx;
796 spin_lock_irqsave(&tx->lock, flags);
797 tx->enabled = 1;
798 tx->submitted_urbs = 0;
799 ieee80211_wake_queues(zd_usb_to_hw(usb));
800 tx->stopped = 0;
801 spin_unlock_irqrestore(&tx->lock, flags);
805 * alloc_tx_urb - provides an tx URB
806 * @usb: a &struct zd_usb pointer
808 * Allocates a new URB. If possible takes the urb from the free list in
809 * usb->tx.
811 static struct urb *alloc_tx_urb(struct zd_usb *usb)
813 struct zd_usb_tx *tx = &usb->tx;
814 unsigned long flags;
815 struct list_head *entry;
816 struct urb *urb;
818 spin_lock_irqsave(&tx->lock, flags);
819 if (list_empty(&tx->free_urb_list)) {
820 urb = usb_alloc_urb(0, GFP_ATOMIC);
821 goto out;
823 entry = tx->free_urb_list.next;
824 list_del(entry);
825 urb = list_entry(entry, struct urb, urb_list);
826 out:
827 spin_unlock_irqrestore(&tx->lock, flags);
828 return urb;
832 * free_tx_urb - frees a used tx URB
833 * @usb: a &struct zd_usb pointer
834 * @urb: URB to be freed
836 * Frees the the transmission URB, which means to put it on the free URB
837 * list.
839 static void free_tx_urb(struct zd_usb *usb, struct urb *urb)
841 struct zd_usb_tx *tx = &usb->tx;
842 unsigned long flags;
844 spin_lock_irqsave(&tx->lock, flags);
845 if (!tx->enabled) {
846 usb_free_urb(urb);
847 goto out;
849 list_add(&urb->urb_list, &tx->free_urb_list);
850 out:
851 spin_unlock_irqrestore(&tx->lock, flags);
854 static void tx_dec_submitted_urbs(struct zd_usb *usb)
856 struct zd_usb_tx *tx = &usb->tx;
857 unsigned long flags;
859 spin_lock_irqsave(&tx->lock, flags);
860 --tx->submitted_urbs;
861 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
862 ieee80211_wake_queues(zd_usb_to_hw(usb));
863 tx->stopped = 0;
865 spin_unlock_irqrestore(&tx->lock, flags);
868 static void tx_inc_submitted_urbs(struct zd_usb *usb)
870 struct zd_usb_tx *tx = &usb->tx;
871 unsigned long flags;
873 spin_lock_irqsave(&tx->lock, flags);
874 ++tx->submitted_urbs;
875 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
876 ieee80211_stop_queues(zd_usb_to_hw(usb));
877 tx->stopped = 1;
879 spin_unlock_irqrestore(&tx->lock, flags);
883 * tx_urb_complete - completes the execution of an URB
884 * @urb: a URB
886 * This function is called if the URB has been transferred to a device or an
887 * error has happened.
889 static void tx_urb_complete(struct urb *urb)
891 int r;
892 struct sk_buff *skb;
893 struct ieee80211_tx_info *info;
894 struct zd_usb *usb;
896 switch (urb->status) {
897 case 0:
898 break;
899 case -ESHUTDOWN:
900 case -EINVAL:
901 case -ENODEV:
902 case -ENOENT:
903 case -ECONNRESET:
904 case -EPIPE:
905 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
906 break;
907 default:
908 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
909 goto resubmit;
911 free_urb:
912 skb = (struct sk_buff *)urb->context;
914 * grab 'usb' pointer before handing off the skb (since
915 * it might be freed by zd_mac_tx_to_dev or mac80211)
917 info = IEEE80211_SKB_CB(skb);
918 usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb;
919 zd_mac_tx_to_dev(skb, urb->status);
920 free_tx_urb(usb, urb);
921 tx_dec_submitted_urbs(usb);
922 return;
923 resubmit:
924 r = usb_submit_urb(urb, GFP_ATOMIC);
925 if (r) {
926 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
927 goto free_urb;
932 * zd_usb_tx: initiates transfer of a frame of the device
934 * @usb: the zd1211rw-private USB structure
935 * @skb: a &struct sk_buff pointer
937 * This function tranmits a frame to the device. It doesn't wait for
938 * completion. The frame must contain the control set and have all the
939 * control set information available.
941 * The function returns 0 if the transfer has been successfully initiated.
943 int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
945 int r;
946 struct usb_device *udev = zd_usb_to_usbdev(usb);
947 struct urb *urb;
949 urb = alloc_tx_urb(usb);
950 if (!urb) {
951 r = -ENOMEM;
952 goto out;
955 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
956 skb->data, skb->len, tx_urb_complete, skb);
958 r = usb_submit_urb(urb, GFP_ATOMIC);
959 if (r)
960 goto error;
961 tx_inc_submitted_urbs(usb);
962 return 0;
963 error:
964 free_tx_urb(usb, urb);
965 out:
966 return r;
969 static inline void init_usb_interrupt(struct zd_usb *usb)
971 struct zd_usb_interrupt *intr = &usb->intr;
973 spin_lock_init(&intr->lock);
974 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
975 init_completion(&intr->read_regs.completion);
976 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
979 static inline void init_usb_rx(struct zd_usb *usb)
981 struct zd_usb_rx *rx = &usb->rx;
982 spin_lock_init(&rx->lock);
983 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
984 rx->usb_packet_size = 512;
985 } else {
986 rx->usb_packet_size = 64;
988 ZD_ASSERT(rx->fragment_length == 0);
991 static inline void init_usb_tx(struct zd_usb *usb)
993 struct zd_usb_tx *tx = &usb->tx;
994 spin_lock_init(&tx->lock);
995 tx->enabled = 0;
996 tx->stopped = 0;
997 INIT_LIST_HEAD(&tx->free_urb_list);
998 tx->submitted_urbs = 0;
1001 void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
1002 struct usb_interface *intf)
1004 memset(usb, 0, sizeof(*usb));
1005 usb->intf = usb_get_intf(intf);
1006 usb_set_intfdata(usb->intf, hw);
1007 init_usb_interrupt(usb);
1008 init_usb_tx(usb);
1009 init_usb_rx(usb);
1012 void zd_usb_clear(struct zd_usb *usb)
1014 usb_set_intfdata(usb->intf, NULL);
1015 usb_put_intf(usb->intf);
1016 ZD_MEMCLEAR(usb, sizeof(*usb));
1017 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1020 static const char *speed(enum usb_device_speed speed)
1022 switch (speed) {
1023 case USB_SPEED_LOW:
1024 return "low";
1025 case USB_SPEED_FULL:
1026 return "full";
1027 case USB_SPEED_HIGH:
1028 return "high";
1029 default:
1030 return "unknown speed";
1034 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1036 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1037 le16_to_cpu(udev->descriptor.idVendor),
1038 le16_to_cpu(udev->descriptor.idProduct),
1039 get_bcdDevice(udev),
1040 speed(udev->speed));
1043 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1045 struct usb_device *udev = interface_to_usbdev(usb->intf);
1046 return scnprint_id(udev, buffer, size);
1049 #ifdef DEBUG
1050 static void print_id(struct usb_device *udev)
1052 char buffer[40];
1054 scnprint_id(udev, buffer, sizeof(buffer));
1055 buffer[sizeof(buffer)-1] = 0;
1056 dev_dbg_f(&udev->dev, "%s\n", buffer);
1058 #else
1059 #define print_id(udev) do { } while (0)
1060 #endif
1062 static int eject_installer(struct usb_interface *intf)
1064 struct usb_device *udev = interface_to_usbdev(intf);
1065 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1066 struct usb_endpoint_descriptor *endpoint;
1067 unsigned char *cmd;
1068 u8 bulk_out_ep;
1069 int r;
1071 /* Find bulk out endpoint */
1072 endpoint = &iface_desc->endpoint[1].desc;
1073 if (usb_endpoint_dir_out(endpoint) &&
1074 usb_endpoint_xfer_bulk(endpoint)) {
1075 bulk_out_ep = endpoint->bEndpointAddress;
1076 } else {
1077 dev_err(&udev->dev,
1078 "zd1211rw: Could not find bulk out endpoint\n");
1079 return -ENODEV;
1082 cmd = kzalloc(31, GFP_KERNEL);
1083 if (cmd == NULL)
1084 return -ENODEV;
1086 /* USB bulk command block */
1087 cmd[0] = 0x55; /* bulk command signature */
1088 cmd[1] = 0x53; /* bulk command signature */
1089 cmd[2] = 0x42; /* bulk command signature */
1090 cmd[3] = 0x43; /* bulk command signature */
1091 cmd[14] = 6; /* command length */
1093 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1094 cmd[19] = 0x2; /* eject disc */
1096 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1097 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1098 cmd, 31, NULL, 2000);
1099 kfree(cmd);
1100 if (r)
1101 return r;
1103 /* At this point, the device disconnects and reconnects with the real
1104 * ID numbers. */
1106 usb_set_intfdata(intf, NULL);
1107 return 0;
1110 int zd_usb_init_hw(struct zd_usb *usb)
1112 int r;
1113 struct zd_mac *mac = zd_usb_to_mac(usb);
1115 dev_dbg_f(zd_usb_dev(usb), "\n");
1117 r = upload_firmware(usb);
1118 if (r) {
1119 dev_err(zd_usb_dev(usb),
1120 "couldn't load firmware. Error number %d\n", r);
1121 return r;
1124 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1125 if (r) {
1126 dev_dbg_f(zd_usb_dev(usb),
1127 "couldn't reset configuration. Error number %d\n", r);
1128 return r;
1131 r = zd_mac_init_hw(mac->hw);
1132 if (r) {
1133 dev_dbg_f(zd_usb_dev(usb),
1134 "couldn't initialize mac. Error number %d\n", r);
1135 return r;
1138 usb->initialized = 1;
1139 return 0;
1142 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1144 int r;
1145 struct usb_device *udev = interface_to_usbdev(intf);
1146 struct zd_usb *usb;
1147 struct ieee80211_hw *hw = NULL;
1149 print_id(udev);
1151 if (id->driver_info & DEVICE_INSTALLER)
1152 return eject_installer(intf);
1154 switch (udev->speed) {
1155 case USB_SPEED_LOW:
1156 case USB_SPEED_FULL:
1157 case USB_SPEED_HIGH:
1158 break;
1159 default:
1160 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1161 r = -ENODEV;
1162 goto error;
1165 r = usb_reset_device(udev);
1166 if (r) {
1167 dev_err(&intf->dev,
1168 "couldn't reset usb device. Error number %d\n", r);
1169 goto error;
1172 hw = zd_mac_alloc_hw(intf);
1173 if (hw == NULL) {
1174 r = -ENOMEM;
1175 goto error;
1178 usb = &zd_hw_mac(hw)->chip.usb;
1179 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1181 r = zd_mac_preinit_hw(hw);
1182 if (r) {
1183 dev_dbg_f(&intf->dev,
1184 "couldn't initialize mac. Error number %d\n", r);
1185 goto error;
1188 r = ieee80211_register_hw(hw);
1189 if (r) {
1190 dev_dbg_f(&intf->dev,
1191 "couldn't register device. Error number %d\n", r);
1192 goto error;
1195 dev_dbg_f(&intf->dev, "successful\n");
1196 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
1197 return 0;
1198 error:
1199 usb_reset_device(interface_to_usbdev(intf));
1200 if (hw) {
1201 zd_mac_clear(zd_hw_mac(hw));
1202 ieee80211_free_hw(hw);
1204 return r;
1207 static void disconnect(struct usb_interface *intf)
1209 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
1210 struct zd_mac *mac;
1211 struct zd_usb *usb;
1213 /* Either something really bad happened, or we're just dealing with
1214 * a DEVICE_INSTALLER. */
1215 if (hw == NULL)
1216 return;
1218 mac = zd_hw_mac(hw);
1219 usb = &mac->chip.usb;
1221 dev_dbg_f(zd_usb_dev(usb), "\n");
1223 ieee80211_unregister_hw(hw);
1225 /* Just in case something has gone wrong! */
1226 zd_usb_disable_rx(usb);
1227 zd_usb_disable_int(usb);
1229 /* If the disconnect has been caused by a removal of the
1230 * driver module, the reset allows reloading of the driver. If the
1231 * reset will not be executed here, the upload of the firmware in the
1232 * probe function caused by the reloading of the driver will fail.
1234 usb_reset_device(interface_to_usbdev(intf));
1236 zd_mac_clear(mac);
1237 ieee80211_free_hw(hw);
1238 dev_dbg(&intf->dev, "disconnected\n");
1241 static struct usb_driver driver = {
1242 .name = KBUILD_MODNAME,
1243 .id_table = usb_ids,
1244 .probe = probe,
1245 .disconnect = disconnect,
1248 struct workqueue_struct *zd_workqueue;
1250 static int __init usb_init(void)
1252 int r;
1254 pr_debug("%s usb_init()\n", driver.name);
1256 zd_workqueue = create_singlethread_workqueue(driver.name);
1257 if (zd_workqueue == NULL) {
1258 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1259 return -ENOMEM;
1262 r = usb_register(&driver);
1263 if (r) {
1264 destroy_workqueue(zd_workqueue);
1265 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1266 driver.name, r);
1267 return r;
1270 pr_debug("%s initialized\n", driver.name);
1271 return 0;
1274 static void __exit usb_exit(void)
1276 pr_debug("%s usb_exit()\n", driver.name);
1277 usb_deregister(&driver);
1278 destroy_workqueue(zd_workqueue);
1281 module_init(usb_init);
1282 module_exit(usb_exit);
1284 static int usb_int_regs_length(unsigned int count)
1286 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1289 static void prepare_read_regs_int(struct zd_usb *usb)
1291 struct zd_usb_interrupt *intr = &usb->intr;
1293 spin_lock_irq(&intr->lock);
1294 intr->read_regs_enabled = 1;
1295 INIT_COMPLETION(intr->read_regs.completion);
1296 spin_unlock_irq(&intr->lock);
1299 static void disable_read_regs_int(struct zd_usb *usb)
1301 struct zd_usb_interrupt *intr = &usb->intr;
1303 spin_lock_irq(&intr->lock);
1304 intr->read_regs_enabled = 0;
1305 spin_unlock_irq(&intr->lock);
1308 static int get_results(struct zd_usb *usb, u16 *values,
1309 struct usb_req_read_regs *req, unsigned int count)
1311 int r;
1312 int i;
1313 struct zd_usb_interrupt *intr = &usb->intr;
1314 struct read_regs_int *rr = &intr->read_regs;
1315 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1317 spin_lock_irq(&intr->lock);
1319 r = -EIO;
1320 /* The created block size seems to be larger than expected.
1321 * However results appear to be correct.
1323 if (rr->length < usb_int_regs_length(count)) {
1324 dev_dbg_f(zd_usb_dev(usb),
1325 "error: actual length %d less than expected %d\n",
1326 rr->length, usb_int_regs_length(count));
1327 goto error_unlock;
1329 if (rr->length > sizeof(rr->buffer)) {
1330 dev_dbg_f(zd_usb_dev(usb),
1331 "error: actual length %d exceeds buffer size %zu\n",
1332 rr->length, sizeof(rr->buffer));
1333 goto error_unlock;
1336 for (i = 0; i < count; i++) {
1337 struct reg_data *rd = &regs->regs[i];
1338 if (rd->addr != req->addr[i]) {
1339 dev_dbg_f(zd_usb_dev(usb),
1340 "rd[%d] addr %#06hx expected %#06hx\n", i,
1341 le16_to_cpu(rd->addr),
1342 le16_to_cpu(req->addr[i]));
1343 goto error_unlock;
1345 values[i] = le16_to_cpu(rd->value);
1348 r = 0;
1349 error_unlock:
1350 spin_unlock_irq(&intr->lock);
1351 return r;
1354 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1355 const zd_addr_t *addresses, unsigned int count)
1357 int r;
1358 int i, req_len, actual_req_len;
1359 struct usb_device *udev;
1360 struct usb_req_read_regs *req = NULL;
1361 unsigned long timeout;
1363 if (count < 1) {
1364 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1365 return -EINVAL;
1367 if (count > USB_MAX_IOREAD16_COUNT) {
1368 dev_dbg_f(zd_usb_dev(usb),
1369 "error: count %u exceeds possible max %u\n",
1370 count, USB_MAX_IOREAD16_COUNT);
1371 return -EINVAL;
1373 if (in_atomic()) {
1374 dev_dbg_f(zd_usb_dev(usb),
1375 "error: io in atomic context not supported\n");
1376 return -EWOULDBLOCK;
1378 if (!usb_int_enabled(usb)) {
1379 dev_dbg_f(zd_usb_dev(usb),
1380 "error: usb interrupt not enabled\n");
1381 return -EWOULDBLOCK;
1384 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1385 req = kmalloc(req_len, GFP_KERNEL);
1386 if (!req)
1387 return -ENOMEM;
1388 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1389 for (i = 0; i < count; i++)
1390 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1392 udev = zd_usb_to_usbdev(usb);
1393 prepare_read_regs_int(usb);
1394 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1395 req, req_len, &actual_req_len, 1000 /* ms */);
1396 if (r) {
1397 dev_dbg_f(zd_usb_dev(usb),
1398 "error in usb_bulk_msg(). Error number %d\n", r);
1399 goto error;
1401 if (req_len != actual_req_len) {
1402 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1403 " req_len %d != actual_req_len %d\n",
1404 req_len, actual_req_len);
1405 r = -EIO;
1406 goto error;
1409 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1410 msecs_to_jiffies(1000));
1411 if (!timeout) {
1412 disable_read_regs_int(usb);
1413 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1414 r = -ETIMEDOUT;
1415 goto error;
1418 r = get_results(usb, values, req, count);
1419 error:
1420 kfree(req);
1421 return r;
1424 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1425 unsigned int count)
1427 int r;
1428 struct usb_device *udev;
1429 struct usb_req_write_regs *req = NULL;
1430 int i, req_len, actual_req_len;
1432 if (count == 0)
1433 return 0;
1434 if (count > USB_MAX_IOWRITE16_COUNT) {
1435 dev_dbg_f(zd_usb_dev(usb),
1436 "error: count %u exceeds possible max %u\n",
1437 count, USB_MAX_IOWRITE16_COUNT);
1438 return -EINVAL;
1440 if (in_atomic()) {
1441 dev_dbg_f(zd_usb_dev(usb),
1442 "error: io in atomic context not supported\n");
1443 return -EWOULDBLOCK;
1446 req_len = sizeof(struct usb_req_write_regs) +
1447 count * sizeof(struct reg_data);
1448 req = kmalloc(req_len, GFP_KERNEL);
1449 if (!req)
1450 return -ENOMEM;
1452 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1453 for (i = 0; i < count; i++) {
1454 struct reg_data *rw = &req->reg_writes[i];
1455 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1456 rw->value = cpu_to_le16(ioreqs[i].value);
1459 udev = zd_usb_to_usbdev(usb);
1460 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1461 req, req_len, &actual_req_len, 1000 /* ms */);
1462 if (r) {
1463 dev_dbg_f(zd_usb_dev(usb),
1464 "error in usb_bulk_msg(). Error number %d\n", r);
1465 goto error;
1467 if (req_len != actual_req_len) {
1468 dev_dbg_f(zd_usb_dev(usb),
1469 "error in usb_bulk_msg()"
1470 " req_len %d != actual_req_len %d\n",
1471 req_len, actual_req_len);
1472 r = -EIO;
1473 goto error;
1476 /* FALL-THROUGH with r == 0 */
1477 error:
1478 kfree(req);
1479 return r;
1482 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1484 int r;
1485 struct usb_device *udev;
1486 struct usb_req_rfwrite *req = NULL;
1487 int i, req_len, actual_req_len;
1488 u16 bit_value_template;
1490 if (in_atomic()) {
1491 dev_dbg_f(zd_usb_dev(usb),
1492 "error: io in atomic context not supported\n");
1493 return -EWOULDBLOCK;
1495 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1496 dev_dbg_f(zd_usb_dev(usb),
1497 "error: bits %d are smaller than"
1498 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1499 bits, USB_MIN_RFWRITE_BIT_COUNT);
1500 return -EINVAL;
1502 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1503 dev_dbg_f(zd_usb_dev(usb),
1504 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1505 bits, USB_MAX_RFWRITE_BIT_COUNT);
1506 return -EINVAL;
1508 #ifdef DEBUG
1509 if (value & (~0UL << bits)) {
1510 dev_dbg_f(zd_usb_dev(usb),
1511 "error: value %#09x has bits >= %d set\n",
1512 value, bits);
1513 return -EINVAL;
1515 #endif /* DEBUG */
1517 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1519 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1520 if (r) {
1521 dev_dbg_f(zd_usb_dev(usb),
1522 "error %d: Couldn't read CR203\n", r);
1523 goto out;
1525 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1527 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1528 req = kmalloc(req_len, GFP_KERNEL);
1529 if (!req)
1530 return -ENOMEM;
1532 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1533 /* 1: 3683a, but not used in ZYDAS driver */
1534 req->value = cpu_to_le16(2);
1535 req->bits = cpu_to_le16(bits);
1537 for (i = 0; i < bits; i++) {
1538 u16 bv = bit_value_template;
1539 if (value & (1 << (bits-1-i)))
1540 bv |= RF_DATA;
1541 req->bit_values[i] = cpu_to_le16(bv);
1544 udev = zd_usb_to_usbdev(usb);
1545 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1546 req, req_len, &actual_req_len, 1000 /* ms */);
1547 if (r) {
1548 dev_dbg_f(zd_usb_dev(usb),
1549 "error in usb_bulk_msg(). Error number %d\n", r);
1550 goto out;
1552 if (req_len != actual_req_len) {
1553 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1554 " req_len %d != actual_req_len %d\n",
1555 req_len, actual_req_len);
1556 r = -EIO;
1557 goto out;
1560 /* FALL-THROUGH with r == 0 */
1561 out:
1562 kfree(req);
1563 return r;