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
);
158 dev_err(&udev
->dev
, "out of memory\n");
165 size_t transfer_size
= size
<= MAX_TRANSFER_SIZE
?
166 size
: MAX_TRANSFER_SIZE
;
168 dev_dbg_f(&udev
->dev
, "transfer size %zu\n", transfer_size
);
170 memcpy(p
, data
, transfer_size
);
171 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
172 USB_REQ_FIRMWARE_DOWNLOAD
,
173 USB_DIR_OUT
| USB_TYPE_VENDOR
,
174 code_offset
, 0, p
, transfer_size
, 1000 /* ms */);
177 "USB control request for firmware upload"
178 " failed. Error number %d\n", r
);
181 transfer_size
= r
& ~1;
183 size
-= transfer_size
;
184 data
+= transfer_size
;
185 code_offset
+= transfer_size
/sizeof(u16
);
188 if (flags
& REBOOT
) {
191 /* Use "DMA-aware" buffer. */
192 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
193 USB_REQ_FIRMWARE_CONFIRM
,
194 USB_DIR_IN
| USB_TYPE_VENDOR
,
195 0, 0, p
, sizeof(ret
), 5000 /* ms */);
196 if (r
!= sizeof(ret
)) {
198 "control request firmeware confirmation failed."
199 " Return value %d\n", r
);
207 "Internal error while downloading."
208 " Firmware confirm return value %#04x\n",
213 dev_dbg_f(&udev
->dev
, "firmware confirm return value %#04x\n",
223 static u16
get_word(const void *data
, u16 offset
)
225 const __le16
*p
= data
;
226 return le16_to_cpu(p
[offset
]);
229 static char *get_fw_name(struct zd_usb
*usb
, char *buffer
, size_t size
,
232 scnprintf(buffer
, size
, "%s%s",
234 FW_ZD1211B_PREFIX
: FW_ZD1211_PREFIX
,
239 static int handle_version_mismatch(struct zd_usb
*usb
,
240 const struct firmware
*ub_fw
)
242 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
243 const struct firmware
*ur_fw
= NULL
;
248 r
= request_fw_file(&ur_fw
,
249 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ur"),
254 r
= upload_code(udev
, ur_fw
->data
, ur_fw
->size
, FW_START
, REBOOT
);
258 offset
= (E2P_BOOT_CODE_OFFSET
* sizeof(u16
));
259 r
= upload_code(udev
, ub_fw
->data
+ offset
, ub_fw
->size
- offset
,
260 E2P_START
+ E2P_BOOT_CODE_OFFSET
, REBOOT
);
262 /* At this point, the vendor driver downloads the whole firmware
263 * image, hacks around with version IDs, and uploads it again,
264 * completely overwriting the boot code. We do not do this here as
265 * it is not required on any tested devices, and it is suspected to
268 release_firmware(ur_fw
);
272 static int upload_firmware(struct zd_usb
*usb
)
277 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
278 const struct firmware
*ub_fw
= NULL
;
279 const struct firmware
*uph_fw
= NULL
;
282 bcdDevice
= get_bcdDevice(udev
);
284 r
= request_fw_file(&ub_fw
,
285 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ub"),
290 fw_bcdDevice
= get_word(ub_fw
->data
, E2P_DATA_OFFSET
);
292 if (fw_bcdDevice
!= bcdDevice
) {
294 "firmware version %#06x and device bootcode version "
295 "%#06x differ\n", fw_bcdDevice
, bcdDevice
);
296 if (bcdDevice
<= 0x4313)
297 dev_warn(&udev
->dev
, "device has old bootcode, please "
298 "report success or failure\n");
300 r
= handle_version_mismatch(usb
, ub_fw
);
304 dev_dbg_f(&udev
->dev
,
305 "firmware device id %#06x is equal to the "
306 "actual device id\n", fw_bcdDevice
);
310 r
= request_fw_file(&uph_fw
,
311 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "uphr"),
316 r
= upload_code(udev
, uph_fw
->data
, uph_fw
->size
, FW_START
, REBOOT
);
319 "Could not upload firmware code uph. Error number %d\n",
325 release_firmware(ub_fw
);
326 release_firmware(uph_fw
);
330 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ur");
331 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ur");
332 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ub");
333 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ub");
334 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"uphr");
335 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"uphr");
337 /* Read data from device address space using "firmware interface" which does
338 * not require firmware to be loaded. */
339 int zd_usb_read_fw(struct zd_usb
*usb
, zd_addr_t addr
, u8
*data
, u16 len
)
342 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
345 /* Use "DMA-aware" buffer. */
346 buf
= kmalloc(len
, GFP_KERNEL
);
349 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
350 USB_REQ_FIRMWARE_READ_DATA
, USB_DIR_IN
| 0x40, addr
, 0,
354 "read over firmware interface failed: %d\n", r
);
356 } else if (r
!= len
) {
358 "incomplete read over firmware interface: %d/%d\n",
364 memcpy(data
, buf
, len
);
370 #define urb_dev(urb) (&(urb)->dev->dev)
372 static inline void handle_regs_int_override(struct urb
*urb
)
374 struct zd_usb
*usb
= urb
->context
;
375 struct zd_usb_interrupt
*intr
= &usb
->intr
;
377 spin_lock(&intr
->lock
);
378 if (atomic_read(&intr
->read_regs_enabled
)) {
379 atomic_set(&intr
->read_regs_enabled
, 0);
380 intr
->read_regs_int_overridden
= 1;
381 complete(&intr
->read_regs
.completion
);
383 spin_unlock(&intr
->lock
);
386 static inline void handle_regs_int(struct urb
*urb
)
388 struct zd_usb
*usb
= urb
->context
;
389 struct zd_usb_interrupt
*intr
= &usb
->intr
;
393 ZD_ASSERT(in_interrupt());
394 spin_lock(&intr
->lock
);
396 int_num
= le16_to_cpu(*(__le16
*)(urb
->transfer_buffer
+2));
397 if (int_num
== CR_INTERRUPT
) {
398 struct zd_mac
*mac
= zd_hw_mac(zd_usb_to_hw(urb
->context
));
399 spin_lock(&mac
->lock
);
400 memcpy(&mac
->intr_buffer
, urb
->transfer_buffer
,
401 USB_MAX_EP_INT_BUFFER
);
402 spin_unlock(&mac
->lock
);
403 schedule_work(&mac
->process_intr
);
404 } else if (atomic_read(&intr
->read_regs_enabled
)) {
405 len
= urb
->actual_length
;
406 intr
->read_regs
.length
= urb
->actual_length
;
407 if (len
> sizeof(intr
->read_regs
.buffer
))
408 len
= sizeof(intr
->read_regs
.buffer
);
410 memcpy(intr
->read_regs
.buffer
, urb
->transfer_buffer
, len
);
412 /* Sometimes USB_INT_ID_REGS is not overridden, but comes after
413 * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this
414 * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of
415 * retry unhandled. Next read-reg command then might catch
416 * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads.
418 if (!check_read_regs(usb
, intr
->read_regs
.req
,
419 intr
->read_regs
.req_count
))
422 atomic_set(&intr
->read_regs_enabled
, 0);
423 intr
->read_regs_int_overridden
= 0;
424 complete(&intr
->read_regs
.completion
);
430 spin_unlock(&intr
->lock
);
432 /* CR_INTERRUPT might override read_reg too. */
433 if (int_num
== CR_INTERRUPT
&& atomic_read(&intr
->read_regs_enabled
))
434 handle_regs_int_override(urb
);
437 static void int_urb_complete(struct urb
*urb
)
440 struct usb_int_header
*hdr
;
442 struct zd_usb_interrupt
*intr
;
444 switch (urb
->status
) {
453 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
456 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
460 if (urb
->actual_length
< sizeof(hdr
)) {
461 dev_dbg_f(urb_dev(urb
), "error: urb %p to small\n", urb
);
465 hdr
= urb
->transfer_buffer
;
466 if (hdr
->type
!= USB_INT_TYPE
) {
467 dev_dbg_f(urb_dev(urb
), "error: urb %p wrong type\n", urb
);
471 /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override
472 * pending USB_INT_ID_REGS causing read command timeout.
476 if (hdr
->id
!= USB_INT_ID_REGS
&& atomic_read(&intr
->read_regs_enabled
))
477 handle_regs_int_override(urb
);
480 case USB_INT_ID_REGS
:
481 handle_regs_int(urb
);
483 case USB_INT_ID_RETRY_FAILED
:
484 zd_mac_tx_failed(urb
);
487 dev_dbg_f(urb_dev(urb
), "error: urb %p unknown id %x\n", urb
,
488 (unsigned int)hdr
->id
);
493 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
495 dev_dbg_f(urb_dev(urb
), "error: resubmit urb %p err code %d\n",
497 /* TODO: add worker to reset intr->urb */
502 static inline int int_urb_interval(struct usb_device
*udev
)
504 switch (udev
->speed
) {
515 static inline int usb_int_enabled(struct zd_usb
*usb
)
518 struct zd_usb_interrupt
*intr
= &usb
->intr
;
521 spin_lock_irqsave(&intr
->lock
, flags
);
523 spin_unlock_irqrestore(&intr
->lock
, flags
);
527 int zd_usb_enable_int(struct zd_usb
*usb
)
530 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
531 struct zd_usb_interrupt
*intr
= &usb
->intr
;
534 dev_dbg_f(zd_usb_dev(usb
), "\n");
536 urb
= usb_alloc_urb(0, GFP_KERNEL
);
542 ZD_ASSERT(!irqs_disabled());
543 spin_lock_irq(&intr
->lock
);
545 spin_unlock_irq(&intr
->lock
);
550 spin_unlock_irq(&intr
->lock
);
553 intr
->buffer
= usb_alloc_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
554 GFP_KERNEL
, &intr
->buffer_dma
);
556 dev_dbg_f(zd_usb_dev(usb
),
557 "couldn't allocate transfer_buffer\n");
558 goto error_set_urb_null
;
561 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, EP_INT_IN
),
562 intr
->buffer
, USB_MAX_EP_INT_BUFFER
,
563 int_urb_complete
, usb
,
565 urb
->transfer_dma
= intr
->buffer_dma
;
566 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
568 dev_dbg_f(zd_usb_dev(usb
), "submit urb %p\n", intr
->urb
);
569 r
= usb_submit_urb(urb
, GFP_KERNEL
);
571 dev_dbg_f(zd_usb_dev(usb
),
572 "Couldn't submit urb. Error number %d\n", r
);
578 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
579 intr
->buffer
, intr
->buffer_dma
);
581 spin_lock_irq(&intr
->lock
);
583 spin_unlock_irq(&intr
->lock
);
590 void zd_usb_disable_int(struct zd_usb
*usb
)
593 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
594 struct zd_usb_interrupt
*intr
= &usb
->intr
;
597 dma_addr_t buffer_dma
;
599 spin_lock_irqsave(&intr
->lock
, flags
);
602 spin_unlock_irqrestore(&intr
->lock
, flags
);
606 buffer
= intr
->buffer
;
607 buffer_dma
= intr
->buffer_dma
;
609 spin_unlock_irqrestore(&intr
->lock
, flags
);
612 dev_dbg_f(zd_usb_dev(usb
), "urb %p killed\n", urb
);
616 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
620 static void handle_rx_packet(struct zd_usb
*usb
, const u8
*buffer
,
624 const struct rx_length_info
*length_info
;
626 if (length
< sizeof(struct rx_length_info
)) {
627 /* It's not a complete packet anyhow. */
628 dev_dbg_f(zd_usb_dev(usb
), "invalid, small RX packet : %d\n",
632 length_info
= (struct rx_length_info
*)
633 (buffer
+ length
- sizeof(struct rx_length_info
));
635 /* It might be that three frames are merged into a single URB
636 * transaction. We have to check for the length info tag.
638 * While testing we discovered that length_info might be unaligned,
639 * because if USB transactions are merged, the last packet will not
640 * be padded. Unaligned access might also happen if the length_info
641 * structure is not present.
643 if (get_unaligned_le16(&length_info
->tag
) == RX_LENGTH_INFO_TAG
)
645 unsigned int l
, k
, n
;
646 for (i
= 0, l
= 0;; i
++) {
647 k
= get_unaligned_le16(&length_info
->length
[i
]);
653 zd_mac_rx(zd_usb_to_hw(usb
), buffer
+l
, k
);
659 zd_mac_rx(zd_usb_to_hw(usb
), buffer
, length
);
663 static void rx_urb_complete(struct urb
*urb
)
667 struct zd_usb_rx
*rx
;
671 switch (urb
->status
) {
680 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
683 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
687 buffer
= urb
->transfer_buffer
;
688 length
= urb
->actual_length
;
692 tasklet_schedule(&rx
->reset_timer_tasklet
);
694 if (length
%rx
->usb_packet_size
> rx
->usb_packet_size
-4) {
695 /* If there is an old first fragment, we don't care. */
696 dev_dbg_f(urb_dev(urb
), "*** first fragment ***\n");
697 ZD_ASSERT(length
<= ARRAY_SIZE(rx
->fragment
));
698 spin_lock(&rx
->lock
);
699 memcpy(rx
->fragment
, buffer
, length
);
700 rx
->fragment_length
= length
;
701 spin_unlock(&rx
->lock
);
705 spin_lock(&rx
->lock
);
706 if (rx
->fragment_length
> 0) {
707 /* We are on a second fragment, we believe */
708 ZD_ASSERT(length
+ rx
->fragment_length
<=
709 ARRAY_SIZE(rx
->fragment
));
710 dev_dbg_f(urb_dev(urb
), "*** second fragment ***\n");
711 memcpy(rx
->fragment
+rx
->fragment_length
, buffer
, length
);
712 handle_rx_packet(usb
, rx
->fragment
,
713 rx
->fragment_length
+ length
);
714 rx
->fragment_length
= 0;
715 spin_unlock(&rx
->lock
);
717 spin_unlock(&rx
->lock
);
718 handle_rx_packet(usb
, buffer
, length
);
722 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
724 dev_dbg_f(urb_dev(urb
), "urb %p resubmit error %d\n", urb
, r
);
727 static struct urb
*alloc_rx_urb(struct zd_usb
*usb
)
729 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
733 urb
= usb_alloc_urb(0, GFP_KERNEL
);
736 buffer
= usb_alloc_coherent(udev
, USB_MAX_RX_SIZE
, GFP_KERNEL
,
743 usb_fill_bulk_urb(urb
, udev
, usb_rcvbulkpipe(udev
, EP_DATA_IN
),
744 buffer
, USB_MAX_RX_SIZE
,
745 rx_urb_complete
, usb
);
746 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
751 static void free_rx_urb(struct urb
*urb
)
755 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
756 urb
->transfer_buffer
, urb
->transfer_dma
);
760 static int __zd_usb_enable_rx(struct zd_usb
*usb
)
763 struct zd_usb_rx
*rx
= &usb
->rx
;
766 dev_dbg_f(zd_usb_dev(usb
), "\n");
769 urbs
= kcalloc(RX_URBS_COUNT
, sizeof(struct urb
*), GFP_KERNEL
);
772 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
773 urbs
[i
] = alloc_rx_urb(usb
);
778 ZD_ASSERT(!irqs_disabled());
779 spin_lock_irq(&rx
->lock
);
781 spin_unlock_irq(&rx
->lock
);
786 rx
->urbs_count
= RX_URBS_COUNT
;
787 spin_unlock_irq(&rx
->lock
);
789 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
790 r
= usb_submit_urb(urbs
[i
], GFP_KERNEL
);
797 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
798 usb_kill_urb(urbs
[i
]);
800 spin_lock_irq(&rx
->lock
);
803 spin_unlock_irq(&rx
->lock
);
806 for (i
= 0; i
< RX_URBS_COUNT
; i
++)
807 free_rx_urb(urbs
[i
]);
812 int zd_usb_enable_rx(struct zd_usb
*usb
)
815 struct zd_usb_rx
*rx
= &usb
->rx
;
817 mutex_lock(&rx
->setup_mutex
);
818 r
= __zd_usb_enable_rx(usb
);
819 mutex_unlock(&rx
->setup_mutex
);
821 zd_usb_reset_rx_idle_timer(usb
);
826 static void __zd_usb_disable_rx(struct zd_usb
*usb
)
832 struct zd_usb_rx
*rx
= &usb
->rx
;
834 spin_lock_irqsave(&rx
->lock
, flags
);
836 count
= rx
->urbs_count
;
837 spin_unlock_irqrestore(&rx
->lock
, flags
);
841 for (i
= 0; i
< count
; i
++) {
842 usb_kill_urb(urbs
[i
]);
843 free_rx_urb(urbs
[i
]);
847 spin_lock_irqsave(&rx
->lock
, flags
);
850 spin_unlock_irqrestore(&rx
->lock
, flags
);
853 void zd_usb_disable_rx(struct zd_usb
*usb
)
855 struct zd_usb_rx
*rx
= &usb
->rx
;
857 mutex_lock(&rx
->setup_mutex
);
858 __zd_usb_disable_rx(usb
);
859 mutex_unlock(&rx
->setup_mutex
);
861 tasklet_kill(&rx
->reset_timer_tasklet
);
862 cancel_delayed_work_sync(&rx
->idle_work
);
865 static void zd_usb_reset_rx(struct zd_usb
*usb
)
868 struct zd_usb_rx
*rx
= &usb
->rx
;
871 mutex_lock(&rx
->setup_mutex
);
873 spin_lock_irqsave(&rx
->lock
, flags
);
874 do_reset
= rx
->urbs
!= NULL
;
875 spin_unlock_irqrestore(&rx
->lock
, flags
);
878 __zd_usb_disable_rx(usb
);
879 __zd_usb_enable_rx(usb
);
882 mutex_unlock(&rx
->setup_mutex
);
885 zd_usb_reset_rx_idle_timer(usb
);
889 * zd_usb_disable_tx - disable transmission
890 * @usb: the zd1211rw-private USB structure
892 * Frees all URBs in the free list and marks the transmission as disabled.
894 void zd_usb_disable_tx(struct zd_usb
*usb
)
896 struct zd_usb_tx
*tx
= &usb
->tx
;
899 atomic_set(&tx
->enabled
, 0);
901 /* kill all submitted tx-urbs */
902 usb_kill_anchored_urbs(&tx
->submitted
);
904 spin_lock_irqsave(&tx
->lock
, flags
);
905 WARN_ON(!skb_queue_empty(&tx
->submitted_skbs
));
906 WARN_ON(tx
->submitted_urbs
!= 0);
907 tx
->submitted_urbs
= 0;
908 spin_unlock_irqrestore(&tx
->lock
, flags
);
910 /* The stopped state is ignored, relying on ieee80211_wake_queues()
911 * in a potentionally following zd_usb_enable_tx().
916 * zd_usb_enable_tx - enables transmission
917 * @usb: a &struct zd_usb pointer
919 * This function enables transmission and prepares the &zd_usb_tx data
922 void zd_usb_enable_tx(struct zd_usb
*usb
)
925 struct zd_usb_tx
*tx
= &usb
->tx
;
927 spin_lock_irqsave(&tx
->lock
, flags
);
928 atomic_set(&tx
->enabled
, 1);
929 tx
->submitted_urbs
= 0;
930 ieee80211_wake_queues(zd_usb_to_hw(usb
));
932 spin_unlock_irqrestore(&tx
->lock
, flags
);
935 static void tx_dec_submitted_urbs(struct zd_usb
*usb
)
937 struct zd_usb_tx
*tx
= &usb
->tx
;
940 spin_lock_irqsave(&tx
->lock
, flags
);
941 --tx
->submitted_urbs
;
942 if (tx
->stopped
&& tx
->submitted_urbs
<= ZD_USB_TX_LOW
) {
943 ieee80211_wake_queues(zd_usb_to_hw(usb
));
946 spin_unlock_irqrestore(&tx
->lock
, flags
);
949 static void tx_inc_submitted_urbs(struct zd_usb
*usb
)
951 struct zd_usb_tx
*tx
= &usb
->tx
;
954 spin_lock_irqsave(&tx
->lock
, flags
);
955 ++tx
->submitted_urbs
;
956 if (!tx
->stopped
&& tx
->submitted_urbs
> ZD_USB_TX_HIGH
) {
957 ieee80211_stop_queues(zd_usb_to_hw(usb
));
960 spin_unlock_irqrestore(&tx
->lock
, flags
);
964 * tx_urb_complete - completes the execution of an URB
967 * This function is called if the URB has been transferred to a device or an
968 * error has happened.
970 static void tx_urb_complete(struct urb
*urb
)
974 struct ieee80211_tx_info
*info
;
976 struct zd_usb_tx
*tx
;
978 skb
= (struct sk_buff
*)urb
->context
;
979 info
= IEEE80211_SKB_CB(skb
);
981 * grab 'usb' pointer before handing off the skb (since
982 * it might be freed by zd_mac_tx_to_dev or mac80211)
984 usb
= &zd_hw_mac(info
->rate_driver_data
[0])->chip
.usb
;
987 switch (urb
->status
) {
996 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
999 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
1003 skb_unlink(skb
, &usb
->tx
.submitted_skbs
);
1004 zd_mac_tx_to_dev(skb
, urb
->status
);
1006 tx_dec_submitted_urbs(usb
);
1009 usb_anchor_urb(urb
, &tx
->submitted
);
1010 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
1012 usb_unanchor_urb(urb
);
1013 dev_dbg_f(urb_dev(urb
), "error resubmit urb %p %d\n", urb
, r
);
1019 * zd_usb_tx: initiates transfer of a frame of the device
1021 * @usb: the zd1211rw-private USB structure
1022 * @skb: a &struct sk_buff pointer
1024 * This function tranmits a frame to the device. It doesn't wait for
1025 * completion. The frame must contain the control set and have all the
1026 * control set information available.
1028 * The function returns 0 if the transfer has been successfully initiated.
1030 int zd_usb_tx(struct zd_usb
*usb
, struct sk_buff
*skb
)
1033 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1034 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
1036 struct zd_usb_tx
*tx
= &usb
->tx
;
1038 if (!atomic_read(&tx
->enabled
)) {
1043 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1049 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_DATA_OUT
),
1050 skb
->data
, skb
->len
, tx_urb_complete
, skb
);
1052 info
->rate_driver_data
[1] = (void *)jiffies
;
1053 skb_queue_tail(&tx
->submitted_skbs
, skb
);
1054 usb_anchor_urb(urb
, &tx
->submitted
);
1056 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
1058 dev_dbg_f(zd_usb_dev(usb
), "error submit urb %p %d\n", urb
, r
);
1059 usb_unanchor_urb(urb
);
1060 skb_unlink(skb
, &tx
->submitted_skbs
);
1063 tx_inc_submitted_urbs(usb
);
1071 static bool zd_tx_timeout(struct zd_usb
*usb
)
1073 struct zd_usb_tx
*tx
= &usb
->tx
;
1074 struct sk_buff_head
*q
= &tx
->submitted_skbs
;
1075 struct sk_buff
*skb
, *skbnext
;
1076 struct ieee80211_tx_info
*info
;
1077 unsigned long flags
, trans_start
;
1078 bool have_timedout
= false;
1080 spin_lock_irqsave(&q
->lock
, flags
);
1081 skb_queue_walk_safe(q
, skb
, skbnext
) {
1082 info
= IEEE80211_SKB_CB(skb
);
1083 trans_start
= (unsigned long)info
->rate_driver_data
[1];
1085 if (time_is_before_jiffies(trans_start
+ ZD_TX_TIMEOUT
)) {
1086 have_timedout
= true;
1090 spin_unlock_irqrestore(&q
->lock
, flags
);
1092 return have_timedout
;
1095 static void zd_tx_watchdog_handler(struct work_struct
*work
)
1097 struct zd_usb
*usb
=
1098 container_of(work
, struct zd_usb
, tx
.watchdog_work
.work
);
1099 struct zd_usb_tx
*tx
= &usb
->tx
;
1101 if (!atomic_read(&tx
->enabled
) || !tx
->watchdog_enabled
)
1103 if (!zd_tx_timeout(usb
))
1106 /* TX halted, try reset */
1107 dev_warn(zd_usb_dev(usb
), "TX-stall detected, reseting device...");
1109 usb_queue_reset_device(usb
->intf
);
1111 /* reset will stop this worker, don't rearm */
1114 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1115 ZD_TX_WATCHDOG_INTERVAL
);
1118 void zd_tx_watchdog_enable(struct zd_usb
*usb
)
1120 struct zd_usb_tx
*tx
= &usb
->tx
;
1122 if (!tx
->watchdog_enabled
) {
1123 dev_dbg_f(zd_usb_dev(usb
), "\n");
1124 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1125 ZD_TX_WATCHDOG_INTERVAL
);
1126 tx
->watchdog_enabled
= 1;
1130 void zd_tx_watchdog_disable(struct zd_usb
*usb
)
1132 struct zd_usb_tx
*tx
= &usb
->tx
;
1134 if (tx
->watchdog_enabled
) {
1135 dev_dbg_f(zd_usb_dev(usb
), "\n");
1136 tx
->watchdog_enabled
= 0;
1137 cancel_delayed_work_sync(&tx
->watchdog_work
);
1141 static void zd_rx_idle_timer_handler(struct work_struct
*work
)
1143 struct zd_usb
*usb
=
1144 container_of(work
, struct zd_usb
, rx
.idle_work
.work
);
1145 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1147 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1150 dev_dbg_f(zd_usb_dev(usb
), "\n");
1152 /* 30 seconds since last rx, reset rx */
1153 zd_usb_reset_rx(usb
);
1156 static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param
)
1158 struct zd_usb
*usb
= (struct zd_usb
*)param
;
1160 zd_usb_reset_rx_idle_timer(usb
);
1163 void zd_usb_reset_rx_idle_timer(struct zd_usb
*usb
)
1165 struct zd_usb_rx
*rx
= &usb
->rx
;
1167 cancel_delayed_work(&rx
->idle_work
);
1168 queue_delayed_work(zd_workqueue
, &rx
->idle_work
, ZD_RX_IDLE_INTERVAL
);
1171 static inline void init_usb_interrupt(struct zd_usb
*usb
)
1173 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1175 spin_lock_init(&intr
->lock
);
1176 intr
->interval
= int_urb_interval(zd_usb_to_usbdev(usb
));
1177 init_completion(&intr
->read_regs
.completion
);
1178 atomic_set(&intr
->read_regs_enabled
, 0);
1179 intr
->read_regs
.cr_int_addr
= cpu_to_le16((u16
)CR_INTERRUPT
);
1182 static inline void init_usb_rx(struct zd_usb
*usb
)
1184 struct zd_usb_rx
*rx
= &usb
->rx
;
1186 spin_lock_init(&rx
->lock
);
1187 mutex_init(&rx
->setup_mutex
);
1188 if (interface_to_usbdev(usb
->intf
)->speed
== USB_SPEED_HIGH
) {
1189 rx
->usb_packet_size
= 512;
1191 rx
->usb_packet_size
= 64;
1193 ZD_ASSERT(rx
->fragment_length
== 0);
1194 INIT_DELAYED_WORK(&rx
->idle_work
, zd_rx_idle_timer_handler
);
1195 rx
->reset_timer_tasklet
.func
= zd_usb_reset_rx_idle_timer_tasklet
;
1196 rx
->reset_timer_tasklet
.data
= (unsigned long)usb
;
1199 static inline void init_usb_tx(struct zd_usb
*usb
)
1201 struct zd_usb_tx
*tx
= &usb
->tx
;
1203 spin_lock_init(&tx
->lock
);
1204 atomic_set(&tx
->enabled
, 0);
1206 skb_queue_head_init(&tx
->submitted_skbs
);
1207 init_usb_anchor(&tx
->submitted
);
1208 tx
->submitted_urbs
= 0;
1209 tx
->watchdog_enabled
= 0;
1210 INIT_DELAYED_WORK(&tx
->watchdog_work
, zd_tx_watchdog_handler
);
1213 void zd_usb_init(struct zd_usb
*usb
, struct ieee80211_hw
*hw
,
1214 struct usb_interface
*intf
)
1216 memset(usb
, 0, sizeof(*usb
));
1217 usb
->intf
= usb_get_intf(intf
);
1218 usb_set_intfdata(usb
->intf
, hw
);
1219 init_usb_anchor(&usb
->submitted_cmds
);
1220 init_usb_interrupt(usb
);
1225 void zd_usb_clear(struct zd_usb
*usb
)
1227 usb_set_intfdata(usb
->intf
, NULL
);
1228 usb_put_intf(usb
->intf
);
1229 ZD_MEMCLEAR(usb
, sizeof(*usb
));
1230 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1233 static const char *speed(enum usb_device_speed speed
)
1238 case USB_SPEED_FULL
:
1240 case USB_SPEED_HIGH
:
1243 return "unknown speed";
1247 static int scnprint_id(struct usb_device
*udev
, char *buffer
, size_t size
)
1249 return scnprintf(buffer
, size
, "%04hx:%04hx v%04hx %s",
1250 le16_to_cpu(udev
->descriptor
.idVendor
),
1251 le16_to_cpu(udev
->descriptor
.idProduct
),
1252 get_bcdDevice(udev
),
1253 speed(udev
->speed
));
1256 int zd_usb_scnprint_id(struct zd_usb
*usb
, char *buffer
, size_t size
)
1258 struct usb_device
*udev
= interface_to_usbdev(usb
->intf
);
1259 return scnprint_id(udev
, buffer
, size
);
1263 static void print_id(struct usb_device
*udev
)
1267 scnprint_id(udev
, buffer
, sizeof(buffer
));
1268 buffer
[sizeof(buffer
)-1] = 0;
1269 dev_dbg_f(&udev
->dev
, "%s\n", buffer
);
1272 #define print_id(udev) do { } while (0)
1275 static int eject_installer(struct usb_interface
*intf
)
1277 struct usb_device
*udev
= interface_to_usbdev(intf
);
1278 struct usb_host_interface
*iface_desc
= &intf
->altsetting
[0];
1279 struct usb_endpoint_descriptor
*endpoint
;
1284 /* Find bulk out endpoint */
1285 for (r
= 1; r
>= 0; r
--) {
1286 endpoint
= &iface_desc
->endpoint
[r
].desc
;
1287 if (usb_endpoint_dir_out(endpoint
) &&
1288 usb_endpoint_xfer_bulk(endpoint
)) {
1289 bulk_out_ep
= endpoint
->bEndpointAddress
;
1295 "zd1211rw: Could not find bulk out endpoint\n");
1299 cmd
= kzalloc(31, GFP_KERNEL
);
1303 /* USB bulk command block */
1304 cmd
[0] = 0x55; /* bulk command signature */
1305 cmd
[1] = 0x53; /* bulk command signature */
1306 cmd
[2] = 0x42; /* bulk command signature */
1307 cmd
[3] = 0x43; /* bulk command signature */
1308 cmd
[14] = 6; /* command length */
1310 cmd
[15] = 0x1b; /* SCSI command: START STOP UNIT */
1311 cmd
[19] = 0x2; /* eject disc */
1313 dev_info(&udev
->dev
, "Ejecting virtual installer media...\n");
1314 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, bulk_out_ep
),
1315 cmd
, 31, NULL
, 2000);
1320 /* At this point, the device disconnects and reconnects with the real
1323 usb_set_intfdata(intf
, NULL
);
1327 int zd_usb_init_hw(struct zd_usb
*usb
)
1330 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1332 dev_dbg_f(zd_usb_dev(usb
), "\n");
1334 r
= upload_firmware(usb
);
1336 dev_err(zd_usb_dev(usb
),
1337 "couldn't load firmware. Error number %d\n", r
);
1341 r
= usb_reset_configuration(zd_usb_to_usbdev(usb
));
1343 dev_dbg_f(zd_usb_dev(usb
),
1344 "couldn't reset configuration. Error number %d\n", r
);
1348 r
= zd_mac_init_hw(mac
->hw
);
1350 dev_dbg_f(zd_usb_dev(usb
),
1351 "couldn't initialize mac. Error number %d\n", r
);
1355 usb
->initialized
= 1;
1359 static int probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1362 struct usb_device
*udev
= interface_to_usbdev(intf
);
1364 struct ieee80211_hw
*hw
= NULL
;
1368 if (id
->driver_info
& DEVICE_INSTALLER
)
1369 return eject_installer(intf
);
1371 switch (udev
->speed
) {
1373 case USB_SPEED_FULL
:
1374 case USB_SPEED_HIGH
:
1377 dev_dbg_f(&intf
->dev
, "Unknown USB speed\n");
1382 r
= usb_reset_device(udev
);
1385 "couldn't reset usb device. Error number %d\n", r
);
1389 hw
= zd_mac_alloc_hw(intf
);
1395 usb
= &zd_hw_mac(hw
)->chip
.usb
;
1396 usb
->is_zd1211b
= (id
->driver_info
== DEVICE_ZD1211B
) != 0;
1398 r
= zd_mac_preinit_hw(hw
);
1400 dev_dbg_f(&intf
->dev
,
1401 "couldn't initialize mac. Error number %d\n", r
);
1405 r
= ieee80211_register_hw(hw
);
1407 dev_dbg_f(&intf
->dev
,
1408 "couldn't register device. Error number %d\n", r
);
1412 dev_dbg_f(&intf
->dev
, "successful\n");
1413 dev_info(&intf
->dev
, "%s\n", wiphy_name(hw
->wiphy
));
1416 usb_reset_device(interface_to_usbdev(intf
));
1418 zd_mac_clear(zd_hw_mac(hw
));
1419 ieee80211_free_hw(hw
);
1424 static void disconnect(struct usb_interface
*intf
)
1426 struct ieee80211_hw
*hw
= zd_intf_to_hw(intf
);
1430 /* Either something really bad happened, or we're just dealing with
1431 * a DEVICE_INSTALLER. */
1435 mac
= zd_hw_mac(hw
);
1436 usb
= &mac
->chip
.usb
;
1438 dev_dbg_f(zd_usb_dev(usb
), "\n");
1440 ieee80211_unregister_hw(hw
);
1442 /* Just in case something has gone wrong! */
1443 zd_usb_disable_tx(usb
);
1444 zd_usb_disable_rx(usb
);
1445 zd_usb_disable_int(usb
);
1447 /* If the disconnect has been caused by a removal of the
1448 * driver module, the reset allows reloading of the driver. If the
1449 * reset will not be executed here, the upload of the firmware in the
1450 * probe function caused by the reloading of the driver will fail.
1452 usb_reset_device(interface_to_usbdev(intf
));
1455 ieee80211_free_hw(hw
);
1456 dev_dbg(&intf
->dev
, "disconnected\n");
1459 static void zd_usb_resume(struct zd_usb
*usb
)
1461 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1464 dev_dbg_f(zd_usb_dev(usb
), "\n");
1466 r
= zd_op_start(zd_usb_to_hw(usb
));
1468 dev_warn(zd_usb_dev(usb
), "Device resume failed "
1469 "with error code %d. Retrying...\n", r
);
1470 if (usb
->was_running
)
1471 set_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1472 usb_queue_reset_device(usb
->intf
);
1476 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
) {
1477 r
= zd_restore_settings(mac
);
1479 dev_dbg(zd_usb_dev(usb
),
1480 "failed to restore settings, %d\n", r
);
1486 static void zd_usb_stop(struct zd_usb
*usb
)
1488 dev_dbg_f(zd_usb_dev(usb
), "\n");
1490 zd_op_stop(zd_usb_to_hw(usb
));
1492 zd_usb_disable_tx(usb
);
1493 zd_usb_disable_rx(usb
);
1494 zd_usb_disable_int(usb
);
1496 usb
->initialized
= 0;
1499 static int pre_reset(struct usb_interface
*intf
)
1501 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1505 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1508 mac
= zd_hw_mac(hw
);
1509 usb
= &mac
->chip
.usb
;
1511 usb
->was_running
= test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1515 mutex_lock(&mac
->chip
.mutex
);
1519 static int post_reset(struct usb_interface
*intf
)
1521 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1525 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1528 mac
= zd_hw_mac(hw
);
1529 usb
= &mac
->chip
.usb
;
1531 mutex_unlock(&mac
->chip
.mutex
);
1533 if (usb
->was_running
)
1538 static struct usb_driver driver
= {
1539 .name
= KBUILD_MODNAME
,
1540 .id_table
= usb_ids
,
1542 .disconnect
= disconnect
,
1543 .pre_reset
= pre_reset
,
1544 .post_reset
= post_reset
,
1547 struct workqueue_struct
*zd_workqueue
;
1549 static int __init
usb_init(void)
1553 pr_debug("%s usb_init()\n", driver
.name
);
1555 zd_workqueue
= create_singlethread_workqueue(driver
.name
);
1556 if (zd_workqueue
== NULL
) {
1557 printk(KERN_ERR
"%s couldn't create workqueue\n", driver
.name
);
1561 r
= usb_register(&driver
);
1563 destroy_workqueue(zd_workqueue
);
1564 printk(KERN_ERR
"%s usb_register() failed. Error number %d\n",
1569 pr_debug("%s initialized\n", driver
.name
);
1573 static void __exit
usb_exit(void)
1575 pr_debug("%s usb_exit()\n", driver
.name
);
1576 usb_deregister(&driver
);
1577 destroy_workqueue(zd_workqueue
);
1580 module_init(usb_init
);
1581 module_exit(usb_exit
);
1583 static int zd_ep_regs_out_msg(struct usb_device
*udev
, void *data
, int len
,
1584 int *actual_length
, int timeout
)
1586 /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in
1587 * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint
1590 struct usb_host_endpoint
*ep
;
1593 pipe
= usb_sndintpipe(udev
, EP_REGS_OUT
);
1594 ep
= usb_pipe_endpoint(udev
, pipe
);
1598 if (usb_endpoint_xfer_int(&ep
->desc
)) {
1599 return usb_interrupt_msg(udev
, pipe
, data
, len
,
1600 actual_length
, timeout
);
1602 pipe
= usb_sndbulkpipe(udev
, EP_REGS_OUT
);
1603 return usb_bulk_msg(udev
, pipe
, data
, len
, actual_length
,
1608 static int usb_int_regs_length(unsigned int count
)
1610 return sizeof(struct usb_int_regs
) + count
* sizeof(struct reg_data
);
1613 static void prepare_read_regs_int(struct zd_usb
*usb
,
1614 struct usb_req_read_regs
*req
,
1617 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1619 spin_lock_irq(&intr
->lock
);
1620 atomic_set(&intr
->read_regs_enabled
, 1);
1621 intr
->read_regs
.req
= req
;
1622 intr
->read_regs
.req_count
= count
;
1623 INIT_COMPLETION(intr
->read_regs
.completion
);
1624 spin_unlock_irq(&intr
->lock
);
1627 static void disable_read_regs_int(struct zd_usb
*usb
)
1629 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1631 spin_lock_irq(&intr
->lock
);
1632 atomic_set(&intr
->read_regs_enabled
, 0);
1633 spin_unlock_irq(&intr
->lock
);
1636 static bool check_read_regs(struct zd_usb
*usb
, struct usb_req_read_regs
*req
,
1640 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1641 struct read_regs_int
*rr
= &intr
->read_regs
;
1642 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1644 /* The created block size seems to be larger than expected.
1645 * However results appear to be correct.
1647 if (rr
->length
< usb_int_regs_length(count
)) {
1648 dev_dbg_f(zd_usb_dev(usb
),
1649 "error: actual length %d less than expected %d\n",
1650 rr
->length
, usb_int_regs_length(count
));
1654 if (rr
->length
> sizeof(rr
->buffer
)) {
1655 dev_dbg_f(zd_usb_dev(usb
),
1656 "error: actual length %d exceeds buffer size %zu\n",
1657 rr
->length
, sizeof(rr
->buffer
));
1661 for (i
= 0; i
< count
; i
++) {
1662 struct reg_data
*rd
= ®s
->regs
[i
];
1663 if (rd
->addr
!= req
->addr
[i
]) {
1664 dev_dbg_f(zd_usb_dev(usb
),
1665 "rd[%d] addr %#06hx expected %#06hx\n", i
,
1666 le16_to_cpu(rd
->addr
),
1667 le16_to_cpu(req
->addr
[i
]));
1675 static int get_results(struct zd_usb
*usb
, u16
*values
,
1676 struct usb_req_read_regs
*req
, unsigned int count
,
1681 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1682 struct read_regs_int
*rr
= &intr
->read_regs
;
1683 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1685 spin_lock_irq(&intr
->lock
);
1689 /* Read failed because firmware bug? */
1690 *retry
= !!intr
->read_regs_int_overridden
;
1694 if (!check_read_regs(usb
, req
, count
)) {
1695 dev_dbg_f(zd_usb_dev(usb
), "error: invalid read regs\n");
1699 for (i
= 0; i
< count
; i
++) {
1700 struct reg_data
*rd
= ®s
->regs
[i
];
1701 values
[i
] = le16_to_cpu(rd
->value
);
1706 spin_unlock_irq(&intr
->lock
);
1710 int zd_usb_ioread16v(struct zd_usb
*usb
, u16
*values
,
1711 const zd_addr_t
*addresses
, unsigned int count
)
1713 int r
, i
, req_len
, actual_req_len
, try_count
= 0;
1714 struct usb_device
*udev
;
1715 struct usb_req_read_regs
*req
= NULL
;
1716 unsigned long timeout
;
1720 dev_dbg_f(zd_usb_dev(usb
), "error: count is zero\n");
1723 if (count
> USB_MAX_IOREAD16_COUNT
) {
1724 dev_dbg_f(zd_usb_dev(usb
),
1725 "error: count %u exceeds possible max %u\n",
1726 count
, USB_MAX_IOREAD16_COUNT
);
1730 dev_dbg_f(zd_usb_dev(usb
),
1731 "error: io in atomic context not supported\n");
1732 return -EWOULDBLOCK
;
1734 if (!usb_int_enabled(usb
)) {
1735 dev_dbg_f(zd_usb_dev(usb
),
1736 "error: usb interrupt not enabled\n");
1737 return -EWOULDBLOCK
;
1740 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1741 BUILD_BUG_ON(sizeof(struct usb_req_read_regs
) + USB_MAX_IOREAD16_COUNT
*
1742 sizeof(__le16
) > sizeof(usb
->req_buf
));
1743 BUG_ON(sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
) >
1744 sizeof(usb
->req_buf
));
1746 req_len
= sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
);
1747 req
= (void *)usb
->req_buf
;
1749 req
->id
= cpu_to_le16(USB_REQ_READ_REGS
);
1750 for (i
= 0; i
< count
; i
++)
1751 req
->addr
[i
] = cpu_to_le16((u16
)addresses
[i
]);
1755 udev
= zd_usb_to_usbdev(usb
);
1756 prepare_read_regs_int(usb
, req
, count
);
1757 r
= zd_ep_regs_out_msg(udev
, req
, req_len
, &actual_req_len
, 50 /*ms*/);
1759 dev_dbg_f(zd_usb_dev(usb
),
1760 "error in zd_ep_regs_out_msg(). Error number %d\n", r
);
1763 if (req_len
!= actual_req_len
) {
1764 dev_dbg_f(zd_usb_dev(usb
), "error in zd_ep_regs_out_msg()\n"
1765 " req_len %d != actual_req_len %d\n",
1766 req_len
, actual_req_len
);
1771 timeout
= wait_for_completion_timeout(&usb
->intr
.read_regs
.completion
,
1772 msecs_to_jiffies(50));
1774 disable_read_regs_int(usb
);
1775 dev_dbg_f(zd_usb_dev(usb
), "read timed out\n");
1780 r
= get_results(usb
, values
, req
, count
, &retry
);
1781 if (retry
&& try_count
< 20) {
1782 dev_dbg_f(zd_usb_dev(usb
), "read retry, tries so far: %d\n",
1790 static void iowrite16v_urb_complete(struct urb
*urb
)
1792 struct zd_usb
*usb
= urb
->context
;
1794 if (urb
->status
&& !usb
->cmd_error
)
1795 usb
->cmd_error
= urb
->status
;
1797 if (!usb
->cmd_error
&&
1798 urb
->actual_length
!= urb
->transfer_buffer_length
)
1799 usb
->cmd_error
= -EIO
;
1802 static int zd_submit_waiting_urb(struct zd_usb
*usb
, bool last
)
1805 struct urb
*urb
= usb
->urb_async_waiting
;
1810 usb
->urb_async_waiting
= NULL
;
1813 urb
->transfer_flags
|= URB_NO_INTERRUPT
;
1815 usb_anchor_urb(urb
, &usb
->submitted_cmds
);
1816 r
= usb_submit_urb(urb
, GFP_KERNEL
);
1818 usb_unanchor_urb(urb
);
1819 dev_dbg_f(zd_usb_dev(usb
),
1820 "error in usb_submit_urb(). Error number %d\n", r
);
1824 /* fall-through with r == 0 */
1830 void zd_usb_iowrite16v_async_start(struct zd_usb
*usb
)
1832 ZD_ASSERT(usb_anchor_empty(&usb
->submitted_cmds
));
1833 ZD_ASSERT(usb
->urb_async_waiting
== NULL
);
1834 ZD_ASSERT(!usb
->in_async
);
1836 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1840 usb
->urb_async_waiting
= NULL
;
1843 int zd_usb_iowrite16v_async_end(struct zd_usb
*usb
, unsigned int timeout
)
1847 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1848 ZD_ASSERT(usb
->in_async
);
1850 /* Submit last iowrite16v URB */
1851 r
= zd_submit_waiting_urb(usb
, true);
1853 dev_dbg_f(zd_usb_dev(usb
),
1854 "error in zd_submit_waiting_usb(). "
1855 "Error number %d\n", r
);
1857 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1862 timeout
= usb_wait_anchor_empty_timeout(&usb
->submitted_cmds
,
1865 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1866 if (usb
->cmd_error
== -ENOENT
) {
1867 dev_dbg_f(zd_usb_dev(usb
), "timed out");
1879 int zd_usb_iowrite16v_async(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1883 struct usb_device
*udev
;
1884 struct usb_req_write_regs
*req
= NULL
;
1887 struct usb_host_endpoint
*ep
;
1889 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1890 ZD_ASSERT(usb
->in_async
);
1894 if (count
> USB_MAX_IOWRITE16_COUNT
) {
1895 dev_dbg_f(zd_usb_dev(usb
),
1896 "error: count %u exceeds possible max %u\n",
1897 count
, USB_MAX_IOWRITE16_COUNT
);
1901 dev_dbg_f(zd_usb_dev(usb
),
1902 "error: io in atomic context not supported\n");
1903 return -EWOULDBLOCK
;
1906 udev
= zd_usb_to_usbdev(usb
);
1908 ep
= usb_pipe_endpoint(udev
, usb_sndintpipe(udev
, EP_REGS_OUT
));
1912 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1916 req_len
= sizeof(struct usb_req_write_regs
) +
1917 count
* sizeof(struct reg_data
);
1918 req
= kmalloc(req_len
, GFP_KERNEL
);
1924 req
->id
= cpu_to_le16(USB_REQ_WRITE_REGS
);
1925 for (i
= 0; i
< count
; i
++) {
1926 struct reg_data
*rw
= &req
->reg_writes
[i
];
1927 rw
->addr
= cpu_to_le16((u16
)ioreqs
[i
].addr
);
1928 rw
->value
= cpu_to_le16(ioreqs
[i
].value
);
1931 /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
1932 * endpoint is bulk. Select correct type URB by endpoint descriptor.
1934 if (usb_endpoint_xfer_int(&ep
->desc
))
1935 usb_fill_int_urb(urb
, udev
, usb_sndintpipe(udev
, EP_REGS_OUT
),
1936 req
, req_len
, iowrite16v_urb_complete
, usb
,
1937 ep
->desc
.bInterval
);
1939 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_REGS_OUT
),
1940 req
, req_len
, iowrite16v_urb_complete
, usb
);
1942 urb
->transfer_flags
|= URB_FREE_BUFFER
;
1944 /* Submit previous URB */
1945 r
= zd_submit_waiting_urb(usb
, false);
1947 dev_dbg_f(zd_usb_dev(usb
),
1948 "error in zd_submit_waiting_usb(). "
1949 "Error number %d\n", r
);
1953 /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1954 * of currect batch except for very last.
1956 usb
->urb_async_waiting
= urb
;
1963 int zd_usb_iowrite16v(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1968 zd_usb_iowrite16v_async_start(usb
);
1969 r
= zd_usb_iowrite16v_async(usb
, ioreqs
, count
);
1971 zd_usb_iowrite16v_async_end(usb
, 0);
1974 return zd_usb_iowrite16v_async_end(usb
, 50 /* ms */);
1977 int zd_usb_rfwrite(struct zd_usb
*usb
, u32 value
, u8 bits
)
1980 struct usb_device
*udev
;
1981 struct usb_req_rfwrite
*req
= NULL
;
1982 int i
, req_len
, actual_req_len
;
1983 u16 bit_value_template
;
1986 dev_dbg_f(zd_usb_dev(usb
),
1987 "error: io in atomic context not supported\n");
1988 return -EWOULDBLOCK
;
1990 if (bits
< USB_MIN_RFWRITE_BIT_COUNT
) {
1991 dev_dbg_f(zd_usb_dev(usb
),
1992 "error: bits %d are smaller than"
1993 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1994 bits
, USB_MIN_RFWRITE_BIT_COUNT
);
1997 if (bits
> USB_MAX_RFWRITE_BIT_COUNT
) {
1998 dev_dbg_f(zd_usb_dev(usb
),
1999 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
2000 bits
, USB_MAX_RFWRITE_BIT_COUNT
);
2004 if (value
& (~0UL << bits
)) {
2005 dev_dbg_f(zd_usb_dev(usb
),
2006 "error: value %#09x has bits >= %d set\n",
2012 dev_dbg_f(zd_usb_dev(usb
), "value %#09x bits %d\n", value
, bits
);
2014 r
= zd_usb_ioread16(usb
, &bit_value_template
, ZD_CR203
);
2016 dev_dbg_f(zd_usb_dev(usb
),
2017 "error %d: Couldn't read ZD_CR203\n", r
);
2020 bit_value_template
&= ~(RF_IF_LE
|RF_CLK
|RF_DATA
);
2022 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
2023 BUILD_BUG_ON(sizeof(struct usb_req_rfwrite
) +
2024 USB_MAX_RFWRITE_BIT_COUNT
* sizeof(__le16
) >
2025 sizeof(usb
->req_buf
));
2026 BUG_ON(sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
) >
2027 sizeof(usb
->req_buf
));
2029 req_len
= sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
);
2030 req
= (void *)usb
->req_buf
;
2032 req
->id
= cpu_to_le16(USB_REQ_WRITE_RF
);
2033 /* 1: 3683a, but not used in ZYDAS driver */
2034 req
->value
= cpu_to_le16(2);
2035 req
->bits
= cpu_to_le16(bits
);
2037 for (i
= 0; i
< bits
; i
++) {
2038 u16 bv
= bit_value_template
;
2039 if (value
& (1 << (bits
-1-i
)))
2041 req
->bit_values
[i
] = cpu_to_le16(bv
);
2044 udev
= zd_usb_to_usbdev(usb
);
2045 r
= zd_ep_regs_out_msg(udev
, req
, req_len
, &actual_req_len
, 50 /*ms*/);
2047 dev_dbg_f(zd_usb_dev(usb
),
2048 "error in zd_ep_regs_out_msg(). Error number %d\n", r
);
2051 if (req_len
!= actual_req_len
) {
2052 dev_dbg_f(zd_usb_dev(usb
), "error in zd_ep_regs_out_msg()"
2053 " req_len %d != actual_req_len %d\n",
2054 req_len
, actual_req_len
);
2059 /* FALL-THROUGH with r == 0 */