MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / drivers / net / usb / kaweth.c
blob7cb10a0a53164dc45650a7225ae95c38e4517557
1 /****************************************************************
3 * kaweth.c - driver for KL5KUSB101 based USB->Ethernet
5 * (c) 2000 Interlan Communications
6 * (c) 2000 Stephane Alnet
7 * (C) 2001 Brad Hards
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 ****************************************************************/
33 /* TODO:
34 * Fix in_interrupt() problem
35 * Develop test procedures for USB net interfaces
36 * Run test procedures
37 * Fix bugs from previous two steps
38 * Snoop other OSs for any tricks we're not doing
39 * SMP locking
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>
64 #undef DEBUG
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
85 /* Table 7 */
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,
123 unsigned int pipe,
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 /****************************************************************
130 * usb_device_id
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 /****************************************************************
173 * kaweth_driver
174 ****************************************************************/
175 static struct usb_driver kaweth_driver = {
176 .name = driver_name,
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 /****************************************************************
188 * usb_eth_dev
189 ****************************************************************/
190 struct usb_eth_dev {
191 char *name;
192 __u16 vendor;
193 __u16 device;
194 void *pdata;
197 /****************************************************************
198 * kaweth_ethernet_configuration
199 * Refer Table 8
200 ****************************************************************/
201 struct kaweth_ethernet_configuration
203 __u8 size;
204 __u8 reserved1;
205 __u8 reserved2;
206 eth_addr_t hw_addr;
207 __u32 statistics_mask;
208 __le16 segment_size;
209 __u16 max_multicast_filters;
210 __u8 reserved3;
211 } __attribute__ ((packed));
213 /****************************************************************
214 * kaweth_device
215 ****************************************************************/
216 struct kaweth_device
218 spinlock_t device_lock;
220 __u32 status;
221 int end;
222 int suspend_lowmem_rx;
223 int suspend_lowmem_ctrl;
224 int linkstate;
225 int opened;
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;
233 struct urb *rx_urb;
234 struct urb *tx_urb;
235 struct urb *irq_urb;
237 dma_addr_t intbufferhandle;
238 __u8 *intbuffer;
239 dma_addr_t rxbufferhandle;
240 __u8 *rx_buf;
243 struct sk_buff *tx_skb;
245 __u8 *firmware_buf;
246 __u8 scratch[KAWETH_SCRATCH_SIZE];
247 __u16 packet_filter_bitmap;
249 struct kaweth_ethernet_configuration configuration;
251 struct net_device_stats stats;
254 /****************************************************************
255 * kaweth_control
256 ****************************************************************/
257 static int kaweth_control(struct kaweth_device *kaweth,
258 unsigned int pipe,
259 __u8 request,
260 __u8 requesttype,
261 __u16 value,
262 __u16 index,
263 void *data,
264 __u16 size,
265 int timeout)
267 struct usb_ctrlrequest *dr;
269 dbg("kaweth_control()");
271 if(in_interrupt()) {
272 dbg("in_interrupt()");
273 return -EBUSY;
276 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
278 if (!dr) {
279 dbg("kmalloc() failed");
280 return -ENOMEM;
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,
290 pipe,
292 data,
293 size,
294 timeout);
297 /****************************************************************
298 * kaweth_read_configuration
299 ****************************************************************/
300 static int kaweth_read_configuration(struct kaweth_device *kaweth)
302 int retval;
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);
316 return retval;
319 /****************************************************************
320 * kaweth_set_urb_size
321 ****************************************************************/
322 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
324 int retval;
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,
332 urb_size,
334 (void *)&kaweth->scratch,
336 KAWETH_CONTROL_TIMEOUT);
338 return retval;
341 /****************************************************************
342 * kaweth_set_sofs_wait
343 ****************************************************************/
344 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
346 int retval;
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,
354 sofs_wait,
356 (void *)&kaweth->scratch,
358 KAWETH_CONTROL_TIMEOUT);
360 return retval;
363 /****************************************************************
364 * kaweth_set_receive_filter
365 ****************************************************************/
366 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
367 __u16 receive_filter)
369 int retval;
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,
377 receive_filter,
379 (void *)&kaweth->scratch,
381 KAWETH_CONTROL_TIMEOUT);
383 return retval;
386 /****************************************************************
387 * kaweth_download_firmware
388 ****************************************************************/
389 static int kaweth_download_firmware(struct kaweth_device *kaweth,
390 const char *fwname,
391 __u8 interrupt,
392 __u8 type)
394 const struct firmware *fw;
395 int data_len;
396 int ret;
398 ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
399 if (ret) {
400 err("Firmware request failed\n");
401 return ret;
404 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
405 err("Firmware too big: %zu", fw->size);
406 return -ENOSPC;
408 data_len = 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",
422 fw->data, kaweth);
423 dbg("Firmware length: %d", data_len);
425 return kaweth_control(kaweth,
426 usb_sndctrlpipe(kaweth->dev, 0),
427 KAWETH_COMMAND_SCAN,
428 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
431 (void *)kaweth->firmware_buf,
432 data_len,
433 KAWETH_CONTROL_TIMEOUT);
436 /****************************************************************
437 * kaweth_trigger_firmware
438 ****************************************************************/
439 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
440 __u8 interrupt)
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),
455 KAWETH_COMMAND_SCAN,
456 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
459 (void *)kaweth->firmware_buf,
461 KAWETH_CONTROL_TIMEOUT);
464 /****************************************************************
465 * kaweth_reset
466 ****************************************************************/
467 static int kaweth_reset(struct kaweth_device *kaweth)
469 int result;
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,
478 NULL,
480 KAWETH_CONTROL_TIMEOUT);
482 mdelay(10);
484 dbg("kaweth_reset() returns %d.",result);
486 return result;
489 static void kaweth_usb_receive(struct urb *);
490 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
492 /****************************************************************
493 int_callback
494 *****************************************************************/
496 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
498 int status;
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);
504 } else {
505 kaweth->suspend_lowmem_ctrl = 0;
508 if (status)
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;
517 int act_state;
518 int status = u->status;
520 switch (status) {
521 case 0: /* success */
522 break;
523 case -ECONNRESET: /* unlink */
524 case -ENOENT:
525 case -ESHUTDOWN:
526 return;
527 /* -EPIPE: should clear the halt */
528 default: /* error */
529 goto resubmit;
532 /* we check the link state to report changes */
533 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
534 if (act_state)
535 netif_carrier_on(kaweth->net);
536 else
537 netif_carrier_off(kaweth->net);
539 kaweth->linkstate = act_state;
541 resubmit:
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))
551 return;
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,
565 gfp_t mem_flags)
567 int result;
569 usb_fill_bulk_urb(kaweth->rx_urb,
570 kaweth->dev,
571 usb_rcvbulkpipe(kaweth->dev, 1),
572 kaweth->rx_buf,
573 KAWETH_BUF_SIZE,
574 kaweth_usb_receive,
575 kaweth);
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);
585 } else {
586 kaweth->suspend_lowmem_rx = 0;
589 return result;
592 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
594 /****************************************************************
595 * kaweth_usb_receive
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);
608 struct sk_buff *skb;
610 if(unlikely(status == -ECONNRESET || status == -ESHUTDOWN))
611 /* we are killed - set a flag and wake the disconnect handler */
613 kaweth->end = 1;
614 wake_up(&kaweth->term_wait);
615 return;
618 spin_lock(&kaweth->device_lock);
619 if (IS_BLOCKED(kaweth->status)) {
620 spin_unlock(&kaweth->device_lock);
621 return;
623 spin_unlock(&kaweth->device_lock);
625 if(status && status != -EREMOTEIO && count != 1) {
626 err("%s RX status: %d count: %d packet_len: %d",
627 net->name,
628 status,
629 count,
630 (int)pkt_len);
631 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
632 return;
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);
641 return;
644 if(!(skb = dev_alloc_skb(pkt_len+2))) {
645 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
646 return;
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);
657 netif_rx(skb);
659 kaweth->stats.rx_packets++;
660 kaweth->stats.rx_bytes += pkt_len;
663 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
666 /****************************************************************
667 * kaweth_open
668 ****************************************************************/
669 static int kaweth_open(struct net_device *net)
671 struct kaweth_device *kaweth = netdev_priv(net);
672 int res;
674 dbg("Opening network device.");
676 res = usb_autopm_get_interface(kaweth->intf);
677 if (res) {
678 err("Interface cannot be resumed.");
679 return -EIO;
681 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
682 if (res)
683 goto err_out;
685 usb_fill_int_urb(
686 kaweth->irq_urb,
687 kaweth->dev,
688 usb_rcvintpipe(kaweth->dev, 3),
689 kaweth->intbuffer,
690 INTBUFFERSIZE,
691 int_callback,
692 kaweth,
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);
698 if (res) {
699 usb_kill_urb(kaweth->rx_urb);
700 goto err_out;
702 kaweth->opened = 1;
704 netif_start_queue(net);
706 kaweth_async_set_rx_mode(kaweth);
707 return 0;
709 err_out:
710 usb_autopm_enable(kaweth->intf);
711 return -EIO;
714 /****************************************************************
715 * kaweth_kill_urbs
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,
726 we hit them again */
727 usb_kill_urb(kaweth->irq_urb);
728 usb_kill_urb(kaweth->rx_urb);
731 /****************************************************************
732 * kaweth_close
733 ****************************************************************/
734 static int kaweth_close(struct net_device *net)
736 struct kaweth_device *kaweth = netdev_priv(net);
738 netif_stop_queue(net);
739 kaweth->opened = 0;
741 kaweth->status |= KAWETH_STATUS_CLOSING;
743 kaweth_kill_urbs(kaweth);
745 kaweth->status &= ~KAWETH_STATUS_CLOSING;
747 usb_autopm_enable(kaweth->intf);
749 return 0;
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 /****************************************************************
790 * kaweth_start_xmit
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;
797 int res;
799 spin_lock(&kaweth->device_lock);
801 kaweth_async_set_rx_mode(kaweth);
802 netif_stop_queue(net);
803 if (IS_BLOCKED(kaweth->status)) {
804 goto skip;
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);
813 skb = copied_skb;
814 if (!copied_skb) {
815 kaweth->stats.tx_errors++;
816 netif_start_queue(net);
817 spin_unlock(&kaweth->device_lock);
818 return 0;
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,
827 kaweth->dev,
828 usb_sndbulkpipe(kaweth->dev, 2),
829 private_header,
830 skb->len,
831 kaweth_usb_transmit_complete,
832 kaweth);
833 kaweth->end = 0;
835 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
837 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
838 skip:
839 kaweth->stats.tx_errors++;
841 netif_start_queue(net);
842 dev_kfree_skb_irq(skb);
844 else
846 kaweth->stats.tx_packets++;
847 kaweth->stats.tx_bytes += skb->len;
848 net->trans_start = jiffies;
851 spin_unlock(&kaweth->device_lock);
853 return 0;
856 /****************************************************************
857 * kaweth_set_rx_mode
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)
890 return;
893 int result;
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);
904 if(result < 0) {
905 err("Failed to set Rx mode: %d", result);
907 else {
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 /****************************************************************
923 * kaweth_tx_timeout
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 /****************************************************************
937 * kaweth_suspend
938 ****************************************************************/
939 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
941 struct kaweth_device *kaweth = usb_get_intfdata(intf);
942 unsigned long flags;
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);
950 return 0;
953 /****************************************************************
954 * kaweth_resume
955 ****************************************************************/
956 static int kaweth_resume(struct usb_interface *intf)
958 struct kaweth_device *kaweth = usb_get_intfdata(intf);
959 unsigned long flags;
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);
966 if (!kaweth->opened)
967 return 0;
968 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
969 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
971 return 0;
974 /****************************************************************
975 * kaweth_probe
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 };
997 int result = 0;
999 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
1000 dev->devnum,
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));
1012 if (!netdev)
1013 return -ENOMEM;
1015 kaweth = netdev_priv(netdev);
1016 kaweth->dev = dev;
1017 kaweth->net = netdev;
1019 spin_lock_init(&kaweth->device_lock);
1020 init_waitqueue_head(&kaweth->term_wait);
1022 dbg("Resetting.");
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");
1033 } else {
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",
1039 100,
1040 2)) < 0) {
1041 err("Error downloading firmware (%d)", result);
1042 goto err_fw;
1045 if ((result = kaweth_download_firmware(kaweth,
1046 "kaweth/new_code_fix.bin",
1047 100,
1048 3)) < 0) {
1049 err("Error downloading firmware fix (%d)", result);
1050 goto err_fw;
1053 if ((result = kaweth_download_firmware(kaweth,
1054 "kaweth/trigger_code.bin",
1055 126,
1056 2)) < 0) {
1057 err("Error downloading trigger code (%d)", result);
1058 goto err_fw;
1062 if ((result = kaweth_download_firmware(kaweth,
1063 "kaweth/trigger_code_fix.bin",
1064 126,
1065 3)) < 0) {
1066 err("Error downloading trigger code fix (%d)", result);
1067 goto err_fw;
1071 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1072 err("Error triggering firmware (%d)", result);
1073 goto err_fw;
1076 /* Device will now disappear for a moment... */
1077 dev_info(&intf->dev, "Firmware loaded. I'll be back...\n");
1078 err_fw:
1079 free_page((unsigned long)kaweth->firmware_buf);
1080 free_netdev(netdev);
1081 return -EIO;
1084 result = kaweth_read_configuration(kaweth);
1086 if(result < 0) {
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,
1103 &bcast_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);
1124 if(result < 0) {
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)
1138 goto err_only_tx;
1139 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1140 if (!kaweth->irq_urb)
1141 goto err_tx_and_rx;
1143 kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
1144 INTBUFFERSIZE,
1145 GFP_KERNEL,
1146 &kaweth->intbufferhandle);
1147 if (!kaweth->intbuffer)
1148 goto err_tx_and_rx_and_irq;
1149 kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
1150 KAWETH_BUF_SIZE,
1151 GFP_KERNEL,
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);
1169 #if 0
1170 // dma_supported() is deeply broken on almost all architectures
1171 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1172 kaweth->net->features |= NETIF_F_HIGHDMA;
1173 #endif
1175 SET_NETDEV_DEV(netdev, &intf->dev);
1176 if (register_netdev(netdev) != 0) {
1177 err("Error registering netdev.");
1178 goto err_intfdata;
1181 dev_info(&intf->dev, "kaweth interface created at %s\n",
1182 kaweth->net->name);
1184 dbg("Kaweth probe returning.");
1186 return 0;
1188 err_intfdata:
1189 usb_set_intfdata(intf, NULL);
1190 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1191 err_all_but_rxbuf:
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);
1195 err_tx_and_rx:
1196 usb_free_urb(kaweth->rx_urb);
1197 err_only_tx:
1198 usb_free_urb(kaweth->tx_urb);
1199 err_free_netdev:
1200 free_netdev(netdev);
1202 return -EIO;
1205 /****************************************************************
1206 * kaweth_disconnect
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);
1216 if (!kaweth) {
1217 dev_warn(&intf->dev, "unregistering non-existant device\n");
1218 return;
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;
1240 int done;
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;
1250 awd->done=1;
1251 wake_up(&awd->wqh);
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;
1262 int status;
1264 init_waitqueue_head(&awd.wqh);
1265 awd.done = 0;
1267 urb->context = &awd;
1268 status = usb_submit_urb(urb, GFP_NOIO);
1269 if (status) {
1270 // something went wrong
1271 usb_free_urb(urb);
1272 return status;
1275 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1276 // timeout
1277 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
1278 usb_kill_urb(urb); // remove urb safely
1279 status = -ETIMEDOUT;
1281 else {
1282 status = urb->status;
1285 if (actual_length) {
1286 *actual_length = urb->actual_length;
1289 usb_free_urb(urb);
1290 return status;
1293 /*-------------------------------------------------------------------*/
1294 // returns status (negative) or length (positive)
1295 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1296 unsigned int pipe,
1297 struct usb_ctrlrequest *cmd, void *data,
1298 int len, int timeout)
1300 struct urb *urb;
1301 int retv;
1302 int length = 0; /* shut up GCC */
1304 urb = usb_alloc_urb(0, GFP_NOIO);
1305 if (!urb)
1306 return -ENOMEM;
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);
1312 if (retv < 0) {
1313 return retv;
1315 else {
1316 return length;
1321 /****************************************************************
1322 * kaweth_init
1323 ****************************************************************/
1324 static int __init kaweth_init(void)
1326 dbg("Driver loading");
1327 return usb_register(&kaweth_driver);
1330 /****************************************************************
1331 * kaweth_exit
1332 ****************************************************************/
1333 static void __exit kaweth_exit(void)
1335 usb_deregister(&kaweth_driver);
1338 module_init(kaweth_init);
1339 module_exit(kaweth_exit);