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 <linux/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 const 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;
247 /****************************************************************
249 ****************************************************************/
250 static int kaweth_control(struct kaweth_device *kaweth,
260 struct usb_ctrlrequest *dr;
264 netdev_dbg(kaweth->net, "in_interrupt()\n");
268 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
272 dr->bRequestType = requesttype;
273 dr->bRequest = request;
274 dr->wValue = cpu_to_le16(value);
275 dr->wIndex = cpu_to_le16(index);
276 dr->wLength = cpu_to_le16(size);
278 retval = kaweth_internal_control_msg(kaweth->dev,
289 /****************************************************************
290 * kaweth_read_configuration
291 ****************************************************************/
292 static int kaweth_read_configuration(struct kaweth_device *kaweth)
296 retval = kaweth_control(kaweth,
297 usb_rcvctrlpipe(kaweth->dev, 0),
298 KAWETH_COMMAND_GET_ETHERNET_DESC,
299 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
302 (void *)&kaweth->configuration,
303 sizeof(kaweth->configuration),
304 KAWETH_CONTROL_TIMEOUT);
309 /****************************************************************
310 * kaweth_set_urb_size
311 ****************************************************************/
312 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
316 netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
318 retval = kaweth_control(kaweth,
319 usb_sndctrlpipe(kaweth->dev, 0),
320 KAWETH_COMMAND_SET_URB_SIZE,
321 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
324 (void *)&kaweth->scratch,
326 KAWETH_CONTROL_TIMEOUT);
331 /****************************************************************
332 * kaweth_set_sofs_wait
333 ****************************************************************/
334 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
338 netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
340 retval = kaweth_control(kaweth,
341 usb_sndctrlpipe(kaweth->dev, 0),
342 KAWETH_COMMAND_SET_SOFS_WAIT,
343 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
346 (void *)&kaweth->scratch,
348 KAWETH_CONTROL_TIMEOUT);
353 /****************************************************************
354 * kaweth_set_receive_filter
355 ****************************************************************/
356 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
357 __u16 receive_filter)
361 netdev_dbg(kaweth->net, "Set receive filter to %d\n",
362 (unsigned)receive_filter);
364 retval = kaweth_control(kaweth,
365 usb_sndctrlpipe(kaweth->dev, 0),
366 KAWETH_COMMAND_SET_PACKET_FILTER,
367 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
370 (void *)&kaweth->scratch,
372 KAWETH_CONTROL_TIMEOUT);
377 /****************************************************************
378 * kaweth_download_firmware
379 ****************************************************************/
380 static int kaweth_download_firmware(struct kaweth_device *kaweth,
385 const struct firmware *fw;
389 ret = reject_firmware(&fw, fwname, &kaweth->dev->dev);
391 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
395 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
396 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
398 release_firmware(fw);
402 memcpy(kaweth->firmware_buf, fw->data, fw->size);
404 release_firmware(fw);
406 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
407 kaweth->firmware_buf[3] = data_len >> 8;
408 kaweth->firmware_buf[4] = type;
409 kaweth->firmware_buf[5] = interrupt;
411 netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
412 kaweth->firmware_buf[2]);
414 netdev_dbg(kaweth->net,
415 "Downloading firmware at %p to kaweth device at %p\n",
416 kaweth->firmware_buf, kaweth);
417 netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
419 return kaweth_control(kaweth,
420 usb_sndctrlpipe(kaweth->dev, 0),
422 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
425 (void *)kaweth->firmware_buf,
427 KAWETH_CONTROL_TIMEOUT);
430 /****************************************************************
431 * kaweth_trigger_firmware
432 ****************************************************************/
433 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
436 kaweth->firmware_buf[0] = 0xB6;
437 kaweth->firmware_buf[1] = 0xC3;
438 kaweth->firmware_buf[2] = 0x01;
439 kaweth->firmware_buf[3] = 0x00;
440 kaweth->firmware_buf[4] = 0x06;
441 kaweth->firmware_buf[5] = interrupt;
442 kaweth->firmware_buf[6] = 0x00;
443 kaweth->firmware_buf[7] = 0x00;
445 return kaweth_control(kaweth,
446 usb_sndctrlpipe(kaweth->dev, 0),
448 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
451 (void *)kaweth->firmware_buf,
453 KAWETH_CONTROL_TIMEOUT);
456 /****************************************************************
458 ****************************************************************/
459 static int kaweth_reset(struct kaweth_device *kaweth)
463 result = usb_reset_configuration(kaweth->dev);
466 netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
471 static void kaweth_usb_receive(struct urb *);
472 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
474 /****************************************************************
476 *****************************************************************/
478 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
482 status = usb_submit_urb (kaweth->irq_urb, mf);
483 if (unlikely(status == -ENOMEM)) {
484 kaweth->suspend_lowmem_ctrl = 1;
485 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
487 kaweth->suspend_lowmem_ctrl = 0;
491 dev_err(&kaweth->intf->dev,
492 "can't resubmit intr, %s-%s, status %d\n",
493 kaweth->dev->bus->bus_name,
494 kaweth->dev->devpath, status);
497 static void int_callback(struct urb *u)
499 struct kaweth_device *kaweth = u->context;
501 int status = u->status;
504 case 0: /* success */
506 case -ECONNRESET: /* unlink */
510 /* -EPIPE: should clear the halt */
515 /* we check the link state to report changes */
516 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
518 netif_carrier_on(kaweth->net);
520 netif_carrier_off(kaweth->net);
522 kaweth->linkstate = act_state;
525 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
528 static void kaweth_resubmit_tl(struct work_struct *work)
530 struct kaweth_device *kaweth =
531 container_of(work, struct kaweth_device, lowmem_work.work);
533 if (IS_BLOCKED(kaweth->status))
536 if (kaweth->suspend_lowmem_rx)
537 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
539 if (kaweth->suspend_lowmem_ctrl)
540 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
544 /****************************************************************
545 * kaweth_resubmit_rx_urb
546 ****************************************************************/
547 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
552 usb_fill_bulk_urb(kaweth->rx_urb,
554 usb_rcvbulkpipe(kaweth->dev, 1),
559 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
560 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
562 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
563 if (result == -ENOMEM) {
564 kaweth->suspend_lowmem_rx = 1;
565 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
567 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
570 kaweth->suspend_lowmem_rx = 0;
576 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
578 /****************************************************************
580 ****************************************************************/
581 static void kaweth_usb_receive(struct urb *urb)
583 struct device *dev = &urb->dev->dev;
584 struct kaweth_device *kaweth = urb->context;
585 struct net_device *net = kaweth->net;
586 int status = urb->status;
588 int count = urb->actual_length;
589 int count2 = urb->transfer_buffer_length;
591 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
595 if (unlikely(status == -EPIPE)) {
596 net->stats.rx_errors++;
598 wake_up(&kaweth->term_wait);
599 dev_dbg(dev, "Status was -EPIPE.\n");
602 if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
603 /* we are killed - set a flag and wake the disconnect handler */
605 wake_up(&kaweth->term_wait);
606 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
609 if (unlikely(status == -EPROTO || status == -ETIME ||
610 status == -EILSEQ)) {
611 net->stats.rx_errors++;
612 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
615 if (unlikely(status == -EOVERFLOW)) {
616 net->stats.rx_errors++;
617 dev_dbg(dev, "Status was -EOVERFLOW.\n");
619 spin_lock(&kaweth->device_lock);
620 if (IS_BLOCKED(kaweth->status)) {
621 spin_unlock(&kaweth->device_lock);
624 spin_unlock(&kaweth->device_lock);
626 if(status && status != -EREMOTEIO && count != 1) {
627 dev_err(&kaweth->intf->dev,
628 "%s RX status: %d count: %d packet_len: %d\n",
629 net->name, status, count, (int)pkt_len);
630 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
634 if(kaweth->net && (count > 2)) {
635 if(pkt_len > (count - 2)) {
636 dev_err(&kaweth->intf->dev,
637 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
639 dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
641 dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
642 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
646 if(!(skb = dev_alloc_skb(pkt_len+2))) {
647 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
651 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
653 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
655 skb_put(skb, pkt_len);
657 skb->protocol = eth_type_trans(skb, net);
661 net->stats.rx_packets++;
662 net->stats.rx_bytes += pkt_len;
665 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
668 /****************************************************************
670 ****************************************************************/
671 static int kaweth_open(struct net_device *net)
673 struct kaweth_device *kaweth = netdev_priv(net);
676 res = usb_autopm_get_interface(kaweth->intf);
678 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
681 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
688 usb_rcvintpipe(kaweth->dev, 3),
693 250); /* overriding the descriptor */
694 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
695 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
697 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
699 usb_kill_urb(kaweth->rx_urb);
704 netif_start_queue(net);
706 kaweth_async_set_rx_mode(kaweth);
710 usb_autopm_put_interface(kaweth->intf);
714 /****************************************************************
716 ****************************************************************/
717 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
719 usb_kill_urb(kaweth->irq_urb);
720 usb_kill_urb(kaweth->rx_urb);
721 usb_kill_urb(kaweth->tx_urb);
723 cancel_delayed_work_sync(&kaweth->lowmem_work);
725 /* a scheduled work may have resubmitted,
727 usb_kill_urb(kaweth->irq_urb);
728 usb_kill_urb(kaweth->rx_urb);
731 /****************************************************************
733 ****************************************************************/
734 static int kaweth_close(struct net_device *net)
736 struct kaweth_device *kaweth = netdev_priv(net);
738 netif_stop_queue(net);
741 kaweth->status |= KAWETH_STATUS_CLOSING;
743 kaweth_kill_urbs(kaweth);
745 kaweth->status &= ~KAWETH_STATUS_CLOSING;
747 usb_autopm_put_interface(kaweth->intf);
752 static u32 kaweth_get_link(struct net_device *dev)
754 struct kaweth_device *kaweth = netdev_priv(dev);
756 return kaweth->linkstate;
759 static const struct ethtool_ops ops = {
760 .get_link = kaweth_get_link
763 /****************************************************************
764 * kaweth_usb_transmit_complete
765 ****************************************************************/
766 static void kaweth_usb_transmit_complete(struct urb *urb)
768 struct kaweth_device *kaweth = urb->context;
769 struct sk_buff *skb = kaweth->tx_skb;
770 int status = urb->status;
772 if (unlikely(status != 0))
773 if (status != -ENOENT)
774 dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
775 kaweth->net->name, status);
777 netif_wake_queue(kaweth->net);
778 dev_kfree_skb_irq(skb);
781 /****************************************************************
783 ****************************************************************/
784 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
785 struct net_device *net)
787 struct kaweth_device *kaweth = netdev_priv(net);
788 __le16 *private_header;
792 spin_lock_irq(&kaweth->device_lock);
794 kaweth_async_set_rx_mode(kaweth);
795 netif_stop_queue(net);
796 if (IS_BLOCKED(kaweth->status)) {
800 /* We now decide whether we can put our special header into the sk_buff */
801 if (skb_cow_head(skb, 2)) {
802 net->stats.tx_errors++;
803 netif_start_queue(net);
804 spin_unlock_irq(&kaweth->device_lock);
805 dev_kfree_skb_any(skb);
809 private_header = __skb_push(skb, 2);
810 *private_header = cpu_to_le16(skb->len-2);
811 kaweth->tx_skb = skb;
813 usb_fill_bulk_urb(kaweth->tx_urb,
815 usb_sndbulkpipe(kaweth->dev, 2),
818 kaweth_usb_transmit_complete,
822 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
824 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
826 net->stats.tx_errors++;
828 netif_start_queue(net);
829 dev_kfree_skb_irq(skb);
833 net->stats.tx_packets++;
834 net->stats.tx_bytes += skb->len;
837 spin_unlock_irq(&kaweth->device_lock);
842 /****************************************************************
844 ****************************************************************/
845 static void kaweth_set_rx_mode(struct net_device *net)
847 struct kaweth_device *kaweth = netdev_priv(net);
849 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
850 KAWETH_PACKET_FILTER_BROADCAST |
851 KAWETH_PACKET_FILTER_MULTICAST;
853 netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
855 netif_stop_queue(net);
857 if (net->flags & IFF_PROMISC) {
858 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
860 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
861 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
864 kaweth->packet_filter_bitmap = packet_filter_bitmap;
865 netif_wake_queue(net);
868 /****************************************************************
869 * kaweth_async_set_rx_mode
870 ****************************************************************/
871 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
874 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
876 kaweth->packet_filter_bitmap = 0;
877 if (packet_filter_bitmap == 0)
883 result = kaweth_control(kaweth,
884 usb_sndctrlpipe(kaweth->dev, 0),
885 KAWETH_COMMAND_SET_PACKET_FILTER,
886 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
887 packet_filter_bitmap,
889 (void *)&kaweth->scratch,
891 KAWETH_CONTROL_TIMEOUT);
894 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
898 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
899 packet_filter_bitmap);
903 /****************************************************************
905 ****************************************************************/
906 static void kaweth_tx_timeout(struct net_device *net)
908 struct kaweth_device *kaweth = netdev_priv(net);
910 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
911 net->stats.tx_errors++;
912 netif_trans_update(net);
914 usb_unlink_urb(kaweth->tx_urb);
917 /****************************************************************
919 ****************************************************************/
920 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
922 struct kaweth_device *kaweth = usb_get_intfdata(intf);
925 spin_lock_irqsave(&kaweth->device_lock, flags);
926 kaweth->status |= KAWETH_STATUS_SUSPENDING;
927 spin_unlock_irqrestore(&kaweth->device_lock, flags);
929 kaweth_kill_urbs(kaweth);
933 /****************************************************************
935 ****************************************************************/
936 static int kaweth_resume(struct usb_interface *intf)
938 struct kaweth_device *kaweth = usb_get_intfdata(intf);
941 spin_lock_irqsave(&kaweth->device_lock, flags);
942 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
943 spin_unlock_irqrestore(&kaweth->device_lock, flags);
947 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
948 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
953 /****************************************************************
955 ****************************************************************/
958 static const struct net_device_ops kaweth_netdev_ops = {
959 .ndo_open = kaweth_open,
960 .ndo_stop = kaweth_close,
961 .ndo_start_xmit = kaweth_start_xmit,
962 .ndo_tx_timeout = kaweth_tx_timeout,
963 .ndo_set_rx_mode = kaweth_set_rx_mode,
964 .ndo_set_mac_address = eth_mac_addr,
965 .ndo_validate_addr = eth_validate_addr,
968 static int kaweth_probe(
969 struct usb_interface *intf,
970 const struct usb_device_id *id /* from id_table */
973 struct device *dev = &intf->dev;
974 struct usb_device *udev = interface_to_usbdev(intf);
975 struct kaweth_device *kaweth;
976 struct net_device *netdev;
977 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
982 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
983 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
984 le16_to_cpu(udev->descriptor.idProduct),
985 le16_to_cpu(udev->descriptor.bcdDevice));
987 dev_dbg(dev, "Device at %p\n", udev);
989 dev_dbg(dev, "Descriptor length: %x type: %x\n",
990 (int)udev->descriptor.bLength,
991 (int)udev->descriptor.bDescriptorType);
993 netdev = alloc_etherdev(sizeof(*kaweth));
997 kaweth = netdev_priv(netdev);
999 kaweth->net = netdev;
1000 kaweth->intf = intf;
1002 spin_lock_init(&kaweth->device_lock);
1003 init_waitqueue_head(&kaweth->term_wait);
1005 dev_dbg(dev, "Resetting.\n");
1007 kaweth_reset(kaweth);
1010 * If high byte of bcdDevice is nonzero, firmware is already
1011 * downloaded. Don't try to do it again, or we'll hang the device.
1014 if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1015 dev_info(dev, "Firmware present in device.\n");
1017 /* Download the firmware */
1018 dev_info(dev, "Downloading firmware...\n");
1019 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1020 if (!kaweth->firmware_buf) {
1022 goto err_free_netdev;
1024 if ((result = kaweth_download_firmware(kaweth,
1028 dev_err(dev, "Error downloading firmware (%d)\n",
1033 if ((result = kaweth_download_firmware(kaweth,
1037 dev_err(dev, "Error downloading firmware fix (%d)\n",
1042 if ((result = kaweth_download_firmware(kaweth,
1046 dev_err(dev, "Error downloading trigger code (%d)\n",
1052 if ((result = kaweth_download_firmware(kaweth,
1056 dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1061 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1062 dev_err(dev, "Error triggering firmware (%d)\n", result);
1066 /* Device will now disappear for a moment... */
1067 dev_info(dev, "Firmware loaded. I'll be back...\n");
1069 free_page((unsigned long)kaweth->firmware_buf);
1070 free_netdev(netdev);
1074 result = kaweth_read_configuration(kaweth);
1077 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1078 goto err_free_netdev;
1081 dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1082 dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1083 dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1084 dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1086 if(!memcmp(&kaweth->configuration.hw_addr,
1088 sizeof(bcast_addr))) {
1089 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1090 goto err_free_netdev;
1093 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1094 dev_dbg(dev, "Error setting URB size\n");
1095 goto err_free_netdev;
1098 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1099 dev_err(dev, "Error setting SOFS wait\n");
1100 goto err_free_netdev;
1103 result = kaweth_set_receive_filter(kaweth,
1104 KAWETH_PACKET_FILTER_DIRECTED |
1105 KAWETH_PACKET_FILTER_BROADCAST |
1106 KAWETH_PACKET_FILTER_MULTICAST);
1109 dev_err(dev, "Error setting receive filter\n");
1110 goto err_free_netdev;
1113 dev_dbg(dev, "Initializing net device.\n");
1115 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1116 if (!kaweth->tx_urb)
1117 goto err_free_netdev;
1118 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1119 if (!kaweth->rx_urb)
1121 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1122 if (!kaweth->irq_urb)
1125 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1128 &kaweth->intbufferhandle);
1129 if (!kaweth->intbuffer)
1130 goto err_tx_and_rx_and_irq;
1131 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
1134 &kaweth->rxbufferhandle);
1135 if (!kaweth->rx_buf)
1136 goto err_all_but_rxbuf;
1138 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1139 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1140 sizeof(kaweth->configuration.hw_addr));
1142 netdev->netdev_ops = &kaweth_netdev_ops;
1143 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1144 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1145 netdev->ethtool_ops = &ops;
1147 /* kaweth is zeroed as part of alloc_netdev */
1148 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1149 usb_set_intfdata(intf, kaweth);
1151 SET_NETDEV_DEV(netdev, dev);
1152 if (register_netdev(netdev) != 0) {
1153 dev_err(dev, "Error registering netdev.\n");
1157 dev_info(dev, "kaweth interface created at %s\n",
1163 usb_set_intfdata(intf, NULL);
1164 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1166 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1167 err_tx_and_rx_and_irq:
1168 usb_free_urb(kaweth->irq_urb);
1170 usb_free_urb(kaweth->rx_urb);
1172 usb_free_urb(kaweth->tx_urb);
1174 free_netdev(netdev);
1179 /****************************************************************
1181 ****************************************************************/
1182 static void kaweth_disconnect(struct usb_interface *intf)
1184 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1185 struct net_device *netdev;
1187 usb_set_intfdata(intf, NULL);
1189 dev_warn(&intf->dev, "unregistering non-existent device\n");
1192 netdev = kaweth->net;
1194 netdev_dbg(kaweth->net, "Unregistering net device\n");
1195 unregister_netdev(netdev);
1197 usb_free_urb(kaweth->rx_urb);
1198 usb_free_urb(kaweth->tx_urb);
1199 usb_free_urb(kaweth->irq_urb);
1201 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1202 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1204 free_netdev(netdev);
1208 // FIXME this completion stuff is a modified clone of
1209 // an OLD version of some stuff in usb.c ...
1210 struct usb_api_data {
1211 wait_queue_head_t wqh;
1215 /*-------------------------------------------------------------------*
1216 * completion handler for compatibility wrappers (sync control/bulk) *
1217 *-------------------------------------------------------------------*/
1218 static void usb_api_blocking_completion(struct urb *urb)
1220 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1226 /*-------------------------------------------------------------------*
1227 * COMPATIBILITY STUFF *
1228 *-------------------------------------------------------------------*/
1230 // Starts urb and waits for completion or timeout
1231 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1233 struct usb_api_data awd;
1236 init_waitqueue_head(&awd.wqh);
1239 urb->context = &awd;
1240 status = usb_submit_urb(urb, GFP_ATOMIC);
1242 // something went wrong
1247 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1249 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1250 usb_kill_urb(urb); // remove urb safely
1251 status = -ETIMEDOUT;
1254 status = urb->status;
1257 if (actual_length) {
1258 *actual_length = urb->actual_length;
1265 /*-------------------------------------------------------------------*/
1266 // returns status (negative) or length (positive)
1267 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1269 struct usb_ctrlrequest *cmd, void *data,
1270 int len, int timeout)
1274 int length = 0; /* shut up GCC */
1276 urb = usb_alloc_urb(0, GFP_ATOMIC);
1280 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1281 len, usb_api_blocking_completion, NULL);
1283 retv = usb_start_wait_urb(urb, timeout, &length);
1292 module_usb_driver(kaweth_driver);