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 <net/mac80211.h>
32 #include <asm/unaligned.h>
38 static struct usb_device_id usb_ids
[] = {
40 { USB_DEVICE(0x0105, 0x145f), .driver_info
= DEVICE_ZD1211
},
41 { USB_DEVICE(0x0586, 0x3401), .driver_info
= DEVICE_ZD1211
},
42 { USB_DEVICE(0x0586, 0x3402), .driver_info
= DEVICE_ZD1211
},
43 { USB_DEVICE(0x0586, 0x3407), .driver_info
= DEVICE_ZD1211
},
44 { USB_DEVICE(0x0586, 0x3409), .driver_info
= DEVICE_ZD1211
},
45 { USB_DEVICE(0x079b, 0x004a), .driver_info
= DEVICE_ZD1211
},
46 { USB_DEVICE(0x07b8, 0x6001), .driver_info
= DEVICE_ZD1211
},
47 { USB_DEVICE(0x0ace, 0x1211), .driver_info
= DEVICE_ZD1211
},
48 { USB_DEVICE(0x0ace, 0xa211), .driver_info
= DEVICE_ZD1211
},
49 { USB_DEVICE(0x0b05, 0x170c), .driver_info
= DEVICE_ZD1211
},
50 { USB_DEVICE(0x0b3b, 0x1630), .driver_info
= DEVICE_ZD1211
},
51 { USB_DEVICE(0x0b3b, 0x5630), .driver_info
= DEVICE_ZD1211
},
52 { USB_DEVICE(0x0df6, 0x9071), .driver_info
= DEVICE_ZD1211
},
53 { USB_DEVICE(0x0df6, 0x9075), .driver_info
= DEVICE_ZD1211
},
54 { USB_DEVICE(0x126f, 0xa006), .driver_info
= DEVICE_ZD1211
},
55 { USB_DEVICE(0x129b, 0x1666), .driver_info
= DEVICE_ZD1211
},
56 { USB_DEVICE(0x13b1, 0x001e), .driver_info
= DEVICE_ZD1211
},
57 { USB_DEVICE(0x1435, 0x0711), .driver_info
= DEVICE_ZD1211
},
58 { USB_DEVICE(0x14ea, 0xab10), .driver_info
= DEVICE_ZD1211
},
59 { USB_DEVICE(0x14ea, 0xab13), .driver_info
= DEVICE_ZD1211
},
60 { USB_DEVICE(0x157e, 0x300a), .driver_info
= DEVICE_ZD1211
},
61 { USB_DEVICE(0x157e, 0x300b), .driver_info
= DEVICE_ZD1211
},
62 { USB_DEVICE(0x157e, 0x3204), .driver_info
= DEVICE_ZD1211
},
63 { USB_DEVICE(0x157e, 0x3207), .driver_info
= DEVICE_ZD1211
},
64 { USB_DEVICE(0x1740, 0x2000), .driver_info
= DEVICE_ZD1211
},
65 { USB_DEVICE(0x6891, 0xa727), .driver_info
= DEVICE_ZD1211
},
67 { USB_DEVICE(0x0053, 0x5301), .driver_info
= DEVICE_ZD1211B
},
68 { USB_DEVICE(0x0409, 0x0248), .driver_info
= DEVICE_ZD1211B
},
69 { USB_DEVICE(0x0411, 0x00da), .driver_info
= DEVICE_ZD1211B
},
70 { USB_DEVICE(0x0471, 0x1236), .driver_info
= DEVICE_ZD1211B
},
71 { USB_DEVICE(0x0471, 0x1237), .driver_info
= DEVICE_ZD1211B
},
72 { USB_DEVICE(0x050d, 0x705c), .driver_info
= DEVICE_ZD1211B
},
73 { USB_DEVICE(0x054c, 0x0257), .driver_info
= DEVICE_ZD1211B
},
74 { USB_DEVICE(0x0586, 0x340a), .driver_info
= DEVICE_ZD1211B
},
75 { USB_DEVICE(0x0586, 0x340f), .driver_info
= DEVICE_ZD1211B
},
76 { USB_DEVICE(0x0586, 0x3410), .driver_info
= DEVICE_ZD1211B
},
77 { USB_DEVICE(0x0586, 0x3412), .driver_info
= DEVICE_ZD1211B
},
78 { USB_DEVICE(0x0586, 0x3413), .driver_info
= DEVICE_ZD1211B
},
79 { USB_DEVICE(0x079b, 0x0062), .driver_info
= DEVICE_ZD1211B
},
80 { USB_DEVICE(0x07b8, 0x6001), .driver_info
= DEVICE_ZD1211B
},
81 { USB_DEVICE(0x07fa, 0x1196), .driver_info
= DEVICE_ZD1211B
},
82 { USB_DEVICE(0x083a, 0x4505), .driver_info
= DEVICE_ZD1211B
},
83 { USB_DEVICE(0x083a, 0xe501), .driver_info
= DEVICE_ZD1211B
},
84 { USB_DEVICE(0x083a, 0xe503), .driver_info
= DEVICE_ZD1211B
},
85 { USB_DEVICE(0x083a, 0xe506), .driver_info
= DEVICE_ZD1211B
},
86 { USB_DEVICE(0x0ace, 0x1215), .driver_info
= DEVICE_ZD1211B
},
87 { USB_DEVICE(0x0ace, 0xb215), .driver_info
= DEVICE_ZD1211B
},
88 { USB_DEVICE(0x0b05, 0x171b), .driver_info
= DEVICE_ZD1211B
},
89 { USB_DEVICE(0x0baf, 0x0121), .driver_info
= DEVICE_ZD1211B
},
90 { USB_DEVICE(0x0cde, 0x001a), .driver_info
= DEVICE_ZD1211B
},
91 { USB_DEVICE(0x0df6, 0x0036), .driver_info
= DEVICE_ZD1211B
},
92 { USB_DEVICE(0x129b, 0x1667), .driver_info
= DEVICE_ZD1211B
},
93 { USB_DEVICE(0x13b1, 0x0024), .driver_info
= DEVICE_ZD1211B
},
94 { USB_DEVICE(0x157e, 0x300d), .driver_info
= DEVICE_ZD1211B
},
95 { USB_DEVICE(0x1582, 0x6003), .driver_info
= DEVICE_ZD1211B
},
96 { USB_DEVICE(0x2019, 0x5303), .driver_info
= DEVICE_ZD1211B
},
97 { USB_DEVICE(0x2019, 0xed01), .driver_info
= DEVICE_ZD1211B
},
98 /* "Driverless" devices that need ejecting */
99 { USB_DEVICE(0x0ace, 0x2011), .driver_info
= DEVICE_INSTALLER
},
100 { USB_DEVICE(0x0ace, 0x20ff), .driver_info
= DEVICE_INSTALLER
},
104 MODULE_LICENSE("GPL");
105 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
106 MODULE_AUTHOR("Ulrich Kunitz");
107 MODULE_AUTHOR("Daniel Drake");
108 MODULE_VERSION("1.0");
109 MODULE_DEVICE_TABLE(usb
, usb_ids
);
111 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
112 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
114 /* USB device initialization */
115 static void int_urb_complete(struct urb
*urb
);
117 static int request_fw_file(
118 const struct firmware
**fw
, const char *name
, struct device
*device
)
122 dev_dbg_f(device
, "fw name %s\n", name
);
124 r
= request_firmware(fw
, name
, device
);
127 "Could not load firmware file %s. Error number %d\n",
132 static inline u16
get_bcdDevice(const struct usb_device
*udev
)
134 return le16_to_cpu(udev
->descriptor
.bcdDevice
);
137 enum upload_code_flags
{
141 /* Ensures that MAX_TRANSFER_SIZE is even. */
142 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
144 static int upload_code(struct usb_device
*udev
,
145 const u8
*data
, size_t size
, u16 code_offset
, int flags
)
150 /* USB request blocks need "kmalloced" buffers.
152 p
= kmalloc(MAX_TRANSFER_SIZE
, GFP_KERNEL
);
154 dev_err(&udev
->dev
, "out of memory\n");
161 size_t transfer_size
= size
<= MAX_TRANSFER_SIZE
?
162 size
: MAX_TRANSFER_SIZE
;
164 dev_dbg_f(&udev
->dev
, "transfer size %zu\n", transfer_size
);
166 memcpy(p
, data
, transfer_size
);
167 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
168 USB_REQ_FIRMWARE_DOWNLOAD
,
169 USB_DIR_OUT
| USB_TYPE_VENDOR
,
170 code_offset
, 0, p
, transfer_size
, 1000 /* ms */);
173 "USB control request for firmware upload"
174 " failed. Error number %d\n", r
);
177 transfer_size
= r
& ~1;
179 size
-= transfer_size
;
180 data
+= transfer_size
;
181 code_offset
+= transfer_size
/sizeof(u16
);
184 if (flags
& REBOOT
) {
187 /* Use "DMA-aware" buffer. */
188 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
189 USB_REQ_FIRMWARE_CONFIRM
,
190 USB_DIR_IN
| USB_TYPE_VENDOR
,
191 0, 0, p
, sizeof(ret
), 5000 /* ms */);
192 if (r
!= sizeof(ret
)) {
194 "control request firmeware confirmation failed."
195 " Return value %d\n", r
);
203 "Internal error while downloading."
204 " Firmware confirm return value %#04x\n",
209 dev_dbg_f(&udev
->dev
, "firmware confirm return value %#04x\n",
219 static u16
get_word(const void *data
, u16 offset
)
221 const __le16
*p
= data
;
222 return le16_to_cpu(p
[offset
]);
225 static char *get_fw_name(struct zd_usb
*usb
, char *buffer
, size_t size
,
228 scnprintf(buffer
, size
, "%s%s",
230 FW_ZD1211B_PREFIX
: FW_ZD1211_PREFIX
,
235 static int handle_version_mismatch(struct zd_usb
*usb
,
236 const struct firmware
*ub_fw
)
238 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
239 const struct firmware
*ur_fw
= NULL
;
244 r
= request_fw_file(&ur_fw
,
245 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ur"),
250 r
= upload_code(udev
, ur_fw
->data
, ur_fw
->size
, FW_START
, REBOOT
);
254 offset
= (E2P_BOOT_CODE_OFFSET
* sizeof(u16
));
255 r
= upload_code(udev
, ub_fw
->data
+ offset
, ub_fw
->size
- offset
,
256 E2P_START
+ E2P_BOOT_CODE_OFFSET
, REBOOT
);
258 /* At this point, the vendor driver downloads the whole firmware
259 * image, hacks around with version IDs, and uploads it again,
260 * completely overwriting the boot code. We do not do this here as
261 * it is not required on any tested devices, and it is suspected to
264 release_firmware(ur_fw
);
268 static int upload_firmware(struct zd_usb
*usb
)
273 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
274 const struct firmware
*ub_fw
= NULL
;
275 const struct firmware
*uph_fw
= NULL
;
278 bcdDevice
= get_bcdDevice(udev
);
280 r
= request_fw_file(&ub_fw
,
281 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "ub"),
286 fw_bcdDevice
= get_word(ub_fw
->data
, E2P_DATA_OFFSET
);
288 if (fw_bcdDevice
!= bcdDevice
) {
290 "firmware version %#06x and device bootcode version "
291 "%#06x differ\n", fw_bcdDevice
, bcdDevice
);
292 if (bcdDevice
<= 0x4313)
293 dev_warn(&udev
->dev
, "device has old bootcode, please "
294 "report success or failure\n");
296 r
= handle_version_mismatch(usb
, ub_fw
);
300 dev_dbg_f(&udev
->dev
,
301 "firmware device id %#06x is equal to the "
302 "actual device id\n", fw_bcdDevice
);
306 r
= request_fw_file(&uph_fw
,
307 get_fw_name(usb
, fw_name
, sizeof(fw_name
), "uphr"),
312 r
= upload_code(udev
, uph_fw
->data
, uph_fw
->size
, FW_START
, REBOOT
);
315 "Could not upload firmware code uph. Error number %d\n",
321 release_firmware(ub_fw
);
322 release_firmware(uph_fw
);
326 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ur");
327 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ur");
328 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"ub");
329 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"ub");
330 MODULE_FIRMWARE(FW_ZD1211B_PREFIX
"uphr");
331 MODULE_FIRMWARE(FW_ZD1211_PREFIX
"uphr");
333 /* Read data from device address space using "firmware interface" which does
334 * not require firmware to be loaded. */
335 int zd_usb_read_fw(struct zd_usb
*usb
, zd_addr_t addr
, u8
*data
, u16 len
)
338 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
341 /* Use "DMA-aware" buffer. */
342 buf
= kmalloc(len
, GFP_KERNEL
);
345 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
346 USB_REQ_FIRMWARE_READ_DATA
, USB_DIR_IN
| 0x40, addr
, 0,
350 "read over firmware interface failed: %d\n", r
);
352 } else if (r
!= len
) {
354 "incomplete read over firmware interface: %d/%d\n",
360 memcpy(data
, buf
, len
);
366 #define urb_dev(urb) (&(urb)->dev->dev)
368 static inline void handle_regs_int(struct urb
*urb
)
370 struct zd_usb
*usb
= urb
->context
;
371 struct zd_usb_interrupt
*intr
= &usb
->intr
;
375 ZD_ASSERT(in_interrupt());
376 spin_lock(&intr
->lock
);
378 int_num
= le16_to_cpu(*(__le16
*)(urb
->transfer_buffer
+2));
379 if (int_num
== CR_INTERRUPT
) {
380 struct zd_mac
*mac
= zd_hw_mac(zd_usb_to_hw(urb
->context
));
381 spin_lock(&mac
->lock
);
382 memcpy(&mac
->intr_buffer
, urb
->transfer_buffer
,
383 USB_MAX_EP_INT_BUFFER
);
384 spin_unlock(&mac
->lock
);
385 schedule_work(&mac
->process_intr
);
386 } else if (intr
->read_regs_enabled
) {
387 intr
->read_regs
.length
= len
= urb
->actual_length
;
389 if (len
> sizeof(intr
->read_regs
.buffer
))
390 len
= sizeof(intr
->read_regs
.buffer
);
391 memcpy(intr
->read_regs
.buffer
, urb
->transfer_buffer
, len
);
392 intr
->read_regs_enabled
= 0;
393 complete(&intr
->read_regs
.completion
);
398 spin_unlock(&intr
->lock
);
401 static void int_urb_complete(struct urb
*urb
)
404 struct usb_int_header
*hdr
;
406 switch (urb
->status
) {
415 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
418 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
422 if (urb
->actual_length
< sizeof(hdr
)) {
423 dev_dbg_f(urb_dev(urb
), "error: urb %p to small\n", urb
);
427 hdr
= urb
->transfer_buffer
;
428 if (hdr
->type
!= USB_INT_TYPE
) {
429 dev_dbg_f(urb_dev(urb
), "error: urb %p wrong type\n", urb
);
434 case USB_INT_ID_REGS
:
435 handle_regs_int(urb
);
437 case USB_INT_ID_RETRY_FAILED
:
438 zd_mac_tx_failed(urb
);
441 dev_dbg_f(urb_dev(urb
), "error: urb %p unknown id %x\n", urb
,
442 (unsigned int)hdr
->id
);
447 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
449 dev_dbg_f(urb_dev(urb
), "error: resubmit urb %p err code %d\n",
451 /* TODO: add worker to reset intr->urb */
456 static inline int int_urb_interval(struct usb_device
*udev
)
458 switch (udev
->speed
) {
469 static inline int usb_int_enabled(struct zd_usb
*usb
)
472 struct zd_usb_interrupt
*intr
= &usb
->intr
;
475 spin_lock_irqsave(&intr
->lock
, flags
);
477 spin_unlock_irqrestore(&intr
->lock
, flags
);
481 int zd_usb_enable_int(struct zd_usb
*usb
)
484 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
485 struct zd_usb_interrupt
*intr
= &usb
->intr
;
488 dev_dbg_f(zd_usb_dev(usb
), "\n");
490 urb
= usb_alloc_urb(0, GFP_KERNEL
);
496 ZD_ASSERT(!irqs_disabled());
497 spin_lock_irq(&intr
->lock
);
499 spin_unlock_irq(&intr
->lock
);
504 spin_unlock_irq(&intr
->lock
);
507 intr
->buffer
= usb_alloc_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
508 GFP_KERNEL
, &intr
->buffer_dma
);
510 dev_dbg_f(zd_usb_dev(usb
),
511 "couldn't allocate transfer_buffer\n");
512 goto error_set_urb_null
;
515 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, EP_INT_IN
),
516 intr
->buffer
, USB_MAX_EP_INT_BUFFER
,
517 int_urb_complete
, usb
,
519 urb
->transfer_dma
= intr
->buffer_dma
;
520 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
522 dev_dbg_f(zd_usb_dev(usb
), "submit urb %p\n", intr
->urb
);
523 r
= usb_submit_urb(urb
, GFP_KERNEL
);
525 dev_dbg_f(zd_usb_dev(usb
),
526 "Couldn't submit urb. Error number %d\n", r
);
532 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
533 intr
->buffer
, intr
->buffer_dma
);
535 spin_lock_irq(&intr
->lock
);
537 spin_unlock_irq(&intr
->lock
);
544 void zd_usb_disable_int(struct zd_usb
*usb
)
547 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
548 struct zd_usb_interrupt
*intr
= &usb
->intr
;
551 dma_addr_t buffer_dma
;
553 spin_lock_irqsave(&intr
->lock
, flags
);
556 spin_unlock_irqrestore(&intr
->lock
, flags
);
560 buffer
= intr
->buffer
;
561 buffer_dma
= intr
->buffer_dma
;
563 spin_unlock_irqrestore(&intr
->lock
, flags
);
566 dev_dbg_f(zd_usb_dev(usb
), "urb %p killed\n", urb
);
570 usb_free_coherent(udev
, USB_MAX_EP_INT_BUFFER
,
574 static void handle_rx_packet(struct zd_usb
*usb
, const u8
*buffer
,
578 const struct rx_length_info
*length_info
;
580 if (length
< sizeof(struct rx_length_info
)) {
581 /* It's not a complete packet anyhow. */
582 printk("%s: invalid, small RX packet : %d\n",
586 length_info
= (struct rx_length_info
*)
587 (buffer
+ length
- sizeof(struct rx_length_info
));
589 /* It might be that three frames are merged into a single URB
590 * transaction. We have to check for the length info tag.
592 * While testing we discovered that length_info might be unaligned,
593 * because if USB transactions are merged, the last packet will not
594 * be padded. Unaligned access might also happen if the length_info
595 * structure is not present.
597 if (get_unaligned_le16(&length_info
->tag
) == RX_LENGTH_INFO_TAG
)
599 unsigned int l
, k
, n
;
600 for (i
= 0, l
= 0;; i
++) {
601 k
= get_unaligned_le16(&length_info
->length
[i
]);
607 zd_mac_rx(zd_usb_to_hw(usb
), buffer
+l
, k
);
613 zd_mac_rx(zd_usb_to_hw(usb
), buffer
, length
);
617 static void rx_urb_complete(struct urb
*urb
)
621 struct zd_usb_rx
*rx
;
625 switch (urb
->status
) {
634 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
637 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
641 buffer
= urb
->transfer_buffer
;
642 length
= urb
->actual_length
;
646 tasklet_schedule(&rx
->reset_timer_tasklet
);
648 if (length
%rx
->usb_packet_size
> rx
->usb_packet_size
-4) {
649 /* If there is an old first fragment, we don't care. */
650 dev_dbg_f(urb_dev(urb
), "*** first fragment ***\n");
651 ZD_ASSERT(length
<= ARRAY_SIZE(rx
->fragment
));
652 spin_lock(&rx
->lock
);
653 memcpy(rx
->fragment
, buffer
, length
);
654 rx
->fragment_length
= length
;
655 spin_unlock(&rx
->lock
);
659 spin_lock(&rx
->lock
);
660 if (rx
->fragment_length
> 0) {
661 /* We are on a second fragment, we believe */
662 ZD_ASSERT(length
+ rx
->fragment_length
<=
663 ARRAY_SIZE(rx
->fragment
));
664 dev_dbg_f(urb_dev(urb
), "*** second fragment ***\n");
665 memcpy(rx
->fragment
+rx
->fragment_length
, buffer
, length
);
666 handle_rx_packet(usb
, rx
->fragment
,
667 rx
->fragment_length
+ length
);
668 rx
->fragment_length
= 0;
669 spin_unlock(&rx
->lock
);
671 spin_unlock(&rx
->lock
);
672 handle_rx_packet(usb
, buffer
, length
);
676 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
678 dev_dbg_f(urb_dev(urb
), "urb %p resubmit error %d\n", urb
, r
);
681 static struct urb
*alloc_rx_urb(struct zd_usb
*usb
)
683 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
687 urb
= usb_alloc_urb(0, GFP_KERNEL
);
690 buffer
= usb_alloc_coherent(udev
, USB_MAX_RX_SIZE
, GFP_KERNEL
,
697 usb_fill_bulk_urb(urb
, udev
, usb_rcvbulkpipe(udev
, EP_DATA_IN
),
698 buffer
, USB_MAX_RX_SIZE
,
699 rx_urb_complete
, usb
);
700 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
705 static void free_rx_urb(struct urb
*urb
)
709 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
710 urb
->transfer_buffer
, urb
->transfer_dma
);
714 static int __zd_usb_enable_rx(struct zd_usb
*usb
)
717 struct zd_usb_rx
*rx
= &usb
->rx
;
720 dev_dbg_f(zd_usb_dev(usb
), "\n");
723 urbs
= kcalloc(RX_URBS_COUNT
, sizeof(struct urb
*), GFP_KERNEL
);
726 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
727 urbs
[i
] = alloc_rx_urb(usb
);
732 ZD_ASSERT(!irqs_disabled());
733 spin_lock_irq(&rx
->lock
);
735 spin_unlock_irq(&rx
->lock
);
740 rx
->urbs_count
= RX_URBS_COUNT
;
741 spin_unlock_irq(&rx
->lock
);
743 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
744 r
= usb_submit_urb(urbs
[i
], GFP_KERNEL
);
751 for (i
= 0; i
< RX_URBS_COUNT
; i
++) {
752 usb_kill_urb(urbs
[i
]);
754 spin_lock_irq(&rx
->lock
);
757 spin_unlock_irq(&rx
->lock
);
760 for (i
= 0; i
< RX_URBS_COUNT
; i
++)
761 free_rx_urb(urbs
[i
]);
766 int zd_usb_enable_rx(struct zd_usb
*usb
)
769 struct zd_usb_rx
*rx
= &usb
->rx
;
771 mutex_lock(&rx
->setup_mutex
);
772 r
= __zd_usb_enable_rx(usb
);
773 mutex_unlock(&rx
->setup_mutex
);
775 zd_usb_reset_rx_idle_timer(usb
);
780 static void __zd_usb_disable_rx(struct zd_usb
*usb
)
786 struct zd_usb_rx
*rx
= &usb
->rx
;
788 spin_lock_irqsave(&rx
->lock
, flags
);
790 count
= rx
->urbs_count
;
791 spin_unlock_irqrestore(&rx
->lock
, flags
);
795 for (i
= 0; i
< count
; i
++) {
796 usb_kill_urb(urbs
[i
]);
797 free_rx_urb(urbs
[i
]);
801 spin_lock_irqsave(&rx
->lock
, flags
);
804 spin_unlock_irqrestore(&rx
->lock
, flags
);
807 void zd_usb_disable_rx(struct zd_usb
*usb
)
809 struct zd_usb_rx
*rx
= &usb
->rx
;
811 mutex_lock(&rx
->setup_mutex
);
812 __zd_usb_disable_rx(usb
);
813 mutex_unlock(&rx
->setup_mutex
);
815 tasklet_kill(&rx
->reset_timer_tasklet
);
816 cancel_delayed_work_sync(&rx
->idle_work
);
819 static void zd_usb_reset_rx(struct zd_usb
*usb
)
822 struct zd_usb_rx
*rx
= &usb
->rx
;
825 mutex_lock(&rx
->setup_mutex
);
827 spin_lock_irqsave(&rx
->lock
, flags
);
828 do_reset
= rx
->urbs
!= NULL
;
829 spin_unlock_irqrestore(&rx
->lock
, flags
);
832 __zd_usb_disable_rx(usb
);
833 __zd_usb_enable_rx(usb
);
836 mutex_unlock(&rx
->setup_mutex
);
839 zd_usb_reset_rx_idle_timer(usb
);
843 * zd_usb_disable_tx - disable transmission
844 * @usb: the zd1211rw-private USB structure
846 * Frees all URBs in the free list and marks the transmission as disabled.
848 void zd_usb_disable_tx(struct zd_usb
*usb
)
850 struct zd_usb_tx
*tx
= &usb
->tx
;
853 atomic_set(&tx
->enabled
, 0);
855 /* kill all submitted tx-urbs */
856 usb_kill_anchored_urbs(&tx
->submitted
);
858 spin_lock_irqsave(&tx
->lock
, flags
);
859 WARN_ON(!skb_queue_empty(&tx
->submitted_skbs
));
860 WARN_ON(tx
->submitted_urbs
!= 0);
861 tx
->submitted_urbs
= 0;
862 spin_unlock_irqrestore(&tx
->lock
, flags
);
864 /* The stopped state is ignored, relying on ieee80211_wake_queues()
865 * in a potentionally following zd_usb_enable_tx().
870 * zd_usb_enable_tx - enables transmission
871 * @usb: a &struct zd_usb pointer
873 * This function enables transmission and prepares the &zd_usb_tx data
876 void zd_usb_enable_tx(struct zd_usb
*usb
)
879 struct zd_usb_tx
*tx
= &usb
->tx
;
881 spin_lock_irqsave(&tx
->lock
, flags
);
882 atomic_set(&tx
->enabled
, 1);
883 tx
->submitted_urbs
= 0;
884 ieee80211_wake_queues(zd_usb_to_hw(usb
));
886 spin_unlock_irqrestore(&tx
->lock
, flags
);
889 static void tx_dec_submitted_urbs(struct zd_usb
*usb
)
891 struct zd_usb_tx
*tx
= &usb
->tx
;
894 spin_lock_irqsave(&tx
->lock
, flags
);
895 --tx
->submitted_urbs
;
896 if (tx
->stopped
&& tx
->submitted_urbs
<= ZD_USB_TX_LOW
) {
897 ieee80211_wake_queues(zd_usb_to_hw(usb
));
900 spin_unlock_irqrestore(&tx
->lock
, flags
);
903 static void tx_inc_submitted_urbs(struct zd_usb
*usb
)
905 struct zd_usb_tx
*tx
= &usb
->tx
;
908 spin_lock_irqsave(&tx
->lock
, flags
);
909 ++tx
->submitted_urbs
;
910 if (!tx
->stopped
&& tx
->submitted_urbs
> ZD_USB_TX_HIGH
) {
911 ieee80211_stop_queues(zd_usb_to_hw(usb
));
914 spin_unlock_irqrestore(&tx
->lock
, flags
);
918 * tx_urb_complete - completes the execution of an URB
921 * This function is called if the URB has been transferred to a device or an
922 * error has happened.
924 static void tx_urb_complete(struct urb
*urb
)
928 struct ieee80211_tx_info
*info
;
930 struct zd_usb_tx
*tx
;
932 skb
= (struct sk_buff
*)urb
->context
;
933 info
= IEEE80211_SKB_CB(skb
);
935 * grab 'usb' pointer before handing off the skb (since
936 * it might be freed by zd_mac_tx_to_dev or mac80211)
938 usb
= &zd_hw_mac(info
->rate_driver_data
[0])->chip
.usb
;
941 switch (urb
->status
) {
950 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
953 dev_dbg_f(urb_dev(urb
), "urb %p error %d\n", urb
, urb
->status
);
957 skb_unlink(skb
, &usb
->tx
.submitted_skbs
);
958 zd_mac_tx_to_dev(skb
, urb
->status
);
960 tx_dec_submitted_urbs(usb
);
963 usb_anchor_urb(urb
, &tx
->submitted
);
964 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
966 usb_unanchor_urb(urb
);
967 dev_dbg_f(urb_dev(urb
), "error resubmit urb %p %d\n", urb
, r
);
973 * zd_usb_tx: initiates transfer of a frame of the device
975 * @usb: the zd1211rw-private USB structure
976 * @skb: a &struct sk_buff pointer
978 * This function tranmits a frame to the device. It doesn't wait for
979 * completion. The frame must contain the control set and have all the
980 * control set information available.
982 * The function returns 0 if the transfer has been successfully initiated.
984 int zd_usb_tx(struct zd_usb
*usb
, struct sk_buff
*skb
)
987 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
988 struct usb_device
*udev
= zd_usb_to_usbdev(usb
);
990 struct zd_usb_tx
*tx
= &usb
->tx
;
992 if (!atomic_read(&tx
->enabled
)) {
997 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1003 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, EP_DATA_OUT
),
1004 skb
->data
, skb
->len
, tx_urb_complete
, skb
);
1006 info
->rate_driver_data
[1] = (void *)jiffies
;
1007 skb_queue_tail(&tx
->submitted_skbs
, skb
);
1008 usb_anchor_urb(urb
, &tx
->submitted
);
1010 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
1012 dev_dbg_f(zd_usb_dev(usb
), "error submit urb %p %d\n", urb
, r
);
1013 usb_unanchor_urb(urb
);
1014 skb_unlink(skb
, &tx
->submitted_skbs
);
1017 tx_inc_submitted_urbs(usb
);
1025 static bool zd_tx_timeout(struct zd_usb
*usb
)
1027 struct zd_usb_tx
*tx
= &usb
->tx
;
1028 struct sk_buff_head
*q
= &tx
->submitted_skbs
;
1029 struct sk_buff
*skb
, *skbnext
;
1030 struct ieee80211_tx_info
*info
;
1031 unsigned long flags
, trans_start
;
1032 bool have_timedout
= false;
1034 spin_lock_irqsave(&q
->lock
, flags
);
1035 skb_queue_walk_safe(q
, skb
, skbnext
) {
1036 info
= IEEE80211_SKB_CB(skb
);
1037 trans_start
= (unsigned long)info
->rate_driver_data
[1];
1039 if (time_is_before_jiffies(trans_start
+ ZD_TX_TIMEOUT
)) {
1040 have_timedout
= true;
1044 spin_unlock_irqrestore(&q
->lock
, flags
);
1046 return have_timedout
;
1049 static void zd_tx_watchdog_handler(struct work_struct
*work
)
1051 struct zd_usb
*usb
=
1052 container_of(work
, struct zd_usb
, tx
.watchdog_work
.work
);
1053 struct zd_usb_tx
*tx
= &usb
->tx
;
1055 if (!atomic_read(&tx
->enabled
) || !tx
->watchdog_enabled
)
1057 if (!zd_tx_timeout(usb
))
1060 /* TX halted, try reset */
1061 dev_warn(zd_usb_dev(usb
), "TX-stall detected, reseting device...");
1063 usb_queue_reset_device(usb
->intf
);
1065 /* reset will stop this worker, don't rearm */
1068 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1069 ZD_TX_WATCHDOG_INTERVAL
);
1072 void zd_tx_watchdog_enable(struct zd_usb
*usb
)
1074 struct zd_usb_tx
*tx
= &usb
->tx
;
1076 if (!tx
->watchdog_enabled
) {
1077 dev_dbg_f(zd_usb_dev(usb
), "\n");
1078 queue_delayed_work(zd_workqueue
, &tx
->watchdog_work
,
1079 ZD_TX_WATCHDOG_INTERVAL
);
1080 tx
->watchdog_enabled
= 1;
1084 void zd_tx_watchdog_disable(struct zd_usb
*usb
)
1086 struct zd_usb_tx
*tx
= &usb
->tx
;
1088 if (tx
->watchdog_enabled
) {
1089 dev_dbg_f(zd_usb_dev(usb
), "\n");
1090 tx
->watchdog_enabled
= 0;
1091 cancel_delayed_work_sync(&tx
->watchdog_work
);
1095 static void zd_rx_idle_timer_handler(struct work_struct
*work
)
1097 struct zd_usb
*usb
=
1098 container_of(work
, struct zd_usb
, rx
.idle_work
.work
);
1099 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1101 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1104 dev_dbg_f(zd_usb_dev(usb
), "\n");
1106 /* 30 seconds since last rx, reset rx */
1107 zd_usb_reset_rx(usb
);
1110 static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param
)
1112 struct zd_usb
*usb
= (struct zd_usb
*)param
;
1114 zd_usb_reset_rx_idle_timer(usb
);
1117 void zd_usb_reset_rx_idle_timer(struct zd_usb
*usb
)
1119 struct zd_usb_rx
*rx
= &usb
->rx
;
1121 cancel_delayed_work(&rx
->idle_work
);
1122 queue_delayed_work(zd_workqueue
, &rx
->idle_work
, ZD_RX_IDLE_INTERVAL
);
1125 static inline void init_usb_interrupt(struct zd_usb
*usb
)
1127 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1129 spin_lock_init(&intr
->lock
);
1130 intr
->interval
= int_urb_interval(zd_usb_to_usbdev(usb
));
1131 init_completion(&intr
->read_regs
.completion
);
1132 intr
->read_regs
.cr_int_addr
= cpu_to_le16((u16
)CR_INTERRUPT
);
1135 static inline void init_usb_rx(struct zd_usb
*usb
)
1137 struct zd_usb_rx
*rx
= &usb
->rx
;
1139 spin_lock_init(&rx
->lock
);
1140 mutex_init(&rx
->setup_mutex
);
1141 if (interface_to_usbdev(usb
->intf
)->speed
== USB_SPEED_HIGH
) {
1142 rx
->usb_packet_size
= 512;
1144 rx
->usb_packet_size
= 64;
1146 ZD_ASSERT(rx
->fragment_length
== 0);
1147 INIT_DELAYED_WORK(&rx
->idle_work
, zd_rx_idle_timer_handler
);
1148 rx
->reset_timer_tasklet
.func
= zd_usb_reset_rx_idle_timer_tasklet
;
1149 rx
->reset_timer_tasklet
.data
= (unsigned long)usb
;
1152 static inline void init_usb_tx(struct zd_usb
*usb
)
1154 struct zd_usb_tx
*tx
= &usb
->tx
;
1156 spin_lock_init(&tx
->lock
);
1157 atomic_set(&tx
->enabled
, 0);
1159 skb_queue_head_init(&tx
->submitted_skbs
);
1160 init_usb_anchor(&tx
->submitted
);
1161 tx
->submitted_urbs
= 0;
1162 tx
->watchdog_enabled
= 0;
1163 INIT_DELAYED_WORK(&tx
->watchdog_work
, zd_tx_watchdog_handler
);
1166 void zd_usb_init(struct zd_usb
*usb
, struct ieee80211_hw
*hw
,
1167 struct usb_interface
*intf
)
1169 memset(usb
, 0, sizeof(*usb
));
1170 usb
->intf
= usb_get_intf(intf
);
1171 usb_set_intfdata(usb
->intf
, hw
);
1172 init_usb_anchor(&usb
->submitted_cmds
);
1173 init_usb_interrupt(usb
);
1178 void zd_usb_clear(struct zd_usb
*usb
)
1180 usb_set_intfdata(usb
->intf
, NULL
);
1181 usb_put_intf(usb
->intf
);
1182 ZD_MEMCLEAR(usb
, sizeof(*usb
));
1183 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
1186 static const char *speed(enum usb_device_speed speed
)
1191 case USB_SPEED_FULL
:
1193 case USB_SPEED_HIGH
:
1196 return "unknown speed";
1200 static int scnprint_id(struct usb_device
*udev
, char *buffer
, size_t size
)
1202 return scnprintf(buffer
, size
, "%04hx:%04hx v%04hx %s",
1203 le16_to_cpu(udev
->descriptor
.idVendor
),
1204 le16_to_cpu(udev
->descriptor
.idProduct
),
1205 get_bcdDevice(udev
),
1206 speed(udev
->speed
));
1209 int zd_usb_scnprint_id(struct zd_usb
*usb
, char *buffer
, size_t size
)
1211 struct usb_device
*udev
= interface_to_usbdev(usb
->intf
);
1212 return scnprint_id(udev
, buffer
, size
);
1216 static void print_id(struct usb_device
*udev
)
1220 scnprint_id(udev
, buffer
, sizeof(buffer
));
1221 buffer
[sizeof(buffer
)-1] = 0;
1222 dev_dbg_f(&udev
->dev
, "%s\n", buffer
);
1225 #define print_id(udev) do { } while (0)
1228 static int eject_installer(struct usb_interface
*intf
)
1230 struct usb_device
*udev
= interface_to_usbdev(intf
);
1231 struct usb_host_interface
*iface_desc
= &intf
->altsetting
[0];
1232 struct usb_endpoint_descriptor
*endpoint
;
1237 /* Find bulk out endpoint */
1238 for (r
= 1; r
>= 0; r
--) {
1239 endpoint
= &iface_desc
->endpoint
[r
].desc
;
1240 if (usb_endpoint_dir_out(endpoint
) &&
1241 usb_endpoint_xfer_bulk(endpoint
)) {
1242 bulk_out_ep
= endpoint
->bEndpointAddress
;
1248 "zd1211rw: Could not find bulk out endpoint\n");
1252 cmd
= kzalloc(31, GFP_KERNEL
);
1256 /* USB bulk command block */
1257 cmd
[0] = 0x55; /* bulk command signature */
1258 cmd
[1] = 0x53; /* bulk command signature */
1259 cmd
[2] = 0x42; /* bulk command signature */
1260 cmd
[3] = 0x43; /* bulk command signature */
1261 cmd
[14] = 6; /* command length */
1263 cmd
[15] = 0x1b; /* SCSI command: START STOP UNIT */
1264 cmd
[19] = 0x2; /* eject disc */
1266 dev_info(&udev
->dev
, "Ejecting virtual installer media...\n");
1267 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, bulk_out_ep
),
1268 cmd
, 31, NULL
, 2000);
1273 /* At this point, the device disconnects and reconnects with the real
1276 usb_set_intfdata(intf
, NULL
);
1280 int zd_usb_init_hw(struct zd_usb
*usb
)
1283 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1285 dev_dbg_f(zd_usb_dev(usb
), "\n");
1287 r
= upload_firmware(usb
);
1289 dev_err(zd_usb_dev(usb
),
1290 "couldn't load firmware. Error number %d\n", r
);
1294 r
= usb_reset_configuration(zd_usb_to_usbdev(usb
));
1296 dev_dbg_f(zd_usb_dev(usb
),
1297 "couldn't reset configuration. Error number %d\n", r
);
1301 r
= zd_mac_init_hw(mac
->hw
);
1303 dev_dbg_f(zd_usb_dev(usb
),
1304 "couldn't initialize mac. Error number %d\n", r
);
1308 usb
->initialized
= 1;
1312 static int probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1315 struct usb_device
*udev
= interface_to_usbdev(intf
);
1317 struct ieee80211_hw
*hw
= NULL
;
1321 if (id
->driver_info
& DEVICE_INSTALLER
)
1322 return eject_installer(intf
);
1324 switch (udev
->speed
) {
1326 case USB_SPEED_FULL
:
1327 case USB_SPEED_HIGH
:
1330 dev_dbg_f(&intf
->dev
, "Unknown USB speed\n");
1335 r
= usb_reset_device(udev
);
1338 "couldn't reset usb device. Error number %d\n", r
);
1342 hw
= zd_mac_alloc_hw(intf
);
1348 usb
= &zd_hw_mac(hw
)->chip
.usb
;
1349 usb
->is_zd1211b
= (id
->driver_info
== DEVICE_ZD1211B
) != 0;
1351 r
= zd_mac_preinit_hw(hw
);
1353 dev_dbg_f(&intf
->dev
,
1354 "couldn't initialize mac. Error number %d\n", r
);
1358 r
= ieee80211_register_hw(hw
);
1360 dev_dbg_f(&intf
->dev
,
1361 "couldn't register device. Error number %d\n", r
);
1365 dev_dbg_f(&intf
->dev
, "successful\n");
1366 dev_info(&intf
->dev
, "%s\n", wiphy_name(hw
->wiphy
));
1369 usb_reset_device(interface_to_usbdev(intf
));
1371 zd_mac_clear(zd_hw_mac(hw
));
1372 ieee80211_free_hw(hw
);
1377 static void disconnect(struct usb_interface
*intf
)
1379 struct ieee80211_hw
*hw
= zd_intf_to_hw(intf
);
1383 /* Either something really bad happened, or we're just dealing with
1384 * a DEVICE_INSTALLER. */
1388 mac
= zd_hw_mac(hw
);
1389 usb
= &mac
->chip
.usb
;
1391 dev_dbg_f(zd_usb_dev(usb
), "\n");
1393 ieee80211_unregister_hw(hw
);
1395 /* Just in case something has gone wrong! */
1396 zd_usb_disable_tx(usb
);
1397 zd_usb_disable_rx(usb
);
1398 zd_usb_disable_int(usb
);
1400 /* If the disconnect has been caused by a removal of the
1401 * driver module, the reset allows reloading of the driver. If the
1402 * reset will not be executed here, the upload of the firmware in the
1403 * probe function caused by the reloading of the driver will fail.
1405 usb_reset_device(interface_to_usbdev(intf
));
1408 ieee80211_free_hw(hw
);
1409 dev_dbg(&intf
->dev
, "disconnected\n");
1412 static void zd_usb_resume(struct zd_usb
*usb
)
1414 struct zd_mac
*mac
= zd_usb_to_mac(usb
);
1417 dev_dbg_f(zd_usb_dev(usb
), "\n");
1419 r
= zd_op_start(zd_usb_to_hw(usb
));
1421 dev_warn(zd_usb_dev(usb
), "Device resume failed "
1422 "with error code %d. Retrying...\n", r
);
1423 if (usb
->was_running
)
1424 set_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1425 usb_queue_reset_device(usb
->intf
);
1429 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
) {
1430 r
= zd_restore_settings(mac
);
1432 dev_dbg(zd_usb_dev(usb
),
1433 "failed to restore settings, %d\n", r
);
1439 static void zd_usb_stop(struct zd_usb
*usb
)
1441 dev_dbg_f(zd_usb_dev(usb
), "\n");
1443 zd_op_stop(zd_usb_to_hw(usb
));
1445 zd_usb_disable_tx(usb
);
1446 zd_usb_disable_rx(usb
);
1447 zd_usb_disable_int(usb
);
1449 usb
->initialized
= 0;
1452 static int pre_reset(struct usb_interface
*intf
)
1454 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1458 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1461 mac
= zd_hw_mac(hw
);
1462 usb
= &mac
->chip
.usb
;
1464 usb
->was_running
= test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
1468 mutex_lock(&mac
->chip
.mutex
);
1472 static int post_reset(struct usb_interface
*intf
)
1474 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1478 if (!hw
|| intf
->condition
!= USB_INTERFACE_BOUND
)
1481 mac
= zd_hw_mac(hw
);
1482 usb
= &mac
->chip
.usb
;
1484 mutex_unlock(&mac
->chip
.mutex
);
1486 if (usb
->was_running
)
1491 static struct usb_driver driver
= {
1492 .name
= KBUILD_MODNAME
,
1493 .id_table
= usb_ids
,
1495 .disconnect
= disconnect
,
1496 .pre_reset
= pre_reset
,
1497 .post_reset
= post_reset
,
1500 struct workqueue_struct
*zd_workqueue
;
1502 static int __init
usb_init(void)
1506 pr_debug("%s usb_init()\n", driver
.name
);
1508 zd_workqueue
= create_singlethread_workqueue(driver
.name
);
1509 if (zd_workqueue
== NULL
) {
1510 printk(KERN_ERR
"%s couldn't create workqueue\n", driver
.name
);
1514 r
= usb_register(&driver
);
1516 destroy_workqueue(zd_workqueue
);
1517 printk(KERN_ERR
"%s usb_register() failed. Error number %d\n",
1522 pr_debug("%s initialized\n", driver
.name
);
1526 static void __exit
usb_exit(void)
1528 pr_debug("%s usb_exit()\n", driver
.name
);
1529 usb_deregister(&driver
);
1530 destroy_workqueue(zd_workqueue
);
1533 module_init(usb_init
);
1534 module_exit(usb_exit
);
1536 static int usb_int_regs_length(unsigned int count
)
1538 return sizeof(struct usb_int_regs
) + count
* sizeof(struct reg_data
);
1541 static void prepare_read_regs_int(struct zd_usb
*usb
)
1543 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1545 spin_lock_irq(&intr
->lock
);
1546 intr
->read_regs_enabled
= 1;
1547 INIT_COMPLETION(intr
->read_regs
.completion
);
1548 spin_unlock_irq(&intr
->lock
);
1551 static void disable_read_regs_int(struct zd_usb
*usb
)
1553 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1555 spin_lock_irq(&intr
->lock
);
1556 intr
->read_regs_enabled
= 0;
1557 spin_unlock_irq(&intr
->lock
);
1560 static int get_results(struct zd_usb
*usb
, u16
*values
,
1561 struct usb_req_read_regs
*req
, unsigned int count
)
1565 struct zd_usb_interrupt
*intr
= &usb
->intr
;
1566 struct read_regs_int
*rr
= &intr
->read_regs
;
1567 struct usb_int_regs
*regs
= (struct usb_int_regs
*)rr
->buffer
;
1569 spin_lock_irq(&intr
->lock
);
1572 /* The created block size seems to be larger than expected.
1573 * However results appear to be correct.
1575 if (rr
->length
< usb_int_regs_length(count
)) {
1576 dev_dbg_f(zd_usb_dev(usb
),
1577 "error: actual length %d less than expected %d\n",
1578 rr
->length
, usb_int_regs_length(count
));
1581 if (rr
->length
> sizeof(rr
->buffer
)) {
1582 dev_dbg_f(zd_usb_dev(usb
),
1583 "error: actual length %d exceeds buffer size %zu\n",
1584 rr
->length
, sizeof(rr
->buffer
));
1588 for (i
= 0; i
< count
; i
++) {
1589 struct reg_data
*rd
= ®s
->regs
[i
];
1590 if (rd
->addr
!= req
->addr
[i
]) {
1591 dev_dbg_f(zd_usb_dev(usb
),
1592 "rd[%d] addr %#06hx expected %#06hx\n", i
,
1593 le16_to_cpu(rd
->addr
),
1594 le16_to_cpu(req
->addr
[i
]));
1597 values
[i
] = le16_to_cpu(rd
->value
);
1602 spin_unlock_irq(&intr
->lock
);
1606 int zd_usb_ioread16v(struct zd_usb
*usb
, u16
*values
,
1607 const zd_addr_t
*addresses
, unsigned int count
)
1610 int i
, req_len
, actual_req_len
;
1611 struct usb_device
*udev
;
1612 struct usb_req_read_regs
*req
= NULL
;
1613 unsigned long timeout
;
1616 dev_dbg_f(zd_usb_dev(usb
), "error: count is zero\n");
1619 if (count
> USB_MAX_IOREAD16_COUNT
) {
1620 dev_dbg_f(zd_usb_dev(usb
),
1621 "error: count %u exceeds possible max %u\n",
1622 count
, USB_MAX_IOREAD16_COUNT
);
1626 dev_dbg_f(zd_usb_dev(usb
),
1627 "error: io in atomic context not supported\n");
1628 return -EWOULDBLOCK
;
1630 if (!usb_int_enabled(usb
)) {
1631 dev_dbg_f(zd_usb_dev(usb
),
1632 "error: usb interrupt not enabled\n");
1633 return -EWOULDBLOCK
;
1636 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1637 BUILD_BUG_ON(sizeof(struct usb_req_read_regs
) + USB_MAX_IOREAD16_COUNT
*
1638 sizeof(__le16
) > sizeof(usb
->req_buf
));
1639 BUG_ON(sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
) >
1640 sizeof(usb
->req_buf
));
1642 req_len
= sizeof(struct usb_req_read_regs
) + count
* sizeof(__le16
);
1643 req
= (void *)usb
->req_buf
;
1645 req
->id
= cpu_to_le16(USB_REQ_READ_REGS
);
1646 for (i
= 0; i
< count
; i
++)
1647 req
->addr
[i
] = cpu_to_le16((u16
)addresses
[i
]);
1649 udev
= zd_usb_to_usbdev(usb
);
1650 prepare_read_regs_int(usb
);
1651 r
= usb_interrupt_msg(udev
, usb_sndintpipe(udev
, EP_REGS_OUT
),
1652 req
, req_len
, &actual_req_len
, 50 /* ms */);
1654 dev_dbg_f(zd_usb_dev(usb
),
1655 "error in usb_interrupt_msg(). Error number %d\n", r
);
1658 if (req_len
!= actual_req_len
) {
1659 dev_dbg_f(zd_usb_dev(usb
), "error in usb_interrupt_msg()\n"
1660 " req_len %d != actual_req_len %d\n",
1661 req_len
, actual_req_len
);
1666 timeout
= wait_for_completion_timeout(&usb
->intr
.read_regs
.completion
,
1667 msecs_to_jiffies(50));
1669 disable_read_regs_int(usb
);
1670 dev_dbg_f(zd_usb_dev(usb
), "read timed out\n");
1675 r
= get_results(usb
, values
, req
, count
);
1680 static void iowrite16v_urb_complete(struct urb
*urb
)
1682 struct zd_usb
*usb
= urb
->context
;
1684 if (urb
->status
&& !usb
->cmd_error
)
1685 usb
->cmd_error
= urb
->status
;
1687 if (!usb
->cmd_error
&&
1688 urb
->actual_length
!= urb
->transfer_buffer_length
)
1689 usb
->cmd_error
= -EIO
;
1692 static int zd_submit_waiting_urb(struct zd_usb
*usb
, bool last
)
1695 struct urb
*urb
= usb
->urb_async_waiting
;
1700 usb
->urb_async_waiting
= NULL
;
1703 urb
->transfer_flags
|= URB_NO_INTERRUPT
;
1705 usb_anchor_urb(urb
, &usb
->submitted_cmds
);
1706 r
= usb_submit_urb(urb
, GFP_KERNEL
);
1708 usb_unanchor_urb(urb
);
1709 dev_dbg_f(zd_usb_dev(usb
),
1710 "error in usb_submit_urb(). Error number %d\n", r
);
1714 /* fall-through with r == 0 */
1720 void zd_usb_iowrite16v_async_start(struct zd_usb
*usb
)
1722 ZD_ASSERT(usb_anchor_empty(&usb
->submitted_cmds
));
1723 ZD_ASSERT(usb
->urb_async_waiting
== NULL
);
1724 ZD_ASSERT(!usb
->in_async
);
1726 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1730 usb
->urb_async_waiting
= NULL
;
1733 int zd_usb_iowrite16v_async_end(struct zd_usb
*usb
, unsigned int timeout
)
1737 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1738 ZD_ASSERT(usb
->in_async
);
1740 /* Submit last iowrite16v URB */
1741 r
= zd_submit_waiting_urb(usb
, true);
1743 dev_dbg_f(zd_usb_dev(usb
),
1744 "error in zd_submit_waiting_usb(). "
1745 "Error number %d\n", r
);
1747 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1752 timeout
= usb_wait_anchor_empty_timeout(&usb
->submitted_cmds
,
1755 usb_kill_anchored_urbs(&usb
->submitted_cmds
);
1756 if (usb
->cmd_error
== -ENOENT
) {
1757 dev_dbg_f(zd_usb_dev(usb
), "timed out");
1769 int zd_usb_iowrite16v_async(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1773 struct usb_device
*udev
;
1774 struct usb_req_write_regs
*req
= NULL
;
1777 struct usb_host_endpoint
*ep
;
1779 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1780 ZD_ASSERT(usb
->in_async
);
1784 if (count
> USB_MAX_IOWRITE16_COUNT
) {
1785 dev_dbg_f(zd_usb_dev(usb
),
1786 "error: count %u exceeds possible max %u\n",
1787 count
, USB_MAX_IOWRITE16_COUNT
);
1791 dev_dbg_f(zd_usb_dev(usb
),
1792 "error: io in atomic context not supported\n");
1793 return -EWOULDBLOCK
;
1796 udev
= zd_usb_to_usbdev(usb
);
1798 ep
= usb_pipe_endpoint(udev
, usb_sndintpipe(udev
, EP_REGS_OUT
));
1802 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1806 req_len
= sizeof(struct usb_req_write_regs
) +
1807 count
* sizeof(struct reg_data
);
1808 req
= kmalloc(req_len
, GFP_KERNEL
);
1814 req
->id
= cpu_to_le16(USB_REQ_WRITE_REGS
);
1815 for (i
= 0; i
< count
; i
++) {
1816 struct reg_data
*rw
= &req
->reg_writes
[i
];
1817 rw
->addr
= cpu_to_le16((u16
)ioreqs
[i
].addr
);
1818 rw
->value
= cpu_to_le16(ioreqs
[i
].value
);
1821 usb_fill_int_urb(urb
, udev
, usb_sndintpipe(udev
, EP_REGS_OUT
),
1822 req
, req_len
, iowrite16v_urb_complete
, usb
,
1823 ep
->desc
.bInterval
);
1824 urb
->transfer_flags
|= URB_FREE_BUFFER
;
1826 /* Submit previous URB */
1827 r
= zd_submit_waiting_urb(usb
, false);
1829 dev_dbg_f(zd_usb_dev(usb
),
1830 "error in zd_submit_waiting_usb(). "
1831 "Error number %d\n", r
);
1835 /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1836 * of currect batch except for very last.
1838 usb
->urb_async_waiting
= urb
;
1845 int zd_usb_iowrite16v(struct zd_usb
*usb
, const struct zd_ioreq16
*ioreqs
,
1850 zd_usb_iowrite16v_async_start(usb
);
1851 r
= zd_usb_iowrite16v_async(usb
, ioreqs
, count
);
1853 zd_usb_iowrite16v_async_end(usb
, 0);
1856 return zd_usb_iowrite16v_async_end(usb
, 50 /* ms */);
1859 int zd_usb_rfwrite(struct zd_usb
*usb
, u32 value
, u8 bits
)
1862 struct usb_device
*udev
;
1863 struct usb_req_rfwrite
*req
= NULL
;
1864 int i
, req_len
, actual_req_len
;
1865 u16 bit_value_template
;
1868 dev_dbg_f(zd_usb_dev(usb
),
1869 "error: io in atomic context not supported\n");
1870 return -EWOULDBLOCK
;
1872 if (bits
< USB_MIN_RFWRITE_BIT_COUNT
) {
1873 dev_dbg_f(zd_usb_dev(usb
),
1874 "error: bits %d are smaller than"
1875 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1876 bits
, USB_MIN_RFWRITE_BIT_COUNT
);
1879 if (bits
> USB_MAX_RFWRITE_BIT_COUNT
) {
1880 dev_dbg_f(zd_usb_dev(usb
),
1881 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1882 bits
, USB_MAX_RFWRITE_BIT_COUNT
);
1886 if (value
& (~0UL << bits
)) {
1887 dev_dbg_f(zd_usb_dev(usb
),
1888 "error: value %#09x has bits >= %d set\n",
1894 dev_dbg_f(zd_usb_dev(usb
), "value %#09x bits %d\n", value
, bits
);
1896 r
= zd_usb_ioread16(usb
, &bit_value_template
, ZD_CR203
);
1898 dev_dbg_f(zd_usb_dev(usb
),
1899 "error %d: Couldn't read ZD_CR203\n", r
);
1902 bit_value_template
&= ~(RF_IF_LE
|RF_CLK
|RF_DATA
);
1904 ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb
)->mutex
));
1905 BUILD_BUG_ON(sizeof(struct usb_req_rfwrite
) +
1906 USB_MAX_RFWRITE_BIT_COUNT
* sizeof(__le16
) >
1907 sizeof(usb
->req_buf
));
1908 BUG_ON(sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
) >
1909 sizeof(usb
->req_buf
));
1911 req_len
= sizeof(struct usb_req_rfwrite
) + bits
* sizeof(__le16
);
1912 req
= (void *)usb
->req_buf
;
1914 req
->id
= cpu_to_le16(USB_REQ_WRITE_RF
);
1915 /* 1: 3683a, but not used in ZYDAS driver */
1916 req
->value
= cpu_to_le16(2);
1917 req
->bits
= cpu_to_le16(bits
);
1919 for (i
= 0; i
< bits
; i
++) {
1920 u16 bv
= bit_value_template
;
1921 if (value
& (1 << (bits
-1-i
)))
1923 req
->bit_values
[i
] = cpu_to_le16(bv
);
1926 udev
= zd_usb_to_usbdev(usb
);
1927 r
= usb_interrupt_msg(udev
, usb_sndintpipe(udev
, EP_REGS_OUT
),
1928 req
, req_len
, &actual_req_len
, 50 /* ms */);
1930 dev_dbg_f(zd_usb_dev(usb
),
1931 "error in usb_interrupt_msg(). Error number %d\n", r
);
1934 if (req_len
!= actual_req_len
) {
1935 dev_dbg_f(zd_usb_dev(usb
), "error in usb_interrupt_msg()"
1936 " req_len %d != actual_req_len %d\n",
1937 req_len
, actual_req_len
);
1942 /* FALL-THROUGH with r == 0 */