46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/string.h>
51 #include <linux/netdevice.h>
54 #include <linux/types.h>
55 #include <linux/ethtool.h>
57 #include <linux/wait.h>
59 #include <asm/uaccess.h>
60 #include <asm/byteorder.h>
64 #define KAWETH_MTU 1514
65 #define KAWETH_BUF_SIZE 1664
66 #define KAWETH_TX_TIMEOUT (5 * HZ)
67 #define KAWETH_SCRATCH_SIZE 32
68 #define KAWETH_FIRMWARE_BUF_SIZE 4096
69 #define KAWETH_CONTROL_TIMEOUT (30000)
71 #define KAWETH_STATUS_BROKEN 0x0000001
72 #define KAWETH_STATUS_CLOSING 0x0000002
73 #define KAWETH_STATUS_SUSPENDING 0x0000004
75 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
77 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
78 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
79 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
80 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
81 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
84 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
85 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
86 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
87 #define KAWETH_COMMAND_STATISTICS 0x03
88 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
89 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
90 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
91 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
92 #define KAWETH_COMMAND_SCAN 0xFF
94 #define KAWETH_SOFS_TO_WAIT 0x05
96 #define INTBUFFERSIZE 4
98 #define STATE_OFFSET 0
99 #define STATE_MASK 0x40
100 #define STATE_SHIFT 5
102 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
115 static int kaweth_probe(
120 static int kaweth_internal_control_msg(
struct usb_device *usb_dev,
123 int len,
int timeout);
131 { USB_DEVICE(0x03e8, 0x0008) },
132 { USB_DEVICE(0x04bb, 0x0901) },
133 { USB_DEVICE(0x0506, 0x03e8) },
134 { USB_DEVICE(0x0506, 0x11f8) },
135 { USB_DEVICE(0x0557, 0x2002) },
136 { USB_DEVICE(0x0557, 0x4000) },
137 { USB_DEVICE(0x0565, 0x0002) },
138 { USB_DEVICE(0x0565, 0x0003) },
139 { USB_DEVICE(0x0565, 0x0005) },
140 { USB_DEVICE(0x05e9, 0x0008) },
141 { USB_DEVICE(0x05e9, 0x0009) },
142 { USB_DEVICE(0x066b, 0x2202) },
143 { USB_DEVICE(0x06e1, 0x0008) },
144 { USB_DEVICE(0x06e1, 0x0009) },
145 { USB_DEVICE(0x0707, 0x0100) },
146 { USB_DEVICE(0x07aa, 0x0001) },
147 { USB_DEVICE(0x07b8, 0x4000) },
148 { USB_DEVICE(0x07c9, 0xb010) },
149 { USB_DEVICE(0x0846, 0x1001) },
150 { USB_DEVICE(0x0846, 0x1002) },
151 { USB_DEVICE(0x085a, 0x0008) },
152 { USB_DEVICE(0x085a, 0x0009) },
153 { USB_DEVICE(0x087d, 0x5704) },
154 { USB_DEVICE(0x0951, 0x0008) },
155 { USB_DEVICE(0x095a, 0x3003) },
156 { USB_DEVICE(0x10bd, 0x1427) },
157 { USB_DEVICE(0x1342, 0x0204) },
158 { USB_DEVICE(0x13d2, 0x0400) },
159 { USB_DEVICE(0x1485, 0x0001) },
160 { USB_DEVICE(0x1485, 0x0002) },
161 { USB_DEVICE(0x1645, 0x0005) },
162 { USB_DEVICE(0x1645, 0x0008) },
163 { USB_DEVICE(0x1645, 0x8005) },
164 { USB_DEVICE(0x1668, 0x0323) },
165 { USB_DEVICE(0x2001, 0x4000) },
174 static struct usb_driver kaweth_driver = {
176 .probe = kaweth_probe,
177 .disconnect = kaweth_disconnect,
178 .suspend = kaweth_suspend,
179 .resume = kaweth_resume,
180 .id_table = usb_klsi_table,
181 .supports_autosuspend = 1,
182 .disable_hub_initiated_lpm = 1,
287 retval = kaweth_internal_control_msg(kaweth->
dev,
301 static int kaweth_read_configuration(
struct kaweth_device *kaweth)
307 retval = kaweth_control(kaweth,
308 usb_rcvctrlpipe(kaweth->
dev, 0),
327 netdev_dbg(kaweth->
net,
"Setting URB size to %d\n", (
unsigned)urb_size);
329 retval = kaweth_control(kaweth,
330 usb_sndctrlpipe(kaweth->
dev, 0),
349 netdev_dbg(kaweth->
net,
"Set SOFS wait to %d\n", (
unsigned)sofs_wait);
351 retval = kaweth_control(kaweth,
352 usb_sndctrlpipe(kaweth->
dev, 0),
367 static int kaweth_set_receive_filter(
struct kaweth_device *kaweth,
368 __u16 receive_filter)
373 (
unsigned)receive_filter);
375 retval = kaweth_control(kaweth,
376 usb_sndctrlpipe(kaweth->
dev, 0),
391 static int kaweth_download_firmware(
struct kaweth_device *kaweth,
402 dev_err(&kaweth->
intf->dev,
"Firmware request failed\n");
407 dev_err(&kaweth->
intf->dev,
"Firmware too big: %zu\n",
426 "Downloading firmware at %p to kaweth device at %p\n",
430 return kaweth_control(kaweth,
431 usb_sndctrlpipe(kaweth->
dev, 0),
444 static int kaweth_trigger_firmware(
struct kaweth_device *kaweth,
458 return kaweth_control(kaweth,
459 usb_sndctrlpipe(kaweth->
dev, 0),
480 netdev_dbg(kaweth->
net,
"kaweth_reset() returns %d.\n", result);
485 static void kaweth_usb_receive(
struct urb *);
506 "can't resubmit intr, %s-%s, status %d\n",
507 kaweth->
dev->bus->bus_name,
508 kaweth->
dev->devpath, status);
511 static void int_callback(
struct urb *
u)
551 kaweth_resubmit_rx_urb(kaweth,
GFP_NOIO);
554 kaweth_resubmit_int_urb(kaweth,
GFP_NOIO);
561 static int kaweth_resubmit_rx_urb(
struct kaweth_device *kaweth,
566 usb_fill_bulk_urb(kaweth->
rx_urb,
568 usb_rcvbulkpipe(kaweth->
dev, 1),
573 kaweth->
rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
581 dev_err(&kaweth->
intf->dev,
"resubmitting rx_urb %d failed\n",
590 static void kaweth_async_set_rx_mode(
struct kaweth_device *kaweth);
595 static void kaweth_usb_receive(
struct urb *
urb)
600 int status = urb->status;
602 int count = urb->actual_length;
603 int count2 = urb->transfer_buffer_length;
610 kaweth->
stats.rx_errors++;
613 dev_dbg(dev,
"Status was -EPIPE.\n");
620 dev_dbg(dev,
"Status was -ECONNRESET or -ESHUTDOWN.\n");
625 kaweth->
stats.rx_errors++;
626 dev_dbg(dev,
"Status was -EPROTO, -ETIME, or -EILSEQ.\n");
630 kaweth->
stats.rx_errors++;
631 dev_dbg(dev,
"Status was -EOVERFLOW.\n");
640 if(status && status != -
EREMOTEIO && count != 1) {
642 "%s RX status: %d count: %d packet_len: %d\n",
643 net->
name, status, count, (
int)pkt_len);
648 if(kaweth->
net && (count > 2)) {
649 if(pkt_len > (count - 2)) {
651 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
653 dev_err(&kaweth->
intf->dev,
"Packet len & 2047: %x\n",
655 dev_err(&kaweth->
intf->dev,
"Count 2: %x\n", count2);
660 if(!(skb = dev_alloc_skb(pkt_len+2))) {
667 skb_copy_to_linear_data(skb, kaweth->
rx_buf + 2, pkt_len);
675 kaweth->
stats.rx_packets++;
685 static int kaweth_open(
struct net_device *net)
692 res = usb_autopm_get_interface(kaweth->
intf);
694 dev_err(&kaweth->
intf->dev,
"Interface cannot be resumed.\n");
697 res = kaweth_resubmit_rx_urb(kaweth,
GFP_KERNEL);
704 usb_rcvintpipe(kaweth->
dev, 3),
711 kaweth->
irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
720 netif_start_queue(net);
722 kaweth_async_set_rx_mode(kaweth);
726 usb_autopm_put_interface(kaweth->
intf);
750 static int kaweth_close(
struct net_device *net)
754 netif_stop_queue(net);
759 kaweth_kill_urbs(kaweth);
763 usb_autopm_put_interface(kaweth->
intf);
776 .get_link = kaweth_get_link
782 static void kaweth_usb_transmit_complete(
struct urb *urb)
786 int status = urb->status;
790 dev_dbg(&urb->dev->dev,
"%s: TX status %d.\n",
791 kaweth->
net->name, status);
793 netif_wake_queue(kaweth->
net);
810 kaweth_async_set_rx_mode(kaweth);
811 netif_stop_queue(net);
817 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
824 kaweth->
stats.tx_errors++;
825 netif_start_queue(net);
831 private_header = (
__le16 *)__skb_push(skb, 2);
835 usb_fill_bulk_urb(kaweth->
tx_urb,
837 usb_sndbulkpipe(kaweth->
dev, 2),
840 kaweth_usb_transmit_complete,
846 dev_warn(&net->
dev,
"kaweth failed tx_urb %d\n", res);
848 kaweth->
stats.tx_errors++;
850 netif_start_queue(net);
855 kaweth->
stats.tx_packets++;
867 static void kaweth_set_rx_mode(
struct net_device *net)
875 netdev_dbg(net,
"Setting Rx mode to %d\n", packet_filter_bitmap);
877 netif_stop_queue(net);
887 netif_wake_queue(net);
893 static void kaweth_async_set_rx_mode(
struct kaweth_device *kaweth)
899 if (packet_filter_bitmap == 0)
905 result = kaweth_control(kaweth,
906 usb_sndctrlpipe(kaweth->
dev, 0),
909 packet_filter_bitmap,
916 dev_err(&kaweth->
intf->dev,
"Failed to set Rx mode: %d\n",
921 packet_filter_bitmap);
931 return &kaweth->
stats;
937 static void kaweth_tx_timeout(
struct net_device *net)
942 kaweth->
stats.tx_errors++;
956 dev_dbg(&intf->dev,
"Suspending device\n");
959 spin_unlock_irqrestore(&kaweth->
device_lock, flags);
961 kaweth_kill_urbs(kaweth);
973 dev_dbg(&intf->dev,
"Resuming device\n");
976 spin_unlock_irqrestore(&kaweth->
device_lock, flags);
980 kaweth_resubmit_rx_urb(kaweth,
GFP_NOIO);
981 kaweth_resubmit_int_urb(kaweth,
GFP_NOIO);
992 .ndo_open = kaweth_open,
993 .ndo_stop = kaweth_close,
994 .ndo_start_xmit = kaweth_start_xmit,
995 .ndo_tx_timeout = kaweth_tx_timeout,
996 .ndo_set_rx_mode = kaweth_set_rx_mode,
997 .ndo_get_stats = kaweth_netdev_stats,
1003 static int kaweth_probe(
1008 struct device *dev = &intf->dev;
1009 struct usb_device *
udev = interface_to_usbdev(intf);
1012 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1016 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
1017 udev->devnum,
le16_to_cpu(udev->descriptor.idVendor),
1021 dev_dbg(dev,
"Device at %p\n", udev);
1023 dev_dbg(dev,
"Descriptor length: %x type: %x\n",
1024 (
int)udev->descriptor.bLength,
1025 (
int)udev->descriptor.bDescriptorType);
1027 netdev = alloc_etherdev(
sizeof(*kaweth));
1031 kaweth = netdev_priv(netdev);
1033 kaweth->
net = netdev;
1040 kaweth_reset(kaweth);
1047 if (
le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1048 dev_info(dev,
"Firmware present in device.\n");
1051 dev_info(dev,
"Downloading firmware...\n");
1053 if ((result = kaweth_download_firmware(kaweth,
1054 "kaweth/new_code.bin",
1057 dev_err(dev,
"Error downloading firmware (%d)\n",
1062 if ((result = kaweth_download_firmware(kaweth,
1063 "kaweth/new_code_fix.bin",
1066 dev_err(dev,
"Error downloading firmware fix (%d)\n",
1071 if ((result = kaweth_download_firmware(kaweth,
1072 "kaweth/trigger_code.bin",
1075 dev_err(dev,
"Error downloading trigger code (%d)\n",
1081 if ((result = kaweth_download_firmware(kaweth,
1082 "kaweth/trigger_code_fix.bin",
1085 dev_err(dev,
"Error downloading trigger code fix (%d)\n", result);
1090 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1091 dev_err(dev,
"Error triggering firmware (%d)\n", result);
1096 dev_info(dev,
"Firmware loaded. I'll be back...\n");
1103 result = kaweth_read_configuration(kaweth);
1106 dev_err(dev,
"Error reading configuration (%d), no net device created\n", result);
1107 goto err_free_netdev;
1111 dev_info(dev,
"Multicast filter limit: %x\n", kaweth->
configuration.max_multicast_filters & ((1 << 15) - 1));
1117 sizeof(bcast_addr))) {
1118 dev_err(dev,
"Firmware not functioning properly, no net device created\n");
1119 goto err_free_netdev;
1123 dev_dbg(dev,
"Error setting URB size\n");
1124 goto err_free_netdev;
1128 dev_err(dev,
"Error setting SOFS wait\n");
1129 goto err_free_netdev;
1132 result = kaweth_set_receive_filter(kaweth,
1138 dev_err(dev,
"Error setting receive filter\n");
1139 goto err_free_netdev;
1142 dev_dbg(dev,
"Initializing net device.\n");
1148 goto err_free_netdev;
1161 goto err_tx_and_rx_and_irq;
1167 goto err_all_but_rxbuf;
1180 usb_set_intfdata(intf, kaweth);
1190 dev_err(dev,
"Error registering netdev.\n");
1194 dev_info(dev,
"kaweth interface created at %s\n",
1197 dev_dbg(dev,
"Kaweth probe returning.\n");
1202 usb_set_intfdata(intf,
NULL);
1206 err_tx_and_rx_and_irq:
1226 dev_info(&intf->dev,
"Unregistering\n");
1228 usb_set_intfdata(intf,
NULL);
1230 dev_warn(&intf->dev,
"unregistering non-existent device\n");
1233 netdev = kaweth->
net;
1259 static void usb_api_blocking_completion(
struct urb *urb)
1272 static int usb_start_wait_urb(
struct urb *urb,
int timeout,
int* actual_length)
1280 urb->context = &awd;
1290 dev_warn(&urb->dev->dev,
"usb_control/bulk_msg: timeout\n");
1295 status = urb->status;
1298 if (actual_length) {
1299 *actual_length = urb->actual_length;
1308 static int kaweth_internal_control_msg(
struct usb_device *usb_dev,
1311 int len,
int timeout)
1321 usb_fill_control_urb(urb, usb_dev, pipe, (
unsigned char*)cmd, data,
1322 len, usb_api_blocking_completion,
NULL);
1324 retv = usb_start_wait_urb(urb, timeout, &length);