1 /* ZD1211 USB-WLAN driver for Linux
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/firmware.h>
25 #include <linux/device.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/skbuff.h>
29 #include <linux/usb.h>
30 #include <linux/workqueue.h>
31 #include <linux/module.h>
32 #include <net/mac80211.h>
33 #include <asm/unaligned.h>
39 static struct usb_device_id usb_ids
[] = {
41 { USB_DEVICE(0x0105, 0x145f), .driver_info
= DEVICE_ZD1211
},
42 { USB_DEVICE(0x0586, 0x3401), .driver_info
= DEVICE_ZD1211
},
43 { USB_DEVICE(0x0586, 0x3402), .driver_info
= DEVICE_ZD1211
},
44 { USB_DEVICE(0x0586, 0x3407), .driver_info
= DEVICE_ZD1211
},
45 { USB_DEVICE(0x0586, 0x3409), .driver_info
= DEVICE_ZD1211
},
46 { USB_DEVICE(0x079b, 0x004a), .driver_info
= DEVICE_ZD1211
},
47 { USB_DEVICE(0x07b8, 0x6001), .driver_info
= DEVICE_ZD1211
},
48 { USB_DEVICE(0x0ace, 0x1211), .driver_info
= DEVICE_ZD1211
},
49 { USB_DEVICE(0x0ace, 0xa211), .driver_info
= DEVICE_ZD1211
},
50 { USB_DEVICE(0x0b05, 0x170c), .driver_info
= DEVICE_ZD1211
},
51 { USB_DEVICE(0x0b3b, 0x1630), .driver_info
= DEVICE_ZD1211
},
52 { USB_DEVICE(0x0b3b, 0x5630), .driver_info
= DEVICE_ZD1211
},
53 { USB_DEVICE(0x0df6, 0x9071), .driver_info
= DEVICE_ZD1211
},
54 { USB_DEVICE(0x0df6, 0x9075), .driver_info
= DEVICE_ZD1211
},
55 { USB_DEVICE(0x126f, 0xa006), .driver_info
= DEVICE_ZD1211
},
56 { USB_DEVICE(0x129b, 0x1666), .driver_info
= DEVICE_ZD1211
},
57 { USB_DEVICE(0x13b1, 0x001e), .driver_info
= DEVICE_ZD1211
},
58 { USB_DEVICE(0x1435, 0x0711), .driver_info
= DEVICE_ZD1211
},
59 { USB_DEVICE(0x14ea, 0xab10), .driver_info
= DEVICE_ZD1211
},
60 { USB_DEVICE(0x14ea, 0xab13), .driver_info
= DEVICE_ZD1211
},
61 { USB_DEVICE(0x157e, 0x300a), .driver_info
= DEVICE_ZD1211
},
62 { USB_DEVICE(0x157e, 0x300b), .driver_info
= DEVICE_ZD1211
},
63 { USB_DEVICE(0x157e, 0x3204), .driver_info
= DEVICE_ZD1211
},
64 { USB_DEVICE(0x157e, 0x3207), .driver_info
= DEVICE_ZD1211
},
65 { USB_DEVICE(0x1740, 0x2000), .driver_info
= DEVICE_ZD1211
},
66 { USB_DEVICE(0x6891, 0xa727), .driver_info
= DEVICE_ZD1211
},
68 { USB_DEVICE(0x0053, 0x5301), .driver_info
= DEVICE_ZD1211B
},
69 { USB_DEVICE(0x0409, 0x0248), .driver_info
= DEVICE_ZD1211B
},
70 { USB_DEVICE(0x0411, 0x00da), .driver_info
= DEVICE_ZD1211B
},
71 { USB_DEVICE(0x0471, 0x1236), .driver_info
= DEVICE_ZD1211B
},
72 { USB_DEVICE(0x0471, 0x1237), .driver_info
= DEVICE_ZD1211B
},
73 { USB_DEVICE(0x050d, 0x705c), .driver_info
= DEVICE_ZD1211B
},
74 { USB_DEVICE(0x054c, 0x0257), .driver_info
= DEVICE_ZD1211B
},
75 { USB_DEVICE(0x0586, 0x340a), .driver_info
= DEVICE_ZD1211B
},
76 { USB_DEVICE(0x0586, 0x340f), .driver_info
= DEVICE_ZD1211B
},
77 { USB_DEVICE(0x0586, 0x3410), .driver_info
= DEVICE_ZD1211B
},
78 { USB_DEVICE(0x0586, 0x3412), .driver_info
= DEVICE_ZD1211B
},
79 { USB_DEVICE(0x0586, 0x3413), .driver_info
= DEVICE_ZD1211B
},
80 { USB_DEVICE(0x079b, 0x0062), .driver_info
= DEVICE_ZD1211B
},
81 { USB_DEVICE(0x07b8, 0x6001), .driver_info
= DEVICE_ZD1211B
},
82 { USB_DEVICE(0x07fa, 0x1196), .driver_info
= DEVICE_ZD1211B
},
83 { USB_DEVICE(0x083a, 0x4505), .driver_info
= DEVICE_ZD1211B
},
84 { USB_DEVICE(0x083a, 0xe501), .driver_info
= DEVICE_ZD1211B
},
85 { USB_DEVICE(0x083a, 0xe503), .driver_info
= DEVICE_ZD1211B
},
86 { USB_DEVICE(0x083a, 0xe506), .driver_info
= DEVICE_ZD1211B
},
87 { USB_DEVICE(0x0ace, 0x1215), .driver_info
= DEVICE_ZD1211B
},
88 { USB_DEVICE(0x0ace, 0xb215), .driver_info
= DEVICE_ZD1211B
},
89 { USB_DEVICE(0x0b05, 0x171b), .driver_info
= DEVICE_ZD1211B
},
90 { USB_DEVICE(0x0baf, 0x0121), .driver_info
= DEVICE_ZD1211B
},
91 { USB_DEVICE(0x0cde, 0x001a), .driver_info
= DEVICE_ZD1211B
},
92 { USB_DEVICE(0x0df6, 0x0036), .driver_info
= DEVICE_ZD1211B
},
93 { USB_DEVICE(0x129b, 0x1667), .driver_info
= DEVICE_ZD1211B
},
94 { USB_DEVICE(0x13b1, 0x0024), .driver_info
= DEVICE_ZD1211B
},
95 { USB_DEVICE(0x157e, 0x300d), .driver_info
= DEVICE_ZD1211B
},
96 { USB_DEVICE(0x1582, 0x6003), .driver_info
= DEVICE_ZD1211B
},
97 { USB_DEVICE(0x2019, 0x5303), .driver_info
= DEVICE_ZD1211B
},
98 { USB_DEVICE(0x2019, 0xed01), .driver_info
= DEVICE_ZD1211B
},
99 /* "Driverless" devices that need ejecting */
100 { USB_DEVICE(0x0ace, 0x2011), .driver_info
= DEVICE_INSTALLER
},
101 { USB_DEVICE(0x0ace, 0x20ff), .driver_info
= DEVICE_INSTALLER
},
105 MODULE_LICENSE("GPL");
106 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
107 MODULE_AUTHOR("Ulrich Kunitz");
108 MODULE_AUTHOR("Daniel Drake");
109 MODULE_VERSION("1.0");
110 MODULE_DEVICE_TABLE(usb
, usb_ids
);
112 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
113 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
115 static bool check_read_regs(struct zd_usb
*usb
, struct usb_req_read_regs
*req
,
118 /* USB device initialization */
119 static void int_urb_complete(struct urb
*urb
);
121 static int request_fw_file(
122 const struct firmware
**fw
, const char *name
, struct device
*device
)
126 dev_dbg_f(device
, "fw name %s\n", name
);
128 r
= request_firmware(fw
, name
, device
);
131 "Could not load firmware file %s. Error number %d\n",
136 static inline u16
get_bcdDevice(const struct usb_device
*udev
)
138 return le16_to_cpu(udev
->descriptor
.bcdDevice
);
141 enum upload_code_flags
{
145 /* Ensures that MAX_TRANSFER_SIZE is even. */
146 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
148 static int upload_code(struct usb_device
*udev
,
149 const u8
*data
, size_t size
, u16 code_offset
, int flags
)
154 /* USB request blocks need "kmalloced" buffers.
156 p
= kmalloc(MAX_TRANSFER_SIZE
, GFP_KERNEL
);
164 size_t transfer_size
= size
<= MAX_TRANSFER_SIZE
?
165 size
: MAX_TRANSFER_SIZE
;
167 dev_dbg_f(&udev
->dev
, "transfer size %zu\n", transfer_size
);
169 memcpy(p
, data
, transfer_size
);
170 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
171 USB_REQ_FIRMWARE_DOWNLOAD
,
172 USB_DIR_OUT
| USB_TYPE_VENDOR
,
173 code_offset
, 0, p
, transfer_size
, 1000 /* ms */);
176 "USB control request for firmware upload"
177 " failed. Error number %d\n", r
);
180 transfer_size
= r
& ~1;
182 size
-= transfer_size
;
183 data
+= transfer_size
;
184 code_offset
+= transfer_size
/sizeof(u16
);
187 if (flags
& REBOOT
) {
190 /* Use "DMA-aware" buffer. */
191 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
192 USB_REQ_FIRMWARE_CONFIRM
,
193 USB_DIR_IN
| USB_TYPE_VENDOR
,
194 0, 0, p
, sizeof(ret
), 5000 /* ms */);
195 if (r
!= sizeof(ret
)) {
197 "control request firmeware confirmation failed."
198 " Return value %d\n", r
);
206 "Internal error while downloading."
207 " Firmware confirm return value %#04x\n",
212 dev_dbg_f(&udev
->dev
, "firmware confirm return value %#04x\n",
222 static u16
get_word(const void *data
, u16 offset
)
224 const __le16
*p
= data
;
225 return le16_to_cpu(p
[offset
]);
228 static char *get_fw_name(struct zd_usb
*usb
, char *buffer
, size_t size
,
231 scnprintf(buffer
, size
, "%s%s",
233 FW_ZD1211B_PREFIX
: FW_ZD1211_PREFIX
,
238 static int handle_version_mismatch(struct zd_usb
*usb
,
239 const struct firmware
*ub_fw
)
241 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
242 const struct firmware
*ur_fw
= NULL
;
247 r
= request_fw_file(&ur_fw
,
248 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ur"),
253 r
= upload_code(udev
, ur_fw
->data
, ur_fw
->size
, FW_START
, REBOOT
);
257 offset
= (E2P_BOOT_CODE_OFFSET
* sizeof(u16
));
258 r
= upload_code(udev
, ub_fw
->data
+ offset
, ub_fw
->size
- offset
,
259 E2P_START
+ E2P_BOOT_CODE_OFFSET
, REBOOT
);
261 /* At this point, the vendor driver downloads the whole firmware
262 * image, hacks around with version IDs, and uploads it again,
263 * completely overwriting the boot code. We do not do this here as
264 * it is not required on any tested devices, and it is suspected to
267 release_firmware(ur_fw
);
271 static int upload_firmware(struct zd_usb
*usb
)
276 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
277 const struct firmware
*ub_fw
= NULL
;
278 const struct firmware
*uph_fw
= NULL
;
281 bcdDevice
= get_bcdDevice(udev
);
283 r
= request_fw_file(&ub_fw
,
284 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ub"),
289 fw_bcdDevice
= get_word(ub_fw
->data
, E2P_DATA_OFFSET
);
291 if (fw_bcdDevice
!= bcdDevice
) {
293 "firmware version %#06x and device bootcode version "
294 "%#06x differ\n", fw_bcdDevice
, bcdDevice
);
295 if (bcdDevice
<= 0x4313)
296 dev_warn(&udev
->dev
, "device has old bootcode, please "
297 "report success or failure\n");
299 r
= handle_version_mismatch(usb
, ub_fw
);
303 dev_dbg_f(&udev
->dev
,
304 "firmware device id %#06x is equal to the "
305 "actual device id\n", fw_bcdDevice
);
309 r
= request_fw_file(&uph_fw
,
310 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "uphr"),
315 r
= upload_code(udev
, uph_fw
->data
, uph_fw
->size
, FW_START
, REBOOT
);
318 "Could not upload firmware code uph. Error number %d\n",
324 release_firmware(ub_fw
);
325 release_firmware(uph_fw
);
329 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ur");
330 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ur");
331 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ub");
332 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ub");
333 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"uphr");
334 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"uphr");
336 /* Read data from device address space using "firmware interface" which does
337 * not require firmware to be loaded. */
338 int zd_usb_read_fw(struct zd_usb
*usb
, zd_addr_t addr
, u8
*data
, u16 len
)
341 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
344 /* Use "DMA-aware" buffer. */
345 buf
= kmalloc(len
, GFP_KERNEL
);
348 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
349 USB_REQ_FIRMWARE_READ_DATA
, USB_DIR_IN
| 0x40, addr
, 0,
353 "read over firmware interface failed: %d\n", r
);
355 } else if (r
!= len
) {
357 "incomplete read over firmware interface: %d/%d\n",
363 memcpy(data
, buf
, len
);
369 #define urb_dev(urb) (&(urb)->dev->dev)
371 static inline void handle_regs_int_override(struct urb
*urb
)
373 struct zd_usb
*usb
= urb
->context
;
374 struct zd_usb_interrupt
*intr
= &usb
->intr
;
376 spin_lock(&intr
->lock
);
377 if (atomic_read(&intr
->read_regs_enabled
)) {
378 atomic_set(&intr
->read_regs_enabled
, 0);
379 intr
->read_regs_int_overridden
= 1;
380 complete(&intr
->read_regs
.completion
);
382 spin_unlock(&intr
->lock
);
385 static inline void handle_regs_int(struct urb
*urb
)
387 struct zd_usb
*usb
= urb
->context
;
388 struct zd_usb_interrupt
*intr
= &usb
->intr
;
392 ZD_ASSERT(in_interrupt());
393 spin_lock(&intr
->lock
);
395 int_num
= le16_to_cpu(*(__le16
*)(urb
->transfer_buffer
+2));
396 if (int_num
== CR_INTERRUPT
) {
397 struct zd_mac
*mac
= zd_hw_mac(zd_usb_to_hw(urb
->context
));
398 spin_lock(&mac
->lock
);
399 memcpy(&mac
->intr_buffer
, urb
->transfer_buffer
,
400 USB_MAX_EP_INT_BUFFER
);
401 spin_unlock(&mac
->lock
);
402 schedule_work(&mac
->process_intr
);
403 } else if (atomic_read(&intr
->read_regs_enabled
)) {
404 len
= urb
->actual_length
;
405 intr
->read_regs
.length
= urb
->actual_length
;
406 if (len
> sizeof(intr
->read_regs
.buffer
))
407 len
= sizeof(intr
->read_regs
.buffer
);
409 memcpy(intr
->read_regs
.buffer
, urb
->transfer_buffer
, len
);
411 /* Sometimes USB_INT_ID_REGS is not overridden, but comes after
412 * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this
413 * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of
414 * retry unhandled. Next read-reg command then might catch
415 * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads.
417 if (!check_read_regs(usb
, intr
->read_regs
.req
,
418 intr
->read_regs
.req_count
))
421 atomic_set(&intr
->read_regs_enabled
, 0);
422 intr
->read_regs_int_overridden
= 0;
423 complete(&intr
->read_regs
.completion
);
429 spin_unlock(&intr
->lock
);
431 /* CR_INTERRUPT might override read_reg too. */
432 if (int_num
== CR_INTERRUPT
&& atomic_read(&intr
->read_regs_enabled
))
433 handle_regs_int_override(urb
);
436 static void int_urb_complete(struct urb
*urb
)
439 struct usb_int_header
*hdr
;
441 struct zd_usb_interrupt
*intr
;
443 switch (urb
->status
) {
452 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
455 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
459 if (urb
->actual_length
< sizeof(hdr
)) {
460 dev_dbg_f(urb_dev(urb
), "error: urb %p to small\n", urb
);
464 hdr
= urb
->transfer_buffer
;
465 if (hdr
->type
!= USB_INT_TYPE
) {
466 dev_dbg_f(urb_dev(urb
), "error: urb %p wrong type\n", urb
);
470 /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override
471 * pending USB_INT_ID_REGS causing read command timeout.
475 if (hdr
->id
!= USB_INT_ID_REGS
&& atomic_read(&intr
->read_regs_enabled
))
476 handle_regs_int_override(urb
);
479 case USB_INT_ID_REGS
:
480 handle_regs_int(urb
);
482 case USB_INT_ID_RETRY_FAILED
:
483 zd_mac_tx_failed(urb
);
486 dev_dbg_f(urb_dev(urb
), "error: urb %p unknown id %x\n", urb
,
487 (unsigned int)hdr
->id
);
492 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
494 dev_dbg_f(urb_dev(urb
), "error: resubmit urb %p err code %d\n",
496 /* TODO: add worker to reset intr->urb */
501 static inline int int_urb_interval(struct usb_device
*udev
)
503 switch (udev
->speed
) {
514 static inline int usb_int_enabled(struct zd_usb
*usb
)
517 struct zd_usb_interrupt
*intr
= &usb
->intr
;
520 spin_lock_irqsave(&intr
->lock
, flags
);
522 spin_unlock_irqrestore(&intr
->lock
, flags
);
526 int zd_usb_enable_int(struct zd_usb
*usb
)
529 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
530 struct zd_usb_interrupt
*intr
= &usb
->intr
;
533 dev_dbg_f(zd_usb_dev(usb
), "\n");
535 urb
= usb_alloc_urb(0, GFP_KERNEL
);
541 ZD_ASSERT(!irqs_disabled());
542 spin_lock_irq(&intr
->lock
);
544 spin_unlock_irq(&intr
->lock
);
549 spin_unlock_irq(&intr
->lock
);
552 intr
->buffer
= usb_alloc_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
553 GFP_KERNEL
, &intr
->buffer_dma
);
555 dev_dbg_f(zd_usb_dev(usb
),
556 "couldn't allocate transfer_buffer\n");
557 goto error_set_urb_null
;
560 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, EP_INT_IN
),
561 intr
->buffer
, USB_MAX_EP_INT_BUFFER
,
562 int_urb_complete
, usb
,
564 urb
->transfer_dma
= intr
->buffer_dma
;
565 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
567 dev_dbg_f(zd_usb_dev(usb
), "submit urb %p\n", intr
->urb
);
568 r
= usb_submit_urb(urb
, GFP_KERNEL
);
570 dev_dbg_f(zd_usb_dev(usb
),
571 "Couldn't submit urb. Error number %d\n", r
);
577 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
578 intr
->buffer
, intr
->buffer_dma
);
580 spin_lock_irq(&intr
->lock
);
582 spin_unlock_irq(&intr
->lock
);
589 void zd_usb_disable_int(struct zd_usb
*usb
)
592 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
593 struct zd_usb_interrupt
*intr
= &usb
->intr
;
596 dma_addr_t buffer_dma
;
598 spin_lock_irqsave(&intr
->lock
, flags
);
601 spin_unlock_irqrestore(&intr
->lock
, flags
);
605 buffer
= intr
->buffer
;
606 buffer_dma
= intr
->buffer_dma
;
608 spin_unlock_irqrestore(&intr
->lock
, flags
);
611 dev_dbg_f(zd_usb_dev(usb
), "urb %p killed\n", urb
);
615 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
619 static void handle_rx_packet(struct zd_usb
*usb
, const u8
*buffer
,
623 const struct rx_length_info
*length_info
;
625 if (length
< sizeof(struct rx_length_info
)) {
626 /* It's not a complete packet anyhow. */
627 dev_dbg_f(zd_usb_dev(usb
), "invalid, small RX packet : %d\n",
631 length_info
= (struct rx_length_info
*)
632 (buffer
+ length
- sizeof(struct rx_length_info
));
634 /* It might be that three frames are merged into a single URB
635 * transaction. We have to check for the length info tag.
637 * While testing we discovered that length_info might be unaligned,
638 * because if USB transactions are merged, the last packet will not
639 * be padded. Unaligned access might also happen if the length_info
640 * structure is not present.
642 if (get_unaligned_le16(&length_info
->tag
) == RX_LENGTH_INFO_TAG
)
644 unsigned int l
, k
, n
;
645 for (i
= 0, l
= 0;; i
++) {
646 k
= get_unaligned_le16(&length_info
->length
[i
]);
652 zd_mac_rx(zd_usb_to_hw(usb
), buffer
+l
, k
);
658 zd_mac_rx(zd_usb_to_hw(usb
), buffer
, length
);
662 static void rx_urb_complete(struct urb
*urb
)
666 struct zd_usb_rx
*rx
;
670 switch (urb
->status
) {
679 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
682 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
686 buffer
= urb
->transfer_buffer
;
687 length
= urb
->actual_length
;
691 tasklet_schedule(&rx
->reset_timer_tasklet
);
693 if (length
%rx
->usb_packet_size
> rx
->usb_packet_size
-4) {
694 /* If there is an old first fragment, we don't care. */
695 dev_dbg_f(urb_dev(urb
), "*** first fragment ***\n");
696 ZD_ASSERT(length
<= ARRAY_SIZE(rx
->fragment
));
697 spin_lock(&rx
->lock
);
698 memcpy(rx
->fragment
, buffer
, length
);
699 rx
->fragment_length
= length
;
700 spin_unlock(&rx
->lock
);
704 spin_lock(&rx
->lock
);
705 if (rx
->fragment_length
> 0) {
706 /* We are on a second fragment, we believe */
707 ZD_ASSERT(length
+ rx
->fragment_length
<=
708 ARRAY_SIZE(rx
->fragment
));
709 dev_dbg_f(urb_dev(urb
), "*** second fragment ***\n");
710 memcpy(rx
->fragment
+rx
->fragment_length
, buffer
, length
);
711 handle_rx_packet(usb
, rx
->fragment
,
712 rx
->fragment_length
+ length
);
713 rx
->fragment_length
= 0;
714 spin_unlock(&rx
->lock
);
716 spin_unlock(&rx
->lock
);
717 handle_rx_packet(usb
, buffer
, length
);
721 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
723 dev_dbg_f(urb_dev(urb
), "urb %p resubmit error %d\n", urb
, r
);
726 static struct urb
*alloc_rx_urb(struct zd_usb
*usb
)
728 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
732 urb
= usb_alloc_urb(0, GFP_KERNEL
);
735 buffer
= usb_alloc_coherent(udev
, USB_MAX_RX_SIZE
, GFP_KERNEL
,
742 usb_fill_bulk_urb(urb
, udev
, usb_rcvbulkpipe(udev
, EP_DATA_IN
),
743 buffer
, USB_MAX_RX_SIZE
,
744 rx_urb_complete
, usb
);
745 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
750 static void free_rx_urb(struct urb
*urb
)
754 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
755 urb
->transfer_buffer
, urb
->transfer_dma
);
759 static int __zd_usb_enable_rx(struct zd_usb
*usb
)
762 struct zd_usb_rx
*rx
= &usb
->rx
;
765 dev_dbg_f(zd_usb_dev(usb
), "\n");
768 urbs
= kcalloc(RX_URBS_COUNT
, sizeof(struct urb
*), GFP_KERNEL
);
771 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
772 urbs
[i
] = alloc_rx_urb(usb
);
777 ZD_ASSERT(!irqs_disabled());
778 spin_lock_irq(&rx
->lock
);
780 spin_unlock_irq(&rx
->lock
);
785 rx
->urbs_count
= RX_URBS_COUNT
;
786 spin_unlock_irq(&rx
->lock
);
788 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
789 r
= usb_submit_urb(urbs
[i
], GFP_KERNEL
);
796 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
797 usb_kill_urb(urbs
[i
]);
799 spin_lock_irq(&rx
->lock
);
802 spin_unlock_irq(&rx
->lock
);
805 for (i
= 0; i
< RX_URBS_COUNT
; i
++)
806 free_rx_urb(urbs
[i
]);
811 int zd_usb_enable_rx(struct zd_usb
*usb
)
814 struct zd_usb_rx
*rx
= &usb
->rx
;
816 mutex_lock(&rx
->setup_mutex
);
817 r
= __zd_usb_enable_rx(usb
);
818 mutex_unlock(&rx
->setup_mutex
);
820 zd_usb_reset_rx_idle_timer(usb
);
825 static void __zd_usb_disable_rx(struct zd_usb
*usb
)
831 struct zd_usb_rx
*rx
= &usb
->rx
;
833 spin_lock_irqsave(&rx
->lock
, flags
);
835 count
= rx
->urbs_count
;
836 spin_unlock_irqrestore(&rx
->lock
, flags
);
840 for (i
= 0; i
< count
; i
++) {
841 usb_kill_urb(urbs
[i
]);
842 free_rx_urb(urbs
[i
]);
846 spin_lock_irqsave(&rx
->lock
, flags
);
849 spin_unlock_irqrestore(&rx
->lock
, flags
);
852 void zd_usb_disable_rx(struct zd_usb
*usb
)
854 struct zd_usb_rx
*rx
= &usb
->rx
;
856 mutex_lock(&rx
->setup_mutex
);
857 __zd_usb_disable_rx(usb
);
858 mutex_unlock(&rx
->setup_mutex
);
860 tasklet_kill(&rx
->reset_timer_tasklet
);
861 cancel_delayed_work_sync(&rx
->idle_work
);
864 static void zd_usb_reset_rx(struct zd_usb
*usb
)
867 struct zd_usb_rx
*rx
= &usb
->rx
;
870 mutex_lock(&rx
->setup_mutex
);
872 spin_lock_irqsave(&rx
->lock
, flags
);
873 do_reset
= rx
->urbs
!= NULL
;
874 spin_unlock_irqrestore(&rx
->lock
, flags
);
877 __zd_usb_disable_rx(usb
);
878 __zd_usb_enable_rx(usb
);
881 mutex_unlock(&rx
->setup_mutex
);
884 zd_usb_reset_rx_idle_timer(usb
);
888 * zd_usb_disable_tx - disable transmission
889 * @usb: the zd1211rw-private USB structure
891 * Frees all URBs in the free list and marks the transmission as disabled.
893 void zd_usb_disable_tx(struct zd_usb
*usb
)
895 struct zd_usb_tx
*tx
= &usb
->tx
;
898 atomic_set(&tx
->enabled
, 0);
900 /* kill all submitted tx-urbs */
901 usb_kill_anchored_urbs(&tx
->submitted
);
903 spin_lock_irqsave(&tx
->lock
, flags
);
904 WARN_ON(!skb_queue_empty(&tx
->submitted_skbs
));
905 WARN_ON(tx
->submitted_urbs
!= 0);
906 tx
->submitted_urbs
= 0;
907 spin_unlock_irqrestore(&tx
->lock
, flags
);
909 /* The stopped state is ignored, relying on ieee80211_wake_queues()
910 * in a potentionally following zd_usb_enable_tx().
915 * zd_usb_enable_tx - enables transmission
916 * @usb: a &struct zd_usb pointer
918 * This function enables transmission and prepares the &zd_usb_tx data
921 void zd_usb_enable_tx(struct zd_usb
*usb
)
924 struct zd_usb_tx
*tx
= &usb
->tx
;
926 spin_lock_irqsave(&tx
->lock
, flags
);
927 atomic_set(&tx
->enabled
, 1);
928 tx
->submitted_urbs
= 0;
929 ieee80211_wake_queues(zd_usb_to_hw(usb
));
931 spin_unlock_irqrestore(&tx
->lock
, flags
);
934 static void tx_dec_submitted_urbs(struct zd_usb
*usb
)
936 struct zd_usb_tx
*tx
= &usb
->tx
;
939 spin_lock_irqsave(&tx
->lock
, flags
);
940 --tx
->submitted_urbs
;
941 if (tx
->stopped
&& tx
->submitted_urbs
<= ZD_USB_TX_LOW
) {
942 ieee80211_wake_queues(zd_usb_to_hw(usb
));
945 spin_unlock_irqrestore(&tx
->lock
, flags
);
948 static void tx_inc_submitted_urbs(struct zd_usb
*usb
)
950 struct zd_usb_tx
*tx
= &usb
->tx
;
953 spin_lock_irqsave(&tx
->lock
, flags
);
954 ++tx
->submitted_urbs
;
955 if (!tx
->stopped
&& tx
->submitted_urbs
> ZD_USB_TX_HIGH
) {
956 ieee80211_stop_queues(zd_usb_to_hw(usb
));
959 spin_unlock_irqrestore(&tx
->lock
, flags
);
963 * tx_urb_complete - completes the execution of an URB
966 * This function is called if the URB has been transferred to a device or an
967 * error has happened.
969 static void tx_urb_complete(struct urb
*urb
)
973 struct ieee80211_tx_info
*info
;
975 struct zd_usb_tx
*tx
;
977 skb
= (struct sk_buff
*)urb
->context
;
978 info
= IEEE80211_SKB_CB(skb
);
980 * grab 'usb' pointer before handing off the skb (since
981 * it might be freed by zd_mac_tx_to_dev or mac80211)
983 usb
= &zd_hw_mac(info
->rate_driver_data
[0])->chip
.usb
;
986 switch (urb
->status
) {
995 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
998 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
1002 skb_unlink(skb
, &usb
->tx
.submitted_skbs
);
1003 zd_mac_tx_to_dev(skb
, urb
->status
);
1005 tx_dec_submitted_urbs(usb
);
1008 usb_anchor_urb(urb
, &tx
->submitted
);
1009 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
1011 usb_unanchor_urb(urb
);
1012 dev_dbg_f(urb_dev(urb
), "error resubmit urb %p %d\n", urb
, r
);
1018 * zd_usb_tx: initiates transfer of a frame of the device
1020 * @usb: the zd1211rw-private USB structure
1021 * @skb: a &struct sk_buff pointer
1023 * This function tranmits a frame to the device. It doesn't wait for
1024 * completion. The frame must contain the control set and have all the
1025 * control set information available.
1027 * The function returns 0 if the transfer has been successfully initiated.
1029 int zd_usb_tx(struct zd_usb
*usb
, struct sk_buff
*skb
)
1032 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1033 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
1035 struct zd_usb_tx
*tx
= &usb
->tx
;
1037 if (!atomic_read(&tx
->enabled
)) {
1042 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1048 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_DATA_OUT
),
1049 skb
->data
, skb
->len
, tx_urb_complete
, skb
);
1051 info
->rate_driver_data
[1] = (void *)jiffies
;
1052 skb_queue_tail(&tx
->submitted_skbs
, skb
);
1053 usb_anchor_urb(urb
, &tx
->submitted
);
1055 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
1057 dev_dbg_f(zd_usb_dev(usb
), "error submit urb %p %d\n", urb
, r
);
1058 usb_unanchor_urb(urb
);
1059 skb_unlink(skb
, &tx
->submitted_skbs
);
1062 tx_inc_submitted_urbs(usb
);
1070 static bool zd_tx_timeout(struct zd_usb
*usb
)
1072 struct zd_usb_tx
*tx
= &usb
->tx
;
1073 struct sk_buff_head
*q
= &tx
->submitted_skbs
;
1074 struct sk_buff
*skb
, *skbnext
;
1075 struct ieee80211_tx_info
*info
;
1076 unsigned long flags
, trans_start
;
1077 bool have_timedout
= false;
1079 spin_lock_irqsave(&q
->lock
, flags
);
1080 skb_queue_walk_safe(q
, skb
, skbnext
) {
1081 info
= IEEE80211_SKB_CB(skb
);
1082 trans_start
= (unsigned long)info
->rate_driver_data
[1];
1084 if (time_is_before_jiffies(trans_start
+ ZD_TX_TIMEOUT
)) {
1085 have_timedout
= true;
1089 spin_unlock_irqrestore(&q
->lock
, flags
);
1091 return have_timedout
;
1094 static void zd_tx_watchdog_handler(struct work_struct
*work
)
1096 struct zd_usb
*usb
=
1097 container_of(work
, struct zd_usb
, tx
.watchdog_work
.work
);
1098 struct zd_usb_tx
*tx
= &usb
->tx
;
1100 if (!atomic_read(&tx
->enabled
) || !tx
->watchdog_enabled
)
1102 if (!zd_tx_timeout(usb
))
1105 /* TX halted, try reset */
1106 dev_warn(zd_usb_dev(usb
), "TX-stall detected, resetting device...");
1108 usb_queue_reset_device(usb
->intf
);
1110 /* reset will stop this worker, don't rearm */
1113 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1114 ZD_TX_WATCHDOG_INTERVAL
);
1117 void zd_tx_watchdog_enable(struct zd_usb
*usb
)
1119 struct zd_usb_tx
*tx
= &usb
->tx
;
1121 if (!tx
->watchdog_enabled
) {
1122 dev_dbg_f(zd_usb_dev(usb
), "\n");
1123 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1124 ZD_TX_WATCHDOG_INTERVAL
);
1125 tx
->watchdog_enabled
= 1;
1129 void zd_tx_watchdog_disable(struct zd_usb
*usb
)
1131 struct zd_usb_tx
*tx
= &usb
->tx
;
1133 if (tx
->watchdog_enabled
) {
1134 dev_dbg_f(zd_usb_dev(usb
), "\n");
1135 tx
->watchdog_enabled
= 0;
1136 cancel_delayed_work_sync(&tx
->watchdog_work
);
1140 static void zd_rx_idle_timer_handler(struct work_struct
*work
)
1142 struct zd_usb
*usb
=
1143 container_of(work
, struct zd_usb
, rx
.idle_work
.work
);
1144 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1146 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1149 dev_dbg_f(zd_usb_dev(usb
), "\n");
1151 /* 30 seconds since last rx, reset rx */
1152 zd_usb_reset_rx(usb
);
1155 static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param
)
1157 struct zd_usb
*usb
= (struct zd_usb
*)param
;
1159 zd_usb_reset_rx_idle_timer(usb
);
1162 void zd_usb_reset_rx_idle_timer(struct zd_usb
*usb
)
1164 struct zd_usb_rx
*rx
= &usb
->rx
;
1166 mod_delayed_work(zd_workqueue
, &rx
->idle_work
, ZD_RX_IDLE_INTERVAL
);
1169 static inline void init_usb_interrupt(struct zd_usb
*usb
)
1171 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1173 spin_lock_init(&intr
->lock
);
1174 intr
->interval
= int_urb_interval(zd_usb_to_usbdev(usb
));
1175 init_completion(&intr
->read_regs
.completion
);
1176 atomic_set(&intr
->read_regs_enabled
, 0);
1177 intr
->read_regs
.cr_int_addr
= cpu_to_le16((u16
)CR_INTERRUPT
);
1180 static inline void init_usb_rx(struct zd_usb
*usb
)
1182 struct zd_usb_rx
*rx
= &usb
->rx
;
1184 spin_lock_init(&rx
->lock
);
1185 mutex_init(&rx
->setup_mutex
);
1186 if (interface_to_usbdev(usb
->intf
)->speed
== USB_SPEED_HIGH
) {
1187 rx
->usb_packet_size
= 512;
1189 rx
->usb_packet_size
= 64;
1191 ZD_ASSERT(rx
->fragment_length
== 0);
1192 INIT_DELAYED_WORK(&rx
->idle_work
, zd_rx_idle_timer_handler
);
1193 rx
->reset_timer_tasklet
.func
= zd_usb_reset_rx_idle_timer_tasklet
;
1194 rx
->reset_timer_tasklet
.data
= (unsigned long)usb
;
1197 static inline void init_usb_tx(struct zd_usb
*usb
)
1199 struct zd_usb_tx
*tx
= &usb
->tx
;
1201 spin_lock_init(&tx
->lock
);
1202 atomic_set(&tx
->enabled
, 0);
1204 skb_queue_head_init(&tx
->submitted_skbs
);
1205 init_usb_anchor(&tx
->submitted
);
1206 tx
->submitted_urbs
= 0;
1207 tx
->watchdog_enabled
= 0;
1208 INIT_DELAYED_WORK(&tx
->watchdog_work
, zd_tx_watchdog_handler
);
1211 void zd_usb_init(struct zd_usb
*usb
, struct ieee80211_hw
*hw
,
1212 struct usb_interface
*intf
)
1214 memset(usb
, 0, sizeof(*usb
));
1215 usb
->intf
= usb_get_intf(intf
);
1216 usb_set_intfdata(usb
->intf
, hw
);
1217 init_usb_anchor(&usb
->submitted_cmds
);
1218 init_usb_interrupt(usb
);
1223 void zd_usb_clear(struct zd_usb
*usb
)
1225 usb_set_intfdata(usb
->intf
, NULL
);
1226 usb_put_intf(usb
->intf
);
1227 ZD_MEMCLEAR(usb
, sizeof(*usb
));
1228 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1231 static const char *speed(enum usb_device_speed speed
)
1236 case USB_SPEED_FULL
:
1238 case USB_SPEED_HIGH
:
1241 return "unknown speed";
1245 static int scnprint_id(struct usb_device
*udev
, char *buffer
, size_t size
)
1247 return scnprintf(buffer
, size
, "%04hx:%04hx v%04hx %s",
1248 le16_to_cpu(udev
->descriptor
.idVendor
),
1249 le16_to_cpu(udev
->descriptor
.idProduct
),
1250 get_bcdDevice(udev
),
1251 speed(udev
->speed
));
1254 int zd_usb_scnprint_id(struct zd_usb
*usb
, char *buffer
, size_t size
)
1256 struct usb_device
*udev
= interface_to_usbdev(usb
->intf
);
1257 return scnprint_id(udev
, buffer
, size
);
1261 static void print_id(struct usb_device
*udev
)
1265 scnprint_id(udev
, buffer
, sizeof(buffer
));
1266 buffer
[sizeof(buffer
)-1] = 0;
1267 dev_dbg_f(&udev
->dev
, "%s\n", buffer
);
1270 #define print_id(udev) do { } while (0)
1273 static int eject_installer(struct usb_interface
*intf
)
1275 struct usb_device
*udev
= interface_to_usbdev(intf
);
1276 struct usb_host_interface
*iface_desc
= &intf
->altsetting
[0];
1277 struct usb_endpoint_descriptor
*endpoint
;
1282 /* Find bulk out endpoint */
1283 for (r
= 1; r
>= 0; r
--) {
1284 endpoint
= &iface_desc
->endpoint
[r
].desc
;
1285 if (usb_endpoint_dir_out(endpoint
) &&
1286 usb_endpoint_xfer_bulk(endpoint
)) {
1287 bulk_out_ep
= endpoint
->bEndpointAddress
;
1293 "zd1211rw: Could not find bulk out endpoint\n");
1297 cmd
= kzalloc(31, GFP_KERNEL
);
1301 /* USB bulk command block */
1302 cmd
[0] = 0x55; /* bulk command signature */
1303 cmd
[1] = 0x53; /* bulk command signature */
1304 cmd
[2] = 0x42; /* bulk command signature */
1305 cmd
[3] = 0x43; /* bulk command signature */
1306 cmd
[14] = 6; /* command length */
1308 cmd
[15] = 0x1b; /* SCSI command: START STOP UNIT */
1309 cmd
[19] = 0x2; /* eject disc */
1311 dev_info(&udev
->dev
, "Ejecting virtual installer media...\n");
1312 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, bulk_out_ep
),
1313 cmd
, 31, NULL
, 2000);
1318 /* At this point, the device disconnects and reconnects with the real
1321 usb_set_intfdata(intf
, NULL
);
1325 int zd_usb_init_hw(struct zd_usb
*usb
)
1328 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1330 dev_dbg_f(zd_usb_dev(usb
), "\n");
1332 r
= upload_firmware(usb
);
1334 dev_err(zd_usb_dev(usb
),
1335 "couldn't load firmware. Error number %d\n", r
);
1339 r
= usb_reset_configuration(zd_usb_to_usbdev(usb
));
1341 dev_dbg_f(zd_usb_dev(usb
),
1342 "couldn't reset configuration. Error number %d\n", r
);
1346 r
= zd_mac_init_hw(mac
->hw
);
1348 dev_dbg_f(zd_usb_dev(usb
),
1349 "couldn't initialize mac. Error number %d\n", r
);
1353 usb
->initialized
= 1;
1357 static int probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1360 struct usb_device
*udev
= interface_to_usbdev(intf
);
1362 struct ieee80211_hw
*hw
= NULL
;
1366 if (id
->driver_info
& DEVICE_INSTALLER
)
1367 return eject_installer(intf
);
1369 switch (udev
->speed
) {
1371 case USB_SPEED_FULL
:
1372 case USB_SPEED_HIGH
:
1375 dev_dbg_f(&intf
->dev
, "Unknown USB speed\n");
1380 r
= usb_reset_device(udev
);
1383 "couldn't reset usb device. Error number %d\n", r
);
1387 hw
= zd_mac_alloc_hw(intf
);
1393 usb
= &zd_hw_mac(hw
)->chip
.usb
;
1394 usb
->is_zd1211b
= (id
->driver_info
== DEVICE_ZD1211B
) != 0;
1396 r
= zd_mac_preinit_hw(hw
);
1398 dev_dbg_f(&intf
->dev
,
1399 "couldn't initialize mac. Error number %d\n", r
);
1403 r
= ieee80211_register_hw(hw
);
1405 dev_dbg_f(&intf
->dev
,
1406 "couldn't register device. Error number %d\n", r
);
1410 dev_dbg_f(&intf
->dev
, "successful\n");
1411 dev_info(&intf
->dev
, "%s\n", wiphy_name(hw
->wiphy
));
1414 usb_reset_device(interface_to_usbdev(intf
));
1416 zd_mac_clear(zd_hw_mac(hw
));
1417 ieee80211_free_hw(hw
);
1422 static void disconnect(struct usb_interface
*intf
)
1424 struct ieee80211_hw
*hw
= zd_intf_to_hw(intf
);
1428 /* Either something really bad happened, or we're just dealing with
1429 * a DEVICE_INSTALLER. */
1433 mac
= zd_hw_mac(hw
);
1434 usb
= &mac
->chip
.usb
;
1436 dev_dbg_f(zd_usb_dev(usb
), "\n");
1438 ieee80211_unregister_hw(hw
);
1440 /* Just in case something has gone wrong! */
1441 zd_usb_disable_tx(usb
);
1442 zd_usb_disable_rx(usb
);
1443 zd_usb_disable_int(usb
);
1445 /* If the disconnect has been caused by a removal of the
1446 * driver module, the reset allows reloading of the driver. If the
1447 * reset will not be executed here, the upload of the firmware in the
1448 * probe function caused by the reloading of the driver will fail.
1450 usb_reset_device(interface_to_usbdev(intf
));
1453 ieee80211_free_hw(hw
);
1454 dev_dbg(&intf
->dev
, "disconnected\n");
1457 static void zd_usb_resume(struct zd_usb
*usb
)
1459 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1462 dev_dbg_f(zd_usb_dev(usb
), "\n");
1464 r
= zd_op_start(zd_usb_to_hw(usb
));
1466 dev_warn(zd_usb_dev(usb
), "Device resume failed "
1467 "with error code %d. Retrying...\n", r
);
1468 if (usb
->was_running
)
1469 set_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1470 usb_queue_reset_device(usb
->intf
);
1474 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
) {
1475 r
= zd_restore_settings(mac
);
1477 dev_dbg(zd_usb_dev(usb
),
1478 "failed to restore settings, %d\n", r
);
1484 static void zd_usb_stop(struct zd_usb
*usb
)
1486 dev_dbg_f(zd_usb_dev(usb
), "\n");
1488 zd_op_stop(zd_usb_to_hw(usb
));
1490 zd_usb_disable_tx(usb
);
1491 zd_usb_disable_rx(usb
);
1492 zd_usb_disable_int(usb
);
1494 usb
->initialized
= 0;
1497 static int pre_reset(struct usb_interface
*intf
)
1499 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1503 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1506 mac
= zd_hw_mac(hw
);
1507 usb
= &mac
->chip
.usb
;
1509 usb
->was_running
= test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1513 mutex_lock(&mac
->chip
.mutex
);
1517 static int post_reset(struct usb_interface
*intf
)
1519 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1523 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1526 mac
= zd_hw_mac(hw
);
1527 usb
= &mac
->chip
.usb
;
1529 mutex_unlock(&mac
->chip
.mutex
);
1531 if (usb
->was_running
)
1536 static struct usb_driver driver
= {
1537 .name
= KBUILD_MODNAME
,
1538 .id_table
= usb_ids
,
1540 .disconnect
= disconnect
,
1541 .pre_reset
= pre_reset
,
1542 .post_reset
= post_reset
,
1543 .disable_hub_initiated_lpm
= 1,
1546 struct workqueue_struct
*zd_workqueue
;
1548 static int __init
usb_init(void)
1552 pr_debug("%s usb_init()\n", driver
.name
);
1554 zd_workqueue
= create_singlethread_workqueue(driver
.name
);
1555 if (zd_workqueue
== NULL
) {
1556 printk(KERN_ERR
"%s couldn't create workqueue\n", driver
.name
);
1560 r
= usb_register(&driver
);
1562 destroy_workqueue(zd_workqueue
);
1563 printk(KERN_ERR
"%s usb_register() failed. Error number %d\n",
1568 pr_debug("%s initialized\n", driver
.name
);
1572 static void __exit
usb_exit(void)
1574 pr_debug("%s usb_exit()\n", driver
.name
);
1575 usb_deregister(&driver
);
1576 destroy_workqueue(zd_workqueue
);
1579 module_init(usb_init
);
1580 module_exit(usb_exit
);
1582 static int zd_ep_regs_out_msg(struct usb_device
*udev
, void *data
, int len
,
1583 int *actual_length
, int timeout
)
1585 /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in
1586 * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint
1589 struct usb_host_endpoint
*ep
;
1592 pipe
= usb_sndintpipe(udev
, EP_REGS_OUT
);
1593 ep
= usb_pipe_endpoint(udev
, pipe
);
1597 if (usb_endpoint_xfer_int(&ep
->desc
)) {
1598 return usb_interrupt_msg(udev
, pipe
, data
, len
,
1599 actual_length
, timeout
);
1601 pipe
= usb_sndbulkpipe(udev
, EP_REGS_OUT
);
1602 return usb_bulk_msg(udev
, pipe
, data
, len
, actual_length
,
1607 static int usb_int_regs_length(unsigned int count
)
1609 return sizeof(struct usb_int_regs
) + count
* sizeof(struct reg_data
);
1612 static void prepare_read_regs_int(struct zd_usb
*usb
,
1613 struct usb_req_read_regs
*req
,
1616 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1618 spin_lock_irq(&intr
->lock
);
1619 atomic_set(&intr
->read_regs_enabled
, 1);
1620 intr
->read_regs
.req
= req
;
1621 intr
->read_regs
.req_count
= count
;
1622 INIT_COMPLETION(intr
->read_regs
.completion
);
1623 spin_unlock_irq(&intr
->lock
);
1626 static void disable_read_regs_int(struct zd_usb
*usb
)
1628 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1630 spin_lock_irq(&intr
->lock
);
1631 atomic_set(&intr
->read_regs_enabled
, 0);
1632 spin_unlock_irq(&intr
->lock
);
1635 static bool check_read_regs(struct zd_usb
*usb
, struct usb_req_read_regs
*req
,
1639 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1640 struct read_regs_int
*rr
= &intr
->read_regs
;
1641 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1643 /* The created block size seems to be larger than expected.
1644 * However results appear to be correct.
1646 if (rr
->length
< usb_int_regs_length(count
)) {
1647 dev_dbg_f(zd_usb_dev(usb
),
1648 "error: actual length %d less than expected %d\n",
1649 rr
->length
, usb_int_regs_length(count
));
1653 if (rr
->length
> sizeof(rr
->buffer
)) {
1654 dev_dbg_f(zd_usb_dev(usb
),
1655 "error: actual length %d exceeds buffer size %zu\n",
1656 rr
->length
, sizeof(rr
->buffer
));
1660 for (i
= 0; i
< count
; i
++) {
1661 struct reg_data
*rd
= ®s
->regs
[i
];
1662 if (rd
->addr
!= req
->addr
[i
]) {
1663 dev_dbg_f(zd_usb_dev(usb
),
1664 "rd[%d] addr %#06hx expected %#06hx\n", i
,
1665 le16_to_cpu(rd
->addr
),
1666 le16_to_cpu(req
->addr
[i
]));
1674 static int get_results(struct zd_usb
*usb
, u16
*values
,
1675 struct usb_req_read_regs
*req
, unsigned int count
,
1680 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1681 struct read_regs_int
*rr
= &intr
->read_regs
;
1682 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1684 spin_lock_irq(&intr
->lock
);
1688 /* Read failed because firmware bug? */
1689 *retry
= !!intr
->read_regs_int_overridden
;
1693 if (!check_read_regs(usb
, req
, count
)) {
1694 dev_dbg_f(zd_usb_dev(usb
), "error: invalid read regs\n");
1698 for (i
= 0; i
< count
; i
++) {
1699 struct reg_data
*rd
= ®s
->regs
[i
];
1700 values
[i
] = le16_to_cpu(rd
->value
);
1705 spin_unlock_irq(&intr
->lock
);
1709 int zd_usb_ioread16v(struct zd_usb
*usb
, u16
*values
,
1710 const zd_addr_t
*addresses
, unsigned int count
)
1712 int r
, i
, req_len
, actual_req_len
, try_count
= 0;
1713 struct usb_device
*udev
;
1714 struct usb_req_read_regs
*req
= NULL
;
1715 unsigned long timeout
;
1719 dev_dbg_f(zd_usb_dev(usb
), "error: count is zero\n");
1722 if (count
> USB_MAX_IOREAD16_COUNT
) {
1723 dev_dbg_f(zd_usb_dev(usb
),
1724 "error: count %u exceeds possible max %u\n",
1725 count
, USB_MAX_IOREAD16_COUNT
);
1729 dev_dbg_f(zd_usb_dev(usb
),
1730 "error: io in atomic context not supported\n");
1731 return -EWOULDBLOCK
;
1733 if (!usb_int_enabled(usb
)) {
1734 dev_dbg_f(zd_usb_dev(usb
),
1735 "error: usb interrupt not enabled\n");
1736 return -EWOULDBLOCK
;
1739 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1740 BUILD_BUG_ON(sizeof(struct usb_req_read_regs
) + USB_MAX_IOREAD16_COUNT
*
1741 sizeof(__le16
) > sizeof(usb
->req_buf
));
1742 BUG_ON(sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
) >
1743 sizeof(usb
->req_buf
));
1745 req_len
= sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
);
1746 req
= (void *)usb
->req_buf
;
1748 req
->id
= cpu_to_le16(USB_REQ_READ_REGS
);
1749 for (i
= 0; i
< count
; i
++)
1750 req
->addr
[i
] = cpu_to_le16((u16
)addresses
[i
]);
1754 udev
= zd_usb_to_usbdev(usb
);
1755 prepare_read_regs_int(usb
, req
, count
);
1756 r
= zd_ep_regs_out_msg(udev
, req
, req_len
, &actual_req_len
, 50 /*ms*/);
1758 dev_dbg_f(zd_usb_dev(usb
),
1759 "error in zd_ep_regs_out_msg(). Error number %d\n", r
);
1762 if (req_len
!= actual_req_len
) {
1763 dev_dbg_f(zd_usb_dev(usb
), "error in zd_ep_regs_out_msg()\n"
1764 " req_len %d != actual_req_len %d\n",
1765 req_len
, actual_req_len
);
1770 timeout
= wait_for_completion_timeout(&usb
->intr
.read_regs
.completion
,
1771 msecs_to_jiffies(50));
1773 disable_read_regs_int(usb
);
1774 dev_dbg_f(zd_usb_dev(usb
), "read timed out\n");
1779 r
= get_results(usb
, values
, req
, count
, &retry
);
1780 if (retry
&& try_count
< 20) {
1781 dev_dbg_f(zd_usb_dev(usb
), "read retry, tries so far: %d\n",
1789 static void iowrite16v_urb_complete(struct urb
*urb
)
1791 struct zd_usb
*usb
= urb
->context
;
1793 if (urb
->status
&& !usb
->cmd_error
)
1794 usb
->cmd_error
= urb
->status
;
1796 if (!usb
->cmd_error
&&
1797 urb
->actual_length
!= urb
->transfer_buffer_length
)
1798 usb
->cmd_error
= -EIO
;
1801 static int zd_submit_waiting_urb(struct zd_usb
*usb
, bool last
)
1804 struct urb
*urb
= usb
->urb_async_waiting
;
1809 usb
->urb_async_waiting
= NULL
;
1812 urb
->transfer_flags
|= URB_NO_INTERRUPT
;
1814 usb_anchor_urb(urb
, &usb
->submitted_cmds
);
1815 r
= usb_submit_urb(urb
, GFP_KERNEL
);
1817 usb_unanchor_urb(urb
);
1818 dev_dbg_f(zd_usb_dev(usb
),
1819 "error in usb_submit_urb(). Error number %d\n", r
);
1823 /* fall-through with r == 0 */
1829 void zd_usb_iowrite16v_async_start(struct zd_usb
*usb
)
1831 ZD_ASSERT(usb_anchor_empty(&usb
->submitted_cmds
));
1832 ZD_ASSERT(usb
->urb_async_waiting
== NULL
);
1833 ZD_ASSERT(!usb
->in_async
);
1835 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1839 usb
->urb_async_waiting
= NULL
;
1842 int zd_usb_iowrite16v_async_end(struct zd_usb
*usb
, unsigned int timeout
)
1846 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1847 ZD_ASSERT(usb
->in_async
);
1849 /* Submit last iowrite16v URB */
1850 r
= zd_submit_waiting_urb(usb
, true);
1852 dev_dbg_f(zd_usb_dev(usb
),
1853 "error in zd_submit_waiting_usb(). "
1854 "Error number %d\n", r
);
1856 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1861 timeout
= usb_wait_anchor_empty_timeout(&usb
->submitted_cmds
,
1864 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1865 if (usb
->cmd_error
== -ENOENT
) {
1866 dev_dbg_f(zd_usb_dev(usb
), "timed out");
1878 int zd_usb_iowrite16v_async(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1882 struct usb_device
*udev
;
1883 struct usb_req_write_regs
*req
= NULL
;
1886 struct usb_host_endpoint
*ep
;
1888 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1889 ZD_ASSERT(usb
->in_async
);
1893 if (count
> USB_MAX_IOWRITE16_COUNT
) {
1894 dev_dbg_f(zd_usb_dev(usb
),
1895 "error: count %u exceeds possible max %u\n",
1896 count
, USB_MAX_IOWRITE16_COUNT
);
1900 dev_dbg_f(zd_usb_dev(usb
),
1901 "error: io in atomic context not supported\n");
1902 return -EWOULDBLOCK
;
1905 udev
= zd_usb_to_usbdev(usb
);
1907 ep
= usb_pipe_endpoint(udev
, usb_sndintpipe(udev
, EP_REGS_OUT
));
1911 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1915 req_len
= sizeof(struct usb_req_write_regs
) +
1916 count
* sizeof(struct reg_data
);
1917 req
= kmalloc(req_len
, GFP_KERNEL
);
1923 req
->id
= cpu_to_le16(USB_REQ_WRITE_REGS
);
1924 for (i
= 0; i
< count
; i
++) {
1925 struct reg_data
*rw
= &req
->reg_writes
[i
];
1926 rw
->addr
= cpu_to_le16((u16
)ioreqs
[i
].addr
);
1927 rw
->value
= cpu_to_le16(ioreqs
[i
].value
);
1930 /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
1931 * endpoint is bulk. Select correct type URB by endpoint descriptor.
1933 if (usb_endpoint_xfer_int(&ep
->desc
))
1934 usb_fill_int_urb(urb
, udev
, usb_sndintpipe(udev
, EP_REGS_OUT
),
1935 req
, req_len
, iowrite16v_urb_complete
, usb
,
1936 ep
->desc
.bInterval
);
1938 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_REGS_OUT
),
1939 req
, req_len
, iowrite16v_urb_complete
, usb
);
1941 urb
->transfer_flags
|= URB_FREE_BUFFER
;
1943 /* Submit previous URB */
1944 r
= zd_submit_waiting_urb(usb
, false);
1946 dev_dbg_f(zd_usb_dev(usb
),
1947 "error in zd_submit_waiting_usb(). "
1948 "Error number %d\n", r
);
1952 /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1953 * of currect batch except for very last.
1955 usb
->urb_async_waiting
= urb
;
1962 int zd_usb_iowrite16v(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1967 zd_usb_iowrite16v_async_start(usb
);
1968 r
= zd_usb_iowrite16v_async(usb
, ioreqs
, count
);
1970 zd_usb_iowrite16v_async_end(usb
, 0);
1973 return zd_usb_iowrite16v_async_end(usb
, 50 /* ms */);
1976 int zd_usb_rfwrite(struct zd_usb
*usb
, u32 value
, u8 bits
)
1979 struct usb_device
*udev
;
1980 struct usb_req_rfwrite
*req
= NULL
;
1981 int i
, req_len
, actual_req_len
;
1982 u16 bit_value_template
;
1985 dev_dbg_f(zd_usb_dev(usb
),
1986 "error: io in atomic context not supported\n");
1987 return -EWOULDBLOCK
;
1989 if (bits
< USB_MIN_RFWRITE_BIT_COUNT
) {
1990 dev_dbg_f(zd_usb_dev(usb
),
1991 "error: bits %d are smaller than"
1992 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1993 bits
, USB_MIN_RFWRITE_BIT_COUNT
);
1996 if (bits
> USB_MAX_RFWRITE_BIT_COUNT
) {
1997 dev_dbg_f(zd_usb_dev(usb
),
1998 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1999 bits
, USB_MAX_RFWRITE_BIT_COUNT
);
2003 if (value
& (~0UL << bits
)) {
2004 dev_dbg_f(zd_usb_dev(usb
),
2005 "error: value %#09x has bits >= %d set\n",
2011 dev_dbg_f(zd_usb_dev(usb
), "value %#09x bits %d\n", value
, bits
);
2013 r
= zd_usb_ioread16(usb
, &bit_value_template
, ZD_CR203
);
2015 dev_dbg_f(zd_usb_dev(usb
),
2016 "error %d: Couldn't read ZD_CR203\n", r
);
2019 bit_value_template
&= ~(RF_IF_LE
|RF_CLK
|RF_DATA
);
2021 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
2022 BUILD_BUG_ON(sizeof(struct usb_req_rfwrite
) +
2023 USB_MAX_RFWRITE_BIT_COUNT
* sizeof(__le16
) >
2024 sizeof(usb
->req_buf
));
2025 BUG_ON(sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
) >
2026 sizeof(usb
->req_buf
));
2028 req_len
= sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
);
2029 req
= (void *)usb
->req_buf
;
2031 req
->id
= cpu_to_le16(USB_REQ_WRITE_RF
);
2032 /* 1: 3683a, but not used in ZYDAS driver */
2033 req
->value
= cpu_to_le16(2);
2034 req
->bits
= cpu_to_le16(bits
);
2036 for (i
= 0; i
< bits
; i
++) {
2037 u16 bv
= bit_value_template
;
2038 if (value
& (1 << (bits
-1-i
)))
2040 req
->bit_values
[i
] = cpu_to_le16(bv
);
2043 udev
= zd_usb_to_usbdev(usb
);
2044 r
= zd_ep_regs_out_msg(udev
, req
, req_len
, &actual_req_len
, 50 /*ms*/);
2046 dev_dbg_f(zd_usb_dev(usb
),
2047 "error in zd_ep_regs_out_msg(). Error number %d\n", r
);
2050 if (req_len
!= actual_req_len
) {
2051 dev_dbg_f(zd_usb_dev(usb
), "error in zd_ep_regs_out_msg()"
2052 " req_len %d != actual_req_len %d\n",
2053 req_len
, actual_req_len
);
2058 /* FALL-THROUGH with r == 0 */