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>
37 static struct usb_device_id usb_ids[] = {
39 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
55 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
57 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
58 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
59 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
60 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
61 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
62 { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 },
64 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
66 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
67 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
70 { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
71 { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
73 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
80 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
81 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
82 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
83 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
84 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
85 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
86 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
87 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
88 { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B },
89 { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
90 /* "Driverless" devices that need ejecting */
91 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
92 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
96 MODULE_LICENSE("GPL");
97 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
98 MODULE_AUTHOR("Ulrich Kunitz");
99 MODULE_AUTHOR("Daniel Drake");
100 MODULE_VERSION("1.0");
101 MODULE_DEVICE_TABLE(usb, usb_ids);
103 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
104 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
106 /* USB device initialization */
107 static void int_urb_complete(struct urb *urb);
109 static int request_fw_file(
110 const struct firmware **fw, const char *name, struct device *device)
114 dev_dbg_f(device, "fw name %s\n", name);
116 r = request_firmware(fw, name, device);
119 "Could not load firmware file %s. Error number %d\n",
124 static inline u16 get_bcdDevice(const struct usb_device *udev)
126 return le16_to_cpu(udev->descriptor.bcdDevice);
129 enum upload_code_flags {
133 /* Ensures that MAX_TRANSFER_SIZE is even. */
134 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
136 static int upload_code(struct usb_device *udev,
137 const u8 *data, size_t size, u16 code_offset, int flags)
142 /* USB request blocks need "kmalloced" buffers.
144 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
146 dev_err(&udev->dev, "out of memory\n");
153 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
154 size : MAX_TRANSFER_SIZE;
156 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
158 memcpy(p, data, transfer_size);
159 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
160 USB_REQ_FIRMWARE_DOWNLOAD,
161 USB_DIR_OUT | USB_TYPE_VENDOR,
162 code_offset, 0, p, transfer_size, 1000 /* ms */);
165 "USB control request for firmware upload"
166 " failed. Error number %d\n", r);
169 transfer_size = r & ~1;
171 size -= transfer_size;
172 data += transfer_size;
173 code_offset += transfer_size/sizeof(u16);
176 if (flags & REBOOT) {
179 /* Use "DMA-aware" buffer. */
180 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
181 USB_REQ_FIRMWARE_CONFIRM,
182 USB_DIR_IN | USB_TYPE_VENDOR,
183 0, 0, p, sizeof(ret), 5000 /* ms */);
184 if (r != sizeof(ret)) {
186 "control request firmeware confirmation failed."
187 " Return value %d\n", r);
195 "Internal error while downloading."
196 " Firmware confirm return value %#04x\n",
201 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
211 static u16 get_word(const void *data, u16 offset)
213 const __le16 *p = data;
214 return le16_to_cpu(p[offset]);
217 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
220 scnprintf(buffer, size, "%s%s",
222 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
227 static int handle_version_mismatch(struct zd_usb *usb,
228 const struct firmware *ub_fw)
230 struct usb_device *udev = zd_usb_to_usbdev(usb);
231 const struct firmware *ur_fw = NULL;
236 r = request_fw_file(&ur_fw,
237 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
242 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
246 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
247 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
248 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
250 /* At this point, the vendor driver downloads the whole firmware
251 * image, hacks around with version IDs, and uploads it again,
252 * completely overwriting the boot code. We do not do this here as
253 * it is not required on any tested devices, and it is suspected to
256 release_firmware(ur_fw);
260 static int upload_firmware(struct zd_usb *usb)
265 struct usb_device *udev = zd_usb_to_usbdev(usb);
266 const struct firmware *ub_fw = NULL;
267 const struct firmware *uph_fw = NULL;
270 bcdDevice = get_bcdDevice(udev);
272 r = request_fw_file(&ub_fw,
273 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
278 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
280 if (fw_bcdDevice != bcdDevice) {
282 "firmware version %#06x and device bootcode version "
283 "%#06x differ\n", fw_bcdDevice, bcdDevice);
284 if (bcdDevice <= 0x4313)
285 dev_warn(&udev->dev, "device has old bootcode, please "
286 "report success or failure\n");
288 r = handle_version_mismatch(usb, ub_fw);
292 dev_dbg_f(&udev->dev,
293 "firmware device id %#06x is equal to the "
294 "actual device id\n", fw_bcdDevice);
298 r = request_fw_file(&uph_fw,
299 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
304 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
307 "Could not upload firmware code uph. Error number %d\n",
313 release_firmware(ub_fw);
314 release_firmware(uph_fw);
318 /* Read data from device address space using "firmware interface" which does
319 * not require firmware to be loaded. */
320 int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
323 struct usb_device *udev = zd_usb_to_usbdev(usb);
326 /* Use "DMA-aware" buffer. */
327 buf = kmalloc(len, GFP_KERNEL);
330 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
331 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
335 "read over firmware interface failed: %d\n", r);
337 } else if (r != len) {
339 "incomplete read over firmware interface: %d/%d\n",
345 memcpy(data, buf, len);
351 #define urb_dev(urb) (&(urb)->dev->dev)
353 static inline void handle_regs_int(struct urb *urb)
355 struct zd_usb *usb = urb->context;
356 struct zd_usb_interrupt *intr = &usb->intr;
360 ZD_ASSERT(in_interrupt());
361 spin_lock(&intr->lock);
363 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
364 if (int_num == CR_INTERRUPT) {
365 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
366 memcpy(&mac->intr_buffer, urb->transfer_buffer,
367 USB_MAX_EP_INT_BUFFER);
368 schedule_work(&mac->process_intr);
369 } else if (intr->read_regs_enabled) {
370 intr->read_regs.length = len = urb->actual_length;
372 if (len > sizeof(intr->read_regs.buffer))
373 len = sizeof(intr->read_regs.buffer);
374 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
375 intr->read_regs_enabled = 0;
376 complete(&intr->read_regs.completion);
381 spin_unlock(&intr->lock);
384 static void int_urb_complete(struct urb *urb)
387 struct usb_int_header *hdr;
389 switch (urb->status) {
403 if (urb->actual_length < sizeof(hdr)) {
404 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
408 hdr = urb->transfer_buffer;
409 if (hdr->type != USB_INT_TYPE) {
410 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
415 case USB_INT_ID_REGS:
416 handle_regs_int(urb);
418 case USB_INT_ID_RETRY_FAILED:
419 zd_mac_tx_failed(zd_usb_to_hw(urb->context));
422 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
423 (unsigned int)hdr->id);
428 r = usb_submit_urb(urb, GFP_ATOMIC);
430 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
435 kfree(urb->transfer_buffer);
438 static inline int int_urb_interval(struct usb_device *udev)
440 switch (udev->speed) {
451 static inline int usb_int_enabled(struct zd_usb *usb)
454 struct zd_usb_interrupt *intr = &usb->intr;
457 spin_lock_irqsave(&intr->lock, flags);
459 spin_unlock_irqrestore(&intr->lock, flags);
463 int zd_usb_enable_int(struct zd_usb *usb)
466 struct usb_device *udev;
467 struct zd_usb_interrupt *intr = &usb->intr;
468 void *transfer_buffer = NULL;
471 dev_dbg_f(zd_usb_dev(usb), "\n");
473 urb = usb_alloc_urb(0, GFP_KERNEL);
479 ZD_ASSERT(!irqs_disabled());
480 spin_lock_irq(&intr->lock);
482 spin_unlock_irq(&intr->lock);
487 spin_unlock_irq(&intr->lock);
489 /* TODO: make it a DMA buffer */
491 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
492 if (!transfer_buffer) {
493 dev_dbg_f(zd_usb_dev(usb),
494 "couldn't allocate transfer_buffer\n");
495 goto error_set_urb_null;
498 udev = zd_usb_to_usbdev(usb);
499 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
500 transfer_buffer, USB_MAX_EP_INT_BUFFER,
501 int_urb_complete, usb,
504 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
505 r = usb_submit_urb(urb, GFP_KERNEL);
507 dev_dbg_f(zd_usb_dev(usb),
508 "Couldn't submit urb. Error number %d\n", r);
514 kfree(transfer_buffer);
516 spin_lock_irq(&intr->lock);
518 spin_unlock_irq(&intr->lock);
525 void zd_usb_disable_int(struct zd_usb *usb)
528 struct zd_usb_interrupt *intr = &usb->intr;
531 spin_lock_irqsave(&intr->lock, flags);
534 spin_unlock_irqrestore(&intr->lock, flags);
538 spin_unlock_irqrestore(&intr->lock, flags);
541 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
545 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
549 const struct rx_length_info *length_info;
551 if (length < sizeof(struct rx_length_info)) {
552 /* It's not a complete packet anyhow. */
555 length_info = (struct rx_length_info *)
556 (buffer + length - sizeof(struct rx_length_info));
558 /* It might be that three frames are merged into a single URB
559 * transaction. We have to check for the length info tag.
561 * While testing we discovered that length_info might be unaligned,
562 * because if USB transactions are merged, the last packet will not
563 * be padded. Unaligned access might also happen if the length_info
564 * structure is not present.
566 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
568 unsigned int l, k, n;
569 for (i = 0, l = 0;; i++) {
570 k = get_unaligned_le16(&length_info->length[i]);
576 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
582 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
586 static void rx_urb_complete(struct urb *urb)
589 struct zd_usb_rx *rx;
593 switch (urb->status) {
604 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
608 buffer = urb->transfer_buffer;
609 length = urb->actual_length;
613 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
614 /* If there is an old first fragment, we don't care. */
615 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
616 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
617 spin_lock(&rx->lock);
618 memcpy(rx->fragment, buffer, length);
619 rx->fragment_length = length;
620 spin_unlock(&rx->lock);
624 spin_lock(&rx->lock);
625 if (rx->fragment_length > 0) {
626 /* We are on a second fragment, we believe */
627 ZD_ASSERT(length + rx->fragment_length <=
628 ARRAY_SIZE(rx->fragment));
629 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
630 memcpy(rx->fragment+rx->fragment_length, buffer, length);
631 handle_rx_packet(usb, rx->fragment,
632 rx->fragment_length + length);
633 rx->fragment_length = 0;
634 spin_unlock(&rx->lock);
636 spin_unlock(&rx->lock);
637 handle_rx_packet(usb, buffer, length);
641 usb_submit_urb(urb, GFP_ATOMIC);
644 static struct urb *alloc_rx_urb(struct zd_usb *usb)
646 struct usb_device *udev = zd_usb_to_usbdev(usb);
650 urb = usb_alloc_urb(0, GFP_KERNEL);
653 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
660 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
661 buffer, USB_MAX_RX_SIZE,
662 rx_urb_complete, usb);
663 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
668 static void free_rx_urb(struct urb *urb)
672 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
673 urb->transfer_buffer, urb->transfer_dma);
677 int zd_usb_enable_rx(struct zd_usb *usb)
680 struct zd_usb_rx *rx = &usb->rx;
683 dev_dbg_f(zd_usb_dev(usb), "\n");
686 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
689 for (i = 0; i < RX_URBS_COUNT; i++) {
690 urbs[i] = alloc_rx_urb(usb);
695 ZD_ASSERT(!irqs_disabled());
696 spin_lock_irq(&rx->lock);
698 spin_unlock_irq(&rx->lock);
703 rx->urbs_count = RX_URBS_COUNT;
704 spin_unlock_irq(&rx->lock);
706 for (i = 0; i < RX_URBS_COUNT; i++) {
707 r = usb_submit_urb(urbs[i], GFP_KERNEL);
714 for (i = 0; i < RX_URBS_COUNT; i++) {
715 usb_kill_urb(urbs[i]);
717 spin_lock_irq(&rx->lock);
720 spin_unlock_irq(&rx->lock);
723 for (i = 0; i < RX_URBS_COUNT; i++)
724 free_rx_urb(urbs[i]);
729 void zd_usb_disable_rx(struct zd_usb *usb)
735 struct zd_usb_rx *rx = &usb->rx;
737 spin_lock_irqsave(&rx->lock, flags);
739 count = rx->urbs_count;
740 spin_unlock_irqrestore(&rx->lock, flags);
744 for (i = 0; i < count; i++) {
745 usb_kill_urb(urbs[i]);
746 free_rx_urb(urbs[i]);
750 spin_lock_irqsave(&rx->lock, flags);
753 spin_unlock_irqrestore(&rx->lock, flags);
757 * zd_usb_disable_tx - disable transmission
758 * @usb: the zd1211rw-private USB structure
760 * Frees all URBs in the free list and marks the transmission as disabled.
762 void zd_usb_disable_tx(struct zd_usb *usb)
764 struct zd_usb_tx *tx = &usb->tx;
766 struct list_head *pos, *n;
768 spin_lock_irqsave(&tx->lock, flags);
769 list_for_each_safe(pos, n, &tx->free_urb_list) {
771 usb_free_urb(list_entry(pos, struct urb, urb_list));
774 tx->submitted_urbs = 0;
775 /* The stopped state is ignored, relying on ieee80211_wake_queues()
776 * in a potentionally following zd_usb_enable_tx().
778 spin_unlock_irqrestore(&tx->lock, flags);
782 * zd_usb_enable_tx - enables transmission
783 * @usb: a &struct zd_usb pointer
785 * This function enables transmission and prepares the &zd_usb_tx data
788 void zd_usb_enable_tx(struct zd_usb *usb)
791 struct zd_usb_tx *tx = &usb->tx;
793 spin_lock_irqsave(&tx->lock, flags);
795 tx->submitted_urbs = 0;
796 ieee80211_wake_queues(zd_usb_to_hw(usb));
798 spin_unlock_irqrestore(&tx->lock, flags);
802 * alloc_tx_urb - provides an tx URB
803 * @usb: a &struct zd_usb pointer
805 * Allocates a new URB. If possible takes the urb from the free list in
808 static struct urb *alloc_tx_urb(struct zd_usb *usb)
810 struct zd_usb_tx *tx = &usb->tx;
812 struct list_head *entry;
815 spin_lock_irqsave(&tx->lock, flags);
816 if (list_empty(&tx->free_urb_list)) {
817 urb = usb_alloc_urb(0, GFP_ATOMIC);
820 entry = tx->free_urb_list.next;
822 urb = list_entry(entry, struct urb, urb_list);
824 spin_unlock_irqrestore(&tx->lock, flags);
829 * free_tx_urb - frees a used tx URB
830 * @usb: a &struct zd_usb pointer
831 * @urb: URB to be freed
833 * Frees the the transmission URB, which means to put it on the free URB
836 static void free_tx_urb(struct zd_usb *usb, struct urb *urb)
838 struct zd_usb_tx *tx = &usb->tx;
841 spin_lock_irqsave(&tx->lock, flags);
846 list_add(&urb->urb_list, &tx->free_urb_list);
848 spin_unlock_irqrestore(&tx->lock, flags);
851 static void tx_dec_submitted_urbs(struct zd_usb *usb)
853 struct zd_usb_tx *tx = &usb->tx;
856 spin_lock_irqsave(&tx->lock, flags);
857 --tx->submitted_urbs;
858 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
859 ieee80211_wake_queues(zd_usb_to_hw(usb));
862 spin_unlock_irqrestore(&tx->lock, flags);
865 static void tx_inc_submitted_urbs(struct zd_usb *usb)
867 struct zd_usb_tx *tx = &usb->tx;
870 spin_lock_irqsave(&tx->lock, flags);
871 ++tx->submitted_urbs;
872 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
873 ieee80211_stop_queues(zd_usb_to_hw(usb));
876 spin_unlock_irqrestore(&tx->lock, flags);
880 * tx_urb_complete - completes the execution of an URB
883 * This function is called if the URB has been transferred to a device or an
884 * error has happened.
886 static void tx_urb_complete(struct urb *urb)
890 struct ieee80211_tx_info *info;
893 switch (urb->status) {
902 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
905 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
909 skb = (struct sk_buff *)urb->context;
911 * grab 'usb' pointer before handing off the skb (since
912 * it might be freed by zd_mac_tx_to_dev or mac80211)
914 info = IEEE80211_SKB_CB(skb);
915 usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb;
916 zd_mac_tx_to_dev(skb, urb->status);
917 free_tx_urb(usb, urb);
918 tx_dec_submitted_urbs(usb);
921 r = usb_submit_urb(urb, GFP_ATOMIC);
923 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
929 * zd_usb_tx: initiates transfer of a frame of the device
931 * @usb: the zd1211rw-private USB structure
932 * @skb: a &struct sk_buff pointer
934 * This function tranmits a frame to the device. It doesn't wait for
935 * completion. The frame must contain the control set and have all the
936 * control set information available.
938 * The function returns 0 if the transfer has been successfully initiated.
940 int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
943 struct usb_device *udev = zd_usb_to_usbdev(usb);
946 urb = alloc_tx_urb(usb);
952 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
953 skb->data, skb->len, tx_urb_complete, skb);
955 r = usb_submit_urb(urb, GFP_ATOMIC);
958 tx_inc_submitted_urbs(usb);
961 free_tx_urb(usb, urb);
966 static inline void init_usb_interrupt(struct zd_usb *usb)
968 struct zd_usb_interrupt *intr = &usb->intr;
970 spin_lock_init(&intr->lock);
971 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
972 init_completion(&intr->read_regs.completion);
973 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
976 static inline void init_usb_rx(struct zd_usb *usb)
978 struct zd_usb_rx *rx = &usb->rx;
979 spin_lock_init(&rx->lock);
980 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
981 rx->usb_packet_size = 512;
983 rx->usb_packet_size = 64;
985 ZD_ASSERT(rx->fragment_length == 0);
988 static inline void init_usb_tx(struct zd_usb *usb)
990 struct zd_usb_tx *tx = &usb->tx;
991 spin_lock_init(&tx->lock);
994 INIT_LIST_HEAD(&tx->free_urb_list);
995 tx->submitted_urbs = 0;
998 void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
999 struct usb_interface *intf)
1001 memset(usb, 0, sizeof(*usb));
1002 usb->intf = usb_get_intf(intf);
1003 usb_set_intfdata(usb->intf, hw);
1004 init_usb_interrupt(usb);
1009 void zd_usb_clear(struct zd_usb *usb)
1011 usb_set_intfdata(usb->intf, NULL);
1012 usb_put_intf(usb->intf);
1013 ZD_MEMCLEAR(usb, sizeof(*usb));
1014 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1017 static const char *speed(enum usb_device_speed speed)
1022 case USB_SPEED_FULL:
1024 case USB_SPEED_HIGH:
1027 return "unknown speed";
1031 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1033 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1034 le16_to_cpu(udev->descriptor.idVendor),
1035 le16_to_cpu(udev->descriptor.idProduct),
1036 get_bcdDevice(udev),
1037 speed(udev->speed));
1040 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1042 struct usb_device *udev = interface_to_usbdev(usb->intf);
1043 return scnprint_id(udev, buffer, size);
1047 static void print_id(struct usb_device *udev)
1051 scnprint_id(udev, buffer, sizeof(buffer));
1052 buffer[sizeof(buffer)-1] = 0;
1053 dev_dbg_f(&udev->dev, "%s\n", buffer);
1056 #define print_id(udev) do { } while (0)
1059 static int eject_installer(struct usb_interface *intf)
1061 struct usb_device *udev = interface_to_usbdev(intf);
1062 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1063 struct usb_endpoint_descriptor *endpoint;
1068 /* Find bulk out endpoint */
1069 endpoint = &iface_desc->endpoint[1].desc;
1070 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
1071 usb_endpoint_xfer_bulk(endpoint)) {
1072 bulk_out_ep = endpoint->bEndpointAddress;
1075 "zd1211rw: Could not find bulk out endpoint\n");
1079 cmd = kzalloc(31, GFP_KERNEL);
1083 /* USB bulk command block */
1084 cmd[0] = 0x55; /* bulk command signature */
1085 cmd[1] = 0x53; /* bulk command signature */
1086 cmd[2] = 0x42; /* bulk command signature */
1087 cmd[3] = 0x43; /* bulk command signature */
1088 cmd[14] = 6; /* command length */
1090 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1091 cmd[19] = 0x2; /* eject disc */
1093 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1094 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1095 cmd, 31, NULL, 2000);
1100 /* At this point, the device disconnects and reconnects with the real
1103 usb_set_intfdata(intf, NULL);
1107 int zd_usb_init_hw(struct zd_usb *usb)
1110 struct zd_mac *mac = zd_usb_to_mac(usb);
1112 dev_dbg_f(zd_usb_dev(usb), "\n");
1114 r = upload_firmware(usb);
1116 dev_err(zd_usb_dev(usb),
1117 "couldn't load firmware. Error number %d\n", r);
1121 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1123 dev_dbg_f(zd_usb_dev(usb),
1124 "couldn't reset configuration. Error number %d\n", r);
1128 r = zd_mac_init_hw(mac->hw);
1130 dev_dbg_f(zd_usb_dev(usb),
1131 "couldn't initialize mac. Error number %d\n", r);
1135 usb->initialized = 1;
1139 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1142 struct usb_device *udev = interface_to_usbdev(intf);
1144 struct ieee80211_hw *hw = NULL;
1148 if (id->driver_info & DEVICE_INSTALLER)
1149 return eject_installer(intf);
1151 switch (udev->speed) {
1153 case USB_SPEED_FULL:
1154 case USB_SPEED_HIGH:
1157 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1162 r = usb_reset_device(udev);
1165 "couldn't reset usb device. Error number %d\n", r);
1169 hw = zd_mac_alloc_hw(intf);
1175 usb = &zd_hw_mac(hw)->chip.usb;
1176 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1178 r = zd_mac_preinit_hw(hw);
1180 dev_dbg_f(&intf->dev,
1181 "couldn't initialize mac. Error number %d\n", r);
1185 r = ieee80211_register_hw(hw);
1187 dev_dbg_f(&intf->dev,
1188 "couldn't register device. Error number %d\n", r);
1192 dev_dbg_f(&intf->dev, "successful\n");
1193 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
1196 usb_reset_device(interface_to_usbdev(intf));
1198 zd_mac_clear(zd_hw_mac(hw));
1199 ieee80211_free_hw(hw);
1204 static void disconnect(struct usb_interface *intf)
1206 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
1210 /* Either something really bad happened, or we're just dealing with
1211 * a DEVICE_INSTALLER. */
1215 mac = zd_hw_mac(hw);
1216 usb = &mac->chip.usb;
1218 dev_dbg_f(zd_usb_dev(usb), "\n");
1220 ieee80211_unregister_hw(hw);
1222 /* Just in case something has gone wrong! */
1223 zd_usb_disable_rx(usb);
1224 zd_usb_disable_int(usb);
1226 /* If the disconnect has been caused by a removal of the
1227 * driver module, the reset allows reloading of the driver. If the
1228 * reset will not be executed here, the upload of the firmware in the
1229 * probe function caused by the reloading of the driver will fail.
1231 usb_reset_device(interface_to_usbdev(intf));
1234 ieee80211_free_hw(hw);
1235 dev_dbg(&intf->dev, "disconnected\n");
1238 static struct usb_driver driver = {
1239 .name = KBUILD_MODNAME,
1240 .id_table = usb_ids,
1242 .disconnect = disconnect,
1245 struct workqueue_struct *zd_workqueue;
1247 static int __init usb_init(void)
1251 pr_debug("%s usb_init()\n", driver.name);
1253 zd_workqueue = create_singlethread_workqueue(driver.name);
1254 if (zd_workqueue == NULL) {
1255 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1259 r = usb_register(&driver);
1261 destroy_workqueue(zd_workqueue);
1262 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1267 pr_debug("%s initialized\n", driver.name);
1271 static void __exit usb_exit(void)
1273 pr_debug("%s usb_exit()\n", driver.name);
1274 usb_deregister(&driver);
1275 destroy_workqueue(zd_workqueue);
1278 module_init(usb_init);
1279 module_exit(usb_exit);
1281 static int usb_int_regs_length(unsigned int count)
1283 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1286 static void prepare_read_regs_int(struct zd_usb *usb)
1288 struct zd_usb_interrupt *intr = &usb->intr;
1290 spin_lock_irq(&intr->lock);
1291 intr->read_regs_enabled = 1;
1292 INIT_COMPLETION(intr->read_regs.completion);
1293 spin_unlock_irq(&intr->lock);
1296 static void disable_read_regs_int(struct zd_usb *usb)
1298 struct zd_usb_interrupt *intr = &usb->intr;
1300 spin_lock_irq(&intr->lock);
1301 intr->read_regs_enabled = 0;
1302 spin_unlock_irq(&intr->lock);
1305 static int get_results(struct zd_usb *usb, u16 *values,
1306 struct usb_req_read_regs *req, unsigned int count)
1310 struct zd_usb_interrupt *intr = &usb->intr;
1311 struct read_regs_int *rr = &intr->read_regs;
1312 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1314 spin_lock_irq(&intr->lock);
1317 /* The created block size seems to be larger than expected.
1318 * However results appear to be correct.
1320 if (rr->length < usb_int_regs_length(count)) {
1321 dev_dbg_f(zd_usb_dev(usb),
1322 "error: actual length %d less than expected %d\n",
1323 rr->length, usb_int_regs_length(count));
1326 if (rr->length > sizeof(rr->buffer)) {
1327 dev_dbg_f(zd_usb_dev(usb),
1328 "error: actual length %d exceeds buffer size %zu\n",
1329 rr->length, sizeof(rr->buffer));
1333 for (i = 0; i < count; i++) {
1334 struct reg_data *rd = ®s->regs[i];
1335 if (rd->addr != req->addr[i]) {
1336 dev_dbg_f(zd_usb_dev(usb),
1337 "rd[%d] addr %#06hx expected %#06hx\n", i,
1338 le16_to_cpu(rd->addr),
1339 le16_to_cpu(req->addr[i]));
1342 values[i] = le16_to_cpu(rd->value);
1347 spin_unlock_irq(&intr->lock);
1351 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1352 const zd_addr_t *addresses, unsigned int count)
1355 int i, req_len, actual_req_len;
1356 struct usb_device *udev;
1357 struct usb_req_read_regs *req = NULL;
1358 unsigned long timeout;
1361 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1364 if (count > USB_MAX_IOREAD16_COUNT) {
1365 dev_dbg_f(zd_usb_dev(usb),
1366 "error: count %u exceeds possible max %u\n",
1367 count, USB_MAX_IOREAD16_COUNT);
1371 dev_dbg_f(zd_usb_dev(usb),
1372 "error: io in atomic context not supported\n");
1373 return -EWOULDBLOCK;
1375 if (!usb_int_enabled(usb)) {
1376 dev_dbg_f(zd_usb_dev(usb),
1377 "error: usb interrupt not enabled\n");
1378 return -EWOULDBLOCK;
1381 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1382 req = kmalloc(req_len, GFP_KERNEL);
1385 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1386 for (i = 0; i < count; i++)
1387 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1389 udev = zd_usb_to_usbdev(usb);
1390 prepare_read_regs_int(usb);
1391 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1392 req, req_len, &actual_req_len, 1000 /* ms */);
1394 dev_dbg_f(zd_usb_dev(usb),
1395 "error in usb_bulk_msg(). Error number %d\n", r);
1398 if (req_len != actual_req_len) {
1399 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1400 " req_len %d != actual_req_len %d\n",
1401 req_len, actual_req_len);
1406 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1407 msecs_to_jiffies(1000));
1409 disable_read_regs_int(usb);
1410 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1415 r = get_results(usb, values, req, count);
1421 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1425 struct usb_device *udev;
1426 struct usb_req_write_regs *req = NULL;
1427 int i, req_len, actual_req_len;
1431 if (count > USB_MAX_IOWRITE16_COUNT) {
1432 dev_dbg_f(zd_usb_dev(usb),
1433 "error: count %u exceeds possible max %u\n",
1434 count, USB_MAX_IOWRITE16_COUNT);
1438 dev_dbg_f(zd_usb_dev(usb),
1439 "error: io in atomic context not supported\n");
1440 return -EWOULDBLOCK;
1443 req_len = sizeof(struct usb_req_write_regs) +
1444 count * sizeof(struct reg_data);
1445 req = kmalloc(req_len, GFP_KERNEL);
1449 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1450 for (i = 0; i < count; i++) {
1451 struct reg_data *rw = &req->reg_writes[i];
1452 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1453 rw->value = cpu_to_le16(ioreqs[i].value);
1456 udev = zd_usb_to_usbdev(usb);
1457 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1458 req, req_len, &actual_req_len, 1000 /* ms */);
1460 dev_dbg_f(zd_usb_dev(usb),
1461 "error in usb_bulk_msg(). Error number %d\n", r);
1464 if (req_len != actual_req_len) {
1465 dev_dbg_f(zd_usb_dev(usb),
1466 "error in usb_bulk_msg()"
1467 " req_len %d != actual_req_len %d\n",
1468 req_len, actual_req_len);
1473 /* FALL-THROUGH with r == 0 */
1479 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1482 struct usb_device *udev;
1483 struct usb_req_rfwrite *req = NULL;
1484 int i, req_len, actual_req_len;
1485 u16 bit_value_template;
1488 dev_dbg_f(zd_usb_dev(usb),
1489 "error: io in atomic context not supported\n");
1490 return -EWOULDBLOCK;
1492 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1493 dev_dbg_f(zd_usb_dev(usb),
1494 "error: bits %d are smaller than"
1495 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1496 bits, USB_MIN_RFWRITE_BIT_COUNT);
1499 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1500 dev_dbg_f(zd_usb_dev(usb),
1501 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1502 bits, USB_MAX_RFWRITE_BIT_COUNT);
1506 if (value & (~0UL << bits)) {
1507 dev_dbg_f(zd_usb_dev(usb),
1508 "error: value %#09x has bits >= %d set\n",
1514 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1516 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1518 dev_dbg_f(zd_usb_dev(usb),
1519 "error %d: Couldn't read CR203\n", r);
1522 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1524 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1525 req = kmalloc(req_len, GFP_KERNEL);
1529 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1530 /* 1: 3683a, but not used in ZYDAS driver */
1531 req->value = cpu_to_le16(2);
1532 req->bits = cpu_to_le16(bits);
1534 for (i = 0; i < bits; i++) {
1535 u16 bv = bit_value_template;
1536 if (value & (1 << (bits-1-i)))
1538 req->bit_values[i] = cpu_to_le16(bv);
1541 udev = zd_usb_to_usbdev(usb);
1542 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1543 req, req_len, &actual_req_len, 1000 /* ms */);
1545 dev_dbg_f(zd_usb_dev(usb),
1546 "error in usb_bulk_msg(). Error number %d\n", r);
1549 if (req_len != actual_req_len) {
1550 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1551 " req_len %d != actual_req_len %d\n",
1552 req_len, actual_req_len);
1557 /* FALL-THROUGH with r == 0 */