Input: inport - use correct config option for ATIXL
[firewire-audio.git] / drivers / usb / net / kaweth.c
blob36a989160a684ccba108d06a706008d5500a8941
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/sched.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/init.h>
53 #include <linux/delay.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/usb.h>
57 #include <linux/types.h>
58 #include <linux/ethtool.h>
59 #include <linux/pci.h>
60 #include <linux/dma-mapping.h>
61 #include <linux/wait.h>
62 #include <asm/uaccess.h>
63 #include <asm/semaphore.h>
64 #include <asm/byteorder.h>
66 #undef DEBUG
68 #include "kawethfw.h"
70 #define KAWETH_MTU 1514
71 #define KAWETH_BUF_SIZE 1664
72 #define KAWETH_TX_TIMEOUT (5 * HZ)
73 #define KAWETH_SCRATCH_SIZE 32
74 #define KAWETH_FIRMWARE_BUF_SIZE 4096
75 #define KAWETH_CONTROL_TIMEOUT (30 * HZ)
77 #define KAWETH_STATUS_BROKEN 0x0000001
78 #define KAWETH_STATUS_CLOSING 0x0000002
79 #define KAWETH_STATUS_SUSPENDING 0x0000004
81 #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
83 #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
84 #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
85 #define KAWETH_PACKET_FILTER_DIRECTED 0x04
86 #define KAWETH_PACKET_FILTER_BROADCAST 0x08
87 #define KAWETH_PACKET_FILTER_MULTICAST 0x10
89 /* Table 7 */
90 #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
91 #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
92 #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
93 #define KAWETH_COMMAND_STATISTICS 0x03
94 #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
95 #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
96 #define KAWETH_COMMAND_SET_URB_SIZE 0x08
97 #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
98 #define KAWETH_COMMAND_SCAN 0xFF
100 #define KAWETH_SOFS_TO_WAIT 0x05
102 #define INTBUFFERSIZE 4
104 #define STATE_OFFSET 0
105 #define STATE_MASK 0x40
106 #define STATE_SHIFT 5
108 #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
111 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>");
112 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
113 MODULE_LICENSE("GPL");
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;
255 /****************************************************************
256 * kaweth_control
257 ****************************************************************/
258 static int kaweth_control(struct kaweth_device *kaweth,
259 unsigned int pipe,
260 __u8 request,
261 __u8 requesttype,
262 __u16 value,
263 __u16 index,
264 void *data,
265 __u16 size,
266 int timeout)
268 struct usb_ctrlrequest *dr;
270 dbg("kaweth_control()");
272 if(in_interrupt()) {
273 dbg("in_interrupt()");
274 return -EBUSY;
277 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
279 if (!dr) {
280 dbg("kmalloc() failed");
281 return -ENOMEM;
284 dr->bRequestType= requesttype;
285 dr->bRequest = request;
286 dr->wValue = cpu_to_le16p(&value);
287 dr->wIndex = cpu_to_le16p(&index);
288 dr->wLength = cpu_to_le16p(&size);
290 return kaweth_internal_control_msg(kaweth->dev,
291 pipe,
293 data,
294 size,
295 timeout);
298 /****************************************************************
299 * kaweth_read_configuration
300 ****************************************************************/
301 static int kaweth_read_configuration(struct kaweth_device *kaweth)
303 int retval;
305 dbg("Reading kaweth configuration");
307 retval = kaweth_control(kaweth,
308 usb_rcvctrlpipe(kaweth->dev, 0),
309 KAWETH_COMMAND_GET_ETHERNET_DESC,
310 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
313 (void *)&kaweth->configuration,
314 sizeof(kaweth->configuration),
315 KAWETH_CONTROL_TIMEOUT);
317 return retval;
320 /****************************************************************
321 * kaweth_set_urb_size
322 ****************************************************************/
323 static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
325 int retval;
327 dbg("Setting URB size to %d", (unsigned)urb_size);
329 retval = kaweth_control(kaweth,
330 usb_sndctrlpipe(kaweth->dev, 0),
331 KAWETH_COMMAND_SET_URB_SIZE,
332 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
333 urb_size,
335 (void *)&kaweth->scratch,
337 KAWETH_CONTROL_TIMEOUT);
339 return retval;
342 /****************************************************************
343 * kaweth_set_sofs_wait
344 ****************************************************************/
345 static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
347 int retval;
349 dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
351 retval = kaweth_control(kaweth,
352 usb_sndctrlpipe(kaweth->dev, 0),
353 KAWETH_COMMAND_SET_SOFS_WAIT,
354 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
355 sofs_wait,
357 (void *)&kaweth->scratch,
359 KAWETH_CONTROL_TIMEOUT);
361 return retval;
364 /****************************************************************
365 * kaweth_set_receive_filter
366 ****************************************************************/
367 static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
368 __u16 receive_filter)
370 int retval;
372 dbg("Set receive filter to %d", (unsigned)receive_filter);
374 retval = kaweth_control(kaweth,
375 usb_sndctrlpipe(kaweth->dev, 0),
376 KAWETH_COMMAND_SET_PACKET_FILTER,
377 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
378 receive_filter,
380 (void *)&kaweth->scratch,
382 KAWETH_CONTROL_TIMEOUT);
384 return retval;
387 /****************************************************************
388 * kaweth_download_firmware
389 ****************************************************************/
390 static int kaweth_download_firmware(struct kaweth_device *kaweth,
391 __u8 *data,
392 __u16 data_len,
393 __u8 interrupt,
394 __u8 type)
396 if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
397 err("Firmware too big: %d", data_len);
398 return -ENOSPC;
401 memcpy(kaweth->firmware_buf, data, data_len);
403 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
404 kaweth->firmware_buf[3] = data_len >> 8;
405 kaweth->firmware_buf[4] = type;
406 kaweth->firmware_buf[5] = interrupt;
408 dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
409 kaweth->firmware_buf[2]);
411 dbg("Downloading firmware at %p to kaweth device at %p",
412 data,
413 kaweth);
414 dbg("Firmware length: %d", data_len);
416 return kaweth_control(kaweth,
417 usb_sndctrlpipe(kaweth->dev, 0),
418 KAWETH_COMMAND_SCAN,
419 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
422 (void *)kaweth->firmware_buf,
423 data_len,
424 KAWETH_CONTROL_TIMEOUT);
427 /****************************************************************
428 * kaweth_trigger_firmware
429 ****************************************************************/
430 static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
431 __u8 interrupt)
433 kaweth->firmware_buf[0] = 0xB6;
434 kaweth->firmware_buf[1] = 0xC3;
435 kaweth->firmware_buf[2] = 0x01;
436 kaweth->firmware_buf[3] = 0x00;
437 kaweth->firmware_buf[4] = 0x06;
438 kaweth->firmware_buf[5] = interrupt;
439 kaweth->firmware_buf[6] = 0x00;
440 kaweth->firmware_buf[7] = 0x00;
442 dbg("Triggering firmware");
444 return kaweth_control(kaweth,
445 usb_sndctrlpipe(kaweth->dev, 0),
446 KAWETH_COMMAND_SCAN,
447 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
450 (void *)kaweth->firmware_buf,
452 KAWETH_CONTROL_TIMEOUT);
455 /****************************************************************
456 * kaweth_reset
457 ****************************************************************/
458 static int kaweth_reset(struct kaweth_device *kaweth)
460 int result;
462 dbg("kaweth_reset(%p)", kaweth);
463 result = kaweth_control(kaweth,
464 usb_sndctrlpipe(kaweth->dev, 0),
465 USB_REQ_SET_CONFIGURATION,
467 kaweth->dev->config[0].desc.bConfigurationValue,
469 NULL,
471 KAWETH_CONTROL_TIMEOUT);
473 mdelay(10);
475 dbg("kaweth_reset() returns %d.",result);
477 return result;
480 static void kaweth_usb_receive(struct urb *);
481 static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
483 /****************************************************************
484 int_callback
485 *****************************************************************/
487 static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
489 int status;
491 status = usb_submit_urb (kaweth->irq_urb, mf);
492 if (unlikely(status == -ENOMEM)) {
493 kaweth->suspend_lowmem_ctrl = 1;
494 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
495 } else {
496 kaweth->suspend_lowmem_ctrl = 0;
499 if (status)
500 err ("can't resubmit intr, %s-%s, status %d",
501 kaweth->dev->bus->bus_name,
502 kaweth->dev->devpath, status);
505 static void int_callback(struct urb *u)
507 struct kaweth_device *kaweth = u->context;
508 int act_state;
510 switch (u->status) {
511 case 0: /* success */
512 break;
513 case -ECONNRESET: /* unlink */
514 case -ENOENT:
515 case -ESHUTDOWN:
516 return;
517 /* -EPIPE: should clear the halt */
518 default: /* error */
519 goto resubmit;
522 /* we check the link state to report changes */
523 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
524 if (act_state)
525 netif_carrier_on(kaweth->net);
526 else
527 netif_carrier_off(kaweth->net);
529 kaweth->linkstate = act_state;
531 resubmit:
532 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
535 static void kaweth_resubmit_tl(struct work_struct *work)
537 struct kaweth_device *kaweth =
538 container_of(work, struct kaweth_device, lowmem_work.work);
540 if (IS_BLOCKED(kaweth->status))
541 return;
543 if (kaweth->suspend_lowmem_rx)
544 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
546 if (kaweth->suspend_lowmem_ctrl)
547 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
551 /****************************************************************
552 * kaweth_resubmit_rx_urb
553 ****************************************************************/
554 static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
555 gfp_t mem_flags)
557 int result;
559 usb_fill_bulk_urb(kaweth->rx_urb,
560 kaweth->dev,
561 usb_rcvbulkpipe(kaweth->dev, 1),
562 kaweth->rx_buf,
563 KAWETH_BUF_SIZE,
564 kaweth_usb_receive,
565 kaweth);
566 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
567 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
569 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
570 if (result == -ENOMEM) {
571 kaweth->suspend_lowmem_rx = 1;
572 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
574 err("resubmitting rx_urb %d failed", result);
575 } else {
576 kaweth->suspend_lowmem_rx = 0;
579 return result;
582 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
584 /****************************************************************
585 * kaweth_usb_receive
586 ****************************************************************/
587 static void kaweth_usb_receive(struct urb *urb)
589 struct kaweth_device *kaweth = urb->context;
590 struct net_device *net = kaweth->net;
592 int count = urb->actual_length;
593 int count2 = urb->transfer_buffer_length;
595 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
597 struct sk_buff *skb;
599 if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
600 /* we are killed - set a flag and wake the disconnect handler */
602 kaweth->end = 1;
603 wake_up(&kaweth->term_wait);
604 return;
607 spin_lock(&kaweth->device_lock);
608 if (IS_BLOCKED(kaweth->status)) {
609 spin_unlock(&kaweth->device_lock);
610 return;
612 spin_unlock(&kaweth->device_lock);
614 if(urb->status && urb->status != -EREMOTEIO && count != 1) {
615 err("%s RX status: %d count: %d packet_len: %d",
616 net->name,
617 urb->status,
618 count,
619 (int)pkt_len);
620 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
621 return;
624 if(kaweth->net && (count > 2)) {
625 if(pkt_len > (count - 2)) {
626 err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
627 err("Packet len & 2047: %x", pkt_len & 2047);
628 err("Count 2: %x", count2);
629 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
630 return;
633 if(!(skb = dev_alloc_skb(pkt_len+2))) {
634 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
635 return;
638 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
640 skb->dev = net;
642 eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
644 skb_put(skb, pkt_len);
646 skb->protocol = eth_type_trans(skb, net);
648 netif_rx(skb);
650 kaweth->stats.rx_packets++;
651 kaweth->stats.rx_bytes += pkt_len;
654 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
657 /****************************************************************
658 * kaweth_open
659 ****************************************************************/
660 static int kaweth_open(struct net_device *net)
662 struct kaweth_device *kaweth = netdev_priv(net);
663 int res;
665 dbg("Opening network device.");
667 res = usb_autopm_get_interface(kaweth->intf);
668 if (res) {
669 err("Interface cannot be resumed.");
670 return -EIO;
672 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
673 if (res)
674 goto err_out;
676 usb_fill_int_urb(
677 kaweth->irq_urb,
678 kaweth->dev,
679 usb_rcvintpipe(kaweth->dev, 3),
680 kaweth->intbuffer,
681 INTBUFFERSIZE,
682 int_callback,
683 kaweth,
684 250); /* overriding the descriptor */
685 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
686 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
688 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
689 if (res) {
690 usb_kill_urb(kaweth->rx_urb);
691 goto err_out;
693 kaweth->opened = 1;
695 netif_start_queue(net);
697 kaweth_async_set_rx_mode(kaweth);
698 return 0;
700 err_out:
701 usb_autopm_enable(kaweth->intf);
702 return -EIO;
705 /****************************************************************
706 * kaweth_kill_urbs
707 ****************************************************************/
708 static void kaweth_kill_urbs(struct kaweth_device *kaweth)
710 usb_kill_urb(kaweth->irq_urb);
711 usb_kill_urb(kaweth->rx_urb);
712 usb_kill_urb(kaweth->tx_urb);
714 flush_scheduled_work();
716 /* a scheduled work may have resubmitted,
717 we hit them again */
718 usb_kill_urb(kaweth->irq_urb);
719 usb_kill_urb(kaweth->rx_urb);
722 /****************************************************************
723 * kaweth_close
724 ****************************************************************/
725 static int kaweth_close(struct net_device *net)
727 struct kaweth_device *kaweth = netdev_priv(net);
729 netif_stop_queue(net);
730 kaweth->opened = 0;
732 kaweth->status |= KAWETH_STATUS_CLOSING;
734 kaweth_kill_urbs(kaweth);
736 kaweth->status &= ~KAWETH_STATUS_CLOSING;
738 usb_autopm_enable(kaweth->intf);
740 return 0;
743 static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
745 struct kaweth_device *kaweth = netdev_priv(dev);
747 strlcpy(info->driver, driver_name, sizeof(info->driver));
748 usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
751 static u32 kaweth_get_link(struct net_device *dev)
753 struct kaweth_device *kaweth = netdev_priv(dev);
755 return kaweth->linkstate;
758 static struct ethtool_ops ops = {
759 .get_drvinfo = kaweth_get_drvinfo,
760 .get_link = kaweth_get_link
763 /****************************************************************
764 * kaweth_usb_transmit_complete
765 ****************************************************************/
766 static void kaweth_usb_transmit_complete(struct urb *urb)
768 struct kaweth_device *kaweth = urb->context;
769 struct sk_buff *skb = kaweth->tx_skb;
771 if (unlikely(urb->status != 0))
772 if (urb->status != -ENOENT)
773 dbg("%s: TX status %d.", kaweth->net->name, urb->status);
775 netif_wake_queue(kaweth->net);
776 dev_kfree_skb_irq(skb);
779 /****************************************************************
780 * kaweth_start_xmit
781 ****************************************************************/
782 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
784 struct kaweth_device *kaweth = netdev_priv(net);
785 __le16 *private_header;
787 int res;
789 spin_lock(&kaweth->device_lock);
791 kaweth_async_set_rx_mode(kaweth);
792 netif_stop_queue(net);
793 if (IS_BLOCKED(kaweth->status)) {
794 goto skip;
797 /* We now decide whether we can put our special header into the sk_buff */
798 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
799 /* no such luck - we make our own */
800 struct sk_buff *copied_skb;
801 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
802 dev_kfree_skb_irq(skb);
803 skb = copied_skb;
804 if (!copied_skb) {
805 kaweth->stats.tx_errors++;
806 netif_start_queue(net);
807 spin_unlock(&kaweth->device_lock);
808 return 0;
812 private_header = (__le16 *)__skb_push(skb, 2);
813 *private_header = cpu_to_le16(skb->len-2);
814 kaweth->tx_skb = skb;
816 usb_fill_bulk_urb(kaweth->tx_urb,
817 kaweth->dev,
818 usb_sndbulkpipe(kaweth->dev, 2),
819 private_header,
820 skb->len,
821 kaweth_usb_transmit_complete,
822 kaweth);
823 kaweth->end = 0;
825 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
827 warn("kaweth failed tx_urb %d", res);
828 skip:
829 kaweth->stats.tx_errors++;
831 netif_start_queue(net);
832 dev_kfree_skb_irq(skb);
834 else
836 kaweth->stats.tx_packets++;
837 kaweth->stats.tx_bytes += skb->len;
838 net->trans_start = jiffies;
841 spin_unlock(&kaweth->device_lock);
843 return 0;
846 /****************************************************************
847 * kaweth_set_rx_mode
848 ****************************************************************/
849 static void kaweth_set_rx_mode(struct net_device *net)
851 struct kaweth_device *kaweth = netdev_priv(net);
853 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
854 KAWETH_PACKET_FILTER_BROADCAST |
855 KAWETH_PACKET_FILTER_MULTICAST;
857 dbg("Setting Rx mode to %d", packet_filter_bitmap);
859 netif_stop_queue(net);
861 if (net->flags & IFF_PROMISC) {
862 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
864 else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
865 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
868 kaweth->packet_filter_bitmap = packet_filter_bitmap;
869 netif_wake_queue(net);
872 /****************************************************************
873 * kaweth_async_set_rx_mode
874 ****************************************************************/
875 static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
877 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
878 kaweth->packet_filter_bitmap = 0;
879 if (packet_filter_bitmap == 0)
880 return;
883 int result;
884 result = kaweth_control(kaweth,
885 usb_sndctrlpipe(kaweth->dev, 0),
886 KAWETH_COMMAND_SET_PACKET_FILTER,
887 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
888 packet_filter_bitmap,
890 (void *)&kaweth->scratch,
892 KAWETH_CONTROL_TIMEOUT);
894 if(result < 0) {
895 err("Failed to set Rx mode: %d", result);
897 else {
898 dbg("Set Rx mode to %d", packet_filter_bitmap);
903 /****************************************************************
904 * kaweth_netdev_stats
905 ****************************************************************/
906 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
908 struct kaweth_device *kaweth = netdev_priv(dev);
909 return &kaweth->stats;
912 /****************************************************************
913 * kaweth_tx_timeout
914 ****************************************************************/
915 static void kaweth_tx_timeout(struct net_device *net)
917 struct kaweth_device *kaweth = netdev_priv(net);
919 warn("%s: Tx timed out. Resetting.", net->name);
920 kaweth->stats.tx_errors++;
921 net->trans_start = jiffies;
923 usb_unlink_urb(kaweth->tx_urb);
926 /****************************************************************
927 * kaweth_suspend
928 ****************************************************************/
929 static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
931 struct kaweth_device *kaweth = usb_get_intfdata(intf);
932 unsigned long flags;
934 dbg("Suspending device");
935 spin_lock_irqsave(&kaweth->device_lock, flags);
936 kaweth->status |= KAWETH_STATUS_SUSPENDING;
937 spin_unlock_irqrestore(&kaweth->device_lock, flags);
939 kaweth_kill_urbs(kaweth);
940 return 0;
943 /****************************************************************
944 * kaweth_resume
945 ****************************************************************/
946 static int kaweth_resume(struct usb_interface *intf)
948 struct kaweth_device *kaweth = usb_get_intfdata(intf);
949 unsigned long flags;
951 dbg("Resuming device");
952 spin_lock_irqsave(&kaweth->device_lock, flags);
953 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
954 spin_unlock_irqrestore(&kaweth->device_lock, flags);
956 if (!kaweth->opened)
957 return 0;
958 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
959 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
961 return 0;
964 /****************************************************************
965 * kaweth_probe
966 ****************************************************************/
967 static int kaweth_probe(
968 struct usb_interface *intf,
969 const struct usb_device_id *id /* from id_table */
972 struct usb_device *dev = interface_to_usbdev(intf);
973 struct kaweth_device *kaweth;
974 struct net_device *netdev;
975 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
976 int result = 0;
978 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
979 dev->devnum,
980 le16_to_cpu(dev->descriptor.idVendor),
981 le16_to_cpu(dev->descriptor.idProduct),
982 le16_to_cpu(dev->descriptor.bcdDevice));
984 dbg("Device at %p", dev);
986 dbg("Descriptor length: %x type: %x",
987 (int)dev->descriptor.bLength,
988 (int)dev->descriptor.bDescriptorType);
990 netdev = alloc_etherdev(sizeof(*kaweth));
991 if (!netdev)
992 return -ENOMEM;
994 kaweth = netdev_priv(netdev);
995 kaweth->dev = dev;
996 kaweth->net = netdev;
998 spin_lock_init(&kaweth->device_lock);
999 init_waitqueue_head(&kaweth->term_wait);
1001 dbg("Resetting.");
1003 kaweth_reset(kaweth);
1006 * If high byte of bcdDevice is nonzero, firmware is already
1007 * downloaded. Don't try to do it again, or we'll hang the device.
1010 if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
1011 info("Firmware present in device.");
1012 } else {
1013 /* Download the firmware */
1014 info("Downloading firmware...");
1015 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1016 if ((result = kaweth_download_firmware(kaweth,
1017 kaweth_new_code,
1018 len_kaweth_new_code,
1019 100,
1020 2)) < 0) {
1021 err("Error downloading firmware (%d)", result);
1022 goto err_fw;
1025 if ((result = kaweth_download_firmware(kaweth,
1026 kaweth_new_code_fix,
1027 len_kaweth_new_code_fix,
1028 100,
1029 3)) < 0) {
1030 err("Error downloading firmware fix (%d)", result);
1031 goto err_fw;
1034 if ((result = kaweth_download_firmware(kaweth,
1035 kaweth_trigger_code,
1036 len_kaweth_trigger_code,
1037 126,
1038 2)) < 0) {
1039 err("Error downloading trigger code (%d)", result);
1040 goto err_fw;
1044 if ((result = kaweth_download_firmware(kaweth,
1045 kaweth_trigger_code_fix,
1046 len_kaweth_trigger_code_fix,
1047 126,
1048 3)) < 0) {
1049 err("Error downloading trigger code fix (%d)", result);
1050 goto err_fw;
1054 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
1055 err("Error triggering firmware (%d)", result);
1056 goto err_fw;
1059 /* Device will now disappear for a moment... */
1060 info("Firmware loaded. I'll be back...");
1061 err_fw:
1062 free_page((unsigned long)kaweth->firmware_buf);
1063 free_netdev(netdev);
1064 return -EIO;
1067 result = kaweth_read_configuration(kaweth);
1069 if(result < 0) {
1070 err("Error reading configuration (%d), no net device created", result);
1071 goto err_free_netdev;
1074 info("Statistics collection: %x", kaweth->configuration.statistics_mask);
1075 info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1076 info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
1077 info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
1078 (int)kaweth->configuration.hw_addr[0],
1079 (int)kaweth->configuration.hw_addr[1],
1080 (int)kaweth->configuration.hw_addr[2],
1081 (int)kaweth->configuration.hw_addr[3],
1082 (int)kaweth->configuration.hw_addr[4],
1083 (int)kaweth->configuration.hw_addr[5]);
1085 if(!memcmp(&kaweth->configuration.hw_addr,
1086 &bcast_addr,
1087 sizeof(bcast_addr))) {
1088 err("Firmware not functioning properly, no net device created");
1089 goto err_free_netdev;
1092 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
1093 dbg("Error setting URB size");
1094 goto err_free_netdev;
1097 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
1098 err("Error setting SOFS wait");
1099 goto err_free_netdev;
1102 result = kaweth_set_receive_filter(kaweth,
1103 KAWETH_PACKET_FILTER_DIRECTED |
1104 KAWETH_PACKET_FILTER_BROADCAST |
1105 KAWETH_PACKET_FILTER_MULTICAST);
1107 if(result < 0) {
1108 err("Error setting receive filter");
1109 goto err_free_netdev;
1112 dbg("Initializing net device.");
1114 kaweth->intf = intf;
1116 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1117 if (!kaweth->tx_urb)
1118 goto err_free_netdev;
1119 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1120 if (!kaweth->rx_urb)
1121 goto err_only_tx;
1122 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1123 if (!kaweth->irq_urb)
1124 goto err_tx_and_rx;
1126 kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
1127 INTBUFFERSIZE,
1128 GFP_KERNEL,
1129 &kaweth->intbufferhandle);
1130 if (!kaweth->intbuffer)
1131 goto err_tx_and_rx_and_irq;
1132 kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
1133 KAWETH_BUF_SIZE,
1134 GFP_KERNEL,
1135 &kaweth->rxbufferhandle);
1136 if (!kaweth->rx_buf)
1137 goto err_all_but_rxbuf;
1139 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1140 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1141 sizeof(kaweth->configuration.hw_addr));
1143 netdev->open = kaweth_open;
1144 netdev->stop = kaweth_close;
1146 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1147 netdev->tx_timeout = kaweth_tx_timeout;
1149 netdev->hard_start_xmit = kaweth_start_xmit;
1150 netdev->set_multicast_list = kaweth_set_rx_mode;
1151 netdev->get_stats = kaweth_netdev_stats;
1152 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1153 SET_ETHTOOL_OPS(netdev, &ops);
1155 /* kaweth is zeroed as part of alloc_netdev */
1157 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
1159 SET_MODULE_OWNER(netdev);
1161 usb_set_intfdata(intf, kaweth);
1163 #if 0
1164 // dma_supported() is deeply broken on almost all architectures
1165 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1166 kaweth->net->features |= NETIF_F_HIGHDMA;
1167 #endif
1169 SET_NETDEV_DEV(netdev, &intf->dev);
1170 if (register_netdev(netdev) != 0) {
1171 err("Error registering netdev.");
1172 goto err_intfdata;
1175 info("kaweth interface created at %s", kaweth->net->name);
1177 dbg("Kaweth probe returning.");
1179 return 0;
1181 err_intfdata:
1182 usb_set_intfdata(intf, NULL);
1183 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1184 err_all_but_rxbuf:
1185 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1186 err_tx_and_rx_and_irq:
1187 usb_free_urb(kaweth->irq_urb);
1188 err_tx_and_rx:
1189 usb_free_urb(kaweth->rx_urb);
1190 err_only_tx:
1191 usb_free_urb(kaweth->tx_urb);
1192 err_free_netdev:
1193 free_netdev(netdev);
1195 return -EIO;
1198 /****************************************************************
1199 * kaweth_disconnect
1200 ****************************************************************/
1201 static void kaweth_disconnect(struct usb_interface *intf)
1203 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1204 struct net_device *netdev;
1206 info("Unregistering");
1208 usb_set_intfdata(intf, NULL);
1209 if (!kaweth) {
1210 warn("unregistering non-existant device");
1211 return;
1213 netdev = kaweth->net;
1215 dbg("Unregistering net device");
1216 unregister_netdev(netdev);
1218 usb_free_urb(kaweth->rx_urb);
1219 usb_free_urb(kaweth->tx_urb);
1220 usb_free_urb(kaweth->irq_urb);
1222 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1223 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1225 free_netdev(netdev);
1229 // FIXME this completion stuff is a modified clone of
1230 // an OLD version of some stuff in usb.c ...
1231 struct usb_api_data {
1232 wait_queue_head_t wqh;
1233 int done;
1236 /*-------------------------------------------------------------------*
1237 * completion handler for compatibility wrappers (sync control/bulk) *
1238 *-------------------------------------------------------------------*/
1239 static void usb_api_blocking_completion(struct urb *urb)
1241 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1243 awd->done=1;
1244 wake_up(&awd->wqh);
1247 /*-------------------------------------------------------------------*
1248 * COMPATIBILITY STUFF *
1249 *-------------------------------------------------------------------*/
1251 // Starts urb and waits for completion or timeout
1252 static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1254 struct usb_api_data awd;
1255 int status;
1257 init_waitqueue_head(&awd.wqh);
1258 awd.done = 0;
1260 urb->context = &awd;
1261 status = usb_submit_urb(urb, GFP_NOIO);
1262 if (status) {
1263 // something went wrong
1264 usb_free_urb(urb);
1265 return status;
1268 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1269 // timeout
1270 warn("usb_control/bulk_msg: timeout");
1271 usb_kill_urb(urb); // remove urb safely
1272 status = -ETIMEDOUT;
1274 else {
1275 status = urb->status;
1278 if (actual_length) {
1279 *actual_length = urb->actual_length;
1282 usb_free_urb(urb);
1283 return status;
1286 /*-------------------------------------------------------------------*/
1287 // returns status (negative) or length (positive)
1288 static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1289 unsigned int pipe,
1290 struct usb_ctrlrequest *cmd, void *data,
1291 int len, int timeout)
1293 struct urb *urb;
1294 int retv;
1295 int length = 0; /* shut up GCC */
1297 urb = usb_alloc_urb(0, GFP_NOIO);
1298 if (!urb)
1299 return -ENOMEM;
1301 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1302 len, usb_api_blocking_completion, NULL);
1304 retv = usb_start_wait_urb(urb, timeout, &length);
1305 if (retv < 0) {
1306 return retv;
1308 else {
1309 return length;
1314 /****************************************************************
1315 * kaweth_init
1316 ****************************************************************/
1317 static int __init kaweth_init(void)
1319 dbg("Driver loading");
1320 return usb_register(&kaweth_driver);
1323 /****************************************************************
1324 * kaweth_exit
1325 ****************************************************************/
1326 static void __exit kaweth_exit(void)
1328 usb_deregister(&kaweth_driver);
1331 module_init(kaweth_init);
1332 module_exit(kaweth_exit);