blob: d52480aced802aef8ce9a2664dd08b24451387f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Networking Links
David Brownellf29fc252005-08-31 09:52:31 -07003 * Copyright (C) 2000-2005 by David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/*
23 * This is a generic "USB networking" framework that works with several
24 * kinds of full and high speed networking devices:
25 *
26 * + USB host-to-host "network cables", used for IP-over-USB links.
27 * These are often used for Laplink style connectivity products.
28 * - AnchorChip 2720
29 * - Belkin, eTEK (interops with Win32 drivers)
30 * - GeneSys GL620USB-A
31 * - NetChip 1080 (interoperates with NetChip Win32 drivers)
32 * - Prolific PL-2301/2302 (replaces "plusb" driver)
33 * - KC Technology KC2190
34 *
35 * + Smart USB devices can support such links directly, using Internet
36 * standard protocols instead of proprietary host-to-device links.
37 * - Linux PDAs like iPaq, Yopy, and Zaurus
38 * - The BLOB boot loader (for diskless booting)
39 * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
40 * - Devices using EPSON's sample USB firmware
41 * - CDC-Ethernet class devices, such as many cable modems
42 *
43 * + Adapters to networks such as Ethernet.
44 * - AX8817X based USB 2.0 products
45 *
46 * Links to these devices can be bridged using Linux Ethernet bridging.
47 * With minor exceptions, these all use similar USB framing for network
48 * traffic, but need different protocols for control traffic.
49 *
50 * USB devices can implement their side of this protocol at the cost
51 * of two bulk endpoints; it's not restricted to "cable" applications.
52 * See the SA1110, Zaurus, or EPSON device/client support in this driver;
53 * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
54 * "g_ether" (in the Linux "gadget" framework) implement that behavior
55 * within devices.
56 *
57 *
58 * CHANGELOG:
59 *
60 * 13-sep-2000 experimental, new
61 * 10-oct-2000 usb_device_id table created.
62 * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
63 * 01-nov-2000 usb_device_id table and probing api update by
64 * Adam J. Richter <adam@yggdrasil.com>.
65 * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
66 * and prolific support, isolate net1080-specific bits, cleanup.
67 * fix unlink_urbs oops in D3 PM resume code path.
68 *
69 * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
70 * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
71 * AnchorChips 2720 support (from spec) for testing;
72 * fix bit-ordering problem with ethernet multicast addr
73 * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
74 * updates. Oleg Drokin (green@iXcelerator.com)
75 * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
76 * expecting cleared skb->cb and framing change to match latest
77 * handhelds.org version (Oleg). Enable device IDs from the
78 * Win32 Belkin driver; other cleanups (db).
79 * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
80 * cleanups for problems not yet seen in the field. (db)
81 * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
82 * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
83 * rx unlinks somehow weren't async; minor cleanup.
84 * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
85 * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
86 * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
87 * tie mostly to (sub)driver info. Workaround some PL-2302
88 * chips that seem to reject SET_INTERFACE requests.
89 *
90 * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
91 * Level of diagnostics is more configurable; they use device
92 * location (usb_device->devpath) instead of address (2.5).
93 * For tx_fixup, memflags can't be NOIO.
94 * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
95 * for USB 2.0 TTs) and memory shortages (potential) too. (db)
96 * Use "locally assigned" IEEE802 address space. (Brad Hards)
97 * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
98 * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
99 * cleanups and stubbed PXA-250 support (db), fix for framing
100 * issues on Z, net1080, and gl620a (Toby Milne)
101 *
102 * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
103 * vs pxa25x, and CDC Ethernet. Throttle down log floods on
104 * disconnect; other cleanups. (db) Flush net1080 fifos
105 * after several sequential framing errors. (Johannes Erdfelt)
106 * 22-aug-2003 AX8817X support (Dave Hollis).
107 * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
108 * (Neil Bortnak)
109 * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
110 *
111 * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
112 *-------------------------------------------------------------------------*/
113
114// #define DEBUG // error path messages, extra info
115// #define VERBOSE // more; success messages
116
117#include <linux/config.h>
118#ifdef CONFIG_USB_DEBUG
119# define DEBUG
120#endif
121#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sched.h>
123#include <linux/init.h>
124#include <linux/netdevice.h>
125#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#include <linux/ethtool.h>
127#include <linux/workqueue.h>
128#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#include <linux/usb.h>
David Brownellf29fc252005-08-31 09:52:31 -0700130
131#include "usbnet.h"
132
133#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135
136/*-------------------------------------------------------------------------*/
137
138/*
139 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
140 * Several dozen bytes of IPv4 data can fit in two such transactions.
141 * One maximum size Ethernet packet takes twenty four of them.
142 * For high speed, each frame comfortably fits almost 36 max size
143 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -0700144 *
145 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
146 * let the USB host controller be busy for 5msec or more before an irq
147 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
149#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
150#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152// reawaken network queue this soon after stopping; else watchdog barks
153#define TX_TIMEOUT_JIFFIES (5*HZ)
154
155// throttle rx/tx briefly after some faults, so khubd might disconnect()
156// us (it polls at HZ/4 usually) before we report too many false errors.
157#define THROTTLE_JIFFIES (HZ/8)
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159// between wakeups
160#define UNLINK_TIMEOUT_MS 3
161
162/*-------------------------------------------------------------------------*/
163
164// randomly generated ethernet address
165static u8 node_id [ETH_ALEN];
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static const char driver_name [] = "usbnet";
168
169/* use ethtool to change the level for any given device */
170static int msg_level = -1;
171module_param (msg_level, int, 0);
172MODULE_PARM_DESC (msg_level, "Override default message level");
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*-------------------------------------------------------------------------*/
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static u32 usbnet_get_link (struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* mostly for PDA style devices, which are always connected if present */
179static int always_connected (struct usbnet *dev)
180{
181 return 0;
182}
183
184/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -0700185int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int tmp;
188 struct usb_host_interface *alt = NULL;
189 struct usb_host_endpoint *in = NULL, *out = NULL;
190 struct usb_host_endpoint *status = NULL;
191
192 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
193 unsigned ep;
194
195 in = out = status = NULL;
196 alt = intf->altsetting + tmp;
197
198 /* take the first altsetting with in-bulk + out-bulk;
199 * remember any status endpoint, just in case;
200 * ignore other endpoints and altsetttings.
201 */
202 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
203 struct usb_host_endpoint *e;
204 int intr = 0;
205
206 e = alt->endpoint + ep;
207 switch (e->desc.bmAttributes) {
208 case USB_ENDPOINT_XFER_INT:
209 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
210 continue;
211 intr = 1;
212 /* FALLTHROUGH */
213 case USB_ENDPOINT_XFER_BULK:
214 break;
215 default:
216 continue;
217 }
218 if (e->desc.bEndpointAddress & USB_DIR_IN) {
219 if (!intr && !in)
220 in = e;
221 else if (intr && !status)
222 status = e;
223 } else {
224 if (!out)
225 out = e;
226 }
227 }
228 if (in && out)
229 break;
230 }
231 if (!alt || !in || !out)
232 return -EINVAL;
233
234 if (alt->desc.bAlternateSetting != 0
235 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
236 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
237 alt->desc.bAlternateSetting);
238 if (tmp < 0)
239 return tmp;
240 }
241
242 dev->in = usb_rcvbulkpipe (dev->udev,
243 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
244 dev->out = usb_sndbulkpipe (dev->udev,
245 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
246 dev->status = status;
247 return 0;
248}
David Brownell2e55cc72005-08-31 09:53:10 -0700249EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251static void intr_complete (struct urb *urb, struct pt_regs *regs);
252
253static int init_status (struct usbnet *dev, struct usb_interface *intf)
254{
255 char *buf = NULL;
256 unsigned pipe = 0;
257 unsigned maxp;
258 unsigned period;
259
260 if (!dev->driver_info->status)
261 return 0;
262
263 pipe = usb_rcvintpipe (dev->udev,
264 dev->status->desc.bEndpointAddress
265 & USB_ENDPOINT_NUMBER_MASK);
266 maxp = usb_maxpacket (dev->udev, pipe, 0);
267
268 /* avoid 1 msec chatter: min 8 msec poll rate */
269 period = max ((int) dev->status->desc.bInterval,
270 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
271
272 buf = kmalloc (maxp, SLAB_KERNEL);
273 if (buf) {
274 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
275 if (!dev->interrupt) {
276 kfree (buf);
277 return -ENOMEM;
278 } else {
279 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
280 buf, maxp, intr_complete, dev, period);
281 dev_dbg(&intf->dev,
282 "status ep%din, %d bytes period %d\n",
283 usb_pipeendpoint(pipe), maxp, period);
284 }
285 }
286 return 0;
287}
288
David Brownell2e55cc72005-08-31 09:53:10 -0700289/* Passes this packet up the stack, updating its accounting.
290 * Some link protocols batch packets, so their rx_fixup paths
291 * can return clones as well as just modify the original skb.
292 */
293void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int status;
296
297 skb->dev = dev->net;
298 skb->protocol = eth_type_trans (skb, dev->net);
299 dev->stats.rx_packets++;
300 dev->stats.rx_bytes += skb->len;
301
302 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700303 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 skb->len + sizeof (struct ethhdr), skb->protocol);
305 memset (skb->cb, 0, sizeof (struct skb_data));
306 status = netif_rx (skb);
307 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
308 devdbg (dev, "netif_rx status %d", status);
309}
David Brownell2e55cc72005-08-31 09:53:10 -0700310EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*-------------------------------------------------------------------------
314 *
315 * Communications Device Class declarations.
316 * Used by CDC Ethernet, and some CDC variants
317 *
318 *-------------------------------------------------------------------------*/
319
320#ifdef CONFIG_USB_CDCETHER
321#define NEED_GENERIC_CDC
322#endif
323
324#ifdef CONFIG_USB_ZAURUS
325/* Ethernet variant uses funky framing, broken ethernet addressing */
326#define NEED_GENERIC_CDC
327#endif
328
329#ifdef CONFIG_USB_RNDIS
330/* ACM variant uses even funkier framing, complex control RPC scheme */
331#define NEED_GENERIC_CDC
332#endif
333
334
335#ifdef NEED_GENERIC_CDC
336
337#include <linux/usb_cdc.h>
338
339struct cdc_state {
340 struct usb_cdc_header_desc *header;
341 struct usb_cdc_union_desc *u;
342 struct usb_cdc_ether_desc *ether;
343 struct usb_interface *control;
344 struct usb_interface *data;
345};
346
347static struct usb_driver usbnet_driver;
348
349/*
350 * probes control interface, claims data interface, collects the bulk
351 * endpoints, activates data interface (if needed), maybe sets MTU.
352 * all pure cdc, except for certain firmware workarounds.
353 */
354static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
355{
356 u8 *buf = intf->cur_altsetting->extra;
357 int len = intf->cur_altsetting->extralen;
358 struct usb_interface_descriptor *d;
359 struct cdc_state *info = (void *) &dev->data;
360 int status;
361 int rndis;
362
363 if (sizeof dev->data < sizeof *info)
364 return -EDOM;
365
366 /* expect strict spec conformance for the descriptors, but
367 * cope with firmware which stores them in the wrong place
368 */
369 if (len == 0 && dev->udev->actconfig->extralen) {
370 /* Motorola SB4100 (and others: Brad Hards says it's
371 * from a Broadcom design) put CDC descriptors here
372 */
373 buf = dev->udev->actconfig->extra;
374 len = dev->udev->actconfig->extralen;
375 if (len)
376 dev_dbg (&intf->dev,
377 "CDC descriptors on config\n");
378 }
379
380 /* this assumes that if there's a non-RNDIS vendor variant
381 * of cdc-acm, it'll fail RNDIS requests cleanly.
382 */
383 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
384
385 memset (info, 0, sizeof *info);
386 info->control = intf;
387 while (len > 3) {
388 if (buf [1] != USB_DT_CS_INTERFACE)
389 goto next_desc;
390
391 /* use bDescriptorSubType to identify the CDC descriptors.
392 * We expect devices with CDC header and union descriptors.
393 * For CDC Ethernet we need the ethernet descriptor.
394 * For RNDIS, ignore two (pointless) CDC modem descriptors
395 * in favor of a complicated OID-based RPC scheme doing what
396 * CDC Ethernet achieves with a simple descriptor.
397 */
398 switch (buf [2]) {
399 case USB_CDC_HEADER_TYPE:
400 if (info->header) {
401 dev_dbg (&intf->dev, "extra CDC header\n");
402 goto bad_desc;
403 }
404 info->header = (void *) buf;
405 if (info->header->bLength != sizeof *info->header) {
406 dev_dbg (&intf->dev, "CDC header len %u\n",
407 info->header->bLength);
408 goto bad_desc;
409 }
410 break;
411 case USB_CDC_UNION_TYPE:
412 if (info->u) {
413 dev_dbg (&intf->dev, "extra CDC union\n");
414 goto bad_desc;
415 }
416 info->u = (void *) buf;
417 if (info->u->bLength != sizeof *info->u) {
418 dev_dbg (&intf->dev, "CDC union len %u\n",
419 info->u->bLength);
420 goto bad_desc;
421 }
422
423 /* we need a master/control interface (what we're
424 * probed with) and a slave/data interface; union
425 * descriptors sort this all out.
426 */
427 info->control = usb_ifnum_to_if(dev->udev,
428 info->u->bMasterInterface0);
429 info->data = usb_ifnum_to_if(dev->udev,
430 info->u->bSlaveInterface0);
431 if (!info->control || !info->data) {
432 dev_dbg (&intf->dev,
433 "master #%u/%p slave #%u/%p\n",
434 info->u->bMasterInterface0,
435 info->control,
436 info->u->bSlaveInterface0,
437 info->data);
438 goto bad_desc;
439 }
440 if (info->control != intf) {
441 dev_dbg (&intf->dev, "bogus CDC Union\n");
442 /* Ambit USB Cable Modem (and maybe others)
443 * interchanges master and slave interface.
444 */
445 if (info->data == intf) {
446 info->data = info->control;
447 info->control = intf;
448 } else
449 goto bad_desc;
450 }
451
452 /* a data interface altsetting does the real i/o */
453 d = &info->data->cur_altsetting->desc;
454 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
455 dev_dbg (&intf->dev, "slave class %u\n",
456 d->bInterfaceClass);
457 goto bad_desc;
458 }
459 break;
460 case USB_CDC_ETHERNET_TYPE:
461 if (info->ether) {
462 dev_dbg (&intf->dev, "extra CDC ether\n");
463 goto bad_desc;
464 }
465 info->ether = (void *) buf;
466 if (info->ether->bLength != sizeof *info->ether) {
467 dev_dbg (&intf->dev, "CDC ether len %u\n",
David Brownelle3bc8b42005-06-15 08:04:30 -0700468 info->ether->bLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto bad_desc;
470 }
David Brownellf29fc252005-08-31 09:52:31 -0700471 dev->hard_mtu = le16_to_cpu(
472 info->ether->wMaxSegmentSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* because of Zaurus, we may be ignoring the host
474 * side link address we were given.
475 */
476 break;
477 }
478next_desc:
479 len -= buf [0]; /* bLength */
480 buf += buf [0];
481 }
482
483 if (!info->header || !info->u || (!rndis && !info->ether)) {
484 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
485 info->header ? "" : "header ",
486 info->u ? "" : "union ",
487 info->ether ? "" : "ether ");
488 goto bad_desc;
489 }
490
491 /* claim data interface and set it up ... with side effects.
492 * network traffic can't flow until an altsetting is enabled.
493 */
494 status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
495 if (status < 0)
496 return status;
David Brownell2e55cc72005-08-31 09:53:10 -0700497 status = usbnet_get_endpoints (dev, info->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (status < 0) {
499 /* ensure immediate exit from usbnet_disconnect */
500 usb_set_intfdata(info->data, NULL);
501 usb_driver_release_interface (&usbnet_driver, info->data);
502 return status;
503 }
504
505 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
506 dev->status = NULL;
507 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
508 struct usb_endpoint_descriptor *desc;
509
510 dev->status = &info->control->cur_altsetting->endpoint [0];
511 desc = &dev->status->desc;
512 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
513 || !(desc->bEndpointAddress & USB_DIR_IN)
514 || (le16_to_cpu(desc->wMaxPacketSize)
515 < sizeof (struct usb_cdc_notification))
516 || !desc->bInterval) {
517 dev_dbg (&intf->dev, "bad notification endpoint\n");
518 dev->status = NULL;
519 }
520 }
521 if (rndis && !dev->status) {
522 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
523 usb_set_intfdata(info->data, NULL);
524 usb_driver_release_interface (&usbnet_driver, info->data);
525 return -ENODEV;
526 }
527 return 0;
528
529bad_desc:
530 dev_info (&dev->udev->dev, "bad CDC descriptors\n");
531 return -ENODEV;
532}
533
534static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
535{
536 struct cdc_state *info = (void *) &dev->data;
537
538 /* disconnect master --> disconnect slave */
539 if (intf == info->control && info->data) {
540 /* ensure immediate exit from usbnet_disconnect */
541 usb_set_intfdata(info->data, NULL);
542 usb_driver_release_interface (&usbnet_driver, info->data);
543 info->data = NULL;
544 }
545
546 /* and vice versa (just in case) */
547 else if (intf == info->data && info->control) {
548 /* ensure immediate exit from usbnet_disconnect */
549 usb_set_intfdata(info->control, NULL);
550 usb_driver_release_interface (&usbnet_driver, info->control);
551 info->control = NULL;
552 }
553}
554
David Brownell80615f82005-05-10 06:45:10 -0700555#endif /* NEED_GENERIC_CDC */
556
557
558#ifdef CONFIG_USB_CDCETHER
559#define HAVE_HARDWARE
560
561/*-------------------------------------------------------------------------
562 *
563 * Communications Device Class, Ethernet Control model
564 *
565 * Takes two interfaces. The DATA interface is inactive till an altsetting
566 * is selected. Configuration data includes class descriptors.
567 *
568 * This should interop with whatever the 2.4 "CDCEther.c" driver
569 * (by Brad Hards) talked with.
570 *
571 *-------------------------------------------------------------------------*/
572
573#include <linux/ctype.h>
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576static void dumpspeed (struct usbnet *dev, __le32 *speeds)
577{
578 if (netif_msg_timer (dev))
579 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
580 __le32_to_cpu(speeds[0]) / 1000,
581 __le32_to_cpu(speeds[1]) / 1000);
582}
583
584static void cdc_status (struct usbnet *dev, struct urb *urb)
585{
586 struct usb_cdc_notification *event;
587
588 if (urb->actual_length < sizeof *event)
589 return;
590
591 /* SPEED_CHANGE can get split into two 8-byte packets */
592 if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
593 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
594 return;
595 }
596
597 event = urb->transfer_buffer;
598 switch (event->bNotificationType) {
599 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
600 if (netif_msg_timer (dev))
601 devdbg (dev, "CDC: carrier %s",
602 event->wValue ? "on" : "off");
603 if (event->wValue)
604 netif_carrier_on(dev->net);
605 else
606 netif_carrier_off(dev->net);
607 break;
608 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
609 if (netif_msg_timer (dev))
610 devdbg (dev, "CDC: speed change (len %d)",
611 urb->actual_length);
612 if (urb->actual_length != (sizeof *event + 8))
613 set_bit (EVENT_STS_SPLIT, &dev->flags);
614 else
615 dumpspeed (dev, (__le32 *) &event[1]);
616 break;
617 // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: /* RNDIS; or unsolicited */
618 default:
619 deverr (dev, "CDC: unexpected notification %02x!",
620 event->bNotificationType);
621 break;
622 }
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static u8 nibble (unsigned char c)
626{
627 if (likely (isdigit (c)))
628 return c - '0';
629 c = toupper (c);
630 if (likely (isxdigit (c)))
631 return 10 + c - 'A';
632 return 0;
633}
634
635static inline int
636get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
637{
638 int tmp, i;
639 unsigned char buf [13];
640
641 tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
642 if (tmp != 12) {
643 dev_dbg (&dev->udev->dev,
644 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
645 if (tmp >= 0)
646 tmp = -EINVAL;
647 return tmp;
648 }
649 for (i = tmp = 0; i < 6; i++, tmp += 2)
650 dev->net->dev_addr [i] =
651 (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
652 return 0;
653}
654
655static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
656{
657 int status;
658 struct cdc_state *info = (void *) &dev->data;
659
660 status = generic_cdc_bind (dev, intf);
661 if (status < 0)
662 return status;
663
664 status = get_ethernet_addr (dev, info->ether);
665 if (status < 0) {
666 usb_set_intfdata(info->data, NULL);
667 usb_driver_release_interface (&usbnet_driver, info->data);
668 return status;
669 }
670
671 /* FIXME cdc-ether has some multicast code too, though it complains
672 * in routine cases. info->ether describes the multicast support.
673 */
674 return 0;
675}
676
677static const struct driver_info cdc_info = {
678 .description = "CDC Ethernet Device",
679 .flags = FLAG_ETHER,
680 // .check_connect = cdc_check_connect,
681 .bind = cdc_bind,
682 .unbind = cdc_unbind,
683 .status = cdc_status,
684};
685
686#endif /* CONFIG_USB_CDCETHER */
687
688
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690#ifdef CONFIG_USB_PL2301
691#define HAVE_HARDWARE
692
693/*-------------------------------------------------------------------------
694 *
695 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
696 *
697 * The protocol and handshaking used here should be bug-compatible
698 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
699 *
700 *-------------------------------------------------------------------------*/
701
702/*
703 * Bits 0-4 can be used for software handshaking; they're set from
704 * one end, cleared from the other, "read" with the interrupt byte.
705 */
706#define PL_S_EN (1<<7) /* (feature only) suspend enable */
707/* reserved bit -- rx ready (6) ? */
708#define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
709#define PL_RESET_OUT (1<<4) /* reset output pipe */
710#define PL_RESET_IN (1<<3) /* reset input pipe */
711#define PL_TX_C (1<<2) /* transmission complete */
712#define PL_TX_REQ (1<<1) /* transmission received */
713#define PL_PEER_E (1<<0) /* peer exists */
714
715static inline int
716pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
717{
718 return usb_control_msg (dev->udev,
719 usb_rcvctrlpipe (dev->udev, 0),
720 req,
721 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
722 val, index,
723 NULL, 0,
David Brownell47ee3052005-08-31 09:53:42 -0700724 USB_CTRL_GET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static inline int
728pl_clear_QuickLink_features (struct usbnet *dev, int val)
729{
730 return pl_vendor_req (dev, 1, (u8) val, 0);
731}
732
733static inline int
734pl_set_QuickLink_features (struct usbnet *dev, int val)
735{
736 return pl_vendor_req (dev, 3, (u8) val, 0);
737}
738
739/*-------------------------------------------------------------------------*/
740
741static int pl_reset (struct usbnet *dev)
742{
743 /* some units seem to need this reset, others reject it utterly.
744 * FIXME be more like "naplink" or windows drivers.
745 */
746 (void) pl_set_QuickLink_features (dev,
747 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
748 return 0;
749}
750
751static const struct driver_info prolific_info = {
752 .description = "Prolific PL-2301/PL-2302",
753 .flags = FLAG_NO_SETINT,
754 /* some PL-2302 versions seem to fail usb_set_interface() */
755 .reset = pl_reset,
756};
757
758#endif /* CONFIG_USB_PL2301 */
759
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#ifdef CONFIG_USB_ZAURUS
762#define HAVE_HARDWARE
763
764#include <linux/crc32.h>
765
766/*-------------------------------------------------------------------------
767 *
768 * Zaurus is also a SA-1110 based PDA, but one using a different driver
769 * (and framing) for its USB slave/gadget controller than the case above.
770 *
771 * For the current version of that driver, the main way that framing is
772 * nonstandard (also from perspective of the CDC ethernet model!) is a
773 * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
774 * haven't been fully worked around. Also, all Zaurii use the same
775 * default Ethernet address.
776 *
777 * PXA based models use the same framing, and also can't implement
778 * set_interface properly.
779 *
780 * All known Zaurii lie about their standards conformance. Most lie by
781 * saying they support CDC Ethernet. Some lie and say they support CDC
782 * MDLM (as if for access to cell phone modems). Someone, please beat
David Brownellf3fae6e2005-04-22 15:07:02 -0700783 * on Sharp (and other such vendors) for a while with a cluestick.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *
785 *-------------------------------------------------------------------------*/
786
787static struct sk_buff *
david-b@pacbell.net0d9899f2005-07-28 20:46:32 -0700788zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 int padlen;
791 struct sk_buff *skb2;
792
793 padlen = 2;
794 if (!skb_cloned (skb)) {
795 int tailroom = skb_tailroom (skb);
796 if ((padlen + 4) <= tailroom)
797 goto done;
798 }
799 skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
800 dev_kfree_skb_any (skb);
801 skb = skb2;
802 if (skb) {
803 u32 fcs;
804done:
805 fcs = crc32_le (~0, skb->data, skb->len);
806 fcs = ~fcs;
807
808 *skb_put (skb, 1) = fcs & 0xff;
809 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
810 *skb_put (skb, 1) = (fcs>>16) & 0xff;
811 *skb_put (skb, 1) = (fcs>>24) & 0xff;
812 }
813 return skb;
814}
815
David Brownellf29fc252005-08-31 09:52:31 -0700816static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf)
817{
818 /* Belcarra's funky framing has other options; mostly
819 * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
820 */
821 dev->net->hard_header_len += 6;
822 return generic_cdc_bind(dev, intf);
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825static const struct driver_info zaurus_sl5x00_info = {
826 .description = "Sharp Zaurus SL-5x00",
827 .flags = FLAG_FRAMING_Z,
828 .check_connect = always_connected,
David Brownellf29fc252005-08-31 09:52:31 -0700829 .bind = zaurus_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .unbind = cdc_unbind,
831 .tx_fixup = zaurus_tx_fixup,
832};
833#define ZAURUS_STRONGARM_INFO ((unsigned long)&zaurus_sl5x00_info)
834
835static const struct driver_info zaurus_pxa_info = {
836 .description = "Sharp Zaurus, PXA-2xx based",
837 .flags = FLAG_FRAMING_Z,
838 .check_connect = always_connected,
David Brownellf29fc252005-08-31 09:52:31 -0700839 .bind = zaurus_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 .unbind = cdc_unbind,
841 .tx_fixup = zaurus_tx_fixup,
842};
843#define ZAURUS_PXA_INFO ((unsigned long)&zaurus_pxa_info)
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static const struct driver_info olympus_mxl_info = {
846 .description = "Olympus R1000",
847 .flags = FLAG_FRAMING_Z,
848 .check_connect = always_connected,
David Brownellf29fc252005-08-31 09:52:31 -0700849 .bind = zaurus_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .unbind = cdc_unbind,
851 .tx_fixup = zaurus_tx_fixup,
852};
853#define OLYMPUS_MXL_INFO ((unsigned long)&olympus_mxl_info)
854
David Brownellf3fae6e2005-04-22 15:07:02 -0700855
856/* Some more recent products using Lineo/Belcarra code will wrongly claim
857 * CDC MDLM conformance. They aren't conformant: data endpoints live
858 * in the control interface, there's no data interface, and it's not used
859 * to talk to a cell phone radio. But at least we can detect these two
860 * pseudo-classes, rather than growing this product list with entries for
861 * each new nonconformant product (sigh).
862 */
863static const u8 safe_guid[16] = {
864 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
865 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
866};
867static const u8 blan_guid[16] = {
868 0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
869 0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
870};
871
872static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf)
873{
874 u8 *buf = intf->cur_altsetting->extra;
875 int len = intf->cur_altsetting->extralen;
876 struct usb_cdc_mdlm_desc *desc = NULL;
877 struct usb_cdc_mdlm_detail_desc *detail = NULL;
878
879 while (len > 3) {
880 if (buf [1] != USB_DT_CS_INTERFACE)
881 goto next_desc;
882
883 /* use bDescriptorSubType, and just verify that we get a
884 * "BLAN" (or "SAFE") descriptor.
885 */
886 switch (buf [2]) {
887 case USB_CDC_MDLM_TYPE:
888 if (desc) {
889 dev_dbg (&intf->dev, "extra MDLM\n");
890 goto bad_desc;
891 }
892 desc = (void *) buf;
893 if (desc->bLength != sizeof *desc) {
894 dev_dbg (&intf->dev, "MDLM len %u\n",
895 desc->bLength);
896 goto bad_desc;
897 }
898 /* expect bcdVersion 1.0, ignore */
899 if (memcmp(&desc->bGUID, blan_guid, 16)
David Brownellf4d340c2005-06-03 08:01:35 -0700900 && memcmp(&desc->bGUID, safe_guid, 16) ) {
David Brownellf3fae6e2005-04-22 15:07:02 -0700901 /* hey, this one might _really_ be MDLM! */
902 dev_dbg (&intf->dev, "MDLM guid\n");
903 goto bad_desc;
904 }
905 break;
906 case USB_CDC_MDLM_DETAIL_TYPE:
907 if (detail) {
908 dev_dbg (&intf->dev, "extra MDLM detail\n");
909 goto bad_desc;
910 }
911 detail = (void *) buf;
912 switch (detail->bGuidDescriptorType) {
913 case 0: /* "SAFE" */
914 if (detail->bLength != (sizeof *detail + 2))
915 goto bad_detail;
916 break;
917 case 1: /* "BLAN" */
918 if (detail->bLength != (sizeof *detail + 3))
919 goto bad_detail;
920 break;
921 default:
922 goto bad_detail;
923 }
924
925 /* assuming we either noticed BLAN already, or will
926 * find it soon, there are some data bytes here:
927 * - bmNetworkCapabilities (unused)
928 * - bmDataCapabilities (bits, see below)
929 * - bPad (ignored, for PADAFTER -- BLAN-only)
930 * bits are:
931 * - 0x01 -- Zaurus framing (add CRC)
David Brownell80615f82005-05-10 06:45:10 -0700932 * - 0x02 -- PADBEFORE (CRC includes some padding)
933 * - 0x04 -- PADAFTER (some padding after CRC)
David Brownellf3fae6e2005-04-22 15:07:02 -0700934 * - 0x08 -- "fermat" packet mangling (for hw bugs)
David Brownell80615f82005-05-10 06:45:10 -0700935 * the PADBEFORE appears not to matter; we interop
936 * with devices that use it and those that don't.
David Brownellf3fae6e2005-04-22 15:07:02 -0700937 */
David Brownell80615f82005-05-10 06:45:10 -0700938 if ((detail->bDetailData[1] & ~02) != 0x01) {
David Brownellf3fae6e2005-04-22 15:07:02 -0700939 /* bmDataCapabilites == 0 would be fine too,
940 * but framing is minidriver-coupled for now.
941 */
942bad_detail:
943 dev_dbg (&intf->dev,
944 "bad MDLM detail, %d %d %d\n",
945 detail->bLength,
946 detail->bDetailData[0],
947 detail->bDetailData[2]);
948 goto bad_desc;
949 }
950 break;
951 }
952next_desc:
953 len -= buf [0]; /* bLength */
954 buf += buf [0];
955 }
956
957 if (!desc || !detail) {
958 dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
959 desc ? "" : "func ",
960 detail ? "" : "detail ");
961 goto bad_desc;
962 }
963
964 /* There's probably a CDC Ethernet descriptor there, but we can't
965 * rely on the Ethernet address it provides since not all vendors
966 * bother to make it unique. Likewise there's no point in tracking
967 * of the CDC event notifications.
968 */
David Brownell2e55cc72005-08-31 09:53:10 -0700969 return usbnet_get_endpoints (dev, intf);
David Brownellf3fae6e2005-04-22 15:07:02 -0700970
971bad_desc:
972 dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n");
973 return -ENODEV;
974}
975
976static const struct driver_info bogus_mdlm_info = {
977 .description = "pseudo-MDLM (BLAN) device",
978 .flags = FLAG_FRAMING_Z,
979 .check_connect = always_connected,
980 .tx_fixup = zaurus_tx_fixup,
981 .bind = blan_mdlm_bind,
982};
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#else
985
986/* blacklist all those devices */
987#define ZAURUS_STRONGARM_INFO 0
988#define ZAURUS_PXA_INFO 0
989#define OLYMPUS_MXL_INFO 0
990
991#endif
992
993
994/*-------------------------------------------------------------------------
995 *
996 * Network Device Driver (peer link to "Host Device", from USB host)
997 *
998 *-------------------------------------------------------------------------*/
999
1000static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1001{
1002 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -07001003 int ll_mtu = new_mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David Brownellf29fc252005-08-31 09:52:31 -07001005 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -07001008 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -EDOM;
1010 net->mtu = new_mtu;
1011 return 0;
1012}
1013
1014/*-------------------------------------------------------------------------*/
1015
1016static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1017{
1018 struct usbnet *dev = netdev_priv(net);
1019 return &dev->stats;
1020}
1021
1022/*-------------------------------------------------------------------------*/
1023
1024/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1025 * completion callbacks. 2.5 should have fixed those bugs...
1026 */
1027
David S. Miller8728b832005-08-09 19:25:21 -07001028static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unsigned long flags;
1031
David S. Miller8728b832005-08-09 19:25:21 -07001032 spin_lock_irqsave(&list->lock, flags);
1033 __skb_unlink(skb, list);
1034 spin_unlock(&list->lock);
1035 spin_lock(&dev->done.lock);
1036 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -07001038 tasklet_schedule(&dev->bh);
1039 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/* some work can't be done in tasklets, so we use keventd
1043 *
1044 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1045 * but tasklet_schedule() doesn't. hope the failure is rare.
1046 */
David Brownell2e55cc72005-08-31 09:53:10 -07001047void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 set_bit (work, &dev->flags);
1050 if (!schedule_work (&dev->kevent))
1051 deverr (dev, "kevent %d may have been dropped", work);
1052 else
1053 devdbg (dev, "kevent %d scheduled", work);
1054}
David Brownell2e55cc72005-08-31 09:53:10 -07001055EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057/*-------------------------------------------------------------------------*/
1058
1059static void rx_complete (struct urb *urb, struct pt_regs *regs);
1060
david-b@pacbell.net0d9899f2005-07-28 20:46:32 -07001061static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct sk_buff *skb;
1064 struct skb_data *entry;
1065 int retval = 0;
1066 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -07001067 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
1070 if (netif_msg_rx_err (dev))
1071 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -07001072 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 usb_free_urb (urb);
1074 return;
1075 }
1076 skb_reserve (skb, NET_IP_ALIGN);
1077
1078 entry = (struct skb_data *) skb->cb;
1079 entry->urb = urb;
1080 entry->dev = dev;
1081 entry->state = rx_start;
1082 entry->length = 0;
1083
1084 usb_fill_bulk_urb (urb, dev->udev, dev->in,
1085 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 spin_lock_irqsave (&dev->rxq.lock, lockflags);
1088
1089 if (netif_running (dev->net)
1090 && netif_device_present (dev->net)
1091 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1092 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
1093 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001094 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 break;
1096 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -07001097 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 case -ENODEV:
1100 if (netif_msg_ifdown (dev))
1101 devdbg (dev, "device gone");
1102 netif_device_detach (dev->net);
1103 break;
1104 default:
1105 if (netif_msg_rx_err (dev))
1106 devdbg (dev, "rx submit, %d", retval);
1107 tasklet_schedule (&dev->bh);
1108 break;
1109 case 0:
1110 __skb_queue_tail (&dev->rxq, skb);
1111 }
1112 } else {
1113 if (netif_msg_ifdown (dev))
1114 devdbg (dev, "rx: stopped");
1115 retval = -ENOLINK;
1116 }
1117 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1118 if (retval) {
1119 dev_kfree_skb_any (skb);
1120 usb_free_urb (urb);
1121 }
1122}
1123
1124
1125/*-------------------------------------------------------------------------*/
1126
1127static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1128{
1129 if (dev->driver_info->rx_fixup
1130 && !dev->driver_info->rx_fixup (dev, skb))
1131 goto error;
1132 // else network stack removes extra byte if we forced a short packet
1133
1134 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -07001135 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 else {
1137 if (netif_msg_rx_err (dev))
1138 devdbg (dev, "drop");
1139error:
1140 dev->stats.rx_errors++;
1141 skb_queue_tail (&dev->done, skb);
1142 }
1143}
1144
1145/*-------------------------------------------------------------------------*/
1146
1147static void rx_complete (struct urb *urb, struct pt_regs *regs)
1148{
1149 struct sk_buff *skb = (struct sk_buff *) urb->context;
1150 struct skb_data *entry = (struct skb_data *) skb->cb;
1151 struct usbnet *dev = entry->dev;
1152 int urb_status = urb->status;
1153
1154 skb_put (skb, urb->actual_length);
1155 entry->state = rx_done;
1156 entry->urb = NULL;
1157
1158 switch (urb_status) {
1159 // success
1160 case 0:
David Brownellf29fc252005-08-31 09:52:31 -07001161 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 entry->state = rx_cleanup;
1163 dev->stats.rx_errors++;
1164 dev->stats.rx_length_errors++;
1165 if (netif_msg_rx_err (dev))
1166 devdbg (dev, "rx length %d", skb->len);
1167 }
1168 break;
1169
1170 // stalls need manual reset. this is rare ... except that
1171 // when going through USB 2.0 TTs, unplug appears this way.
1172 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1173 // storm, recovering as needed.
1174 case -EPIPE:
1175 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -07001176 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 // FALLTHROUGH
1178
1179 // software-driven interface shutdown
1180 case -ECONNRESET: // async unlink
1181 case -ESHUTDOWN: // hardware gone
1182 if (netif_msg_ifdown (dev))
1183 devdbg (dev, "rx shutdown, code %d", urb_status);
1184 goto block;
1185
1186 // we get controller i/o faults during khubd disconnect() delays.
1187 // throttle down resubmits, to avoid log floods; just temporarily,
1188 // so we still recover when the fault isn't a khubd delay.
1189 case -EPROTO: // ehci
1190 case -ETIMEDOUT: // ohci
1191 case -EILSEQ: // uhci
1192 dev->stats.rx_errors++;
1193 if (!timer_pending (&dev->delay)) {
1194 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1195 if (netif_msg_link (dev))
1196 devdbg (dev, "rx throttle %d", urb_status);
1197 }
1198block:
1199 entry->state = rx_cleanup;
1200 entry->urb = urb;
1201 urb = NULL;
1202 break;
1203
1204 // data overrun ... flush fifo?
1205 case -EOVERFLOW:
1206 dev->stats.rx_over_errors++;
1207 // FALLTHROUGH
1208
1209 default:
1210 entry->state = rx_cleanup;
1211 dev->stats.rx_errors++;
1212 if (netif_msg_rx_err (dev))
1213 devdbg (dev, "rx status %d", urb_status);
1214 break;
1215 }
1216
David S. Miller8728b832005-08-09 19:25:21 -07001217 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (urb) {
1220 if (netif_running (dev->net)
1221 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1222 rx_submit (dev, urb, GFP_ATOMIC);
1223 return;
1224 }
1225 usb_free_urb (urb);
1226 }
1227 if (netif_msg_rx_err (dev))
1228 devdbg (dev, "no read resubmitted");
1229}
1230
1231static void intr_complete (struct urb *urb, struct pt_regs *regs)
1232{
1233 struct usbnet *dev = urb->context;
1234 int status = urb->status;
1235
1236 switch (status) {
1237 /* success */
1238 case 0:
1239 dev->driver_info->status(dev, urb);
1240 break;
1241
1242 /* software-driven interface shutdown */
1243 case -ENOENT: // urb killed
1244 case -ESHUTDOWN: // hardware gone
1245 if (netif_msg_ifdown (dev))
1246 devdbg (dev, "intr shutdown, code %d", status);
1247 return;
1248
1249 /* NOTE: not throttling like RX/TX, since this endpoint
1250 * already polls infrequently
1251 */
1252 default:
1253 devdbg (dev, "intr status %d", status);
1254 break;
1255 }
1256
1257 if (!netif_running (dev->net))
1258 return;
1259
1260 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
1261 status = usb_submit_urb (urb, GFP_ATOMIC);
1262 if (status != 0 && netif_msg_timer (dev))
1263 deverr(dev, "intr resubmit --> %d", status);
1264}
1265
1266/*-------------------------------------------------------------------------*/
1267
1268// unlink pending rx/tx; completion handlers do all other cleanup
1269
1270static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1271{
1272 unsigned long flags;
1273 struct sk_buff *skb, *skbnext;
1274 int count = 0;
1275
1276 spin_lock_irqsave (&q->lock, flags);
1277 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1278 struct skb_data *entry;
1279 struct urb *urb;
1280 int retval;
1281
1282 entry = (struct skb_data *) skb->cb;
1283 urb = entry->urb;
1284 skbnext = skb->next;
1285
1286 // during some PM-driven resume scenarios,
1287 // these (async) unlinks complete immediately
1288 retval = usb_unlink_urb (urb);
1289 if (retval != -EINPROGRESS && retval != 0)
1290 devdbg (dev, "unlink urb err, %d", retval);
1291 else
1292 count++;
1293 }
1294 spin_unlock_irqrestore (&q->lock, flags);
1295 return count;
1296}
1297
1298
1299/*-------------------------------------------------------------------------*/
1300
1301// precondition: never called in_interrupt
1302
1303static int usbnet_stop (struct net_device *net)
1304{
1305 struct usbnet *dev = netdev_priv(net);
1306 int temp;
1307 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
1308 DECLARE_WAITQUEUE (wait, current);
1309
1310 netif_stop_queue (net);
1311
1312 if (netif_msg_ifdown (dev))
1313 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
1314 dev->stats.rx_packets, dev->stats.tx_packets,
1315 dev->stats.rx_errors, dev->stats.tx_errors
1316 );
1317
1318 // ensure there are no more active urbs
1319 add_wait_queue (&unlink_wakeup, &wait);
1320 dev->wait = &unlink_wakeup;
1321 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
1322
1323 // maybe wait for deletions to finish.
David S. Millerb03efcf2005-07-08 14:57:23 -07001324 while (!skb_queue_empty(&dev->rxq) &&
1325 !skb_queue_empty(&dev->txq) &&
1326 !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 msleep(UNLINK_TIMEOUT_MS);
1328 if (netif_msg_ifdown (dev))
1329 devdbg (dev, "waited for %d urb completions", temp);
1330 }
1331 dev->wait = NULL;
1332 remove_wait_queue (&unlink_wakeup, &wait);
1333
1334 usb_kill_urb(dev->interrupt);
1335
1336 /* deferred work (task, timer, softirq) must also stop.
1337 * can't flush_scheduled_work() until we drop rtnl (later),
1338 * else workers could deadlock; so make workers a NOP.
1339 */
1340 dev->flags = 0;
1341 del_timer_sync (&dev->delay);
1342 tasklet_kill (&dev->bh);
1343
1344 return 0;
1345}
1346
1347/*-------------------------------------------------------------------------*/
1348
1349// posts reads, and enables write queuing
1350
1351// precondition: never called in_interrupt
1352
1353static int usbnet_open (struct net_device *net)
1354{
1355 struct usbnet *dev = netdev_priv(net);
1356 int retval = 0;
1357 struct driver_info *info = dev->driver_info;
1358
1359 // put into "known safe" state
1360 if (info->reset && (retval = info->reset (dev)) < 0) {
1361 if (netif_msg_ifup (dev))
1362 devinfo (dev,
1363 "open reset fail (%d) usbnet usb-%s-%s, %s",
1364 retval,
1365 dev->udev->bus->bus_name, dev->udev->devpath,
1366 info->description);
1367 goto done;
1368 }
1369
1370 // insist peer be connected
1371 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
1372 if (netif_msg_ifup (dev))
1373 devdbg (dev, "can't open; %d", retval);
1374 goto done;
1375 }
1376
1377 /* start any status interrupt transfer */
1378 if (dev->interrupt) {
1379 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
1380 if (retval < 0) {
1381 if (netif_msg_ifup (dev))
1382 deverr (dev, "intr submit %d", retval);
1383 goto done;
1384 }
1385 }
1386
1387 netif_start_queue (net);
1388 if (netif_msg_ifup (dev)) {
1389 char *framing;
1390
1391 if (dev->driver_info->flags & FLAG_FRAMING_NC)
1392 framing = "NetChip";
1393 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
1394 framing = "GeneSys";
1395 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
1396 framing = "Zaurus";
1397 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
1398 framing = "RNDIS";
1399 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
1400 framing = "ASIX";
1401 else
1402 framing = "simple";
1403
1404 devinfo (dev, "open: enable queueing "
1405 "(rx %d, tx %d) mtu %d %s framing",
1406 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
1407 framing);
1408 }
1409
1410 // delay posting reads until we're fully open
1411 tasklet_schedule (&dev->bh);
1412done:
1413 return retval;
1414}
1415
1416/*-------------------------------------------------------------------------*/
1417
David Brownell2e55cc72005-08-31 09:53:10 -07001418/* ethtool methods; minidrivers may need to add some more, but
1419 * they'll probably want to use this base set.
1420 */
1421
1422void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct usbnet *dev = netdev_priv(net);
1425
David Brownell2e55cc72005-08-31 09:53:10 -07001426 /* REVISIT don't always return "usbnet" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 strncpy (info->driver, driver_name, sizeof info->driver);
1428 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
1429 strncpy (info->fw_version, dev->driver_info->description,
1430 sizeof info->fw_version);
1431 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1432}
David Brownell2e55cc72005-08-31 09:53:10 -07001433EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435static u32 usbnet_get_link (struct net_device *net)
1436{
1437 struct usbnet *dev = netdev_priv(net);
1438
David Brownellf29fc252005-08-31 09:52:31 -07001439 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (dev->driver_info->check_connect)
1441 return dev->driver_info->check_connect (dev) == 0;
1442
David Brownellf29fc252005-08-31 09:52:31 -07001443 /* Otherwise, say we're up (to avoid breaking scripts) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 1;
1445}
1446
David Brownell2e55cc72005-08-31 09:53:10 -07001447u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct usbnet *dev = netdev_priv(net);
1450
1451 return dev->msg_enable;
1452}
David Brownell2e55cc72005-08-31 09:53:10 -07001453EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
David Brownell2e55cc72005-08-31 09:53:10 -07001455void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct usbnet *dev = netdev_priv(net);
1458
1459 dev->msg_enable = level;
1460}
David Brownell2e55cc72005-08-31 09:53:10 -07001461EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
David Brownell2e55cc72005-08-31 09:53:10 -07001463/* drivers may override default ethtool_ops in their bind() routine */
1464static struct ethtool_ops usbnet_ethtool_ops = {
1465 .get_drvinfo = usbnet_get_drvinfo,
1466 .get_link = usbnet_get_link,
1467 .get_msglevel = usbnet_get_msglevel,
1468 .set_msglevel = usbnet_set_msglevel,
1469};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471/*-------------------------------------------------------------------------*/
1472
1473/* work that cannot be done in interrupt context uses keventd.
1474 *
1475 * NOTE: with 2.5 we could do more of this using completion callbacks,
1476 * especially now that control transfers can be queued.
1477 */
1478static void
1479kevent (void *data)
1480{
1481 struct usbnet *dev = data;
1482 int status;
1483
1484 /* usb_clear_halt() needs a thread context */
1485 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1486 unlink_urbs (dev, &dev->txq);
1487 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -07001488 if (status < 0
1489 && status != -EPIPE
1490 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (netif_msg_tx_err (dev))
1492 deverr (dev, "can't clear tx halt, status %d",
1493 status);
1494 } else {
1495 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -07001496 if (status != -ESHUTDOWN)
1497 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 }
1500 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1501 unlink_urbs (dev, &dev->rxq);
1502 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -07001503 if (status < 0
1504 && status != -EPIPE
1505 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (netif_msg_rx_err (dev))
1507 deverr (dev, "can't clear rx halt, status %d",
1508 status);
1509 } else {
1510 clear_bit (EVENT_RX_HALT, &dev->flags);
1511 tasklet_schedule (&dev->bh);
1512 }
1513 }
1514
1515 /* tasklet could resubmit itself forever if memory is tight */
1516 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1517 struct urb *urb = NULL;
1518
1519 if (netif_running (dev->net))
1520 urb = usb_alloc_urb (0, GFP_KERNEL);
1521 else
1522 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1523 if (urb != NULL) {
1524 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1525 rx_submit (dev, urb, GFP_KERNEL);
1526 tasklet_schedule (&dev->bh);
1527 }
1528 }
1529
David Hollis7ea13c92005-04-22 15:07:02 -07001530 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1531 struct driver_info *info = dev->driver_info;
1532 int retval = 0;
1533
1534 clear_bit (EVENT_LINK_RESET, &dev->flags);
1535 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1536 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
1537 retval,
1538 dev->udev->bus->bus_name, dev->udev->devpath,
1539 info->description);
1540 }
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (dev->flags)
1544 devdbg (dev, "kevent done, flags = 0x%lx",
1545 dev->flags);
1546}
1547
1548/*-------------------------------------------------------------------------*/
1549
1550static void tx_complete (struct urb *urb, struct pt_regs *regs)
1551{
1552 struct sk_buff *skb = (struct sk_buff *) urb->context;
1553 struct skb_data *entry = (struct skb_data *) skb->cb;
1554 struct usbnet *dev = entry->dev;
1555
1556 if (urb->status == 0) {
1557 dev->stats.tx_packets++;
1558 dev->stats.tx_bytes += entry->length;
1559 } else {
1560 dev->stats.tx_errors++;
1561
1562 switch (urb->status) {
1563 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001564 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566
1567 /* software-driven interface shutdown */
1568 case -ECONNRESET: // async unlink
1569 case -ESHUTDOWN: // hardware gone
1570 break;
1571
1572 // like rx, tx gets controller i/o faults during khubd delays
1573 // and so it uses the same throttling mechanism.
1574 case -EPROTO: // ehci
1575 case -ETIMEDOUT: // ohci
1576 case -EILSEQ: // uhci
1577 if (!timer_pending (&dev->delay)) {
1578 mod_timer (&dev->delay,
1579 jiffies + THROTTLE_JIFFIES);
1580 if (netif_msg_link (dev))
1581 devdbg (dev, "tx throttle %d",
1582 urb->status);
1583 }
1584 netif_stop_queue (dev->net);
1585 break;
1586 default:
1587 if (netif_msg_tx_err (dev))
1588 devdbg (dev, "tx err %d", entry->urb->status);
1589 break;
1590 }
1591 }
1592
1593 urb->dev = NULL;
1594 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -07001595 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598/*-------------------------------------------------------------------------*/
1599
1600static void usbnet_tx_timeout (struct net_device *net)
1601{
1602 struct usbnet *dev = netdev_priv(net);
1603
1604 unlink_urbs (dev, &dev->txq);
1605 tasklet_schedule (&dev->bh);
1606
1607 // FIXME: device recovery -- reset?
1608}
1609
1610/*-------------------------------------------------------------------------*/
1611
1612static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1613{
1614 struct usbnet *dev = netdev_priv(net);
1615 int length;
1616 int retval = NET_XMIT_SUCCESS;
1617 struct urb *urb = NULL;
1618 struct skb_data *entry;
1619 struct driver_info *info = dev->driver_info;
1620 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 // some devices want funky USB-level framing, for
1623 // win32 driver (usually) and/or hardware quirks
1624 if (info->tx_fixup) {
1625 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1626 if (!skb) {
1627 if (netif_msg_tx_err (dev))
1628 devdbg (dev, "can't tx_fixup skb");
1629 goto drop;
1630 }
1631 }
1632 length = skb->len;
1633
1634 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1635 if (netif_msg_tx_err (dev))
1636 devdbg (dev, "no urb");
1637 goto drop;
1638 }
1639
1640 entry = (struct skb_data *) skb->cb;
1641 entry->urb = urb;
1642 entry->dev = dev;
1643 entry->state = tx_start;
1644 entry->length = length;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1647 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* don't assume the hardware handles USB_ZERO_PACKET
1650 * NOTE: strictly conforming cdc-ether devices should expect
1651 * the ZLP here, but ignore the one-byte packet.
1652 *
1653 * FIXME zero that byte, if it doesn't require a new skb.
1654 */
1655 if ((length % dev->maxpacket) == 0)
1656 urb->transfer_buffer_length++;
1657
1658 spin_lock_irqsave (&dev->txq.lock, flags);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1661 case -EPIPE:
1662 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001663 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 default:
1666 if (netif_msg_tx_err (dev))
1667 devdbg (dev, "tx: submit urb err %d", retval);
1668 break;
1669 case 0:
1670 net->trans_start = jiffies;
1671 __skb_queue_tail (&dev->txq, skb);
1672 if (dev->txq.qlen >= TX_QLEN (dev))
1673 netif_stop_queue (net);
1674 }
1675 spin_unlock_irqrestore (&dev->txq.lock, flags);
1676
1677 if (retval) {
1678 if (netif_msg_tx_err (dev))
1679 devdbg (dev, "drop, code %d", retval);
1680drop:
1681 retval = NET_XMIT_SUCCESS;
1682 dev->stats.tx_dropped++;
1683 if (skb)
1684 dev_kfree_skb_any (skb);
1685 usb_free_urb (urb);
1686 } else if (netif_msg_tx_queued (dev)) {
1687 devdbg (dev, "> tx, len %d, type 0x%x",
1688 length, skb->protocol);
1689 }
1690 return retval;
1691}
1692
1693
1694/*-------------------------------------------------------------------------*/
1695
1696// tasklet (work deferred from completions, in_irq) or timer
1697
1698static void usbnet_bh (unsigned long param)
1699{
1700 struct usbnet *dev = (struct usbnet *) param;
1701 struct sk_buff *skb;
1702 struct skb_data *entry;
1703
1704 while ((skb = skb_dequeue (&dev->done))) {
1705 entry = (struct skb_data *) skb->cb;
1706 switch (entry->state) {
1707 case rx_done:
1708 entry->state = rx_cleanup;
1709 rx_process (dev, skb);
1710 continue;
1711 case tx_done:
1712 case rx_cleanup:
1713 usb_free_urb (entry->urb);
1714 dev_kfree_skb (skb);
1715 continue;
1716 default:
1717 devdbg (dev, "bogus skb state %d", entry->state);
1718 }
1719 }
1720
1721 // waiting for all pending urbs to complete?
1722 if (dev->wait) {
1723 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1724 wake_up (dev->wait);
1725 }
1726
1727 // or are we maybe short a few urbs?
1728 } else if (netif_running (dev->net)
1729 && netif_device_present (dev->net)
1730 && !timer_pending (&dev->delay)
1731 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1732 int temp = dev->rxq.qlen;
1733 int qlen = RX_QLEN (dev);
1734
1735 if (temp < qlen) {
1736 struct urb *urb;
1737 int i;
1738
1739 // don't refill the queue all at once
1740 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1741 urb = usb_alloc_urb (0, GFP_ATOMIC);
1742 if (urb != NULL)
1743 rx_submit (dev, urb, GFP_ATOMIC);
1744 }
1745 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1746 devdbg (dev, "rxqlen %d --> %d",
1747 temp, dev->rxq.qlen);
1748 if (dev->rxq.qlen < qlen)
1749 tasklet_schedule (&dev->bh);
1750 }
1751 if (dev->txq.qlen < TX_QLEN (dev))
1752 netif_wake_queue (dev->net);
1753 }
1754}
1755
1756
1757
1758/*-------------------------------------------------------------------------
1759 *
1760 * USB Device Driver support
1761 *
1762 *-------------------------------------------------------------------------*/
1763
1764// precondition: never called in_interrupt
1765
David Brownell38bde1d2005-08-31 09:52:45 -07001766void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct usbnet *dev;
1769 struct usb_device *xdev;
1770 struct net_device *net;
1771
1772 dev = usb_get_intfdata(intf);
1773 usb_set_intfdata(intf, NULL);
1774 if (!dev)
1775 return;
1776
1777 xdev = interface_to_usbdev (intf);
1778
1779 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001780 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1781 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 xdev->bus->bus_name, xdev->devpath,
1783 dev->driver_info->description);
1784
1785 net = dev->net;
1786 unregister_netdev (net);
1787
1788 /* we don't hold rtnl here ... */
1789 flush_scheduled_work ();
1790
1791 if (dev->driver_info->unbind)
1792 dev->driver_info->unbind (dev, intf);
1793
1794 free_netdev(net);
1795 usb_put_dev (xdev);
1796}
David Brownell38bde1d2005-08-31 09:52:45 -07001797EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799
1800/*-------------------------------------------------------------------------*/
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802// precondition: never called in_interrupt
1803
David Brownell38bde1d2005-08-31 09:52:45 -07001804int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1806{
1807 struct usbnet *dev;
1808 struct net_device *net;
1809 struct usb_host_interface *interface;
1810 struct driver_info *info;
1811 struct usb_device *xdev;
1812 int status;
1813
1814 info = (struct driver_info *) prod->driver_info;
1815 if (!info) {
1816 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1817 return -ENODEV;
1818 }
1819 xdev = interface_to_usbdev (udev);
1820 interface = udev->cur_altsetting;
1821
1822 usb_get_dev (xdev);
1823
1824 status = -ENOMEM;
1825
1826 // set up our own records
1827 net = alloc_etherdev(sizeof(*dev));
1828 if (!net) {
1829 dbg ("can't kmalloc dev");
1830 goto out;
1831 }
1832
1833 dev = netdev_priv(net);
1834 dev->udev = xdev;
1835 dev->driver_info = info;
1836 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1837 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1838 skb_queue_head_init (&dev->rxq);
1839 skb_queue_head_init (&dev->txq);
1840 skb_queue_head_init (&dev->done);
1841 dev->bh.func = usbnet_bh;
1842 dev->bh.data = (unsigned long) dev;
1843 INIT_WORK (&dev->kevent, kevent, dev);
1844 dev->delay.function = usbnet_bh;
1845 dev->delay.data = (unsigned long) dev;
1846 init_timer (&dev->delay);
1847
1848 SET_MODULE_OWNER (net);
1849 dev->net = net;
1850 strcpy (net->name, "usb%d");
1851 memcpy (net->dev_addr, node_id, sizeof node_id);
1852
David Brownell2e55cc72005-08-31 09:53:10 -07001853 /* rx and tx sides can use different message sizes;
1854 * bind() should set rx_urb_size in that case.
1855 */
1856 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#if 0
1858// dma_supported() is deeply broken on almost all architectures
1859 // possible with some EHCI controllers
1860 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1861 net->features |= NETIF_F_HIGHDMA;
1862#endif
1863
1864 net->change_mtu = usbnet_change_mtu;
1865 net->get_stats = usbnet_get_stats;
1866 net->hard_start_xmit = usbnet_start_xmit;
1867 net->open = usbnet_open;
1868 net->stop = usbnet_stop;
1869 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1870 net->tx_timeout = usbnet_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 net->ethtool_ops = &usbnet_ethtool_ops;
1872
1873 // allow device-specific bind/init procedures
1874 // NOTE net->name still not usable ...
1875 if (info->bind) {
1876 status = info->bind (dev, udev);
1877 // heuristic: "usb%d" for links we know are two-host,
1878 // else "eth%d" when there's reasonable doubt. userspace
1879 // can rename the link if it knows better.
1880 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1881 && (net->dev_addr [0] & 0x02) == 0)
1882 strcpy (net->name, "eth%d");
David Brownellf29fc252005-08-31 09:52:31 -07001883
1884 /* maybe the remote can't receive an Ethernet MTU */
1885 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1886 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001887 } else if (!info->in || !info->out)
1888 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 else {
1890 dev->in = usb_rcvbulkpipe (xdev, info->in);
1891 dev->out = usb_sndbulkpipe (xdev, info->out);
1892 if (!(info->flags & FLAG_NO_SETINT))
1893 status = usb_set_interface (xdev,
1894 interface->desc.bInterfaceNumber,
1895 interface->desc.bAlternateSetting);
1896 else
1897 status = 0;
1898
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (status == 0 && dev->status)
1901 status = init_status (dev, udev);
1902 if (status < 0)
1903 goto out1;
1904
David Brownell2e55cc72005-08-31 09:53:10 -07001905 if (!dev->rx_urb_size)
1906 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1908
1909 SET_NETDEV_DEV(net, &udev->dev);
1910 status = register_netdev (net);
1911 if (status)
1912 goto out3;
1913 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001914 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 "%02x:%02x:%02x:%02x:%02x:%02x",
David Brownell38bde1d2005-08-31 09:52:45 -07001916 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 xdev->bus->bus_name, xdev->devpath,
1918 dev->driver_info->description,
1919 net->dev_addr [0], net->dev_addr [1],
1920 net->dev_addr [2], net->dev_addr [3],
1921 net->dev_addr [4], net->dev_addr [5]);
1922
1923 // ok, it's ready to go.
1924 usb_set_intfdata (udev, dev);
1925
1926 // start as if the link is up
1927 netif_device_attach (net);
1928
1929 return 0;
1930
1931out3:
1932 if (info->unbind)
1933 info->unbind (dev, udev);
1934out1:
1935 free_netdev(net);
1936out:
1937 usb_put_dev(xdev);
1938 return status;
1939}
David Brownell38bde1d2005-08-31 09:52:45 -07001940EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942/*-------------------------------------------------------------------------*/
1943
David Brownell38bde1d2005-08-31 09:52:45 -07001944/* FIXME these suspend/resume methods assume non-CDC style
1945 * devices, with only one interface.
1946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
David Brownell38bde1d2005-08-31 09:52:45 -07001948int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct usbnet *dev = usb_get_intfdata(intf);
1951
David Brownell27d72e82005-04-18 17:39:22 -07001952 /* accelerate emptying of the rx and queues, to avoid
1953 * having everything error out.
1954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 netif_device_detach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001956 (void) unlink_urbs (dev, &dev->rxq);
1957 (void) unlink_urbs (dev, &dev->txq);
1958 intf->dev.power.power_state = PMSG_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return 0;
1960}
David Brownell38bde1d2005-08-31 09:52:45 -07001961EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
David Brownell38bde1d2005-08-31 09:52:45 -07001963int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct usbnet *dev = usb_get_intfdata(intf);
1966
David Brownell27d72e82005-04-18 17:39:22 -07001967 intf->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 netif_device_attach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001969 tasklet_schedule (&dev->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return 0;
1971}
David Brownell38bde1d2005-08-31 09:52:45 -07001972EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975/*-------------------------------------------------------------------------*/
1976
1977#ifndef HAVE_HARDWARE
1978#error You need to configure some hardware for this driver
1979#endif
1980
1981/*
1982 * chip vendor names won't normally be on the cables, and
1983 * may not be on the device.
1984 */
1985
1986static const struct usb_device_id products [] = {
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988#ifdef CONFIG_USB_PL2301
1989{
1990 USB_DEVICE (0x067b, 0x0000), // PL-2301
1991 .driver_info = (unsigned long) &prolific_info,
1992}, {
1993 USB_DEVICE (0x067b, 0x0001), // PL-2302
1994 .driver_info = (unsigned long) &prolific_info,
1995},
1996#endif
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998#ifdef CONFIG_USB_RNDIS
1999{
2000 /* RNDIS is MSFT's un-official variant of CDC ACM */
2001 USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2002 .driver_info = (unsigned long) &rndis_info,
2003},
2004#endif
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006#if defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
2007/*
2008 * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2009 * Same idea as above, but different framing.
2010 *
2011 * PXA-2xx based models are also lying-about-cdc.
2012 * Some models don't even tell the same lies ...
2013 *
2014 * NOTE: OpenZaurus versions with 2.6 kernels won't use these entries,
2015 * unlike the older ones with 2.4 "embedix" kernels.
2016 *
2017 * NOTE: These entries do double-duty, serving as blacklist entries
2018 * whenever Zaurus support isn't enabled, but CDC Ethernet is.
2019 */
2020#define ZAURUS_MASTER_INTERFACE \
2021 .bInterfaceClass = USB_CLASS_COMM, \
2022 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
2023 .bInterfaceProtocol = USB_CDC_PROTO_NONE
2024{
2025 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2026 | USB_DEVICE_ID_MATCH_DEVICE,
2027 .idVendor = 0x04DD,
2028 .idProduct = 0x8004,
2029 ZAURUS_MASTER_INTERFACE,
2030 .driver_info = ZAURUS_STRONGARM_INFO,
2031}, {
2032 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2033 | USB_DEVICE_ID_MATCH_DEVICE,
2034 .idVendor = 0x04DD,
2035 .idProduct = 0x8005, /* A-300 */
2036 ZAURUS_MASTER_INTERFACE,
2037 .driver_info = ZAURUS_PXA_INFO,
2038}, {
2039 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2040 | USB_DEVICE_ID_MATCH_DEVICE,
2041 .idVendor = 0x04DD,
2042 .idProduct = 0x8006, /* B-500/SL-5600 */
2043 ZAURUS_MASTER_INTERFACE,
2044 .driver_info = ZAURUS_PXA_INFO,
2045}, {
2046 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2047 | USB_DEVICE_ID_MATCH_DEVICE,
2048 .idVendor = 0x04DD,
2049 .idProduct = 0x8007, /* C-700 */
2050 ZAURUS_MASTER_INTERFACE,
2051 .driver_info = ZAURUS_PXA_INFO,
2052}, {
2053 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2054 | USB_DEVICE_ID_MATCH_DEVICE,
2055 .idVendor = 0x04DD,
2056 .idProduct = 0x9031, /* C-750 C-760 */
2057 ZAURUS_MASTER_INTERFACE,
2058 .driver_info = ZAURUS_PXA_INFO,
2059}, {
2060 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2061 | USB_DEVICE_ID_MATCH_DEVICE,
2062 .idVendor = 0x04DD,
2063 .idProduct = 0x9032, /* SL-6000 */
2064 ZAURUS_MASTER_INTERFACE,
2065 .driver_info = ZAURUS_PXA_INFO,
2066}, {
2067 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2068 | USB_DEVICE_ID_MATCH_DEVICE,
2069 .idVendor = 0x04DD,
2070 /* reported with some C860 units */
2071 .idProduct = 0x9050, /* C-860 */
2072 ZAURUS_MASTER_INTERFACE,
2073 .driver_info = ZAURUS_PXA_INFO,
David Brownella2fe2012005-04-18 17:39:29 -07002074},
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076#ifdef CONFIG_USB_ZAURUS
David Brownellf3fae6e2005-04-22 15:07:02 -07002077 /* At least some (reports vary) PXA units have very different lies
2078 * about their standards support: they claim to be cell phones with
2079 * direct access to their radios. (They don't conform to CDC MDLM.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
David Brownella2fe2012005-04-18 17:39:29 -07002081{
David Brownellf3fae6e2005-04-22 15:07:02 -07002082 USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
2083 USB_CDC_PROTO_NONE),
2084 .driver_info = (unsigned long) &bogus_mdlm_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085},
David Brownella2fe2012005-04-18 17:39:29 -07002086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088/* Olympus has some models with a Zaurus-compatible option.
2089 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
2090 */
2091{
2092 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2093 | USB_DEVICE_ID_MATCH_DEVICE,
2094 .idVendor = 0x07B4,
2095 .idProduct = 0x0F02, /* R-1000 */
2096 ZAURUS_MASTER_INTERFACE,
2097 .driver_info = OLYMPUS_MXL_INFO,
2098},
2099#endif
2100
2101#ifdef CONFIG_USB_CDCETHER
2102{
2103 /* CDC Ether uses two interfaces, not necessarily consecutive.
2104 * We match the main interface, ignoring the optional device
2105 * class so we could handle devices that aren't exclusively
2106 * CDC ether.
2107 *
2108 * NOTE: this match must come AFTER entries working around
2109 * bugs/quirks in a given product (like Zaurus, above).
2110 */
2111 USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
2112 USB_CDC_PROTO_NONE),
2113 .driver_info = (unsigned long) &cdc_info,
2114},
2115#endif
2116
2117 { }, // END
2118};
2119MODULE_DEVICE_TABLE (usb, products);
2120
2121static struct usb_driver usbnet_driver = {
2122 .owner = THIS_MODULE,
2123 .name = driver_name,
2124 .id_table = products,
2125 .probe = usbnet_probe,
2126 .disconnect = usbnet_disconnect,
2127 .suspend = usbnet_suspend,
2128 .resume = usbnet_resume,
2129};
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*-------------------------------------------------------------------------*/
2132
David Brownellf29fc252005-08-31 09:52:31 -07002133static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 // compiler should optimize these out
2136 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
2137 < sizeof (struct skb_data));
2138#ifdef CONFIG_USB_CDCETHER
2139 BUG_ON ((sizeof (((struct usbnet *)0)->data)
2140 < sizeof (struct cdc_state)));
2141#endif
2142
2143 random_ether_addr(node_id);
2144
2145 return usb_register(&usbnet_driver);
2146}
David Brownellf29fc252005-08-31 09:52:31 -07002147module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
David Brownellf29fc252005-08-31 09:52:31 -07002149static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
David Brownellf29fc252005-08-31 09:52:31 -07002151 usb_deregister(&usbnet_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
David Brownellf29fc252005-08-31 09:52:31 -07002153module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David Brownellf29fc252005-08-31 09:52:31 -07002155MODULE_AUTHOR("David Brownell");
2156MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
2157MODULE_LICENSE("GPL");