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/init.h>
49 #include <linux/delay.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/usb.h>
53 #include <linux/types.h>
54 #include <linux/ethtool.h>
55 #include <linux/dma-mapping.h>
56 #include <linux/wait.h>
57 #include <linux/firmware.h>
58 #include <asm/uaccess.h>
59 #include <asm/byteorder.h>
63 #define KAWETH_MTU 1514
64 #define KAWETH_BUF_SIZE 1664
65 #define KAWETH_TX_TIMEOUT (5 * HZ)
66 #define KAWETH_SCRATCH_SIZE 32
67 #define KAWETH_FIRMWARE_BUF_SIZE 4096
68 #define KAWETH_CONTROL_TIMEOUT (30000)
70 #define KAWETH_STATUS_BROKEN 0x0000001
71 #define KAWETH_STATUS_CLOSING 0x0000002
72 #define KAWETH_STATUS_SUSPENDING 0x0000004
74 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
76 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
77 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
78 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
79 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
80 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
83 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
84 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
85 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
86 #define KAWETH_COMMAND_STATISTICS 0x03
87 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
88 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
89 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
90 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
91 #define KAWETH_COMMAND_SCAN 0xFF
93 #define KAWETH_SOFS_TO_WAIT 0x05
95 #define INTBUFFERSIZE 4
97 #define STATE_OFFSET 0
98 #define STATE_MASK 0x40
101 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
104 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>");
105 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
106 MODULE_LICENSE("GPL");
107 MODULE_FIRMWARE("kaweth/new_code.bin");
108 MODULE_FIRMWARE("kaweth/new_code_fix.bin");
109 MODULE_FIRMWARE("kaweth/trigger_code.bin");
110 MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
112 static const char driver_name[] = "kaweth";
114 static int kaweth_probe(
115 struct usb_interface *intf,
116 const struct usb_device_id *id /* from id_table */
118 static void kaweth_disconnect(struct usb_interface *intf);
119 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
121 struct usb_ctrlrequest *cmd, void *data,
122 int len, int timeout);
123 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
124 static int kaweth_resume(struct usb_interface *intf);
126 /****************************************************************
128 ****************************************************************/
129 static struct usb_device_id usb_klsi_table[] = {
130 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
131 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
132 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
133 { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
134 { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
135 { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
136 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
137 { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
138 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
139 { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
140 { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
141 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
142 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
143 { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
144 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
145 { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
146 { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
147 { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
148 { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
149 { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
150 { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
151 { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
152 { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
153 { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
154 { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
155 { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
156 { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
157 { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
158 { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
159 { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
160 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
161 { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
162 { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
163 { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
164 { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
165 {} /* Null terminator */
168 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
170 /****************************************************************
172 ****************************************************************/
173 static struct usb_driver kaweth_driver = {
175 .probe = kaweth_probe,
176 .disconnect = kaweth_disconnect,
177 .suspend = kaweth_suspend,
178 .resume = kaweth_resume,
179 .id_table = usb_klsi_table,
180 .supports_autosuspend = 1,
181 .disable_hub_initiated_lpm = 1,
184 typedef __u8 eth_addr_t[6];
186 /****************************************************************
188 ****************************************************************/
196 /****************************************************************
197 * kaweth_ethernet_configuration
199 ****************************************************************/
200 struct kaweth_ethernet_configuration
206 __u32 statistics_mask;
208 __u16 max_multicast_filters;
212 /****************************************************************
214 ****************************************************************/
217 spinlock_t device_lock;
221 int suspend_lowmem_rx;
222 int suspend_lowmem_ctrl;
225 struct delayed_work lowmem_work;
227 struct usb_device *dev;
228 struct usb_interface *intf;
229 struct net_device *net;
230 wait_queue_head_t term_wait;
236 dma_addr_t intbufferhandle;
238 dma_addr_t rxbufferhandle;
242 struct sk_buff *tx_skb;
245 __u8 scratch[KAWETH_SCRATCH_SIZE];
246 __u16 packet_filter_bitmap;
248 struct kaweth_ethernet_configuration configuration;
250 struct net_device_stats stats;
253 /****************************************************************
255 ****************************************************************/
256 static int kaweth_control(struct kaweth_device *kaweth,
266 struct usb_ctrlrequest *dr;
269 netdev_dbg(kaweth->net, "kaweth_control()\n");
272 netdev_dbg(kaweth->net, "in_interrupt()\n");
276 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
280 dr->bRequestType = requesttype;
281 dr->bRequest = request;
282 dr->wValue = cpu_to_le16(value);
283 dr->wIndex = cpu_to_le16(index);
284 dr->wLength = cpu_to_le16(size);
286 retval = kaweth_internal_control_msg(kaweth->dev,
297 /****************************************************************
298 * kaweth_read_configuration
299 ****************************************************************/
300 static int kaweth_read_configuration(struct kaweth_device *kaweth)
304 netdev_dbg(kaweth->net, "Reading kaweth configuration\n");
306 retval = kaweth_control(kaweth,
307 usb_rcvctrlpipe(kaweth->dev, 0),
308 KAWETH_COMMAND_GET_ETHERNET_DESC,
309 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
312 (void *)&kaweth->configuration,
313 sizeof(kaweth->configuration),
314 KAWETH_CONTROL_TIMEOUT);
319 /****************************************************************
320 * kaweth_set_urb_size
321 ****************************************************************/
322 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
326 netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
328 retval = kaweth_control(kaweth,
329 usb_sndctrlpipe(kaweth->dev, 0),
330 KAWETH_COMMAND_SET_URB_SIZE,
331 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
334 (void *)&kaweth->scratch,
336 KAWETH_CONTROL_TIMEOUT);
341 /****************************************************************
342 * kaweth_set_sofs_wait
343 ****************************************************************/
344 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
348 netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
350 retval = kaweth_control(kaweth,
351 usb_sndctrlpipe(kaweth->dev, 0),
352 KAWETH_COMMAND_SET_SOFS_WAIT,
353 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
356 (void *)&kaweth->scratch,
358 KAWETH_CONTROL_TIMEOUT);
363 /****************************************************************
364 * kaweth_set_receive_filter
365 ****************************************************************/
366 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
367 __u16 receive_filter)
371 netdev_dbg(kaweth->net, "Set receive filter to %d\n",
372 (unsigned)receive_filter);
374 retval = kaweth_control(kaweth,
375 usb_sndctrlpipe(kaweth->dev, 0),
376 KAWETH_COMMAND_SET_PACKET_FILTER,
377 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
380 (void *)&kaweth->scratch,
382 KAWETH_CONTROL_TIMEOUT);
387 /****************************************************************
388 * kaweth_download_firmware
389 ****************************************************************/
390 static int kaweth_download_firmware(struct kaweth_device *kaweth,
395 const struct firmware *fw;
399 ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
401 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
405 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
406 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
408 release_firmware(fw);
412 memcpy(kaweth->firmware_buf, fw->data, fw->size);
414 release_firmware(fw);
416 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
417 kaweth->firmware_buf[3] = data_len >> 8;
418 kaweth->firmware_buf[4] = type;
419 kaweth->firmware_buf[5] = interrupt;
421 netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
422 kaweth->firmware_buf[2]);
424 netdev_dbg(kaweth->net,
425 "Downloading firmware at %p to kaweth device at %p\n",
426 kaweth->firmware_buf, kaweth);
427 netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
429 return kaweth_control(kaweth,
430 usb_sndctrlpipe(kaweth->dev, 0),
432 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
435 (void *)kaweth->firmware_buf,
437 KAWETH_CONTROL_TIMEOUT);
440 /****************************************************************
441 * kaweth_trigger_firmware
442 ****************************************************************/
443 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
446 kaweth->firmware_buf[0] = 0xB6;
447 kaweth->firmware_buf[1] = 0xC3;
448 kaweth->firmware_buf[2] = 0x01;
449 kaweth->firmware_buf[3] = 0x00;
450 kaweth->firmware_buf[4] = 0x06;
451 kaweth->firmware_buf[5] = interrupt;
452 kaweth->firmware_buf[6] = 0x00;
453 kaweth->firmware_buf[7] = 0x00;
455 netdev_dbg(kaweth->net, "Triggering firmware\n");
457 return kaweth_control(kaweth,
458 usb_sndctrlpipe(kaweth->dev, 0),
460 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
463 (void *)kaweth->firmware_buf,
465 KAWETH_CONTROL_TIMEOUT);
468 /****************************************************************
470 ****************************************************************/
471 static int kaweth_reset(struct kaweth_device *kaweth)
475 netdev_dbg(kaweth->net, "kaweth_reset(%p)\n", kaweth);
476 result = usb_reset_configuration(kaweth->dev);
479 netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
484 static void kaweth_usb_receive(struct urb *);
485 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
487 /****************************************************************
489 *****************************************************************/
491 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
495 status = usb_submit_urb (kaweth->irq_urb, mf);
496 if (unlikely(status == -ENOMEM)) {
497 kaweth->suspend_lowmem_ctrl = 1;
498 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
500 kaweth->suspend_lowmem_ctrl = 0;
504 dev_err(&kaweth->intf->dev,
505 "can't resubmit intr, %s-%s, status %d\n",
506 kaweth->dev->bus->bus_name,
507 kaweth->dev->devpath, status);
510 static void int_callback(struct urb *u)
512 struct kaweth_device *kaweth = u->context;
514 int status = u->status;
517 case 0: /* success */
519 case -ECONNRESET: /* unlink */
523 /* -EPIPE: should clear the halt */
528 /* we check the link state to report changes */
529 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
531 netif_carrier_on(kaweth->net);
533 netif_carrier_off(kaweth->net);
535 kaweth->linkstate = act_state;
538 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
541 static void kaweth_resubmit_tl(struct work_struct *work)
543 struct kaweth_device *kaweth =
544 container_of(work, struct kaweth_device, lowmem_work.work);
546 if (IS_BLOCKED(kaweth->status))
549 if (kaweth->suspend_lowmem_rx)
550 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
552 if (kaweth->suspend_lowmem_ctrl)
553 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
557 /****************************************************************
558 * kaweth_resubmit_rx_urb
559 ****************************************************************/
560 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
565 usb_fill_bulk_urb(kaweth->rx_urb,
567 usb_rcvbulkpipe(kaweth->dev, 1),
572 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
573 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
575 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
576 if (result == -ENOMEM) {
577 kaweth->suspend_lowmem_rx = 1;
578 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
580 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
583 kaweth->suspend_lowmem_rx = 0;
589 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
591 /****************************************************************
593 ****************************************************************/
594 static void kaweth_usb_receive(struct urb *urb)
596 struct device *dev = &urb->dev->dev;
597 struct kaweth_device *kaweth = urb->context;
598 struct net_device *net = kaweth->net;
599 int status = urb->status;
601 int count = urb->actual_length;
602 int count2 = urb->transfer_buffer_length;
604 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
608 if (unlikely(status == -EPIPE)) {
609 kaweth->stats.rx_errors++;
611 wake_up(&kaweth->term_wait);
612 dev_dbg(dev, "Status was -EPIPE.\n");
615 if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
616 /* we are killed - set a flag and wake the disconnect handler */
618 wake_up(&kaweth->term_wait);
619 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
622 if (unlikely(status == -EPROTO || status == -ETIME ||
623 status == -EILSEQ)) {
624 kaweth->stats.rx_errors++;
625 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
628 if (unlikely(status == -EOVERFLOW)) {
629 kaweth->stats.rx_errors++;
630 dev_dbg(dev, "Status was -EOVERFLOW.\n");
632 spin_lock(&kaweth->device_lock);
633 if (IS_BLOCKED(kaweth->status)) {
634 spin_unlock(&kaweth->device_lock);
637 spin_unlock(&kaweth->device_lock);
639 if(status && status != -EREMOTEIO && count != 1) {
640 dev_err(&kaweth->intf->dev,
641 "%s RX status: %d count: %d packet_len: %d\n",
642 net->name, status, count, (int)pkt_len);
643 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
647 if(kaweth->net && (count > 2)) {
648 if(pkt_len > (count - 2)) {
649 dev_err(&kaweth->intf->dev,
650 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
652 dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
654 dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
655 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
659 if(!(skb = dev_alloc_skb(pkt_len+2))) {
660 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
664 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
666 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
668 skb_put(skb, pkt_len);
670 skb->protocol = eth_type_trans(skb, net);
674 kaweth->stats.rx_packets++;
675 kaweth->stats.rx_bytes += pkt_len;
678 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
681 /****************************************************************
683 ****************************************************************/
684 static int kaweth_open(struct net_device *net)
686 struct kaweth_device *kaweth = netdev_priv(net);
689 netdev_dbg(kaweth->net, "Opening network device.\n");
691 res = usb_autopm_get_interface(kaweth->intf);
693 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
696 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
703 usb_rcvintpipe(kaweth->dev, 3),
708 250); /* overriding the descriptor */
709 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
710 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
712 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
714 usb_kill_urb(kaweth->rx_urb);
719 netif_start_queue(net);
721 kaweth_async_set_rx_mode(kaweth);
725 usb_autopm_put_interface(kaweth->intf);
729 /****************************************************************
731 ****************************************************************/
732 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
734 usb_kill_urb(kaweth->irq_urb);
735 usb_kill_urb(kaweth->rx_urb);
736 usb_kill_urb(kaweth->tx_urb);
738 cancel_delayed_work_sync(&kaweth->lowmem_work);
740 /* a scheduled work may have resubmitted,
742 usb_kill_urb(kaweth->irq_urb);
743 usb_kill_urb(kaweth->rx_urb);
746 /****************************************************************
748 ****************************************************************/
749 static int kaweth_close(struct net_device *net)
751 struct kaweth_device *kaweth = netdev_priv(net);
753 netif_stop_queue(net);
756 kaweth->status |= KAWETH_STATUS_CLOSING;
758 kaweth_kill_urbs(kaweth);
760 kaweth->status &= ~KAWETH_STATUS_CLOSING;
762 usb_autopm_put_interface(kaweth->intf);
767 static u32 kaweth_get_link(struct net_device *dev)
769 struct kaweth_device *kaweth = netdev_priv(dev);
771 return kaweth->linkstate;
774 static const struct ethtool_ops ops = {
775 .get_link = kaweth_get_link
778 /****************************************************************
779 * kaweth_usb_transmit_complete
780 ****************************************************************/
781 static void kaweth_usb_transmit_complete(struct urb *urb)
783 struct kaweth_device *kaweth = urb->context;
784 struct sk_buff *skb = kaweth->tx_skb;
785 int status = urb->status;
787 if (unlikely(status != 0))
788 if (status != -ENOENT)
789 dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
790 kaweth->net->name, status);
792 netif_wake_queue(kaweth->net);
793 dev_kfree_skb_irq(skb);
796 /****************************************************************
798 ****************************************************************/
799 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
800 struct net_device *net)
802 struct kaweth_device *kaweth = netdev_priv(net);
803 __le16 *private_header;
807 spin_lock_irq(&kaweth->device_lock);
809 kaweth_async_set_rx_mode(kaweth);
810 netif_stop_queue(net);
811 if (IS_BLOCKED(kaweth->status)) {
815 /* We now decide whether we can put our special header into the sk_buff */
816 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
817 /* no such luck - we make our own */
818 struct sk_buff *copied_skb;
819 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
820 dev_kfree_skb_irq(skb);
823 kaweth->stats.tx_errors++;
824 netif_start_queue(net);
825 spin_unlock_irq(&kaweth->device_lock);
830 private_header = (__le16 *)__skb_push(skb, 2);
831 *private_header = cpu_to_le16(skb->len-2);
832 kaweth->tx_skb = skb;
834 usb_fill_bulk_urb(kaweth->tx_urb,
836 usb_sndbulkpipe(kaweth->dev, 2),
839 kaweth_usb_transmit_complete,
843 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
845 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
847 kaweth->stats.tx_errors++;
849 netif_start_queue(net);
850 dev_kfree_skb_irq(skb);
854 kaweth->stats.tx_packets++;
855 kaweth->stats.tx_bytes += skb->len;
858 spin_unlock_irq(&kaweth->device_lock);
863 /****************************************************************
865 ****************************************************************/
866 static void kaweth_set_rx_mode(struct net_device *net)
868 struct kaweth_device *kaweth = netdev_priv(net);
870 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
871 KAWETH_PACKET_FILTER_BROADCAST |
872 KAWETH_PACKET_FILTER_MULTICAST;
874 netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
876 netif_stop_queue(net);
878 if (net->flags & IFF_PROMISC) {
879 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
881 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
882 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
885 kaweth->packet_filter_bitmap = packet_filter_bitmap;
886 netif_wake_queue(net);
889 /****************************************************************
890 * kaweth_async_set_rx_mode
891 ****************************************************************/
892 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
895 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
897 kaweth->packet_filter_bitmap = 0;
898 if (packet_filter_bitmap == 0)
904 result = kaweth_control(kaweth,
905 usb_sndctrlpipe(kaweth->dev, 0),
906 KAWETH_COMMAND_SET_PACKET_FILTER,
907 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
908 packet_filter_bitmap,
910 (void *)&kaweth->scratch,
912 KAWETH_CONTROL_TIMEOUT);
915 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
919 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
920 packet_filter_bitmap);
924 /****************************************************************
925 * kaweth_netdev_stats
926 ****************************************************************/
927 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
929 struct kaweth_device *kaweth = netdev_priv(dev);
930 return &kaweth->stats;
933 /****************************************************************
935 ****************************************************************/
936 static void kaweth_tx_timeout(struct net_device *net)
938 struct kaweth_device *kaweth = netdev_priv(net);
940 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
941 kaweth->stats.tx_errors++;
942 net->trans_start = jiffies;
944 usb_unlink_urb(kaweth->tx_urb);
947 /****************************************************************
949 ****************************************************************/
950 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
952 struct kaweth_device *kaweth = usb_get_intfdata(intf);
955 dev_dbg(&intf->dev, "Suspending device\n");
956 spin_lock_irqsave(&kaweth->device_lock, flags);
957 kaweth->status |= KAWETH_STATUS_SUSPENDING;
958 spin_unlock_irqrestore(&kaweth->device_lock, flags);
960 kaweth_kill_urbs(kaweth);
964 /****************************************************************
966 ****************************************************************/
967 static int kaweth_resume(struct usb_interface *intf)
969 struct kaweth_device *kaweth = usb_get_intfdata(intf);
972 dev_dbg(&intf->dev, "Resuming device\n");
973 spin_lock_irqsave(&kaweth->device_lock, flags);
974 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
975 spin_unlock_irqrestore(&kaweth->device_lock, flags);
979 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
980 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
985 /****************************************************************
987 ****************************************************************/
990 static const struct net_device_ops kaweth_netdev_ops = {
991 .ndo_open = kaweth_open,
992 .ndo_stop = kaweth_close,
993 .ndo_start_xmit = kaweth_start_xmit,
994 .ndo_tx_timeout = kaweth_tx_timeout,
995 .ndo_set_rx_mode = kaweth_set_rx_mode,
996 .ndo_get_stats = kaweth_netdev_stats,
997 .ndo_change_mtu = eth_change_mtu,
998 .ndo_set_mac_address = eth_mac_addr,
999 .ndo_validate_addr = eth_validate_addr,
1002 static int kaweth_probe(
1003 struct usb_interface *intf,
1004 const struct usb_device_id *id /* from id_table */
1007 struct device *dev = &intf->dev;
1008 struct usb_device *udev = interface_to_usbdev(intf);
1009 struct kaweth_device *kaweth;
1010 struct net_device *netdev;
1011 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1015 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
1016 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
1017 le16_to_cpu(udev->descriptor.idProduct),
1018 le16_to_cpu(udev->descriptor.bcdDevice));
1020 dev_dbg(dev, "Device at %p\n", udev);
1022 dev_dbg(dev, "Descriptor length: %x type: %x\n",
1023 (int)udev->descriptor.bLength,
1024 (int)udev->descriptor.bDescriptorType);
1026 netdev = alloc_etherdev(sizeof(*kaweth));
1030 kaweth = netdev_priv(netdev);
1032 kaweth->net = netdev;
1034 spin_lock_init(&kaweth->device_lock);
1035 init_waitqueue_head(&kaweth->term_wait);
1037 dev_dbg(dev, "Resetting.\n");
1039 kaweth_reset(kaweth);
1042 * If high byte of bcdDevice is nonzero, firmware is already
1043 * downloaded. Don't try to do it again, or we'll hang the device.
1046 if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
1047 dev_info(dev, "Firmware present in device.\n");
1049 /* Download the firmware */
1050 dev_info(dev, "Downloading firmware...\n");
1051 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1052 if ((result = kaweth_download_firmware(kaweth,
1053 "kaweth/new_code.bin",
1056 dev_err(dev, "Error downloading firmware (%d)\n",
1061 if ((result = kaweth_download_firmware(kaweth,
1062 "kaweth/new_code_fix.bin",
1065 dev_err(dev, "Error downloading firmware fix (%d)\n",
1070 if ((result = kaweth_download_firmware(kaweth,
1071 "kaweth/trigger_code.bin",
1074 dev_err(dev, "Error downloading trigger code (%d)\n",
1080 if ((result = kaweth_download_firmware(kaweth,
1081 "kaweth/trigger_code_fix.bin",
1084 dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
1089 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1090 dev_err(dev, "Error triggering firmware (%d)\n", result);
1094 /* Device will now disappear for a moment... */
1095 dev_info(dev, "Firmware loaded. I'll be back...\n");
1097 free_page((unsigned long)kaweth->firmware_buf);
1098 free_netdev(netdev);
1102 result = kaweth_read_configuration(kaweth);
1105 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
1106 goto err_free_netdev;
1109 dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1110 dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1111 dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1112 dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
1114 if(!memcmp(&kaweth->configuration.hw_addr,
1116 sizeof(bcast_addr))) {
1117 dev_err(dev, "Firmware not functioning properly, no net device created\n");
1118 goto err_free_netdev;
1121 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1122 dev_dbg(dev, "Error setting URB size\n");
1123 goto err_free_netdev;
1126 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1127 dev_err(dev, "Error setting SOFS wait\n");
1128 goto err_free_netdev;
1131 result = kaweth_set_receive_filter(kaweth,
1132 KAWETH_PACKET_FILTER_DIRECTED |
1133 KAWETH_PACKET_FILTER_BROADCAST |
1134 KAWETH_PACKET_FILTER_MULTICAST);
1137 dev_err(dev, "Error setting receive filter\n");
1138 goto err_free_netdev;
1141 dev_dbg(dev, "Initializing net device.\n");
1143 kaweth->intf = intf;
1145 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1146 if (!kaweth->tx_urb)
1147 goto err_free_netdev;
1148 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1149 if (!kaweth->rx_urb)
1151 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1152 if (!kaweth->irq_urb)
1155 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1158 &kaweth->intbufferhandle);
1159 if (!kaweth->intbuffer)
1160 goto err_tx_and_rx_and_irq;
1161 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
1164 &kaweth->rxbufferhandle);
1165 if (!kaweth->rx_buf)
1166 goto err_all_but_rxbuf;
1168 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1169 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1170 sizeof(kaweth->configuration.hw_addr));
1172 netdev->netdev_ops = &kaweth_netdev_ops;
1173 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1174 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1175 SET_ETHTOOL_OPS(netdev, &ops);
1177 /* kaweth is zeroed as part of alloc_netdev */
1178 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1179 usb_set_intfdata(intf, kaweth);
1182 // dma_supported() is deeply broken on almost all architectures
1183 if (dma_supported (dev, 0xffffffffffffffffULL))
1184 kaweth->net->features |= NETIF_F_HIGHDMA;
1187 SET_NETDEV_DEV(netdev, dev);
1188 if (register_netdev(netdev) != 0) {
1189 dev_err(dev, "Error registering netdev.\n");
1193 dev_info(dev, "kaweth interface created at %s\n",
1196 dev_dbg(dev, "Kaweth probe returning.\n");
1201 usb_set_intfdata(intf, NULL);
1202 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1204 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1205 err_tx_and_rx_and_irq:
1206 usb_free_urb(kaweth->irq_urb);
1208 usb_free_urb(kaweth->rx_urb);
1210 usb_free_urb(kaweth->tx_urb);
1212 free_netdev(netdev);
1217 /****************************************************************
1219 ****************************************************************/
1220 static void kaweth_disconnect(struct usb_interface *intf)
1222 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1223 struct net_device *netdev;
1225 dev_info(&intf->dev, "Unregistering\n");
1227 usb_set_intfdata(intf, NULL);
1229 dev_warn(&intf->dev, "unregistering non-existent device\n");
1232 netdev = kaweth->net;
1234 netdev_dbg(kaweth->net, "Unregistering net device\n");
1235 unregister_netdev(netdev);
1237 usb_free_urb(kaweth->rx_urb);
1238 usb_free_urb(kaweth->tx_urb);
1239 usb_free_urb(kaweth->irq_urb);
1241 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1242 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1244 free_netdev(netdev);
1248 // FIXME this completion stuff is a modified clone of
1249 // an OLD version of some stuff in usb.c ...
1250 struct usb_api_data {
1251 wait_queue_head_t wqh;
1255 /*-------------------------------------------------------------------*
1256 * completion handler for compatibility wrappers (sync control/bulk) *
1257 *-------------------------------------------------------------------*/
1258 static void usb_api_blocking_completion(struct urb *urb)
1260 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1266 /*-------------------------------------------------------------------*
1267 * COMPATIBILITY STUFF *
1268 *-------------------------------------------------------------------*/
1270 // Starts urb and waits for completion or timeout
1271 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1273 struct usb_api_data awd;
1276 init_waitqueue_head(&awd.wqh);
1279 urb->context = &awd;
1280 status = usb_submit_urb(urb, GFP_NOIO);
1282 // something went wrong
1287 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1289 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1290 usb_kill_urb(urb); // remove urb safely
1291 status = -ETIMEDOUT;
1294 status = urb->status;
1297 if (actual_length) {
1298 *actual_length = urb->actual_length;
1305 /*-------------------------------------------------------------------*/
1306 // returns status (negative) or length (positive)
1307 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1309 struct usb_ctrlrequest *cmd, void *data,
1310 int len, int timeout)
1314 int length = 0; /* shut up GCC */
1316 urb = usb_alloc_urb(0, GFP_ATOMIC);
1320 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1321 len, usb_api_blocking_completion, NULL);
1323 retv = usb_start_wait_urb(urb, timeout, &length);
1332 module_usb_driver(kaweth_driver);