blob: 7703725327d2560388fbd8b11751268493a33de9 [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) 2003-2005 David Hollis <dhollis@davehollis.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
10 *
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.
15 *
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
19 */
20
21/*
22 * This is a generic "USB networking" framework that works with several
23 * kinds of full and high speed networking devices:
24 *
25 * + USB host-to-host "network cables", used for IP-over-USB links.
26 * These are often used for Laplink style connectivity products.
27 * - AnchorChip 2720
28 * - Belkin, eTEK (interops with Win32 drivers)
29 * - GeneSys GL620USB-A
30 * - NetChip 1080 (interoperates with NetChip Win32 drivers)
31 * - Prolific PL-2301/2302 (replaces "plusb" driver)
32 * - KC Technology KC2190
33 *
34 * + Smart USB devices can support such links directly, using Internet
35 * standard protocols instead of proprietary host-to-device links.
36 * - Linux PDAs like iPaq, Yopy, and Zaurus
37 * - The BLOB boot loader (for diskless booting)
38 * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
39 * - Devices using EPSON's sample USB firmware
40 * - CDC-Ethernet class devices, such as many cable modems
41 *
42 * + Adapters to networks such as Ethernet.
43 * - AX8817X based USB 2.0 products
44 *
45 * Links to these devices can be bridged using Linux Ethernet bridging.
46 * With minor exceptions, these all use similar USB framing for network
47 * traffic, but need different protocols for control traffic.
48 *
49 * USB devices can implement their side of this protocol at the cost
50 * of two bulk endpoints; it's not restricted to "cable" applications.
51 * See the SA1110, Zaurus, or EPSON device/client support in this driver;
52 * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
53 * "g_ether" (in the Linux "gadget" framework) implement that behavior
54 * within devices.
55 *
56 *
57 * CHANGELOG:
58 *
59 * 13-sep-2000 experimental, new
60 * 10-oct-2000 usb_device_id table created.
61 * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
62 * 01-nov-2000 usb_device_id table and probing api update by
63 * Adam J. Richter <adam@yggdrasil.com>.
64 * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
65 * and prolific support, isolate net1080-specific bits, cleanup.
66 * fix unlink_urbs oops in D3 PM resume code path.
67 *
68 * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
69 * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
70 * AnchorChips 2720 support (from spec) for testing;
71 * fix bit-ordering problem with ethernet multicast addr
72 * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
73 * updates. Oleg Drokin (green@iXcelerator.com)
74 * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
75 * expecting cleared skb->cb and framing change to match latest
76 * handhelds.org version (Oleg). Enable device IDs from the
77 * Win32 Belkin driver; other cleanups (db).
78 * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
79 * cleanups for problems not yet seen in the field. (db)
80 * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
81 * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
82 * rx unlinks somehow weren't async; minor cleanup.
83 * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
84 * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
85 * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
86 * tie mostly to (sub)driver info. Workaround some PL-2302
87 * chips that seem to reject SET_INTERFACE requests.
88 *
89 * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
90 * Level of diagnostics is more configurable; they use device
91 * location (usb_device->devpath) instead of address (2.5).
92 * For tx_fixup, memflags can't be NOIO.
93 * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
94 * for USB 2.0 TTs) and memory shortages (potential) too. (db)
95 * Use "locally assigned" IEEE802 address space. (Brad Hards)
96 * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
97 * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
98 * cleanups and stubbed PXA-250 support (db), fix for framing
99 * issues on Z, net1080, and gl620a (Toby Milne)
100 *
101 * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
102 * vs pxa25x, and CDC Ethernet. Throttle down log floods on
103 * disconnect; other cleanups. (db) Flush net1080 fifos
104 * after several sequential framing errors. (Johannes Erdfelt)
105 * 22-aug-2003 AX8817X support (Dave Hollis).
106 * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
107 * (Neil Bortnak)
108 * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
109 *
110 * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
111 *-------------------------------------------------------------------------*/
112
113// #define DEBUG // error path messages, extra info
114// #define VERBOSE // more; success messages
115
116#include <linux/config.h>
117#ifdef CONFIG_USB_DEBUG
118# define DEBUG
119#endif
120#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sched.h>
122#include <linux/init.h>
123#include <linux/netdevice.h>
124#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#include <linux/ethtool.h>
126#include <linux/workqueue.h>
127#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#include <linux/usb.h>
David Brownellf29fc252005-08-31 09:52:31 -0700129
130#include "usbnet.h"
131
132#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134
135/*-------------------------------------------------------------------------*/
136
137/*
138 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
139 * Several dozen bytes of IPv4 data can fit in two such transactions.
140 * One maximum size Ethernet packet takes twenty four of them.
141 * For high speed, each frame comfortably fits almost 36 max size
142 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -0700143 *
144 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
145 * let the USB host controller be busy for 5msec or more before an irq
146 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
148#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
149#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151// reawaken network queue this soon after stopping; else watchdog barks
152#define TX_TIMEOUT_JIFFIES (5*HZ)
153
154// throttle rx/tx briefly after some faults, so khubd might disconnect()
155// us (it polls at HZ/4 usually) before we report too many false errors.
156#define THROTTLE_JIFFIES (HZ/8)
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158// between wakeups
159#define UNLINK_TIMEOUT_MS 3
160
161/*-------------------------------------------------------------------------*/
162
163// randomly generated ethernet address
164static u8 node_id [ETH_ALEN];
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static const char driver_name [] = "usbnet";
167
168/* use ethtool to change the level for any given device */
169static int msg_level = -1;
170module_param (msg_level, int, 0);
171MODULE_PARM_DESC (msg_level, "Override default message level");
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*-------------------------------------------------------------------------*/
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -0700176int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int tmp;
179 struct usb_host_interface *alt = NULL;
180 struct usb_host_endpoint *in = NULL, *out = NULL;
181 struct usb_host_endpoint *status = NULL;
182
183 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
184 unsigned ep;
185
186 in = out = status = NULL;
187 alt = intf->altsetting + tmp;
188
189 /* take the first altsetting with in-bulk + out-bulk;
190 * remember any status endpoint, just in case;
191 * ignore other endpoints and altsetttings.
192 */
193 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
194 struct usb_host_endpoint *e;
195 int intr = 0;
196
197 e = alt->endpoint + ep;
198 switch (e->desc.bmAttributes) {
199 case USB_ENDPOINT_XFER_INT:
200 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
201 continue;
202 intr = 1;
203 /* FALLTHROUGH */
204 case USB_ENDPOINT_XFER_BULK:
205 break;
206 default:
207 continue;
208 }
209 if (e->desc.bEndpointAddress & USB_DIR_IN) {
210 if (!intr && !in)
211 in = e;
212 else if (intr && !status)
213 status = e;
214 } else {
215 if (!out)
216 out = e;
217 }
218 }
219 if (in && out)
220 break;
221 }
222 if (!alt || !in || !out)
223 return -EINVAL;
224
225 if (alt->desc.bAlternateSetting != 0
226 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
227 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
228 alt->desc.bAlternateSetting);
229 if (tmp < 0)
230 return tmp;
231 }
232
233 dev->in = usb_rcvbulkpipe (dev->udev,
234 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
235 dev->out = usb_sndbulkpipe (dev->udev,
236 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
237 dev->status = status;
238 return 0;
239}
David Brownell2e55cc72005-08-31 09:53:10 -0700240EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242static void intr_complete (struct urb *urb, struct pt_regs *regs);
243
244static int init_status (struct usbnet *dev, struct usb_interface *intf)
245{
246 char *buf = NULL;
247 unsigned pipe = 0;
248 unsigned maxp;
249 unsigned period;
250
251 if (!dev->driver_info->status)
252 return 0;
253
254 pipe = usb_rcvintpipe (dev->udev,
255 dev->status->desc.bEndpointAddress
256 & USB_ENDPOINT_NUMBER_MASK);
257 maxp = usb_maxpacket (dev->udev, pipe, 0);
258
259 /* avoid 1 msec chatter: min 8 msec poll rate */
260 period = max ((int) dev->status->desc.bInterval,
261 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
262
263 buf = kmalloc (maxp, SLAB_KERNEL);
264 if (buf) {
265 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
266 if (!dev->interrupt) {
267 kfree (buf);
268 return -ENOMEM;
269 } else {
270 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
271 buf, maxp, intr_complete, dev, period);
272 dev_dbg(&intf->dev,
273 "status ep%din, %d bytes period %d\n",
274 usb_pipeendpoint(pipe), maxp, period);
275 }
276 }
277 return 0;
278}
279
David Brownell2e55cc72005-08-31 09:53:10 -0700280/* Passes this packet up the stack, updating its accounting.
281 * Some link protocols batch packets, so their rx_fixup paths
282 * can return clones as well as just modify the original skb.
283 */
284void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int status;
287
288 skb->dev = dev->net;
289 skb->protocol = eth_type_trans (skb, dev->net);
290 dev->stats.rx_packets++;
291 dev->stats.rx_bytes += skb->len;
292
293 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700294 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 skb->len + sizeof (struct ethhdr), skb->protocol);
296 memset (skb->cb, 0, sizeof (struct skb_data));
297 status = netif_rx (skb);
298 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
299 devdbg (dev, "netif_rx status %d", status);
300}
David Brownell2e55cc72005-08-31 09:53:10 -0700301EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifdef CONFIG_USB_PL2301
305#define HAVE_HARDWARE
306
307/*-------------------------------------------------------------------------
308 *
309 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
310 *
311 * The protocol and handshaking used here should be bug-compatible
312 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
313 *
314 *-------------------------------------------------------------------------*/
315
316/*
317 * Bits 0-4 can be used for software handshaking; they're set from
318 * one end, cleared from the other, "read" with the interrupt byte.
319 */
320#define PL_S_EN (1<<7) /* (feature only) suspend enable */
321/* reserved bit -- rx ready (6) ? */
322#define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
323#define PL_RESET_OUT (1<<4) /* reset output pipe */
324#define PL_RESET_IN (1<<3) /* reset input pipe */
325#define PL_TX_C (1<<2) /* transmission complete */
326#define PL_TX_REQ (1<<1) /* transmission received */
327#define PL_PEER_E (1<<0) /* peer exists */
328
329static inline int
330pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
331{
332 return usb_control_msg (dev->udev,
333 usb_rcvctrlpipe (dev->udev, 0),
334 req,
335 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
336 val, index,
337 NULL, 0,
David Brownell47ee3052005-08-31 09:53:42 -0700338 USB_CTRL_GET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static inline int
342pl_clear_QuickLink_features (struct usbnet *dev, int val)
343{
344 return pl_vendor_req (dev, 1, (u8) val, 0);
345}
346
347static inline int
348pl_set_QuickLink_features (struct usbnet *dev, int val)
349{
350 return pl_vendor_req (dev, 3, (u8) val, 0);
351}
352
353/*-------------------------------------------------------------------------*/
354
355static int pl_reset (struct usbnet *dev)
356{
357 /* some units seem to need this reset, others reject it utterly.
358 * FIXME be more like "naplink" or windows drivers.
359 */
360 (void) pl_set_QuickLink_features (dev,
361 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
362 return 0;
363}
364
365static const struct driver_info prolific_info = {
366 .description = "Prolific PL-2301/PL-2302",
367 .flags = FLAG_NO_SETINT,
368 /* some PL-2302 versions seem to fail usb_set_interface() */
369 .reset = pl_reset,
370};
371
372#endif /* CONFIG_USB_PL2301 */
373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*-------------------------------------------------------------------------
376 *
377 * Network Device Driver (peer link to "Host Device", from USB host)
378 *
379 *-------------------------------------------------------------------------*/
380
381static int usbnet_change_mtu (struct net_device *net, int new_mtu)
382{
383 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700384 int ll_mtu = new_mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Brownellf29fc252005-08-31 09:52:31 -0700386 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700389 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -EDOM;
391 net->mtu = new_mtu;
392 return 0;
393}
394
395/*-------------------------------------------------------------------------*/
396
397static struct net_device_stats *usbnet_get_stats (struct net_device *net)
398{
399 struct usbnet *dev = netdev_priv(net);
400 return &dev->stats;
401}
402
403/*-------------------------------------------------------------------------*/
404
405/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
406 * completion callbacks. 2.5 should have fixed those bugs...
407 */
408
David S. Miller8728b832005-08-09 19:25:21 -0700409static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 unsigned long flags;
412
David S. Miller8728b832005-08-09 19:25:21 -0700413 spin_lock_irqsave(&list->lock, flags);
414 __skb_unlink(skb, list);
415 spin_unlock(&list->lock);
416 spin_lock(&dev->done.lock);
417 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700419 tasklet_schedule(&dev->bh);
420 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/* some work can't be done in tasklets, so we use keventd
424 *
425 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
426 * but tasklet_schedule() doesn't. hope the failure is rare.
427 */
David Brownell2e55cc72005-08-31 09:53:10 -0700428void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 set_bit (work, &dev->flags);
431 if (!schedule_work (&dev->kevent))
432 deverr (dev, "kevent %d may have been dropped", work);
433 else
434 devdbg (dev, "kevent %d scheduled", work);
435}
David Brownell2e55cc72005-08-31 09:53:10 -0700436EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*-------------------------------------------------------------------------*/
439
440static void rx_complete (struct urb *urb, struct pt_regs *regs);
441
david-b@pacbell.net0d9899f2005-07-28 20:46:32 -0700442static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct sk_buff *skb;
445 struct skb_data *entry;
446 int retval = 0;
447 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700448 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
451 if (netif_msg_rx_err (dev))
452 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700453 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 usb_free_urb (urb);
455 return;
456 }
457 skb_reserve (skb, NET_IP_ALIGN);
458
459 entry = (struct skb_data *) skb->cb;
460 entry->urb = urb;
461 entry->dev = dev;
462 entry->state = rx_start;
463 entry->length = 0;
464
465 usb_fill_bulk_urb (urb, dev->udev, dev->in,
466 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 spin_lock_irqsave (&dev->rxq.lock, lockflags);
469
470 if (netif_running (dev->net)
471 && netif_device_present (dev->net)
472 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
473 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
474 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700475 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700478 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case -ENODEV:
481 if (netif_msg_ifdown (dev))
482 devdbg (dev, "device gone");
483 netif_device_detach (dev->net);
484 break;
485 default:
486 if (netif_msg_rx_err (dev))
487 devdbg (dev, "rx submit, %d", retval);
488 tasklet_schedule (&dev->bh);
489 break;
490 case 0:
491 __skb_queue_tail (&dev->rxq, skb);
492 }
493 } else {
494 if (netif_msg_ifdown (dev))
495 devdbg (dev, "rx: stopped");
496 retval = -ENOLINK;
497 }
498 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
499 if (retval) {
500 dev_kfree_skb_any (skb);
501 usb_free_urb (urb);
502 }
503}
504
505
506/*-------------------------------------------------------------------------*/
507
508static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
509{
510 if (dev->driver_info->rx_fixup
511 && !dev->driver_info->rx_fixup (dev, skb))
512 goto error;
513 // else network stack removes extra byte if we forced a short packet
514
515 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700516 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 else {
518 if (netif_msg_rx_err (dev))
519 devdbg (dev, "drop");
520error:
521 dev->stats.rx_errors++;
522 skb_queue_tail (&dev->done, skb);
523 }
524}
525
526/*-------------------------------------------------------------------------*/
527
528static void rx_complete (struct urb *urb, struct pt_regs *regs)
529{
530 struct sk_buff *skb = (struct sk_buff *) urb->context;
531 struct skb_data *entry = (struct skb_data *) skb->cb;
532 struct usbnet *dev = entry->dev;
533 int urb_status = urb->status;
534
535 skb_put (skb, urb->actual_length);
536 entry->state = rx_done;
537 entry->urb = NULL;
538
539 switch (urb_status) {
540 // success
541 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700542 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 entry->state = rx_cleanup;
544 dev->stats.rx_errors++;
545 dev->stats.rx_length_errors++;
546 if (netif_msg_rx_err (dev))
547 devdbg (dev, "rx length %d", skb->len);
548 }
549 break;
550
551 // stalls need manual reset. this is rare ... except that
552 // when going through USB 2.0 TTs, unplug appears this way.
553 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
554 // storm, recovering as needed.
555 case -EPIPE:
556 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700557 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 // FALLTHROUGH
559
560 // software-driven interface shutdown
561 case -ECONNRESET: // async unlink
562 case -ESHUTDOWN: // hardware gone
563 if (netif_msg_ifdown (dev))
564 devdbg (dev, "rx shutdown, code %d", urb_status);
565 goto block;
566
567 // we get controller i/o faults during khubd disconnect() delays.
568 // throttle down resubmits, to avoid log floods; just temporarily,
569 // so we still recover when the fault isn't a khubd delay.
570 case -EPROTO: // ehci
571 case -ETIMEDOUT: // ohci
572 case -EILSEQ: // uhci
573 dev->stats.rx_errors++;
574 if (!timer_pending (&dev->delay)) {
575 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
576 if (netif_msg_link (dev))
577 devdbg (dev, "rx throttle %d", urb_status);
578 }
579block:
580 entry->state = rx_cleanup;
581 entry->urb = urb;
582 urb = NULL;
583 break;
584
585 // data overrun ... flush fifo?
586 case -EOVERFLOW:
587 dev->stats.rx_over_errors++;
588 // FALLTHROUGH
589
590 default:
591 entry->state = rx_cleanup;
592 dev->stats.rx_errors++;
593 if (netif_msg_rx_err (dev))
594 devdbg (dev, "rx status %d", urb_status);
595 break;
596 }
597
David S. Miller8728b832005-08-09 19:25:21 -0700598 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (urb) {
601 if (netif_running (dev->net)
602 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
603 rx_submit (dev, urb, GFP_ATOMIC);
604 return;
605 }
606 usb_free_urb (urb);
607 }
608 if (netif_msg_rx_err (dev))
609 devdbg (dev, "no read resubmitted");
610}
611
612static void intr_complete (struct urb *urb, struct pt_regs *regs)
613{
614 struct usbnet *dev = urb->context;
615 int status = urb->status;
616
617 switch (status) {
618 /* success */
619 case 0:
620 dev->driver_info->status(dev, urb);
621 break;
622
623 /* software-driven interface shutdown */
624 case -ENOENT: // urb killed
625 case -ESHUTDOWN: // hardware gone
626 if (netif_msg_ifdown (dev))
627 devdbg (dev, "intr shutdown, code %d", status);
628 return;
629
630 /* NOTE: not throttling like RX/TX, since this endpoint
631 * already polls infrequently
632 */
633 default:
634 devdbg (dev, "intr status %d", status);
635 break;
636 }
637
638 if (!netif_running (dev->net))
639 return;
640
641 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
642 status = usb_submit_urb (urb, GFP_ATOMIC);
643 if (status != 0 && netif_msg_timer (dev))
644 deverr(dev, "intr resubmit --> %d", status);
645}
646
647/*-------------------------------------------------------------------------*/
648
649// unlink pending rx/tx; completion handlers do all other cleanup
650
651static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
652{
653 unsigned long flags;
654 struct sk_buff *skb, *skbnext;
655 int count = 0;
656
657 spin_lock_irqsave (&q->lock, flags);
658 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
659 struct skb_data *entry;
660 struct urb *urb;
661 int retval;
662
663 entry = (struct skb_data *) skb->cb;
664 urb = entry->urb;
665 skbnext = skb->next;
666
667 // during some PM-driven resume scenarios,
668 // these (async) unlinks complete immediately
669 retval = usb_unlink_urb (urb);
670 if (retval != -EINPROGRESS && retval != 0)
671 devdbg (dev, "unlink urb err, %d", retval);
672 else
673 count++;
674 }
675 spin_unlock_irqrestore (&q->lock, flags);
676 return count;
677}
678
679
680/*-------------------------------------------------------------------------*/
681
682// precondition: never called in_interrupt
683
684static int usbnet_stop (struct net_device *net)
685{
686 struct usbnet *dev = netdev_priv(net);
687 int temp;
688 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
689 DECLARE_WAITQUEUE (wait, current);
690
691 netif_stop_queue (net);
692
693 if (netif_msg_ifdown (dev))
694 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
695 dev->stats.rx_packets, dev->stats.tx_packets,
696 dev->stats.rx_errors, dev->stats.tx_errors
697 );
698
699 // ensure there are no more active urbs
700 add_wait_queue (&unlink_wakeup, &wait);
701 dev->wait = &unlink_wakeup;
702 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
703
704 // maybe wait for deletions to finish.
David S. Millerb03efcf2005-07-08 14:57:23 -0700705 while (!skb_queue_empty(&dev->rxq) &&
706 !skb_queue_empty(&dev->txq) &&
707 !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 msleep(UNLINK_TIMEOUT_MS);
709 if (netif_msg_ifdown (dev))
710 devdbg (dev, "waited for %d urb completions", temp);
711 }
712 dev->wait = NULL;
713 remove_wait_queue (&unlink_wakeup, &wait);
714
715 usb_kill_urb(dev->interrupt);
716
717 /* deferred work (task, timer, softirq) must also stop.
718 * can't flush_scheduled_work() until we drop rtnl (later),
719 * else workers could deadlock; so make workers a NOP.
720 */
721 dev->flags = 0;
722 del_timer_sync (&dev->delay);
723 tasklet_kill (&dev->bh);
724
725 return 0;
726}
727
728/*-------------------------------------------------------------------------*/
729
730// posts reads, and enables write queuing
731
732// precondition: never called in_interrupt
733
734static int usbnet_open (struct net_device *net)
735{
736 struct usbnet *dev = netdev_priv(net);
737 int retval = 0;
738 struct driver_info *info = dev->driver_info;
739
740 // put into "known safe" state
741 if (info->reset && (retval = info->reset (dev)) < 0) {
742 if (netif_msg_ifup (dev))
743 devinfo (dev,
744 "open reset fail (%d) usbnet usb-%s-%s, %s",
745 retval,
746 dev->udev->bus->bus_name, dev->udev->devpath,
747 info->description);
748 goto done;
749 }
750
751 // insist peer be connected
752 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
753 if (netif_msg_ifup (dev))
754 devdbg (dev, "can't open; %d", retval);
755 goto done;
756 }
757
758 /* start any status interrupt transfer */
759 if (dev->interrupt) {
760 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
761 if (retval < 0) {
762 if (netif_msg_ifup (dev))
763 deverr (dev, "intr submit %d", retval);
764 goto done;
765 }
766 }
767
768 netif_start_queue (net);
769 if (netif_msg_ifup (dev)) {
770 char *framing;
771
772 if (dev->driver_info->flags & FLAG_FRAMING_NC)
773 framing = "NetChip";
774 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
775 framing = "GeneSys";
776 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
777 framing = "Zaurus";
778 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
779 framing = "RNDIS";
780 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
781 framing = "ASIX";
782 else
783 framing = "simple";
784
785 devinfo (dev, "open: enable queueing "
786 "(rx %d, tx %d) mtu %d %s framing",
787 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
788 framing);
789 }
790
791 // delay posting reads until we're fully open
792 tasklet_schedule (&dev->bh);
793done:
794 return retval;
795}
796
797/*-------------------------------------------------------------------------*/
798
David Brownell2e55cc72005-08-31 09:53:10 -0700799/* ethtool methods; minidrivers may need to add some more, but
800 * they'll probably want to use this base set.
801 */
802
803void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct usbnet *dev = netdev_priv(net);
806
David Brownell2e55cc72005-08-31 09:53:10 -0700807 /* REVISIT don't always return "usbnet" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 strncpy (info->driver, driver_name, sizeof info->driver);
809 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
810 strncpy (info->fw_version, dev->driver_info->description,
811 sizeof info->fw_version);
812 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
813}
David Brownell2e55cc72005-08-31 09:53:10 -0700814EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816static u32 usbnet_get_link (struct net_device *net)
817{
818 struct usbnet *dev = netdev_priv(net);
819
David Brownellf29fc252005-08-31 09:52:31 -0700820 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (dev->driver_info->check_connect)
822 return dev->driver_info->check_connect (dev) == 0;
823
David Brownellf29fc252005-08-31 09:52:31 -0700824 /* Otherwise, say we're up (to avoid breaking scripts) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 1;
826}
827
David Brownell2e55cc72005-08-31 09:53:10 -0700828u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct usbnet *dev = netdev_priv(net);
831
832 return dev->msg_enable;
833}
David Brownell2e55cc72005-08-31 09:53:10 -0700834EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
David Brownell2e55cc72005-08-31 09:53:10 -0700836void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct usbnet *dev = netdev_priv(net);
839
840 dev->msg_enable = level;
841}
David Brownell2e55cc72005-08-31 09:53:10 -0700842EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
David Brownell2e55cc72005-08-31 09:53:10 -0700844/* drivers may override default ethtool_ops in their bind() routine */
845static struct ethtool_ops usbnet_ethtool_ops = {
846 .get_drvinfo = usbnet_get_drvinfo,
847 .get_link = usbnet_get_link,
848 .get_msglevel = usbnet_get_msglevel,
849 .set_msglevel = usbnet_set_msglevel,
850};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852/*-------------------------------------------------------------------------*/
853
854/* work that cannot be done in interrupt context uses keventd.
855 *
856 * NOTE: with 2.5 we could do more of this using completion callbacks,
857 * especially now that control transfers can be queued.
858 */
859static void
860kevent (void *data)
861{
862 struct usbnet *dev = data;
863 int status;
864
865 /* usb_clear_halt() needs a thread context */
866 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
867 unlink_urbs (dev, &dev->txq);
868 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700869 if (status < 0
870 && status != -EPIPE
871 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (netif_msg_tx_err (dev))
873 deverr (dev, "can't clear tx halt, status %d",
874 status);
875 } else {
876 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700877 if (status != -ESHUTDOWN)
878 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880 }
881 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
882 unlink_urbs (dev, &dev->rxq);
883 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700884 if (status < 0
885 && status != -EPIPE
886 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (netif_msg_rx_err (dev))
888 deverr (dev, "can't clear rx halt, status %d",
889 status);
890 } else {
891 clear_bit (EVENT_RX_HALT, &dev->flags);
892 tasklet_schedule (&dev->bh);
893 }
894 }
895
896 /* tasklet could resubmit itself forever if memory is tight */
897 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
898 struct urb *urb = NULL;
899
900 if (netif_running (dev->net))
901 urb = usb_alloc_urb (0, GFP_KERNEL);
902 else
903 clear_bit (EVENT_RX_MEMORY, &dev->flags);
904 if (urb != NULL) {
905 clear_bit (EVENT_RX_MEMORY, &dev->flags);
906 rx_submit (dev, urb, GFP_KERNEL);
907 tasklet_schedule (&dev->bh);
908 }
909 }
910
David Hollis7ea13c92005-04-22 15:07:02 -0700911 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
912 struct driver_info *info = dev->driver_info;
913 int retval = 0;
914
915 clear_bit (EVENT_LINK_RESET, &dev->flags);
916 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
917 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
918 retval,
919 dev->udev->bus->bus_name, dev->udev->devpath,
920 info->description);
921 }
922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (dev->flags)
925 devdbg (dev, "kevent done, flags = 0x%lx",
926 dev->flags);
927}
928
929/*-------------------------------------------------------------------------*/
930
931static void tx_complete (struct urb *urb, struct pt_regs *regs)
932{
933 struct sk_buff *skb = (struct sk_buff *) urb->context;
934 struct skb_data *entry = (struct skb_data *) skb->cb;
935 struct usbnet *dev = entry->dev;
936
937 if (urb->status == 0) {
938 dev->stats.tx_packets++;
939 dev->stats.tx_bytes += entry->length;
940 } else {
941 dev->stats.tx_errors++;
942
943 switch (urb->status) {
944 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700945 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947
948 /* software-driven interface shutdown */
949 case -ECONNRESET: // async unlink
950 case -ESHUTDOWN: // hardware gone
951 break;
952
953 // like rx, tx gets controller i/o faults during khubd delays
954 // and so it uses the same throttling mechanism.
955 case -EPROTO: // ehci
956 case -ETIMEDOUT: // ohci
957 case -EILSEQ: // uhci
958 if (!timer_pending (&dev->delay)) {
959 mod_timer (&dev->delay,
960 jiffies + THROTTLE_JIFFIES);
961 if (netif_msg_link (dev))
962 devdbg (dev, "tx throttle %d",
963 urb->status);
964 }
965 netif_stop_queue (dev->net);
966 break;
967 default:
968 if (netif_msg_tx_err (dev))
969 devdbg (dev, "tx err %d", entry->urb->status);
970 break;
971 }
972 }
973
974 urb->dev = NULL;
975 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700976 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*-------------------------------------------------------------------------*/
980
981static void usbnet_tx_timeout (struct net_device *net)
982{
983 struct usbnet *dev = netdev_priv(net);
984
985 unlink_urbs (dev, &dev->txq);
986 tasklet_schedule (&dev->bh);
987
988 // FIXME: device recovery -- reset?
989}
990
991/*-------------------------------------------------------------------------*/
992
993static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
994{
995 struct usbnet *dev = netdev_priv(net);
996 int length;
997 int retval = NET_XMIT_SUCCESS;
998 struct urb *urb = NULL;
999 struct skb_data *entry;
1000 struct driver_info *info = dev->driver_info;
1001 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 // some devices want funky USB-level framing, for
1004 // win32 driver (usually) and/or hardware quirks
1005 if (info->tx_fixup) {
1006 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1007 if (!skb) {
1008 if (netif_msg_tx_err (dev))
1009 devdbg (dev, "can't tx_fixup skb");
1010 goto drop;
1011 }
1012 }
1013 length = skb->len;
1014
1015 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1016 if (netif_msg_tx_err (dev))
1017 devdbg (dev, "no urb");
1018 goto drop;
1019 }
1020
1021 entry = (struct skb_data *) skb->cb;
1022 entry->urb = urb;
1023 entry->dev = dev;
1024 entry->state = tx_start;
1025 entry->length = length;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1028 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* don't assume the hardware handles USB_ZERO_PACKET
1031 * NOTE: strictly conforming cdc-ether devices should expect
1032 * the ZLP here, but ignore the one-byte packet.
1033 *
1034 * FIXME zero that byte, if it doesn't require a new skb.
1035 */
1036 if ((length % dev->maxpacket) == 0)
1037 urb->transfer_buffer_length++;
1038
1039 spin_lock_irqsave (&dev->txq.lock, flags);
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1042 case -EPIPE:
1043 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001044 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 break;
1046 default:
1047 if (netif_msg_tx_err (dev))
1048 devdbg (dev, "tx: submit urb err %d", retval);
1049 break;
1050 case 0:
1051 net->trans_start = jiffies;
1052 __skb_queue_tail (&dev->txq, skb);
1053 if (dev->txq.qlen >= TX_QLEN (dev))
1054 netif_stop_queue (net);
1055 }
1056 spin_unlock_irqrestore (&dev->txq.lock, flags);
1057
1058 if (retval) {
1059 if (netif_msg_tx_err (dev))
1060 devdbg (dev, "drop, code %d", retval);
1061drop:
1062 retval = NET_XMIT_SUCCESS;
1063 dev->stats.tx_dropped++;
1064 if (skb)
1065 dev_kfree_skb_any (skb);
1066 usb_free_urb (urb);
1067 } else if (netif_msg_tx_queued (dev)) {
1068 devdbg (dev, "> tx, len %d, type 0x%x",
1069 length, skb->protocol);
1070 }
1071 return retval;
1072}
1073
1074
1075/*-------------------------------------------------------------------------*/
1076
1077// tasklet (work deferred from completions, in_irq) or timer
1078
1079static void usbnet_bh (unsigned long param)
1080{
1081 struct usbnet *dev = (struct usbnet *) param;
1082 struct sk_buff *skb;
1083 struct skb_data *entry;
1084
1085 while ((skb = skb_dequeue (&dev->done))) {
1086 entry = (struct skb_data *) skb->cb;
1087 switch (entry->state) {
1088 case rx_done:
1089 entry->state = rx_cleanup;
1090 rx_process (dev, skb);
1091 continue;
1092 case tx_done:
1093 case rx_cleanup:
1094 usb_free_urb (entry->urb);
1095 dev_kfree_skb (skb);
1096 continue;
1097 default:
1098 devdbg (dev, "bogus skb state %d", entry->state);
1099 }
1100 }
1101
1102 // waiting for all pending urbs to complete?
1103 if (dev->wait) {
1104 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1105 wake_up (dev->wait);
1106 }
1107
1108 // or are we maybe short a few urbs?
1109 } else if (netif_running (dev->net)
1110 && netif_device_present (dev->net)
1111 && !timer_pending (&dev->delay)
1112 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1113 int temp = dev->rxq.qlen;
1114 int qlen = RX_QLEN (dev);
1115
1116 if (temp < qlen) {
1117 struct urb *urb;
1118 int i;
1119
1120 // don't refill the queue all at once
1121 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1122 urb = usb_alloc_urb (0, GFP_ATOMIC);
1123 if (urb != NULL)
1124 rx_submit (dev, urb, GFP_ATOMIC);
1125 }
1126 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1127 devdbg (dev, "rxqlen %d --> %d",
1128 temp, dev->rxq.qlen);
1129 if (dev->rxq.qlen < qlen)
1130 tasklet_schedule (&dev->bh);
1131 }
1132 if (dev->txq.qlen < TX_QLEN (dev))
1133 netif_wake_queue (dev->net);
1134 }
1135}
1136
1137
1138
1139/*-------------------------------------------------------------------------
1140 *
1141 * USB Device Driver support
1142 *
1143 *-------------------------------------------------------------------------*/
1144
1145// precondition: never called in_interrupt
1146
David Brownell38bde1d2005-08-31 09:52:45 -07001147void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct usbnet *dev;
1150 struct usb_device *xdev;
1151 struct net_device *net;
1152
1153 dev = usb_get_intfdata(intf);
1154 usb_set_intfdata(intf, NULL);
1155 if (!dev)
1156 return;
1157
1158 xdev = interface_to_usbdev (intf);
1159
1160 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001161 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1162 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xdev->bus->bus_name, xdev->devpath,
1164 dev->driver_info->description);
1165
1166 net = dev->net;
1167 unregister_netdev (net);
1168
1169 /* we don't hold rtnl here ... */
1170 flush_scheduled_work ();
1171
1172 if (dev->driver_info->unbind)
1173 dev->driver_info->unbind (dev, intf);
1174
1175 free_netdev(net);
1176 usb_put_dev (xdev);
1177}
David Brownell38bde1d2005-08-31 09:52:45 -07001178EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180
1181/*-------------------------------------------------------------------------*/
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183// precondition: never called in_interrupt
1184
David Brownell38bde1d2005-08-31 09:52:45 -07001185int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1187{
1188 struct usbnet *dev;
1189 struct net_device *net;
1190 struct usb_host_interface *interface;
1191 struct driver_info *info;
1192 struct usb_device *xdev;
1193 int status;
1194
1195 info = (struct driver_info *) prod->driver_info;
1196 if (!info) {
1197 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1198 return -ENODEV;
1199 }
1200 xdev = interface_to_usbdev (udev);
1201 interface = udev->cur_altsetting;
1202
1203 usb_get_dev (xdev);
1204
1205 status = -ENOMEM;
1206
1207 // set up our own records
1208 net = alloc_etherdev(sizeof(*dev));
1209 if (!net) {
1210 dbg ("can't kmalloc dev");
1211 goto out;
1212 }
1213
1214 dev = netdev_priv(net);
1215 dev->udev = xdev;
1216 dev->driver_info = info;
1217 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1218 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1219 skb_queue_head_init (&dev->rxq);
1220 skb_queue_head_init (&dev->txq);
1221 skb_queue_head_init (&dev->done);
1222 dev->bh.func = usbnet_bh;
1223 dev->bh.data = (unsigned long) dev;
1224 INIT_WORK (&dev->kevent, kevent, dev);
1225 dev->delay.function = usbnet_bh;
1226 dev->delay.data = (unsigned long) dev;
1227 init_timer (&dev->delay);
1228
1229 SET_MODULE_OWNER (net);
1230 dev->net = net;
1231 strcpy (net->name, "usb%d");
1232 memcpy (net->dev_addr, node_id, sizeof node_id);
1233
David Brownell2e55cc72005-08-31 09:53:10 -07001234 /* rx and tx sides can use different message sizes;
1235 * bind() should set rx_urb_size in that case.
1236 */
1237 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#if 0
1239// dma_supported() is deeply broken on almost all architectures
1240 // possible with some EHCI controllers
1241 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1242 net->features |= NETIF_F_HIGHDMA;
1243#endif
1244
1245 net->change_mtu = usbnet_change_mtu;
1246 net->get_stats = usbnet_get_stats;
1247 net->hard_start_xmit = usbnet_start_xmit;
1248 net->open = usbnet_open;
1249 net->stop = usbnet_stop;
1250 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1251 net->tx_timeout = usbnet_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 net->ethtool_ops = &usbnet_ethtool_ops;
1253
1254 // allow device-specific bind/init procedures
1255 // NOTE net->name still not usable ...
1256 if (info->bind) {
1257 status = info->bind (dev, udev);
1258 // heuristic: "usb%d" for links we know are two-host,
1259 // else "eth%d" when there's reasonable doubt. userspace
1260 // can rename the link if it knows better.
1261 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1262 && (net->dev_addr [0] & 0x02) == 0)
1263 strcpy (net->name, "eth%d");
David Brownellf29fc252005-08-31 09:52:31 -07001264
1265 /* maybe the remote can't receive an Ethernet MTU */
1266 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1267 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001268 } else if (!info->in || !info->out)
1269 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else {
1271 dev->in = usb_rcvbulkpipe (xdev, info->in);
1272 dev->out = usb_sndbulkpipe (xdev, info->out);
1273 if (!(info->flags & FLAG_NO_SETINT))
1274 status = usb_set_interface (xdev,
1275 interface->desc.bInterfaceNumber,
1276 interface->desc.bAlternateSetting);
1277 else
1278 status = 0;
1279
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (status == 0 && dev->status)
1282 status = init_status (dev, udev);
1283 if (status < 0)
1284 goto out1;
1285
David Brownell2e55cc72005-08-31 09:53:10 -07001286 if (!dev->rx_urb_size)
1287 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1289
1290 SET_NETDEV_DEV(net, &udev->dev);
1291 status = register_netdev (net);
1292 if (status)
1293 goto out3;
1294 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001295 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 "%02x:%02x:%02x:%02x:%02x:%02x",
David Brownell38bde1d2005-08-31 09:52:45 -07001297 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xdev->bus->bus_name, xdev->devpath,
1299 dev->driver_info->description,
1300 net->dev_addr [0], net->dev_addr [1],
1301 net->dev_addr [2], net->dev_addr [3],
1302 net->dev_addr [4], net->dev_addr [5]);
1303
1304 // ok, it's ready to go.
1305 usb_set_intfdata (udev, dev);
1306
1307 // start as if the link is up
1308 netif_device_attach (net);
1309
1310 return 0;
1311
1312out3:
1313 if (info->unbind)
1314 info->unbind (dev, udev);
1315out1:
1316 free_netdev(net);
1317out:
1318 usb_put_dev(xdev);
1319 return status;
1320}
David Brownell38bde1d2005-08-31 09:52:45 -07001321EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323/*-------------------------------------------------------------------------*/
1324
David Brownell38bde1d2005-08-31 09:52:45 -07001325/* FIXME these suspend/resume methods assume non-CDC style
1326 * devices, with only one interface.
1327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
David Brownell38bde1d2005-08-31 09:52:45 -07001329int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct usbnet *dev = usb_get_intfdata(intf);
1332
David Brownell27d72e82005-04-18 17:39:22 -07001333 /* accelerate emptying of the rx and queues, to avoid
1334 * having everything error out.
1335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 netif_device_detach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001337 (void) unlink_urbs (dev, &dev->rxq);
1338 (void) unlink_urbs (dev, &dev->txq);
1339 intf->dev.power.power_state = PMSG_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341}
David Brownell38bde1d2005-08-31 09:52:45 -07001342EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
David Brownell38bde1d2005-08-31 09:52:45 -07001344int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct usbnet *dev = usb_get_intfdata(intf);
1347
David Brownell27d72e82005-04-18 17:39:22 -07001348 intf->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 netif_device_attach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001350 tasklet_schedule (&dev->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
David Brownell38bde1d2005-08-31 09:52:45 -07001353EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/*-------------------------------------------------------------------------*/
1357
1358#ifndef HAVE_HARDWARE
1359#error You need to configure some hardware for this driver
1360#endif
1361
1362/*
1363 * chip vendor names won't normally be on the cables, and
1364 * may not be on the device.
1365 */
1366
1367static const struct usb_device_id products [] = {
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#ifdef CONFIG_USB_PL2301
1370{
1371 USB_DEVICE (0x067b, 0x0000), // PL-2301
1372 .driver_info = (unsigned long) &prolific_info,
1373}, {
1374 USB_DEVICE (0x067b, 0x0001), // PL-2302
1375 .driver_info = (unsigned long) &prolific_info,
1376},
1377#endif
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#ifdef CONFIG_USB_RNDIS
1380{
1381 /* RNDIS is MSFT's un-official variant of CDC ACM */
1382 USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
1383 .driver_info = (unsigned long) &rndis_info,
1384},
1385#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 { }, // END
1387};
1388MODULE_DEVICE_TABLE (usb, products);
1389
1390static struct usb_driver usbnet_driver = {
1391 .owner = THIS_MODULE,
1392 .name = driver_name,
1393 .id_table = products,
1394 .probe = usbnet_probe,
1395 .disconnect = usbnet_disconnect,
1396 .suspend = usbnet_suspend,
1397 .resume = usbnet_resume,
1398};
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/*-------------------------------------------------------------------------*/
1401
David Brownellf29fc252005-08-31 09:52:31 -07001402static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 // compiler should optimize these out
1405 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1406 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 random_ether_addr(node_id);
1409
1410 return usb_register(&usbnet_driver);
1411}
David Brownellf29fc252005-08-31 09:52:31 -07001412module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
David Brownellf29fc252005-08-31 09:52:31 -07001414static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
David Brownellf29fc252005-08-31 09:52:31 -07001416 usb_deregister(&usbnet_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
David Brownellf29fc252005-08-31 09:52:31 -07001418module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
David Brownellf29fc252005-08-31 09:52:31 -07001420MODULE_AUTHOR("David Brownell");
1421MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1422MODULE_LICENSE("GPL");