]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/usb/usbnet.c
drivers/net: Move && and || to end of previous line
[net-next-2.6.git] / drivers / net / usb / usbnet.c
CommitLineData
1da177e4 1/*
090ffa9d 2 * USB Network driver infrastructure
f29fc259 3 * Copyright (C) 2000-2005 by David Brownell
1da177e4 4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
1da177e4
LT
5 *
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
090ffa9d
DB
23 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
1da177e4 25 *
090ffa9d
DB
26 * 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 */
1da177e4
LT
32
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
1da177e4 36#include <linux/module.h>
1da177e4
LT
37#include <linux/init.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
03ad032b 40#include <linux/ctype.h>
1da177e4
LT
41#include <linux/ethtool.h>
42#include <linux/workqueue.h>
43#include <linux/mii.h>
1da177e4 44#include <linux/usb.h>
3692e94f 45#include <linux/usb/usbnet.h>
f29fc259
DB
46
47#define DRIVER_VERSION "22-Aug-2005"
1da177e4
LT
48
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).
f29fc259
DB
58 *
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.
1da177e4 62 */
a99c1949
JP
63#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)
1da177e4 68
1da177e4
LT
69// 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
1da177e4
LT
76// between wakeups
77#define UNLINK_TIMEOUT_MS 3
78
79/*-------------------------------------------------------------------------*/
80
81// randomly generated ethernet address
82static u8 node_id [ETH_ALEN];
83
1da177e4
LT
84static 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
1da177e4
LT
91/*-------------------------------------------------------------------------*/
92
1da177e4 93/* handles CDC Ethernet and many other network "bulk data" interfaces */
2e55cc72 94int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
1da177e4
LT
95{
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:
fc6e2544 118 if (!usb_endpoint_dir_in(&e->desc))
1da177e4
LT
119 continue;
120 intr = 1;
121 /* FALLTHROUGH */
122 case USB_ENDPOINT_XFER_BULK:
123 break;
124 default:
125 continue;
126 }
fc6e2544 127 if (usb_endpoint_dir_in(&e->desc)) {
1da177e4
LT
128 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
8e95a202
JP
143 if (alt->desc.bAlternateSetting != 0 ||
144 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1da177e4
LT
145 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146 alt->desc.bAlternateSetting);
147 if (tmp < 0)
148 return tmp;
149 }
cb1cebbe 150
1da177e4
LT
151 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}
2e55cc72 158EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1da177e4 159
03ad032b
PH
160static u8 nibble(unsigned char c)
161{
162 if (likely(isdigit(c)))
163 return c - '0';
164 c = toupper(c);
165 if (likely(isxdigit(c)))
166 return 10 + c - 'A';
167 return 0;
168}
169
170int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
171{
172 int tmp, i;
173 unsigned char buf [13];
174
175 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
176 if (tmp != 12) {
177 dev_dbg(&dev->udev->dev,
178 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
179 if (tmp >= 0)
180 tmp = -EINVAL;
181 return tmp;
182 }
183 for (i = tmp = 0; i < 6; i++, tmp += 2)
184 dev->net->dev_addr [i] =
185 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
186 return 0;
187}
188EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
189
7d12e780 190static void intr_complete (struct urb *urb);
1da177e4
LT
191
192static int init_status (struct usbnet *dev, struct usb_interface *intf)
193{
194 char *buf = NULL;
195 unsigned pipe = 0;
196 unsigned maxp;
197 unsigned period;
198
199 if (!dev->driver_info->status)
200 return 0;
201
202 pipe = usb_rcvintpipe (dev->udev,
203 dev->status->desc.bEndpointAddress
204 & USB_ENDPOINT_NUMBER_MASK);
205 maxp = usb_maxpacket (dev->udev, pipe, 0);
206
207 /* avoid 1 msec chatter: min 8 msec poll rate */
208 period = max ((int) dev->status->desc.bInterval,
209 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
210
e94b1766 211 buf = kmalloc (maxp, GFP_KERNEL);
1da177e4 212 if (buf) {
e94b1766 213 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
1da177e4
LT
214 if (!dev->interrupt) {
215 kfree (buf);
216 return -ENOMEM;
217 } else {
218 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
219 buf, maxp, intr_complete, dev, period);
220 dev_dbg(&intf->dev,
221 "status ep%din, %d bytes period %d\n",
222 usb_pipeendpoint(pipe), maxp, period);
223 }
224 }
18ab458f 225 return 0;
1da177e4
LT
226}
227
2e55cc72
DB
228/* Passes this packet up the stack, updating its accounting.
229 * Some link protocols batch packets, so their rx_fixup paths
230 * can return clones as well as just modify the original skb.
231 */
232void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
1da177e4
LT
233{
234 int status;
235
7834ddbc
JK
236 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
237 skb_queue_tail(&dev->rxq_pause, skb);
238 return;
239 }
240
1da177e4 241 skb->protocol = eth_type_trans (skb, dev->net);
7963837f
HX
242 dev->net->stats.rx_packets++;
243 dev->net->stats.rx_bytes += skb->len;
1da177e4
LT
244
245 if (netif_msg_rx_status (dev))
5330e927 246 devdbg (dev, "< rx, len %zu, type 0x%x",
1da177e4
LT
247 skb->len + sizeof (struct ethhdr), skb->protocol);
248 memset (skb->cb, 0, sizeof (struct skb_data));
249 status = netif_rx (skb);
250 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
251 devdbg (dev, "netif_rx status %d", status);
252}
2e55cc72 253EXPORT_SYMBOL_GPL(usbnet_skb_return);
1da177e4 254
1da177e4
LT
255\f
256/*-------------------------------------------------------------------------
257 *
258 * Network Device Driver (peer link to "Host Device", from USB host)
259 *
260 *-------------------------------------------------------------------------*/
261
777baa47 262int usbnet_change_mtu (struct net_device *net, int new_mtu)
1da177e4
LT
263{
264 struct usbnet *dev = netdev_priv(net);
f29fc259 265 int ll_mtu = new_mtu + net->hard_header_len;
a99c1949
JP
266 int old_hard_mtu = dev->hard_mtu;
267 int old_rx_urb_size = dev->rx_urb_size;
1da177e4 268
a99c1949 269 if (new_mtu <= 0)
1da177e4 270 return -EINVAL;
1da177e4 271 // no second zero-length packet read wanted after mtu-sized packets
f29fc259 272 if ((ll_mtu % dev->maxpacket) == 0)
1da177e4
LT
273 return -EDOM;
274 net->mtu = new_mtu;
a99c1949
JP
275
276 dev->hard_mtu = net->mtu + net->hard_header_len;
277 if (dev->rx_urb_size == old_hard_mtu) {
278 dev->rx_urb_size = dev->hard_mtu;
279 if (dev->rx_urb_size > old_rx_urb_size)
280 usbnet_unlink_rx_urbs(dev);
281 }
282
1da177e4
LT
283 return 0;
284}
777baa47 285EXPORT_SYMBOL_GPL(usbnet_change_mtu);
1da177e4
LT
286
287/*-------------------------------------------------------------------------*/
288
1da177e4
LT
289/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
290 * completion callbacks. 2.5 should have fixed those bugs...
291 */
292
8728b834 293static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
1da177e4 294{
1da177e4
LT
295 unsigned long flags;
296
8728b834
DM
297 spin_lock_irqsave(&list->lock, flags);
298 __skb_unlink(skb, list);
299 spin_unlock(&list->lock);
300 spin_lock(&dev->done.lock);
301 __skb_queue_tail(&dev->done, skb);
1da177e4 302 if (dev->done.qlen == 1)
8728b834
DM
303 tasklet_schedule(&dev->bh);
304 spin_unlock_irqrestore(&dev->done.lock, flags);
1da177e4
LT
305}
306
307/* some work can't be done in tasklets, so we use keventd
308 *
309 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
310 * but tasklet_schedule() doesn't. hope the failure is rare.
311 */
2e55cc72 312void usbnet_defer_kevent (struct usbnet *dev, int work)
1da177e4
LT
313{
314 set_bit (work, &dev->flags);
315 if (!schedule_work (&dev->kevent))
316 deverr (dev, "kevent %d may have been dropped", work);
317 else
318 devdbg (dev, "kevent %d scheduled", work);
319}
2e55cc72 320EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1da177e4
LT
321
322/*-------------------------------------------------------------------------*/
323
7d12e780 324static void rx_complete (struct urb *urb);
1da177e4 325
55016f10 326static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
1da177e4
LT
327{
328 struct sk_buff *skb;
329 struct skb_data *entry;
330 int retval = 0;
331 unsigned long lockflags;
2e55cc72 332 size_t size = dev->rx_urb_size;
1da177e4 333
1da177e4
LT
334 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
335 if (netif_msg_rx_err (dev))
336 devdbg (dev, "no rx skb");
2e55cc72 337 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
338 usb_free_urb (urb);
339 return;
340 }
341 skb_reserve (skb, NET_IP_ALIGN);
342
343 entry = (struct skb_data *) skb->cb;
344 entry->urb = urb;
345 entry->dev = dev;
346 entry->state = rx_start;
347 entry->length = 0;
348
349 usb_fill_bulk_urb (urb, dev->udev, dev->in,
350 skb->data, size, rx_complete, skb);
1da177e4
LT
351
352 spin_lock_irqsave (&dev->rxq.lock, lockflags);
353
8e95a202
JP
354 if (netif_running (dev->net) &&
355 netif_device_present (dev->net) &&
356 !test_bit (EVENT_RX_HALT, &dev->flags)) {
18ab458f 357 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
1da177e4 358 case -EPIPE:
2e55cc72 359 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
360 break;
361 case -ENOMEM:
2e55cc72 362 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
363 break;
364 case -ENODEV:
365 if (netif_msg_ifdown (dev))
366 devdbg (dev, "device gone");
367 netif_device_detach (dev->net);
368 break;
369 default:
370 if (netif_msg_rx_err (dev))
371 devdbg (dev, "rx submit, %d", retval);
372 tasklet_schedule (&dev->bh);
373 break;
374 case 0:
375 __skb_queue_tail (&dev->rxq, skb);
376 }
377 } else {
378 if (netif_msg_ifdown (dev))
379 devdbg (dev, "rx: stopped");
380 retval = -ENOLINK;
381 }
382 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
383 if (retval) {
384 dev_kfree_skb_any (skb);
385 usb_free_urb (urb);
386 }
387}
388
389
390/*-------------------------------------------------------------------------*/
391
392static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
393{
8e95a202
JP
394 if (dev->driver_info->rx_fixup &&
395 !dev->driver_info->rx_fixup (dev, skb))
1da177e4
LT
396 goto error;
397 // else network stack removes extra byte if we forced a short packet
398
399 if (skb->len)
2e55cc72 400 usbnet_skb_return (dev, skb);
1da177e4
LT
401 else {
402 if (netif_msg_rx_err (dev))
403 devdbg (dev, "drop");
404error:
7963837f 405 dev->net->stats.rx_errors++;
1da177e4
LT
406 skb_queue_tail (&dev->done, skb);
407 }
408}
409
410/*-------------------------------------------------------------------------*/
411
7d12e780 412static void rx_complete (struct urb *urb)
1da177e4
LT
413{
414 struct sk_buff *skb = (struct sk_buff *) urb->context;
415 struct skb_data *entry = (struct skb_data *) skb->cb;
416 struct usbnet *dev = entry->dev;
417 int urb_status = urb->status;
418
419 skb_put (skb, urb->actual_length);
420 entry->state = rx_done;
421 entry->urb = NULL;
422
423 switch (urb_status) {
18ab458f
DB
424 /* success */
425 case 0:
f29fc259 426 if (skb->len < dev->net->hard_header_len) {
1da177e4 427 entry->state = rx_cleanup;
7963837f
HX
428 dev->net->stats.rx_errors++;
429 dev->net->stats.rx_length_errors++;
1da177e4
LT
430 if (netif_msg_rx_err (dev))
431 devdbg (dev, "rx length %d", skb->len);
432 }
433 break;
434
18ab458f
DB
435 /* stalls need manual reset. this is rare ... except that
436 * when going through USB 2.0 TTs, unplug appears this way.
3ac49a1c 437 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
18ab458f
DB
438 * storm, recovering as needed.
439 */
440 case -EPIPE:
7963837f 441 dev->net->stats.rx_errors++;
2e55cc72 442 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
443 // FALLTHROUGH
444
18ab458f
DB
445 /* software-driven interface shutdown */
446 case -ECONNRESET: /* async unlink */
447 case -ESHUTDOWN: /* hardware gone */
1da177e4
LT
448 if (netif_msg_ifdown (dev))
449 devdbg (dev, "rx shutdown, code %d", urb_status);
450 goto block;
451
18ab458f
DB
452 /* we get controller i/o faults during khubd disconnect() delays.
453 * throttle down resubmits, to avoid log floods; just temporarily,
454 * so we still recover when the fault isn't a khubd delay.
455 */
456 case -EPROTO:
457 case -ETIME:
458 case -EILSEQ:
7963837f 459 dev->net->stats.rx_errors++;
1da177e4
LT
460 if (!timer_pending (&dev->delay)) {
461 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
462 if (netif_msg_link (dev))
463 devdbg (dev, "rx throttle %d", urb_status);
464 }
465block:
466 entry->state = rx_cleanup;
467 entry->urb = urb;
468 urb = NULL;
469 break;
470
18ab458f
DB
471 /* data overrun ... flush fifo? */
472 case -EOVERFLOW:
7963837f 473 dev->net->stats.rx_over_errors++;
1da177e4 474 // FALLTHROUGH
cb1cebbe 475
18ab458f 476 default:
1da177e4 477 entry->state = rx_cleanup;
7963837f 478 dev->net->stats.rx_errors++;
1da177e4
LT
479 if (netif_msg_rx_err (dev))
480 devdbg (dev, "rx status %d", urb_status);
481 break;
482 }
483
8728b834 484 defer_bh(dev, skb, &dev->rxq);
1da177e4
LT
485
486 if (urb) {
8e95a202
JP
487 if (netif_running (dev->net) &&
488 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1da177e4
LT
489 rx_submit (dev, urb, GFP_ATOMIC);
490 return;
491 }
492 usb_free_urb (urb);
493 }
494 if (netif_msg_rx_err (dev))
495 devdbg (dev, "no read resubmitted");
496}
497
7d12e780 498static void intr_complete (struct urb *urb)
1da177e4
LT
499{
500 struct usbnet *dev = urb->context;
501 int status = urb->status;
502
503 switch (status) {
18ab458f
DB
504 /* success */
505 case 0:
1da177e4
LT
506 dev->driver_info->status(dev, urb);
507 break;
508
18ab458f
DB
509 /* software-driven interface shutdown */
510 case -ENOENT: /* urb killed */
511 case -ESHUTDOWN: /* hardware gone */
1da177e4
LT
512 if (netif_msg_ifdown (dev))
513 devdbg (dev, "intr shutdown, code %d", status);
514 return;
515
18ab458f
DB
516 /* NOTE: not throttling like RX/TX, since this endpoint
517 * already polls infrequently
518 */
519 default:
1da177e4
LT
520 devdbg (dev, "intr status %d", status);
521 break;
522 }
523
524 if (!netif_running (dev->net))
525 return;
526
527 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
528 status = usb_submit_urb (urb, GFP_ATOMIC);
529 if (status != 0 && netif_msg_timer (dev))
530 deverr(dev, "intr resubmit --> %d", status);
531}
532
7834ddbc
JK
533/*-------------------------------------------------------------------------*/
534void usbnet_pause_rx(struct usbnet *dev)
535{
536 set_bit(EVENT_RX_PAUSED, &dev->flags);
537
538 if (netif_msg_rx_status(dev))
539 devdbg(dev, "paused rx queue enabled");
540}
541EXPORT_SYMBOL_GPL(usbnet_pause_rx);
542
543void usbnet_resume_rx(struct usbnet *dev)
544{
545 struct sk_buff *skb;
546 int num = 0;
547
548 clear_bit(EVENT_RX_PAUSED, &dev->flags);
549
550 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
551 usbnet_skb_return(dev, skb);
552 num++;
553 }
554
555 tasklet_schedule(&dev->bh);
556
557 if (netif_msg_rx_status(dev))
558 devdbg(dev, "paused rx queue disabled, %d skbs requeued", num);
559}
560EXPORT_SYMBOL_GPL(usbnet_resume_rx);
561
562void usbnet_purge_paused_rxq(struct usbnet *dev)
563{
564 skb_queue_purge(&dev->rxq_pause);
565}
566EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
567
1da177e4
LT
568/*-------------------------------------------------------------------------*/
569
570// unlink pending rx/tx; completion handlers do all other cleanup
571
572static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
573{
574 unsigned long flags;
575 struct sk_buff *skb, *skbnext;
576 int count = 0;
577
578 spin_lock_irqsave (&q->lock, flags);
83bfba5f 579 skb_queue_walk_safe(q, skb, skbnext) {
1da177e4
LT
580 struct skb_data *entry;
581 struct urb *urb;
582 int retval;
583
584 entry = (struct skb_data *) skb->cb;
585 urb = entry->urb;
1da177e4
LT
586
587 // during some PM-driven resume scenarios,
588 // these (async) unlinks complete immediately
589 retval = usb_unlink_urb (urb);
590 if (retval != -EINPROGRESS && retval != 0)
591 devdbg (dev, "unlink urb err, %d", retval);
592 else
593 count++;
594 }
595 spin_unlock_irqrestore (&q->lock, flags);
596 return count;
597}
598
a99c1949
JP
599// Flush all pending rx urbs
600// minidrivers may need to do this when the MTU changes
601
602void usbnet_unlink_rx_urbs(struct usbnet *dev)
603{
604 if (netif_running(dev->net)) {
605 (void) unlink_urbs (dev, &dev->rxq);
606 tasklet_schedule(&dev->bh);
607 }
608}
609EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
1da177e4
LT
610
611/*-------------------------------------------------------------------------*/
612
613// precondition: never called in_interrupt
614
777baa47 615int usbnet_stop (struct net_device *net)
1da177e4
LT
616{
617 struct usbnet *dev = netdev_priv(net);
a33e9e7f 618 struct driver_info *info = dev->driver_info;
1da177e4 619 int temp;
a33e9e7f 620 int retval;
7259f0d0 621 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
1da177e4
LT
622 DECLARE_WAITQUEUE (wait, current);
623
624 netif_stop_queue (net);
625
626 if (netif_msg_ifdown (dev))
627 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
7963837f
HX
628 net->stats.rx_packets, net->stats.tx_packets,
629 net->stats.rx_errors, net->stats.tx_errors
1da177e4
LT
630 );
631
a33e9e7f
JK
632 /* allow minidriver to stop correctly (wireless devices to turn off
633 * radio etc) */
634 if (info->stop) {
635 retval = info->stop(dev);
636 if (retval < 0 && netif_msg_ifdown(dev))
637 devinfo(dev,
638 "stop fail (%d) usbnet usb-%s-%s, %s",
639 retval,
640 dev->udev->bus->bus_name, dev->udev->devpath,
641 info->description);
642 }
643
1487cd5e
JK
644 if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) {
645 /* ensure there are no more active urbs */
646 add_wait_queue(&unlink_wakeup, &wait);
647 dev->wait = &unlink_wakeup;
648 temp = unlink_urbs(dev, &dev->txq) +
649 unlink_urbs(dev, &dev->rxq);
650
651 /* maybe wait for deletions to finish. */
8e95a202
JP
652 while (!skb_queue_empty(&dev->rxq) &&
653 !skb_queue_empty(&dev->txq) &&
654 !skb_queue_empty(&dev->done)) {
1487cd5e
JK
655 msleep(UNLINK_TIMEOUT_MS);
656 if (netif_msg_ifdown(dev))
657 devdbg(dev, "waited for %d urb completions",
658 temp);
659 }
660 dev->wait = NULL;
661 remove_wait_queue(&unlink_wakeup, &wait);
1da177e4 662 }
1da177e4
LT
663
664 usb_kill_urb(dev->interrupt);
665
7834ddbc
JK
666 usbnet_purge_paused_rxq(dev);
667
1da177e4
LT
668 /* deferred work (task, timer, softirq) must also stop.
669 * can't flush_scheduled_work() until we drop rtnl (later),
670 * else workers could deadlock; so make workers a NOP.
671 */
672 dev->flags = 0;
673 del_timer_sync (&dev->delay);
674 tasklet_kill (&dev->bh);
a11a6544 675 usb_autopm_put_interface(dev->intf);
1da177e4
LT
676
677 return 0;
678}
777baa47 679EXPORT_SYMBOL_GPL(usbnet_stop);
1da177e4
LT
680
681/*-------------------------------------------------------------------------*/
682
683// posts reads, and enables write queuing
684
685// precondition: never called in_interrupt
686
777baa47 687int usbnet_open (struct net_device *net)
1da177e4
LT
688{
689 struct usbnet *dev = netdev_priv(net);
a11a6544 690 int retval;
1da177e4
LT
691 struct driver_info *info = dev->driver_info;
692
a11a6544
ON
693 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
694 if (netif_msg_ifup (dev))
695 devinfo (dev,
696 "resumption fail (%d) usbnet usb-%s-%s, %s",
697 retval,
698 dev->udev->bus->bus_name, dev->udev->devpath,
699 info->description);
700 goto done_nopm;
701 }
702
1da177e4
LT
703 // put into "known safe" state
704 if (info->reset && (retval = info->reset (dev)) < 0) {
705 if (netif_msg_ifup (dev))
706 devinfo (dev,
707 "open reset fail (%d) usbnet usb-%s-%s, %s",
708 retval,
709 dev->udev->bus->bus_name, dev->udev->devpath,
710 info->description);
711 goto done;
712 }
713
714 // insist peer be connected
715 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
716 if (netif_msg_ifup (dev))
717 devdbg (dev, "can't open; %d", retval);
718 goto done;
719 }
720
721 /* start any status interrupt transfer */
722 if (dev->interrupt) {
723 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
724 if (retval < 0) {
725 if (netif_msg_ifup (dev))
726 deverr (dev, "intr submit %d", retval);
727 goto done;
728 }
729 }
730
731 netif_start_queue (net);
732 if (netif_msg_ifup (dev)) {
733 char *framing;
734
735 if (dev->driver_info->flags & FLAG_FRAMING_NC)
736 framing = "NetChip";
737 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
738 framing = "GeneSys";
739 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
740 framing = "Zaurus";
741 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
742 framing = "RNDIS";
743 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
744 framing = "ASIX";
745 else
746 framing = "simple";
747
748 devinfo (dev, "open: enable queueing "
749 "(rx %d, tx %d) mtu %d %s framing",
25d94e68 750 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
1da177e4
LT
751 framing);
752 }
753
754 // delay posting reads until we're fully open
755 tasklet_schedule (&dev->bh);
a11a6544 756 return retval;
1da177e4 757done:
a11a6544
ON
758 usb_autopm_put_interface(dev->intf);
759done_nopm:
1da177e4
LT
760 return retval;
761}
777baa47 762EXPORT_SYMBOL_GPL(usbnet_open);
1da177e4
LT
763
764/*-------------------------------------------------------------------------*/
765
2e55cc72
DB
766/* ethtool methods; minidrivers may need to add some more, but
767 * they'll probably want to use this base set.
768 */
769
c41286fd
AB
770int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
771{
772 struct usbnet *dev = netdev_priv(net);
773
774 if (!dev->mii.mdio_read)
775 return -EOPNOTSUPP;
776
777 return mii_ethtool_gset(&dev->mii, cmd);
778}
779EXPORT_SYMBOL_GPL(usbnet_get_settings);
780
781int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
782{
783 struct usbnet *dev = netdev_priv(net);
784 int retval;
785
786 if (!dev->mii.mdio_write)
787 return -EOPNOTSUPP;
788
789 retval = mii_ethtool_sset(&dev->mii, cmd);
790
791 /* link speed/duplex might have changed */
792 if (dev->driver_info->link_reset)
793 dev->driver_info->link_reset(dev);
794
795 return retval;
796
797}
798EXPORT_SYMBOL_GPL(usbnet_set_settings);
799
c41286fd 800u32 usbnet_get_link (struct net_device *net)
1da177e4
LT
801{
802 struct usbnet *dev = netdev_priv(net);
803
f29fc259 804 /* If a check_connect is defined, return its result */
1da177e4
LT
805 if (dev->driver_info->check_connect)
806 return dev->driver_info->check_connect (dev) == 0;
807
c41286fd
AB
808 /* if the device has mii operations, use those */
809 if (dev->mii.mdio_read)
810 return mii_link_ok(&dev->mii);
811
05ffb3e2
BM
812 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
813 return ethtool_op_get_link(net);
1da177e4 814}
c41286fd 815EXPORT_SYMBOL_GPL(usbnet_get_link);
1da177e4 816
18ee91fa 817int usbnet_nway_reset(struct net_device *net)
1da177e4
LT
818{
819 struct usbnet *dev = netdev_priv(net);
820
18ee91fa
DB
821 if (!dev->mii.mdio_write)
822 return -EOPNOTSUPP;
823
824 return mii_nway_restart(&dev->mii);
1da177e4 825}
18ee91fa 826EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1da177e4 827
18ee91fa 828void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1da177e4
LT
829{
830 struct usbnet *dev = netdev_priv(net);
831
296c0242 832 strncpy (info->driver, dev->driver_name, sizeof info->driver);
18ee91fa
DB
833 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
834 strncpy (info->fw_version, dev->driver_info->description,
835 sizeof info->fw_version);
836 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1da177e4 837}
18ee91fa 838EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1da177e4 839
18ee91fa 840u32 usbnet_get_msglevel (struct net_device *net)
c41286fd
AB
841{
842 struct usbnet *dev = netdev_priv(net);
843
18ee91fa
DB
844 return dev->msg_enable;
845}
846EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
c41286fd 847
18ee91fa
DB
848void usbnet_set_msglevel (struct net_device *net, u32 level)
849{
850 struct usbnet *dev = netdev_priv(net);
851
852 dev->msg_enable = level;
c41286fd 853}
18ee91fa 854EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
c41286fd 855
2e55cc72 856/* drivers may override default ethtool_ops in their bind() routine */
0fc0b732 857static const struct ethtool_ops usbnet_ethtool_ops = {
c41286fd
AB
858 .get_settings = usbnet_get_settings,
859 .set_settings = usbnet_set_settings,
2e55cc72 860 .get_link = usbnet_get_link,
c41286fd 861 .nway_reset = usbnet_nway_reset,
18ee91fa 862 .get_drvinfo = usbnet_get_drvinfo,
2e55cc72
DB
863 .get_msglevel = usbnet_get_msglevel,
864 .set_msglevel = usbnet_set_msglevel,
865};
1da177e4
LT
866
867/*-------------------------------------------------------------------------*/
868
869/* work that cannot be done in interrupt context uses keventd.
870 *
871 * NOTE: with 2.5 we could do more of this using completion callbacks,
872 * especially now that control transfers can be queued.
873 */
874static void
c4028958 875kevent (struct work_struct *work)
1da177e4 876{
c4028958
DH
877 struct usbnet *dev =
878 container_of(work, struct usbnet, kevent);
1da177e4
LT
879 int status;
880
881 /* usb_clear_halt() needs a thread context */
882 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
883 unlink_urbs (dev, &dev->txq);
884 status = usb_clear_halt (dev->udev, dev->out);
8e95a202
JP
885 if (status < 0 &&
886 status != -EPIPE &&
887 status != -ESHUTDOWN) {
1da177e4
LT
888 if (netif_msg_tx_err (dev))
889 deverr (dev, "can't clear tx halt, status %d",
890 status);
891 } else {
892 clear_bit (EVENT_TX_HALT, &dev->flags);
f29fc259
DB
893 if (status != -ESHUTDOWN)
894 netif_wake_queue (dev->net);
1da177e4
LT
895 }
896 }
897 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
898 unlink_urbs (dev, &dev->rxq);
899 status = usb_clear_halt (dev->udev, dev->in);
8e95a202
JP
900 if (status < 0 &&
901 status != -EPIPE &&
902 status != -ESHUTDOWN) {
1da177e4
LT
903 if (netif_msg_rx_err (dev))
904 deverr (dev, "can't clear rx halt, status %d",
905 status);
906 } else {
907 clear_bit (EVENT_RX_HALT, &dev->flags);
908 tasklet_schedule (&dev->bh);
909 }
910 }
911
912 /* tasklet could resubmit itself forever if memory is tight */
913 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
914 struct urb *urb = NULL;
915
916 if (netif_running (dev->net))
917 urb = usb_alloc_urb (0, GFP_KERNEL);
918 else
919 clear_bit (EVENT_RX_MEMORY, &dev->flags);
920 if (urb != NULL) {
921 clear_bit (EVENT_RX_MEMORY, &dev->flags);
922 rx_submit (dev, urb, GFP_KERNEL);
923 tasklet_schedule (&dev->bh);
924 }
925 }
926
7ea13c9c 927 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
cb1cebbe 928 struct driver_info *info = dev->driver_info;
7ea13c9c
DH
929 int retval = 0;
930
931 clear_bit (EVENT_LINK_RESET, &dev->flags);
932 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
933 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
934 retval,
935 dev->udev->bus->bus_name, dev->udev->devpath,
936 info->description);
937 }
938 }
939
1da177e4
LT
940 if (dev->flags)
941 devdbg (dev, "kevent done, flags = 0x%lx",
942 dev->flags);
943}
944
945/*-------------------------------------------------------------------------*/
946
7d12e780 947static void tx_complete (struct urb *urb)
1da177e4
LT
948{
949 struct sk_buff *skb = (struct sk_buff *) urb->context;
950 struct skb_data *entry = (struct skb_data *) skb->cb;
951 struct usbnet *dev = entry->dev;
952
953 if (urb->status == 0) {
7963837f
HX
954 dev->net->stats.tx_packets++;
955 dev->net->stats.tx_bytes += entry->length;
1da177e4 956 } else {
7963837f 957 dev->net->stats.tx_errors++;
1da177e4
LT
958
959 switch (urb->status) {
960 case -EPIPE:
2e55cc72 961 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
962 break;
963
964 /* software-driven interface shutdown */
965 case -ECONNRESET: // async unlink
966 case -ESHUTDOWN: // hardware gone
967 break;
968
969 // like rx, tx gets controller i/o faults during khubd delays
970 // and so it uses the same throttling mechanism.
38e2bfc9
PZ
971 case -EPROTO:
972 case -ETIME:
973 case -EILSEQ:
1da177e4
LT
974 if (!timer_pending (&dev->delay)) {
975 mod_timer (&dev->delay,
976 jiffies + THROTTLE_JIFFIES);
977 if (netif_msg_link (dev))
978 devdbg (dev, "tx throttle %d",
979 urb->status);
980 }
981 netif_stop_queue (dev->net);
982 break;
983 default:
984 if (netif_msg_tx_err (dev))
985 devdbg (dev, "tx err %d", entry->urb->status);
986 break;
987 }
988 }
989
990 urb->dev = NULL;
991 entry->state = tx_done;
8728b834 992 defer_bh(dev, skb, &dev->txq);
1da177e4
LT
993}
994
995/*-------------------------------------------------------------------------*/
996
777baa47 997void usbnet_tx_timeout (struct net_device *net)
1da177e4
LT
998{
999 struct usbnet *dev = netdev_priv(net);
1000
1001 unlink_urbs (dev, &dev->txq);
1002 tasklet_schedule (&dev->bh);
1003
1004 // FIXME: device recovery -- reset?
1005}
777baa47 1006EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1da177e4
LT
1007
1008/*-------------------------------------------------------------------------*/
1009
25a79c41
SH
1010netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1011 struct net_device *net)
1da177e4
LT
1012{
1013 struct usbnet *dev = netdev_priv(net);
1014 int length;
1da177e4
LT
1015 struct urb *urb = NULL;
1016 struct skb_data *entry;
1017 struct driver_info *info = dev->driver_info;
1018 unsigned long flags;
25a79c41 1019 int retval;
1da177e4
LT
1020
1021 // some devices want funky USB-level framing, for
1022 // win32 driver (usually) and/or hardware quirks
1023 if (info->tx_fixup) {
1024 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1025 if (!skb) {
1026 if (netif_msg_tx_err (dev))
1027 devdbg (dev, "can't tx_fixup skb");
1028 goto drop;
1029 }
1030 }
1031 length = skb->len;
1032
1033 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1034 if (netif_msg_tx_err (dev))
1035 devdbg (dev, "no urb");
1036 goto drop;
1037 }
1038
1039 entry = (struct skb_data *) skb->cb;
1040 entry->urb = urb;
1041 entry->dev = dev;
1042 entry->state = tx_start;
1043 entry->length = length;
1044
1da177e4
LT
1045 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1046 skb->data, skb->len, tx_complete, skb);
1da177e4
LT
1047
1048 /* don't assume the hardware handles USB_ZERO_PACKET
1049 * NOTE: strictly conforming cdc-ether devices should expect
1050 * the ZLP here, but ignore the one-byte packet.
1da177e4 1051 */
ec475623 1052 if (!(info->flags & FLAG_SEND_ZLP) && (length % dev->maxpacket) == 0) {
1da177e4 1053 urb->transfer_buffer_length++;
3e323f3e
PK
1054 if (skb_tailroom(skb)) {
1055 skb->data[skb->len] = 0;
1056 __skb_put(skb, 1);
1057 }
1058 }
1da177e4
LT
1059
1060 spin_lock_irqsave (&dev->txq.lock, flags);
1061
1da177e4
LT
1062 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1063 case -EPIPE:
1064 netif_stop_queue (net);
2e55cc72 1065 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
1066 break;
1067 default:
1068 if (netif_msg_tx_err (dev))
1069 devdbg (dev, "tx: submit urb err %d", retval);
1070 break;
1071 case 0:
1072 net->trans_start = jiffies;
1073 __skb_queue_tail (&dev->txq, skb);
1074 if (dev->txq.qlen >= TX_QLEN (dev))
1075 netif_stop_queue (net);
1076 }
1077 spin_unlock_irqrestore (&dev->txq.lock, flags);
1078
1079 if (retval) {
1080 if (netif_msg_tx_err (dev))
1081 devdbg (dev, "drop, code %d", retval);
1082drop:
7963837f 1083 dev->net->stats.tx_dropped++;
1da177e4
LT
1084 if (skb)
1085 dev_kfree_skb_any (skb);
1086 usb_free_urb (urb);
1087 } else if (netif_msg_tx_queued (dev)) {
1088 devdbg (dev, "> tx, len %d, type 0x%x",
1089 length, skb->protocol);
1090 }
25a79c41 1091 return NETDEV_TX_OK;
1da177e4 1092}
777baa47 1093EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1da177e4
LT
1094
1095/*-------------------------------------------------------------------------*/
1096
1097// tasklet (work deferred from completions, in_irq) or timer
1098
1099static void usbnet_bh (unsigned long param)
1100{
1101 struct usbnet *dev = (struct usbnet *) param;
1102 struct sk_buff *skb;
1103 struct skb_data *entry;
1104
1105 while ((skb = skb_dequeue (&dev->done))) {
1106 entry = (struct skb_data *) skb->cb;
1107 switch (entry->state) {
18ab458f 1108 case rx_done:
1da177e4
LT
1109 entry->state = rx_cleanup;
1110 rx_process (dev, skb);
1111 continue;
18ab458f
DB
1112 case tx_done:
1113 case rx_cleanup:
1da177e4
LT
1114 usb_free_urb (entry->urb);
1115 dev_kfree_skb (skb);
1116 continue;
18ab458f 1117 default:
1da177e4
LT
1118 devdbg (dev, "bogus skb state %d", entry->state);
1119 }
1120 }
1121
1122 // waiting for all pending urbs to complete?
1123 if (dev->wait) {
1124 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1125 wake_up (dev->wait);
1126 }
1127
1128 // or are we maybe short a few urbs?
8e95a202
JP
1129 } else if (netif_running (dev->net) &&
1130 netif_device_present (dev->net) &&
1131 !timer_pending (&dev->delay) &&
1132 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1da177e4
LT
1133 int temp = dev->rxq.qlen;
1134 int qlen = RX_QLEN (dev);
1135
1136 if (temp < qlen) {
1137 struct urb *urb;
1138 int i;
1139
1140 // don't refill the queue all at once
1141 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1142 urb = usb_alloc_urb (0, GFP_ATOMIC);
1143 if (urb != NULL)
1144 rx_submit (dev, urb, GFP_ATOMIC);
1145 }
1146 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1147 devdbg (dev, "rxqlen %d --> %d",
1148 temp, dev->rxq.qlen);
1149 if (dev->rxq.qlen < qlen)
1150 tasklet_schedule (&dev->bh);
1151 }
1152 if (dev->txq.qlen < TX_QLEN (dev))
1153 netif_wake_queue (dev->net);
1154 }
1155}
1156
1157
1da177e4
LT
1158/*-------------------------------------------------------------------------
1159 *
1160 * USB Device Driver support
1161 *
1162 *-------------------------------------------------------------------------*/
cb1cebbe 1163
1da177e4
LT
1164// precondition: never called in_interrupt
1165
38bde1d4 1166void usbnet_disconnect (struct usb_interface *intf)
1da177e4
LT
1167{
1168 struct usbnet *dev;
1169 struct usb_device *xdev;
1170 struct net_device *net;
1171
1172 dev = usb_get_intfdata(intf);
1173 usb_set_intfdata(intf, NULL);
1174 if (!dev)
1175 return;
1176
1177 xdev = interface_to_usbdev (intf);
1178
1179 if (netif_msg_probe (dev))
38bde1d4
DB
1180 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1181 intf->dev.driver->name,
1da177e4
LT
1182 xdev->bus->bus_name, xdev->devpath,
1183 dev->driver_info->description);
cb1cebbe 1184
1da177e4
LT
1185 net = dev->net;
1186 unregister_netdev (net);
1187
1188 /* we don't hold rtnl here ... */
1189 flush_scheduled_work ();
1190
1191 if (dev->driver_info->unbind)
1192 dev->driver_info->unbind (dev, intf);
1193
1194 free_netdev(net);
1195 usb_put_dev (xdev);
1196}
38bde1d4 1197EXPORT_SYMBOL_GPL(usbnet_disconnect);
1da177e4 1198
777baa47
SH
1199static const struct net_device_ops usbnet_netdev_ops = {
1200 .ndo_open = usbnet_open,
1201 .ndo_stop = usbnet_stop,
1202 .ndo_start_xmit = usbnet_start_xmit,
1203 .ndo_tx_timeout = usbnet_tx_timeout,
1204 .ndo_change_mtu = usbnet_change_mtu,
1205 .ndo_set_mac_address = eth_mac_addr,
1206 .ndo_validate_addr = eth_validate_addr,
1207};
1da177e4
LT
1208
1209/*-------------------------------------------------------------------------*/
1210
1da177e4
LT
1211// precondition: never called in_interrupt
1212
225794f8
MH
1213static struct device_type wlan_type = {
1214 .name = "wlan",
1215};
1216
1217static struct device_type wwan_type = {
1218 .name = "wwan",
1219};
1220
38bde1d4 1221int
1da177e4
LT
1222usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1223{
1224 struct usbnet *dev;
cb1cebbe 1225 struct net_device *net;
1da177e4
LT
1226 struct usb_host_interface *interface;
1227 struct driver_info *info;
1228 struct usb_device *xdev;
1229 int status;
296c0242 1230 const char *name;
1da177e4 1231
296c0242 1232 name = udev->dev.driver->name;
1da177e4
LT
1233 info = (struct driver_info *) prod->driver_info;
1234 if (!info) {
296c0242 1235 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1da177e4
LT
1236 return -ENODEV;
1237 }
1238 xdev = interface_to_usbdev (udev);
1239 interface = udev->cur_altsetting;
1240
1241 usb_get_dev (xdev);
1242
1243 status = -ENOMEM;
1244
1245 // set up our own records
1246 net = alloc_etherdev(sizeof(*dev));
1247 if (!net) {
1248 dbg ("can't kmalloc dev");
1249 goto out;
1250 }
1251
1252 dev = netdev_priv(net);
1253 dev->udev = xdev;
a11a6544 1254 dev->intf = udev;
1da177e4 1255 dev->driver_info = info;
296c0242 1256 dev->driver_name = name;
1da177e4
LT
1257 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1258 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1259 skb_queue_head_init (&dev->rxq);
1260 skb_queue_head_init (&dev->txq);
1261 skb_queue_head_init (&dev->done);
7834ddbc 1262 skb_queue_head_init(&dev->rxq_pause);
1da177e4
LT
1263 dev->bh.func = usbnet_bh;
1264 dev->bh.data = (unsigned long) dev;
c4028958 1265 INIT_WORK (&dev->kevent, kevent);
1da177e4
LT
1266 dev->delay.function = usbnet_bh;
1267 dev->delay.data = (unsigned long) dev;
1268 init_timer (&dev->delay);
a9fc6338 1269 mutex_init (&dev->phy_mutex);
1da177e4 1270
1da177e4
LT
1271 dev->net = net;
1272 strcpy (net->name, "usb%d");
1273 memcpy (net->dev_addr, node_id, sizeof node_id);
1274
2e55cc72
DB
1275 /* rx and tx sides can use different message sizes;
1276 * bind() should set rx_urb_size in that case.
1277 */
1278 dev->hard_mtu = net->mtu + net->hard_header_len;
1da177e4
LT
1279#if 0
1280// dma_supported() is deeply broken on almost all architectures
1281 // possible with some EHCI controllers
6a35528a 1282 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1da177e4
LT
1283 net->features |= NETIF_F_HIGHDMA;
1284#endif
1285
777baa47 1286 net->netdev_ops = &usbnet_netdev_ops;
777baa47 1287 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1da177e4
LT
1288 net->ethtool_ops = &usbnet_ethtool_ops;
1289
1290 // allow device-specific bind/init procedures
1291 // NOTE net->name still not usable ...
1292 if (info->bind) {
1293 status = info->bind (dev, udev);
cb1cebbe
DB
1294 if (status < 0)
1295 goto out1;
1296
1da177e4
LT
1297 // heuristic: "usb%d" for links we know are two-host,
1298 // else "eth%d" when there's reasonable doubt. userspace
1299 // can rename the link if it knows better.
8e95a202
JP
1300 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1301 (net->dev_addr [0] & 0x02) == 0)
1da177e4 1302 strcpy (net->name, "eth%d");
6e3bbcc5
JK
1303 /* WLAN devices should always be named "wlan%d" */
1304 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1305 strcpy(net->name, "wlan%d");
e1e499ee
MH
1306 /* WWAN devices should always be named "wwan%d" */
1307 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1308 strcpy(net->name, "wwan%d");
f29fc259
DB
1309
1310 /* maybe the remote can't receive an Ethernet MTU */
1311 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1312 net->mtu = dev->hard_mtu - net->hard_header_len;
2e55cc72
DB
1313 } else if (!info->in || !info->out)
1314 status = usbnet_get_endpoints (dev, udev);
1da177e4
LT
1315 else {
1316 dev->in = usb_rcvbulkpipe (xdev, info->in);
1317 dev->out = usb_sndbulkpipe (xdev, info->out);
1318 if (!(info->flags & FLAG_NO_SETINT))
1319 status = usb_set_interface (xdev,
1320 interface->desc.bInterfaceNumber,
1321 interface->desc.bAlternateSetting);
1322 else
1323 status = 0;
1324
1325 }
9514bfe5 1326 if (status >= 0 && dev->status)
1da177e4
LT
1327 status = init_status (dev, udev);
1328 if (status < 0)
cb1cebbe 1329 goto out3;
1da177e4 1330
2e55cc72
DB
1331 if (!dev->rx_urb_size)
1332 dev->rx_urb_size = dev->hard_mtu;
1da177e4 1333 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
cb1cebbe 1334
1da177e4 1335 SET_NETDEV_DEV(net, &udev->dev);
225794f8
MH
1336
1337 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1338 SET_NETDEV_DEVTYPE(net, &wlan_type);
1339 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1340 SET_NETDEV_DEVTYPE(net, &wwan_type);
1341
1da177e4
LT
1342 status = register_netdev (net);
1343 if (status)
1344 goto out3;
1345 if (netif_msg_probe (dev))
e174961c 1346 devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
38bde1d4 1347 udev->dev.driver->name,
1da177e4
LT
1348 xdev->bus->bus_name, xdev->devpath,
1349 dev->driver_info->description,
e174961c 1350 net->dev_addr);
1da177e4
LT
1351
1352 // ok, it's ready to go.
1353 usb_set_intfdata (udev, dev);
1354
1da177e4
LT
1355 netif_device_attach (net);
1356
37e8273c
BH
1357 if (dev->driver_info->flags & FLAG_LINK_INTR)
1358 netif_carrier_off(net);
1359
1da177e4
LT
1360 return 0;
1361
1362out3:
1363 if (info->unbind)
1364 info->unbind (dev, udev);
1365out1:
1366 free_netdev(net);
1367out:
1368 usb_put_dev(xdev);
1369 return status;
1370}
38bde1d4 1371EXPORT_SYMBOL_GPL(usbnet_probe);
1da177e4
LT
1372
1373/*-------------------------------------------------------------------------*/
1374
36433127
ON
1375/*
1376 * suspend the whole driver as soon as the first interface is suspended
1377 * resume only when the last interface is resumed
38bde1d4 1378 */
1da177e4 1379
38bde1d4 1380int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1381{
1382 struct usbnet *dev = usb_get_intfdata(intf);
cb1cebbe 1383
36433127 1384 if (!dev->suspend_count++) {
a11a6544
ON
1385 /*
1386 * accelerate emptying of the rx and queues, to avoid
36433127
ON
1387 * having everything error out.
1388 */
1389 netif_device_detach (dev->net);
1390 (void) unlink_urbs (dev, &dev->rxq);
1391 (void) unlink_urbs (dev, &dev->txq);
a11a6544
ON
1392 /*
1393 * reattach so runtime management can use and
1394 * wake the device
1395 */
1396 netif_device_attach (dev->net);
36433127 1397 }
1da177e4
LT
1398 return 0;
1399}
38bde1d4 1400EXPORT_SYMBOL_GPL(usbnet_suspend);
1da177e4 1401
38bde1d4 1402int usbnet_resume (struct usb_interface *intf)
1da177e4
LT
1403{
1404 struct usbnet *dev = usb_get_intfdata(intf);
1405
a11a6544 1406 if (!--dev->suspend_count)
36433127 1407 tasklet_schedule (&dev->bh);
a11a6544 1408
1da177e4
LT
1409 return 0;
1410}
38bde1d4 1411EXPORT_SYMBOL_GPL(usbnet_resume);
1da177e4 1412
1da177e4 1413
1da177e4
LT
1414/*-------------------------------------------------------------------------*/
1415
f29fc259 1416static int __init usbnet_init(void)
1da177e4 1417{
090ffa9d 1418 /* compiler should optimize this out */
f8ac232a 1419 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1da177e4 1420 < sizeof (struct skb_data));
1da177e4
LT
1421
1422 random_ether_addr(node_id);
cb1cebbe 1423 return 0;
1da177e4 1424}
f29fc259 1425module_init(usbnet_init);
1da177e4 1426
f29fc259 1427static void __exit usbnet_exit(void)
1da177e4 1428{
1da177e4 1429}
f29fc259 1430module_exit(usbnet_exit);
1da177e4 1431
f29fc259 1432MODULE_AUTHOR("David Brownell");
090ffa9d 1433MODULE_DESCRIPTION("USB network driver framework");
f29fc259 1434MODULE_LICENSE("GPL");