Merge with Linux 2.5.73.
[linux-2.6/linux-mips.git] / drivers / usb / net / usbnet.c
blob314d02187cf997eaeec58bfced6b7ead35c81218
1 /*
2 * USB Host-to-Host Links
3 * Copyright (C) 2000-2002 by David Brownell <dbrownell@users.sourceforge.net>
4 * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This is used for "USB networking", connecting USB hosts as peers.
24 * It can be used with USB "network cables", for IP-over-USB communications;
25 * Ethernet speeds without the Ethernet. USB devices (including some PDAs)
26 * can support such links directly, replacing device-specific protocols
27 * with Internet standard ones.
29 * The links can be bridged using the Ethernet bridging (net/bridge)
30 * support as appropriate. Devices currently supported include:
32 * - AnchorChip 2720
33 * - Belkin, eTEK (interops with Win32 drivers)
34 * - EPSON USB clients
35 * - GeneSys GL620USB-A
36 * - NetChip 1080 (interoperates with NetChip Win32 drivers)
37 * - Prolific PL-2301/2302 (replaces "plusb" driver)
38 * - PXA-250 or SA-1100 Linux PDAs like iPaq, Yopy, and Zaurus
40 * USB devices can implement their side of this protocol at the cost
41 * of two bulk endpoints; it's not restricted to "cable" applications.
42 * See the SA1110, Zaurus, or EPSON device/client support in this driver;
43 * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) are
44 * used inside USB slave/target devices.
47 * Status:
49 * - AN2720 ... not widely available, but reportedly works well
51 * - Belkin/eTEK ... no known issues
53 * - Both GeneSys and PL-230x use interrupt transfers for driver-to-driver
54 * handshaking; it'd be worth implementing those as "carrier detect".
55 * Prefer generic hooks, not minidriver-specific hacks.
57 * - For Netchip, should use keventd to poll via control requests to detect
58 * hardware level "carrier detect".
60 * - PL-230x ... the initialization protocol doesn't seem to match chip data
61 * sheets, sometimes it's not needed and sometimes it hangs. Prolific has
62 * not responded to repeated support/information requests.
64 * - SA-1100 PDAs ... the standard ARM Linux SA-1100 support works nicely,
65 * as found in www.handhelds.org and other kernels. The Sharp/Lineo
66 * kernels use different drivers, which also talk to this code.
68 * Interop with more Win32 drivers may be a good thing.
70 * Seems like reporting "peer connected" (carrier present) events may end
71 * up going through the netlink event system, not hotplug ... so new links
72 * would likely be handled with a link monitoring thread in some daemon.
74 * There are reports that bridging gives lower-than-usual throughput.
76 * Need smarter hotplug policy scripts ... ones that know how to arrange
77 * bridging with "brctl", and can handle static and dynamic ("pump") setups.
78 * Use those eventual "peer connected" events, and zeroconf.
81 * CHANGELOG:
83 * 13-sep-2000 experimental, new
84 * 10-oct-2000 usb_device_id table created.
85 * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
86 * 01-nov-2000 usb_device_id table and probing api update by
87 * Adam J. Richter <adam@yggdrasil.com>.
88 * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
89 * and prolific support, isolate net1080-specific bits, cleanup.
90 * fix unlink_urbs oops in D3 PM resume code path.
92 * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
93 * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
94 * AnchorChips 2720 support (from spec) for testing;
95 * fix bit-ordering problem with ethernet multicast addr
96 * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
97 * updates. Oleg Drokin (green@iXcelerator.com)
98 * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
99 * expecting cleared skb->cb and framing change to match latest
100 * handhelds.org version (Oleg). Enable device IDs from the
101 * Win32 Belkin driver; other cleanups (db).
102 * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
103 * cleanups for problems not yet seen in the field. (db)
104 * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
105 * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
106 * rx unlinks somehow weren't async; minor cleanup.
107 * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
108 * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
109 * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
110 * tie mostly to (sub)driver info. Workaround some PL-2302
111 * chips that seem to reject SET_INTERFACE requests.
113 * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
114 * Level of diagnostics is more configurable; they use device
115 * location (usb_device->devpath) instead of address (2.5).
116 * For tx_fixup, memflags can't be NOIO.
117 * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
118 * for USB 2.0 TTs) and memory shortages (potential) too. (db)
119 * Use "locally assigned" IEEE802 address space. (Brad Hards)
120 * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
121 * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
122 * cleanups and stubbed PXA-250 support (db), fix for framing
123 * issues on Z, net1080, and gl620a (Toby Milne)
125 * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
126 * vs pxa25x, and CDC Ethernet. Throttle down log floods on
127 * disconnect; other cleanups. (db) Flush net1080 fifos
128 * after several sequential framing errors. (Johannes Erdfelt)
130 *-------------------------------------------------------------------------*/
132 #include <linux/config.h>
133 #include <linux/module.h>
134 #include <linux/kmod.h>
135 #include <linux/sched.h>
136 #include <linux/init.h>
137 #include <linux/netdevice.h>
138 #include <linux/etherdevice.h>
139 #include <linux/random.h>
140 #include <linux/ethtool.h>
141 #include <linux/workqueue.h>
142 #include <asm/uaccess.h>
143 #include <asm/unaligned.h>
145 // #define DEBUG // error path messages, extra info
146 // #define VERBOSE // more; success messages
147 #define REALLY_QUEUE
149 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
150 # define DEBUG
151 #endif
152 #include <linux/usb.h>
154 #include <asm/io.h>
155 #include <asm/scatterlist.h>
156 #include <linux/mm.h>
157 #include <linux/dma-mapping.h>
160 #define DRIVER_VERSION "25-Apr-2003"
162 /*-------------------------------------------------------------------------*/
165 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
166 * Several dozen bytes of IPv4 data can fit in two such transactions.
167 * One maximum size Ethernet packet takes twenty four of them.
168 * For high speed, each frame comfortably fits almost 36 max size
169 * Ethernet packets (so queues should be bigger).
171 #ifdef REALLY_QUEUE
172 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
173 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
174 #else
175 #define RX_QLEN(dev) 1
176 #define TX_QLEN(dev) 1
177 #endif
179 // packets are always ethernet inside
180 // ... except they can be bigger (limit of 64K with NetChip framing)
181 #define MIN_PACKET sizeof(struct ethhdr)
182 #define MAX_PACKET 32768
184 // reawaken network queue this soon after stopping; else watchdog barks
185 #define TX_TIMEOUT_JIFFIES (5*HZ)
187 // throttle rx/tx briefly after some faults, so khubd might disconnect()
188 // us (it polls at HZ/4 usually) before we report too many false errors.
189 #define THROTTLE_JIFFIES (HZ/8)
191 // for vendor-specific control operations
192 #define CONTROL_TIMEOUT_MS (500) /* msec */
193 #define CONTROL_TIMEOUT_JIFFIES ((CONTROL_TIMEOUT_MS * HZ)/1000)
195 // between wakeups
196 #define UNLINK_TIMEOUT_JIFFIES ((3 /*ms*/ * HZ)/1000)
198 /*-------------------------------------------------------------------------*/
200 // randomly generated ethernet address
201 static u8 node_id [ETH_ALEN];
203 // state we keep for each device we handle
204 struct usbnet {
205 // housekeeping
206 struct usb_device *udev;
207 struct driver_info *driver_info;
208 wait_queue_head_t *wait;
210 // i/o info: pipes etc
211 unsigned in, out;
212 unsigned maxpacket;
213 struct timer_list delay;
215 // protocol/interface state
216 struct net_device *net;
217 struct net_device_stats stats;
218 int msg_level;
219 unsigned long data [5];
221 // various kinds of pending driver work
222 struct sk_buff_head rxq;
223 struct sk_buff_head txq;
224 struct sk_buff_head done;
225 struct tasklet_struct bh;
227 struct work_struct kevent;
228 unsigned long flags;
229 # define EVENT_TX_HALT 0
230 # define EVENT_RX_HALT 1
231 # define EVENT_RX_MEMORY 2
234 // device-specific info used by the driver
235 struct driver_info {
236 char *description;
238 int flags;
239 /* framing is CDC Ethernet, not writing ZLPs (hw issues), or optionally: */
240 #define FLAG_FRAMING_NC 0x0001 /* guard against device dropouts */
241 #define FLAG_FRAMING_GL 0x0002 /* genelink batches packets */
242 #define FLAG_FRAMING_Z 0x0004 /* zaurus adds a trailer */
244 #define FLAG_NO_SETINT 0x0010 /* device can't set_interface() */
245 #define FLAG_ETHER 0x0020 /* maybe use "eth%d" names */
247 /* init device ... can sleep, or cause probe() failure */
248 int (*bind)(struct usbnet *, struct usb_interface *);
250 /* cleanup device ... can sleep, but can't fail */
251 void (*unbind)(struct usbnet *, struct usb_interface *);
253 /* reset device ... can sleep */
254 int (*reset)(struct usbnet *);
256 /* see if peer is connected ... can sleep */
257 int (*check_connect)(struct usbnet *);
259 /* fixup rx packet (strip framing) */
260 int (*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
262 /* fixup tx packet (add framing) */
263 struct sk_buff *(*tx_fixup)(struct usbnet *dev,
264 struct sk_buff *skb, int flags);
266 // FIXME -- also an interrupt mechanism
267 // useful for at least PL2301/2302 and GL620USB-A
268 // and CDC use them to report 'is it connected' changes
270 /* for new devices, use the descriptor-reading code instead */
271 int in; /* rx endpoint */
272 int out; /* tx endpoint */
275 // we record the state for each of our queued skbs
276 enum skb_state {
277 illegal = 0,
278 tx_start, tx_done,
279 rx_start, rx_done, rx_cleanup
282 struct skb_data { // skb->cb is one of these
283 struct urb *urb;
284 struct usbnet *dev;
285 enum skb_state state;
286 size_t length;
289 static const char driver_name [] = "usbnet";
291 /* use ethtool to change the level for any given device */
292 static int msg_level = 1;
293 MODULE_PARM (msg_level, "i");
294 MODULE_PARM_DESC (msg_level, "Initial message level (default = 1)");
297 #define RUN_CONTEXT (in_irq () ? "in_irq" \
298 : (in_interrupt () ? "in_interrupt" : "can sleep"))
300 #ifdef DEBUG
301 #define devdbg(usbnet, fmt, arg...) \
302 printk(KERN_DEBUG "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
303 #else
304 #define devdbg(usbnet, fmt, arg...) do {} while(0)
305 #endif
307 #define deverr(usbnet, fmt, arg...) \
308 printk(KERN_ERR "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
309 #define devwarn(usbnet, fmt, arg...) \
310 printk(KERN_WARNING "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
312 #define devinfo(usbnet, fmt, arg...) \
313 do { if ((usbnet)->msg_level >= 1) \
314 printk(KERN_INFO "%s: " fmt "\n" , (usbnet)->net->name , ## arg); \
315 } while (0)
317 /*-------------------------------------------------------------------------*/
319 /* mostly for PDA style devices, which are always connected if present */
320 static int always_connected (struct usbnet *dev)
322 return 0;
325 /* handles CDC Ethernet and many other network "bulk data" interfaces */
326 static int
327 get_endpoints (struct usbnet *dev, struct usb_interface *intf)
329 int tmp;
330 struct usb_host_interface *alt;
331 struct usb_host_endpoint *in, *out;
333 for (tmp = 0; tmp < intf->max_altsetting; tmp++) {
334 unsigned ep;
336 in = out = 0;
337 alt = intf->altsetting + tmp;
339 /* take the first altsetting with in-bulk + out-bulk;
340 * ignore other endpoints and altsetttings.
342 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
343 struct usb_host_endpoint *e;
345 e = alt->endpoint + ep;
346 if (e->desc.bmAttributes != USB_ENDPOINT_XFER_BULK)
347 continue;
348 if (e->desc.bEndpointAddress & USB_DIR_IN) {
349 if (!in)
350 in = e;
351 } else {
352 if (!out)
353 out = e;
355 if (in && out)
356 goto found;
359 return -EINVAL;
361 found:
362 if (alt->desc.bAlternateSetting != 0
363 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
364 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
365 alt->desc.bAlternateSetting);
366 if (tmp < 0)
367 return tmp;
370 dev->in = usb_rcvbulkpipe (dev->udev,
371 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
372 dev->out = usb_sndbulkpipe (dev->udev,
373 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
374 return 0;
378 #ifdef CONFIG_USB_AN2720
379 #define HAVE_HARDWARE
381 /*-------------------------------------------------------------------------
383 * AnchorChips 2720 driver ... http://www.cypress.com
385 * This doesn't seem to have a way to detect whether the peer is
386 * connected, or need any reset handshaking. It's got pretty big
387 * internal buffers (handles most of a frame's worth of data).
388 * Chip data sheets don't describe any vendor control messages.
390 *-------------------------------------------------------------------------*/
392 static const struct driver_info an2720_info = {
393 .description = "AnchorChips/Cypress 2720",
394 // no reset available!
395 // no check_connect available!
397 .in = 2, .out = 2, // direction distinguishes these
400 #endif /* CONFIG_USB_AN2720 */
404 #ifdef CONFIG_USB_BELKIN
405 #define HAVE_HARDWARE
407 /*-------------------------------------------------------------------------
409 * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
411 * ... also two eTEK designs, including one sold as "Advance USBNET"
413 *-------------------------------------------------------------------------*/
415 static const struct driver_info belkin_info = {
416 .description = "Belkin, eTEK, or compatible",
419 #endif /* CONFIG_USB_BELKIN */
423 #if defined (CONFIG_USB_CDCETHER) || defined (CONFIG_USB_ZAURUS)
425 /*-------------------------------------------------------------------------
427 * Communications Device Class, Ethernet Control model
429 * Takes two interfaces. The DATA interface is inactive till an altsetting
430 * is selected. Configuration data includes class descriptors.
432 * Zaurus uses nonstandard framing, and doesn't uniquify its Ethernet
433 * addresses, but is otherwise CDC Ether.
435 * This should interop with whatever the 2.4 "CDCEther.c" driver
436 * (by Brad Hards) talked with.
438 *-------------------------------------------------------------------------*/
440 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
441 struct header_desc {
442 u8 bLength;
443 u8 bDescriptorType;
444 u8 bDescriptorSubType;
446 u16 bcdCDC;
447 } __attribute__ ((packed));
449 /* "Union Functional Descriptor" from CDC spec 5.2.3.X */
450 struct union_desc {
451 u8 bLength;
452 u8 bDescriptorType;
453 u8 bDescriptorSubType;
455 u8 bMasterInterface0;
456 u8 bSlaveInterface0;
457 /* ... and there could be other slave interfaces */
458 } __attribute__ ((packed));
460 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
461 struct ether_desc {
462 u8 bLength;
463 u8 bDescriptorType;
464 u8 bDescriptorSubType;
466 u8 iMACAddress;
467 u32 bmEthernetStatistics;
468 u16 wMaxSegmentSize;
469 u16 wNumberMCFilters;
470 u8 bNumberPowerFilters;
471 } __attribute__ ((packed));
473 struct cdc_info {
474 struct header_desc *header;
475 struct union_desc *u;
476 struct ether_desc *ether;
477 struct usb_interface *control;
478 struct usb_interface *data;
481 #include <linux/ctype.h>
483 static u8 nibble (unsigned char c)
485 if (likely (isdigit (c)))
486 return c - '0';
487 c = toupper (c);
488 if (likely (isxdigit (c)))
489 return 10 + c - 'A';
490 return 0;
493 static inline int get_ethernet_addr (struct usbnet *dev, struct ether_desc *e)
495 int tmp, i;
496 unsigned char buf [13];
498 tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
499 if (tmp < 0)
500 return tmp;
501 else if (tmp != 12)
502 return -EINVAL;
503 for (i = tmp = 0; i < 6; i++, tmp += 2)
504 dev->net->dev_addr [i] =
505 (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
506 return 0;
509 static struct usb_driver usbnet_driver;
511 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
513 u8 *buf = intf->altsetting->extra;
514 int len = intf->altsetting->extralen;
515 struct usb_interface_descriptor *d;
516 struct cdc_info *info = (void *) &dev->data;
517 int status;
519 if (sizeof dev->data < sizeof *info)
520 return -EDOM;
522 /* expect strict spec conformance for the descriptors */
523 memset (info, 0, sizeof *info);
524 info->control = intf;
525 while (len > 3) {
526 /* ignore bDescriptorType != CS_INTERFACE */
527 if (buf [1] != 0x24)
528 goto next_desc;
530 /* bDescriptorSubType identifies three "must have" descriptors;
531 * save them for later.
533 switch (buf [2]) {
534 case 0x00: /* Header, mostly useless */
535 if (info->header)
536 goto bad_desc;
537 info->header = (void *) buf;
538 if (info->header->bLength != sizeof *info->header)
539 goto bad_desc;
540 break;
541 case 0x06: /* Union (groups interfaces) */
542 if (info->u)
543 goto bad_desc;
544 info->u = (void *) buf;
545 if (info->u->bLength != sizeof *info->u)
546 goto bad_desc;
547 d = &intf->altsetting->desc;
548 if (info->u->bMasterInterface0 != d->bInterfaceNumber)
549 goto bad_desc;
550 info->data = dev->udev->actconfig->interface;
551 if (intf != (info->data + info->u->bMasterInterface0))
552 goto bad_desc;
554 /* a data interface altsetting does the real i/o */
555 info->data += info->u->bSlaveInterface0;
556 d = &info->data->altsetting->desc;
557 if (info->u->bSlaveInterface0 != d->bInterfaceNumber
558 || d->bInterfaceClass != USB_CLASS_CDC_DATA)
559 goto bad_desc;
560 if (usb_interface_claimed (info->data))
561 return -EBUSY;
562 break;
563 case 0x0F: /* Ethernet Networking */
564 if (info->ether)
565 goto bad_desc;
566 info->ether = (void *) buf;
567 if (info->ether->bLength != sizeof *info->ether)
568 goto bad_desc;
569 break;
571 next_desc:
572 len -= buf [0]; /* bLength */
573 buf += buf [0];
575 if (!info->header || !info ->u || !info->ether)
576 goto bad_desc;
578 #ifdef CONFIG_USB_ZAURUS
579 /* Zaurus ethernet addresses aren't unique ... */
580 if ((dev->driver_info->flags & FLAG_FRAMING_Z) != 0)
581 /* ignore */ ;
582 else
583 #endif
585 status = get_ethernet_addr (dev, info->ether);
586 if (status < 0)
587 return status;
590 /* claim data interface and set it up ... with side effects.
591 * network traffic can't flow until an altsetting is enabled.
593 usb_driver_claim_interface (&usbnet_driver, info->data, dev);
594 status = get_endpoints (dev, info->data);
595 if (status < 0) {
596 usb_driver_release_interface (&usbnet_driver, info->data);
597 return status;
600 /* FIXME cdc-ether has some multicast code too, though it complains
601 * in routine cases. info->ether describes the multicast support.
604 dev->net->mtu = cpu_to_le16p (&info->ether->wMaxSegmentSize)
605 - ETH_HLEN;
606 return 0;
608 bad_desc:
609 // devdbg (dev, "bad CDC descriptors");
610 return -ENODEV;
613 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
615 struct cdc_info *info = (void *) &dev->data;
617 /* disconnect master --> disconnect slave */
618 if (intf == info->control && info->data) {
619 usb_driver_release_interface (&usbnet_driver, info->data);
620 info->data = 0;
623 /* and vice versa (just in case) */
624 else if (intf == info->data && info->control) {
625 usb_driver_release_interface (&usbnet_driver, info->control);
626 info->control = 0;
631 #endif /* CONFIG_USB_ZAURUS || CONFIG_USB_CDCETHER */
634 #ifdef CONFIG_USB_CDCETHER
635 #define HAVE_HARDWARE
637 static const struct driver_info cdc_info = {
638 .description = "CDC Ethernet Device",
639 .flags = FLAG_ETHER,
640 // .check_connect = cdc_check_connect,
641 .bind = cdc_bind,
642 .unbind = cdc_unbind,
645 #endif /* CONFIG_USB_CDCETHER */
649 #ifdef CONFIG_USB_EPSON2888
650 #define HAVE_HARDWARE
652 /*-------------------------------------------------------------------------
654 * EPSON USB clients
656 * This is the same idea as Linux PDAs (below) except the firmware in the
657 * device might not be Tux-powered. Epson provides reference firmware that
658 * implements this interface. Product developers can reuse or modify that
659 * code, such as by using their own product and vendor codes.
661 * Support was from Juro Bystricky <bystricky.juro@erd.epson.com>
663 *-------------------------------------------------------------------------*/
665 static const struct driver_info epson2888_info = {
666 .description = "Epson USB Device",
667 .check_connect = always_connected,
669 .in = 4, .out = 3,
672 #endif /* CONFIG_USB_EPSON2888 */
675 #ifdef CONFIG_USB_GENESYS
676 #define HAVE_HARDWARE
678 /*-------------------------------------------------------------------------
680 * GeneSys GL620USB-A (www.genesyslogic.com.tw)
682 * ... should partially interop with the Win32 driver for this hardware
683 * The GeneSys docs imply there's some NDIS issue motivating this framing.
685 * Some info from GeneSys:
686 * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
687 * (Some cables, like the BAFO-100c, use the half duplex version.)
688 * - For the full duplex model, the low bit of the version code says
689 * which side is which ("left/right").
690 * - For the half duplex type, a control/interrupt handshake settles
691 * the transfer direction. (That's disabled here, partially coded.)
692 * A control URB would block until other side writes an interrupt.
694 * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
695 * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
697 *-------------------------------------------------------------------------*/
699 // control msg write command
700 #define GENELINK_CONNECT_WRITE 0xF0
701 // interrupt pipe index
702 #define GENELINK_INTERRUPT_PIPE 0x03
703 // interrupt read buffer size
704 #define INTERRUPT_BUFSIZE 0x08
705 // interrupt pipe interval value
706 #define GENELINK_INTERRUPT_INTERVAL 0x10
707 // max transmit packet number per transmit
708 #define GL_MAX_TRANSMIT_PACKETS 32
709 // max packet length
710 #define GL_MAX_PACKET_LEN 1514
711 // max receive buffer size
712 #define GL_RCV_BUF_SIZE \
713 (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
715 struct gl_packet {
716 u32 packet_length;
717 char packet_data [1];
720 struct gl_header {
721 u32 packet_count;
722 struct gl_packet packets;
725 #ifdef GENLINK_ACK
727 // FIXME: this code is incomplete, not debugged; it doesn't
728 // handle interrupts correctly. interrupts should be generic
729 // code like all other device I/O, anyway.
731 struct gl_priv {
732 struct urb *irq_urb;
733 char irq_buf [INTERRUPT_BUFSIZE];
736 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
738 int retval;
740 retval = usb_control_msg (dev->udev,
741 usb_sndctrlpipe (dev->udev, 0),
742 request,
743 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
744 value,
745 0, // index
746 0, // data buffer
747 0, // size
748 CONTROL_TIMEOUT_JIFFIES);
749 return retval;
752 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
754 int status = urb->status;
756 switch (status) {
757 case 0:
758 /* success */
759 break;
760 case -ECONNRESET:
761 case -ENOENT:
762 case -ESHUTDOWN:
763 /* this urb is terminated, clean up */
764 dbg("%s - urb shutting down with status: %d",
765 __FUNCTION__, status);
766 return;
767 default:
768 dbg("%s - nonzero urb status received: %d",
769 __FUNCTION__, urb->status);
772 status = usb_submit_urb (urb, GFP_ATOMIC);
773 if (status)
774 err ("%s - usb_submit_urb failed with result %d",
775 __FUNCTION__, status);
778 static int gl_interrupt_read (struct usbnet *dev)
780 struct gl_priv *priv = dev->priv_data;
781 int retval;
783 // issue usb interrupt read
784 if (priv && priv->irq_urb) {
785 // submit urb
786 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
787 dbg ("gl_interrupt_read: submit fail - %X...", retval);
788 else
789 dbg ("gl_interrupt_read: submit success...");
792 return 0;
795 // check whether another side is connected
796 static int genelink_check_connect (struct usbnet *dev)
798 int retval;
800 dbg ("genelink_check_connect...");
802 // detect whether another side is connected
803 if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
804 dbg ("%s: genelink_check_connect write fail - %X",
805 dev->net->name, retval);
806 return retval;
809 // usb interrupt read to ack another side
810 if ((retval = gl_interrupt_read (dev)) != 0) {
811 dbg ("%s: genelink_check_connect read fail - %X",
812 dev->net->name, retval);
813 return retval;
816 dbg ("%s: genelink_check_connect read success", dev->net->name);
817 return 0;
820 // allocate and initialize the private data for genelink
821 static int genelink_init (struct usbnet *dev)
823 struct gl_priv *priv;
825 // allocate the private data structure
826 if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
827 dbg ("%s: cannot allocate private data per device",
828 dev->net->name);
829 return -ENOMEM;
832 // allocate irq urb
833 if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
834 dbg ("%s: cannot allocate private irq urb per device",
835 dev->net->name);
836 kfree (priv);
837 return -ENOMEM;
840 // fill irq urb
841 usb_fill_int_urb (priv->irq_urb, dev->udev,
842 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
843 priv->irq_buf, INTERRUPT_BUFSIZE,
844 gl_interrupt_complete, 0,
845 GENELINK_INTERRUPT_INTERVAL);
847 // set private data pointer
848 dev->priv_data = priv;
850 return 0;
853 // release the private data
854 static int genelink_free (struct usbnet *dev)
856 struct gl_priv *priv = dev->priv_data;
858 if (!priv)
859 return 0;
861 // FIXME: can't cancel here; it's synchronous, and
862 // should have happened earlier in any case (interrupt
863 // handling needs to be generic)
865 // cancel irq urb first
866 usb_unlink_urb (priv->irq_urb);
868 // free irq urb
869 usb_free_urb (priv->irq_urb);
871 // free the private data structure
872 kfree (priv);
874 return 0;
877 #endif
879 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
881 struct gl_header *header;
882 struct gl_packet *packet;
883 struct sk_buff *gl_skb;
884 int status;
885 u32 size;
887 header = (struct gl_header *) skb->data;
889 // get the packet count of the received skb
890 le32_to_cpus (&header->packet_count);
891 if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
892 || (header->packet_count < 0)) {
893 dbg ("genelink: illegal received packet count %d",
894 header->packet_count);
895 return 0;
898 // set the current packet pointer to the first packet
899 packet = &header->packets;
901 // decrement the length for the packet count size 4 bytes
902 skb_pull (skb, 4);
904 while (header->packet_count > 1) {
905 // get the packet length
906 size = packet->packet_length;
908 // this may be a broken packet
909 if (size > GL_MAX_PACKET_LEN) {
910 dbg ("genelink: illegal rx length %d", size);
911 return 0;
914 // allocate the skb for the individual packet
915 gl_skb = alloc_skb (size, GFP_ATOMIC);
916 if (gl_skb) {
918 // copy the packet data to the new skb
919 memcpy(skb_put(gl_skb, size), packet->packet_data, size);
920 gl_skb->dev = dev->net;
922 // determine the packet's protocol ID
923 gl_skb->protocol = eth_type_trans (gl_skb, dev->net);
925 // update the status
926 dev->stats.rx_packets++;
927 dev->stats.rx_bytes += size;
929 // notify os of the received packet
930 status = netif_rx (gl_skb);
933 // advance to the next packet
934 packet = (struct gl_packet *)
935 &packet->packet_data [size];
936 header->packet_count--;
938 // shift the data pointer to the next gl_packet
939 skb_pull (skb, size + 4);
942 // skip the packet length field 4 bytes
943 skb_pull (skb, 4);
945 if (skb->len > GL_MAX_PACKET_LEN) {
946 dbg ("genelink: illegal rx length %d", skb->len);
947 return 0;
949 return 1;
952 static struct sk_buff *
953 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
955 int padlen;
956 int length = skb->len;
957 int headroom = skb_headroom (skb);
958 int tailroom = skb_tailroom (skb);
959 u32 *packet_count;
960 u32 *packet_len;
962 // FIXME: magic numbers, bleech
963 padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
965 if ((!skb_cloned (skb))
966 && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
967 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
968 skb->data = memmove (skb->head + (4 + 4*1),
969 skb->data, skb->len);
970 skb->tail = skb->data + skb->len;
972 } else {
973 struct sk_buff *skb2;
974 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
975 dev_kfree_skb_any (skb);
976 skb = skb2;
979 // attach the packet count to the header
980 packet_count = (u32 *) skb_push (skb, (4 + 4*1));
981 packet_len = packet_count + 1;
983 // FIXME little endian?
984 *packet_count = 1;
985 *packet_len = length;
987 // add padding byte
988 if ((skb->len % dev->maxpacket) == 0)
989 skb_put (skb, 1);
991 return skb;
994 static const struct driver_info genelink_info = {
995 .description = "Genesys GeneLink",
996 .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
997 .rx_fixup = genelink_rx_fixup,
998 .tx_fixup = genelink_tx_fixup,
1000 .in = 1, .out = 2,
1002 #ifdef GENELINK_ACK
1003 .check_connect =genelink_check_connect,
1004 #endif
1007 #endif /* CONFIG_USB_GENESYS */
1011 #ifdef CONFIG_USB_NET1080
1012 #define HAVE_HARDWARE
1014 /*-------------------------------------------------------------------------
1016 * Netchip 1080 driver ... http://www.netchip.com
1017 * Used in LapLink cables
1019 *-------------------------------------------------------------------------*/
1021 #define dev_packet_id data[0]
1022 #define frame_errors data[1]
1025 * NetChip framing of ethernet packets, supporting additional error
1026 * checks for links that may drop bulk packets from inside messages.
1027 * Odd USB length == always short read for last usb packet.
1028 * - nc_header
1029 * - Ethernet header (14 bytes)
1030 * - payload
1031 * - (optional padding byte, if needed so length becomes odd)
1032 * - nc_trailer
1034 * This framing is to be avoided for non-NetChip devices.
1037 struct nc_header { // packed:
1038 u16 hdr_len; // sizeof nc_header (LE, all)
1039 u16 packet_len; // payload size (including ethhdr)
1040 u16 packet_id; // detects dropped packets
1041 #define MIN_HEADER 6
1043 // all else is optional, and must start with:
1044 // u16 vendorId; // from usb-if
1045 // u16 productId;
1046 } __attribute__((__packed__));
1048 #define PAD_BYTE ((unsigned char)0xAC)
1050 struct nc_trailer {
1051 u16 packet_id;
1052 } __attribute__((__packed__));
1054 // packets may use FLAG_FRAMING_NC and optional pad
1055 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
1056 + sizeof (struct ethhdr) \
1057 + (mtu) \
1058 + 1 \
1059 + sizeof (struct nc_trailer))
1061 #define MIN_FRAMED FRAMED_SIZE(0)
1065 * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
1066 * before the hardware drops it. If that's done, the driver will need to
1067 * frame network packets to guard against the dropped USB packets. The win32
1068 * driver sets this for both sides of the link.
1070 #define NC_READ_TTL_MS ((u8)255) // ms
1073 * We ignore most registers and EEPROM contents.
1075 #define REG_USBCTL ((u8)0x04)
1076 #define REG_TTL ((u8)0x10)
1077 #define REG_STATUS ((u8)0x11)
1080 * Vendor specific requests to read/write data
1082 #define REQUEST_REGISTER ((u8)0x10)
1083 #define REQUEST_EEPROM ((u8)0x11)
1085 static int
1086 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
1088 int status = usb_control_msg (dev->udev,
1089 usb_rcvctrlpipe (dev->udev, 0),
1090 req,
1091 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1092 0, regnum,
1093 retval_ptr, sizeof *retval_ptr,
1094 CONTROL_TIMEOUT_JIFFIES);
1095 if (status > 0)
1096 status = 0;
1097 if (!status)
1098 le16_to_cpus (retval_ptr);
1099 return status;
1102 static inline int
1103 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
1105 return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
1108 // no retval ... can become async, usable in_interrupt()
1109 static void
1110 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
1112 usb_control_msg (dev->udev,
1113 usb_sndctrlpipe (dev->udev, 0),
1114 req,
1115 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1116 value, regnum,
1117 0, 0, // data is in setup packet
1118 CONTROL_TIMEOUT_JIFFIES);
1121 static inline void
1122 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
1124 nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
1128 #if 0
1129 static void nc_dump_registers (struct usbnet *dev)
1131 u8 reg;
1132 u16 *vp = kmalloc (sizeof (u16));
1134 if (!vp) {
1135 dbg ("no memory?");
1136 return;
1139 dbg ("%s registers:", dev->net->name);
1140 for (reg = 0; reg < 0x20; reg++) {
1141 int retval;
1143 // reading some registers is trouble
1144 if (reg >= 0x08 && reg <= 0xf)
1145 continue;
1146 if (reg >= 0x12 && reg <= 0x1e)
1147 continue;
1149 retval = nc_register_read (dev, reg, vp);
1150 if (retval < 0)
1151 dbg ("%s reg [0x%x] ==> error %d",
1152 dev->net->name, reg, retval);
1153 else
1154 dbg ("%s reg [0x%x] = 0x%x",
1155 dev->net->name, reg, *vp);
1157 kfree (vp);
1159 #endif
1162 /*-------------------------------------------------------------------------*/
1165 * Control register
1168 #define USBCTL_WRITABLE_MASK 0x1f0f
1169 // bits 15-13 reserved, r/o
1170 #define USBCTL_ENABLE_LANG (1 << 12)
1171 #define USBCTL_ENABLE_MFGR (1 << 11)
1172 #define USBCTL_ENABLE_PROD (1 << 10)
1173 #define USBCTL_ENABLE_SERIAL (1 << 9)
1174 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
1175 // bits 7-4 reserved, r/o
1176 #define USBCTL_FLUSH_OTHER (1 << 3)
1177 #define USBCTL_FLUSH_THIS (1 << 2)
1178 #define USBCTL_DISCONN_OTHER (1 << 1)
1179 #define USBCTL_DISCONN_THIS (1 << 0)
1181 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
1183 #ifdef DEBUG
1184 devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
1185 " this%s%s;"
1186 " other%s%s; r/o 0x%x",
1187 dev->udev->bus->bus_name, dev->udev->devpath,
1188 usbctl,
1189 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
1190 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
1191 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
1192 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
1193 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
1195 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
1196 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
1197 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
1198 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
1199 usbctl & ~USBCTL_WRITABLE_MASK
1201 #endif
1204 /*-------------------------------------------------------------------------*/
1207 * Status register
1210 #define STATUS_PORT_A (1 << 15)
1212 #define STATUS_CONN_OTHER (1 << 14)
1213 #define STATUS_SUSPEND_OTHER (1 << 13)
1214 #define STATUS_MAILBOX_OTHER (1 << 12)
1215 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
1217 #define STATUS_CONN_THIS (1 << 6)
1218 #define STATUS_SUSPEND_THIS (1 << 5)
1219 #define STATUS_MAILBOX_THIS (1 << 4)
1220 #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03)
1222 #define STATUS_UNSPEC_MASK 0x0c8c
1223 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
1226 static inline void nc_dump_status (struct usbnet *dev, u16 status)
1228 #ifdef DEBUG
1229 devdbg (dev, "net1080 %s-%s status 0x%x:"
1230 " this (%c) PKT=%d%s%s%s;"
1231 " other PKT=%d%s%s%s; unspec 0x%x",
1232 dev->udev->bus->bus_name, dev->udev->devpath,
1233 status,
1235 // XXX the packet counts don't seem right
1236 // (1 at reset, not 0); maybe UNSPEC too
1238 (status & STATUS_PORT_A) ? 'A' : 'B',
1239 STATUS_PACKETS_THIS (status),
1240 (status & STATUS_CONN_THIS) ? " CON" : "",
1241 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
1242 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
1244 STATUS_PACKETS_OTHER (status),
1245 (status & STATUS_CONN_OTHER) ? " CON" : "",
1246 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
1247 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
1249 status & STATUS_UNSPEC_MASK
1251 #endif
1254 /*-------------------------------------------------------------------------*/
1257 * TTL register
1260 #define TTL_THIS(ttl) (0x00ff & ttl)
1261 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
1262 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
1264 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
1266 #ifdef DEBUG
1267 devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
1268 dev->udev->bus->bus_name, dev->udev->devpath,
1269 ttl,
1271 TTL_THIS (ttl),
1272 TTL_OTHER (ttl)
1274 #endif
1277 /*-------------------------------------------------------------------------*/
1279 static int net1080_reset (struct usbnet *dev)
1281 u16 usbctl, status, ttl;
1282 u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1283 int retval;
1285 if (!vp)
1286 return -ENOMEM;
1288 // nc_dump_registers (dev);
1290 if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
1291 dbg ("can't read %s-%s status: %d",
1292 dev->udev->bus->bus_name, dev->udev->devpath, retval);
1293 goto done;
1295 status = *vp;
1296 // nc_dump_status (dev, status);
1298 if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
1299 dbg ("can't read USBCTL, %d", retval);
1300 goto done;
1302 usbctl = *vp;
1303 // nc_dump_usbctl (dev, usbctl);
1305 nc_register_write (dev, REG_USBCTL,
1306 USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
1308 if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
1309 dbg ("can't read TTL, %d", retval);
1310 goto done;
1312 ttl = *vp;
1313 // nc_dump_ttl (dev, ttl);
1315 nc_register_write (dev, REG_TTL,
1316 MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
1317 dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
1319 if (dev->msg_level >= 2)
1320 devinfo (dev, "port %c, peer %sconnected",
1321 (status & STATUS_PORT_A) ? 'A' : 'B',
1322 (status & STATUS_CONN_OTHER) ? "" : "dis"
1324 retval = 0;
1326 done:
1327 kfree (vp);
1328 return retval;
1331 static int net1080_check_connect (struct usbnet *dev)
1333 int retval;
1334 u16 status;
1335 u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1337 if (!vp)
1338 return -ENOMEM;
1339 retval = nc_register_read (dev, REG_STATUS, vp);
1340 status = *vp;
1341 kfree (vp);
1342 if (retval != 0) {
1343 dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
1344 return retval;
1346 if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
1347 return -ENOLINK;
1348 return 0;
1351 static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
1353 kfree (urb->context);
1354 usb_free_urb(urb);
1357 static void nc_ensure_sync (struct usbnet *dev)
1359 dev->frame_errors++;
1360 if (dev->frame_errors > 5) {
1361 struct urb *urb;
1362 struct usb_ctrlrequest *req;
1363 int status;
1365 /* Send a flush */
1366 urb = usb_alloc_urb (0, SLAB_ATOMIC);
1367 if (!urb)
1368 return;
1370 req = kmalloc (sizeof *req, GFP_ATOMIC);
1371 if (!req) {
1372 usb_free_urb (urb);
1373 return;
1376 req->bRequestType = USB_DIR_OUT
1377 | USB_TYPE_VENDOR
1378 | USB_RECIP_DEVICE;
1379 req->bRequest = REQUEST_REGISTER;
1380 req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
1381 | USBCTL_FLUSH_OTHER);
1382 req->wIndex = cpu_to_le16 (REG_USBCTL);
1383 req->wLength = cpu_to_le16 (0);
1385 /* queue an async control request, we don't need
1386 * to do anything when it finishes except clean up.
1388 usb_fill_control_urb (urb, dev->udev,
1389 usb_sndctrlpipe (dev->udev, 0),
1390 (unsigned char *) req,
1391 NULL, 0,
1392 nc_flush_complete, req);
1393 status = usb_submit_urb (urb, GFP_ATOMIC);
1394 if (status) {
1395 kfree (req);
1396 usb_free_urb (urb);
1397 return;
1400 devdbg (dev, "flush net1080; too many framing errors");
1401 dev->frame_errors = 0;
1405 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1407 struct nc_header *header;
1408 struct nc_trailer *trailer;
1410 if (!(skb->len & 0x01)
1411 || MIN_FRAMED > skb->len
1412 || skb->len > FRAMED_SIZE (dev->net->mtu)) {
1413 dev->stats.rx_frame_errors++;
1414 dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
1415 (int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net->mtu),
1416 dev->net->mtu);
1417 nc_ensure_sync (dev);
1418 return 0;
1421 header = (struct nc_header *) skb->data;
1422 le16_to_cpus (&header->hdr_len);
1423 le16_to_cpus (&header->packet_len);
1424 if (FRAMED_SIZE (header->packet_len) > MAX_PACKET) {
1425 dev->stats.rx_frame_errors++;
1426 dbg ("packet too big, %d", header->packet_len);
1427 nc_ensure_sync (dev);
1428 return 0;
1429 } else if (header->hdr_len < MIN_HEADER) {
1430 dev->stats.rx_frame_errors++;
1431 dbg ("header too short, %d", header->hdr_len);
1432 nc_ensure_sync (dev);
1433 return 0;
1434 } else if (header->hdr_len > MIN_HEADER) {
1435 // out of band data for us?
1436 dbg ("header OOB, %d bytes",
1437 header->hdr_len - MIN_HEADER);
1438 nc_ensure_sync (dev);
1439 // switch (vendor/product ids) { ... }
1441 skb_pull (skb, header->hdr_len);
1443 trailer = (struct nc_trailer *)
1444 (skb->data + skb->len - sizeof *trailer);
1445 skb_trim (skb, skb->len - sizeof *trailer);
1447 if ((header->packet_len & 0x01) == 0) {
1448 if (skb->data [header->packet_len] != PAD_BYTE) {
1449 dev->stats.rx_frame_errors++;
1450 dbg ("bad pad");
1451 return 0;
1453 skb_trim (skb, skb->len - 1);
1455 if (skb->len != header->packet_len) {
1456 dev->stats.rx_frame_errors++;
1457 dbg ("bad packet len %d (expected %d)",
1458 skb->len, header->packet_len);
1459 nc_ensure_sync (dev);
1460 return 0;
1462 if (header->packet_id != get_unaligned (&trailer->packet_id)) {
1463 dev->stats.rx_fifo_errors++;
1464 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
1465 header->packet_id, trailer->packet_id);
1466 return 0;
1468 #if 0
1469 devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
1470 header->packet_len, header->packet_id);
1471 #endif
1472 dev->frame_errors = 0;
1473 return 1;
1476 static struct sk_buff *
1477 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1479 int padlen;
1480 struct sk_buff *skb2;
1482 padlen = ((skb->len + sizeof (struct nc_header)
1483 + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
1484 if (!skb_cloned (skb)) {
1485 int headroom = skb_headroom (skb);
1486 int tailroom = skb_tailroom (skb);
1488 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
1489 && sizeof (struct nc_header) <= headroom)
1490 /* There's enough head and tail room */
1491 return skb;
1493 if ((sizeof (struct nc_header) + padlen
1494 + sizeof (struct nc_trailer)) <
1495 (headroom + tailroom)) {
1496 /* There's enough total room, so just readjust */
1497 skb->data = memmove (skb->head
1498 + sizeof (struct nc_header),
1499 skb->data, skb->len);
1500 skb->tail = skb->data + skb->len;
1501 return skb;
1505 /* Create a new skb to use with the correct size */
1506 skb2 = skb_copy_expand (skb,
1507 sizeof (struct nc_header),
1508 sizeof (struct nc_trailer) + padlen,
1509 flags);
1510 dev_kfree_skb_any (skb);
1511 return skb2;
1514 static const struct driver_info net1080_info = {
1515 .description = "NetChip TurboCONNECT",
1516 .flags = FLAG_FRAMING_NC,
1517 .reset = net1080_reset,
1518 .check_connect =net1080_check_connect,
1519 .rx_fixup = net1080_rx_fixup,
1520 .tx_fixup = net1080_tx_fixup,
1523 #endif /* CONFIG_USB_NET1080 */
1527 #ifdef CONFIG_USB_PL2301
1528 #define HAVE_HARDWARE
1530 /*-------------------------------------------------------------------------
1532 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
1534 * The protocol and handshaking used here should be bug-compatible
1535 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
1537 *-------------------------------------------------------------------------*/
1540 * Bits 0-4 can be used for software handshaking; they're set from
1541 * one end, cleared from the other, "read" with the interrupt byte.
1543 #define PL_S_EN (1<<7) /* (feature only) suspend enable */
1544 /* reserved bit -- rx ready (6) ? */
1545 #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
1546 #define PL_RESET_OUT (1<<4) /* reset output pipe */
1547 #define PL_RESET_IN (1<<3) /* reset input pipe */
1548 #define PL_TX_C (1<<2) /* transmission complete */
1549 #define PL_TX_REQ (1<<1) /* transmission received */
1550 #define PL_PEER_E (1<<0) /* peer exists */
1552 static inline int
1553 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
1555 return usb_control_msg (dev->udev,
1556 usb_rcvctrlpipe (dev->udev, 0),
1557 req,
1558 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1559 val, index,
1560 0, 0,
1561 CONTROL_TIMEOUT_JIFFIES);
1564 static inline int
1565 pl_clear_QuickLink_features (struct usbnet *dev, int val)
1567 return pl_vendor_req (dev, 1, (u8) val, 0);
1570 static inline int
1571 pl_set_QuickLink_features (struct usbnet *dev, int val)
1573 return pl_vendor_req (dev, 3, (u8) val, 0);
1576 /*-------------------------------------------------------------------------*/
1578 static int pl_reset (struct usbnet *dev)
1580 return pl_set_QuickLink_features (dev,
1581 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
1584 static const struct driver_info prolific_info = {
1585 .description = "Prolific PL-2301/PL-2302",
1586 .flags = FLAG_NO_SETINT,
1587 /* some PL-2302 versions seem to fail usb_set_interface() */
1588 .reset = pl_reset,
1591 #endif /* CONFIG_USB_PL2301 */
1595 #ifdef CONFIG_USB_ARMLINUX
1596 #define HAVE_HARDWARE
1598 /*-------------------------------------------------------------------------
1600 * Intel's SA-1100 chip integrates basic USB support, and is used
1601 * in PDAs like some iPaqs, the Yopy, some Zaurus models, and more.
1602 * When they run Linux, arch/arm/mach-sa1100/usb-eth.c may be used to
1603 * network using minimal USB framing data.
1605 * This describes the driver currently in standard ARM Linux kernels.
1606 * The Zaurus uses a different driver (see later).
1608 * PXA25x and PXA210 use XScale cores (ARM v5TE) with better USB support
1609 * and different USB endpoint numbering than the SA1100 devices. The
1610 * mach-pxa/usb-eth.c driver re-uses the device ids from mach-sa1100
1611 * so we rely on the endpoint descriptors.
1613 *-------------------------------------------------------------------------*/
1615 static const struct driver_info linuxdev_info = {
1616 .description = "Linux Device",
1617 .check_connect = always_connected,
1620 static const struct driver_info yopy_info = {
1621 .description = "Yopy",
1622 .check_connect = always_connected,
1625 static const struct driver_info blob_info = {
1626 .description = "Boot Loader OBject",
1627 .check_connect = always_connected,
1630 #endif /* CONFIG_USB_ARMLINUX */
1633 #ifdef CONFIG_USB_ZAURUS
1634 #define HAVE_HARDWARE
1636 #include <linux/crc32.h>
1638 /*-------------------------------------------------------------------------
1640 * Zaurus is also a SA-1110 based PDA, but one using a different driver
1641 * (and framing) for its USB slave/gadget controller than the case above.
1643 * For the current version of that driver, the main way that framing is
1644 * nonstandard (also from perspective of the CDC ethernet model!) is a
1645 * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
1646 * haven't been fully worked around.
1648 *-------------------------------------------------------------------------*/
1650 static struct sk_buff *
1651 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1653 int padlen;
1654 struct sk_buff *skb2;
1656 padlen = 2;
1657 if (!skb_cloned (skb)) {
1658 int tailroom = skb_tailroom (skb);
1659 if ((padlen + 4) <= tailroom)
1660 goto done;
1662 skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
1663 dev_kfree_skb_any (skb);
1664 skb = skb2;
1665 if (skb) {
1666 u32 fcs;
1667 done:
1668 fcs = crc32_le (~0, skb->data, skb->len);
1669 fcs = ~fcs;
1671 *skb_put (skb, 1) = fcs & 0xff;
1672 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
1673 *skb_put (skb, 1) = (fcs>>16) & 0xff;
1674 *skb_put (skb, 1) = (fcs>>24) & 0xff;
1676 return skb;
1679 static const struct driver_info zaurus_sl5x00_info = {
1680 .description = "Sharp Zaurus SL-5x00",
1681 .flags = FLAG_FRAMING_Z,
1682 .check_connect = always_connected,
1683 .bind = cdc_bind,
1684 .unbind = cdc_unbind,
1685 .tx_fixup = zaurus_tx_fixup,
1687 static const struct driver_info zaurus_sla300_info = {
1688 .description = "Sharp Zaurus SL-A300",
1689 .flags = FLAG_FRAMING_Z,
1690 .check_connect = always_connected,
1691 .tx_fixup = zaurus_tx_fixup,
1693 .in = 1, .out = 2,
1695 static const struct driver_info zaurus_slb500_info = {
1696 /* Japanese B500 ~= US SL-5600 */
1697 .description = "Sharp Zaurus SL-B500",
1698 .flags = FLAG_FRAMING_Z,
1699 .check_connect = always_connected,
1700 .tx_fixup = zaurus_tx_fixup,
1702 .in = 1, .out = 2,
1705 // SL-5600 and C-700 are PXA based; should resemble A300
1707 #endif
1710 /*-------------------------------------------------------------------------
1712 * Network Device Driver (peer link to "Host Device", from USB host)
1714 *-------------------------------------------------------------------------*/
1716 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1718 struct usbnet *dev = (struct usbnet *) net->priv;
1720 if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
1721 return -EINVAL;
1722 #ifdef CONFIG_USB_NET1080
1723 if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
1724 if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
1725 return -EINVAL;
1727 #endif
1728 #ifdef CONFIG_USB_GENESYS
1729 if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
1730 && new_mtu > GL_MAX_PACKET_LEN)
1731 return -EINVAL;
1732 #endif
1733 // no second zero-length packet read wanted after mtu-sized packets
1734 if (((new_mtu + sizeof (struct ethhdr)) % dev->maxpacket) == 0)
1735 return -EDOM;
1736 net->mtu = new_mtu;
1737 return 0;
1740 /*-------------------------------------------------------------------------*/
1742 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1744 return &((struct usbnet *) net->priv)->stats;
1747 /*-------------------------------------------------------------------------*/
1749 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1750 * completion callbacks. 2.5 should have fixed those bugs...
1753 static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
1755 struct sk_buff_head *list = skb->list;
1756 unsigned long flags;
1758 spin_lock_irqsave (&list->lock, flags);
1759 __skb_unlink (skb, list);
1760 spin_unlock (&list->lock);
1761 spin_lock (&dev->done.lock);
1762 __skb_queue_tail (&dev->done, skb);
1763 if (dev->done.qlen == 1)
1764 tasklet_schedule (&dev->bh);
1765 spin_unlock_irqrestore (&dev->done.lock, flags);
1768 /* some work can't be done in tasklets, so we use keventd
1770 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1771 * but tasklet_schedule() doesn't. hope the failure is rare.
1773 static void defer_kevent (struct usbnet *dev, int work)
1775 set_bit (work, &dev->flags);
1776 if (!schedule_work (&dev->kevent))
1777 deverr (dev, "kevent %d may have been dropped", work);
1778 else
1779 devdbg (dev, "kevent %d scheduled", work);
1782 /*-------------------------------------------------------------------------*/
1784 static void rx_complete (struct urb *urb, struct pt_regs *regs);
1786 static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
1788 struct sk_buff *skb;
1789 struct skb_data *entry;
1790 int retval = 0;
1791 unsigned long lockflags;
1792 size_t size;
1794 #ifdef CONFIG_USB_NET1080
1795 if (dev->driver_info->flags & FLAG_FRAMING_NC)
1796 size = FRAMED_SIZE (dev->net->mtu);
1797 else
1798 #endif
1799 #ifdef CONFIG_USB_GENESYS
1800 if (dev->driver_info->flags & FLAG_FRAMING_GL)
1801 size = GL_RCV_BUF_SIZE;
1802 else
1803 #endif
1804 #ifdef CONFIG_USB_ZAURUS
1805 if (dev->driver_info->flags & FLAG_FRAMING_Z)
1806 size = 6 + (sizeof (struct ethhdr) + dev->net->mtu);
1807 else
1808 #endif
1809 size = (sizeof (struct ethhdr) + dev->net->mtu);
1811 if ((skb = alloc_skb (size, flags)) == 0) {
1812 devdbg (dev, "no rx skb");
1813 defer_kevent (dev, EVENT_RX_MEMORY);
1814 usb_free_urb (urb);
1815 return;
1818 entry = (struct skb_data *) skb->cb;
1819 entry->urb = urb;
1820 entry->dev = dev;
1821 entry->state = rx_start;
1822 entry->length = 0;
1824 usb_fill_bulk_urb (urb, dev->udev, dev->in,
1825 skb->data, size, rx_complete, skb);
1826 urb->transfer_flags |= URB_ASYNC_UNLINK;
1828 spin_lock_irqsave (&dev->rxq.lock, lockflags);
1830 if (netif_running (dev->net)
1831 && netif_device_present (dev->net)
1832 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1833 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
1834 case -EPIPE:
1835 defer_kevent (dev, EVENT_RX_HALT);
1836 break;
1837 case -ENOMEM:
1838 defer_kevent (dev, EVENT_RX_MEMORY);
1839 break;
1840 case -ENODEV:
1841 devdbg (dev, "device gone");
1842 netif_device_detach (dev->net);
1843 break;
1844 default:
1845 devdbg (dev, "rx submit, %d", retval);
1846 tasklet_schedule (&dev->bh);
1847 break;
1848 case 0:
1849 __skb_queue_tail (&dev->rxq, skb);
1851 } else {
1852 devdbg (dev, "rx: stopped");
1853 retval = -ENOLINK;
1855 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1856 if (retval) {
1857 dev_kfree_skb_any (skb);
1858 usb_free_urb (urb);
1863 /*-------------------------------------------------------------------------*/
1865 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1867 if (dev->driver_info->rx_fixup
1868 && !dev->driver_info->rx_fixup (dev, skb))
1869 goto error;
1870 // else network stack removes extra byte if we forced a short packet
1872 if (skb->len) {
1873 int status;
1875 skb->dev = dev->net;
1876 skb->protocol = eth_type_trans (skb, dev->net);
1877 dev->stats.rx_packets++;
1878 dev->stats.rx_bytes += skb->len;
1880 #ifdef VERBOSE
1881 devdbg (dev, "< rx, len %d, type 0x%x",
1882 skb->len + sizeof (struct ethhdr), skb->protocol);
1883 #endif
1884 memset (skb->cb, 0, sizeof (struct skb_data));
1885 status = netif_rx (skb);
1886 if (status != NET_RX_SUCCESS)
1887 devdbg (dev, "netif_rx status %d", status);
1888 } else {
1889 devdbg (dev, "drop");
1890 error:
1891 dev->stats.rx_errors++;
1892 skb_queue_tail (&dev->done, skb);
1896 /*-------------------------------------------------------------------------*/
1898 static void rx_complete (struct urb *urb, struct pt_regs *regs)
1900 struct sk_buff *skb = (struct sk_buff *) urb->context;
1901 struct skb_data *entry = (struct skb_data *) skb->cb;
1902 struct usbnet *dev = entry->dev;
1903 int urb_status = urb->status;
1905 skb_put (skb, urb->actual_length);
1906 entry->state = rx_done;
1907 entry->urb = 0;
1909 switch (urb_status) {
1910 // success
1911 case 0:
1912 if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
1913 entry->state = rx_cleanup;
1914 dev->stats.rx_errors++;
1915 dev->stats.rx_length_errors++;
1916 devdbg (dev, "rx length %d", skb->len);
1918 break;
1920 // stalls need manual reset. this is rare ... except that
1921 // when going through USB 2.0 TTs, unplug appears this way.
1922 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1923 // storm, recovering as needed.
1924 case -EPIPE:
1925 dev->stats.rx_errors++;
1926 defer_kevent (dev, EVENT_RX_HALT);
1927 // FALLTHROUGH
1929 // software-driven interface shutdown
1930 case -ECONNRESET: // async unlink
1931 case -ESHUTDOWN: // hardware gone
1932 #ifdef VERBOSE
1933 devdbg (dev, "rx shutdown, code %d", urb_status);
1934 #endif
1935 goto block;
1937 // we get controller i/o faults during khubd disconnect() delays.
1938 // throttle down resubmits, to avoid log floods; just temporarily,
1939 // so we still recover when the fault isn't a khubd delay.
1940 case -EPROTO: // ehci
1941 case -ETIMEDOUT: // ohci
1942 case -EILSEQ: // uhci
1943 dev->stats.rx_errors++;
1944 if (!timer_pending (&dev->delay)) {
1945 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1946 devdbg (dev, "rx throttle %d", urb_status);
1948 block:
1949 entry->state = rx_cleanup;
1950 entry->urb = urb;
1951 urb = 0;
1952 break;
1954 // data overrun ... flush fifo?
1955 case -EOVERFLOW:
1956 dev->stats.rx_over_errors++;
1957 // FALLTHROUGH
1959 default:
1960 entry->state = rx_cleanup;
1961 dev->stats.rx_errors++;
1962 devdbg (dev, "rx status %d", urb_status);
1963 break;
1966 defer_bh (dev, skb);
1968 if (urb) {
1969 if (netif_running (dev->net)
1970 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1971 rx_submit (dev, urb, GFP_ATOMIC);
1972 return;
1974 usb_free_urb (urb);
1976 #ifdef VERBOSE
1977 devdbg (dev, "no read resubmitted");
1978 #endif /* VERBOSE */
1981 /*-------------------------------------------------------------------------*/
1983 // unlink pending rx/tx; completion handlers do all other cleanup
1985 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1987 unsigned long flags;
1988 struct sk_buff *skb, *skbnext;
1989 int count = 0;
1991 spin_lock_irqsave (&q->lock, flags);
1992 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1993 struct skb_data *entry;
1994 struct urb *urb;
1995 int retval;
1997 entry = (struct skb_data *) skb->cb;
1998 urb = entry->urb;
1999 skbnext = skb->next;
2001 // during some PM-driven resume scenarios,
2002 // these (async) unlinks complete immediately
2003 retval = usb_unlink_urb (urb);
2004 if (retval != -EINPROGRESS && retval != 0)
2005 devdbg (dev, "unlink urb err, %d", retval);
2006 else
2007 count++;
2009 spin_unlock_irqrestore (&q->lock, flags);
2010 return count;
2014 /*-------------------------------------------------------------------------*/
2016 // precondition: never called in_interrupt
2018 static int usbnet_stop (struct net_device *net)
2020 struct usbnet *dev = (struct usbnet *) net->priv;
2021 int temp;
2022 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
2023 DECLARE_WAITQUEUE (wait, current);
2025 netif_stop_queue (net);
2027 if (dev->msg_level >= 2)
2028 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
2029 dev->stats.rx_packets, dev->stats.tx_packets,
2030 dev->stats.rx_errors, dev->stats.tx_errors
2033 // ensure there are no more active urbs
2034 add_wait_queue (&unlink_wakeup, &wait);
2035 dev->wait = &unlink_wakeup;
2036 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
2038 // maybe wait for deletions to finish.
2039 while (skb_queue_len (&dev->rxq)
2040 && skb_queue_len (&dev->txq)
2041 && skb_queue_len (&dev->done)) {
2042 set_current_state (TASK_UNINTERRUPTIBLE);
2043 schedule_timeout (UNLINK_TIMEOUT_JIFFIES);
2044 devdbg (dev, "waited for %d urb completions", temp);
2046 dev->wait = 0;
2047 remove_wait_queue (&unlink_wakeup, &wait);
2049 // deferred work (task, timer, softirq) must also stop
2050 flush_scheduled_work ();
2051 del_timer_sync (&dev->delay);
2052 tasklet_kill (&dev->bh);
2054 return 0;
2057 /*-------------------------------------------------------------------------*/
2059 // posts reads, and enables write queuing
2061 // precondition: never called in_interrupt
2063 static int usbnet_open (struct net_device *net)
2065 struct usbnet *dev = (struct usbnet *) net->priv;
2066 int retval = 0;
2067 struct driver_info *info = dev->driver_info;
2069 // put into "known safe" state
2070 if (info->reset && (retval = info->reset (dev)) < 0) {
2071 devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s",
2072 retval,
2073 dev->udev->bus->bus_name, dev->udev->devpath,
2074 info->description);
2075 goto done;
2078 // insist peer be connected
2079 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
2080 devdbg (dev, "can't open; %d", retval);
2081 goto done;
2084 netif_start_queue (net);
2085 if (dev->msg_level >= 2)
2086 devinfo (dev, "open: enable queueing "
2087 "(rx %d, tx %d) mtu %d %s framing",
2088 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
2089 (info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL))
2090 ? ((info->flags & FLAG_FRAMING_NC)
2091 ? "NetChip"
2092 : "GeneSys")
2093 : "raw"
2096 // delay posting reads until we're fully open
2097 tasklet_schedule (&dev->bh);
2098 done:
2099 return retval;
2102 /*-------------------------------------------------------------------------*/
2104 static inline int
2105 usbnet_ethtool_ioctl (struct net_device *net, void __user *useraddr)
2107 struct usbnet *dev = (struct usbnet *) net->priv;
2108 u32 cmd;
2110 if (get_user (cmd, (u32 *)useraddr))
2111 return -EFAULT;
2112 switch (cmd) {
2114 case ETHTOOL_GDRVINFO: { /* get driver info */
2115 struct ethtool_drvinfo info;
2117 memset (&info, 0, sizeof info);
2118 info.cmd = ETHTOOL_GDRVINFO;
2119 strncpy (info.driver, driver_name, sizeof info.driver);
2120 strncpy (info.version, DRIVER_VERSION, sizeof info.version);
2121 strncpy (info.fw_version, dev->driver_info->description,
2122 sizeof info.fw_version);
2123 usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info);
2124 if (copy_to_user (useraddr, &info, sizeof (info)))
2125 return -EFAULT;
2126 return 0;
2129 case ETHTOOL_GLINK: /* get link status */
2130 if (dev->driver_info->check_connect) {
2131 struct ethtool_value edata = { ETHTOOL_GLINK };
2133 edata.data = dev->driver_info->check_connect (dev) == 0;
2134 if (copy_to_user (useraddr, &edata, sizeof (edata)))
2135 return -EFAULT;
2136 return 0;
2138 break;
2140 case ETHTOOL_GMSGLVL: { /* get message-level */
2141 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
2143 edata.data = dev->msg_level;
2144 if (copy_to_user (useraddr, &edata, sizeof (edata)))
2145 return -EFAULT;
2146 return 0;
2149 case ETHTOOL_SMSGLVL: { /* set message-level */
2150 struct ethtool_value edata;
2152 if (copy_from_user (&edata, useraddr, sizeof (edata)))
2153 return -EFAULT;
2154 dev->msg_level = edata.data;
2155 return 0;
2158 /* could also map RINGPARAM to RX/TX QLEN */
2161 /* Note that the ethtool user space code requires EOPNOTSUPP */
2162 return -EOPNOTSUPP;
2165 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
2167 switch (cmd) {
2168 case SIOCETHTOOL:
2169 return usbnet_ethtool_ioctl (net, (void __user *)rq->ifr_data);
2170 default:
2171 return -EOPNOTSUPP;
2175 /*-------------------------------------------------------------------------*/
2177 /* work that cannot be done in interrupt context uses keventd.
2179 * NOTE: with 2.5 we could do more of this using completion callbacks,
2180 * especially now that control transfers can be queued.
2182 static void
2183 kevent (void *data)
2185 struct usbnet *dev = data;
2186 int status;
2188 /* usb_clear_halt() needs a thread context */
2189 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
2190 unlink_urbs (dev, &dev->txq);
2191 status = usb_clear_halt (dev->udev, dev->out);
2192 if (status < 0)
2193 deverr (dev, "can't clear tx halt, status %d",
2194 status);
2195 else {
2196 clear_bit (EVENT_TX_HALT, &dev->flags);
2197 netif_wake_queue (dev->net);
2200 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
2201 unlink_urbs (dev, &dev->rxq);
2202 status = usb_clear_halt (dev->udev, dev->in);
2203 if (status < 0)
2204 deverr (dev, "can't clear rx halt, status %d",
2205 status);
2206 else {
2207 clear_bit (EVENT_RX_HALT, &dev->flags);
2208 tasklet_schedule (&dev->bh);
2212 /* tasklet could resubmit itself forever if memory is tight */
2213 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
2214 struct urb *urb = 0;
2216 if (netif_running (dev->net))
2217 urb = usb_alloc_urb (0, GFP_KERNEL);
2218 else
2219 clear_bit (EVENT_RX_MEMORY, &dev->flags);
2220 if (urb != 0) {
2221 clear_bit (EVENT_RX_MEMORY, &dev->flags);
2222 rx_submit (dev, urb, GFP_KERNEL);
2223 tasklet_schedule (&dev->bh);
2227 if (dev->flags)
2228 devdbg (dev, "kevent done, flags = 0x%lx",
2229 dev->flags);
2232 /*-------------------------------------------------------------------------*/
2234 static void tx_complete (struct urb *urb, struct pt_regs *regs)
2236 struct sk_buff *skb = (struct sk_buff *) urb->context;
2237 struct skb_data *entry = (struct skb_data *) skb->cb;
2238 struct usbnet *dev = entry->dev;
2240 if (urb->status == 0) {
2241 dev->stats.tx_packets++;
2242 dev->stats.tx_bytes += entry->length;
2243 } else {
2244 dev->stats.tx_errors++;
2246 switch (urb->status) {
2247 case -EPIPE:
2248 defer_kevent (dev, EVENT_TX_HALT);
2249 break;
2251 // like rx, tx gets controller i/o faults during khubd delays
2252 // and so it uses the same throttling mechanism.
2253 case -EPROTO: // ehci
2254 case -ETIMEDOUT: // ohci
2255 case -EILSEQ: // uhci
2256 if (!timer_pending (&dev->delay)) {
2257 mod_timer (&dev->delay,
2258 jiffies + THROTTLE_JIFFIES);
2259 devdbg (dev, "tx throttle %d", urb->status);
2261 netif_stop_queue (dev->net);
2262 break;
2263 default:
2264 devdbg (dev, "tx err %d", entry->urb->status);
2265 break;
2269 urb->dev = 0;
2270 entry->state = tx_done;
2271 defer_bh (dev, skb);
2274 /*-------------------------------------------------------------------------*/
2276 static void usbnet_tx_timeout (struct net_device *net)
2278 struct usbnet *dev = (struct usbnet *) net->priv;
2280 unlink_urbs (dev, &dev->txq);
2281 tasklet_schedule (&dev->bh);
2283 // FIXME: device recovery -- reset?
2286 /*-------------------------------------------------------------------------*/
2288 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
2290 struct usbnet *dev = (struct usbnet *) net->priv;
2291 int length;
2292 int retval = NET_XMIT_SUCCESS;
2293 struct urb *urb = 0;
2294 struct skb_data *entry;
2295 struct driver_info *info = dev->driver_info;
2296 unsigned long flags;
2297 #ifdef CONFIG_USB_NET1080
2298 struct nc_header *header = 0;
2299 struct nc_trailer *trailer = 0;
2300 #endif /* CONFIG_USB_NET1080 */
2302 // some devices want funky USB-level framing, for
2303 // win32 driver (usually) and/or hardware quirks
2304 if (info->tx_fixup) {
2305 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
2306 if (!skb) {
2307 devdbg (dev, "can't tx_fixup skb");
2308 goto drop;
2311 length = skb->len;
2313 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
2314 devdbg (dev, "no urb");
2315 goto drop;
2318 entry = (struct skb_data *) skb->cb;
2319 entry->urb = urb;
2320 entry->dev = dev;
2321 entry->state = tx_start;
2322 entry->length = length;
2324 // FIXME: reorganize a bit, so that fixup() fills out NetChip
2325 // framing too. (Packet ID update needs the spinlock...)
2326 // [ BETTER: we already own net->xmit_lock, that's enough ]
2328 #ifdef CONFIG_USB_NET1080
2329 if (info->flags & FLAG_FRAMING_NC) {
2330 header = (struct nc_header *) skb_push (skb, sizeof *header);
2331 header->hdr_len = cpu_to_le16 (sizeof (*header));
2332 header->packet_len = cpu_to_le16 (length);
2333 if (!((skb->len + sizeof *trailer) & 0x01))
2334 *skb_put (skb, 1) = PAD_BYTE;
2335 trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
2336 } else
2337 #endif /* CONFIG_USB_NET1080 */
2339 usb_fill_bulk_urb (urb, dev->udev, dev->out,
2340 skb->data, skb->len, tx_complete, skb);
2341 urb->transfer_flags |= URB_ASYNC_UNLINK;
2343 /* don't assume the hardware handles USB_ZERO_PACKET
2344 * NOTE: strictly conforming cdc-ether devices should expect
2345 * the ZLP here, but ignore the one-byte packet.
2347 * FIXME zero that byte, if it doesn't require a new skb.
2349 if ((length % dev->maxpacket) == 0)
2350 urb->transfer_buffer_length++;
2352 spin_lock_irqsave (&dev->txq.lock, flags);
2354 #ifdef CONFIG_USB_NET1080
2355 if (info->flags & FLAG_FRAMING_NC) {
2356 header->packet_id = cpu_to_le16 ((u16)dev->dev_packet_id++);
2357 put_unaligned (header->packet_id, &trailer->packet_id);
2358 #if 0
2359 devdbg (dev, "frame >tx h %d p %d id %d",
2360 header->hdr_len, header->packet_len,
2361 header->packet_id);
2362 #endif
2364 #endif /* CONFIG_USB_NET1080 */
2366 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
2367 case -EPIPE:
2368 netif_stop_queue (net);
2369 defer_kevent (dev, EVENT_TX_HALT);
2370 break;
2371 default:
2372 devdbg (dev, "tx: submit urb err %d", retval);
2373 break;
2374 case 0:
2375 net->trans_start = jiffies;
2376 __skb_queue_tail (&dev->txq, skb);
2377 if (dev->txq.qlen >= TX_QLEN (dev))
2378 netif_stop_queue (net);
2380 spin_unlock_irqrestore (&dev->txq.lock, flags);
2382 if (retval) {
2383 devdbg (dev, "drop, code %d", retval);
2384 drop:
2385 retval = NET_XMIT_DROP;
2386 dev->stats.tx_dropped++;
2387 if (skb)
2388 dev_kfree_skb_any (skb);
2389 usb_free_urb (urb);
2390 #ifdef VERBOSE
2391 } else {
2392 devdbg (dev, "> tx, len %d, type 0x%x",
2393 length, skb->protocol);
2394 #endif
2396 return retval;
2400 /*-------------------------------------------------------------------------*/
2402 // tasklet (work deferred from completions, in_irq) or timer
2404 static void usbnet_bh (unsigned long param)
2406 struct usbnet *dev = (struct usbnet *) param;
2407 struct sk_buff *skb;
2408 struct skb_data *entry;
2410 while ((skb = skb_dequeue (&dev->done))) {
2411 entry = (struct skb_data *) skb->cb;
2412 switch (entry->state) {
2413 case rx_done:
2414 entry->state = rx_cleanup;
2415 rx_process (dev, skb);
2416 continue;
2417 case tx_done:
2418 case rx_cleanup:
2419 usb_free_urb (entry->urb);
2420 dev_kfree_skb (skb);
2421 continue;
2422 default:
2423 devdbg (dev, "bogus skb state %d", entry->state);
2427 // waiting for all pending urbs to complete?
2428 if (dev->wait) {
2429 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
2430 wake_up (dev->wait);
2433 // or are we maybe short a few urbs?
2434 } else if (netif_running (dev->net)
2435 && netif_device_present (dev->net)
2436 && !timer_pending (&dev->delay)
2437 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2438 int temp = dev->rxq.qlen;
2439 int qlen = RX_QLEN (dev);
2441 if (temp < qlen) {
2442 struct urb *urb;
2443 int i;
2445 // don't refill the queue all at once
2446 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
2447 if ((urb = usb_alloc_urb (0, GFP_ATOMIC)) != 0)
2448 rx_submit (dev, urb, GFP_ATOMIC);
2450 if (temp != dev->rxq.qlen)
2451 devdbg (dev, "rxqlen %d --> %d",
2452 temp, dev->rxq.qlen);
2453 if (dev->rxq.qlen < qlen)
2454 tasklet_schedule (&dev->bh);
2456 if (dev->txq.qlen < TX_QLEN (dev))
2457 netif_wake_queue (dev->net);
2463 /*-------------------------------------------------------------------------
2465 * USB Device Driver support
2467 *-------------------------------------------------------------------------*/
2469 // precondition: never called in_interrupt
2471 static void usbnet_disconnect (struct usb_interface *intf)
2473 struct usbnet *dev;
2474 struct usb_device *xdev;
2476 dev = usb_get_intfdata(intf);
2477 usb_set_intfdata(intf, NULL);
2478 if (!dev)
2479 return;
2481 xdev = interface_to_usbdev (intf);
2483 devinfo (dev, "unregister usbnet usb-%s-%s, %s",
2484 xdev->bus->bus_name, xdev->devpath,
2485 dev->driver_info->description);
2487 unregister_netdev (dev->net);
2489 if (dev->driver_info->unbind)
2490 dev->driver_info->unbind (dev, intf);
2492 kfree(dev->net);
2493 kfree (dev);
2494 usb_put_dev (xdev);
2498 /*-------------------------------------------------------------------------*/
2500 // precondition: never called in_interrupt
2503 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
2505 struct usbnet *dev;
2506 struct net_device *net;
2507 struct usb_host_interface *interface;
2508 struct driver_info *info;
2509 struct usb_device *xdev;
2510 int status;
2512 info = (struct driver_info *) prod->driver_info;
2513 if (!info) {
2514 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
2515 return -ENODEV;
2517 xdev = interface_to_usbdev (udev);
2518 interface = &udev->altsetting [udev->act_altsetting];
2520 usb_get_dev (xdev);
2522 status = -ENOMEM;
2524 // set up our own records
2525 if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
2526 dbg ("can't kmalloc dev");
2527 goto out;
2529 memset (dev, 0, sizeof *dev);
2531 dev->udev = xdev;
2532 dev->driver_info = info;
2533 dev->msg_level = msg_level;
2534 skb_queue_head_init (&dev->rxq);
2535 skb_queue_head_init (&dev->txq);
2536 skb_queue_head_init (&dev->done);
2537 dev->bh.func = usbnet_bh;
2538 dev->bh.data = (unsigned long) dev;
2539 INIT_WORK (&dev->kevent, kevent, dev);
2540 dev->delay.function = usbnet_bh;
2541 dev->delay.data = (unsigned long) dev;
2542 init_timer (&dev->delay);
2544 // set up network interface records
2545 net = alloc_etherdev(0);
2546 if (!net)
2547 goto out1;
2549 SET_MODULE_OWNER (net);
2550 dev->net = net;
2551 net->priv = dev;
2552 strcpy (net->name, "usb%d");
2553 memcpy (net->dev_addr, node_id, sizeof node_id);
2555 // possible with some EHCI controllers
2556 if (dma_supported (&udev->dev, 0xffffffffffffffffULL))
2557 net->features |= NETIF_F_HIGHDMA;
2559 net->change_mtu = usbnet_change_mtu;
2560 net->get_stats = usbnet_get_stats;
2561 net->hard_start_xmit = usbnet_start_xmit;
2562 net->open = usbnet_open;
2563 net->stop = usbnet_stop;
2564 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
2565 net->tx_timeout = usbnet_tx_timeout;
2566 net->do_ioctl = usbnet_ioctl;
2568 // allow device-specific bind/init procedures
2569 // NOTE net->name still not usable ...
2570 if (info->bind) {
2571 status = info->bind (dev, udev);
2572 // heuristic: "usb%d" for links we know are two-host,
2573 // else "eth%d" when there's reasonable doubt. userspace
2574 // can rename the link if it knows better.
2575 if ((dev->driver_info->flags & FLAG_ETHER) != 0
2576 && (net->dev_addr [0] & 0x02) == 0)
2577 strcpy (net->name, "eth%d");
2578 } else if (!info->in || info->out)
2579 status = get_endpoints (dev, udev);
2580 else {
2581 dev->in = usb_rcvbulkpipe (xdev, info->in);
2582 dev->out = usb_sndbulkpipe (xdev, info->out);
2583 if (!(info->flags & FLAG_NO_SETINT))
2584 status = usb_set_interface (xdev,
2585 interface->desc.bInterfaceNumber,
2586 interface->desc.bAlternateSetting);
2587 else
2588 status = 0;
2591 if (status < 0)
2592 goto out2;
2594 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
2596 SET_NETDEV_DEV(dev->net, &dev->udev->dev);
2597 status = register_netdev (dev->net);
2598 if (status)
2599 goto out3;
2600 devinfo (dev, "register usbnet at usb-%s-%s, %s",
2601 xdev->bus->bus_name, xdev->devpath,
2602 dev->driver_info->description);
2604 // ok, it's ready to go.
2605 usb_set_intfdata (udev, dev);
2607 // start as if the link is up
2608 netif_device_attach (dev->net);
2610 return 0;
2612 out3:
2613 if (info->unbind)
2614 info->unbind (dev, udev);
2615 out2:
2616 kfree(net);
2617 out1:
2618 kfree(dev);
2619 out:
2620 usb_put_dev(xdev);
2621 return status;
2625 /*-------------------------------------------------------------------------*/
2627 #ifndef HAVE_HARDWARE
2628 #error You need to configure some hardware for this driver
2629 #endif
2632 * chip vendor names won't normally be on the cables, and
2633 * may not be on the device.
2636 static const struct usb_device_id products [] = {
2638 #ifdef CONFIG_USB_AN2720
2640 USB_DEVICE (0x0547, 0x2720), // AnchorChips defaults
2641 .driver_info = (unsigned long) &an2720_info,
2642 }, {
2643 USB_DEVICE (0x0547, 0x2727), // Xircom PGUNET
2644 .driver_info = (unsigned long) &an2720_info,
2646 #endif
2648 #ifdef CONFIG_USB_BELKIN
2650 USB_DEVICE (0x050d, 0x0004), // Belkin
2651 .driver_info = (unsigned long) &belkin_info,
2652 }, {
2653 USB_DEVICE (0x056c, 0x8100), // eTEK
2654 .driver_info = (unsigned long) &belkin_info,
2655 }, {
2656 USB_DEVICE (0x0525, 0x9901), // Advance USBNET (eTEK)
2657 .driver_info = (unsigned long) &belkin_info,
2659 #endif
2661 #ifdef CONFIG_USB_EPSON2888
2663 USB_DEVICE (0x0525, 0x2888), // EPSON USB client
2664 .driver_info = (unsigned long) &epson2888_info,
2666 #endif
2668 #ifdef CONFIG_USB_GENESYS
2670 USB_DEVICE (0x05e3, 0x0502), // GL620USB-A
2671 .driver_info = (unsigned long) &genelink_info,
2673 /* NOT: USB_DEVICE (0x05e3, 0x0501), // GL620USB
2674 * that's half duplex, not currently supported
2676 #endif
2678 #ifdef CONFIG_USB_NET1080
2680 USB_DEVICE (0x0525, 0x1080), // NetChip ref design
2681 .driver_info = (unsigned long) &net1080_info,
2682 }, {
2683 USB_DEVICE (0x06D0, 0x0622), // Laplink Gold
2684 .driver_info = (unsigned long) &net1080_info,
2686 #endif
2688 #ifdef CONFIG_USB_PL2301
2690 USB_DEVICE (0x067b, 0x0000), // PL-2301
2691 .driver_info = (unsigned long) &prolific_info,
2692 }, {
2693 USB_DEVICE (0x067b, 0x0001), // PL-2302
2694 .driver_info = (unsigned long) &prolific_info,
2696 #endif
2698 #ifdef CONFIG_USB_ARMLINUX
2700 * SA-1100 using standard ARM Linux kernels, or compatible.
2701 * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
2702 * The sa-1100 "usb-eth" driver handles the basic framing.
2704 * PXA25x or PXA210 ... these use a "usb-eth" driver much like
2705 * the sa1100 one, but hardware uses different endpoint numbers.
2708 // 1183 = 0x049F, both used as hex values?
2709 // Compaq "Itsy" vendor/product id
2710 USB_DEVICE (0x049F, 0x505A),
2711 .driver_info = (unsigned long) &linuxdev_info,
2712 }, {
2713 USB_DEVICE (0x0E7E, 0x1001), // G.Mate "Yopy"
2714 .driver_info = (unsigned long) &yopy_info,
2715 }, {
2716 USB_DEVICE (0x8086, 0x07d3), // "blob" bootloader
2717 .driver_info = (unsigned long) &blob_info,
2719 #endif
2721 #ifdef CONFIG_USB_ZAURUS
2723 * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2724 * Same idea as above, but different framing.
2727 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2728 | USB_DEVICE_ID_MATCH_DEVICE,
2729 .idVendor = 0x04DD,
2730 .idProduct = 0x8004,
2731 /* match the master interface */
2732 .bInterfaceClass = USB_CLASS_COMM,
2733 .bInterfaceSubClass = 6 /* Ethernet model */,
2734 .bInterfaceProtocol = 0,
2735 .driver_info = (unsigned long) &zaurus_sl5x00_info,
2736 }, {
2737 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2738 | USB_DEVICE_ID_MATCH_DEVICE,
2739 .idVendor = 0x04DD,
2740 .idProduct = 0x8005,
2741 .bInterfaceClass = 0x02,
2742 .bInterfaceSubClass = 0x0a,
2743 .bInterfaceProtocol = 0x00,
2744 .driver_info = (unsigned long) &zaurus_sla300_info,
2745 }, {
2746 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2747 | USB_DEVICE_ID_MATCH_DEVICE,
2748 .idVendor = 0x04DD,
2749 .idProduct = 0x8006,
2750 .bInterfaceClass = 0x02,
2751 .bInterfaceSubClass = 0x0a,
2752 .bInterfaceProtocol = 0x00,
2753 .driver_info = (unsigned long) &zaurus_slb500_info,
2755 #endif
2757 #ifdef CONFIG_USB_CDCETHER
2759 #ifndef CONFIG_USB_ZAURUS
2760 /* if we couldn't whitelist Zaurus, we must blacklist it */
2762 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2763 | USB_DEVICE_ID_MATCH_DEVICE,
2764 .idVendor = 0x04DD,
2765 .idProduct = 0x8004,
2766 /* match the master interface */
2767 .bInterfaceClass = USB_CLASS_COMM,
2768 .bInterfaceSubClass = 6 /* Ethernet model */,
2769 .bInterfaceProtocol = 0,
2770 .driver_info = 0, /* BLACKLIST */
2772 #endif
2775 /* CDC Ether uses two interfaces, not necessarily consecutive.
2776 * We match the main interface, ignoring the optional device
2777 * class so we could handle devices that aren't exclusively
2778 * CDC ether.
2780 * NOTE: this match must come AFTER entries working around
2781 * bugs/quirks in a given product (like Zaurus, above).
2783 USB_INTERFACE_INFO (USB_CLASS_COMM, 6 /* Ethernet model */, 0),
2784 .driver_info = (unsigned long) &cdc_info,
2786 #endif
2788 { }, // END
2790 MODULE_DEVICE_TABLE (usb, products);
2792 static struct usb_driver usbnet_driver = {
2793 .owner = THIS_MODULE,
2794 .name = driver_name,
2795 .id_table = products,
2796 .probe = usbnet_probe,
2797 .disconnect = usbnet_disconnect,
2800 /*-------------------------------------------------------------------------*/
2802 static int __init usbnet_init (void)
2804 // compiler should optimize this out
2805 if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
2806 BUG ();
2808 get_random_bytes (node_id, sizeof node_id);
2809 node_id [0] &= 0xfe; // clear multicast bit
2810 node_id [0] |= 0x02; // set local assignment bit (IEEE802)
2812 if (usb_register (&usbnet_driver) < 0)
2813 return -1;
2815 return 0;
2817 module_init (usbnet_init);
2819 static void __exit usbnet_exit (void)
2821 usb_deregister (&usbnet_driver);
2823 module_exit (usbnet_exit);
2825 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
2826 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
2827 MODULE_LICENSE ("GPL");