0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include <linux/module.h>
0034 #include <linux/slab.h>
0035 #include <linux/string.h>
0036 #include <linux/delay.h>
0037 #include <linux/netdevice.h>
0038 #include <linux/etherdevice.h>
0039 #include <linux/usb.h>
0040 #include <linux/types.h>
0041 #include <linux/ethtool.h>
0042 #include <linux/dma-mapping.h>
0043 #include <linux/wait.h>
0044 #include <linux/firmware.h>
0045 #include <linux/uaccess.h>
0046 #include <asm/byteorder.h>
0047
0048 #undef DEBUG
0049
0050 #define KAWETH_MTU 1514
0051 #define KAWETH_BUF_SIZE 1664
0052 #define KAWETH_TX_TIMEOUT (5 * HZ)
0053 #define KAWETH_SCRATCH_SIZE 32
0054 #define KAWETH_FIRMWARE_BUF_SIZE 4096
0055 #define KAWETH_CONTROL_TIMEOUT (30000)
0056
0057 #define KAWETH_STATUS_BROKEN 0x0000001
0058 #define KAWETH_STATUS_CLOSING 0x0000002
0059 #define KAWETH_STATUS_SUSPENDING 0x0000004
0060
0061 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
0062
0063 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
0064 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
0065 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
0066 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
0067 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
0068
0069
0070 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
0071 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
0072 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
0073 #define KAWETH_COMMAND_STATISTICS 0x03
0074 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
0075 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
0076 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
0077 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
0078 #define KAWETH_COMMAND_SCAN 0xFF
0079
0080 #define KAWETH_SOFS_TO_WAIT 0x05
0081
0082 #define INTBUFFERSIZE 4
0083
0084 #define STATE_OFFSET 0
0085 #define STATE_MASK 0x40
0086 #define STATE_SHIFT 5
0087
0088 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
0089
0090
0091 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>");
0092 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
0093 MODULE_LICENSE("GPL");
0094 MODULE_FIRMWARE("kaweth/new_code.bin");
0095 MODULE_FIRMWARE("kaweth/new_code_fix.bin");
0096 MODULE_FIRMWARE("kaweth/trigger_code.bin");
0097 MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
0098
0099 static const char driver_name[] = "kaweth";
0100
0101 static int kaweth_probe(
0102 struct usb_interface *intf,
0103 const struct usb_device_id *id
0104 );
0105 static void kaweth_disconnect(struct usb_interface *intf);
0106 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
0107 static int kaweth_resume(struct usb_interface *intf);
0108
0109
0110
0111
0112 static const struct usb_device_id usb_klsi_table[] = {
0113 { USB_DEVICE(0x03e8, 0x0008) },
0114 { USB_DEVICE(0x04bb, 0x0901) },
0115 { USB_DEVICE(0x0506, 0x03e8) },
0116 { USB_DEVICE(0x0506, 0x11f8) },
0117 { USB_DEVICE(0x0557, 0x2002) },
0118 { USB_DEVICE(0x0557, 0x4000) },
0119 { USB_DEVICE(0x0565, 0x0002) },
0120 { USB_DEVICE(0x0565, 0x0003) },
0121 { USB_DEVICE(0x0565, 0x0005) },
0122 { USB_DEVICE(0x05e9, 0x0008) },
0123 { USB_DEVICE(0x05e9, 0x0009) },
0124 { USB_DEVICE(0x066b, 0x2202) },
0125 { USB_DEVICE(0x06e1, 0x0008) },
0126 { USB_DEVICE(0x06e1, 0x0009) },
0127 { USB_DEVICE(0x0707, 0x0100) },
0128 { USB_DEVICE(0x07aa, 0x0001) },
0129 { USB_DEVICE(0x07b8, 0x4000) },
0130 { USB_DEVICE(0x07c9, 0xb010) },
0131 { USB_DEVICE(0x0846, 0x1001) },
0132 { USB_DEVICE(0x0846, 0x1002) },
0133 { USB_DEVICE(0x085a, 0x0008) },
0134 { USB_DEVICE(0x085a, 0x0009) },
0135 { USB_DEVICE(0x087d, 0x5704) },
0136 { USB_DEVICE(0x0951, 0x0008) },
0137 { USB_DEVICE(0x095a, 0x3003) },
0138 { USB_DEVICE(0x10bd, 0x1427) },
0139 { USB_DEVICE(0x1342, 0x0204) },
0140 { USB_DEVICE(0x13d2, 0x0400) },
0141 { USB_DEVICE(0x1485, 0x0001) },
0142 { USB_DEVICE(0x1485, 0x0002) },
0143 { USB_DEVICE(0x1645, 0x0005) },
0144 { USB_DEVICE(0x1645, 0x0008) },
0145 { USB_DEVICE(0x1645, 0x8005) },
0146 { USB_DEVICE(0x1668, 0x0323) },
0147 { USB_DEVICE(0x2001, 0x4000) },
0148 {}
0149 };
0150
0151 MODULE_DEVICE_TABLE (usb, usb_klsi_table);
0152
0153
0154
0155
0156 static struct usb_driver kaweth_driver = {
0157 .name = driver_name,
0158 .probe = kaweth_probe,
0159 .disconnect = kaweth_disconnect,
0160 .suspend = kaweth_suspend,
0161 .resume = kaweth_resume,
0162 .id_table = usb_klsi_table,
0163 .supports_autosuspend = 1,
0164 .disable_hub_initiated_lpm = 1,
0165 };
0166
0167 typedef __u8 eth_addr_t[6];
0168
0169
0170
0171
0172 struct usb_eth_dev {
0173 char *name;
0174 __u16 vendor;
0175 __u16 device;
0176 void *pdata;
0177 };
0178
0179
0180
0181
0182
0183 struct kaweth_ethernet_configuration
0184 {
0185 __u8 size;
0186 __u8 reserved1;
0187 __u8 reserved2;
0188 eth_addr_t hw_addr;
0189 __u32 statistics_mask;
0190 __le16 segment_size;
0191 __u16 max_multicast_filters;
0192 __u8 reserved3;
0193 } __packed;
0194
0195
0196
0197
0198 struct kaweth_device
0199 {
0200 spinlock_t device_lock;
0201
0202 __u32 status;
0203 int end;
0204 int suspend_lowmem_rx;
0205 int suspend_lowmem_ctrl;
0206 int linkstate;
0207 int opened;
0208 struct delayed_work lowmem_work;
0209
0210 struct usb_device *dev;
0211 struct usb_interface *intf;
0212 struct net_device *net;
0213 wait_queue_head_t term_wait;
0214
0215 struct urb *rx_urb;
0216 struct urb *tx_urb;
0217 struct urb *irq_urb;
0218
0219 dma_addr_t intbufferhandle;
0220 __u8 *intbuffer;
0221 dma_addr_t rxbufferhandle;
0222 __u8 *rx_buf;
0223
0224
0225 struct sk_buff *tx_skb;
0226
0227 __u8 *firmware_buf;
0228 __u8 scratch[KAWETH_SCRATCH_SIZE];
0229 __u16 packet_filter_bitmap;
0230
0231 struct kaweth_ethernet_configuration configuration;
0232 };
0233
0234
0235
0236
0237 static int kaweth_read_configuration(struct kaweth_device *kaweth)
0238 {
0239 return usb_control_msg(kaweth->dev, usb_rcvctrlpipe(kaweth->dev, 0),
0240 KAWETH_COMMAND_GET_ETHERNET_DESC,
0241 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
0242 0, 0,
0243 &kaweth->configuration,
0244 sizeof(kaweth->configuration),
0245 KAWETH_CONTROL_TIMEOUT);
0246 }
0247
0248
0249
0250
0251 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
0252 {
0253 netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
0254
0255 return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0256 KAWETH_COMMAND_SET_URB_SIZE,
0257 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0258 urb_size, 0,
0259 &kaweth->scratch, 0,
0260 KAWETH_CONTROL_TIMEOUT);
0261 }
0262
0263
0264
0265
0266 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
0267 {
0268 netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
0269
0270 return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0271 KAWETH_COMMAND_SET_SOFS_WAIT,
0272 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0273 sofs_wait, 0,
0274 &kaweth->scratch, 0,
0275 KAWETH_CONTROL_TIMEOUT);
0276 }
0277
0278
0279
0280
0281 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
0282 __u16 receive_filter)
0283 {
0284 netdev_dbg(kaweth->net, "Set receive filter to %d\n",
0285 (unsigned)receive_filter);
0286
0287 return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0288 KAWETH_COMMAND_SET_PACKET_FILTER,
0289 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0290 receive_filter, 0,
0291 &kaweth->scratch, 0,
0292 KAWETH_CONTROL_TIMEOUT);
0293 }
0294
0295
0296
0297
0298 static int kaweth_download_firmware(struct kaweth_device *kaweth,
0299 const char *fwname,
0300 __u8 interrupt,
0301 __u8 type)
0302 {
0303 const struct firmware *fw;
0304 int data_len;
0305 int ret;
0306
0307 ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
0308 if (ret) {
0309 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
0310 return ret;
0311 }
0312
0313 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
0314 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
0315 fw->size);
0316 release_firmware(fw);
0317 return -ENOSPC;
0318 }
0319 data_len = fw->size;
0320 memcpy(kaweth->firmware_buf, fw->data, fw->size);
0321
0322 release_firmware(fw);
0323
0324 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
0325 kaweth->firmware_buf[3] = data_len >> 8;
0326 kaweth->firmware_buf[4] = type;
0327 kaweth->firmware_buf[5] = interrupt;
0328
0329 netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
0330 kaweth->firmware_buf[2]);
0331
0332 netdev_dbg(kaweth->net,
0333 "Downloading firmware at %p to kaweth device at %p\n",
0334 kaweth->firmware_buf, kaweth);
0335 netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
0336
0337 return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0338 KAWETH_COMMAND_SCAN,
0339 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0340 0, 0,
0341 kaweth->firmware_buf, data_len,
0342 KAWETH_CONTROL_TIMEOUT);
0343 }
0344
0345
0346
0347
0348 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
0349 __u8 interrupt)
0350 {
0351 kaweth->firmware_buf[0] = 0xB6;
0352 kaweth->firmware_buf[1] = 0xC3;
0353 kaweth->firmware_buf[2] = 0x01;
0354 kaweth->firmware_buf[3] = 0x00;
0355 kaweth->firmware_buf[4] = 0x06;
0356 kaweth->firmware_buf[5] = interrupt;
0357 kaweth->firmware_buf[6] = 0x00;
0358 kaweth->firmware_buf[7] = 0x00;
0359
0360 return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0361 KAWETH_COMMAND_SCAN,
0362 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0363 0, 0,
0364 (void *)kaweth->firmware_buf, 8,
0365 KAWETH_CONTROL_TIMEOUT);
0366 }
0367
0368
0369
0370
0371 static int kaweth_reset(struct kaweth_device *kaweth)
0372 {
0373 int result;
0374
0375 result = usb_reset_configuration(kaweth->dev);
0376 mdelay(10);
0377
0378 netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
0379
0380 return result;
0381 }
0382
0383 static void kaweth_usb_receive(struct urb *);
0384 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
0385
0386
0387
0388
0389
0390 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
0391 {
0392 int status;
0393
0394 status = usb_submit_urb (kaweth->irq_urb, mf);
0395 if (unlikely(status == -ENOMEM)) {
0396 kaweth->suspend_lowmem_ctrl = 1;
0397 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
0398 } else {
0399 kaweth->suspend_lowmem_ctrl = 0;
0400 }
0401
0402 if (status)
0403 dev_err(&kaweth->intf->dev,
0404 "can't resubmit intr, %s-%s, status %d\n",
0405 kaweth->dev->bus->bus_name,
0406 kaweth->dev->devpath, status);
0407 }
0408
0409 static void int_callback(struct urb *u)
0410 {
0411 struct kaweth_device *kaweth = u->context;
0412 int act_state;
0413 int status = u->status;
0414
0415 switch (status) {
0416 case 0:
0417 break;
0418 case -ECONNRESET:
0419 case -ENOENT:
0420 case -ESHUTDOWN:
0421 return;
0422
0423 default:
0424 goto resubmit;
0425 }
0426
0427
0428 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
0429 if (act_state)
0430 netif_carrier_on(kaweth->net);
0431 else
0432 netif_carrier_off(kaweth->net);
0433
0434 kaweth->linkstate = act_state;
0435 }
0436 resubmit:
0437 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
0438 }
0439
0440 static void kaweth_resubmit_tl(struct work_struct *work)
0441 {
0442 struct kaweth_device *kaweth =
0443 container_of(work, struct kaweth_device, lowmem_work.work);
0444
0445 if (IS_BLOCKED(kaweth->status))
0446 return;
0447
0448 if (kaweth->suspend_lowmem_rx)
0449 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
0450
0451 if (kaweth->suspend_lowmem_ctrl)
0452 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
0453 }
0454
0455
0456
0457
0458
0459 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
0460 gfp_t mem_flags)
0461 {
0462 int result;
0463
0464 usb_fill_bulk_urb(kaweth->rx_urb,
0465 kaweth->dev,
0466 usb_rcvbulkpipe(kaweth->dev, 1),
0467 kaweth->rx_buf,
0468 KAWETH_BUF_SIZE,
0469 kaweth_usb_receive,
0470 kaweth);
0471 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0472 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
0473
0474 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
0475 if (result == -ENOMEM) {
0476 kaweth->suspend_lowmem_rx = 1;
0477 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
0478 }
0479 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
0480 result);
0481 } else {
0482 kaweth->suspend_lowmem_rx = 0;
0483 }
0484
0485 return result;
0486 }
0487
0488 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
0489 bool may_sleep);
0490
0491
0492
0493
0494 static void kaweth_usb_receive(struct urb *urb)
0495 {
0496 struct device *dev = &urb->dev->dev;
0497 struct kaweth_device *kaweth = urb->context;
0498 struct net_device *net = kaweth->net;
0499 int status = urb->status;
0500 unsigned long flags;
0501 int count = urb->actual_length;
0502 int count2 = urb->transfer_buffer_length;
0503
0504 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
0505
0506 struct sk_buff *skb;
0507
0508 if (unlikely(status == -EPIPE)) {
0509 net->stats.rx_errors++;
0510 kaweth->end = 1;
0511 wake_up(&kaweth->term_wait);
0512 dev_dbg(dev, "Status was -EPIPE.\n");
0513 return;
0514 }
0515 if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
0516
0517 kaweth->end = 1;
0518 wake_up(&kaweth->term_wait);
0519 dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
0520 return;
0521 }
0522 if (unlikely(status == -EPROTO || status == -ETIME ||
0523 status == -EILSEQ)) {
0524 net->stats.rx_errors++;
0525 dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
0526 return;
0527 }
0528 if (unlikely(status == -EOVERFLOW)) {
0529 net->stats.rx_errors++;
0530 dev_dbg(dev, "Status was -EOVERFLOW.\n");
0531 }
0532 spin_lock_irqsave(&kaweth->device_lock, flags);
0533 if (IS_BLOCKED(kaweth->status)) {
0534 spin_unlock_irqrestore(&kaweth->device_lock, flags);
0535 return;
0536 }
0537 spin_unlock_irqrestore(&kaweth->device_lock, flags);
0538
0539 if(status && status != -EREMOTEIO && count != 1) {
0540 dev_err(&kaweth->intf->dev,
0541 "%s RX status: %d count: %d packet_len: %d\n",
0542 net->name, status, count, (int)pkt_len);
0543 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
0544 return;
0545 }
0546
0547 if(kaweth->net && (count > 2)) {
0548 if(pkt_len > (count - 2)) {
0549 dev_err(&kaweth->intf->dev,
0550 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
0551 pkt_len, count);
0552 dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
0553 pkt_len & 2047);
0554 dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
0555 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
0556 return;
0557 }
0558
0559 if(!(skb = dev_alloc_skb(pkt_len+2))) {
0560 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
0561 return;
0562 }
0563
0564 skb_reserve(skb, 2);
0565
0566 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
0567
0568 skb_put(skb, pkt_len);
0569
0570 skb->protocol = eth_type_trans(skb, net);
0571
0572 netif_rx(skb);
0573
0574 net->stats.rx_packets++;
0575 net->stats.rx_bytes += pkt_len;
0576 }
0577
0578 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
0579 }
0580
0581
0582
0583
0584 static int kaweth_open(struct net_device *net)
0585 {
0586 struct kaweth_device *kaweth = netdev_priv(net);
0587 int res;
0588
0589 res = usb_autopm_get_interface(kaweth->intf);
0590 if (res) {
0591 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
0592 return -EIO;
0593 }
0594 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
0595 if (res)
0596 goto err_out;
0597
0598 usb_fill_int_urb(
0599 kaweth->irq_urb,
0600 kaweth->dev,
0601 usb_rcvintpipe(kaweth->dev, 3),
0602 kaweth->intbuffer,
0603 INTBUFFERSIZE,
0604 int_callback,
0605 kaweth,
0606 250);
0607 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
0608 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0609
0610 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
0611 if (res) {
0612 usb_kill_urb(kaweth->rx_urb);
0613 goto err_out;
0614 }
0615 kaweth->opened = 1;
0616
0617 netif_start_queue(net);
0618
0619 kaweth_async_set_rx_mode(kaweth, true);
0620 return 0;
0621
0622 err_out:
0623 usb_autopm_put_interface(kaweth->intf);
0624 return -EIO;
0625 }
0626
0627
0628
0629
0630 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
0631 {
0632 usb_kill_urb(kaweth->irq_urb);
0633 usb_kill_urb(kaweth->rx_urb);
0634 usb_kill_urb(kaweth->tx_urb);
0635
0636 cancel_delayed_work_sync(&kaweth->lowmem_work);
0637
0638
0639
0640 usb_kill_urb(kaweth->irq_urb);
0641 usb_kill_urb(kaweth->rx_urb);
0642 }
0643
0644
0645
0646
0647 static int kaweth_close(struct net_device *net)
0648 {
0649 struct kaweth_device *kaweth = netdev_priv(net);
0650
0651 netif_stop_queue(net);
0652 kaweth->opened = 0;
0653
0654 kaweth->status |= KAWETH_STATUS_CLOSING;
0655
0656 kaweth_kill_urbs(kaweth);
0657
0658 kaweth->status &= ~KAWETH_STATUS_CLOSING;
0659
0660 usb_autopm_put_interface(kaweth->intf);
0661
0662 return 0;
0663 }
0664
0665 static u32 kaweth_get_link(struct net_device *dev)
0666 {
0667 struct kaweth_device *kaweth = netdev_priv(dev);
0668
0669 return kaweth->linkstate;
0670 }
0671
0672 static const struct ethtool_ops ops = {
0673 .get_link = kaweth_get_link
0674 };
0675
0676
0677
0678
0679 static void kaweth_usb_transmit_complete(struct urb *urb)
0680 {
0681 struct kaweth_device *kaweth = urb->context;
0682 struct sk_buff *skb = kaweth->tx_skb;
0683 int status = urb->status;
0684
0685 if (unlikely(status != 0))
0686 if (status != -ENOENT)
0687 dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
0688 kaweth->net->name, status);
0689
0690 netif_wake_queue(kaweth->net);
0691 dev_kfree_skb_irq(skb);
0692 }
0693
0694
0695
0696
0697 static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
0698 struct net_device *net)
0699 {
0700 struct kaweth_device *kaweth = netdev_priv(net);
0701 __le16 *private_header;
0702
0703 int res;
0704
0705 spin_lock_irq(&kaweth->device_lock);
0706
0707 kaweth_async_set_rx_mode(kaweth, false);
0708 netif_stop_queue(net);
0709 if (IS_BLOCKED(kaweth->status)) {
0710 goto skip;
0711 }
0712
0713
0714 if (skb_cow_head(skb, 2)) {
0715 net->stats.tx_errors++;
0716 netif_start_queue(net);
0717 spin_unlock_irq(&kaweth->device_lock);
0718 dev_kfree_skb_any(skb);
0719 return NETDEV_TX_OK;
0720 }
0721
0722 private_header = __skb_push(skb, 2);
0723 *private_header = cpu_to_le16(skb->len-2);
0724 kaweth->tx_skb = skb;
0725
0726 usb_fill_bulk_urb(kaweth->tx_urb,
0727 kaweth->dev,
0728 usb_sndbulkpipe(kaweth->dev, 2),
0729 private_header,
0730 skb->len,
0731 kaweth_usb_transmit_complete,
0732 kaweth);
0733 kaweth->end = 0;
0734
0735 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
0736 {
0737 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
0738 skip:
0739 net->stats.tx_errors++;
0740
0741 netif_start_queue(net);
0742 dev_kfree_skb_irq(skb);
0743 }
0744 else
0745 {
0746 net->stats.tx_packets++;
0747 net->stats.tx_bytes += skb->len;
0748 }
0749
0750 spin_unlock_irq(&kaweth->device_lock);
0751
0752 return NETDEV_TX_OK;
0753 }
0754
0755
0756
0757
0758 static void kaweth_set_rx_mode(struct net_device *net)
0759 {
0760 struct kaweth_device *kaweth = netdev_priv(net);
0761
0762 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
0763 KAWETH_PACKET_FILTER_BROADCAST |
0764 KAWETH_PACKET_FILTER_MULTICAST;
0765
0766 netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
0767
0768 netif_stop_queue(net);
0769
0770 if (net->flags & IFF_PROMISC) {
0771 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
0772 }
0773 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
0774 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
0775 }
0776
0777 kaweth->packet_filter_bitmap = packet_filter_bitmap;
0778 netif_wake_queue(net);
0779 }
0780
0781
0782
0783
0784 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
0785 bool may_sleep)
0786 {
0787 int ret;
0788 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
0789
0790 kaweth->packet_filter_bitmap = 0;
0791 if (packet_filter_bitmap == 0)
0792 return;
0793
0794 if (!may_sleep)
0795 return;
0796
0797 ret = usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
0798 KAWETH_COMMAND_SET_PACKET_FILTER,
0799 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0800 packet_filter_bitmap, 0,
0801 &kaweth->scratch, 0,
0802 KAWETH_CONTROL_TIMEOUT);
0803 if (ret < 0)
0804 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
0805 ret);
0806 else
0807 netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
0808 packet_filter_bitmap);
0809 }
0810
0811
0812
0813
0814 static void kaweth_tx_timeout(struct net_device *net, unsigned int txqueue)
0815 {
0816 struct kaweth_device *kaweth = netdev_priv(net);
0817
0818 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
0819 net->stats.tx_errors++;
0820 netif_trans_update(net);
0821
0822 usb_unlink_urb(kaweth->tx_urb);
0823 }
0824
0825
0826
0827
0828 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
0829 {
0830 struct kaweth_device *kaweth = usb_get_intfdata(intf);
0831 unsigned long flags;
0832
0833 spin_lock_irqsave(&kaweth->device_lock, flags);
0834 kaweth->status |= KAWETH_STATUS_SUSPENDING;
0835 spin_unlock_irqrestore(&kaweth->device_lock, flags);
0836
0837 kaweth_kill_urbs(kaweth);
0838 return 0;
0839 }
0840
0841
0842
0843
0844 static int kaweth_resume(struct usb_interface *intf)
0845 {
0846 struct kaweth_device *kaweth = usb_get_intfdata(intf);
0847 unsigned long flags;
0848
0849 spin_lock_irqsave(&kaweth->device_lock, flags);
0850 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
0851 spin_unlock_irqrestore(&kaweth->device_lock, flags);
0852
0853 if (!kaweth->opened)
0854 return 0;
0855 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
0856 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
0857
0858 return 0;
0859 }
0860
0861
0862
0863
0864
0865
0866 static const struct net_device_ops kaweth_netdev_ops = {
0867 .ndo_open = kaweth_open,
0868 .ndo_stop = kaweth_close,
0869 .ndo_start_xmit = kaweth_start_xmit,
0870 .ndo_tx_timeout = kaweth_tx_timeout,
0871 .ndo_set_rx_mode = kaweth_set_rx_mode,
0872 .ndo_set_mac_address = eth_mac_addr,
0873 .ndo_validate_addr = eth_validate_addr,
0874 };
0875
0876 static int kaweth_probe(
0877 struct usb_interface *intf,
0878 const struct usb_device_id *id
0879 )
0880 {
0881 struct device *dev = &intf->dev;
0882 struct usb_device *udev = interface_to_usbdev(intf);
0883 struct kaweth_device *kaweth;
0884 struct net_device *netdev;
0885 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
0886 int result = 0;
0887 int rv = -EIO;
0888
0889 dev_dbg(dev,
0890 "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
0891 udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
0892 le16_to_cpu(udev->descriptor.idProduct),
0893 le16_to_cpu(udev->descriptor.bcdDevice));
0894
0895 dev_dbg(dev, "Device at %p\n", udev);
0896
0897 dev_dbg(dev, "Descriptor length: %x type: %x\n",
0898 (int)udev->descriptor.bLength,
0899 (int)udev->descriptor.bDescriptorType);
0900
0901 netdev = alloc_etherdev(sizeof(*kaweth));
0902 if (!netdev)
0903 return -ENOMEM;
0904
0905 kaweth = netdev_priv(netdev);
0906 kaweth->dev = udev;
0907 kaweth->net = netdev;
0908 kaweth->intf = intf;
0909
0910 spin_lock_init(&kaweth->device_lock);
0911 init_waitqueue_head(&kaweth->term_wait);
0912
0913 dev_dbg(dev, "Resetting.\n");
0914
0915 kaweth_reset(kaweth);
0916
0917
0918
0919
0920
0921
0922 if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
0923 dev_info(dev, "Firmware present in device.\n");
0924 } else {
0925
0926 dev_info(dev, "Downloading firmware...\n");
0927 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
0928 if (!kaweth->firmware_buf) {
0929 rv = -ENOMEM;
0930 goto err_free_netdev;
0931 }
0932 if ((result = kaweth_download_firmware(kaweth,
0933 "kaweth/new_code.bin",
0934 100,
0935 2)) < 0) {
0936 dev_err(dev, "Error downloading firmware (%d)\n",
0937 result);
0938 goto err_fw;
0939 }
0940
0941 if ((result = kaweth_download_firmware(kaweth,
0942 "kaweth/new_code_fix.bin",
0943 100,
0944 3)) < 0) {
0945 dev_err(dev, "Error downloading firmware fix (%d)\n",
0946 result);
0947 goto err_fw;
0948 }
0949
0950 if ((result = kaweth_download_firmware(kaweth,
0951 "kaweth/trigger_code.bin",
0952 126,
0953 2)) < 0) {
0954 dev_err(dev, "Error downloading trigger code (%d)\n",
0955 result);
0956 goto err_fw;
0957
0958 }
0959
0960 if ((result = kaweth_download_firmware(kaweth,
0961 "kaweth/trigger_code_fix.bin",
0962 126,
0963 3)) < 0) {
0964 dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
0965 goto err_fw;
0966 }
0967
0968
0969 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
0970 dev_err(dev, "Error triggering firmware (%d)\n", result);
0971 goto err_fw;
0972 }
0973
0974
0975 dev_info(dev, "Firmware loaded. I'll be back...\n");
0976 err_fw:
0977 free_page((unsigned long)kaweth->firmware_buf);
0978 free_netdev(netdev);
0979 return -EIO;
0980 }
0981
0982 result = kaweth_read_configuration(kaweth);
0983
0984 if(result < 0) {
0985 dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
0986 goto err_free_netdev;
0987 }
0988
0989 dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
0990 dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
0991 dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
0992 dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
0993
0994 if(!memcmp(&kaweth->configuration.hw_addr,
0995 &bcast_addr,
0996 sizeof(bcast_addr))) {
0997 dev_err(dev, "Firmware not functioning properly, no net device created\n");
0998 goto err_free_netdev;
0999 }
1000
1001 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1002 dev_dbg(dev, "Error setting URB size\n");
1003 goto err_free_netdev;
1004 }
1005
1006 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1007 dev_err(dev, "Error setting SOFS wait\n");
1008 goto err_free_netdev;
1009 }
1010
1011 result = kaweth_set_receive_filter(kaweth,
1012 KAWETH_PACKET_FILTER_DIRECTED |
1013 KAWETH_PACKET_FILTER_BROADCAST |
1014 KAWETH_PACKET_FILTER_MULTICAST);
1015
1016 if(result < 0) {
1017 dev_err(dev, "Error setting receive filter\n");
1018 goto err_free_netdev;
1019 }
1020
1021 dev_dbg(dev, "Initializing net device.\n");
1022
1023 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1024 if (!kaweth->tx_urb)
1025 goto err_free_netdev;
1026 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1027 if (!kaweth->rx_urb)
1028 goto err_only_tx;
1029 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1030 if (!kaweth->irq_urb)
1031 goto err_tx_and_rx;
1032
1033 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
1034 INTBUFFERSIZE,
1035 GFP_KERNEL,
1036 &kaweth->intbufferhandle);
1037 if (!kaweth->intbuffer)
1038 goto err_tx_and_rx_and_irq;
1039 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
1040 KAWETH_BUF_SIZE,
1041 GFP_KERNEL,
1042 &kaweth->rxbufferhandle);
1043 if (!kaweth->rx_buf)
1044 goto err_all_but_rxbuf;
1045
1046 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1047 eth_hw_addr_set(netdev, (u8 *)&kaweth->configuration.hw_addr);
1048
1049 netdev->netdev_ops = &kaweth_netdev_ops;
1050 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1051 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1052 netdev->ethtool_ops = &ops;
1053
1054
1055 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1056 usb_set_intfdata(intf, kaweth);
1057
1058 SET_NETDEV_DEV(netdev, dev);
1059 if (register_netdev(netdev) != 0) {
1060 dev_err(dev, "Error registering netdev.\n");
1061 goto err_intfdata;
1062 }
1063
1064 dev_info(dev, "kaweth interface created at %s\n",
1065 kaweth->net->name);
1066
1067 return 0;
1068
1069 err_intfdata:
1070 usb_set_intfdata(intf, NULL);
1071 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1072 err_all_but_rxbuf:
1073 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1074 err_tx_and_rx_and_irq:
1075 usb_free_urb(kaweth->irq_urb);
1076 err_tx_and_rx:
1077 usb_free_urb(kaweth->rx_urb);
1078 err_only_tx:
1079 usb_free_urb(kaweth->tx_urb);
1080 err_free_netdev:
1081 free_netdev(netdev);
1082
1083 return rv;
1084 }
1085
1086
1087
1088
1089 static void kaweth_disconnect(struct usb_interface *intf)
1090 {
1091 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1092 struct net_device *netdev;
1093
1094 usb_set_intfdata(intf, NULL);
1095 if (!kaweth) {
1096 dev_warn(&intf->dev, "unregistering non-existent device\n");
1097 return;
1098 }
1099 netdev = kaweth->net;
1100
1101 netdev_dbg(kaweth->net, "Unregistering net device\n");
1102 unregister_netdev(netdev);
1103
1104 usb_free_urb(kaweth->rx_urb);
1105 usb_free_urb(kaweth->tx_urb);
1106 usb_free_urb(kaweth->irq_urb);
1107
1108 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1109 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1110
1111 free_netdev(netdev);
1112 }
1113
1114
1115 module_usb_driver(kaweth_driver);