22 #include <linux/kernel.h>
25 #include <linux/device.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
31 #include <linux/module.h>
33 #include <asm/unaligned.h>
112 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
113 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
119 static void int_urb_complete(
struct urb *
urb);
121 static int request_fw_file(
131 "Could not load firmware file %s. Error number %d\n",
136 static inline u16 get_bcdDevice(
const struct usb_device *
udev)
146 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
148 static int upload_code(
struct usb_device *
udev,
158 dev_err(&udev->dev,
"out of memory\n");
168 dev_dbg_f(&udev->dev,
"transfer size %zu\n", transfer_size);
170 memcpy(p, data, transfer_size);
174 code_offset, 0, p, transfer_size, 1000 );
177 "USB control request for firmware upload"
178 " failed. Error number %d\n", r);
181 transfer_size = r & ~1;
185 code_offset += transfer_size/
sizeof(
u16);
195 0, 0, p,
sizeof(ret), 5000 );
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",
239 static int handle_version_mismatch(
struct zd_usb *
usb,
242 struct usb_device *
udev = zd_usb_to_usbdev(usb);
248 r = request_fw_file(&ur_fw,
249 get_fw_name(usb, fw_name,
sizeof(fw_name),
"ur"),
259 r = upload_code(udev, ub_fw->
data + offset, ub_fw->
size - offset,
272 static int upload_firmware(
struct zd_usb *usb)
277 struct usb_device *udev = zd_usb_to_usbdev(usb);
282 bcdDevice = get_bcdDevice(udev);
284 r = request_fw_file(&ub_fw,
285 get_fw_name(usb, fw_name,
sizeof(fw_name),
"ub"),
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);
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"),
319 "Could not upload firmware code uph. Error number %d\n",
342 struct usb_device *udev = zd_usb_to_usbdev(usb);
354 "read over firmware interface failed: %d\n", r);
356 }
else if (r != len) {
358 "incomplete read over firmware interface: %d/%d\n",
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;
377 spin_lock(&intr->
lock);
383 spin_unlock(&intr->
lock);
386 static inline void handle_regs_int(
struct urb *
urb)
388 struct zd_usb *usb = urb->context;
394 spin_lock(&intr->
lock);
398 struct zd_mac *
mac = zd_hw_mac(zd_usb_to_hw(urb->context));
399 spin_lock(&mac->
lock);
402 spin_unlock(&mac->
lock);
405 len = urb->actual_length;
406 intr->
read_regs.length = urb->actual_length;
407 if (len >
sizeof(intr->
read_regs.buffer))
418 if (!check_read_regs(usb, intr->
read_regs.req,
430 spin_unlock(&intr->
lock);
434 handle_regs_int_override(urb);
437 static void int_urb_complete(
struct urb *urb)
444 switch (urb->status) {
460 if (urb->actual_length <
sizeof(hdr)) {
465 hdr = urb->transfer_buffer;
477 handle_regs_int_override(urb);
481 handle_regs_int(urb);
488 (
unsigned int)hdr->
id);
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)
523 spin_unlock_irqrestore(&intr->
lock, flags);
530 struct usb_device *udev = zd_usb_to_usbdev(usb);
543 spin_lock_irq(&intr->
lock);
545 spin_unlock_irq(&intr->
lock);
550 spin_unlock_irq(&intr->
lock);
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),
563 int_urb_complete, usb,
566 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
572 "Couldn't submit urb. Error number %d\n", r);
581 spin_lock_irq(&intr->
lock);
583 spin_unlock_irq(&intr->
lock);
593 struct usb_device *udev = zd_usb_to_usbdev(usb);
602 spin_unlock_irqrestore(&intr->
lock, flags);
609 spin_unlock_irqrestore(&intr->
lock, flags);
620 static void handle_rx_packet(
struct zd_usb *usb,
const u8 *
buffer,
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)
671 switch (urb->status) {
687 buffer = urb->transfer_buffer;
688 length = urb->actual_length;
698 spin_lock(&rx->
lock);
701 spin_unlock(&rx->
lock);
705 spin_lock(&rx->
lock);
715 spin_unlock(&rx->
lock);
717 spin_unlock(&rx->
lock);
718 handle_rx_packet(usb, buffer, length);
727 static struct urb *alloc_rx_urb(
struct zd_usb *usb)
729 struct usb_device *udev = zd_usb_to_usbdev(usb);
743 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev,
EP_DATA_IN),
745 rx_urb_complete, usb);
746 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
751 static void free_rx_urb(
struct urb *urb)
756 urb->transfer_buffer, urb->transfer_dma);
760 static int __zd_usb_enable_rx(
struct zd_usb *usb)
773 urbs[
i] = alloc_rx_urb(usb);
779 spin_lock_irq(&rx->
lock);
781 spin_unlock_irq(&rx->
lock);
787 spin_unlock_irq(&rx->
lock);
800 spin_lock_irq(&rx->
lock);
803 spin_unlock_irq(&rx->
lock);
807 free_rx_urb(urbs[i]);
818 r = __zd_usb_enable_rx(usb);
826 static void __zd_usb_disable_rx(
struct zd_usb *usb)
837 spin_unlock_irqrestore(&rx->
lock, flags);
841 for (i = 0; i <
count; i++) {
843 free_rx_urb(urbs[i]);
850 spin_unlock_irqrestore(&rx->
lock, flags);
858 __zd_usb_disable_rx(usb);
865 static void zd_usb_reset_rx(
struct zd_usb *usb)
875 spin_unlock_irqrestore(&rx->
lock, flags);
878 __zd_usb_disable_rx(usb);
879 __zd_usb_enable_rx(usb);
908 spin_unlock_irqrestore(&tx->
lock, flags);
932 spin_unlock_irqrestore(&tx->
lock, flags);
935 static void tx_dec_submitted_urbs(
struct zd_usb *usb)
946 spin_unlock_irqrestore(&tx->
lock, flags);
949 static void tx_inc_submitted_urbs(
struct zd_usb *usb)
960 spin_unlock_irqrestore(&tx->
lock, flags);
970 static void tx_urb_complete(
struct urb *urb)
978 skb = (
struct sk_buff *)urb->context;
979 info = IEEE80211_SKB_CB(skb);
987 switch (urb->status) {
1006 tx_dec_submitted_urbs(usb);
1034 struct usb_device *udev = zd_usb_to_usbdev(usb);
1049 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev,
EP_DATA_OUT),
1050 skb->
data, skb->
len, tx_urb_complete, skb);
1063 tx_inc_submitted_urbs(usb);
1071 static bool zd_tx_timeout(
struct zd_usb *usb)
1077 unsigned long flags, trans_start;
1078 bool have_timedout =
false;
1081 skb_queue_walk_safe(q, skb, skbnext) {
1082 info = IEEE80211_SKB_CB(skb);
1086 have_timedout =
true;
1090 spin_unlock_irqrestore(&q->
lock, flags);
1092 return have_timedout;
1103 if (!zd_tx_timeout(usb))
1141 static void zd_rx_idle_timer_handler(
struct work_struct *work)
1145 struct zd_mac *mac = zd_usb_to_mac(usb);
1153 zd_usb_reset_rx(usb);
1156 static void zd_usb_reset_rx_idle_timer_tasklet(
unsigned long param)
1170 static inline void init_usb_interrupt(
struct zd_usb *usb)
1175 intr->
interval = int_urb_interval(zd_usb_to_usbdev(usb));
1176 init_completion(&intr->
read_regs.completion);
1181 static inline void init_usb_rx(
struct zd_usb *usb)
1198 static inline void init_usb_tx(
struct zd_usb *usb)
1215 memset(usb, 0,
sizeof(*usb));
1217 usb_set_intfdata(usb->
intf, hw);
1219 init_usb_interrupt(usb);
1242 return "unknown speed";
1246 static int scnprint_id(
struct usb_device *udev,
char *buffer,
size_t size)
1248 return scnprintf(buffer, size,
"%04hx:%04hx v%04hx %s",
1251 get_bcdDevice(udev),
1252 speed(udev->speed));
1257 struct usb_device *udev = interface_to_usbdev(usb->
intf);
1258 return scnprint_id(udev, buffer, size);
1262 static void print_id(
struct usb_device *udev)
1266 scnprint_id(udev, buffer,
sizeof(buffer));
1267 buffer[
sizeof(
buffer)-1] = 0;
1271 #define print_id(udev) do { } while (0)
1276 struct usb_device *udev = interface_to_usbdev(intf);
1277 struct usb_host_interface *iface_desc = &intf->altsetting[0];
1284 for (r = 1; r >= 0; r--) {
1285 endpoint = &iface_desc->endpoint[
r].desc;
1286 if (usb_endpoint_dir_out(endpoint) &&
1287 usb_endpoint_xfer_bulk(endpoint)) {
1294 "zd1211rw: Could not find bulk out endpoint\n");
1312 dev_info(&udev->dev,
"Ejecting virtual installer media...\n");
1313 r =
usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1314 cmd, 31,
NULL, 2000);
1322 usb_set_intfdata(intf,
NULL);
1329 struct zd_mac *mac = zd_usb_to_mac(usb);
1333 r = upload_firmware(usb);
1336 "couldn't load firmware. Error number %d\n", r);
1343 "couldn't reset configuration. Error number %d\n", r);
1350 "couldn't initialize mac. Error number %d\n", r);
1361 struct usb_device *udev = interface_to_usbdev(intf);
1368 return eject_installer(intf);
1370 switch (udev->speed) {
1376 dev_dbg_f(&intf->dev,
"Unknown USB speed\n");
1384 "couldn't reset usb device. Error number %d\n", r);
1394 usb = &zd_hw_mac(hw)->chip.usb;
1400 "couldn't initialize mac. Error number %d\n", r);
1407 "couldn't register device. Error number %d\n", r);
1434 mac = zd_hw_mac(hw);
1435 usb = &mac->
chip.usb;
1455 dev_dbg(&intf->dev,
"disconnected\n");
1458 static void zd_usb_resume(
struct zd_usb *usb)
1460 struct zd_mac *mac = zd_usb_to_mac(usb);
1468 "with error code %d. Retrying...\n", r);
1479 "failed to restore settings, %d\n", r);
1485 static void zd_usb_stop(
struct zd_usb *usb)
1504 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1507 mac = zd_hw_mac(hw);
1508 usb = &mac->
chip.usb;
1524 if (!hw || intf->condition != USB_INTERFACE_BOUND)
1527 mac = zd_hw_mac(hw);
1528 usb = &mac->
chip.usb;
1537 static struct usb_driver
driver = {
1538 .name = KBUILD_MODNAME,
1539 .id_table = usb_ids,
1542 .pre_reset = pre_reset,
1543 .post_reset = post_reset,
1544 .disable_hub_initiated_lpm = 1,
1549 static int __init usb_init(
void)
1553 pr_debug(
"%s usb_init()\n", driver.name);
1556 if (zd_workqueue ==
NULL) {
1561 r = usb_register(&driver);
1569 pr_debug(
"%s initialized\n", driver.name);
1573 static void __exit usb_exit(
void)
1575 pr_debug(
"%s usb_exit()\n", driver.name);
1583 static int zd_ep_regs_out_msg(
struct usb_device *udev,
void *
data,
int len,
1584 int *actual_length,
int timeout)
1590 struct usb_host_endpoint *ep;
1594 ep = usb_pipe_endpoint(udev, pipe);
1598 if (usb_endpoint_xfer_int(&ep->desc)) {
1600 actual_length, timeout);
1603 return usb_bulk_msg(udev, pipe, data, len, actual_length,
1608 static int usb_int_regs_length(
unsigned int count)
1613 static void prepare_read_regs_int(
struct zd_usb *usb,
1619 spin_lock_irq(&intr->
lock);
1624 spin_unlock_irq(&intr->
lock);
1627 static void disable_read_regs_int(
struct zd_usb *usb)
1631 spin_lock_irq(&intr->
lock);
1633 spin_unlock_irq(&intr->
lock);
1647 if (rr->
length < usb_int_regs_length(count)) {
1649 "error: actual length %d less than expected %d\n",
1650 rr->
length, usb_int_regs_length(count));
1656 "error: actual length %d exceeds buffer size %zu\n",
1661 for (i = 0; i <
count; i++) {
1665 "rd[%d] addr %#06hx expected %#06hx\n", i,
1685 spin_lock_irq(&intr->
lock);
1694 if (!check_read_regs(usb, req, count)) {
1699 for (i = 0; i <
count; i++) {
1706 spin_unlock_irq(&intr->
lock);
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;
1716 unsigned long timeout;
1725 "error: count %u exceeds possible max %u\n",
1731 "error: io in atomic context not supported\n");
1734 if (!usb_int_enabled(usb)) {
1736 "error: usb interrupt not enabled\n");
1750 for (i = 0; i <
count; 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 );
1760 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
1763 if (req_len != actual_req_len) {
1765 " req_len %d != actual_req_len %d\n",
1766 req_len, actual_req_len);
1774 disable_read_regs_int(usb);
1780 r = get_results(usb, values, req, count, &retry);
1781 if (retry && try_count < 20) {
1790 static void iowrite16v_urb_complete(
struct urb *urb)
1792 struct zd_usb *usb = urb->context;
1798 urb->actual_length != urb->transfer_buffer_length)
1802 static int zd_submit_waiting_urb(
struct zd_usb *usb,
bool last)
1813 urb->transfer_flags |= URB_NO_INTERRUPT;
1820 "error in usb_submit_urb(). Error number %d\n", r);
1851 r = zd_submit_waiting_urb(usb,
true);
1854 "error in zd_submit_waiting_usb(). "
1855 "Error number %d\n", r);
1883 struct usb_device *
udev;
1887 struct usb_host_endpoint *ep;
1896 "error: count %u exceeds possible max %u\n",
1902 "error: io in atomic context not supported\n");
1906 udev = zd_usb_to_usbdev(usb);
1908 ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev,
EP_REGS_OUT));
1925 for (i = 0; i <
count; i++) {
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;
1945 r = zd_submit_waiting_urb(usb,
false);
1948 "error in zd_submit_waiting_usb(). "
1949 "Error number %d\n", r);
1980 struct usb_device *
udev;
1983 u16 bit_value_template;
1987 "error: io in atomic context not supported\n");
1992 "error: bits %d are smaller than"
1993 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1999 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
2004 if (value & (~0
UL << bits)) {
2006 "error: value %#09x has bits >= %d set\n",
2014 r = zd_usb_ioread16(usb, &bit_value_template,
ZD_CR203);
2017 "error %d: Couldn't read ZD_CR203\n", r);
2037 for (i = 0; i <
bits; i++) {
2038 u16 bv = bit_value_template;
2039 if (value & (1 << (bits-1-i)))
2044 udev = zd_usb_to_usbdev(usb);
2045 r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 );
2048 "error in zd_ep_regs_out_msg(). Error number %d\n", r);
2051 if (req_len != actual_req_len) {
2053 " req_len %d != actual_req_len %d\n",
2054 req_len, actual_req_len);