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_cloned(skb) || skb_headroom(skb) < 2) {
804 /* no such luck - we make our own */
805 struct sk_buff *copied_skb;
806 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
807 dev_kfree_skb_irq(skb);
810 kaweth->stats.tx_errors++;
811 netif_start_queue(net);
812 spin_unlock_irq(&kaweth->device_lock);
817 private_header = (__le16 *)__skb_push(skb, 2);
818 *private_header = cpu_to_le16(skb->len-2);
819 kaweth->tx_skb = skb;
821 usb_fill_bulk_urb(kaweth->tx_urb,
823 usb_sndbulkpipe(kaweth->dev, 2),
826 kaweth_usb_transmit_complete,
830 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
832 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
834 kaweth->stats.tx_errors++;
836 netif_start_queue(net);
837 dev_kfree_skb_irq(skb);
841 kaweth->stats.tx_packets++;
842 kaweth->stats.tx_bytes += skb->len;
845 spin_unlock_irq(&kaweth->device_lock);
850 /****************************************************************
852 ****************************************************************/
853 static void kaweth_set_rx_mode(struct net_device *net)
855 struct kaweth_device *kaweth = netdev_priv(net);
857 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
858 KAWETH_PACKET_FILTER_BROADCAST |
859 KAWETH_PACKET_FILTER_MULTICAST;
861 netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
863 netif_stop_queue(net);
865 if (net->flags & IFF_PROMISC) {
866 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
868 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
869 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
872 kaweth->packet_filter_bitmap = packet_filter_bitmap;
873 netif_wake_queue(net);
876 /****************************************************************
877 * kaweth_async_set_rx_mode
878 ****************************************************************/
879 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
882 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
884 kaweth->packet_filter_bitmap = 0;
885 if (packet_filter_bitmap == 0)
891 result = kaweth_control(kaweth,
892 usb_sndctrlpipe(kaweth->dev, 0),
893 KAWETH_COMMAND_SET_PACKET_FILTER,
894 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
895 packet_filter_bitmap,
897 (void *)&kaweth->scratch,
899 KAWETH_CONTROL_TIMEOUT);
902 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
906 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
907 packet_filter_bitmap);
911 /****************************************************************
912 * kaweth_netdev_stats
913 ****************************************************************/
914 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
916 struct kaweth_device *kaweth = netdev_priv(dev);
917 return &kaweth->stats;
920 /****************************************************************
922 ****************************************************************/
923 static void kaweth_tx_timeout(struct net_device *net)
925 struct kaweth_device *kaweth = netdev_priv(net);
927 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
928 kaweth->stats.tx_errors++;
929 netif_trans_update(net);
931 usb_unlink_urb(kaweth->tx_urb);
934 /****************************************************************
936 ****************************************************************/
937 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
939 struct kaweth_device *kaweth = usb_get_intfdata(intf);
942 spin_lock_irqsave(&kaweth->device_lock, flags);
943 kaweth->status |= KAWETH_STATUS_SUSPENDING;
944 spin_unlock_irqrestore(&kaweth->device_lock, flags);
946 kaweth_kill_urbs(kaweth);
950 /****************************************************************
952 ****************************************************************/
953 static int kaweth_resume(struct usb_interface *intf)
955 struct kaweth_device *kaweth = usb_get_intfdata(intf);
958 spin_lock_irqsave(&kaweth->device_lock, flags);
959 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
960 spin_unlock_irqrestore(&kaweth->device_lock, flags);
964 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
965 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
970 /****************************************************************
972 ****************************************************************/
975 static const struct net_device_ops kaweth_netdev_ops = {
976 .ndo_open = kaweth_open,
977 .ndo_stop = kaweth_close,
978 .ndo_start_xmit = kaweth_start_xmit,
979 .ndo_tx_timeout = kaweth_tx_timeout,
980 .ndo_set_rx_mode = kaweth_set_rx_mode,
981 .ndo_get_stats = kaweth_netdev_stats,
982 .ndo_change_mtu = eth_change_mtu,
983 .ndo_set_mac_address = eth_mac_addr,
984 .ndo_validate_addr = eth_validate_addr,
987 static int kaweth_probe(
988 struct usb_interface *intf,
989 const struct usb_device_id *id /* from id_table */
992 struct device *dev = &intf->dev;
993 struct usb_device *udev = interface_to_usbdev(intf);
994 struct kaweth_device *kaweth;
995 struct net_device *netdev;
996 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1001 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
1002 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
1003 le16_to_cpu(udev->descriptor.idProduct),
1004 le16_to_cpu(udev->descriptor.bcdDevice));
1006 dev_dbg(dev, "Device at %p\n", udev);
1008 dev_dbg(dev, "Descriptor length: %x type: %x\n",
1009 (int)udev->descriptor.bLength,
1010 (int)udev->descriptor.bDescriptorType);
1012 netdev = alloc_etherdev(sizeof(*kaweth));
1016 kaweth = netdev_priv(netdev);
1018 kaweth->net = netdev;
1019 kaweth->intf = intf;
1021 spin_lock_init(&kaweth->device_lock);
1022 init_waitqueue_head(&kaweth->term_wait);
1024 dev_dbg(dev, "Resetting.\n");
1026 kaweth_reset(kaweth);
1029 * If high byte of bcdDevice is nonzero, firmware is already
1030 * downloaded. Don't try to do it again, or we'll hang the device.
1033 if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1034 dev_info(dev, "Firmware present in device.\n");
1036 /* Download the firmware */
1037 dev_info(dev, "Downloading firmware...\n");
1038 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1039 if (!kaweth->firmware_buf) {
1041 goto err_free_netdev;
1043 if ((result = kaweth_download_firmware(kaweth,
1047 dev_err(dev, "Error downloading firmware (%d)\n",
1052 if ((result = kaweth_download_firmware(kaweth,
1056 dev_err(dev, "Error downloading firmware fix (%d)\n",
1061 if ((result = kaweth_download_firmware(kaweth,
1065 dev_err(dev, "Error downloading trigger code (%d)\n",
1071 if ((result = kaweth_download_firmware(kaweth,
1075 dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1080 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1081 dev_err(dev, "Error triggering firmware (%d)\n", result);
1085 /* Device will now disappear for a moment... */
1086 dev_info(dev, "Firmware loaded. I'll be back...\n");
1088 free_page((unsigned long)kaweth->firmware_buf);
1089 free_netdev(netdev);
1093 result = kaweth_read_configuration(kaweth);
1096 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1097 goto err_free_netdev;
1100 dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1101 dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1102 dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1103 dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1105 if(!memcmp(&kaweth->configuration.hw_addr,
1107 sizeof(bcast_addr))) {
1108 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1109 goto err_free_netdev;
1112 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1113 dev_dbg(dev, "Error setting URB size\n");
1114 goto err_free_netdev;
1117 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1118 dev_err(dev, "Error setting SOFS wait\n");
1119 goto err_free_netdev;
1122 result = kaweth_set_receive_filter(kaweth,
1123 KAWETH_PACKET_FILTER_DIRECTED |
1124 KAWETH_PACKET_FILTER_BROADCAST |
1125 KAWETH_PACKET_FILTER_MULTICAST);
1128 dev_err(dev, "Error setting receive filter\n");
1129 goto err_free_netdev;
1132 dev_dbg(dev, "Initializing net device.\n");
1134 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1135 if (!kaweth->tx_urb)
1136 goto err_free_netdev;
1137 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1138 if (!kaweth->rx_urb)
1140 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1141 if (!kaweth->irq_urb)
1144 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1147 &kaweth->intbufferhandle);
1148 if (!kaweth->intbuffer)
1149 goto err_tx_and_rx_and_irq;
1150 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
1153 &kaweth->rxbufferhandle);
1154 if (!kaweth->rx_buf)
1155 goto err_all_but_rxbuf;
1157 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1158 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1159 sizeof(kaweth->configuration.hw_addr));
1161 netdev->netdev_ops = &kaweth_netdev_ops;
1162 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1163 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1164 netdev->ethtool_ops = &ops;
1166 /* kaweth is zeroed as part of alloc_netdev */
1167 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1168 usb_set_intfdata(intf, kaweth);
1170 SET_NETDEV_DEV(netdev, dev);
1171 if (register_netdev(netdev) != 0) {
1172 dev_err(dev, "Error registering netdev.\n");
1176 dev_info(dev, "kaweth interface created at %s\n",
1182 usb_set_intfdata(intf, NULL);
1183 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1185 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1186 err_tx_and_rx_and_irq:
1187 usb_free_urb(kaweth->irq_urb);
1189 usb_free_urb(kaweth->rx_urb);
1191 usb_free_urb(kaweth->tx_urb);
1193 free_netdev(netdev);
1198 /****************************************************************
1200 ****************************************************************/
1201 static void kaweth_disconnect(struct usb_interface *intf)
1203 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1204 struct net_device *netdev;
1206 usb_set_intfdata(intf, NULL);
1208 dev_warn(&intf->dev, "unregistering non-existent device\n");
1211 netdev = kaweth->net;
1213 netdev_dbg(kaweth->net, "Unregistering net device\n");
1214 unregister_netdev(netdev);
1216 usb_free_urb(kaweth->rx_urb);
1217 usb_free_urb(kaweth->tx_urb);
1218 usb_free_urb(kaweth->irq_urb);
1220 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1221 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1223 free_netdev(netdev);
1227 // FIXME this completion stuff is a modified clone of
1228 // an OLD version of some stuff in usb.c ...
1229 struct usb_api_data {
1230 wait_queue_head_t wqh;
1234 /*-------------------------------------------------------------------*
1235 * completion handler for compatibility wrappers (sync control/bulk) *
1236 *-------------------------------------------------------------------*/
1237 static void usb_api_blocking_completion(struct urb *urb)
1239 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1245 /*-------------------------------------------------------------------*
1246 * COMPATIBILITY STUFF *
1247 *-------------------------------------------------------------------*/
1249 // Starts urb and waits for completion or timeout
1250 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1252 struct usb_api_data awd;
1255 init_waitqueue_head(&awd.wqh);
1258 urb->context = &awd;
1259 status = usb_submit_urb(urb, GFP_ATOMIC);
1261 // something went wrong
1266 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1268 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1269 usb_kill_urb(urb); // remove urb safely
1270 status = -ETIMEDOUT;
1273 status = urb->status;
1276 if (actual_length) {
1277 *actual_length = urb->actual_length;
1284 /*-------------------------------------------------------------------*/
1285 // returns status (negative) or length (positive)
1286 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1288 struct usb_ctrlrequest *cmd, void *data,
1289 int len, int timeout)
1293 int length = 0; /* shut up GCC */
1295 urb = usb_alloc_urb(0, GFP_ATOMIC);
1299 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1300 len, usb_api_blocking_completion, NULL);
1302 retv = usb_start_wait_urb(urb, timeout, &length);
1311 module_usb_driver(kaweth_driver);