blob: 6e39e998825914acfaf899c5a9d77e2b13b59d31 [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/sched.h>
38#include <linux/init.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/ethtool.h>
42#include <linux/workqueue.h>
43#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/usb.h>
David Brownellf29fc252005-08-31 09:52:31 -070045
46#include "usbnet.h"
47
48#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50
51/*-------------------------------------------------------------------------*/
52
53/*
54 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
55 * Several dozen bytes of IPv4 data can fit in two such transactions.
56 * One maximum size Ethernet packet takes twenty four of them.
57 * For high speed, each frame comfortably fits almost 36 max size
58 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070059 *
60 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
61 * let the USB host controller be busy for 5msec or more before an irq
62 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Jamie Paintera99c1942006-07-27 14:17:28 -040064#define RX_MAX_QUEUE_MEMORY (60 * 1518)
65#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
67#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
68 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070// reawaken network queue this soon after stopping; else watchdog barks
71#define TX_TIMEOUT_JIFFIES (5*HZ)
72
73// throttle rx/tx briefly after some faults, so khubd might disconnect()
74// us (it polls at HZ/4 usually) before we report too many false errors.
75#define THROTTLE_JIFFIES (HZ/8)
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077// between wakeups
78#define UNLINK_TIMEOUT_MS 3
79
80/*-------------------------------------------------------------------------*/
81
82// randomly generated ethernet address
83static u8 node_id [ETH_ALEN];
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static const char driver_name [] = "usbnet";
86
87/* use ethtool to change the level for any given device */
88static int msg_level = -1;
89module_param (msg_level, int, 0);
90MODULE_PARM_DESC (msg_level, "Override default message level");
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*-------------------------------------------------------------------------*/
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070095int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 int tmp;
98 struct usb_host_interface *alt = NULL;
99 struct usb_host_endpoint *in = NULL, *out = NULL;
100 struct usb_host_endpoint *status = NULL;
101
102 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
103 unsigned ep;
104
105 in = out = status = NULL;
106 alt = intf->altsetting + tmp;
107
108 /* take the first altsetting with in-bulk + out-bulk;
109 * remember any status endpoint, just in case;
110 * ignore other endpoints and altsetttings.
111 */
112 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
113 struct usb_host_endpoint *e;
114 int intr = 0;
115
116 e = alt->endpoint + ep;
117 switch (e->desc.bmAttributes) {
118 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300119 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 continue;
121 intr = 1;
122 /* FALLTHROUGH */
123 case USB_ENDPOINT_XFER_BULK:
124 break;
125 default:
126 continue;
127 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300128 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!intr && !in)
130 in = e;
131 else if (intr && !status)
132 status = e;
133 } else {
134 if (!out)
135 out = e;
136 }
137 }
138 if (in && out)
139 break;
140 }
141 if (!alt || !in || !out)
142 return -EINVAL;
143
144 if (alt->desc.bAlternateSetting != 0
145 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
146 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
147 alt->desc.bAlternateSetting);
148 if (tmp < 0)
149 return tmp;
150 }
151
152 dev->in = usb_rcvbulkpipe (dev->udev,
153 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->out = usb_sndbulkpipe (dev->udev,
155 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
156 dev->status = status;
157 return 0;
158}
David Brownell2e55cc72005-08-31 09:53:10 -0700159EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howells7d12e782006-10-05 14:55:46 +0100161static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163static int init_status (struct usbnet *dev, struct usb_interface *intf)
164{
165 char *buf = NULL;
166 unsigned pipe = 0;
167 unsigned maxp;
168 unsigned period;
169
170 if (!dev->driver_info->status)
171 return 0;
172
173 pipe = usb_rcvintpipe (dev->udev,
174 dev->status->desc.bEndpointAddress
175 & USB_ENDPOINT_NUMBER_MASK);
176 maxp = usb_maxpacket (dev->udev, pipe, 0);
177
178 /* avoid 1 msec chatter: min 8 msec poll rate */
179 period = max ((int) dev->status->desc.bInterval,
180 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
181
Christoph Lametere94b1762006-12-06 20:33:17 -0800182 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800184 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (!dev->interrupt) {
186 kfree (buf);
187 return -ENOMEM;
188 } else {
189 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
190 buf, maxp, intr_complete, dev, period);
191 dev_dbg(&intf->dev,
192 "status ep%din, %d bytes period %d\n",
193 usb_pipeendpoint(pipe), maxp, period);
194 }
195 }
196 return 0;
197}
198
David Brownell2e55cc72005-08-31 09:53:10 -0700199/* Passes this packet up the stack, updating its accounting.
200 * Some link protocols batch packets, so their rx_fixup paths
201 * can return clones as well as just modify the original skb.
202 */
203void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int status;
206
207 skb->dev = dev->net;
208 skb->protocol = eth_type_trans (skb, dev->net);
209 dev->stats.rx_packets++;
210 dev->stats.rx_bytes += skb->len;
211
212 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700213 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 skb->len + sizeof (struct ethhdr), skb->protocol);
215 memset (skb->cb, 0, sizeof (struct skb_data));
216 status = netif_rx (skb);
217 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
218 devdbg (dev, "netif_rx status %d", status);
219}
David Brownell2e55cc72005-08-31 09:53:10 -0700220EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*-------------------------------------------------------------------------
224 *
225 * Network Device Driver (peer link to "Host Device", from USB host)
226 *
227 *-------------------------------------------------------------------------*/
228
229static int usbnet_change_mtu (struct net_device *net, int new_mtu)
230{
231 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700232 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400233 int old_hard_mtu = dev->hard_mtu;
234 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jamie Paintera99c1942006-07-27 14:17:28 -0400236 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700239 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EDOM;
241 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400242
243 dev->hard_mtu = net->mtu + net->hard_header_len;
244 if (dev->rx_urb_size == old_hard_mtu) {
245 dev->rx_urb_size = dev->hard_mtu;
246 if (dev->rx_urb_size > old_rx_urb_size)
247 usbnet_unlink_rx_urbs(dev);
248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251}
252
253/*-------------------------------------------------------------------------*/
254
255static struct net_device_stats *usbnet_get_stats (struct net_device *net)
256{
257 struct usbnet *dev = netdev_priv(net);
258 return &dev->stats;
259}
260
261/*-------------------------------------------------------------------------*/
262
263/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
264 * completion callbacks. 2.5 should have fixed those bugs...
265 */
266
David S. Miller8728b832005-08-09 19:25:21 -0700267static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 unsigned long flags;
270
David S. Miller8728b832005-08-09 19:25:21 -0700271 spin_lock_irqsave(&list->lock, flags);
272 __skb_unlink(skb, list);
273 spin_unlock(&list->lock);
274 spin_lock(&dev->done.lock);
275 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700277 tasklet_schedule(&dev->bh);
278 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/* some work can't be done in tasklets, so we use keventd
282 *
283 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
284 * but tasklet_schedule() doesn't. hope the failure is rare.
285 */
David Brownell2e55cc72005-08-31 09:53:10 -0700286void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 set_bit (work, &dev->flags);
289 if (!schedule_work (&dev->kevent))
290 deverr (dev, "kevent %d may have been dropped", work);
291 else
292 devdbg (dev, "kevent %d scheduled", work);
293}
David Brownell2e55cc72005-08-31 09:53:10 -0700294EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296/*-------------------------------------------------------------------------*/
297
David Howells7d12e782006-10-05 14:55:46 +0100298static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Al Viro55016f12005-10-21 03:21:58 -0400300static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct sk_buff *skb;
303 struct skb_data *entry;
304 int retval = 0;
305 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700306 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
309 if (netif_msg_rx_err (dev))
310 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700311 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 usb_free_urb (urb);
313 return;
314 }
315 skb_reserve (skb, NET_IP_ALIGN);
316
317 entry = (struct skb_data *) skb->cb;
318 entry->urb = urb;
319 entry->dev = dev;
320 entry->state = rx_start;
321 entry->length = 0;
322
323 usb_fill_bulk_urb (urb, dev->udev, dev->in,
324 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 spin_lock_irqsave (&dev->rxq.lock, lockflags);
327
328 if (netif_running (dev->net)
329 && netif_device_present (dev->net)
330 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
331 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
332 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700333 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700336 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338 case -ENODEV:
339 if (netif_msg_ifdown (dev))
340 devdbg (dev, "device gone");
341 netif_device_detach (dev->net);
342 break;
343 default:
344 if (netif_msg_rx_err (dev))
345 devdbg (dev, "rx submit, %d", retval);
346 tasklet_schedule (&dev->bh);
347 break;
348 case 0:
349 __skb_queue_tail (&dev->rxq, skb);
350 }
351 } else {
352 if (netif_msg_ifdown (dev))
353 devdbg (dev, "rx: stopped");
354 retval = -ENOLINK;
355 }
356 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
357 if (retval) {
358 dev_kfree_skb_any (skb);
359 usb_free_urb (urb);
360 }
361}
362
363
364/*-------------------------------------------------------------------------*/
365
366static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
367{
368 if (dev->driver_info->rx_fixup
369 && !dev->driver_info->rx_fixup (dev, skb))
370 goto error;
371 // else network stack removes extra byte if we forced a short packet
372
373 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700374 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 else {
376 if (netif_msg_rx_err (dev))
377 devdbg (dev, "drop");
378error:
379 dev->stats.rx_errors++;
380 skb_queue_tail (&dev->done, skb);
381 }
382}
383
384/*-------------------------------------------------------------------------*/
385
David Howells7d12e782006-10-05 14:55:46 +0100386static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct sk_buff *skb = (struct sk_buff *) urb->context;
389 struct skb_data *entry = (struct skb_data *) skb->cb;
390 struct usbnet *dev = entry->dev;
391 int urb_status = urb->status;
392
393 skb_put (skb, urb->actual_length);
394 entry->state = rx_done;
395 entry->urb = NULL;
396
397 switch (urb_status) {
398 // success
399 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700400 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 entry->state = rx_cleanup;
402 dev->stats.rx_errors++;
403 dev->stats.rx_length_errors++;
404 if (netif_msg_rx_err (dev))
405 devdbg (dev, "rx length %d", skb->len);
406 }
407 break;
408
409 // stalls need manual reset. this is rare ... except that
410 // when going through USB 2.0 TTs, unplug appears this way.
411 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
412 // storm, recovering as needed.
413 case -EPIPE:
414 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700415 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 // FALLTHROUGH
417
418 // software-driven interface shutdown
419 case -ECONNRESET: // async unlink
420 case -ESHUTDOWN: // hardware gone
421 if (netif_msg_ifdown (dev))
422 devdbg (dev, "rx shutdown, code %d", urb_status);
423 goto block;
424
425 // we get controller i/o faults during khubd disconnect() delays.
426 // throttle down resubmits, to avoid log floods; just temporarily,
427 // so we still recover when the fault isn't a khubd delay.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700428 case -EPROTO:
429 case -ETIME:
430 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dev->stats.rx_errors++;
432 if (!timer_pending (&dev->delay)) {
433 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
434 if (netif_msg_link (dev))
435 devdbg (dev, "rx throttle %d", urb_status);
436 }
437block:
438 entry->state = rx_cleanup;
439 entry->urb = urb;
440 urb = NULL;
441 break;
442
443 // data overrun ... flush fifo?
444 case -EOVERFLOW:
445 dev->stats.rx_over_errors++;
446 // FALLTHROUGH
447
448 default:
449 entry->state = rx_cleanup;
450 dev->stats.rx_errors++;
451 if (netif_msg_rx_err (dev))
452 devdbg (dev, "rx status %d", urb_status);
453 break;
454 }
455
David S. Miller8728b832005-08-09 19:25:21 -0700456 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (urb) {
459 if (netif_running (dev->net)
460 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
461 rx_submit (dev, urb, GFP_ATOMIC);
462 return;
463 }
464 usb_free_urb (urb);
465 }
466 if (netif_msg_rx_err (dev))
467 devdbg (dev, "no read resubmitted");
468}
469
David Howells7d12e782006-10-05 14:55:46 +0100470static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct usbnet *dev = urb->context;
473 int status = urb->status;
474
475 switch (status) {
476 /* success */
477 case 0:
478 dev->driver_info->status(dev, urb);
479 break;
480
481 /* software-driven interface shutdown */
482 case -ENOENT: // urb killed
483 case -ESHUTDOWN: // hardware gone
484 if (netif_msg_ifdown (dev))
485 devdbg (dev, "intr shutdown, code %d", status);
486 return;
487
488 /* NOTE: not throttling like RX/TX, since this endpoint
489 * already polls infrequently
490 */
491 default:
492 devdbg (dev, "intr status %d", status);
493 break;
494 }
495
496 if (!netif_running (dev->net))
497 return;
498
499 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
500 status = usb_submit_urb (urb, GFP_ATOMIC);
501 if (status != 0 && netif_msg_timer (dev))
502 deverr(dev, "intr resubmit --> %d", status);
503}
504
505/*-------------------------------------------------------------------------*/
506
507// unlink pending rx/tx; completion handlers do all other cleanup
508
509static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
510{
511 unsigned long flags;
512 struct sk_buff *skb, *skbnext;
513 int count = 0;
514
515 spin_lock_irqsave (&q->lock, flags);
516 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
517 struct skb_data *entry;
518 struct urb *urb;
519 int retval;
520
521 entry = (struct skb_data *) skb->cb;
522 urb = entry->urb;
523 skbnext = skb->next;
524
525 // during some PM-driven resume scenarios,
526 // these (async) unlinks complete immediately
527 retval = usb_unlink_urb (urb);
528 if (retval != -EINPROGRESS && retval != 0)
529 devdbg (dev, "unlink urb err, %d", retval);
530 else
531 count++;
532 }
533 spin_unlock_irqrestore (&q->lock, flags);
534 return count;
535}
536
Jamie Paintera99c1942006-07-27 14:17:28 -0400537// Flush all pending rx urbs
538// minidrivers may need to do this when the MTU changes
539
540void usbnet_unlink_rx_urbs(struct usbnet *dev)
541{
542 if (netif_running(dev->net)) {
543 (void) unlink_urbs (dev, &dev->rxq);
544 tasklet_schedule(&dev->bh);
545 }
546}
547EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549/*-------------------------------------------------------------------------*/
550
551// precondition: never called in_interrupt
552
553static int usbnet_stop (struct net_device *net)
554{
555 struct usbnet *dev = netdev_priv(net);
556 int temp;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800557 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 DECLARE_WAITQUEUE (wait, current);
559
560 netif_stop_queue (net);
561
562 if (netif_msg_ifdown (dev))
563 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
564 dev->stats.rx_packets, dev->stats.tx_packets,
565 dev->stats.rx_errors, dev->stats.tx_errors
566 );
567
568 // ensure there are no more active urbs
569 add_wait_queue (&unlink_wakeup, &wait);
570 dev->wait = &unlink_wakeup;
571 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
572
573 // maybe wait for deletions to finish.
David S. Millerb03efcf2005-07-08 14:57:23 -0700574 while (!skb_queue_empty(&dev->rxq) &&
575 !skb_queue_empty(&dev->txq) &&
576 !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 msleep(UNLINK_TIMEOUT_MS);
578 if (netif_msg_ifdown (dev))
579 devdbg (dev, "waited for %d urb completions", temp);
580 }
581 dev->wait = NULL;
582 remove_wait_queue (&unlink_wakeup, &wait);
583
584 usb_kill_urb(dev->interrupt);
585
586 /* deferred work (task, timer, softirq) must also stop.
587 * can't flush_scheduled_work() until we drop rtnl (later),
588 * else workers could deadlock; so make workers a NOP.
589 */
590 dev->flags = 0;
591 del_timer_sync (&dev->delay);
592 tasklet_kill (&dev->bh);
593
594 return 0;
595}
596
597/*-------------------------------------------------------------------------*/
598
599// posts reads, and enables write queuing
600
601// precondition: never called in_interrupt
602
603static int usbnet_open (struct net_device *net)
604{
605 struct usbnet *dev = netdev_priv(net);
606 int retval = 0;
607 struct driver_info *info = dev->driver_info;
608
609 // put into "known safe" state
610 if (info->reset && (retval = info->reset (dev)) < 0) {
611 if (netif_msg_ifup (dev))
612 devinfo (dev,
613 "open reset fail (%d) usbnet usb-%s-%s, %s",
614 retval,
615 dev->udev->bus->bus_name, dev->udev->devpath,
616 info->description);
617 goto done;
618 }
619
620 // insist peer be connected
621 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
622 if (netif_msg_ifup (dev))
623 devdbg (dev, "can't open; %d", retval);
624 goto done;
625 }
626
627 /* start any status interrupt transfer */
628 if (dev->interrupt) {
629 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
630 if (retval < 0) {
631 if (netif_msg_ifup (dev))
632 deverr (dev, "intr submit %d", retval);
633 goto done;
634 }
635 }
636
637 netif_start_queue (net);
638 if (netif_msg_ifup (dev)) {
639 char *framing;
640
641 if (dev->driver_info->flags & FLAG_FRAMING_NC)
642 framing = "NetChip";
643 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
644 framing = "GeneSys";
645 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
646 framing = "Zaurus";
647 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
648 framing = "RNDIS";
649 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
650 framing = "ASIX";
651 else
652 framing = "simple";
653
654 devinfo (dev, "open: enable queueing "
655 "(rx %d, tx %d) mtu %d %s framing",
Randy Dunlap25d94e62006-08-07 15:56:40 -0700656 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 framing);
658 }
659
660 // delay posting reads until we're fully open
661 tasklet_schedule (&dev->bh);
662done:
663 return retval;
664}
665
666/*-------------------------------------------------------------------------*/
667
David Brownell2e55cc72005-08-31 09:53:10 -0700668/* ethtool methods; minidrivers may need to add some more, but
669 * they'll probably want to use this base set.
670 */
671
David Brownell18ee91fa2006-11-02 12:29:12 -0800672#if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
673#define HAVE_MII
674
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200675int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
676{
677 struct usbnet *dev = netdev_priv(net);
678
679 if (!dev->mii.mdio_read)
680 return -EOPNOTSUPP;
681
682 return mii_ethtool_gset(&dev->mii, cmd);
683}
684EXPORT_SYMBOL_GPL(usbnet_get_settings);
685
686int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
687{
688 struct usbnet *dev = netdev_priv(net);
689 int retval;
690
691 if (!dev->mii.mdio_write)
692 return -EOPNOTSUPP;
693
694 retval = mii_ethtool_sset(&dev->mii, cmd);
695
696 /* link speed/duplex might have changed */
697 if (dev->driver_info->link_reset)
698 dev->driver_info->link_reset(dev);
699
700 return retval;
701
702}
703EXPORT_SYMBOL_GPL(usbnet_set_settings);
704
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200705u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct usbnet *dev = netdev_priv(net);
708
David Brownellf29fc252005-08-31 09:52:31 -0700709 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (dev->driver_info->check_connect)
711 return dev->driver_info->check_connect (dev) == 0;
712
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200713 /* if the device has mii operations, use those */
714 if (dev->mii.mdio_read)
715 return mii_link_ok(&dev->mii);
716
David Brownellf29fc252005-08-31 09:52:31 -0700717 /* Otherwise, say we're up (to avoid breaking scripts) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 1;
719}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200720EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David Brownell18ee91fa2006-11-02 12:29:12 -0800722int usbnet_nway_reset(struct net_device *net)
723{
724 struct usbnet *dev = netdev_priv(net);
725
726 if (!dev->mii.mdio_write)
727 return -EOPNOTSUPP;
728
729 return mii_nway_restart(&dev->mii);
730}
731EXPORT_SYMBOL_GPL(usbnet_nway_reset);
732
733#endif /* HAVE_MII */
734
735void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
736{
737 struct usbnet *dev = netdev_priv(net);
738
739 /* REVISIT don't always return "usbnet" */
740 strncpy (info->driver, driver_name, sizeof info->driver);
741 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
742 strncpy (info->fw_version, dev->driver_info->description,
743 sizeof info->fw_version);
744 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
745}
746EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
747
David Brownell2e55cc72005-08-31 09:53:10 -0700748u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct usbnet *dev = netdev_priv(net);
751
752 return dev->msg_enable;
753}
David Brownell2e55cc72005-08-31 09:53:10 -0700754EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David Brownell2e55cc72005-08-31 09:53:10 -0700756void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct usbnet *dev = netdev_priv(net);
759
760 dev->msg_enable = level;
761}
David Brownell2e55cc72005-08-31 09:53:10 -0700762EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
David Brownell2e55cc72005-08-31 09:53:10 -0700764/* drivers may override default ethtool_ops in their bind() routine */
765static struct ethtool_ops usbnet_ethtool_ops = {
David Brownell18ee91fa2006-11-02 12:29:12 -0800766#ifdef HAVE_MII
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200767 .get_settings = usbnet_get_settings,
768 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700769 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200770 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800771#endif
772 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700773 .get_msglevel = usbnet_get_msglevel,
774 .set_msglevel = usbnet_set_msglevel,
775};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777/*-------------------------------------------------------------------------*/
778
779/* work that cannot be done in interrupt context uses keventd.
780 *
781 * NOTE: with 2.5 we could do more of this using completion callbacks,
782 * especially now that control transfers can be queued.
783 */
784static void
David Howellsc4028952006-11-22 14:57:56 +0000785kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
David Howellsc4028952006-11-22 14:57:56 +0000787 struct usbnet *dev =
788 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int status;
790
791 /* usb_clear_halt() needs a thread context */
792 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
793 unlink_urbs (dev, &dev->txq);
794 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700795 if (status < 0
796 && status != -EPIPE
797 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (netif_msg_tx_err (dev))
799 deverr (dev, "can't clear tx halt, status %d",
800 status);
801 } else {
802 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700803 if (status != -ESHUTDOWN)
804 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 }
807 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
808 unlink_urbs (dev, &dev->rxq);
809 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700810 if (status < 0
811 && status != -EPIPE
812 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (netif_msg_rx_err (dev))
814 deverr (dev, "can't clear rx halt, status %d",
815 status);
816 } else {
817 clear_bit (EVENT_RX_HALT, &dev->flags);
818 tasklet_schedule (&dev->bh);
819 }
820 }
821
822 /* tasklet could resubmit itself forever if memory is tight */
823 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
824 struct urb *urb = NULL;
825
826 if (netif_running (dev->net))
827 urb = usb_alloc_urb (0, GFP_KERNEL);
828 else
829 clear_bit (EVENT_RX_MEMORY, &dev->flags);
830 if (urb != NULL) {
831 clear_bit (EVENT_RX_MEMORY, &dev->flags);
832 rx_submit (dev, urb, GFP_KERNEL);
833 tasklet_schedule (&dev->bh);
834 }
835 }
836
David Hollis7ea13c92005-04-22 15:07:02 -0700837 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
838 struct driver_info *info = dev->driver_info;
839 int retval = 0;
840
841 clear_bit (EVENT_LINK_RESET, &dev->flags);
842 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
843 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
844 retval,
845 dev->udev->bus->bus_name, dev->udev->devpath,
846 info->description);
847 }
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (dev->flags)
851 devdbg (dev, "kevent done, flags = 0x%lx",
852 dev->flags);
853}
854
855/*-------------------------------------------------------------------------*/
856
David Howells7d12e782006-10-05 14:55:46 +0100857static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct sk_buff *skb = (struct sk_buff *) urb->context;
860 struct skb_data *entry = (struct skb_data *) skb->cb;
861 struct usbnet *dev = entry->dev;
862
863 if (urb->status == 0) {
864 dev->stats.tx_packets++;
865 dev->stats.tx_bytes += entry->length;
866 } else {
867 dev->stats.tx_errors++;
868
869 switch (urb->status) {
870 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700871 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873
874 /* software-driven interface shutdown */
875 case -ECONNRESET: // async unlink
876 case -ESHUTDOWN: // hardware gone
877 break;
878
879 // like rx, tx gets controller i/o faults during khubd delays
880 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700881 case -EPROTO:
882 case -ETIME:
883 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!timer_pending (&dev->delay)) {
885 mod_timer (&dev->delay,
886 jiffies + THROTTLE_JIFFIES);
887 if (netif_msg_link (dev))
888 devdbg (dev, "tx throttle %d",
889 urb->status);
890 }
891 netif_stop_queue (dev->net);
892 break;
893 default:
894 if (netif_msg_tx_err (dev))
895 devdbg (dev, "tx err %d", entry->urb->status);
896 break;
897 }
898 }
899
900 urb->dev = NULL;
901 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700902 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905/*-------------------------------------------------------------------------*/
906
907static void usbnet_tx_timeout (struct net_device *net)
908{
909 struct usbnet *dev = netdev_priv(net);
910
911 unlink_urbs (dev, &dev->txq);
912 tasklet_schedule (&dev->bh);
913
914 // FIXME: device recovery -- reset?
915}
916
917/*-------------------------------------------------------------------------*/
918
919static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
920{
921 struct usbnet *dev = netdev_priv(net);
922 int length;
923 int retval = NET_XMIT_SUCCESS;
924 struct urb *urb = NULL;
925 struct skb_data *entry;
926 struct driver_info *info = dev->driver_info;
927 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 // some devices want funky USB-level framing, for
930 // win32 driver (usually) and/or hardware quirks
931 if (info->tx_fixup) {
932 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
933 if (!skb) {
934 if (netif_msg_tx_err (dev))
935 devdbg (dev, "can't tx_fixup skb");
936 goto drop;
937 }
938 }
939 length = skb->len;
940
941 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
942 if (netif_msg_tx_err (dev))
943 devdbg (dev, "no urb");
944 goto drop;
945 }
946
947 entry = (struct skb_data *) skb->cb;
948 entry->urb = urb;
949 entry->dev = dev;
950 entry->state = tx_start;
951 entry->length = length;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 usb_fill_bulk_urb (urb, dev->udev, dev->out,
954 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* don't assume the hardware handles USB_ZERO_PACKET
957 * NOTE: strictly conforming cdc-ether devices should expect
958 * the ZLP here, but ignore the one-byte packet.
959 *
960 * FIXME zero that byte, if it doesn't require a new skb.
961 */
962 if ((length % dev->maxpacket) == 0)
963 urb->transfer_buffer_length++;
964
965 spin_lock_irqsave (&dev->txq.lock, flags);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
968 case -EPIPE:
969 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -0700970 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 default:
973 if (netif_msg_tx_err (dev))
974 devdbg (dev, "tx: submit urb err %d", retval);
975 break;
976 case 0:
977 net->trans_start = jiffies;
978 __skb_queue_tail (&dev->txq, skb);
979 if (dev->txq.qlen >= TX_QLEN (dev))
980 netif_stop_queue (net);
981 }
982 spin_unlock_irqrestore (&dev->txq.lock, flags);
983
984 if (retval) {
985 if (netif_msg_tx_err (dev))
986 devdbg (dev, "drop, code %d", retval);
987drop:
988 retval = NET_XMIT_SUCCESS;
989 dev->stats.tx_dropped++;
990 if (skb)
991 dev_kfree_skb_any (skb);
992 usb_free_urb (urb);
993 } else if (netif_msg_tx_queued (dev)) {
994 devdbg (dev, "> tx, len %d, type 0x%x",
995 length, skb->protocol);
996 }
997 return retval;
998}
999
1000
1001/*-------------------------------------------------------------------------*/
1002
1003// tasklet (work deferred from completions, in_irq) or timer
1004
1005static void usbnet_bh (unsigned long param)
1006{
1007 struct usbnet *dev = (struct usbnet *) param;
1008 struct sk_buff *skb;
1009 struct skb_data *entry;
1010
1011 while ((skb = skb_dequeue (&dev->done))) {
1012 entry = (struct skb_data *) skb->cb;
1013 switch (entry->state) {
1014 case rx_done:
1015 entry->state = rx_cleanup;
1016 rx_process (dev, skb);
1017 continue;
1018 case tx_done:
1019 case rx_cleanup:
1020 usb_free_urb (entry->urb);
1021 dev_kfree_skb (skb);
1022 continue;
1023 default:
1024 devdbg (dev, "bogus skb state %d", entry->state);
1025 }
1026 }
1027
1028 // waiting for all pending urbs to complete?
1029 if (dev->wait) {
1030 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1031 wake_up (dev->wait);
1032 }
1033
1034 // or are we maybe short a few urbs?
1035 } else if (netif_running (dev->net)
1036 && netif_device_present (dev->net)
1037 && !timer_pending (&dev->delay)
1038 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1039 int temp = dev->rxq.qlen;
1040 int qlen = RX_QLEN (dev);
1041
1042 if (temp < qlen) {
1043 struct urb *urb;
1044 int i;
1045
1046 // don't refill the queue all at once
1047 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1048 urb = usb_alloc_urb (0, GFP_ATOMIC);
1049 if (urb != NULL)
1050 rx_submit (dev, urb, GFP_ATOMIC);
1051 }
1052 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1053 devdbg (dev, "rxqlen %d --> %d",
1054 temp, dev->rxq.qlen);
1055 if (dev->rxq.qlen < qlen)
1056 tasklet_schedule (&dev->bh);
1057 }
1058 if (dev->txq.qlen < TX_QLEN (dev))
1059 netif_wake_queue (dev->net);
1060 }
1061}
1062
1063
1064
1065/*-------------------------------------------------------------------------
1066 *
1067 * USB Device Driver support
1068 *
1069 *-------------------------------------------------------------------------*/
1070
1071// precondition: never called in_interrupt
1072
David Brownell38bde1d2005-08-31 09:52:45 -07001073void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct usbnet *dev;
1076 struct usb_device *xdev;
1077 struct net_device *net;
1078
1079 dev = usb_get_intfdata(intf);
1080 usb_set_intfdata(intf, NULL);
1081 if (!dev)
1082 return;
1083
1084 xdev = interface_to_usbdev (intf);
1085
1086 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001087 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1088 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 xdev->bus->bus_name, xdev->devpath,
1090 dev->driver_info->description);
1091
1092 net = dev->net;
1093 unregister_netdev (net);
1094
1095 /* we don't hold rtnl here ... */
1096 flush_scheduled_work ();
1097
1098 if (dev->driver_info->unbind)
1099 dev->driver_info->unbind (dev, intf);
1100
1101 free_netdev(net);
1102 usb_put_dev (xdev);
1103}
David Brownell38bde1d2005-08-31 09:52:45 -07001104EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106
1107/*-------------------------------------------------------------------------*/
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109// precondition: never called in_interrupt
1110
David Brownell38bde1d2005-08-31 09:52:45 -07001111int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1113{
1114 struct usbnet *dev;
1115 struct net_device *net;
1116 struct usb_host_interface *interface;
1117 struct driver_info *info;
1118 struct usb_device *xdev;
1119 int status;
1120
1121 info = (struct driver_info *) prod->driver_info;
1122 if (!info) {
1123 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1124 return -ENODEV;
1125 }
1126 xdev = interface_to_usbdev (udev);
1127 interface = udev->cur_altsetting;
1128
1129 usb_get_dev (xdev);
1130
1131 status = -ENOMEM;
1132
1133 // set up our own records
1134 net = alloc_etherdev(sizeof(*dev));
1135 if (!net) {
1136 dbg ("can't kmalloc dev");
1137 goto out;
1138 }
1139
1140 dev = netdev_priv(net);
1141 dev->udev = xdev;
1142 dev->driver_info = info;
1143 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);
1185 // heuristic: "usb%d" for links we know are two-host,
1186 // else "eth%d" when there's reasonable doubt. userspace
1187 // can rename the link if it knows better.
1188 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1189 && (net->dev_addr [0] & 0x02) == 0)
1190 strcpy (net->name, "eth%d");
David Brownellf29fc252005-08-31 09:52:31 -07001191
1192 /* maybe the remote can't receive an Ethernet MTU */
1193 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1194 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001195 } else if (!info->in || !info->out)
1196 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 else {
1198 dev->in = usb_rcvbulkpipe (xdev, info->in);
1199 dev->out = usb_sndbulkpipe (xdev, info->out);
1200 if (!(info->flags & FLAG_NO_SETINT))
1201 status = usb_set_interface (xdev,
1202 interface->desc.bInterfaceNumber,
1203 interface->desc.bAlternateSetting);
1204 else
1205 status = 0;
1206
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (status == 0 && dev->status)
1209 status = init_status (dev, udev);
1210 if (status < 0)
1211 goto out1;
1212
David Brownell2e55cc72005-08-31 09:53:10 -07001213 if (!dev->rx_urb_size)
1214 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1216
1217 SET_NETDEV_DEV(net, &udev->dev);
1218 status = register_netdev (net);
1219 if (status)
1220 goto out3;
1221 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001222 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 "%02x:%02x:%02x:%02x:%02x:%02x",
David Brownell38bde1d2005-08-31 09:52:45 -07001224 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xdev->bus->bus_name, xdev->devpath,
1226 dev->driver_info->description,
1227 net->dev_addr [0], net->dev_addr [1],
1228 net->dev_addr [2], net->dev_addr [3],
1229 net->dev_addr [4], net->dev_addr [5]);
1230
1231 // ok, it's ready to go.
1232 usb_set_intfdata (udev, dev);
1233
1234 // start as if the link is up
1235 netif_device_attach (net);
1236
1237 return 0;
1238
1239out3:
1240 if (info->unbind)
1241 info->unbind (dev, udev);
1242out1:
1243 free_netdev(net);
1244out:
1245 usb_put_dev(xdev);
1246 return status;
1247}
David Brownell38bde1d2005-08-31 09:52:45 -07001248EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250/*-------------------------------------------------------------------------*/
1251
David Brownell38bde1d2005-08-31 09:52:45 -07001252/* FIXME these suspend/resume methods assume non-CDC style
1253 * devices, with only one interface.
1254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
David Brownell38bde1d2005-08-31 09:52:45 -07001256int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct usbnet *dev = usb_get_intfdata(intf);
1259
David Brownell27d72e82005-04-18 17:39:22 -07001260 /* accelerate emptying of the rx and queues, to avoid
1261 * having everything error out.
1262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 netif_device_detach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001264 (void) unlink_urbs (dev, &dev->rxq);
1265 (void) unlink_urbs (dev, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
David Brownell38bde1d2005-08-31 09:52:45 -07001268EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David Brownell38bde1d2005-08-31 09:52:45 -07001270int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct usbnet *dev = usb_get_intfdata(intf);
1273
1274 netif_device_attach (dev->net);
David Brownell27d72e82005-04-18 17:39:22 -07001275 tasklet_schedule (&dev->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277}
David Brownell38bde1d2005-08-31 09:52:45 -07001278EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281/*-------------------------------------------------------------------------*/
1282
David Brownellf29fc252005-08-31 09:52:31 -07001283static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
David Brownell090ffa92005-08-31 09:54:50 -07001285 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001286 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 random_ether_addr(node_id);
David Brownell090ffa92005-08-31 09:54:50 -07001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
David Brownellf29fc252005-08-31 09:52:31 -07001292module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David Brownellf29fc252005-08-31 09:52:31 -07001294static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
David Brownellf29fc252005-08-31 09:52:31 -07001297module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
David Brownellf29fc252005-08-31 09:52:31 -07001299MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001300MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001301MODULE_LICENSE("GPL");