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(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
55 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
57 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
58 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
59 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
60 { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
62 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
63 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
64 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
66 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
67 { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
70 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
71 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
73 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
80 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
81 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
82 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
83 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
84 /* "Driverless" devices that need ejecting */
85 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
86 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
90 MODULE_LICENSE("GPL");
91 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
92 MODULE_AUTHOR("Ulrich Kunitz");
93 MODULE_AUTHOR("Daniel Drake");
94 MODULE_VERSION("1.0");
95 MODULE_DEVICE_TABLE(usb, usb_ids);
97 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
98 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
100 /* USB device initialization */
101 static void int_urb_complete(struct urb *urb);
103 static int request_fw_file(
104 const struct firmware **fw, const char *name, struct device *device)
108 dev_dbg_f(device, "fw name %s\n", name);
110 r = request_firmware(fw, name, device);
113 "Could not load firmware file %s. Error number %d\n",
118 static inline u16 get_bcdDevice(const struct usb_device *udev)
120 return le16_to_cpu(udev->descriptor.bcdDevice);
123 enum upload_code_flags {
127 /* Ensures that MAX_TRANSFER_SIZE is even. */
128 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
130 static int upload_code(struct usb_device *udev,
131 const u8 *data, size_t size, u16 code_offset, int flags)
136 /* USB request blocks need "kmalloced" buffers.
138 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
140 dev_err(&udev->dev, "out of memory\n");
147 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
148 size : MAX_TRANSFER_SIZE;
150 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
152 memcpy(p, data, transfer_size);
153 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
154 USB_REQ_FIRMWARE_DOWNLOAD,
155 USB_DIR_OUT | USB_TYPE_VENDOR,
156 code_offset, 0, p, transfer_size, 1000 /* ms */);
159 "USB control request for firmware upload"
160 " failed. Error number %d\n", r);
163 transfer_size = r & ~1;
165 size -= transfer_size;
166 data += transfer_size;
167 code_offset += transfer_size/sizeof(u16);
170 if (flags & REBOOT) {
173 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
174 USB_REQ_FIRMWARE_CONFIRM,
175 USB_DIR_IN | USB_TYPE_VENDOR,
176 0, 0, &ret, sizeof(ret), 5000 /* ms */);
177 if (r != sizeof(ret)) {
179 "control request firmeware confirmation failed."
180 " Return value %d\n", r);
187 "Internal error while downloading."
188 " Firmware confirm return value %#04x\n",
193 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
203 static u16 get_word(const void *data, u16 offset)
205 const __le16 *p = data;
206 return le16_to_cpu(p[offset]);
209 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
212 scnprintf(buffer, size, "%s%s",
214 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
219 static int handle_version_mismatch(struct zd_usb *usb,
220 const struct firmware *ub_fw)
222 struct usb_device *udev = zd_usb_to_usbdev(usb);
223 const struct firmware *ur_fw = NULL;
228 r = request_fw_file(&ur_fw,
229 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
234 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
238 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
239 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
240 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
242 /* At this point, the vendor driver downloads the whole firmware
243 * image, hacks around with version IDs, and uploads it again,
244 * completely overwriting the boot code. We do not do this here as
245 * it is not required on any tested devices, and it is suspected to
248 release_firmware(ur_fw);
252 static int upload_firmware(struct zd_usb *usb)
257 struct usb_device *udev = zd_usb_to_usbdev(usb);
258 const struct firmware *ub_fw = NULL;
259 const struct firmware *uph_fw = NULL;
262 bcdDevice = get_bcdDevice(udev);
264 r = request_fw_file(&ub_fw,
265 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
270 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
272 if (fw_bcdDevice != bcdDevice) {
274 "firmware version %#06x and device bootcode version "
275 "%#06x differ\n", fw_bcdDevice, bcdDevice);
276 if (bcdDevice <= 0x4313)
277 dev_warn(&udev->dev, "device has old bootcode, please "
278 "report success or failure\n");
280 r = handle_version_mismatch(usb, ub_fw);
284 dev_dbg_f(&udev->dev,
285 "firmware device id %#06x is equal to the "
286 "actual device id\n", fw_bcdDevice);
290 r = request_fw_file(&uph_fw,
291 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
296 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
299 "Could not upload firmware code uph. Error number %d\n",
305 release_firmware(ub_fw);
306 release_firmware(uph_fw);
310 /* Read data from device address space using "firmware interface" which does
311 * not require firmware to be loaded. */
312 int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
315 struct usb_device *udev = zd_usb_to_usbdev(usb);
317 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
318 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
322 "read over firmware interface failed: %d\n", r);
324 } else if (r != len) {
326 "incomplete read over firmware interface: %d/%d\n",
334 #define urb_dev(urb) (&(urb)->dev->dev)
336 static inline void handle_regs_int(struct urb *urb)
338 struct zd_usb *usb = urb->context;
339 struct zd_usb_interrupt *intr = &usb->intr;
343 ZD_ASSERT(in_interrupt());
344 spin_lock(&intr->lock);
346 int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
347 if (int_num == CR_INTERRUPT) {
348 struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
349 memcpy(&mac->intr_buffer, urb->transfer_buffer,
350 USB_MAX_EP_INT_BUFFER);
351 schedule_work(&mac->process_intr);
352 } else if (intr->read_regs_enabled) {
353 intr->read_regs.length = len = urb->actual_length;
355 if (len > sizeof(intr->read_regs.buffer))
356 len = sizeof(intr->read_regs.buffer);
357 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
358 intr->read_regs_enabled = 0;
359 complete(&intr->read_regs.completion);
364 spin_unlock(&intr->lock);
367 static void int_urb_complete(struct urb *urb)
370 struct usb_int_header *hdr;
372 switch (urb->status) {
386 if (urb->actual_length < sizeof(hdr)) {
387 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
391 hdr = urb->transfer_buffer;
392 if (hdr->type != USB_INT_TYPE) {
393 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
398 case USB_INT_ID_REGS:
399 handle_regs_int(urb);
401 case USB_INT_ID_RETRY_FAILED:
402 zd_mac_tx_failed(zd_usb_to_hw(urb->context));
405 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
406 (unsigned int)hdr->id);
411 r = usb_submit_urb(urb, GFP_ATOMIC);
413 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
418 kfree(urb->transfer_buffer);
421 static inline int int_urb_interval(struct usb_device *udev)
423 switch (udev->speed) {
434 static inline int usb_int_enabled(struct zd_usb *usb)
437 struct zd_usb_interrupt *intr = &usb->intr;
440 spin_lock_irqsave(&intr->lock, flags);
442 spin_unlock_irqrestore(&intr->lock, flags);
446 int zd_usb_enable_int(struct zd_usb *usb)
449 struct usb_device *udev;
450 struct zd_usb_interrupt *intr = &usb->intr;
451 void *transfer_buffer = NULL;
454 dev_dbg_f(zd_usb_dev(usb), "\n");
456 urb = usb_alloc_urb(0, GFP_KERNEL);
462 ZD_ASSERT(!irqs_disabled());
463 spin_lock_irq(&intr->lock);
465 spin_unlock_irq(&intr->lock);
470 spin_unlock_irq(&intr->lock);
472 /* TODO: make it a DMA buffer */
474 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
475 if (!transfer_buffer) {
476 dev_dbg_f(zd_usb_dev(usb),
477 "couldn't allocate transfer_buffer\n");
478 goto error_set_urb_null;
481 udev = zd_usb_to_usbdev(usb);
482 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
483 transfer_buffer, USB_MAX_EP_INT_BUFFER,
484 int_urb_complete, usb,
487 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
488 r = usb_submit_urb(urb, GFP_KERNEL);
490 dev_dbg_f(zd_usb_dev(usb),
491 "Couldn't submit urb. Error number %d\n", r);
497 kfree(transfer_buffer);
499 spin_lock_irq(&intr->lock);
501 spin_unlock_irq(&intr->lock);
508 void zd_usb_disable_int(struct zd_usb *usb)
511 struct zd_usb_interrupt *intr = &usb->intr;
514 spin_lock_irqsave(&intr->lock, flags);
517 spin_unlock_irqrestore(&intr->lock, flags);
521 spin_unlock_irqrestore(&intr->lock, flags);
524 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
528 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
532 const struct rx_length_info *length_info;
534 if (length < sizeof(struct rx_length_info)) {
535 /* It's not a complete packet anyhow. */
538 length_info = (struct rx_length_info *)
539 (buffer + length - sizeof(struct rx_length_info));
541 /* It might be that three frames are merged into a single URB
542 * transaction. We have to check for the length info tag.
544 * While testing we discovered that length_info might be unaligned,
545 * because if USB transactions are merged, the last packet will not
546 * be padded. Unaligned access might also happen if the length_info
547 * structure is not present.
549 if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
551 unsigned int l, k, n;
552 for (i = 0, l = 0;; i++) {
553 k = get_unaligned_le16(&length_info->length[i]);
559 zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
565 zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
569 static void rx_urb_complete(struct urb *urb)
572 struct zd_usb_rx *rx;
576 switch (urb->status) {
587 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
591 buffer = urb->transfer_buffer;
592 length = urb->actual_length;
596 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
597 /* If there is an old first fragment, we don't care. */
598 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
599 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
600 spin_lock(&rx->lock);
601 memcpy(rx->fragment, buffer, length);
602 rx->fragment_length = length;
603 spin_unlock(&rx->lock);
607 spin_lock(&rx->lock);
608 if (rx->fragment_length > 0) {
609 /* We are on a second fragment, we believe */
610 ZD_ASSERT(length + rx->fragment_length <=
611 ARRAY_SIZE(rx->fragment));
612 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
613 memcpy(rx->fragment+rx->fragment_length, buffer, length);
614 handle_rx_packet(usb, rx->fragment,
615 rx->fragment_length + length);
616 rx->fragment_length = 0;
617 spin_unlock(&rx->lock);
619 spin_unlock(&rx->lock);
620 handle_rx_packet(usb, buffer, length);
624 usb_submit_urb(urb, GFP_ATOMIC);
627 static struct urb *alloc_rx_urb(struct zd_usb *usb)
629 struct usb_device *udev = zd_usb_to_usbdev(usb);
633 urb = usb_alloc_urb(0, GFP_KERNEL);
636 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
643 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
644 buffer, USB_MAX_RX_SIZE,
645 rx_urb_complete, usb);
646 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
651 static void free_rx_urb(struct urb *urb)
655 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
656 urb->transfer_buffer, urb->transfer_dma);
660 int zd_usb_enable_rx(struct zd_usb *usb)
663 struct zd_usb_rx *rx = &usb->rx;
666 dev_dbg_f(zd_usb_dev(usb), "\n");
669 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
672 for (i = 0; i < RX_URBS_COUNT; i++) {
673 urbs[i] = alloc_rx_urb(usb);
678 ZD_ASSERT(!irqs_disabled());
679 spin_lock_irq(&rx->lock);
681 spin_unlock_irq(&rx->lock);
686 rx->urbs_count = RX_URBS_COUNT;
687 spin_unlock_irq(&rx->lock);
689 for (i = 0; i < RX_URBS_COUNT; i++) {
690 r = usb_submit_urb(urbs[i], GFP_KERNEL);
697 for (i = 0; i < RX_URBS_COUNT; i++) {
698 usb_kill_urb(urbs[i]);
700 spin_lock_irq(&rx->lock);
703 spin_unlock_irq(&rx->lock);
706 for (i = 0; i < RX_URBS_COUNT; i++)
707 free_rx_urb(urbs[i]);
712 void zd_usb_disable_rx(struct zd_usb *usb)
718 struct zd_usb_rx *rx = &usb->rx;
720 spin_lock_irqsave(&rx->lock, flags);
722 count = rx->urbs_count;
723 spin_unlock_irqrestore(&rx->lock, flags);
727 for (i = 0; i < count; i++) {
728 usb_kill_urb(urbs[i]);
729 free_rx_urb(urbs[i]);
733 spin_lock_irqsave(&rx->lock, flags);
736 spin_unlock_irqrestore(&rx->lock, flags);
740 * zd_usb_disable_tx - disable transmission
741 * @usb: the zd1211rw-private USB structure
743 * Frees all URBs in the free list and marks the transmission as disabled.
745 void zd_usb_disable_tx(struct zd_usb *usb)
747 struct zd_usb_tx *tx = &usb->tx;
749 struct list_head *pos, *n;
751 spin_lock_irqsave(&tx->lock, flags);
752 list_for_each_safe(pos, n, &tx->free_urb_list) {
754 usb_free_urb(list_entry(pos, struct urb, urb_list));
757 tx->submitted_urbs = 0;
758 /* The stopped state is ignored, relying on ieee80211_wake_queues()
759 * in a potentionally following zd_usb_enable_tx().
761 spin_unlock_irqrestore(&tx->lock, flags);
765 * zd_usb_enable_tx - enables transmission
766 * @usb: a &struct zd_usb pointer
768 * This function enables transmission and prepares the &zd_usb_tx data
771 void zd_usb_enable_tx(struct zd_usb *usb)
774 struct zd_usb_tx *tx = &usb->tx;
776 spin_lock_irqsave(&tx->lock, flags);
778 tx->submitted_urbs = 0;
779 ieee80211_wake_queues(zd_usb_to_hw(usb));
781 spin_unlock_irqrestore(&tx->lock, flags);
785 * alloc_tx_urb - provides an tx URB
786 * @usb: a &struct zd_usb pointer
788 * Allocates a new URB. If possible takes the urb from the free list in
791 static struct urb *alloc_tx_urb(struct zd_usb *usb)
793 struct zd_usb_tx *tx = &usb->tx;
795 struct list_head *entry;
798 spin_lock_irqsave(&tx->lock, flags);
799 if (list_empty(&tx->free_urb_list)) {
800 urb = usb_alloc_urb(0, GFP_ATOMIC);
803 entry = tx->free_urb_list.next;
805 urb = list_entry(entry, struct urb, urb_list);
807 spin_unlock_irqrestore(&tx->lock, flags);
812 * free_tx_urb - frees a used tx URB
813 * @usb: a &struct zd_usb pointer
814 * @urb: URB to be freed
816 * Frees the the transmission URB, which means to put it on the free URB
819 static void free_tx_urb(struct zd_usb *usb, struct urb *urb)
821 struct zd_usb_tx *tx = &usb->tx;
824 spin_lock_irqsave(&tx->lock, flags);
829 list_add(&urb->urb_list, &tx->free_urb_list);
831 spin_unlock_irqrestore(&tx->lock, flags);
834 static void tx_dec_submitted_urbs(struct zd_usb *usb)
836 struct zd_usb_tx *tx = &usb->tx;
839 spin_lock_irqsave(&tx->lock, flags);
840 --tx->submitted_urbs;
841 if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
842 ieee80211_wake_queues(zd_usb_to_hw(usb));
845 spin_unlock_irqrestore(&tx->lock, flags);
848 static void tx_inc_submitted_urbs(struct zd_usb *usb)
850 struct zd_usb_tx *tx = &usb->tx;
853 spin_lock_irqsave(&tx->lock, flags);
854 ++tx->submitted_urbs;
855 if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
856 ieee80211_stop_queues(zd_usb_to_hw(usb));
859 spin_unlock_irqrestore(&tx->lock, flags);
863 * tx_urb_complete - completes the execution of an URB
866 * This function is called if the URB has been transferred to a device or an
867 * error has happened.
869 static void tx_urb_complete(struct urb *urb)
873 struct zd_tx_skb_control_block *cb;
876 switch (urb->status) {
885 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
888 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
892 skb = (struct sk_buff *)urb->context;
894 * grab 'usb' pointer before handing off the skb (since
895 * it might be freed by zd_mac_tx_to_dev or mac80211)
897 cb = (struct zd_tx_skb_control_block *)skb->cb;
898 usb = &zd_hw_mac(cb->hw)->chip.usb;
899 zd_mac_tx_to_dev(skb, urb->status);
900 free_tx_urb(usb, urb);
901 tx_dec_submitted_urbs(usb);
904 r = usb_submit_urb(urb, GFP_ATOMIC);
906 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
912 * zd_usb_tx: initiates transfer of a frame of the device
914 * @usb: the zd1211rw-private USB structure
915 * @skb: a &struct sk_buff pointer
917 * This function tranmits a frame to the device. It doesn't wait for
918 * completion. The frame must contain the control set and have all the
919 * control set information available.
921 * The function returns 0 if the transfer has been successfully initiated.
923 int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
926 struct usb_device *udev = zd_usb_to_usbdev(usb);
929 urb = alloc_tx_urb(usb);
935 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
936 skb->data, skb->len, tx_urb_complete, skb);
938 r = usb_submit_urb(urb, GFP_ATOMIC);
941 tx_inc_submitted_urbs(usb);
944 free_tx_urb(usb, urb);
949 static inline void init_usb_interrupt(struct zd_usb *usb)
951 struct zd_usb_interrupt *intr = &usb->intr;
953 spin_lock_init(&intr->lock);
954 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
955 init_completion(&intr->read_regs.completion);
956 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
959 static inline void init_usb_rx(struct zd_usb *usb)
961 struct zd_usb_rx *rx = &usb->rx;
962 spin_lock_init(&rx->lock);
963 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
964 rx->usb_packet_size = 512;
966 rx->usb_packet_size = 64;
968 ZD_ASSERT(rx->fragment_length == 0);
971 static inline void init_usb_tx(struct zd_usb *usb)
973 struct zd_usb_tx *tx = &usb->tx;
974 spin_lock_init(&tx->lock);
977 INIT_LIST_HEAD(&tx->free_urb_list);
978 tx->submitted_urbs = 0;
981 void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
982 struct usb_interface *intf)
984 memset(usb, 0, sizeof(*usb));
985 usb->intf = usb_get_intf(intf);
986 usb_set_intfdata(usb->intf, hw);
987 init_usb_interrupt(usb);
992 void zd_usb_clear(struct zd_usb *usb)
994 usb_set_intfdata(usb->intf, NULL);
995 usb_put_intf(usb->intf);
996 ZD_MEMCLEAR(usb, sizeof(*usb));
997 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1000 static const char *speed(enum usb_device_speed speed)
1005 case USB_SPEED_FULL:
1007 case USB_SPEED_HIGH:
1010 return "unknown speed";
1014 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
1016 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
1017 le16_to_cpu(udev->descriptor.idVendor),
1018 le16_to_cpu(udev->descriptor.idProduct),
1019 get_bcdDevice(udev),
1020 speed(udev->speed));
1023 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
1025 struct usb_device *udev = interface_to_usbdev(usb->intf);
1026 return scnprint_id(udev, buffer, size);
1030 static void print_id(struct usb_device *udev)
1034 scnprint_id(udev, buffer, sizeof(buffer));
1035 buffer[sizeof(buffer)-1] = 0;
1036 dev_dbg_f(&udev->dev, "%s\n", buffer);
1039 #define print_id(udev) do { } while (0)
1042 static int eject_installer(struct usb_interface *intf)
1044 struct usb_device *udev = interface_to_usbdev(intf);
1045 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1046 struct usb_endpoint_descriptor *endpoint;
1051 /* Find bulk out endpoint */
1052 endpoint = &iface_desc->endpoint[1].desc;
1053 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
1054 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1055 USB_ENDPOINT_XFER_BULK) {
1056 bulk_out_ep = endpoint->bEndpointAddress;
1059 "zd1211rw: Could not find bulk out endpoint\n");
1063 cmd = kzalloc(31, GFP_KERNEL);
1067 /* USB bulk command block */
1068 cmd[0] = 0x55; /* bulk command signature */
1069 cmd[1] = 0x53; /* bulk command signature */
1070 cmd[2] = 0x42; /* bulk command signature */
1071 cmd[3] = 0x43; /* bulk command signature */
1072 cmd[14] = 6; /* command length */
1074 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1075 cmd[19] = 0x2; /* eject disc */
1077 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
1078 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1079 cmd, 31, NULL, 2000);
1084 /* At this point, the device disconnects and reconnects with the real
1087 usb_set_intfdata(intf, NULL);
1091 int zd_usb_init_hw(struct zd_usb *usb)
1094 struct zd_mac *mac = zd_usb_to_mac(usb);
1096 dev_dbg_f(zd_usb_dev(usb), "\n");
1098 r = upload_firmware(usb);
1100 dev_err(zd_usb_dev(usb),
1101 "couldn't load firmware. Error number %d\n", r);
1105 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
1107 dev_dbg_f(zd_usb_dev(usb),
1108 "couldn't reset configuration. Error number %d\n", r);
1112 r = zd_mac_init_hw(mac->hw);
1114 dev_dbg_f(zd_usb_dev(usb),
1115 "couldn't initialize mac. Error number %d\n", r);
1119 usb->initialized = 1;
1123 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1126 struct usb_device *udev = interface_to_usbdev(intf);
1128 struct ieee80211_hw *hw = NULL;
1132 if (id->driver_info & DEVICE_INSTALLER)
1133 return eject_installer(intf);
1135 switch (udev->speed) {
1137 case USB_SPEED_FULL:
1138 case USB_SPEED_HIGH:
1141 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1146 r = usb_reset_device(udev);
1149 "couldn't reset usb device. Error number %d\n", r);
1153 hw = zd_mac_alloc_hw(intf);
1159 usb = &zd_hw_mac(hw)->chip.usb;
1160 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1162 r = zd_mac_preinit_hw(hw);
1164 dev_dbg_f(&intf->dev,
1165 "couldn't initialize mac. Error number %d\n", r);
1169 r = ieee80211_register_hw(hw);
1171 dev_dbg_f(&intf->dev,
1172 "couldn't register device. Error number %d\n", r);
1176 dev_dbg_f(&intf->dev, "successful\n");
1177 dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
1180 usb_reset_device(interface_to_usbdev(intf));
1182 zd_mac_clear(zd_hw_mac(hw));
1183 ieee80211_free_hw(hw);
1188 static void disconnect(struct usb_interface *intf)
1190 struct ieee80211_hw *hw = zd_intf_to_hw(intf);
1194 /* Either something really bad happened, or we're just dealing with
1195 * a DEVICE_INSTALLER. */
1199 mac = zd_hw_mac(hw);
1200 usb = &mac->chip.usb;
1202 dev_dbg_f(zd_usb_dev(usb), "\n");
1204 ieee80211_unregister_hw(hw);
1206 /* Just in case something has gone wrong! */
1207 zd_usb_disable_rx(usb);
1208 zd_usb_disable_int(usb);
1210 /* If the disconnect has been caused by a removal of the
1211 * driver module, the reset allows reloading of the driver. If the
1212 * reset will not be executed here, the upload of the firmware in the
1213 * probe function caused by the reloading of the driver will fail.
1215 usb_reset_device(interface_to_usbdev(intf));
1218 ieee80211_free_hw(hw);
1219 dev_dbg(&intf->dev, "disconnected\n");
1222 static struct usb_driver driver = {
1223 .name = KBUILD_MODNAME,
1224 .id_table = usb_ids,
1226 .disconnect = disconnect,
1229 struct workqueue_struct *zd_workqueue;
1231 static int __init usb_init(void)
1235 pr_debug("%s usb_init()\n", driver.name);
1237 zd_workqueue = create_singlethread_workqueue(driver.name);
1238 if (zd_workqueue == NULL) {
1239 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1243 r = usb_register(&driver);
1245 destroy_workqueue(zd_workqueue);
1246 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1251 pr_debug("%s initialized\n", driver.name);
1255 static void __exit usb_exit(void)
1257 pr_debug("%s usb_exit()\n", driver.name);
1258 usb_deregister(&driver);
1259 destroy_workqueue(zd_workqueue);
1262 module_init(usb_init);
1263 module_exit(usb_exit);
1265 static int usb_int_regs_length(unsigned int count)
1267 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1270 static void prepare_read_regs_int(struct zd_usb *usb)
1272 struct zd_usb_interrupt *intr = &usb->intr;
1274 spin_lock_irq(&intr->lock);
1275 intr->read_regs_enabled = 1;
1276 INIT_COMPLETION(intr->read_regs.completion);
1277 spin_unlock_irq(&intr->lock);
1280 static void disable_read_regs_int(struct zd_usb *usb)
1282 struct zd_usb_interrupt *intr = &usb->intr;
1284 spin_lock_irq(&intr->lock);
1285 intr->read_regs_enabled = 0;
1286 spin_unlock_irq(&intr->lock);
1289 static int get_results(struct zd_usb *usb, u16 *values,
1290 struct usb_req_read_regs *req, unsigned int count)
1294 struct zd_usb_interrupt *intr = &usb->intr;
1295 struct read_regs_int *rr = &intr->read_regs;
1296 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1298 spin_lock_irq(&intr->lock);
1301 /* The created block size seems to be larger than expected.
1302 * However results appear to be correct.
1304 if (rr->length < usb_int_regs_length(count)) {
1305 dev_dbg_f(zd_usb_dev(usb),
1306 "error: actual length %d less than expected %d\n",
1307 rr->length, usb_int_regs_length(count));
1310 if (rr->length > sizeof(rr->buffer)) {
1311 dev_dbg_f(zd_usb_dev(usb),
1312 "error: actual length %d exceeds buffer size %zu\n",
1313 rr->length, sizeof(rr->buffer));
1317 for (i = 0; i < count; i++) {
1318 struct reg_data *rd = ®s->regs[i];
1319 if (rd->addr != req->addr[i]) {
1320 dev_dbg_f(zd_usb_dev(usb),
1321 "rd[%d] addr %#06hx expected %#06hx\n", i,
1322 le16_to_cpu(rd->addr),
1323 le16_to_cpu(req->addr[i]));
1326 values[i] = le16_to_cpu(rd->value);
1331 spin_unlock_irq(&intr->lock);
1335 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1336 const zd_addr_t *addresses, unsigned int count)
1339 int i, req_len, actual_req_len;
1340 struct usb_device *udev;
1341 struct usb_req_read_regs *req = NULL;
1342 unsigned long timeout;
1345 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1348 if (count > USB_MAX_IOREAD16_COUNT) {
1349 dev_dbg_f(zd_usb_dev(usb),
1350 "error: count %u exceeds possible max %u\n",
1351 count, USB_MAX_IOREAD16_COUNT);
1355 dev_dbg_f(zd_usb_dev(usb),
1356 "error: io in atomic context not supported\n");
1357 return -EWOULDBLOCK;
1359 if (!usb_int_enabled(usb)) {
1360 dev_dbg_f(zd_usb_dev(usb),
1361 "error: usb interrupt not enabled\n");
1362 return -EWOULDBLOCK;
1365 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1366 req = kmalloc(req_len, GFP_KERNEL);
1369 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1370 for (i = 0; i < count; i++)
1371 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1373 udev = zd_usb_to_usbdev(usb);
1374 prepare_read_regs_int(usb);
1375 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1376 req, req_len, &actual_req_len, 1000 /* ms */);
1378 dev_dbg_f(zd_usb_dev(usb),
1379 "error in usb_bulk_msg(). Error number %d\n", r);
1382 if (req_len != actual_req_len) {
1383 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1384 " req_len %d != actual_req_len %d\n",
1385 req_len, actual_req_len);
1390 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1391 msecs_to_jiffies(1000));
1393 disable_read_regs_int(usb);
1394 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1399 r = get_results(usb, values, req, count);
1405 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1409 struct usb_device *udev;
1410 struct usb_req_write_regs *req = NULL;
1411 int i, req_len, actual_req_len;
1415 if (count > USB_MAX_IOWRITE16_COUNT) {
1416 dev_dbg_f(zd_usb_dev(usb),
1417 "error: count %u exceeds possible max %u\n",
1418 count, USB_MAX_IOWRITE16_COUNT);
1422 dev_dbg_f(zd_usb_dev(usb),
1423 "error: io in atomic context not supported\n");
1424 return -EWOULDBLOCK;
1427 req_len = sizeof(struct usb_req_write_regs) +
1428 count * sizeof(struct reg_data);
1429 req = kmalloc(req_len, GFP_KERNEL);
1433 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1434 for (i = 0; i < count; i++) {
1435 struct reg_data *rw = &req->reg_writes[i];
1436 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1437 rw->value = cpu_to_le16(ioreqs[i].value);
1440 udev = zd_usb_to_usbdev(usb);
1441 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1442 req, req_len, &actual_req_len, 1000 /* ms */);
1444 dev_dbg_f(zd_usb_dev(usb),
1445 "error in usb_bulk_msg(). Error number %d\n", r);
1448 if (req_len != actual_req_len) {
1449 dev_dbg_f(zd_usb_dev(usb),
1450 "error in usb_bulk_msg()"
1451 " req_len %d != actual_req_len %d\n",
1452 req_len, actual_req_len);
1457 /* FALL-THROUGH with r == 0 */
1463 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1466 struct usb_device *udev;
1467 struct usb_req_rfwrite *req = NULL;
1468 int i, req_len, actual_req_len;
1469 u16 bit_value_template;
1472 dev_dbg_f(zd_usb_dev(usb),
1473 "error: io in atomic context not supported\n");
1474 return -EWOULDBLOCK;
1476 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1477 dev_dbg_f(zd_usb_dev(usb),
1478 "error: bits %d are smaller than"
1479 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1480 bits, USB_MIN_RFWRITE_BIT_COUNT);
1483 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1484 dev_dbg_f(zd_usb_dev(usb),
1485 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1486 bits, USB_MAX_RFWRITE_BIT_COUNT);
1490 if (value & (~0UL << bits)) {
1491 dev_dbg_f(zd_usb_dev(usb),
1492 "error: value %#09x has bits >= %d set\n",
1498 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1500 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1502 dev_dbg_f(zd_usb_dev(usb),
1503 "error %d: Couldn't read CR203\n", r);
1506 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1508 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1509 req = kmalloc(req_len, GFP_KERNEL);
1513 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1514 /* 1: 3683a, but not used in ZYDAS driver */
1515 req->value = cpu_to_le16(2);
1516 req->bits = cpu_to_le16(bits);
1518 for (i = 0; i < bits; i++) {
1519 u16 bv = bit_value_template;
1520 if (value & (1 << (bits-1-i)))
1522 req->bit_values[i] = cpu_to_le16(bv);
1525 udev = zd_usb_to_usbdev(usb);
1526 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1527 req, req_len, &actual_req_len, 1000 /* ms */);
1529 dev_dbg_f(zd_usb_dev(usb),
1530 "error in usb_bulk_msg(). Error number %d\n", r);
1533 if (req_len != actual_req_len) {
1534 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1535 " req_len %d != actual_req_len %d\n",
1536 req_len, actual_req_len);
1541 /* FALL-THROUGH with r == 0 */