1 /****************************************************************
3 * kaweth.c - driver for KL5KUSB101 based USB->Ethernet
5 * (c) 2000 Interlan Communications
6 * (c) 2000 Stephane Alnet
8 * (C) 2002 Oliver Neukum
10 * Original author: The Zapman <zapman@interlan.net>
11 * Inspired by, and much credit goes to Michael Rothwell
12 * <rothwell@interlan.net> for the test equipment, help, and patience
13 * Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
14 * Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
15 * for providing the firmware and driver resources.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 ****************************************************************/
33 * Develop test procedures for USB net interfaces
35 * Fix bugs from previous two steps
36 * Snoop other OSs for any tricks we're not doing
37 * Reduce arbitrary timeouts
38 * Smart multicast support
39 * Temporary MAC change support
40 * Tunable SOFs parameter - ioctl()?
41 * Ethernet stats collection
42 * Code formatting improvements
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/delay.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/usb.h>
52 #include <linux/types.h>
53 #include <linux/ethtool.h>
54 #include <linux/dma-mapping.h>
55 #include <linux/wait.h>
56 #include <linux/firmware.h>
57 #include <asm/uaccess.h>
58 #include <asm/byteorder.h>
62 #define KAWETH_MTU 1514
63 #define KAWETH_BUF_SIZE 1664
64 #define KAWETH_TX_TIMEOUT (5 * HZ)
65 #define KAWETH_SCRATCH_SIZE 32
66 #define KAWETH_FIRMWARE_BUF_SIZE 4096
67 #define KAWETH_CONTROL_TIMEOUT (30000)
69 #define KAWETH_STATUS_BROKEN 0x0000001
70 #define KAWETH_STATUS_CLOSING 0x0000002
71 #define KAWETH_STATUS_SUSPENDING 0x0000004
73 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
75 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
76 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
77 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
78 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
79 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
82 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
83 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
84 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
85 #define KAWETH_COMMAND_STATISTICS 0x03
86 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
87 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
88 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
89 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
90 #define KAWETH_COMMAND_SCAN 0xFF
92 #define KAWETH_SOFS_TO_WAIT 0x05
94 #define INTBUFFERSIZE 4
96 #define STATE_OFFSET 0
97 #define STATE_MASK 0x40
100 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
103 MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
104 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
105 MODULE_LICENSE("GPL");
108 static const char driver_name[] = "kaweth";
110 static int kaweth_probe(
111 struct usb_interface *intf,
112 const struct usb_device_id *id /* from id_table */
114 static void kaweth_disconnect(struct usb_interface *intf);
115 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
117 struct usb_ctrlrequest *cmd, void *data,
118 int len, int timeout);
119 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
120 static int kaweth_resume(struct usb_interface *intf);
122 /****************************************************************
124 ****************************************************************/
125 static struct usb_device_id usb_klsi_table[] = {
126 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
127 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
128 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
129 { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
130 { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
131 { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
132 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
133 { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
134 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
135 { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
136 { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
137 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
138 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
139 { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
140 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
141 { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
142 { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
143 { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
144 { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
145 { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
146 { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
147 { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
148 { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
149 { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
150 { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
151 { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
152 { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
153 { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
154 { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
155 { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
156 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
157 { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
158 { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
159 { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
160 { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
161 {} /* Null terminator */
164 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
166 /****************************************************************
168 ****************************************************************/
169 static struct usb_driver kaweth_driver = {
171 .probe = kaweth_probe,
172 .disconnect = kaweth_disconnect,
173 .suspend = kaweth_suspend,
174 .resume = kaweth_resume,
175 .id_table = usb_klsi_table,
176 .supports_autosuspend = 1,
177 .disable_hub_initiated_lpm = 1,
180 typedef __u8 eth_addr_t[6];
182 /****************************************************************
184 ****************************************************************/
192 /****************************************************************
193 * kaweth_ethernet_configuration
195 ****************************************************************/
196 struct kaweth_ethernet_configuration
202 __u32 statistics_mask;
204 __u16 max_multicast_filters;
208 /****************************************************************
210 ****************************************************************/
213 spinlock_t device_lock;
217 int suspend_lowmem_rx;
218 int suspend_lowmem_ctrl;
221 struct delayed_work lowmem_work;
223 struct usb_device *dev;
224 struct usb_interface *intf;
225 struct net_device *net;
226 wait_queue_head_t term_wait;
232 dma_addr_t intbufferhandle;
234 dma_addr_t rxbufferhandle;
238 struct sk_buff *tx_skb;
241 __u8 scratch[KAWETH_SCRATCH_SIZE];
242 __u16 packet_filter_bitmap;
244 struct kaweth_ethernet_configuration configuration;
246 struct net_device_stats stats;
249 /****************************************************************
251 ****************************************************************/
252 static int kaweth_control(struct kaweth_device *kaweth,
262 struct usb_ctrlrequest *dr;
266 netdev_dbg(kaweth->net, "in_interrupt()\n");
270 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
274 dr->bRequestType = requesttype;
275 dr->bRequest = request;
276 dr->wValue = cpu_to_le16(value);
277 dr->wIndex = cpu_to_le16(index);
278 dr->wLength = cpu_to_le16(size);
280 retval = kaweth_internal_control_msg(kaweth->dev,
291 /****************************************************************
292 * kaweth_read_configuration
293 ****************************************************************/
294 static int kaweth_read_configuration(struct kaweth_device *kaweth)
298 retval = kaweth_control(kaweth,
299 usb_rcvctrlpipe(kaweth->dev, 0),
300 KAWETH_COMMAND_GET_ETHERNET_DESC,
301 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
304 (void *)&kaweth->configuration,
305 sizeof(kaweth->configuration),
306 KAWETH_CONTROL_TIMEOUT);
311 /****************************************************************
312 * kaweth_set_urb_size
313 ****************************************************************/
314 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
318 netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
320 retval = kaweth_control(kaweth,
321 usb_sndctrlpipe(kaweth->dev, 0),
322 KAWETH_COMMAND_SET_URB_SIZE,
323 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
326 (void *)&kaweth->scratch,
328 KAWETH_CONTROL_TIMEOUT);
333 /****************************************************************
334 * kaweth_set_sofs_wait
335 ****************************************************************/
336 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
340 netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
342 retval = kaweth_control(kaweth,
343 usb_sndctrlpipe(kaweth->dev, 0),
344 KAWETH_COMMAND_SET_SOFS_WAIT,
345 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
348 (void *)&kaweth->scratch,
350 KAWETH_CONTROL_TIMEOUT);
355 /****************************************************************
356 * kaweth_set_receive_filter
357 ****************************************************************/
358 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
359 __u16 receive_filter)
363 netdev_dbg(kaweth->net, "Set receive filter to %d\n",
364 (unsigned)receive_filter);
366 retval = kaweth_control(kaweth,
367 usb_sndctrlpipe(kaweth->dev, 0),
368 KAWETH_COMMAND_SET_PACKET_FILTER,
369 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
372 (void *)&kaweth->scratch,
374 KAWETH_CONTROL_TIMEOUT);
379 /****************************************************************
380 * kaweth_download_firmware
381 ****************************************************************/
382 static int kaweth_download_firmware(struct kaweth_device *kaweth,
387 const struct firmware *fw;
391 ret = reject_firmware(&fw, fwname, &kaweth->dev->dev);
393 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
397 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
398 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
400 release_firmware(fw);
404 memcpy(kaweth->firmware_buf, fw->data, fw->size);
406 release_firmware(fw);
408 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
409 kaweth->firmware_buf[3] = data_len >> 8;
410 kaweth->firmware_buf[4] = type;
411 kaweth->firmware_buf[5] = interrupt;
413 netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
414 kaweth->firmware_buf[2]);
416 netdev_dbg(kaweth->net,
417 "Downloading firmware at %p to kaweth device at %p\n",
418 kaweth->firmware_buf, kaweth);
419 netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
421 return kaweth_control(kaweth,
422 usb_sndctrlpipe(kaweth->dev, 0),
424 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
427 (void *)kaweth->firmware_buf,
429 KAWETH_CONTROL_TIMEOUT);
432 /****************************************************************
433 * kaweth_trigger_firmware
434 ****************************************************************/
435 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
438 kaweth->firmware_buf[0] = 0xB6;
439 kaweth->firmware_buf[1] = 0xC3;
440 kaweth->firmware_buf[2] = 0x01;
441 kaweth->firmware_buf[3] = 0x00;
442 kaweth->firmware_buf[4] = 0x06;
443 kaweth->firmware_buf[5] = interrupt;
444 kaweth->firmware_buf[6] = 0x00;
445 kaweth->firmware_buf[7] = 0x00;
447 return kaweth_control(kaweth,
448 usb_sndctrlpipe(kaweth->dev, 0),
450 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
453 (void *)kaweth->firmware_buf,
455 KAWETH_CONTROL_TIMEOUT);
458 /****************************************************************
460 ****************************************************************/
461 static int kaweth_reset(struct kaweth_device *kaweth)
465 result = usb_reset_configuration(kaweth->dev);
468 netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
473 static void kaweth_usb_receive(struct urb *);
474 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
476 /****************************************************************
478 *****************************************************************/
480 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
484 status = usb_submit_urb (kaweth->irq_urb, mf);
485 if (unlikely(status == -ENOMEM)) {
486 kaweth->suspend_lowmem_ctrl = 1;
487 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
489 kaweth->suspend_lowmem_ctrl = 0;
493 dev_err(&kaweth->intf->dev,
494 "can't resubmit intr, %s-%s, status %d\n",
495 kaweth->dev->bus->bus_name,
496 kaweth->dev->devpath, status);
499 static void int_callback(struct urb *u)
501 struct kaweth_device *kaweth = u->context;
503 int status = u->status;
506 case 0: /* success */
508 case -ECONNRESET: /* unlink */
512 /* -EPIPE: should clear the halt */
517 /* we check the link state to report changes */
518 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
520 netif_carrier_on(kaweth->net);
522 netif_carrier_off(kaweth->net);
524 kaweth->linkstate = act_state;
527 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
530 static void kaweth_resubmit_tl(struct work_struct *work)
532 struct kaweth_device *kaweth =
533 container_of(work, struct kaweth_device, lowmem_work.work);
535 if (IS_BLOCKED(kaweth->status))
538 if (kaweth->suspend_lowmem_rx)
539 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
541 if (kaweth->suspend_lowmem_ctrl)
542 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
546 /****************************************************************
547 * kaweth_resubmit_rx_urb
548 ****************************************************************/
549 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
554 usb_fill_bulk_urb(kaweth->rx_urb,
556 usb_rcvbulkpipe(kaweth->dev, 1),
561 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
562 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
564 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
565 if (result == -ENOMEM) {
566 kaweth->suspend_lowmem_rx = 1;
567 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
569 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
572 kaweth->suspend_lowmem_rx = 0;
578 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
580 /****************************************************************
582 ****************************************************************/
583 static void kaweth_usb_receive(struct urb *urb)
585 struct device *dev = &urb->dev->dev;
586 struct kaweth_device *kaweth = urb->context;
587 struct net_device *net = kaweth->net;
588 int status = urb->status;
590 int count = urb->actual_length;
591 int count2 = urb->transfer_buffer_length;
593 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
597 if (unlikely(status == -EPIPE)) {
598 kaweth->stats.rx_errors++;
600 wake_up(&kaweth->term_wait);
601 dev_dbg(dev, "Status was -EPIPE.\n");
604 if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
605 /* we are killed - set a flag and wake the disconnect handler */
607 wake_up(&kaweth->term_wait);
608 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
611 if (unlikely(status == -EPROTO || status == -ETIME ||
612 status == -EILSEQ)) {
613 kaweth->stats.rx_errors++;
614 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
617 if (unlikely(status == -EOVERFLOW)) {
618 kaweth->stats.rx_errors++;
619 dev_dbg(dev, "Status was -EOVERFLOW.\n");
621 spin_lock(&kaweth->device_lock);
622 if (IS_BLOCKED(kaweth->status)) {
623 spin_unlock(&kaweth->device_lock);
626 spin_unlock(&kaweth->device_lock);
628 if(status && status != -EREMOTEIO && count != 1) {
629 dev_err(&kaweth->intf->dev,
630 "%s RX status: %d count: %d packet_len: %d\n",
631 net->name, status, count, (int)pkt_len);
632 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
636 if(kaweth->net && (count > 2)) {
637 if(pkt_len > (count - 2)) {
638 dev_err(&kaweth->intf->dev,
639 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
641 dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
643 dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
644 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
648 if(!(skb = dev_alloc_skb(pkt_len+2))) {
649 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
653 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
655 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
657 skb_put(skb, pkt_len);
659 skb->protocol = eth_type_trans(skb, net);
663 kaweth->stats.rx_packets++;
664 kaweth->stats.rx_bytes += pkt_len;
667 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
670 /****************************************************************
672 ****************************************************************/
673 static int kaweth_open(struct net_device *net)
675 struct kaweth_device *kaweth = netdev_priv(net);
678 res = usb_autopm_get_interface(kaweth->intf);
680 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
683 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
690 usb_rcvintpipe(kaweth->dev, 3),
695 250); /* overriding the descriptor */
696 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
697 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
699 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
701 usb_kill_urb(kaweth->rx_urb);
706 netif_start_queue(net);
708 kaweth_async_set_rx_mode(kaweth);
712 usb_autopm_put_interface(kaweth->intf);
716 /****************************************************************
718 ****************************************************************/
719 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
721 usb_kill_urb(kaweth->irq_urb);
722 usb_kill_urb(kaweth->rx_urb);
723 usb_kill_urb(kaweth->tx_urb);
725 cancel_delayed_work_sync(&kaweth->lowmem_work);
727 /* a scheduled work may have resubmitted,
729 usb_kill_urb(kaweth->irq_urb);
730 usb_kill_urb(kaweth->rx_urb);
733 /****************************************************************
735 ****************************************************************/
736 static int kaweth_close(struct net_device *net)
738 struct kaweth_device *kaweth = netdev_priv(net);
740 netif_stop_queue(net);
743 kaweth->status |= KAWETH_STATUS_CLOSING;
745 kaweth_kill_urbs(kaweth);
747 kaweth->status &= ~KAWETH_STATUS_CLOSING;
749 usb_autopm_put_interface(kaweth->intf);
754 static u32 kaweth_get_link(struct net_device *dev)
756 struct kaweth_device *kaweth = netdev_priv(dev);
758 return kaweth->linkstate;
761 static const struct ethtool_ops ops = {
762 .get_link = kaweth_get_link
765 /****************************************************************
766 * kaweth_usb_transmit_complete
767 ****************************************************************/
768 static void kaweth_usb_transmit_complete(struct urb *urb)
770 struct kaweth_device *kaweth = urb->context;
771 struct sk_buff *skb = kaweth->tx_skb;
772 int status = urb->status;
774 if (unlikely(status != 0))
775 if (status != -ENOENT)
776 dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
777 kaweth->net->name, status);
779 netif_wake_queue(kaweth->net);
780 dev_kfree_skb_irq(skb);
783 /****************************************************************
785 ****************************************************************/
786 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
787 struct net_device *net)
789 struct kaweth_device *kaweth = netdev_priv(net);
790 __le16 *private_header;
794 spin_lock_irq(&kaweth->device_lock);
796 kaweth_async_set_rx_mode(kaweth);
797 netif_stop_queue(net);
798 if (IS_BLOCKED(kaweth->status)) {
802 /* We now decide whether we can put our special header into the sk_buff */
803 if (skb_cow_head(skb, 2)) {
804 kaweth->stats.tx_errors++;
805 netif_start_queue(net);
806 spin_unlock_irq(&kaweth->device_lock);
807 dev_kfree_skb_any(skb);
811 private_header = (__le16 *)__skb_push(skb, 2);
812 *private_header = cpu_to_le16(skb->len-2);
813 kaweth->tx_skb = skb;
815 usb_fill_bulk_urb(kaweth->tx_urb,
817 usb_sndbulkpipe(kaweth->dev, 2),
820 kaweth_usb_transmit_complete,
824 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
826 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
828 kaweth->stats.tx_errors++;
830 netif_start_queue(net);
831 dev_kfree_skb_irq(skb);
835 kaweth->stats.tx_packets++;
836 kaweth->stats.tx_bytes += skb->len;
839 spin_unlock_irq(&kaweth->device_lock);
844 /****************************************************************
846 ****************************************************************/
847 static void kaweth_set_rx_mode(struct net_device *net)
849 struct kaweth_device *kaweth = netdev_priv(net);
851 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
852 KAWETH_PACKET_FILTER_BROADCAST |
853 KAWETH_PACKET_FILTER_MULTICAST;
855 netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
857 netif_stop_queue(net);
859 if (net->flags & IFF_PROMISC) {
860 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
862 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
863 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
866 kaweth->packet_filter_bitmap = packet_filter_bitmap;
867 netif_wake_queue(net);
870 /****************************************************************
871 * kaweth_async_set_rx_mode
872 ****************************************************************/
873 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
876 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
878 kaweth->packet_filter_bitmap = 0;
879 if (packet_filter_bitmap == 0)
885 result = kaweth_control(kaweth,
886 usb_sndctrlpipe(kaweth->dev, 0),
887 KAWETH_COMMAND_SET_PACKET_FILTER,
888 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
889 packet_filter_bitmap,
891 (void *)&kaweth->scratch,
893 KAWETH_CONTROL_TIMEOUT);
896 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
900 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
901 packet_filter_bitmap);
905 /****************************************************************
906 * kaweth_netdev_stats
907 ****************************************************************/
908 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
910 struct kaweth_device *kaweth = netdev_priv(dev);
911 return &kaweth->stats;
914 /****************************************************************
916 ****************************************************************/
917 static void kaweth_tx_timeout(struct net_device *net)
919 struct kaweth_device *kaweth = netdev_priv(net);
921 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
922 kaweth->stats.tx_errors++;
923 netif_trans_update(net);
925 usb_unlink_urb(kaweth->tx_urb);
928 /****************************************************************
930 ****************************************************************/
931 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
933 struct kaweth_device *kaweth = usb_get_intfdata(intf);
936 spin_lock_irqsave(&kaweth->device_lock, flags);
937 kaweth->status |= KAWETH_STATUS_SUSPENDING;
938 spin_unlock_irqrestore(&kaweth->device_lock, flags);
940 kaweth_kill_urbs(kaweth);
944 /****************************************************************
946 ****************************************************************/
947 static int kaweth_resume(struct usb_interface *intf)
949 struct kaweth_device *kaweth = usb_get_intfdata(intf);
952 spin_lock_irqsave(&kaweth->device_lock, flags);
953 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
954 spin_unlock_irqrestore(&kaweth->device_lock, flags);
958 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
959 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
964 /****************************************************************
966 ****************************************************************/
969 static const struct net_device_ops kaweth_netdev_ops = {
970 .ndo_open = kaweth_open,
971 .ndo_stop = kaweth_close,
972 .ndo_start_xmit = kaweth_start_xmit,
973 .ndo_tx_timeout = kaweth_tx_timeout,
974 .ndo_set_rx_mode = kaweth_set_rx_mode,
975 .ndo_get_stats = kaweth_netdev_stats,
976 .ndo_change_mtu = eth_change_mtu,
977 .ndo_set_mac_address = eth_mac_addr,
978 .ndo_validate_addr = eth_validate_addr,
981 static int kaweth_probe(
982 struct usb_interface *intf,
983 const struct usb_device_id *id /* from id_table */
986 struct device *dev = &intf->dev;
987 struct usb_device *udev = interface_to_usbdev(intf);
988 struct kaweth_device *kaweth;
989 struct net_device *netdev;
990 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
995 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
996 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
997 le16_to_cpu(udev->descriptor.idProduct),
998 le16_to_cpu(udev->descriptor.bcdDevice));
1000 dev_dbg(dev, "Device at %p\n", udev);
1002 dev_dbg(dev, "Descriptor length: %x type: %x\n",
1003 (int)udev->descriptor.bLength,
1004 (int)udev->descriptor.bDescriptorType);
1006 netdev = alloc_etherdev(sizeof(*kaweth));
1010 kaweth = netdev_priv(netdev);
1012 kaweth->net = netdev;
1013 kaweth->intf = intf;
1015 spin_lock_init(&kaweth->device_lock);
1016 init_waitqueue_head(&kaweth->term_wait);
1018 dev_dbg(dev, "Resetting.\n");
1020 kaweth_reset(kaweth);
1023 * If high byte of bcdDevice is nonzero, firmware is already
1024 * downloaded. Don't try to do it again, or we'll hang the device.
1027 if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1028 dev_info(dev, "Firmware present in device.\n");
1030 /* Download the firmware */
1031 dev_info(dev, "Downloading firmware...\n");
1032 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1033 if (!kaweth->firmware_buf) {
1035 goto err_free_netdev;
1037 if ((result = kaweth_download_firmware(kaweth,
1041 dev_err(dev, "Error downloading firmware (%d)\n",
1046 if ((result = kaweth_download_firmware(kaweth,
1050 dev_err(dev, "Error downloading firmware fix (%d)\n",
1055 if ((result = kaweth_download_firmware(kaweth,
1059 dev_err(dev, "Error downloading trigger code (%d)\n",
1065 if ((result = kaweth_download_firmware(kaweth,
1069 dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1074 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1075 dev_err(dev, "Error triggering firmware (%d)\n", result);
1079 /* Device will now disappear for a moment... */
1080 dev_info(dev, "Firmware loaded. I'll be back...\n");
1082 free_page((unsigned long)kaweth->firmware_buf);
1083 free_netdev(netdev);
1087 result = kaweth_read_configuration(kaweth);
1090 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1091 goto err_free_netdev;
1094 dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1095 dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1096 dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1097 dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1099 if(!memcmp(&kaweth->configuration.hw_addr,
1101 sizeof(bcast_addr))) {
1102 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1103 goto err_free_netdev;
1106 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1107 dev_dbg(dev, "Error setting URB size\n");
1108 goto err_free_netdev;
1111 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1112 dev_err(dev, "Error setting SOFS wait\n");
1113 goto err_free_netdev;
1116 result = kaweth_set_receive_filter(kaweth,
1117 KAWETH_PACKET_FILTER_DIRECTED |
1118 KAWETH_PACKET_FILTER_BROADCAST |
1119 KAWETH_PACKET_FILTER_MULTICAST);
1122 dev_err(dev, "Error setting receive filter\n");
1123 goto err_free_netdev;
1126 dev_dbg(dev, "Initializing net device.\n");
1128 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1129 if (!kaweth->tx_urb)
1130 goto err_free_netdev;
1131 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1132 if (!kaweth->rx_urb)
1134 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1135 if (!kaweth->irq_urb)
1138 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1141 &kaweth->intbufferhandle);
1142 if (!kaweth->intbuffer)
1143 goto err_tx_and_rx_and_irq;
1144 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
1147 &kaweth->rxbufferhandle);
1148 if (!kaweth->rx_buf)
1149 goto err_all_but_rxbuf;
1151 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1152 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1153 sizeof(kaweth->configuration.hw_addr));
1155 netdev->netdev_ops = &kaweth_netdev_ops;
1156 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1157 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1158 netdev->ethtool_ops = &ops;
1160 /* kaweth is zeroed as part of alloc_netdev */
1161 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1162 usb_set_intfdata(intf, kaweth);
1164 SET_NETDEV_DEV(netdev, dev);
1165 if (register_netdev(netdev) != 0) {
1166 dev_err(dev, "Error registering netdev.\n");
1170 dev_info(dev, "kaweth interface created at %s\n",
1176 usb_set_intfdata(intf, NULL);
1177 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1179 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1180 err_tx_and_rx_and_irq:
1181 usb_free_urb(kaweth->irq_urb);
1183 usb_free_urb(kaweth->rx_urb);
1185 usb_free_urb(kaweth->tx_urb);
1187 free_netdev(netdev);
1192 /****************************************************************
1194 ****************************************************************/
1195 static void kaweth_disconnect(struct usb_interface *intf)
1197 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1198 struct net_device *netdev;
1200 usb_set_intfdata(intf, NULL);
1202 dev_warn(&intf->dev, "unregistering non-existent device\n");
1205 netdev = kaweth->net;
1207 netdev_dbg(kaweth->net, "Unregistering net device\n");
1208 unregister_netdev(netdev);
1210 usb_free_urb(kaweth->rx_urb);
1211 usb_free_urb(kaweth->tx_urb);
1212 usb_free_urb(kaweth->irq_urb);
1214 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1215 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1217 free_netdev(netdev);
1221 // FIXME this completion stuff is a modified clone of
1222 // an OLD version of some stuff in usb.c ...
1223 struct usb_api_data {
1224 wait_queue_head_t wqh;
1228 /*-------------------------------------------------------------------*
1229 * completion handler for compatibility wrappers (sync control/bulk) *
1230 *-------------------------------------------------------------------*/
1231 static void usb_api_blocking_completion(struct urb *urb)
1233 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1239 /*-------------------------------------------------------------------*
1240 * COMPATIBILITY STUFF *
1241 *-------------------------------------------------------------------*/
1243 // Starts urb and waits for completion or timeout
1244 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1246 struct usb_api_data awd;
1249 init_waitqueue_head(&awd.wqh);
1252 urb->context = &awd;
1253 status = usb_submit_urb(urb, GFP_ATOMIC);
1255 // something went wrong
1260 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1262 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1263 usb_kill_urb(urb); // remove urb safely
1264 status = -ETIMEDOUT;
1267 status = urb->status;
1270 if (actual_length) {
1271 *actual_length = urb->actual_length;
1278 /*-------------------------------------------------------------------*/
1279 // returns status (negative) or length (positive)
1280 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1282 struct usb_ctrlrequest *cmd, void *data,
1283 int len, int timeout)
1287 int length = 0; /* shut up GCC */
1289 urb = usb_alloc_urb(0, GFP_ATOMIC);
1293 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1294 len, usb_api_blocking_completion, NULL);
1296 retv = usb_start_wait_urb(urb, timeout, &length);
1305 module_usb_driver(kaweth_driver);