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, 0x4505), .driver_info
= DEVICE_ZD1211B
},
68 { USB_DEVICE(0x0471, 0x1236), .driver_info
= DEVICE_ZD1211B
},
69 { USB_DEVICE(0x13b1, 0x0024), .driver_info
= DEVICE_ZD1211B
},
70 { USB_DEVICE(0x0586, 0x340f), .driver_info
= DEVICE_ZD1211B
},
71 { USB_DEVICE(0x0b05, 0x171b), .driver_info
= DEVICE_ZD1211B
},
72 { USB_DEVICE(0x0586, 0x3410), .driver_info
= DEVICE_ZD1211B
},
73 { USB_DEVICE(0x0baf, 0x0121), .driver_info
= DEVICE_ZD1211B
},
74 { USB_DEVICE(0x0586, 0x3412), .driver_info
= DEVICE_ZD1211B
},
75 { USB_DEVICE(0x0586, 0x3413), .driver_info
= DEVICE_ZD1211B
},
76 { USB_DEVICE(0x0053, 0x5301), .driver_info
= DEVICE_ZD1211B
},
77 { USB_DEVICE(0x0411, 0x00da), .driver_info
= DEVICE_ZD1211B
},
78 { USB_DEVICE(0x2019, 0x5303), .driver_info
= DEVICE_ZD1211B
},
79 { USB_DEVICE(0x129b, 0x1667), .driver_info
= DEVICE_ZD1211B
},
80 { USB_DEVICE(0x0cde, 0x001a), .driver_info
= DEVICE_ZD1211B
},
81 { USB_DEVICE(0x0586, 0x340a), .driver_info
= DEVICE_ZD1211B
},
82 { USB_DEVICE(0x0471, 0x1237), .driver_info
= DEVICE_ZD1211B
},
83 /* "Driverless" devices that need ejecting */
84 { USB_DEVICE(0x0ace, 0x2011), .driver_info
= DEVICE_INSTALLER
},
85 { USB_DEVICE(0x0ace, 0x20ff), .driver_info
= DEVICE_INSTALLER
},
89 MODULE_LICENSE("GPL");
90 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
91 MODULE_AUTHOR("Ulrich Kunitz");
92 MODULE_AUTHOR("Daniel Drake");
93 MODULE_VERSION("1.0");
94 MODULE_DEVICE_TABLE(usb
, usb_ids
);
96 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
97 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
99 /* USB device initialization */
100 static void int_urb_complete(struct urb
*urb
);
102 static int request_fw_file(
103 const struct firmware
**fw
, const char *name
, struct device
*device
)
107 dev_dbg_f(device
, "fw name %s\n", name
);
109 r
= request_firmware(fw
, name
, device
);
112 "Could not load firmware file %s. Error number %d\n",
117 static inline u16
get_bcdDevice(const struct usb_device
*udev
)
119 return le16_to_cpu(udev
->descriptor
.bcdDevice
);
122 enum upload_code_flags
{
126 /* Ensures that MAX_TRANSFER_SIZE is even. */
127 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
129 static int upload_code(struct usb_device
*udev
,
130 const u8
*data
, size_t size
, u16 code_offset
, int flags
)
135 /* USB request blocks need "kmalloced" buffers.
137 p
= kmalloc(MAX_TRANSFER_SIZE
, GFP_KERNEL
);
139 dev_err(&udev
->dev
, "out of memory\n");
146 size_t transfer_size
= size
<= MAX_TRANSFER_SIZE
?
147 size
: MAX_TRANSFER_SIZE
;
149 dev_dbg_f(&udev
->dev
, "transfer size %zu\n", transfer_size
);
151 memcpy(p
, data
, transfer_size
);
152 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
153 USB_REQ_FIRMWARE_DOWNLOAD
,
154 USB_DIR_OUT
| USB_TYPE_VENDOR
,
155 code_offset
, 0, p
, transfer_size
, 1000 /* ms */);
158 "USB control request for firmware upload"
159 " failed. Error number %d\n", r
);
162 transfer_size
= r
& ~1;
164 size
-= transfer_size
;
165 data
+= transfer_size
;
166 code_offset
+= transfer_size
/sizeof(u16
);
169 if (flags
& REBOOT
) {
172 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
173 USB_REQ_FIRMWARE_CONFIRM
,
174 USB_DIR_IN
| USB_TYPE_VENDOR
,
175 0, 0, &ret
, sizeof(ret
), 5000 /* ms */);
176 if (r
!= sizeof(ret
)) {
178 "control request firmeware confirmation failed."
179 " Return value %d\n", r
);
186 "Internal error while downloading."
187 " Firmware confirm return value %#04x\n",
192 dev_dbg_f(&udev
->dev
, "firmware confirm return value %#04x\n",
202 static u16
get_word(const void *data
, u16 offset
)
204 const __le16
*p
= data
;
205 return le16_to_cpu(p
[offset
]);
208 static char *get_fw_name(struct zd_usb
*usb
, char *buffer
, size_t size
,
211 scnprintf(buffer
, size
, "%s%s",
213 FW_ZD1211B_PREFIX
: FW_ZD1211_PREFIX
,
218 static int handle_version_mismatch(struct zd_usb
*usb
,
219 const struct firmware
*ub_fw
)
221 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
222 const struct firmware
*ur_fw
= NULL
;
227 r
= request_fw_file(&ur_fw
,
228 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ur"),
233 r
= upload_code(udev
, ur_fw
->data
, ur_fw
->size
, FW_START
, REBOOT
);
237 offset
= (E2P_BOOT_CODE_OFFSET
* sizeof(u16
));
238 r
= upload_code(udev
, ub_fw
->data
+ offset
, ub_fw
->size
- offset
,
239 E2P_START
+ E2P_BOOT_CODE_OFFSET
, REBOOT
);
241 /* At this point, the vendor driver downloads the whole firmware
242 * image, hacks around with version IDs, and uploads it again,
243 * completely overwriting the boot code. We do not do this here as
244 * it is not required on any tested devices, and it is suspected to
247 release_firmware(ur_fw
);
251 static int upload_firmware(struct zd_usb
*usb
)
256 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
257 const struct firmware
*ub_fw
= NULL
;
258 const struct firmware
*uph_fw
= NULL
;
261 bcdDevice
= get_bcdDevice(udev
);
263 r
= request_fw_file(&ub_fw
,
264 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ub"),
269 fw_bcdDevice
= get_word(ub_fw
->data
, E2P_DATA_OFFSET
);
271 if (fw_bcdDevice
!= bcdDevice
) {
273 "firmware version %#06x and device bootcode version "
274 "%#06x differ\n", fw_bcdDevice
, bcdDevice
);
275 if (bcdDevice
<= 0x4313)
276 dev_warn(&udev
->dev
, "device has old bootcode, please "
277 "report success or failure\n");
279 r
= handle_version_mismatch(usb
, ub_fw
);
283 dev_dbg_f(&udev
->dev
,
284 "firmware device id %#06x is equal to the "
285 "actual device id\n", fw_bcdDevice
);
289 r
= request_fw_file(&uph_fw
,
290 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "uphr"),
295 r
= upload_code(udev
, uph_fw
->data
, uph_fw
->size
, FW_START
, REBOOT
);
298 "Could not upload firmware code uph. Error number %d\n",
304 release_firmware(ub_fw
);
305 release_firmware(uph_fw
);
309 /* Read data from device address space using "firmware interface" which does
310 * not require firmware to be loaded. */
311 int zd_usb_read_fw(struct zd_usb
*usb
, zd_addr_t addr
, u8
*data
, u16 len
)
314 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
316 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
317 USB_REQ_FIRMWARE_READ_DATA
, USB_DIR_IN
| 0x40, addr
, 0,
321 "read over firmware interface failed: %d\n", r
);
323 } else if (r
!= len
) {
325 "incomplete read over firmware interface: %d/%d\n",
333 #define urb_dev(urb) (&(urb)->dev->dev)
335 static inline void handle_regs_int(struct urb
*urb
)
337 struct zd_usb
*usb
= urb
->context
;
338 struct zd_usb_interrupt
*intr
= &usb
->intr
;
342 ZD_ASSERT(in_interrupt());
343 spin_lock(&intr
->lock
);
345 int_num
= le16_to_cpu(*(u16
*)(urb
->transfer_buffer
+2));
346 if (int_num
== CR_INTERRUPT
) {
347 struct zd_mac
*mac
= zd_hw_mac(zd_usb_to_hw(urb
->context
));
348 memcpy(&mac
->intr_buffer
, urb
->transfer_buffer
,
349 USB_MAX_EP_INT_BUFFER
);
350 schedule_work(&mac
->process_intr
);
351 } else if (intr
->read_regs_enabled
) {
352 intr
->read_regs
.length
= len
= urb
->actual_length
;
354 if (len
> sizeof(intr
->read_regs
.buffer
))
355 len
= sizeof(intr
->read_regs
.buffer
);
356 memcpy(intr
->read_regs
.buffer
, urb
->transfer_buffer
, len
);
357 intr
->read_regs_enabled
= 0;
358 complete(&intr
->read_regs
.completion
);
363 spin_unlock(&intr
->lock
);
366 static void int_urb_complete(struct urb
*urb
)
369 struct usb_int_header
*hdr
;
371 switch (urb
->status
) {
385 if (urb
->actual_length
< sizeof(hdr
)) {
386 dev_dbg_f(urb_dev(urb
), "error: urb %p to small\n", urb
);
390 hdr
= urb
->transfer_buffer
;
391 if (hdr
->type
!= USB_INT_TYPE
) {
392 dev_dbg_f(urb_dev(urb
), "error: urb %p wrong type\n", urb
);
397 case USB_INT_ID_REGS
:
398 handle_regs_int(urb
);
400 case USB_INT_ID_RETRY_FAILED
:
401 zd_mac_tx_failed(zd_usb_to_hw(urb
->context
));
404 dev_dbg_f(urb_dev(urb
), "error: urb %p unknown id %x\n", urb
,
405 (unsigned int)hdr
->id
);
410 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
412 dev_dbg_f(urb_dev(urb
), "resubmit urb %p\n", urb
);
417 kfree(urb
->transfer_buffer
);
420 static inline int int_urb_interval(struct usb_device
*udev
)
422 switch (udev
->speed
) {
433 static inline int usb_int_enabled(struct zd_usb
*usb
)
436 struct zd_usb_interrupt
*intr
= &usb
->intr
;
439 spin_lock_irqsave(&intr
->lock
, flags
);
441 spin_unlock_irqrestore(&intr
->lock
, flags
);
445 int zd_usb_enable_int(struct zd_usb
*usb
)
448 struct usb_device
*udev
;
449 struct zd_usb_interrupt
*intr
= &usb
->intr
;
450 void *transfer_buffer
= NULL
;
453 dev_dbg_f(zd_usb_dev(usb
), "\n");
455 urb
= usb_alloc_urb(0, GFP_KERNEL
);
461 ZD_ASSERT(!irqs_disabled());
462 spin_lock_irq(&intr
->lock
);
464 spin_unlock_irq(&intr
->lock
);
469 spin_unlock_irq(&intr
->lock
);
471 /* TODO: make it a DMA buffer */
473 transfer_buffer
= kmalloc(USB_MAX_EP_INT_BUFFER
, GFP_KERNEL
);
474 if (!transfer_buffer
) {
475 dev_dbg_f(zd_usb_dev(usb
),
476 "couldn't allocate transfer_buffer\n");
477 goto error_set_urb_null
;
480 udev
= zd_usb_to_usbdev(usb
);
481 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, EP_INT_IN
),
482 transfer_buffer
, USB_MAX_EP_INT_BUFFER
,
483 int_urb_complete
, usb
,
486 dev_dbg_f(zd_usb_dev(usb
), "submit urb %p\n", intr
->urb
);
487 r
= usb_submit_urb(urb
, GFP_KERNEL
);
489 dev_dbg_f(zd_usb_dev(usb
),
490 "Couldn't submit urb. Error number %d\n", r
);
496 kfree(transfer_buffer
);
498 spin_lock_irq(&intr
->lock
);
500 spin_unlock_irq(&intr
->lock
);
507 void zd_usb_disable_int(struct zd_usb
*usb
)
510 struct zd_usb_interrupt
*intr
= &usb
->intr
;
513 spin_lock_irqsave(&intr
->lock
, flags
);
516 spin_unlock_irqrestore(&intr
->lock
, flags
);
520 spin_unlock_irqrestore(&intr
->lock
, flags
);
523 dev_dbg_f(zd_usb_dev(usb
), "urb %p killed\n", urb
);
527 static void handle_rx_packet(struct zd_usb
*usb
, const u8
*buffer
,
531 const struct rx_length_info
*length_info
;
533 if (length
< sizeof(struct rx_length_info
)) {
534 /* It's not a complete packet anyhow. */
537 length_info
= (struct rx_length_info
*)
538 (buffer
+ length
- sizeof(struct rx_length_info
));
540 /* It might be that three frames are merged into a single URB
541 * transaction. We have to check for the length info tag.
543 * While testing we discovered that length_info might be unaligned,
544 * because if USB transactions are merged, the last packet will not
545 * be padded. Unaligned access might also happen if the length_info
546 * structure is not present.
548 if (get_unaligned(&length_info
->tag
) == cpu_to_le16(RX_LENGTH_INFO_TAG
))
550 unsigned int l
, k
, n
;
551 for (i
= 0, l
= 0;; i
++) {
552 k
= le16_to_cpu(get_unaligned(&length_info
->length
[i
]));
558 zd_mac_rx(zd_usb_to_hw(usb
), buffer
+l
, k
);
564 zd_mac_rx(zd_usb_to_hw(usb
), buffer
, length
);
568 static void rx_urb_complete(struct urb
*urb
)
571 struct zd_usb_rx
*rx
;
575 switch (urb
->status
) {
586 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
590 buffer
= urb
->transfer_buffer
;
591 length
= urb
->actual_length
;
595 if (length
%rx
->usb_packet_size
> rx
->usb_packet_size
-4) {
596 /* If there is an old first fragment, we don't care. */
597 dev_dbg_f(urb_dev(urb
), "*** first fragment ***\n");
598 ZD_ASSERT(length
<= ARRAY_SIZE(rx
->fragment
));
599 spin_lock(&rx
->lock
);
600 memcpy(rx
->fragment
, buffer
, length
);
601 rx
->fragment_length
= length
;
602 spin_unlock(&rx
->lock
);
606 spin_lock(&rx
->lock
);
607 if (rx
->fragment_length
> 0) {
608 /* We are on a second fragment, we believe */
609 ZD_ASSERT(length
+ rx
->fragment_length
<=
610 ARRAY_SIZE(rx
->fragment
));
611 dev_dbg_f(urb_dev(urb
), "*** second fragment ***\n");
612 memcpy(rx
->fragment
+rx
->fragment_length
, buffer
, length
);
613 handle_rx_packet(usb
, rx
->fragment
,
614 rx
->fragment_length
+ length
);
615 rx
->fragment_length
= 0;
616 spin_unlock(&rx
->lock
);
618 spin_unlock(&rx
->lock
);
619 handle_rx_packet(usb
, buffer
, length
);
623 usb_submit_urb(urb
, GFP_ATOMIC
);
626 static struct urb
*alloc_rx_urb(struct zd_usb
*usb
)
628 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
632 urb
= usb_alloc_urb(0, GFP_KERNEL
);
635 buffer
= usb_buffer_alloc(udev
, USB_MAX_RX_SIZE
, GFP_KERNEL
,
642 usb_fill_bulk_urb(urb
, udev
, usb_rcvbulkpipe(udev
, EP_DATA_IN
),
643 buffer
, USB_MAX_RX_SIZE
,
644 rx_urb_complete
, usb
);
645 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
650 static void free_rx_urb(struct urb
*urb
)
654 usb_buffer_free(urb
->dev
, urb
->transfer_buffer_length
,
655 urb
->transfer_buffer
, urb
->transfer_dma
);
659 int zd_usb_enable_rx(struct zd_usb
*usb
)
662 struct zd_usb_rx
*rx
= &usb
->rx
;
665 dev_dbg_f(zd_usb_dev(usb
), "\n");
668 urbs
= kcalloc(RX_URBS_COUNT
, sizeof(struct urb
*), GFP_KERNEL
);
671 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
672 urbs
[i
] = alloc_rx_urb(usb
);
677 ZD_ASSERT(!irqs_disabled());
678 spin_lock_irq(&rx
->lock
);
680 spin_unlock_irq(&rx
->lock
);
685 rx
->urbs_count
= RX_URBS_COUNT
;
686 spin_unlock_irq(&rx
->lock
);
688 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
689 r
= usb_submit_urb(urbs
[i
], GFP_KERNEL
);
696 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
697 usb_kill_urb(urbs
[i
]);
699 spin_lock_irq(&rx
->lock
);
702 spin_unlock_irq(&rx
->lock
);
705 for (i
= 0; i
< RX_URBS_COUNT
; i
++)
706 free_rx_urb(urbs
[i
]);
711 void zd_usb_disable_rx(struct zd_usb
*usb
)
717 struct zd_usb_rx
*rx
= &usb
->rx
;
719 spin_lock_irqsave(&rx
->lock
, flags
);
721 count
= rx
->urbs_count
;
722 spin_unlock_irqrestore(&rx
->lock
, flags
);
726 for (i
= 0; i
< count
; i
++) {
727 usb_kill_urb(urbs
[i
]);
728 free_rx_urb(urbs
[i
]);
732 spin_lock_irqsave(&rx
->lock
, flags
);
735 spin_unlock_irqrestore(&rx
->lock
, flags
);
739 * zd_usb_disable_tx - disable transmission
740 * @usb: the zd1211rw-private USB structure
742 * Frees all URBs in the free list and marks the transmission as disabled.
744 void zd_usb_disable_tx(struct zd_usb
*usb
)
746 struct zd_usb_tx
*tx
= &usb
->tx
;
748 struct list_head
*pos
, *n
;
750 spin_lock_irqsave(&tx
->lock
, flags
);
751 list_for_each_safe(pos
, n
, &tx
->free_urb_list
) {
753 usb_free_urb(list_entry(pos
, struct urb
, urb_list
));
756 tx
->submitted_urbs
= 0;
757 /* The stopped state is ignored, relying on ieee80211_wake_queues()
758 * in a potentionally following zd_usb_enable_tx().
760 spin_unlock_irqrestore(&tx
->lock
, flags
);
764 * zd_usb_enable_tx - enables transmission
765 * @usb: a &struct zd_usb pointer
767 * This function enables transmission and prepares the &zd_usb_tx data
770 void zd_usb_enable_tx(struct zd_usb
*usb
)
773 struct zd_usb_tx
*tx
= &usb
->tx
;
775 spin_lock_irqsave(&tx
->lock
, flags
);
777 tx
->submitted_urbs
= 0;
778 ieee80211_wake_queues(zd_usb_to_hw(usb
));
780 spin_unlock_irqrestore(&tx
->lock
, flags
);
784 * alloc_tx_urb - provides an tx URB
785 * @usb: a &struct zd_usb pointer
787 * Allocates a new URB. If possible takes the urb from the free list in
790 static struct urb
*alloc_tx_urb(struct zd_usb
*usb
)
792 struct zd_usb_tx
*tx
= &usb
->tx
;
794 struct list_head
*entry
;
797 spin_lock_irqsave(&tx
->lock
, flags
);
798 if (list_empty(&tx
->free_urb_list
)) {
799 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
802 entry
= tx
->free_urb_list
.next
;
804 urb
= list_entry(entry
, struct urb
, urb_list
);
806 spin_unlock_irqrestore(&tx
->lock
, flags
);
811 * free_tx_urb - frees a used tx URB
812 * @usb: a &struct zd_usb pointer
813 * @urb: URB to be freed
815 * Frees the the transmission URB, which means to put it on the free URB
818 static void free_tx_urb(struct zd_usb
*usb
, struct urb
*urb
)
820 struct zd_usb_tx
*tx
= &usb
->tx
;
823 spin_lock_irqsave(&tx
->lock
, flags
);
828 list_add(&urb
->urb_list
, &tx
->free_urb_list
);
830 spin_unlock_irqrestore(&tx
->lock
, flags
);
833 static void tx_dec_submitted_urbs(struct zd_usb
*usb
)
835 struct zd_usb_tx
*tx
= &usb
->tx
;
838 spin_lock_irqsave(&tx
->lock
, flags
);
839 --tx
->submitted_urbs
;
840 if (tx
->stopped
&& tx
->submitted_urbs
<= ZD_USB_TX_LOW
) {
841 ieee80211_wake_queues(zd_usb_to_hw(usb
));
844 spin_unlock_irqrestore(&tx
->lock
, flags
);
847 static void tx_inc_submitted_urbs(struct zd_usb
*usb
)
849 struct zd_usb_tx
*tx
= &usb
->tx
;
852 spin_lock_irqsave(&tx
->lock
, flags
);
853 ++tx
->submitted_urbs
;
854 if (!tx
->stopped
&& tx
->submitted_urbs
> ZD_USB_TX_HIGH
) {
855 ieee80211_stop_queues(zd_usb_to_hw(usb
));
858 spin_unlock_irqrestore(&tx
->lock
, flags
);
862 * tx_urb_complete - completes the execution of an URB
865 * This function is called if the URB has been transferred to a device or an
866 * error has happened.
868 static void tx_urb_complete(struct urb
*urb
)
872 struct zd_tx_skb_control_block
*cb
;
875 switch (urb
->status
) {
884 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
887 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
891 skb
= (struct sk_buff
*)urb
->context
;
892 zd_mac_tx_to_dev(skb
, urb
->status
);
893 cb
= (struct zd_tx_skb_control_block
*)skb
->cb
;
894 usb
= &zd_hw_mac(cb
->hw
)->chip
.usb
;
895 free_tx_urb(usb
, urb
);
896 tx_dec_submitted_urbs(usb
);
899 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
901 dev_dbg_f(urb_dev(urb
), "error resubmit urb %p %d\n", urb
, r
);
907 * zd_usb_tx: initiates transfer of a frame of the device
909 * @usb: the zd1211rw-private USB structure
910 * @skb: a &struct sk_buff pointer
912 * This function tranmits a frame to the device. It doesn't wait for
913 * completion. The frame must contain the control set and have all the
914 * control set information available.
916 * The function returns 0 if the transfer has been successfully initiated.
918 int zd_usb_tx(struct zd_usb
*usb
, struct sk_buff
*skb
)
921 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
924 urb
= alloc_tx_urb(usb
);
930 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_DATA_OUT
),
931 skb
->data
, skb
->len
, tx_urb_complete
, skb
);
933 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
936 tx_inc_submitted_urbs(usb
);
939 free_tx_urb(usb
, urb
);
944 static inline void init_usb_interrupt(struct zd_usb
*usb
)
946 struct zd_usb_interrupt
*intr
= &usb
->intr
;
948 spin_lock_init(&intr
->lock
);
949 intr
->interval
= int_urb_interval(zd_usb_to_usbdev(usb
));
950 init_completion(&intr
->read_regs
.completion
);
951 intr
->read_regs
.cr_int_addr
= cpu_to_le16((u16
)CR_INTERRUPT
);
954 static inline void init_usb_rx(struct zd_usb
*usb
)
956 struct zd_usb_rx
*rx
= &usb
->rx
;
957 spin_lock_init(&rx
->lock
);
958 if (interface_to_usbdev(usb
->intf
)->speed
== USB_SPEED_HIGH
) {
959 rx
->usb_packet_size
= 512;
961 rx
->usb_packet_size
= 64;
963 ZD_ASSERT(rx
->fragment_length
== 0);
966 static inline void init_usb_tx(struct zd_usb
*usb
)
968 struct zd_usb_tx
*tx
= &usb
->tx
;
969 spin_lock_init(&tx
->lock
);
972 INIT_LIST_HEAD(&tx
->free_urb_list
);
973 tx
->submitted_urbs
= 0;
976 void zd_usb_init(struct zd_usb
*usb
, struct ieee80211_hw
*hw
,
977 struct usb_interface
*intf
)
979 memset(usb
, 0, sizeof(*usb
));
980 usb
->intf
= usb_get_intf(intf
);
981 usb_set_intfdata(usb
->intf
, hw
);
982 init_usb_interrupt(usb
);
987 void zd_usb_clear(struct zd_usb
*usb
)
989 usb_set_intfdata(usb
->intf
, NULL
);
990 usb_put_intf(usb
->intf
);
991 ZD_MEMCLEAR(usb
, sizeof(*usb
));
992 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
995 static const char *speed(enum usb_device_speed speed
)
1000 case USB_SPEED_FULL
:
1002 case USB_SPEED_HIGH
:
1005 return "unknown speed";
1009 static int scnprint_id(struct usb_device
*udev
, char *buffer
, size_t size
)
1011 return scnprintf(buffer
, size
, "%04hx:%04hx v%04hx %s",
1012 le16_to_cpu(udev
->descriptor
.idVendor
),
1013 le16_to_cpu(udev
->descriptor
.idProduct
),
1014 get_bcdDevice(udev
),
1015 speed(udev
->speed
));
1018 int zd_usb_scnprint_id(struct zd_usb
*usb
, char *buffer
, size_t size
)
1020 struct usb_device
*udev
= interface_to_usbdev(usb
->intf
);
1021 return scnprint_id(udev
, buffer
, size
);
1025 static void print_id(struct usb_device
*udev
)
1029 scnprint_id(udev
, buffer
, sizeof(buffer
));
1030 buffer
[sizeof(buffer
)-1] = 0;
1031 dev_dbg_f(&udev
->dev
, "%s\n", buffer
);
1034 #define print_id(udev) do { } while (0)
1037 static int eject_installer(struct usb_interface
*intf
)
1039 struct usb_device
*udev
= interface_to_usbdev(intf
);
1040 struct usb_host_interface
*iface_desc
= &intf
->altsetting
[0];
1041 struct usb_endpoint_descriptor
*endpoint
;
1046 /* Find bulk out endpoint */
1047 endpoint
= &iface_desc
->endpoint
[1].desc
;
1048 if ((endpoint
->bEndpointAddress
& USB_TYPE_MASK
) == USB_DIR_OUT
&&
1049 (endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1050 USB_ENDPOINT_XFER_BULK
) {
1051 bulk_out_ep
= endpoint
->bEndpointAddress
;
1054 "zd1211rw: Could not find bulk out endpoint\n");
1058 cmd
= kzalloc(31, GFP_KERNEL
);
1062 /* USB bulk command block */
1063 cmd
[0] = 0x55; /* bulk command signature */
1064 cmd
[1] = 0x53; /* bulk command signature */
1065 cmd
[2] = 0x42; /* bulk command signature */
1066 cmd
[3] = 0x43; /* bulk command signature */
1067 cmd
[14] = 6; /* command length */
1069 cmd
[15] = 0x1b; /* SCSI command: START STOP UNIT */
1070 cmd
[19] = 0x2; /* eject disc */
1072 dev_info(&udev
->dev
, "Ejecting virtual installer media...\n");
1073 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, bulk_out_ep
),
1074 cmd
, 31, NULL
, 2000);
1079 /* At this point, the device disconnects and reconnects with the real
1082 usb_set_intfdata(intf
, NULL
);
1086 int zd_usb_init_hw(struct zd_usb
*usb
)
1089 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1091 dev_dbg_f(zd_usb_dev(usb
), "\n");
1093 r
= upload_firmware(usb
);
1095 dev_err(zd_usb_dev(usb
),
1096 "couldn't load firmware. Error number %d\n", r
);
1100 r
= usb_reset_configuration(zd_usb_to_usbdev(usb
));
1102 dev_dbg_f(zd_usb_dev(usb
),
1103 "couldn't reset configuration. Error number %d\n", r
);
1107 r
= zd_mac_init_hw(mac
->hw
);
1109 dev_dbg_f(zd_usb_dev(usb
),
1110 "couldn't initialize mac. Error number %d\n", r
);
1114 usb
->initialized
= 1;
1118 static int probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1121 struct usb_device
*udev
= interface_to_usbdev(intf
);
1123 struct ieee80211_hw
*hw
= NULL
;
1127 if (id
->driver_info
& DEVICE_INSTALLER
)
1128 return eject_installer(intf
);
1130 switch (udev
->speed
) {
1132 case USB_SPEED_FULL
:
1133 case USB_SPEED_HIGH
:
1136 dev_dbg_f(&intf
->dev
, "Unknown USB speed\n");
1141 r
= usb_reset_device(udev
);
1144 "couldn't reset usb device. Error number %d\n", r
);
1148 hw
= zd_mac_alloc_hw(intf
);
1154 usb
= &zd_hw_mac(hw
)->chip
.usb
;
1155 usb
->is_zd1211b
= (id
->driver_info
== DEVICE_ZD1211B
) != 0;
1157 r
= zd_mac_preinit_hw(hw
);
1159 dev_dbg_f(&intf
->dev
,
1160 "couldn't initialize mac. Error number %d\n", r
);
1164 r
= ieee80211_register_hw(hw
);
1166 dev_dbg_f(&intf
->dev
,
1167 "couldn't register device. Error number %d\n", r
);
1171 dev_dbg_f(&intf
->dev
, "successful\n");
1172 dev_info(&intf
->dev
, "%s\n", wiphy_name(hw
->wiphy
));
1175 usb_reset_device(interface_to_usbdev(intf
));
1177 zd_mac_clear(zd_hw_mac(hw
));
1178 ieee80211_free_hw(hw
);
1183 static void disconnect(struct usb_interface
*intf
)
1185 struct ieee80211_hw
*hw
= zd_intf_to_hw(intf
);
1189 /* Either something really bad happened, or we're just dealing with
1190 * a DEVICE_INSTALLER. */
1194 mac
= zd_hw_mac(hw
);
1195 usb
= &mac
->chip
.usb
;
1197 dev_dbg_f(zd_usb_dev(usb
), "\n");
1199 ieee80211_unregister_hw(hw
);
1201 /* Just in case something has gone wrong! */
1202 zd_usb_disable_rx(usb
);
1203 zd_usb_disable_int(usb
);
1205 /* If the disconnect has been caused by a removal of the
1206 * driver module, the reset allows reloading of the driver. If the
1207 * reset will not be executed here, the upload of the firmware in the
1208 * probe function caused by the reloading of the driver will fail.
1210 usb_reset_device(interface_to_usbdev(intf
));
1213 ieee80211_free_hw(hw
);
1214 dev_dbg(&intf
->dev
, "disconnected\n");
1217 static struct usb_driver driver
= {
1218 .name
= KBUILD_MODNAME
,
1219 .id_table
= usb_ids
,
1221 .disconnect
= disconnect
,
1224 struct workqueue_struct
*zd_workqueue
;
1226 static int __init
usb_init(void)
1230 pr_debug("%s usb_init()\n", driver
.name
);
1232 zd_workqueue
= create_singlethread_workqueue(driver
.name
);
1233 if (zd_workqueue
== NULL
) {
1234 printk(KERN_ERR
"%s couldn't create workqueue\n", driver
.name
);
1238 r
= usb_register(&driver
);
1240 destroy_workqueue(zd_workqueue
);
1241 printk(KERN_ERR
"%s usb_register() failed. Error number %d\n",
1246 pr_debug("%s initialized\n", driver
.name
);
1250 static void __exit
usb_exit(void)
1252 pr_debug("%s usb_exit()\n", driver
.name
);
1253 usb_deregister(&driver
);
1254 destroy_workqueue(zd_workqueue
);
1257 module_init(usb_init
);
1258 module_exit(usb_exit
);
1260 static int usb_int_regs_length(unsigned int count
)
1262 return sizeof(struct usb_int_regs
) + count
* sizeof(struct reg_data
);
1265 static void prepare_read_regs_int(struct zd_usb
*usb
)
1267 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1269 spin_lock_irq(&intr
->lock
);
1270 intr
->read_regs_enabled
= 1;
1271 INIT_COMPLETION(intr
->read_regs
.completion
);
1272 spin_unlock_irq(&intr
->lock
);
1275 static void disable_read_regs_int(struct zd_usb
*usb
)
1277 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1279 spin_lock_irq(&intr
->lock
);
1280 intr
->read_regs_enabled
= 0;
1281 spin_unlock_irq(&intr
->lock
);
1284 static int get_results(struct zd_usb
*usb
, u16
*values
,
1285 struct usb_req_read_regs
*req
, unsigned int count
)
1289 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1290 struct read_regs_int
*rr
= &intr
->read_regs
;
1291 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1293 spin_lock_irq(&intr
->lock
);
1296 /* The created block size seems to be larger than expected.
1297 * However results appear to be correct.
1299 if (rr
->length
< usb_int_regs_length(count
)) {
1300 dev_dbg_f(zd_usb_dev(usb
),
1301 "error: actual length %d less than expected %d\n",
1302 rr
->length
, usb_int_regs_length(count
));
1305 if (rr
->length
> sizeof(rr
->buffer
)) {
1306 dev_dbg_f(zd_usb_dev(usb
),
1307 "error: actual length %d exceeds buffer size %zu\n",
1308 rr
->length
, sizeof(rr
->buffer
));
1312 for (i
= 0; i
< count
; i
++) {
1313 struct reg_data
*rd
= ®s
->regs
[i
];
1314 if (rd
->addr
!= req
->addr
[i
]) {
1315 dev_dbg_f(zd_usb_dev(usb
),
1316 "rd[%d] addr %#06hx expected %#06hx\n", i
,
1317 le16_to_cpu(rd
->addr
),
1318 le16_to_cpu(req
->addr
[i
]));
1321 values
[i
] = le16_to_cpu(rd
->value
);
1326 spin_unlock_irq(&intr
->lock
);
1330 int zd_usb_ioread16v(struct zd_usb
*usb
, u16
*values
,
1331 const zd_addr_t
*addresses
, unsigned int count
)
1334 int i
, req_len
, actual_req_len
;
1335 struct usb_device
*udev
;
1336 struct usb_req_read_regs
*req
= NULL
;
1337 unsigned long timeout
;
1340 dev_dbg_f(zd_usb_dev(usb
), "error: count is zero\n");
1343 if (count
> USB_MAX_IOREAD16_COUNT
) {
1344 dev_dbg_f(zd_usb_dev(usb
),
1345 "error: count %u exceeds possible max %u\n",
1346 count
, USB_MAX_IOREAD16_COUNT
);
1350 dev_dbg_f(zd_usb_dev(usb
),
1351 "error: io in atomic context not supported\n");
1352 return -EWOULDBLOCK
;
1354 if (!usb_int_enabled(usb
)) {
1355 dev_dbg_f(zd_usb_dev(usb
),
1356 "error: usb interrupt not enabled\n");
1357 return -EWOULDBLOCK
;
1360 req_len
= sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
);
1361 req
= kmalloc(req_len
, GFP_KERNEL
);
1364 req
->id
= cpu_to_le16(USB_REQ_READ_REGS
);
1365 for (i
= 0; i
< count
; i
++)
1366 req
->addr
[i
] = cpu_to_le16((u16
)addresses
[i
]);
1368 udev
= zd_usb_to_usbdev(usb
);
1369 prepare_read_regs_int(usb
);
1370 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, EP_REGS_OUT
),
1371 req
, req_len
, &actual_req_len
, 1000 /* ms */);
1373 dev_dbg_f(zd_usb_dev(usb
),
1374 "error in usb_bulk_msg(). Error number %d\n", r
);
1377 if (req_len
!= actual_req_len
) {
1378 dev_dbg_f(zd_usb_dev(usb
), "error in usb_bulk_msg()\n"
1379 " req_len %d != actual_req_len %d\n",
1380 req_len
, actual_req_len
);
1385 timeout
= wait_for_completion_timeout(&usb
->intr
.read_regs
.completion
,
1386 msecs_to_jiffies(1000));
1388 disable_read_regs_int(usb
);
1389 dev_dbg_f(zd_usb_dev(usb
), "read timed out\n");
1394 r
= get_results(usb
, values
, req
, count
);
1400 int zd_usb_iowrite16v(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1404 struct usb_device
*udev
;
1405 struct usb_req_write_regs
*req
= NULL
;
1406 int i
, req_len
, actual_req_len
;
1410 if (count
> USB_MAX_IOWRITE16_COUNT
) {
1411 dev_dbg_f(zd_usb_dev(usb
),
1412 "error: count %u exceeds possible max %u\n",
1413 count
, USB_MAX_IOWRITE16_COUNT
);
1417 dev_dbg_f(zd_usb_dev(usb
),
1418 "error: io in atomic context not supported\n");
1419 return -EWOULDBLOCK
;
1422 req_len
= sizeof(struct usb_req_write_regs
) +
1423 count
* sizeof(struct reg_data
);
1424 req
= kmalloc(req_len
, GFP_KERNEL
);
1428 req
->id
= cpu_to_le16(USB_REQ_WRITE_REGS
);
1429 for (i
= 0; i
< count
; i
++) {
1430 struct reg_data
*rw
= &req
->reg_writes
[i
];
1431 rw
->addr
= cpu_to_le16((u16
)ioreqs
[i
].addr
);
1432 rw
->value
= cpu_to_le16(ioreqs
[i
].value
);
1435 udev
= zd_usb_to_usbdev(usb
);
1436 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, EP_REGS_OUT
),
1437 req
, req_len
, &actual_req_len
, 1000 /* ms */);
1439 dev_dbg_f(zd_usb_dev(usb
),
1440 "error in usb_bulk_msg(). Error number %d\n", r
);
1443 if (req_len
!= actual_req_len
) {
1444 dev_dbg_f(zd_usb_dev(usb
),
1445 "error in usb_bulk_msg()"
1446 " req_len %d != actual_req_len %d\n",
1447 req_len
, actual_req_len
);
1452 /* FALL-THROUGH with r == 0 */
1458 int zd_usb_rfwrite(struct zd_usb
*usb
, u32 value
, u8 bits
)
1461 struct usb_device
*udev
;
1462 struct usb_req_rfwrite
*req
= NULL
;
1463 int i
, req_len
, actual_req_len
;
1464 u16 bit_value_template
;
1467 dev_dbg_f(zd_usb_dev(usb
),
1468 "error: io in atomic context not supported\n");
1469 return -EWOULDBLOCK
;
1471 if (bits
< USB_MIN_RFWRITE_BIT_COUNT
) {
1472 dev_dbg_f(zd_usb_dev(usb
),
1473 "error: bits %d are smaller than"
1474 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1475 bits
, USB_MIN_RFWRITE_BIT_COUNT
);
1478 if (bits
> USB_MAX_RFWRITE_BIT_COUNT
) {
1479 dev_dbg_f(zd_usb_dev(usb
),
1480 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1481 bits
, USB_MAX_RFWRITE_BIT_COUNT
);
1485 if (value
& (~0UL << bits
)) {
1486 dev_dbg_f(zd_usb_dev(usb
),
1487 "error: value %#09x has bits >= %d set\n",
1493 dev_dbg_f(zd_usb_dev(usb
), "value %#09x bits %d\n", value
, bits
);
1495 r
= zd_usb_ioread16(usb
, &bit_value_template
, CR203
);
1497 dev_dbg_f(zd_usb_dev(usb
),
1498 "error %d: Couldn't read CR203\n", r
);
1501 bit_value_template
&= ~(RF_IF_LE
|RF_CLK
|RF_DATA
);
1503 req_len
= sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
);
1504 req
= kmalloc(req_len
, GFP_KERNEL
);
1508 req
->id
= cpu_to_le16(USB_REQ_WRITE_RF
);
1509 /* 1: 3683a, but not used in ZYDAS driver */
1510 req
->value
= cpu_to_le16(2);
1511 req
->bits
= cpu_to_le16(bits
);
1513 for (i
= 0; i
< bits
; i
++) {
1514 u16 bv
= bit_value_template
;
1515 if (value
& (1 << (bits
-1-i
)))
1517 req
->bit_values
[i
] = cpu_to_le16(bv
);
1520 udev
= zd_usb_to_usbdev(usb
);
1521 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, EP_REGS_OUT
),
1522 req
, req_len
, &actual_req_len
, 1000 /* ms */);
1524 dev_dbg_f(zd_usb_dev(usb
),
1525 "error in usb_bulk_msg(). Error number %d\n", r
);
1528 if (req_len
!= actual_req_len
) {
1529 dev_dbg_f(zd_usb_dev(usb
), "error in usb_bulk_msg()"
1530 " req_len %d != actual_req_len %d\n",
1531 req_len
, actual_req_len
);
1536 /* FALL-THROUGH with r == 0 */