blob: 5b16d9a1269a2851e3f8982b23f835c256044ba7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownell090ffa92005-08-31 09:54:50 -07002 * USB Network driver infrastructure
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
David Brownell090ffa92005-08-31 09:54:50 -070023 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
David Brownell090ffa92005-08-31 09:54:50 -070026 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
41#include <linux/workqueue.h>
42#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/usb.h>
David Brownellf29fc252005-08-31 09:52:31 -070044
45#include "usbnet.h"
46
47#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
50/*-------------------------------------------------------------------------*/
51
52/*
53 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54 * Several dozen bytes of IPv4 data can fit in two such transactions.
55 * One maximum size Ethernet packet takes twenty four of them.
56 * For high speed, each frame comfortably fits almost 36 max size
57 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070058 *
59 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60 * let the USB host controller be busy for 5msec or more before an irq
61 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Jamie Paintera99c1942006-07-27 14:17:28 -040063#define RX_MAX_QUEUE_MEMORY (60 * 1518)
64#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069// reawaken network queue this soon after stopping; else watchdog barks
70#define TX_TIMEOUT_JIFFIES (5*HZ)
71
72// throttle rx/tx briefly after some faults, so khubd might disconnect()
73// us (it polls at HZ/4 usually) before we report too many false errors.
74#define THROTTLE_JIFFIES (HZ/8)
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076// between wakeups
77#define UNLINK_TIMEOUT_MS 3
78
79/*-------------------------------------------------------------------------*/
80
81// randomly generated ethernet address
82static u8 node_id [ETH_ALEN];
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static const char driver_name [] = "usbnet";
85
86/* use ethtool to change the level for any given device */
87static int msg_level = -1;
88module_param (msg_level, int, 0);
89MODULE_PARM_DESC (msg_level, "Override default message level");
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*-------------------------------------------------------------------------*/
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070094int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int tmp;
97 struct usb_host_interface *alt = NULL;
98 struct usb_host_endpoint *in = NULL, *out = NULL;
99 struct usb_host_endpoint *status = NULL;
100
101 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
102 unsigned ep;
103
104 in = out = status = NULL;
105 alt = intf->altsetting + tmp;
106
107 /* take the first altsetting with in-bulk + out-bulk;
108 * remember any status endpoint, just in case;
109 * ignore other endpoints and altsetttings.
110 */
111 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
112 struct usb_host_endpoint *e;
113 int intr = 0;
114
115 e = alt->endpoint + ep;
116 switch (e->desc.bmAttributes) {
117 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300118 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 continue;
120 intr = 1;
121 /* FALLTHROUGH */
122 case USB_ENDPOINT_XFER_BULK:
123 break;
124 default:
125 continue;
126 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300127 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!intr && !in)
129 in = e;
130 else if (intr && !status)
131 status = e;
132 } else {
133 if (!out)
134 out = e;
135 }
136 }
137 if (in && out)
138 break;
139 }
140 if (!alt || !in || !out)
141 return -EINVAL;
142
143 if (alt->desc.bAlternateSetting != 0
144 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
145 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146 alt->desc.bAlternateSetting);
147 if (tmp < 0)
148 return tmp;
149 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 dev->in = usb_rcvbulkpipe (dev->udev,
152 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153 dev->out = usb_sndbulkpipe (dev->udev,
154 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->status = status;
156 return 0;
157}
David Brownell2e55cc72005-08-31 09:53:10 -0700158EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Howells7d12e782006-10-05 14:55:46 +0100160static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static int init_status (struct usbnet *dev, struct usb_interface *intf)
163{
164 char *buf = NULL;
165 unsigned pipe = 0;
166 unsigned maxp;
167 unsigned period;
168
169 if (!dev->driver_info->status)
170 return 0;
171
172 pipe = usb_rcvintpipe (dev->udev,
173 dev->status->desc.bEndpointAddress
174 & USB_ENDPOINT_NUMBER_MASK);
175 maxp = usb_maxpacket (dev->udev, pipe, 0);
176
177 /* avoid 1 msec chatter: min 8 msec poll rate */
178 period = max ((int) dev->status->desc.bInterval,
179 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
180
Christoph Lametere94b1762006-12-06 20:33:17 -0800181 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800183 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!dev->interrupt) {
185 kfree (buf);
186 return -ENOMEM;
187 } else {
188 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
189 buf, maxp, intr_complete, dev, period);
190 dev_dbg(&intf->dev,
191 "status ep%din, %d bytes period %d\n",
192 usb_pipeendpoint(pipe), maxp, period);
193 }
194 }
195 return 0;
196}
197
David Brownell2e55cc72005-08-31 09:53:10 -0700198/* Passes this packet up the stack, updating its accounting.
199 * Some link protocols batch packets, so their rx_fixup paths
200 * can return clones as well as just modify the original skb.
201 */
202void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int status;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->protocol = eth_type_trans (skb, dev->net);
207 dev->stats.rx_packets++;
208 dev->stats.rx_bytes += skb->len;
209
210 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700211 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 skb->len + sizeof (struct ethhdr), skb->protocol);
213 memset (skb->cb, 0, sizeof (struct skb_data));
214 status = netif_rx (skb);
215 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
216 devdbg (dev, "netif_rx status %d", status);
217}
David Brownell2e55cc72005-08-31 09:53:10 -0700218EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*-------------------------------------------------------------------------
222 *
223 * Network Device Driver (peer link to "Host Device", from USB host)
224 *
225 *-------------------------------------------------------------------------*/
226
227static int usbnet_change_mtu (struct net_device *net, int new_mtu)
228{
229 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700230 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400231 int old_hard_mtu = dev->hard_mtu;
232 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jamie Paintera99c1942006-07-27 14:17:28 -0400234 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700237 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EDOM;
239 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400240
241 dev->hard_mtu = net->mtu + net->hard_header_len;
242 if (dev->rx_urb_size == old_hard_mtu) {
243 dev->rx_urb_size = dev->hard_mtu;
244 if (dev->rx_urb_size > old_rx_urb_size)
245 usbnet_unlink_rx_urbs(dev);
246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250
251/*-------------------------------------------------------------------------*/
252
253static struct net_device_stats *usbnet_get_stats (struct net_device *net)
254{
255 struct usbnet *dev = netdev_priv(net);
256 return &dev->stats;
257}
258
259/*-------------------------------------------------------------------------*/
260
261/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
262 * completion callbacks. 2.5 should have fixed those bugs...
263 */
264
David S. Miller8728b832005-08-09 19:25:21 -0700265static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
268
David S. Miller8728b832005-08-09 19:25:21 -0700269 spin_lock_irqsave(&list->lock, flags);
270 __skb_unlink(skb, list);
271 spin_unlock(&list->lock);
272 spin_lock(&dev->done.lock);
273 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700275 tasklet_schedule(&dev->bh);
276 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/* some work can't be done in tasklets, so we use keventd
280 *
281 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
282 * but tasklet_schedule() doesn't. hope the failure is rare.
283 */
David Brownell2e55cc72005-08-31 09:53:10 -0700284void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 set_bit (work, &dev->flags);
287 if (!schedule_work (&dev->kevent))
288 deverr (dev, "kevent %d may have been dropped", work);
289 else
290 devdbg (dev, "kevent %d scheduled", work);
291}
David Brownell2e55cc72005-08-31 09:53:10 -0700292EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/*-------------------------------------------------------------------------*/
295
David Howells7d12e782006-10-05 14:55:46 +0100296static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Al Viro55016f12005-10-21 03:21:58 -0400298static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct sk_buff *skb;
301 struct skb_data *entry;
302 int retval = 0;
303 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700304 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
307 if (netif_msg_rx_err (dev))
308 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700309 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 usb_free_urb (urb);
311 return;
312 }
313 skb_reserve (skb, NET_IP_ALIGN);
314
315 entry = (struct skb_data *) skb->cb;
316 entry->urb = urb;
317 entry->dev = dev;
318 entry->state = rx_start;
319 entry->length = 0;
320
321 usb_fill_bulk_urb (urb, dev->udev, dev->in,
322 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 spin_lock_irqsave (&dev->rxq.lock, lockflags);
325
326 if (netif_running (dev->net)
327 && netif_device_present (dev->net)
328 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800329 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700331 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700334 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 case -ENODEV:
337 if (netif_msg_ifdown (dev))
338 devdbg (dev, "device gone");
339 netif_device_detach (dev->net);
340 break;
341 default:
342 if (netif_msg_rx_err (dev))
343 devdbg (dev, "rx submit, %d", retval);
344 tasklet_schedule (&dev->bh);
345 break;
346 case 0:
347 __skb_queue_tail (&dev->rxq, skb);
348 }
349 } else {
350 if (netif_msg_ifdown (dev))
351 devdbg (dev, "rx: stopped");
352 retval = -ENOLINK;
353 }
354 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
355 if (retval) {
356 dev_kfree_skb_any (skb);
357 usb_free_urb (urb);
358 }
359}
360
361
362/*-------------------------------------------------------------------------*/
363
364static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
365{
366 if (dev->driver_info->rx_fixup
367 && !dev->driver_info->rx_fixup (dev, skb))
368 goto error;
369 // else network stack removes extra byte if we forced a short packet
370
371 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700372 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 else {
374 if (netif_msg_rx_err (dev))
375 devdbg (dev, "drop");
376error:
377 dev->stats.rx_errors++;
378 skb_queue_tail (&dev->done, skb);
379 }
380}
381
382/*-------------------------------------------------------------------------*/
383
David Howells7d12e782006-10-05 14:55:46 +0100384static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct sk_buff *skb = (struct sk_buff *) urb->context;
387 struct skb_data *entry = (struct skb_data *) skb->cb;
388 struct usbnet *dev = entry->dev;
389 int urb_status = urb->status;
390
391 skb_put (skb, urb->actual_length);
392 entry->state = rx_done;
393 entry->urb = NULL;
394
395 switch (urb_status) {
396 // success
397 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700398 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 entry->state = rx_cleanup;
400 dev->stats.rx_errors++;
401 dev->stats.rx_length_errors++;
402 if (netif_msg_rx_err (dev))
403 devdbg (dev, "rx length %d", skb->len);
404 }
405 break;
406
407 // stalls need manual reset. this is rare ... except that
408 // when going through USB 2.0 TTs, unplug appears this way.
409 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
410 // storm, recovering as needed.
411 case -EPIPE:
412 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700413 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 // FALLTHROUGH
415
416 // software-driven interface shutdown
417 case -ECONNRESET: // async unlink
418 case -ESHUTDOWN: // hardware gone
419 if (netif_msg_ifdown (dev))
420 devdbg (dev, "rx shutdown, code %d", urb_status);
421 goto block;
422
423 // we get controller i/o faults during khubd disconnect() delays.
424 // throttle down resubmits, to avoid log floods; just temporarily,
425 // so we still recover when the fault isn't a khubd delay.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700426 case -EPROTO:
427 case -ETIME:
428 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dev->stats.rx_errors++;
430 if (!timer_pending (&dev->delay)) {
431 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
432 if (netif_msg_link (dev))
433 devdbg (dev, "rx throttle %d", urb_status);
434 }
435block:
436 entry->state = rx_cleanup;
437 entry->urb = urb;
438 urb = NULL;
439 break;
440
441 // data overrun ... flush fifo?
442 case -EOVERFLOW:
443 dev->stats.rx_over_errors++;
444 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 default:
447 entry->state = rx_cleanup;
448 dev->stats.rx_errors++;
449 if (netif_msg_rx_err (dev))
450 devdbg (dev, "rx status %d", urb_status);
451 break;
452 }
453
David S. Miller8728b832005-08-09 19:25:21 -0700454 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (urb) {
457 if (netif_running (dev->net)
458 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
459 rx_submit (dev, urb, GFP_ATOMIC);
460 return;
461 }
462 usb_free_urb (urb);
463 }
464 if (netif_msg_rx_err (dev))
465 devdbg (dev, "no read resubmitted");
466}
467
David Howells7d12e782006-10-05 14:55:46 +0100468static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct usbnet *dev = urb->context;
471 int status = urb->status;
472
473 switch (status) {
474 /* success */
475 case 0:
476 dev->driver_info->status(dev, urb);
477 break;
478
479 /* software-driven interface shutdown */
480 case -ENOENT: // urb killed
481 case -ESHUTDOWN: // hardware gone
482 if (netif_msg_ifdown (dev))
483 devdbg (dev, "intr shutdown, code %d", status);
484 return;
485
486 /* NOTE: not throttling like RX/TX, since this endpoint
487 * already polls infrequently
488 */
489 default:
490 devdbg (dev, "intr status %d", status);
491 break;
492 }
493
494 if (!netif_running (dev->net))
495 return;
496
497 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
498 status = usb_submit_urb (urb, GFP_ATOMIC);
499 if (status != 0 && netif_msg_timer (dev))
500 deverr(dev, "intr resubmit --> %d", status);
501}
502
503/*-------------------------------------------------------------------------*/
504
505// unlink pending rx/tx; completion handlers do all other cleanup
506
507static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
508{
509 unsigned long flags;
510 struct sk_buff *skb, *skbnext;
511 int count = 0;
512
513 spin_lock_irqsave (&q->lock, flags);
514 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
515 struct skb_data *entry;
516 struct urb *urb;
517 int retval;
518
519 entry = (struct skb_data *) skb->cb;
520 urb = entry->urb;
521 skbnext = skb->next;
522
523 // during some PM-driven resume scenarios,
524 // these (async) unlinks complete immediately
525 retval = usb_unlink_urb (urb);
526 if (retval != -EINPROGRESS && retval != 0)
527 devdbg (dev, "unlink urb err, %d", retval);
528 else
529 count++;
530 }
531 spin_unlock_irqrestore (&q->lock, flags);
532 return count;
533}
534
Jamie Paintera99c1942006-07-27 14:17:28 -0400535// Flush all pending rx urbs
536// minidrivers may need to do this when the MTU changes
537
538void usbnet_unlink_rx_urbs(struct usbnet *dev)
539{
540 if (netif_running(dev->net)) {
541 (void) unlink_urbs (dev, &dev->rxq);
542 tasklet_schedule(&dev->bh);
543 }
544}
545EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547/*-------------------------------------------------------------------------*/
548
549// precondition: never called in_interrupt
550
551static int usbnet_stop (struct net_device *net)
552{
553 struct usbnet *dev = netdev_priv(net);
554 int temp;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800555 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 DECLARE_WAITQUEUE (wait, current);
557
558 netif_stop_queue (net);
559
560 if (netif_msg_ifdown (dev))
561 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
David Brownellcb1cebb2007-02-15 18:52:30 -0800562 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 dev->stats.rx_errors, dev->stats.tx_errors
564 );
565
566 // ensure there are no more active urbs
567 add_wait_queue (&unlink_wakeup, &wait);
568 dev->wait = &unlink_wakeup;
569 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
570
571 // maybe wait for deletions to finish.
David S. Millerb03efcf2005-07-08 14:57:23 -0700572 while (!skb_queue_empty(&dev->rxq) &&
573 !skb_queue_empty(&dev->txq) &&
574 !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 msleep(UNLINK_TIMEOUT_MS);
576 if (netif_msg_ifdown (dev))
577 devdbg (dev, "waited for %d urb completions", temp);
578 }
579 dev->wait = NULL;
David Brownellcb1cebb2007-02-15 18:52:30 -0800580 remove_wait_queue (&unlink_wakeup, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 usb_kill_urb(dev->interrupt);
583
584 /* deferred work (task, timer, softirq) must also stop.
585 * can't flush_scheduled_work() until we drop rtnl (later),
586 * else workers could deadlock; so make workers a NOP.
587 */
588 dev->flags = 0;
589 del_timer_sync (&dev->delay);
590 tasklet_kill (&dev->bh);
591
592 return 0;
593}
594
595/*-------------------------------------------------------------------------*/
596
597// posts reads, and enables write queuing
598
599// precondition: never called in_interrupt
600
601static int usbnet_open (struct net_device *net)
602{
603 struct usbnet *dev = netdev_priv(net);
604 int retval = 0;
605 struct driver_info *info = dev->driver_info;
606
607 // put into "known safe" state
608 if (info->reset && (retval = info->reset (dev)) < 0) {
609 if (netif_msg_ifup (dev))
610 devinfo (dev,
611 "open reset fail (%d) usbnet usb-%s-%s, %s",
612 retval,
613 dev->udev->bus->bus_name, dev->udev->devpath,
614 info->description);
615 goto done;
616 }
617
618 // insist peer be connected
619 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
620 if (netif_msg_ifup (dev))
621 devdbg (dev, "can't open; %d", retval);
622 goto done;
623 }
624
625 /* start any status interrupt transfer */
626 if (dev->interrupt) {
627 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
628 if (retval < 0) {
629 if (netif_msg_ifup (dev))
630 deverr (dev, "intr submit %d", retval);
631 goto done;
632 }
633 }
634
635 netif_start_queue (net);
636 if (netif_msg_ifup (dev)) {
637 char *framing;
638
639 if (dev->driver_info->flags & FLAG_FRAMING_NC)
640 framing = "NetChip";
641 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
642 framing = "GeneSys";
643 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
644 framing = "Zaurus";
645 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
646 framing = "RNDIS";
647 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
648 framing = "ASIX";
649 else
650 framing = "simple";
651
652 devinfo (dev, "open: enable queueing "
653 "(rx %d, tx %d) mtu %d %s framing",
Randy Dunlap25d94e62006-08-07 15:56:40 -0700654 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 framing);
656 }
657
658 // delay posting reads until we're fully open
659 tasklet_schedule (&dev->bh);
660done:
661 return retval;
662}
663
664/*-------------------------------------------------------------------------*/
665
David Brownell2e55cc72005-08-31 09:53:10 -0700666/* ethtool methods; minidrivers may need to add some more, but
667 * they'll probably want to use this base set.
668 */
669
David Brownell18ee91fa2006-11-02 12:29:12 -0800670#if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
671#define HAVE_MII
672
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200673int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
674{
675 struct usbnet *dev = netdev_priv(net);
676
677 if (!dev->mii.mdio_read)
678 return -EOPNOTSUPP;
679
680 return mii_ethtool_gset(&dev->mii, cmd);
681}
682EXPORT_SYMBOL_GPL(usbnet_get_settings);
683
684int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
685{
686 struct usbnet *dev = netdev_priv(net);
687 int retval;
688
689 if (!dev->mii.mdio_write)
690 return -EOPNOTSUPP;
691
692 retval = mii_ethtool_sset(&dev->mii, cmd);
693
694 /* link speed/duplex might have changed */
695 if (dev->driver_info->link_reset)
696 dev->driver_info->link_reset(dev);
697
698 return retval;
699
700}
701EXPORT_SYMBOL_GPL(usbnet_set_settings);
702
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200703u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct usbnet *dev = netdev_priv(net);
706
David Brownellf29fc252005-08-31 09:52:31 -0700707 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (dev->driver_info->check_connect)
709 return dev->driver_info->check_connect (dev) == 0;
710
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200711 /* if the device has mii operations, use those */
712 if (dev->mii.mdio_read)
713 return mii_link_ok(&dev->mii);
714
David Brownellf29fc252005-08-31 09:52:31 -0700715 /* Otherwise, say we're up (to avoid breaking scripts) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 1;
717}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200718EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David Brownell18ee91fa2006-11-02 12:29:12 -0800720int usbnet_nway_reset(struct net_device *net)
721{
722 struct usbnet *dev = netdev_priv(net);
723
724 if (!dev->mii.mdio_write)
725 return -EOPNOTSUPP;
726
727 return mii_nway_restart(&dev->mii);
728}
729EXPORT_SYMBOL_GPL(usbnet_nway_reset);
730
731#endif /* HAVE_MII */
732
733void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
734{
735 struct usbnet *dev = netdev_priv(net);
736
David Brownell296c0242007-04-17 16:10:10 -0700737 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800738 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
739 strncpy (info->fw_version, dev->driver_info->description,
740 sizeof info->fw_version);
741 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
742}
743EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
744
David Brownell2e55cc72005-08-31 09:53:10 -0700745u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct usbnet *dev = netdev_priv(net);
748
749 return dev->msg_enable;
750}
David Brownell2e55cc72005-08-31 09:53:10 -0700751EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David Brownell2e55cc72005-08-31 09:53:10 -0700753void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct usbnet *dev = netdev_priv(net);
756
757 dev->msg_enable = level;
758}
David Brownell2e55cc72005-08-31 09:53:10 -0700759EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
David Brownell2e55cc72005-08-31 09:53:10 -0700761/* drivers may override default ethtool_ops in their bind() routine */
762static struct ethtool_ops usbnet_ethtool_ops = {
David Brownell18ee91fa2006-11-02 12:29:12 -0800763#ifdef HAVE_MII
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200764 .get_settings = usbnet_get_settings,
765 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700766 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200767 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800768#endif
769 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700770 .get_msglevel = usbnet_get_msglevel,
771 .set_msglevel = usbnet_set_msglevel,
772};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774/*-------------------------------------------------------------------------*/
775
776/* work that cannot be done in interrupt context uses keventd.
777 *
778 * NOTE: with 2.5 we could do more of this using completion callbacks,
779 * especially now that control transfers can be queued.
780 */
781static void
David Howellsc4028952006-11-22 14:57:56 +0000782kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
David Howellsc4028952006-11-22 14:57:56 +0000784 struct usbnet *dev =
785 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int status;
787
788 /* usb_clear_halt() needs a thread context */
789 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
790 unlink_urbs (dev, &dev->txq);
791 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700792 if (status < 0
793 && status != -EPIPE
794 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (netif_msg_tx_err (dev))
796 deverr (dev, "can't clear tx halt, status %d",
797 status);
798 } else {
799 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700800 if (status != -ESHUTDOWN)
801 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 }
804 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
805 unlink_urbs (dev, &dev->rxq);
806 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700807 if (status < 0
808 && status != -EPIPE
809 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (netif_msg_rx_err (dev))
811 deverr (dev, "can't clear rx halt, status %d",
812 status);
813 } else {
814 clear_bit (EVENT_RX_HALT, &dev->flags);
815 tasklet_schedule (&dev->bh);
816 }
817 }
818
819 /* tasklet could resubmit itself forever if memory is tight */
820 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
821 struct urb *urb = NULL;
822
823 if (netif_running (dev->net))
824 urb = usb_alloc_urb (0, GFP_KERNEL);
825 else
826 clear_bit (EVENT_RX_MEMORY, &dev->flags);
827 if (urb != NULL) {
828 clear_bit (EVENT_RX_MEMORY, &dev->flags);
829 rx_submit (dev, urb, GFP_KERNEL);
830 tasklet_schedule (&dev->bh);
831 }
832 }
833
David Hollis7ea13c92005-04-22 15:07:02 -0700834 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800835 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700836 int retval = 0;
837
838 clear_bit (EVENT_LINK_RESET, &dev->flags);
839 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
840 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
841 retval,
842 dev->udev->bus->bus_name, dev->udev->devpath,
843 info->description);
844 }
845 }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (dev->flags)
848 devdbg (dev, "kevent done, flags = 0x%lx",
849 dev->flags);
850}
851
852/*-------------------------------------------------------------------------*/
853
David Howells7d12e782006-10-05 14:55:46 +0100854static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct sk_buff *skb = (struct sk_buff *) urb->context;
857 struct skb_data *entry = (struct skb_data *) skb->cb;
858 struct usbnet *dev = entry->dev;
859
860 if (urb->status == 0) {
861 dev->stats.tx_packets++;
862 dev->stats.tx_bytes += entry->length;
863 } else {
864 dev->stats.tx_errors++;
865
866 switch (urb->status) {
867 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700868 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
870
871 /* software-driven interface shutdown */
872 case -ECONNRESET: // async unlink
873 case -ESHUTDOWN: // hardware gone
874 break;
875
876 // like rx, tx gets controller i/o faults during khubd delays
877 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700878 case -EPROTO:
879 case -ETIME:
880 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (!timer_pending (&dev->delay)) {
882 mod_timer (&dev->delay,
883 jiffies + THROTTLE_JIFFIES);
884 if (netif_msg_link (dev))
885 devdbg (dev, "tx throttle %d",
886 urb->status);
887 }
888 netif_stop_queue (dev->net);
889 break;
890 default:
891 if (netif_msg_tx_err (dev))
892 devdbg (dev, "tx err %d", entry->urb->status);
893 break;
894 }
895 }
896
897 urb->dev = NULL;
898 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700899 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*-------------------------------------------------------------------------*/
903
904static void usbnet_tx_timeout (struct net_device *net)
905{
906 struct usbnet *dev = netdev_priv(net);
907
908 unlink_urbs (dev, &dev->txq);
909 tasklet_schedule (&dev->bh);
910
911 // FIXME: device recovery -- reset?
912}
913
914/*-------------------------------------------------------------------------*/
915
916static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
917{
918 struct usbnet *dev = netdev_priv(net);
919 int length;
920 int retval = NET_XMIT_SUCCESS;
921 struct urb *urb = NULL;
922 struct skb_data *entry;
923 struct driver_info *info = dev->driver_info;
924 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 // some devices want funky USB-level framing, for
927 // win32 driver (usually) and/or hardware quirks
928 if (info->tx_fixup) {
929 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
930 if (!skb) {
931 if (netif_msg_tx_err (dev))
932 devdbg (dev, "can't tx_fixup skb");
933 goto drop;
934 }
935 }
936 length = skb->len;
937
938 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
939 if (netif_msg_tx_err (dev))
940 devdbg (dev, "no urb");
941 goto drop;
942 }
943
944 entry = (struct skb_data *) skb->cb;
945 entry->urb = urb;
946 entry->dev = dev;
947 entry->state = tx_start;
948 entry->length = length;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 usb_fill_bulk_urb (urb, dev->udev, dev->out,
951 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* don't assume the hardware handles USB_ZERO_PACKET
954 * NOTE: strictly conforming cdc-ether devices should expect
955 * the ZLP here, but ignore the one-byte packet.
956 *
957 * FIXME zero that byte, if it doesn't require a new skb.
958 */
959 if ((length % dev->maxpacket) == 0)
960 urb->transfer_buffer_length++;
961
962 spin_lock_irqsave (&dev->txq.lock, flags);
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
965 case -EPIPE:
966 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -0700967 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969 default:
970 if (netif_msg_tx_err (dev))
971 devdbg (dev, "tx: submit urb err %d", retval);
972 break;
973 case 0:
974 net->trans_start = jiffies;
975 __skb_queue_tail (&dev->txq, skb);
976 if (dev->txq.qlen >= TX_QLEN (dev))
977 netif_stop_queue (net);
978 }
979 spin_unlock_irqrestore (&dev->txq.lock, flags);
980
981 if (retval) {
982 if (netif_msg_tx_err (dev))
983 devdbg (dev, "drop, code %d", retval);
984drop:
985 retval = NET_XMIT_SUCCESS;
986 dev->stats.tx_dropped++;
987 if (skb)
988 dev_kfree_skb_any (skb);
989 usb_free_urb (urb);
990 } else if (netif_msg_tx_queued (dev)) {
991 devdbg (dev, "> tx, len %d, type 0x%x",
992 length, skb->protocol);
993 }
994 return retval;
995}
996
997
998/*-------------------------------------------------------------------------*/
999
1000// tasklet (work deferred from completions, in_irq) or timer
1001
1002static void usbnet_bh (unsigned long param)
1003{
1004 struct usbnet *dev = (struct usbnet *) param;
1005 struct sk_buff *skb;
1006 struct skb_data *entry;
1007
1008 while ((skb = skb_dequeue (&dev->done))) {
1009 entry = (struct skb_data *) skb->cb;
1010 switch (entry->state) {
1011 case rx_done:
1012 entry->state = rx_cleanup;
1013 rx_process (dev, skb);
1014 continue;
1015 case tx_done:
1016 case rx_cleanup:
1017 usb_free_urb (entry->urb);
1018 dev_kfree_skb (skb);
1019 continue;
1020 default:
1021 devdbg (dev, "bogus skb state %d", entry->state);
1022 }
1023 }
1024
1025 // waiting for all pending urbs to complete?
1026 if (dev->wait) {
1027 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1028 wake_up (dev->wait);
1029 }
1030
1031 // or are we maybe short a few urbs?
1032 } else if (netif_running (dev->net)
1033 && netif_device_present (dev->net)
1034 && !timer_pending (&dev->delay)
1035 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1036 int temp = dev->rxq.qlen;
1037 int qlen = RX_QLEN (dev);
1038
1039 if (temp < qlen) {
1040 struct urb *urb;
1041 int i;
1042
1043 // don't refill the queue all at once
1044 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1045 urb = usb_alloc_urb (0, GFP_ATOMIC);
1046 if (urb != NULL)
1047 rx_submit (dev, urb, GFP_ATOMIC);
1048 }
1049 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1050 devdbg (dev, "rxqlen %d --> %d",
1051 temp, dev->rxq.qlen);
1052 if (dev->rxq.qlen < qlen)
1053 tasklet_schedule (&dev->bh);
1054 }
1055 if (dev->txq.qlen < TX_QLEN (dev))
1056 netif_wake_queue (dev->net);
1057 }
1058}
1059
1060
1061
1062/*-------------------------------------------------------------------------
1063 *
1064 * USB Device Driver support
1065 *
1066 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068// precondition: never called in_interrupt
1069
David Brownell38bde1d2005-08-31 09:52:45 -07001070void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct usbnet *dev;
1073 struct usb_device *xdev;
1074 struct net_device *net;
1075
1076 dev = usb_get_intfdata(intf);
1077 usb_set_intfdata(intf, NULL);
1078 if (!dev)
1079 return;
1080
1081 xdev = interface_to_usbdev (intf);
1082
1083 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001084 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1085 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 xdev->bus->bus_name, xdev->devpath,
1087 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 net = dev->net;
1090 unregister_netdev (net);
1091
1092 /* we don't hold rtnl here ... */
1093 flush_scheduled_work ();
1094
1095 if (dev->driver_info->unbind)
1096 dev->driver_info->unbind (dev, intf);
1097
1098 free_netdev(net);
1099 usb_put_dev (xdev);
1100}
David Brownell38bde1d2005-08-31 09:52:45 -07001101EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103
1104/*-------------------------------------------------------------------------*/
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106// precondition: never called in_interrupt
1107
David Brownell38bde1d2005-08-31 09:52:45 -07001108int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1110{
1111 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001112 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct usb_host_interface *interface;
1114 struct driver_info *info;
1115 struct usb_device *xdev;
1116 int status;
David Brownell296c0242007-04-17 16:10:10 -07001117 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
David Brownell296c0242007-04-17 16:10:10 -07001119 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 info = (struct driver_info *) prod->driver_info;
1121 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001122 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -ENODEV;
1124 }
1125 xdev = interface_to_usbdev (udev);
1126 interface = udev->cur_altsetting;
1127
1128 usb_get_dev (xdev);
1129
1130 status = -ENOMEM;
1131
1132 // set up our own records
1133 net = alloc_etherdev(sizeof(*dev));
1134 if (!net) {
1135 dbg ("can't kmalloc dev");
1136 goto out;
1137 }
1138
1139 dev = netdev_priv(net);
1140 dev->udev = xdev;
1141 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001142 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1144 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1145 skb_queue_head_init (&dev->rxq);
1146 skb_queue_head_init (&dev->txq);
1147 skb_queue_head_init (&dev->done);
1148 dev->bh.func = usbnet_bh;
1149 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001150 INIT_WORK (&dev->kevent, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 dev->delay.function = usbnet_bh;
1152 dev->delay.data = (unsigned long) dev;
1153 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001154 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 SET_MODULE_OWNER (net);
1157 dev->net = net;
1158 strcpy (net->name, "usb%d");
1159 memcpy (net->dev_addr, node_id, sizeof node_id);
1160
David Brownell2e55cc72005-08-31 09:53:10 -07001161 /* rx and tx sides can use different message sizes;
1162 * bind() should set rx_urb_size in that case.
1163 */
1164 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#if 0
1166// dma_supported() is deeply broken on almost all architectures
1167 // possible with some EHCI controllers
1168 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1169 net->features |= NETIF_F_HIGHDMA;
1170#endif
1171
1172 net->change_mtu = usbnet_change_mtu;
1173 net->get_stats = usbnet_get_stats;
1174 net->hard_start_xmit = usbnet_start_xmit;
1175 net->open = usbnet_open;
1176 net->stop = usbnet_stop;
1177 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1178 net->tx_timeout = usbnet_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 net->ethtool_ops = &usbnet_ethtool_ops;
1180
1181 // allow device-specific bind/init procedures
1182 // NOTE net->name still not usable ...
1183 if (info->bind) {
1184 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001185 if (status < 0)
1186 goto out1;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 // heuristic: "usb%d" for links we know are two-host,
1189 // else "eth%d" when there's reasonable doubt. userspace
1190 // can rename the link if it knows better.
1191 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1192 && (net->dev_addr [0] & 0x02) == 0)
1193 strcpy (net->name, "eth%d");
David Brownellf29fc252005-08-31 09:52:31 -07001194
1195 /* maybe the remote can't receive an Ethernet MTU */
1196 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1197 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001198 } else if (!info->in || !info->out)
1199 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 else {
1201 dev->in = usb_rcvbulkpipe (xdev, info->in);
1202 dev->out = usb_sndbulkpipe (xdev, info->out);
1203 if (!(info->flags & FLAG_NO_SETINT))
1204 status = usb_set_interface (xdev,
1205 interface->desc.bInterfaceNumber,
1206 interface->desc.bAlternateSetting);
1207 else
1208 status = 0;
1209
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (status == 0 && dev->status)
1212 status = init_status (dev, udev);
1213 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001214 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
David Brownell2e55cc72005-08-31 09:53:10 -07001216 if (!dev->rx_urb_size)
1217 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 SET_NETDEV_DEV(net, &udev->dev);
1221 status = register_netdev (net);
1222 if (status)
1223 goto out3;
1224 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001225 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 "%02x:%02x:%02x:%02x:%02x:%02x",
David Brownell38bde1d2005-08-31 09:52:45 -07001227 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xdev->bus->bus_name, xdev->devpath,
1229 dev->driver_info->description,
1230 net->dev_addr [0], net->dev_addr [1],
1231 net->dev_addr [2], net->dev_addr [3],
1232 net->dev_addr [4], net->dev_addr [5]);
1233
1234 // ok, it's ready to go.
1235 usb_set_intfdata (udev, dev);
1236
1237 // start as if the link is up
1238 netif_device_attach (net);
1239
1240 return 0;
1241
1242out3:
1243 if (info->unbind)
1244 info->unbind (dev, udev);
1245out1:
1246 free_netdev(net);
1247out:
1248 usb_put_dev(xdev);
1249 return status;
1250}
David Brownell38bde1d2005-08-31 09:52:45 -07001251EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253/*-------------------------------------------------------------------------*/
1254
Oliver Neukum36433122007-04-30 01:37:44 -07001255/*
1256 * suspend the whole driver as soon as the first interface is suspended
1257 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
David Brownell38bde1d2005-08-31 09:52:45 -07001260int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001263
Oliver Neukum36433122007-04-30 01:37:44 -07001264 if (!dev->suspend_count++) {
1265 /* accelerate emptying of the rx and queues, to avoid
1266 * having everything error out.
1267 */
1268 netif_device_detach (dev->net);
1269 (void) unlink_urbs (dev, &dev->rxq);
1270 (void) unlink_urbs (dev, &dev->txq);
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 0;
1273}
David Brownell38bde1d2005-08-31 09:52:45 -07001274EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
David Brownell38bde1d2005-08-31 09:52:45 -07001276int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct usbnet *dev = usb_get_intfdata(intf);
1279
Oliver Neukum36433122007-04-30 01:37:44 -07001280 if (!--dev->suspend_count) {
1281 netif_device_attach (dev->net);
1282 tasklet_schedule (&dev->bh);
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
David Brownell38bde1d2005-08-31 09:52:45 -07001286EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/*-------------------------------------------------------------------------*/
1290
David Brownellf29fc252005-08-31 09:52:31 -07001291static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
David Brownell090ffa92005-08-31 09:54:50 -07001293 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001294 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
David Brownellf29fc252005-08-31 09:52:31 -07001300module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
David Brownellf29fc252005-08-31 09:52:31 -07001302static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
David Brownellf29fc252005-08-31 09:52:31 -07001305module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
David Brownellf29fc252005-08-31 09:52:31 -07001307MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001308MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001309MODULE_LICENSE("GPL");