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/slab.h>
 
  50 #include <linux/string.h>
 
  51 #include <linux/init.h>
 
  52 #include <linux/delay.h>
 
  53 #include <linux/netdevice.h>
 
  54 #include <linux/etherdevice.h>
 
  55 #include <linux/usb.h>
 
  56 #include <linux/types.h>
 
  57 #include <linux/ethtool.h>
 
  58 #include <linux/dma-mapping.h>
 
  59 #include <linux/wait.h>
 
  60 #include <linux/firmware.h>
 
  61 #include <asm/uaccess.h>
 
  62 #include <asm/byteorder.h>
 
  66 #define KAWETH_MTU                      1514
 
  67 #define KAWETH_BUF_SIZE                 1664
 
  68 #define KAWETH_TX_TIMEOUT               (5 * HZ)
 
  69 #define KAWETH_SCRATCH_SIZE             32
 
  70 #define KAWETH_FIRMWARE_BUF_SIZE        4096
 
  71 #define KAWETH_CONTROL_TIMEOUT          (30000)
 
  73 #define KAWETH_STATUS_BROKEN            0x0000001
 
  74 #define KAWETH_STATUS_CLOSING           0x0000002
 
  75 #define KAWETH_STATUS_SUSPENDING        0x0000004
 
  77 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
 
  79 #define KAWETH_PACKET_FILTER_PROMISCUOUS        0x01
 
  80 #define KAWETH_PACKET_FILTER_ALL_MULTICAST      0x02
 
  81 #define KAWETH_PACKET_FILTER_DIRECTED           0x04
 
  82 #define KAWETH_PACKET_FILTER_BROADCAST          0x08
 
  83 #define KAWETH_PACKET_FILTER_MULTICAST          0x10
 
  86 #define KAWETH_COMMAND_GET_ETHERNET_DESC        0x00
 
  87 #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
 
  88 #define KAWETH_COMMAND_SET_PACKET_FILTER        0x02
 
  89 #define KAWETH_COMMAND_STATISTICS               0x03
 
  90 #define KAWETH_COMMAND_SET_TEMP_MAC             0x06
 
  91 #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
 
  92 #define KAWETH_COMMAND_SET_URB_SIZE             0x08
 
  93 #define KAWETH_COMMAND_SET_SOFS_WAIT            0x09
 
  94 #define KAWETH_COMMAND_SCAN                     0xFF
 
  96 #define KAWETH_SOFS_TO_WAIT                     0x05
 
  98 #define INTBUFFERSIZE                           4
 
 100 #define STATE_OFFSET                            0
 
 101 #define STATE_MASK                              0x40
 
 102 #define STATE_SHIFT                             5
 
 104 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
 
 107 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>");
 
 108 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
 
 109 MODULE_LICENSE("GPL");
 
 110 MODULE_FIRMWARE("kaweth/new_code.bin");
 
 111 MODULE_FIRMWARE("kaweth/new_code_fix.bin");
 
 112 MODULE_FIRMWARE("kaweth/trigger_code.bin");
 
 113 MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
 
 115 static const char driver_name[] = "kaweth";
 
 117 static int kaweth_probe(
 
 118                 struct usb_interface *intf,
 
 119                 const struct usb_device_id *id  /* from id_table */
 
 121 static void kaweth_disconnect(struct usb_interface *intf);
 
 122 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
 
 124                                        struct usb_ctrlrequest *cmd, void *data,
 
 125                                        int len, int timeout);
 
 126 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
 
 127 static int kaweth_resume(struct usb_interface *intf);
 
 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(0x07aa, 0x0001) }, /* Correga K.K. */
 
 149         { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
 
 150         { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
 
 151         { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
 
 152         { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
 
 153         { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
 
 154         { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
 
 155         { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
 
 156         { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
 
 157         { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
 
 158         { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
 
 159         { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
 
 160         { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
 
 161         { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
 
 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(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
 
 166         { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
 
 167         {} /* Null terminator */
 
 170 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
 
 172 /****************************************************************
 
 174  ****************************************************************/
 
 175 static struct usb_driver kaweth_driver = {
 
 177         .probe =        kaweth_probe,
 
 178         .disconnect =   kaweth_disconnect,
 
 179         .suspend =      kaweth_suspend,
 
 180         .resume =       kaweth_resume,
 
 181         .id_table =     usb_klsi_table,
 
 182         .supports_autosuspend = 1,
 
 185 typedef __u8 eth_addr_t[6];
 
 187 /****************************************************************
 
 189  ****************************************************************/
 
 197 /****************************************************************
 
 198  *     kaweth_ethernet_configuration
 
 200  ****************************************************************/
 
 201 struct kaweth_ethernet_configuration
 
 207         __u32 statistics_mask;
 
 209         __u16 max_multicast_filters;
 
 211 } __attribute__ ((packed));
 
 213 /****************************************************************
 
 215  ****************************************************************/
 
 218         spinlock_t device_lock;
 
 222         int suspend_lowmem_rx;
 
 223         int suspend_lowmem_ctrl;
 
 226         struct delayed_work lowmem_work;
 
 228         struct usb_device *dev;
 
 229         struct usb_interface *intf;
 
 230         struct net_device *net;
 
 231         wait_queue_head_t term_wait;
 
 237         dma_addr_t intbufferhandle;
 
 239         dma_addr_t rxbufferhandle;
 
 243         struct sk_buff *tx_skb;
 
 246         __u8 scratch[KAWETH_SCRATCH_SIZE];
 
 247         __u16 packet_filter_bitmap;
 
 249         struct kaweth_ethernet_configuration configuration;
 
 251         struct net_device_stats stats;
 
 254 /****************************************************************
 
 256  ****************************************************************/
 
 257 static int kaweth_control(struct kaweth_device *kaweth,
 
 267         struct usb_ctrlrequest *dr;
 
 269         dbg("kaweth_control()");
 
 272                 dbg("in_interrupt()");
 
 276         dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
 
 279                 dbg("kmalloc() failed");
 
 283         dr->bRequestType= requesttype;
 
 284         dr->bRequest = request;
 
 285         dr->wValue = cpu_to_le16(value);
 
 286         dr->wIndex = cpu_to_le16(index);
 
 287         dr->wLength = cpu_to_le16(size);
 
 289         return kaweth_internal_control_msg(kaweth->dev,
 
 297 /****************************************************************
 
 298  *     kaweth_read_configuration
 
 299  ****************************************************************/
 
 300 static int kaweth_read_configuration(struct kaweth_device *kaweth)
 
 304         dbg("Reading kaweth configuration");
 
 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         dbg("Setting URB size to %d", (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         dbg("Set SOFS wait to %d", (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         dbg("Set receive filter to %d", (unsigned)receive_filter);
 
 373         retval = kaweth_control(kaweth,
 
 374                                 usb_sndctrlpipe(kaweth->dev, 0),
 
 375                                 KAWETH_COMMAND_SET_PACKET_FILTER,
 
 376                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
 
 379                                 (void *)&kaweth->scratch,
 
 381                                 KAWETH_CONTROL_TIMEOUT);
 
 386 /****************************************************************
 
 387  *     kaweth_download_firmware
 
 388  ****************************************************************/
 
 389 static int kaweth_download_firmware(struct kaweth_device *kaweth,
 
 394         const struct firmware *fw;
 
 398         ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
 
 400                 err("Firmware request failed\n");
 
 404         if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
 
 405                 err("Firmware too big: %zu", fw->size);
 
 409         memcpy(kaweth->firmware_buf, fw->data, fw->size);
 
 411         release_firmware(fw);
 
 413         kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
 
 414         kaweth->firmware_buf[3] = data_len >> 8;
 
 415         kaweth->firmware_buf[4] = type;
 
 416         kaweth->firmware_buf[5] = interrupt;
 
 418         dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
 
 419                    kaweth->firmware_buf[2]);
 
 421         dbg("Downloading firmware at %p to kaweth device at %p",
 
 423         dbg("Firmware length: %d", data_len);
 
 425         return kaweth_control(kaweth,
 
 426                               usb_sndctrlpipe(kaweth->dev, 0),
 
 428                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
 
 431                               (void *)kaweth->firmware_buf,
 
 433                               KAWETH_CONTROL_TIMEOUT);
 
 436 /****************************************************************
 
 437  *     kaweth_trigger_firmware
 
 438  ****************************************************************/
 
 439 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
 
 442         kaweth->firmware_buf[0] = 0xB6;
 
 443         kaweth->firmware_buf[1] = 0xC3;
 
 444         kaweth->firmware_buf[2] = 0x01;
 
 445         kaweth->firmware_buf[3] = 0x00;
 
 446         kaweth->firmware_buf[4] = 0x06;
 
 447         kaweth->firmware_buf[5] = interrupt;
 
 448         kaweth->firmware_buf[6] = 0x00;
 
 449         kaweth->firmware_buf[7] = 0x00;
 
 451         dbg("Triggering firmware");
 
 453         return kaweth_control(kaweth,
 
 454                               usb_sndctrlpipe(kaweth->dev, 0),
 
 456                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
 
 459                               (void *)kaweth->firmware_buf,
 
 461                               KAWETH_CONTROL_TIMEOUT);
 
 464 /****************************************************************
 
 466  ****************************************************************/
 
 467 static int kaweth_reset(struct kaweth_device *kaweth)
 
 471         dbg("kaweth_reset(%p)", kaweth);
 
 472         result = kaweth_control(kaweth,
 
 473                                 usb_sndctrlpipe(kaweth->dev, 0),
 
 474                                 USB_REQ_SET_CONFIGURATION,
 
 476                                 kaweth->dev->config[0].desc.bConfigurationValue,
 
 480                                 KAWETH_CONTROL_TIMEOUT);
 
 484         dbg("kaweth_reset() returns %d.",result);
 
 489 static void kaweth_usb_receive(struct urb *);
 
 490 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
 
 492 /****************************************************************
 
 494 *****************************************************************/
 
 496 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
 
 500         status = usb_submit_urb (kaweth->irq_urb, mf);
 
 501         if (unlikely(status == -ENOMEM)) {
 
 502                 kaweth->suspend_lowmem_ctrl = 1;
 
 503                 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
 
 505                 kaweth->suspend_lowmem_ctrl = 0;
 
 509                 err ("can't resubmit intr, %s-%s, status %d",
 
 510                                 kaweth->dev->bus->bus_name,
 
 511                                 kaweth->dev->devpath, status);
 
 514 static void int_callback(struct urb *u)
 
 516         struct kaweth_device *kaweth = u->context;
 
 518         int status = u->status;
 
 521         case 0:                 /* success */
 
 523         case -ECONNRESET:       /* unlink */
 
 527         /* -EPIPE:  should clear the halt */
 
 532         /* we check the link state to report changes */
 
 533         if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
 
 535                         netif_carrier_on(kaweth->net);
 
 537                         netif_carrier_off(kaweth->net);
 
 539                 kaweth->linkstate = act_state;
 
 542         kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
 
 545 static void kaweth_resubmit_tl(struct work_struct *work)
 
 547         struct kaweth_device *kaweth =
 
 548                 container_of(work, struct kaweth_device, lowmem_work.work);
 
 550         if (IS_BLOCKED(kaweth->status))
 
 553         if (kaweth->suspend_lowmem_rx)
 
 554                 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
 
 556         if (kaweth->suspend_lowmem_ctrl)
 
 557                 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
 
 561 /****************************************************************
 
 562  *     kaweth_resubmit_rx_urb
 
 563  ****************************************************************/
 
 564 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
 
 569         usb_fill_bulk_urb(kaweth->rx_urb,
 
 571                       usb_rcvbulkpipe(kaweth->dev, 1),
 
 576         kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 
 577         kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
 
 579         if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
 
 580                 if (result == -ENOMEM) {
 
 581                         kaweth->suspend_lowmem_rx = 1;
 
 582                         schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
 
 584                 err("resubmitting rx_urb %d failed", result);
 
 586                 kaweth->suspend_lowmem_rx = 0;
 
 592 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
 
 594 /****************************************************************
 
 596  ****************************************************************/
 
 597 static void kaweth_usb_receive(struct urb *urb)
 
 599         struct kaweth_device *kaweth = urb->context;
 
 600         struct net_device *net = kaweth->net;
 
 601         int status = urb->status;
 
 603         int count = urb->actual_length;
 
 604         int count2 = urb->transfer_buffer_length;
 
 606         __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
 
 610         if(unlikely(status == -ECONNRESET || status == -ESHUTDOWN))
 
 611         /* we are killed - set a flag and wake the disconnect handler */
 
 614                 wake_up(&kaweth->term_wait);
 
 618         spin_lock(&kaweth->device_lock);
 
 619         if (IS_BLOCKED(kaweth->status)) {
 
 620                 spin_unlock(&kaweth->device_lock);
 
 623         spin_unlock(&kaweth->device_lock);
 
 625         if(status && status != -EREMOTEIO && count != 1) {
 
 626                 err("%s RX status: %d count: %d packet_len: %d",
 
 631                 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
 
 635         if(kaweth->net && (count > 2)) {
 
 636                 if(pkt_len > (count - 2)) {
 
 637                         err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
 
 638                         err("Packet len & 2047: %x", pkt_len & 2047);
 
 639                         err("Count 2: %x", count2);
 
 640                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
 
 644                 if(!(skb = dev_alloc_skb(pkt_len+2))) {
 
 645                         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
 
 649                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
 
 651                 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
 
 653                 skb_put(skb, pkt_len);
 
 655                 skb->protocol = eth_type_trans(skb, net);
 
 659                 kaweth->stats.rx_packets++;
 
 660                 kaweth->stats.rx_bytes += pkt_len;
 
 663         kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
 
 666 /****************************************************************
 
 668  ****************************************************************/
 
 669 static int kaweth_open(struct net_device *net)
 
 671         struct kaweth_device *kaweth = netdev_priv(net);
 
 674         dbg("Opening network device.");
 
 676         res = usb_autopm_get_interface(kaweth->intf);
 
 678                 err("Interface cannot be resumed.");
 
 681         res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
 
 688                 usb_rcvintpipe(kaweth->dev, 3),
 
 693                 250); /* overriding the descriptor */
 
 694         kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
 
 695         kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 
 697         res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
 
 699                 usb_kill_urb(kaweth->rx_urb);
 
 704         netif_start_queue(net);
 
 706         kaweth_async_set_rx_mode(kaweth);
 
 710         usb_autopm_enable(kaweth->intf);
 
 714 /****************************************************************
 
 716  ****************************************************************/
 
 717 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
 
 719         usb_kill_urb(kaweth->irq_urb);
 
 720         usb_kill_urb(kaweth->rx_urb);
 
 721         usb_kill_urb(kaweth->tx_urb);
 
 723         cancel_delayed_work_sync(&kaweth->lowmem_work);
 
 725         /* a scheduled work may have resubmitted,
 
 727         usb_kill_urb(kaweth->irq_urb);
 
 728         usb_kill_urb(kaweth->rx_urb);
 
 731 /****************************************************************
 
 733  ****************************************************************/
 
 734 static int kaweth_close(struct net_device *net)
 
 736         struct kaweth_device *kaweth = netdev_priv(net);
 
 738         netif_stop_queue(net);
 
 741         kaweth->status |= KAWETH_STATUS_CLOSING;
 
 743         kaweth_kill_urbs(kaweth);
 
 745         kaweth->status &= ~KAWETH_STATUS_CLOSING;
 
 747         usb_autopm_enable(kaweth->intf);
 
 752 static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
 
 754         struct kaweth_device *kaweth = netdev_priv(dev);
 
 756         strlcpy(info->driver, driver_name, sizeof(info->driver));
 
 757         usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
 
 760 static u32 kaweth_get_link(struct net_device *dev)
 
 762         struct kaweth_device *kaweth = netdev_priv(dev);
 
 764         return kaweth->linkstate;
 
 767 static struct ethtool_ops ops = {
 
 768         .get_drvinfo    = kaweth_get_drvinfo,
 
 769         .get_link       = kaweth_get_link
 
 772 /****************************************************************
 
 773  *     kaweth_usb_transmit_complete
 
 774  ****************************************************************/
 
 775 static void kaweth_usb_transmit_complete(struct urb *urb)
 
 777         struct kaweth_device *kaweth = urb->context;
 
 778         struct sk_buff *skb = kaweth->tx_skb;
 
 779         int status = urb->status;
 
 781         if (unlikely(status != 0))
 
 782                 if (status != -ENOENT)
 
 783                         dbg("%s: TX status %d.", kaweth->net->name, status);
 
 785         netif_wake_queue(kaweth->net);
 
 786         dev_kfree_skb_irq(skb);
 
 789 /****************************************************************
 
 791  ****************************************************************/
 
 792 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
 
 794         struct kaweth_device *kaweth = netdev_priv(net);
 
 795         __le16 *private_header;
 
 799         spin_lock(&kaweth->device_lock);
 
 801         kaweth_async_set_rx_mode(kaweth);
 
 802         netif_stop_queue(net);
 
 803         if (IS_BLOCKED(kaweth->status)) {
 
 807         /* We now decide whether we can put our special header into the sk_buff */
 
 808         if (skb_cloned(skb) || skb_headroom(skb) < 2) {
 
 809                 /* no such luck - we make our own */
 
 810                 struct sk_buff *copied_skb;
 
 811                 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
 
 812                 dev_kfree_skb_irq(skb);
 
 815                         kaweth->stats.tx_errors++;
 
 816                         netif_start_queue(net);
 
 817                         spin_unlock(&kaweth->device_lock);
 
 822         private_header = (__le16 *)__skb_push(skb, 2);
 
 823         *private_header = cpu_to_le16(skb->len-2);
 
 824         kaweth->tx_skb = skb;
 
 826         usb_fill_bulk_urb(kaweth->tx_urb,
 
 828                       usb_sndbulkpipe(kaweth->dev, 2),
 
 831                       kaweth_usb_transmit_complete,
 
 835         if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
 
 837                 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
 
 839                 kaweth->stats.tx_errors++;
 
 841                 netif_start_queue(net);
 
 842                 dev_kfree_skb_irq(skb);
 
 846                 kaweth->stats.tx_packets++;
 
 847                 kaweth->stats.tx_bytes += skb->len;
 
 848                 net->trans_start = jiffies;
 
 851         spin_unlock(&kaweth->device_lock);
 
 856 /****************************************************************
 
 858  ****************************************************************/
 
 859 static void kaweth_set_rx_mode(struct net_device *net)
 
 861         struct kaweth_device *kaweth = netdev_priv(net);
 
 863         __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
 
 864                                      KAWETH_PACKET_FILTER_BROADCAST |
 
 865                                      KAWETH_PACKET_FILTER_MULTICAST;
 
 867         dbg("Setting Rx mode to %d", packet_filter_bitmap);
 
 869         netif_stop_queue(net);
 
 871         if (net->flags & IFF_PROMISC) {
 
 872                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
 
 874         else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
 
 875                 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
 
 878         kaweth->packet_filter_bitmap = packet_filter_bitmap;
 
 879         netif_wake_queue(net);
 
 882 /****************************************************************
 
 883  *     kaweth_async_set_rx_mode
 
 884  ****************************************************************/
 
 885 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
 
 887         __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
 
 888         kaweth->packet_filter_bitmap = 0;
 
 889         if (packet_filter_bitmap == 0)
 
 894         result = kaweth_control(kaweth,
 
 895                                 usb_sndctrlpipe(kaweth->dev, 0),
 
 896                                 KAWETH_COMMAND_SET_PACKET_FILTER,
 
 897                                 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
 
 898                                 packet_filter_bitmap,
 
 900                                 (void *)&kaweth->scratch,
 
 902                                 KAWETH_CONTROL_TIMEOUT);
 
 905                 err("Failed to set Rx mode: %d", result);
 
 908                 dbg("Set Rx mode to %d", packet_filter_bitmap);
 
 913 /****************************************************************
 
 914  *     kaweth_netdev_stats
 
 915  ****************************************************************/
 
 916 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
 
 918         struct kaweth_device *kaweth = netdev_priv(dev);
 
 919         return &kaweth->stats;
 
 922 /****************************************************************
 
 924  ****************************************************************/
 
 925 static void kaweth_tx_timeout(struct net_device *net)
 
 927         struct kaweth_device *kaweth = netdev_priv(net);
 
 929         dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
 
 930         kaweth->stats.tx_errors++;
 
 931         net->trans_start = jiffies;
 
 933         usb_unlink_urb(kaweth->tx_urb);
 
 936 /****************************************************************
 
 938  ****************************************************************/
 
 939 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
 
 941         struct kaweth_device *kaweth = usb_get_intfdata(intf);
 
 944         dbg("Suspending device");
 
 945         spin_lock_irqsave(&kaweth->device_lock, flags);
 
 946         kaweth->status |= KAWETH_STATUS_SUSPENDING;
 
 947         spin_unlock_irqrestore(&kaweth->device_lock, flags);
 
 949         kaweth_kill_urbs(kaweth);
 
 953 /****************************************************************
 
 955  ****************************************************************/
 
 956 static int kaweth_resume(struct usb_interface *intf)
 
 958         struct kaweth_device *kaweth = usb_get_intfdata(intf);
 
 961         dbg("Resuming device");
 
 962         spin_lock_irqsave(&kaweth->device_lock, flags);
 
 963         kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
 
 964         spin_unlock_irqrestore(&kaweth->device_lock, flags);
 
 968         kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
 
 969         kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
 
 974 /****************************************************************
 
 976  ****************************************************************/
 
 979 static const struct net_device_ops kaweth_netdev_ops = {
 
 980         .ndo_open =                     kaweth_open,
 
 981         .ndo_stop =                     kaweth_close,
 
 982         .ndo_start_xmit =               kaweth_start_xmit,
 
 983         .ndo_tx_timeout =               kaweth_tx_timeout,
 
 984         .ndo_set_multicast_list =       kaweth_set_rx_mode,
 
 985         .ndo_get_stats =                kaweth_netdev_stats,
 
 988 static int kaweth_probe(
 
 989                 struct usb_interface *intf,
 
 990                 const struct usb_device_id *id      /* from id_table */
 
 993         struct usb_device *dev = 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 };
 
 999         dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
 
1001                  le16_to_cpu(dev->descriptor.idVendor),
 
1002                  le16_to_cpu(dev->descriptor.idProduct),
 
1003                  le16_to_cpu(dev->descriptor.bcdDevice));
 
1005         dbg("Device at %p", dev);
 
1007         dbg("Descriptor length: %x type: %x",
 
1008                  (int)dev->descriptor.bLength,
 
1009                  (int)dev->descriptor.bDescriptorType);
 
1011         netdev = alloc_etherdev(sizeof(*kaweth));
 
1015         kaweth = netdev_priv(netdev);
 
1017         kaweth->net = netdev;
 
1019         spin_lock_init(&kaweth->device_lock);
 
1020         init_waitqueue_head(&kaweth->term_wait);
 
1024         kaweth_reset(kaweth);
 
1027          * If high byte of bcdDevice is nonzero, firmware is already
 
1028          * downloaded. Don't try to do it again, or we'll hang the device.
 
1031         if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
 
1032                 dev_info(&intf->dev, "Firmware present in device.\n");
 
1034                 /* Download the firmware */
 
1035                 dev_info(&intf->dev, "Downloading firmware...\n");
 
1036                 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
 
1037                 if ((result = kaweth_download_firmware(kaweth,
 
1038                                                       "kaweth/new_code.bin",
 
1041                         err("Error downloading firmware (%d)", result);
 
1045                 if ((result = kaweth_download_firmware(kaweth,
 
1046                                                       "kaweth/new_code_fix.bin",
 
1049                         err("Error downloading firmware fix (%d)", result);
 
1053                 if ((result = kaweth_download_firmware(kaweth,
 
1054                                                       "kaweth/trigger_code.bin",
 
1057                         err("Error downloading trigger code (%d)", result);
 
1062                 if ((result = kaweth_download_firmware(kaweth,
 
1063                                                       "kaweth/trigger_code_fix.bin",
 
1066                         err("Error downloading trigger code fix (%d)", result);
 
1071                 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
 
1072                         err("Error triggering firmware (%d)", result);
 
1076                 /* Device will now disappear for a moment...  */
 
1077                 dev_info(&intf->dev, "Firmware loaded.  I'll be back...\n");
 
1079                 free_page((unsigned long)kaweth->firmware_buf);
 
1080                 free_netdev(netdev);
 
1084         result = kaweth_read_configuration(kaweth);
 
1087                 err("Error reading configuration (%d), no net device created", result);
 
1088                 goto err_free_netdev;
 
1091         dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
 
1092         dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
 
1093         dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
 
1094         dev_info(&intf->dev, "Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
 
1095                  (int)kaweth->configuration.hw_addr[0],
 
1096                  (int)kaweth->configuration.hw_addr[1],
 
1097                  (int)kaweth->configuration.hw_addr[2],
 
1098                  (int)kaweth->configuration.hw_addr[3],
 
1099                  (int)kaweth->configuration.hw_addr[4],
 
1100                  (int)kaweth->configuration.hw_addr[5]);
 
1102         if(!memcmp(&kaweth->configuration.hw_addr,
 
1104                    sizeof(bcast_addr))) {
 
1105                 err("Firmware not functioning properly, no net device created");
 
1106                 goto err_free_netdev;
 
1109         if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
 
1110                 dbg("Error setting URB size");
 
1111                 goto err_free_netdev;
 
1114         if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
 
1115                 err("Error setting SOFS wait");
 
1116                 goto err_free_netdev;
 
1119         result = kaweth_set_receive_filter(kaweth,
 
1120                                            KAWETH_PACKET_FILTER_DIRECTED |
 
1121                                            KAWETH_PACKET_FILTER_BROADCAST |
 
1122                                            KAWETH_PACKET_FILTER_MULTICAST);
 
1125                 err("Error setting receive filter");
 
1126                 goto err_free_netdev;
 
1129         dbg("Initializing net device.");
 
1131         kaweth->intf = intf;
 
1133         kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
 
1134         if (!kaweth->tx_urb)
 
1135                 goto err_free_netdev;
 
1136         kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
 
1137         if (!kaweth->rx_urb)
 
1139         kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
 
1140         if (!kaweth->irq_urb)
 
1143         kaweth->intbuffer = usb_buffer_alloc(   kaweth->dev,
 
1146                                                 &kaweth->intbufferhandle);
 
1147         if (!kaweth->intbuffer)
 
1148                 goto err_tx_and_rx_and_irq;
 
1149         kaweth->rx_buf = usb_buffer_alloc(      kaweth->dev,
 
1152                                                 &kaweth->rxbufferhandle);
 
1153         if (!kaweth->rx_buf)
 
1154                 goto err_all_but_rxbuf;
 
1156         memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
 
1157         memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
 
1158                sizeof(kaweth->configuration.hw_addr));
 
1160         netdev->netdev_ops = &kaweth_netdev_ops;
 
1161         netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
 
1162         netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
 
1163         SET_ETHTOOL_OPS(netdev, &ops);
 
1165         /* kaweth is zeroed as part of alloc_netdev */
 
1166         INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
 
1167         usb_set_intfdata(intf, kaweth);
 
1170 // dma_supported() is deeply broken on almost all architectures
 
1171         if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
 
1172                 kaweth->net->features |= NETIF_F_HIGHDMA;
 
1175         SET_NETDEV_DEV(netdev, &intf->dev);
 
1176         if (register_netdev(netdev) != 0) {
 
1177                 err("Error registering netdev.");
 
1181         dev_info(&intf->dev, "kaweth interface created at %s\n",
 
1184         dbg("Kaweth probe returning.");
 
1189         usb_set_intfdata(intf, NULL);
 
1190         usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
 
1192         usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
 
1193 err_tx_and_rx_and_irq:
 
1194         usb_free_urb(kaweth->irq_urb);
 
1196         usb_free_urb(kaweth->rx_urb);
 
1198         usb_free_urb(kaweth->tx_urb);
 
1200         free_netdev(netdev);
 
1205 /****************************************************************
 
1207  ****************************************************************/
 
1208 static void kaweth_disconnect(struct usb_interface *intf)
 
1210         struct kaweth_device *kaweth = usb_get_intfdata(intf);
 
1211         struct net_device *netdev;
 
1213         dev_info(&intf->dev, "Unregistering\n");
 
1215         usb_set_intfdata(intf, NULL);
 
1217                 dev_warn(&intf->dev, "unregistering non-existant device\n");
 
1220         netdev = kaweth->net;
 
1222         dbg("Unregistering net device");
 
1223         unregister_netdev(netdev);
 
1225         usb_free_urb(kaweth->rx_urb);
 
1226         usb_free_urb(kaweth->tx_urb);
 
1227         usb_free_urb(kaweth->irq_urb);
 
1229         usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
 
1230         usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
 
1232         free_netdev(netdev);
 
1236 // FIXME this completion stuff is a modified clone of
 
1237 // an OLD version of some stuff in usb.c ...
 
1238 struct usb_api_data {
 
1239         wait_queue_head_t wqh;
 
1243 /*-------------------------------------------------------------------*
 
1244  * completion handler for compatibility wrappers (sync control/bulk) *
 
1245  *-------------------------------------------------------------------*/
 
1246 static void usb_api_blocking_completion(struct urb *urb)
 
1248         struct usb_api_data *awd = (struct usb_api_data *)urb->context;
 
1254 /*-------------------------------------------------------------------*
 
1255  *                         COMPATIBILITY STUFF                       *
 
1256  *-------------------------------------------------------------------*/
 
1258 // Starts urb and waits for completion or timeout
 
1259 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
 
1261         struct usb_api_data awd;
 
1264         init_waitqueue_head(&awd.wqh);
 
1267         urb->context = &awd;
 
1268         status = usb_submit_urb(urb, GFP_NOIO);
 
1270                 // something went wrong
 
1275         if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
 
1277                 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
 
1278                 usb_kill_urb(urb);  // remove urb safely
 
1279                 status = -ETIMEDOUT;
 
1282                 status = urb->status;
 
1285         if (actual_length) {
 
1286                 *actual_length = urb->actual_length;
 
1293 /*-------------------------------------------------------------------*/
 
1294 // returns status (negative) or length (positive)
 
1295 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
 
1297                                        struct usb_ctrlrequest *cmd, void *data,
 
1298                                        int len, int timeout)
 
1302         int length = 0; /* shut up GCC */
 
1304         urb = usb_alloc_urb(0, GFP_NOIO);
 
1308         usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
 
1309                          len, usb_api_blocking_completion, NULL);
 
1311         retv = usb_start_wait_urb(urb, timeout, &length);
 
1321 /****************************************************************
 
1323  ****************************************************************/
 
1324 static int __init kaweth_init(void)
 
1326         dbg("Driver loading");
 
1327         return usb_register(&kaweth_driver);
 
1330 /****************************************************************
 
1332  ****************************************************************/
 
1333 static void __exit kaweth_exit(void)
 
1335         usb_deregister(&kaweth_driver);
 
1338 module_init(kaweth_init);
 
1339 module_exit(kaweth_exit);