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, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 ****************************************************************/
34 * Fix in_interrupt() problem
35 * Develop test procedures for USB net interfaces
37 * Fix bugs from previous two steps
38 * Snoop other OSs for any tricks we're not doing
40 * Reduce arbitrary timeouts
41 * Smart multicast support
42 * Temporary MAC change support
43 * Tunable SOFs parameter - ioctl()?
44 * Ethernet stats collection
45 * Code formatting improvements
48 #include <linux/module.h>
49 #include <linux/sched.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/init.h>
53 #include <linux/delay.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/usb.h>
57 #include <linux/types.h>
58 #include <linux/ethtool.h>
59 #include <asm/uaccess.h>
60 #include <asm/semaphore.h>
61 #include <asm/byteorder.h>
66 #define kaweth_dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" ,##arg)
68 #define kaweth_dbg(format, arg...) do {} while (0)
70 #define kaweth_err(format, arg...) printk(KERN_ERR __FILE__ ": " format "\n" ,##arg)
71 #define kaweth_info(format, arg...) printk(KERN_INFO __FILE__ ": " format "\n" , ##arg)
72 #define kaweth_warn(format, arg...) printk(KERN_WARNING __FILE__ ": " format "\n" , ##arg)
77 #define KAWETH_MTU 1500
78 #define KAWETH_BUF_SIZE 1664
79 #define KAWETH_TX_TIMEOUT (5 * HZ)
80 #define KAWETH_SCRATCH_SIZE 32
81 #define KAWETH_FIRMWARE_BUF_SIZE 4096
82 #define KAWETH_CONTROL_TIMEOUT (30 * HZ)
84 #define KAWETH_STATUS_BROKEN 0x0000001
85 #define KAWETH_STATUS_CLOSING 0x0000002
87 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
88 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
89 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
90 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
91 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
94 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
95 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
96 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
97 #define KAWETH_COMMAND_STATISTICS 0x03
98 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
99 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
100 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
101 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
102 #define KAWETH_COMMAND_SCAN 0xFF
104 #define KAWETH_SOFS_TO_WAIT 0x05
106 #define INTBUFFERSIZE 4
108 #define STATE_OFFSET 0
109 #define STATE_MASK 0x40
110 #define STATE_SHIFT 5
113 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>");
114 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
115 MODULE_LICENSE("GPL");
117 static const char driver_name[] = "kaweth";
119 static void *kaweth_probe(
120 struct usb_device *dev, /* the device */
121 unsigned ifnum, /* what interface */
122 const struct usb_device_id *id /* from id_table */
124 static void kaweth_disconnect(struct usb_device *dev, void *ptr);
125 int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
126 struct usb_ctrlrequest *cmd, void *data,
127 int len, int timeout);
129 /****************************************************************
131 ****************************************************************/
132 static struct usb_device_id usb_klsi_table[] = {
133 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
134 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
135 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
136 { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
137 { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
138 { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
139 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
140 { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
141 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
142 { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
143 { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
144 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
145 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
146 { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
147 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
148 { USB_DEVICE(0x0785, 0x0002) }, /* NTT-TE MN128 USB-Ethernet Adapter */
149 { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
150 { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
151 { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
152 { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
153 { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
154 { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
155 { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
156 { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
157 { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
158 { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
159 { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
160 { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
161 { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
162 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
163 { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
164 { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
165 { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
166 {} /* Null terminator */
169 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
171 /****************************************************************
173 ****************************************************************/
174 static struct usb_driver kaweth_driver = {
176 .probe = kaweth_probe,
177 .disconnect = kaweth_disconnect,
178 .id_table = usb_klsi_table,
181 typedef __u8 eth_addr_t[6];
183 /****************************************************************
185 ****************************************************************/
193 /****************************************************************
194 * kaweth_ethernet_configuration
196 ****************************************************************/
197 struct kaweth_ethernet_configuration
203 __u32 statistics_mask;
205 __u16 max_multicast_filters;
207 } __attribute__ ((packed));
209 /****************************************************************
211 ****************************************************************/
214 spinlock_t device_lock;
222 struct usb_device *dev;
223 struct net_device *net;
224 wait_queue_head_t term_wait;
230 struct sk_buff *tx_skb;
233 __u8 scratch[KAWETH_SCRATCH_SIZE];
234 __u8 rx_buf[KAWETH_BUF_SIZE];
235 __u8 intbuffer[INTBUFFERSIZE];
236 __u16 packet_filter_bitmap;
238 struct kaweth_ethernet_configuration configuration;
240 struct net_device_stats stats;
241 } __attribute__ ((packed));
244 /****************************************************************
246 ****************************************************************/
247 static int kaweth_control(struct kaweth_device *kaweth,
257 struct usb_ctrlrequest *dr;
259 kaweth_dbg("kaweth_control()");
262 kaweth_dbg("in_interrupt()");
266 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
269 kaweth_dbg("kmalloc() failed");
273 dr->bRequestType= requesttype;
274 dr->bRequest = request;
275 dr->wValue = cpu_to_le16p(&value);
276 dr->wIndex = cpu_to_le16p(&index);
277 dr->wLength = cpu_to_le16p(&size);
279 return kaweth_internal_control_msg(kaweth->dev,
287 /****************************************************************
288 * kaweth_read_configuration
289 ****************************************************************/
290 static int kaweth_read_configuration(struct kaweth_device *kaweth)
294 kaweth_dbg("Reading kaweth configuration");
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 kaweth_dbg("Setting URB size to %d", (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 kaweth_dbg("Set SOFS wait to %d", (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 kaweth_dbg("Set receive filter to %d", (unsigned)receive_filter);
363 retval = kaweth_control(kaweth,
364 usb_sndctrlpipe(kaweth->dev, 0),
365 KAWETH_COMMAND_SET_PACKET_FILTER,
366 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
369 (void *)&kaweth->scratch,
371 KAWETH_CONTROL_TIMEOUT);
376 /****************************************************************
377 * kaweth_download_firmware
378 ****************************************************************/
379 static int kaweth_download_firmware(struct kaweth_device *kaweth,
385 if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
386 kaweth_err("Firmware too big: %d", data_len);
390 memcpy(kaweth->firmware_buf, data, data_len);
392 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
393 kaweth->firmware_buf[3] = data_len >> 8;
394 kaweth->firmware_buf[4] = type;
395 kaweth->firmware_buf[5] = interrupt;
397 kaweth_dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
398 kaweth->firmware_buf[2]);
400 kaweth_dbg("Downloading firmware at %p to kaweth device at %p",
403 kaweth_dbg("Firmware length: %d", data_len);
405 return kaweth_control(kaweth,
406 usb_sndctrlpipe(kaweth->dev, 0),
408 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
411 (void *)kaweth->firmware_buf,
413 KAWETH_CONTROL_TIMEOUT);
416 /****************************************************************
417 * kaweth_trigger_firmware
418 ****************************************************************/
419 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
422 kaweth->firmware_buf[0] = 0xB6;
423 kaweth->firmware_buf[1] = 0xC3;
424 kaweth->firmware_buf[2] = 0x01;
425 kaweth->firmware_buf[3] = 0x00;
426 kaweth->firmware_buf[4] = 0x06;
427 kaweth->firmware_buf[5] = interrupt;
428 kaweth->firmware_buf[6] = 0x00;
429 kaweth->firmware_buf[7] = 0x00;
431 kaweth_dbg("Triggering firmware");
433 return kaweth_control(kaweth,
434 usb_sndctrlpipe(kaweth->dev, 0),
436 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
439 (void *)kaweth->firmware_buf,
441 KAWETH_CONTROL_TIMEOUT);
444 /****************************************************************
446 ****************************************************************/
447 static int kaweth_reset(struct kaweth_device *kaweth)
451 kaweth_dbg("kaweth_reset(%p)", kaweth);
452 result = kaweth_control(kaweth,
453 usb_sndctrlpipe(kaweth->dev, 0),
454 USB_REQ_SET_CONFIGURATION,
456 kaweth->dev->config[0].bConfigurationValue,
460 KAWETH_CONTROL_TIMEOUT);
464 kaweth_dbg("kaweth_reset() returns %d.",result);
469 static void kaweth_usb_receive(struct urb *);
470 static void kaweth_resubmit_rx_urb(struct kaweth_device *);
472 /****************************************************************
474 *****************************************************************/
475 static void int_callback(struct urb *u)
477 struct kaweth_device *kaweth = u->context;
480 /* we abuse the interrupt urb for rebsubmitting under low memory saving a timer */
481 if (kaweth->suspend_lowmem)
482 kaweth_resubmit_rx_urb(kaweth);
484 /* we check the link state to report changes */
485 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
487 netif_carrier_on(kaweth->net);
489 netif_carrier_off(kaweth->net);
491 kaweth->linkstate = act_state;
496 /****************************************************************
497 * kaweth_resubmit_rx_urb
498 ****************************************************************/
499 static void kaweth_resubmit_rx_urb(struct kaweth_device *kaweth)
504 FILL_BULK_URB(kaweth->rx_urb,
506 usb_rcvbulkpipe(kaweth->dev, 1),
512 spin_lock_irqsave(&kaweth->device_lock, flags);
513 if (!kaweth->removed) { /* no resubmit if disconnecting */
514 if((result = usb_submit_urb(kaweth->rx_urb))) {
515 if (result == -ENOMEM)
516 kaweth->suspend_lowmem = 1;
517 kaweth_err("resubmitting rx_urb %d failed", result);
519 kaweth->suspend_lowmem = 0;
522 spin_unlock_irqrestore(&kaweth->device_lock, flags);
525 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
527 /****************************************************************
529 ****************************************************************/
530 static void kaweth_usb_receive(struct urb *urb)
532 struct kaweth_device *kaweth = urb->context;
533 struct net_device *net = kaweth->net;
535 int count = urb->actual_length;
536 int count2 = urb->transfer_buffer_length;
538 __u16 pkt_len = le16_to_cpup((u16 *)kaweth->rx_buf);
542 if(urb->status == -ECONNRESET || urb->status == -ECONNABORTED)
543 /* we are killed - set a flag and wake the disconnect handler */
546 wake_up(&kaweth->term_wait);
550 if (kaweth->status & KAWETH_STATUS_CLOSING)
553 if(urb->status && urb->status != -EREMOTEIO && count != 1) {
554 kaweth_err("%s RX status: %d count: %d packet_len: %d",
559 kaweth_resubmit_rx_urb(kaweth);
563 if(kaweth->net && (count > 2)) {
564 if(pkt_len > (count - 2)) {
565 kaweth_err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
566 kaweth_err("Packet len & 2047: %x", pkt_len & 2047);
567 kaweth_err("Count 2: %x", count2);
568 kaweth_resubmit_rx_urb(kaweth);
572 if(!(skb = dev_alloc_skb(pkt_len+2))) {
573 kaweth_resubmit_rx_urb(kaweth);
577 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
581 eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
583 skb_put(skb, pkt_len);
585 skb->protocol = eth_type_trans(skb, net);
589 kaweth->stats.rx_packets++;
590 kaweth->stats.rx_bytes += pkt_len;
593 kaweth_resubmit_rx_urb(kaweth);
596 /****************************************************************
598 ****************************************************************/
599 static int kaweth_open(struct net_device *net)
601 struct kaweth_device *kaweth = (struct kaweth_device *)net->priv;
603 kaweth_dbg("Dev usage: %d", kaweth->dev->refcnt.counter);
605 kaweth_dbg("Opening network device.");
609 kaweth_resubmit_rx_urb(kaweth);
614 usb_rcvintpipe(kaweth->dev, 3),
621 usb_submit_urb(kaweth->irq_urb);
623 netif_start_queue(net);
625 kaweth_async_set_rx_mode(kaweth);
629 /****************************************************************
631 ****************************************************************/
632 static int kaweth_close(struct net_device *net)
634 struct kaweth_device *kaweth = net->priv;
636 netif_stop_queue(net);
638 spin_lock_irq(&kaweth->device_lock);
639 kaweth->status |= KAWETH_STATUS_CLOSING;
640 spin_unlock_irq(&kaweth->device_lock);
642 usb_unlink_urb(kaweth->irq_urb);
643 usb_unlink_urb(kaweth->rx_urb);
645 kaweth->status &= ~KAWETH_STATUS_CLOSING;
649 printk("Dev usage: %d", kaweth->dev->refcnt.counter);
654 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
658 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
662 case ETHTOOL_GDRVINFO: {
663 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
664 strncpy(info.driver, driver_name, sizeof(info.driver)-1);
665 if (copy_to_user(useraddr, &info, sizeof(info)))
674 /****************************************************************
676 ****************************************************************/
677 static int kaweth_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
681 return netdev_ethtool_ioctl(net, (void *) rq->ifr_data);
686 /****************************************************************
687 * kaweth_usb_transmit_complete
688 ****************************************************************/
689 static void kaweth_usb_transmit_complete(struct urb *urb)
691 struct kaweth_device *kaweth = urb->context;
692 struct sk_buff *skb = kaweth->tx_skb;
694 if (urb->status != 0)
695 kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
697 netif_wake_queue(kaweth->net);
698 dev_kfree_skb_irq(skb);
701 /****************************************************************
703 ****************************************************************/
704 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
706 struct kaweth_device *kaweth = net->priv;
711 spin_lock(&kaweth->device_lock);
713 if (kaweth->removed) {
714 /* our device is undergoing disconnection - we bail out */
715 spin_unlock(&kaweth->device_lock);
716 dev_kfree_skb_irq(skb);
720 kaweth_async_set_rx_mode(kaweth);
721 netif_stop_queue(net);
723 /* We now decide whether we can put our special header into the sk_buff */
724 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
725 /* no such luck - we make our own */
726 struct sk_buff *copied_skb;
727 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
728 dev_kfree_skb_irq(skb);
731 kaweth->stats.tx_errors++;
732 netif_start_queue(net);
733 spin_unlock(&kaweth->device_lock);
738 private_header = (u16 *)__skb_push(skb, 2);
739 *private_header = cpu_to_le16(skb->len-2);
740 kaweth->tx_skb = skb;
742 FILL_BULK_URB(kaweth->tx_urb,
744 usb_sndbulkpipe(kaweth->dev, 2),
747 kaweth_usb_transmit_complete,
750 kaweth->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
752 if((res = usb_submit_urb(kaweth->tx_urb)))
754 kaweth_warn("kaweth failed tx_urb %d", res);
755 kaweth->stats.tx_errors++;
757 netif_start_queue(net);
758 dev_kfree_skb_irq(skb);
762 kaweth->stats.tx_packets++;
763 kaweth->stats.tx_bytes += skb->len;
764 net->trans_start = jiffies;
767 spin_unlock(&kaweth->device_lock);
772 /****************************************************************
774 ****************************************************************/
775 static void kaweth_set_rx_mode(struct net_device *net)
777 struct kaweth_device *kaweth = net->priv;
779 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
780 KAWETH_PACKET_FILTER_BROADCAST |
781 KAWETH_PACKET_FILTER_MULTICAST;
783 kaweth_dbg("Setting Rx mode to %d", packet_filter_bitmap);
785 netif_stop_queue(net);
787 if (net->flags & IFF_PROMISC) {
788 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
790 else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
791 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
794 kaweth->packet_filter_bitmap = packet_filter_bitmap;
795 netif_wake_queue(net);
798 /****************************************************************
799 * kaweth_async_set_rx_mode
800 ****************************************************************/
801 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
803 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
804 kaweth->packet_filter_bitmap = 0;
805 if(packet_filter_bitmap == 0) return;
809 result = kaweth_control(kaweth,
810 usb_sndctrlpipe(kaweth->dev, 0),
811 KAWETH_COMMAND_SET_PACKET_FILTER,
812 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
813 packet_filter_bitmap,
815 (void *)&kaweth->scratch,
817 KAWETH_CONTROL_TIMEOUT);
820 kaweth_err("Failed to set Rx mode: %d", result);
823 kaweth_dbg("Set Rx mode to %d", packet_filter_bitmap);
828 /****************************************************************
829 * kaweth_netdev_stats
830 ****************************************************************/
831 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
833 return &((struct kaweth_device *)dev->priv)->stats;
836 /****************************************************************
838 ****************************************************************/
839 static void kaweth_tx_timeout(struct net_device *net)
841 struct kaweth_device *kaweth = net->priv;
843 kaweth_warn("%s: Tx timed out. Resetting.", net->name);
844 kaweth->stats.tx_errors++;
845 net->trans_start = jiffies;
847 usb_unlink_urb(kaweth->tx_urb);
850 /****************************************************************
852 ****************************************************************/
853 static void *kaweth_probe(
854 struct usb_device *dev, /* the device */
855 unsigned ifnum, /* what interface */
856 const struct usb_device_id *id /* from id_table */
859 struct kaweth_device *kaweth;
860 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
863 kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
865 (int)dev->descriptor.idVendor,
866 (int)dev->descriptor.idProduct,
867 (int)dev->descriptor.bcdDevice);
869 kaweth_dbg("Device at %p", dev);
871 kaweth_dbg("Descriptor length: %x type: %x",
872 (int)dev->descriptor.bLength,
873 (int)dev->descriptor.bDescriptorType);
875 if(!(kaweth = kmalloc(sizeof(struct kaweth_device), GFP_KERNEL))) {
876 kaweth_dbg("out of memory allocating device structure\n");
880 memset(kaweth, 0, sizeof(struct kaweth_device));
883 spin_lock_init(&kaweth->device_lock);
884 init_waitqueue_head(&kaweth->term_wait);
886 kaweth_dbg("Resetting.");
888 kaweth_reset(kaweth);
891 * If high byte of bcdDevice is nonzero, firmware is already
892 * downloaded. Don't try to do it again, or we'll hang the device.
895 if (dev->descriptor.bcdDevice >> 8) {
896 kaweth_info("Firmware present in device.");
898 /* Download the firmware */
899 kaweth_info("Downloading firmware...");
900 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
901 if ((result = kaweth_download_firmware(kaweth,
906 kaweth_err("Error downloading firmware (%d)", result);
907 free_page((unsigned long)kaweth->firmware_buf);
912 if ((result = kaweth_download_firmware(kaweth,
914 len_kaweth_new_code_fix,
917 kaweth_err("Error downloading firmware fix (%d)", result);
918 free_page((unsigned long)kaweth->firmware_buf);
923 if ((result = kaweth_download_firmware(kaweth,
925 len_kaweth_trigger_code,
928 kaweth_err("Error downloading trigger code (%d)", result);
929 free_page((unsigned long)kaweth->firmware_buf);
934 if ((result = kaweth_download_firmware(kaweth,
935 kaweth_trigger_code_fix,
936 len_kaweth_trigger_code_fix,
939 kaweth_err("Error downloading trigger code fix (%d)", result);
940 free_page((unsigned long)kaweth->firmware_buf);
946 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
947 kaweth_err("Error triggering firmware (%d)", result);
948 free_page((unsigned long)kaweth->firmware_buf);
953 /* Device will now disappear for a moment... */
954 kaweth_info("Firmware loaded. I'll be back...");
955 free_page((unsigned long)kaweth->firmware_buf);
960 result = kaweth_read_configuration(kaweth);
963 kaweth_err("Error reading configuration (%d), no net device created", result);
968 kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
969 kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
970 kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
971 kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
972 (int)kaweth->configuration.hw_addr[0],
973 (int)kaweth->configuration.hw_addr[1],
974 (int)kaweth->configuration.hw_addr[2],
975 (int)kaweth->configuration.hw_addr[3],
976 (int)kaweth->configuration.hw_addr[4],
977 (int)kaweth->configuration.hw_addr[5]);
979 if(!memcmp(&kaweth->configuration.hw_addr,
981 sizeof(bcast_addr))) {
982 kaweth_err("Firmware not functioning properly, no net device created");
987 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
988 kaweth_dbg("Error setting URB size");
992 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
993 kaweth_err("Error setting SOFS wait");
997 result = kaweth_set_receive_filter(kaweth,
998 KAWETH_PACKET_FILTER_DIRECTED |
999 KAWETH_PACKET_FILTER_BROADCAST |
1000 KAWETH_PACKET_FILTER_MULTICAST);
1003 kaweth_err("Error setting receive filter");
1007 kaweth_dbg("Initializing net device.");
1009 kaweth->tx_urb = usb_alloc_urb(0);
1010 if (!kaweth->tx_urb)
1012 kaweth->rx_urb = usb_alloc_urb(0);
1013 if (!kaweth->rx_urb)
1015 kaweth->irq_urb = usb_alloc_urb(0);
1016 if (!kaweth->irq_urb)
1019 kaweth->net = init_etherdev(0, 0);
1021 kaweth_err("Error calling init_etherdev.");
1025 memcpy(kaweth->net->broadcast, &bcast_addr, sizeof(bcast_addr));
1026 memcpy(kaweth->net->dev_addr,
1027 &kaweth->configuration.hw_addr,
1028 sizeof(kaweth->configuration.hw_addr));
1030 kaweth->net->priv = kaweth;
1031 kaweth->net->open = kaweth_open;
1032 kaweth->net->stop = kaweth_close;
1034 kaweth->net->watchdog_timeo = KAWETH_TX_TIMEOUT;
1035 kaweth->net->tx_timeout = kaweth_tx_timeout;
1037 kaweth->net->do_ioctl = kaweth_ioctl;
1038 kaweth->net->hard_start_xmit = kaweth_start_xmit;
1039 kaweth->net->set_multicast_list = kaweth_set_rx_mode;
1040 kaweth->net->get_stats = kaweth_netdev_stats;
1041 kaweth->net->mtu = le16_to_cpu(kaweth->configuration.segment_size < KAWETH_MTU ?
1042 kaweth->configuration.segment_size : KAWETH_MTU);
1044 memset(&kaweth->stats, 0, sizeof(kaweth->stats));
1046 kaweth_info("kaweth interface created at %s", kaweth->net->name);
1048 kaweth_dbg("Kaweth probe returning.");
1053 usb_free_urb(kaweth->rx_urb);
1055 usb_free_urb(kaweth->tx_urb);
1061 /****************************************************************
1063 ****************************************************************/
1064 static void kaweth_disconnect(struct usb_device *dev, void *ptr)
1066 struct kaweth_device *kaweth = ptr;
1068 kaweth_info("Unregistering");
1071 kaweth_warn("unregistering non-existant device");
1075 kaweth->removed = 1;
1076 usb_unlink_urb(kaweth->irq_urb);
1077 usb_unlink_urb(kaweth->rx_urb);
1079 /* we need to wait for the urb to be cancelled, if it is active */
1080 spin_lock_irq(&kaweth->device_lock);
1081 if (usb_unlink_urb(kaweth->tx_urb) == -EINPROGRESS) {
1082 spin_unlock_irq(&kaweth->device_lock);
1083 wait_event(kaweth->term_wait, kaweth->end);
1085 spin_unlock_irq(&kaweth->device_lock);
1089 if(kaweth->net->flags & IFF_UP) {
1090 kaweth_dbg("Closing net device");
1091 dev_close(kaweth->net);
1094 kaweth_dbg("Unregistering net device");
1095 unregister_netdev(kaweth->net);
1098 usb_free_urb(kaweth->rx_urb);
1099 usb_free_urb(kaweth->tx_urb);
1105 // FIXME this completion stuff is a modified clone of
1106 // an OLD version of some stuff in usb.c ...
1107 struct kw_api_data {
1108 wait_queue_head_t wqh;
1112 /*-------------------------------------------------------------------*
1113 * completion handler for compatibility wrappers (sync control/bulk) *
1114 *-------------------------------------------------------------------*/
1115 static void usb_api_blocking_completion(struct urb *urb)
1117 struct kw_api_data *awd = (struct kw_api_data *)urb->context;
1123 /*-------------------------------------------------------------------*
1124 * COMPATIBILITY STUFF *
1125 *-------------------------------------------------------------------*/
1127 // Starts urb and waits for completion or timeout
1128 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1130 DECLARE_WAITQUEUE(wait, current);
1131 struct kw_api_data awd;
1134 init_waitqueue_head(&awd.wqh);
1137 set_current_state(TASK_INTERRUPTIBLE);
1138 add_wait_queue(&awd.wqh, &wait);
1139 urb->context = &awd;
1140 status = usb_submit_urb(urb);
1142 // something went wrong
1144 set_current_state(TASK_RUNNING);
1145 remove_wait_queue(&awd.wqh, &wait);
1149 while (timeout && !awd.done)
1150 timeout = schedule_timeout(timeout);
1152 set_current_state(TASK_RUNNING);
1153 remove_wait_queue(&awd.wqh, &wait);
1157 kaweth_warn("usb_control/bulk_msg: timeout");
1158 usb_unlink_urb(urb); // remove urb safely
1159 status = -ETIMEDOUT;
1162 status = urb->status;
1165 if (actual_length) {
1166 *actual_length = urb->actual_length;
1173 /*-------------------------------------------------------------------*/
1174 // returns status (negative) or length (positive)
1175 int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
1176 struct usb_ctrlrequest *cmd, void *data, int len,
1183 urb = usb_alloc_urb(0);
1187 FILL_CONTROL_URB(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1188 len, (usb_complete_t)usb_api_blocking_completion,0);
1190 retv = usb_start_wait_urb(urb, timeout, &length);
1200 /****************************************************************
1202 ****************************************************************/
1203 int __init kaweth_init(void)
1205 kaweth_dbg("Driver loading");
1206 return usb_register(&kaweth_driver);
1209 /****************************************************************
1211 ****************************************************************/
1212 void __exit kaweth_exit(void)
1214 usb_deregister(&kaweth_driver);
1217 module_init(kaweth_init);
1218 module_exit(kaweth_exit);