blob: 37bf4f2c0a44c07204dcdc991cecee505462dcf1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownell090ffa92005-08-31 09:54:50 -07002 * USB Network driver infrastructure
David Brownellf29fc252005-08-31 09:52:31 -07003 * Copyright (C) 2000-2005 by David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * This is a generic "USB networking" framework that works with several
David Brownell090ffa92005-08-31 09:54:50 -070023 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
David Brownell090ffa92005-08-31 09:54:50 -070026 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
41#include <linux/workqueue.h>
42#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/usb.h>
David Brownellf29fc252005-08-31 09:52:31 -070044
45#include "usbnet.h"
46
47#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
50/*-------------------------------------------------------------------------*/
51
52/*
53 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54 * Several dozen bytes of IPv4 data can fit in two such transactions.
55 * One maximum size Ethernet packet takes twenty four of them.
56 * For high speed, each frame comfortably fits almost 36 max size
57 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070058 *
59 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60 * let the USB host controller be busy for 5msec or more before an irq
61 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Jamie Paintera99c1942006-07-27 14:17:28 -040063#define RX_MAX_QUEUE_MEMORY (60 * 1518)
64#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069// reawaken network queue this soon after stopping; else watchdog barks
70#define TX_TIMEOUT_JIFFIES (5*HZ)
71
72// throttle rx/tx briefly after some faults, so khubd might disconnect()
73// us (it polls at HZ/4 usually) before we report too many false errors.
74#define THROTTLE_JIFFIES (HZ/8)
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076// between wakeups
77#define UNLINK_TIMEOUT_MS 3
78
79/*-------------------------------------------------------------------------*/
80
81// randomly generated ethernet address
82static u8 node_id [ETH_ALEN];
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static const char driver_name [] = "usbnet";
85
86/* use ethtool to change the level for any given device */
87static int msg_level = -1;
88module_param (msg_level, int, 0);
89MODULE_PARM_DESC (msg_level, "Override default message level");
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*-------------------------------------------------------------------------*/
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070094int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int tmp;
97 struct usb_host_interface *alt = NULL;
98 struct usb_host_endpoint *in = NULL, *out = NULL;
99 struct usb_host_endpoint *status = NULL;
100
101 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
102 unsigned ep;
103
104 in = out = status = NULL;
105 alt = intf->altsetting + tmp;
106
107 /* take the first altsetting with in-bulk + out-bulk;
108 * remember any status endpoint, just in case;
109 * ignore other endpoints and altsetttings.
110 */
111 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
112 struct usb_host_endpoint *e;
113 int intr = 0;
114
115 e = alt->endpoint + ep;
116 switch (e->desc.bmAttributes) {
117 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300118 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 continue;
120 intr = 1;
121 /* FALLTHROUGH */
122 case USB_ENDPOINT_XFER_BULK:
123 break;
124 default:
125 continue;
126 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300127 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!intr && !in)
129 in = e;
130 else if (intr && !status)
131 status = e;
132 } else {
133 if (!out)
134 out = e;
135 }
136 }
137 if (in && out)
138 break;
139 }
140 if (!alt || !in || !out)
141 return -EINVAL;
142
143 if (alt->desc.bAlternateSetting != 0
144 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
145 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146 alt->desc.bAlternateSetting);
147 if (tmp < 0)
148 return tmp;
149 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 dev->in = usb_rcvbulkpipe (dev->udev,
152 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153 dev->out = usb_sndbulkpipe (dev->udev,
154 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->status = status;
156 return 0;
157}
David Brownell2e55cc72005-08-31 09:53:10 -0700158EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Howells7d12e782006-10-05 14:55:46 +0100160static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static int init_status (struct usbnet *dev, struct usb_interface *intf)
163{
164 char *buf = NULL;
165 unsigned pipe = 0;
166 unsigned maxp;
167 unsigned period;
168
169 if (!dev->driver_info->status)
170 return 0;
171
172 pipe = usb_rcvintpipe (dev->udev,
173 dev->status->desc.bEndpointAddress
174 & USB_ENDPOINT_NUMBER_MASK);
175 maxp = usb_maxpacket (dev->udev, pipe, 0);
176
177 /* avoid 1 msec chatter: min 8 msec poll rate */
178 period = max ((int) dev->status->desc.bInterval,
179 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
180
Christoph Lametere94b1762006-12-06 20:33:17 -0800181 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800183 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!dev->interrupt) {
185 kfree (buf);
186 return -ENOMEM;
187 } else {
188 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
189 buf, maxp, intr_complete, dev, period);
190 dev_dbg(&intf->dev,
191 "status ep%din, %d bytes period %d\n",
192 usb_pipeendpoint(pipe), maxp, period);
193 }
194 }
David Brownell18ab4582007-05-25 12:31:32 -0700195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
David Brownell2e55cc72005-08-31 09:53:10 -0700198/* Passes this packet up the stack, updating its accounting.
199 * Some link protocols batch packets, so their rx_fixup paths
200 * can return clones as well as just modify the original skb.
201 */
202void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int status;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->protocol = eth_type_trans (skb, dev->net);
207 dev->stats.rx_packets++;
208 dev->stats.rx_bytes += skb->len;
209
210 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700211 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 skb->len + sizeof (struct ethhdr), skb->protocol);
213 memset (skb->cb, 0, sizeof (struct skb_data));
214 status = netif_rx (skb);
215 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
216 devdbg (dev, "netif_rx status %d", status);
217}
David Brownell2e55cc72005-08-31 09:53:10 -0700218EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*-------------------------------------------------------------------------
222 *
223 * Network Device Driver (peer link to "Host Device", from USB host)
224 *
225 *-------------------------------------------------------------------------*/
226
227static int usbnet_change_mtu (struct net_device *net, int new_mtu)
228{
229 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700230 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400231 int old_hard_mtu = dev->hard_mtu;
232 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jamie Paintera99c1942006-07-27 14:17:28 -0400234 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700237 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EDOM;
239 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400240
241 dev->hard_mtu = net->mtu + net->hard_header_len;
242 if (dev->rx_urb_size == old_hard_mtu) {
243 dev->rx_urb_size = dev->hard_mtu;
244 if (dev->rx_urb_size > old_rx_urb_size)
245 usbnet_unlink_rx_urbs(dev);
246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250
251/*-------------------------------------------------------------------------*/
252
253static struct net_device_stats *usbnet_get_stats (struct net_device *net)
254{
255 struct usbnet *dev = netdev_priv(net);
256 return &dev->stats;
257}
258
259/*-------------------------------------------------------------------------*/
260
261/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
262 * completion callbacks. 2.5 should have fixed those bugs...
263 */
264
David S. Miller8728b832005-08-09 19:25:21 -0700265static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
268
David S. Miller8728b832005-08-09 19:25:21 -0700269 spin_lock_irqsave(&list->lock, flags);
270 __skb_unlink(skb, list);
271 spin_unlock(&list->lock);
272 spin_lock(&dev->done.lock);
273 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700275 tasklet_schedule(&dev->bh);
276 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/* some work can't be done in tasklets, so we use keventd
280 *
281 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
282 * but tasklet_schedule() doesn't. hope the failure is rare.
283 */
David Brownell2e55cc72005-08-31 09:53:10 -0700284void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 set_bit (work, &dev->flags);
287 if (!schedule_work (&dev->kevent))
288 deverr (dev, "kevent %d may have been dropped", work);
289 else
290 devdbg (dev, "kevent %d scheduled", work);
291}
David Brownell2e55cc72005-08-31 09:53:10 -0700292EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/*-------------------------------------------------------------------------*/
295
David Howells7d12e782006-10-05 14:55:46 +0100296static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Al Viro55016f12005-10-21 03:21:58 -0400298static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct sk_buff *skb;
301 struct skb_data *entry;
302 int retval = 0;
303 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700304 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
307 if (netif_msg_rx_err (dev))
308 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700309 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 usb_free_urb (urb);
311 return;
312 }
313 skb_reserve (skb, NET_IP_ALIGN);
314
315 entry = (struct skb_data *) skb->cb;
316 entry->urb = urb;
317 entry->dev = dev;
318 entry->state = rx_start;
319 entry->length = 0;
320
321 usb_fill_bulk_urb (urb, dev->udev, dev->in,
322 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 spin_lock_irqsave (&dev->rxq.lock, lockflags);
325
326 if (netif_running (dev->net)
327 && netif_device_present (dev->net)
328 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700329 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700331 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700334 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 case -ENODEV:
337 if (netif_msg_ifdown (dev))
338 devdbg (dev, "device gone");
339 netif_device_detach (dev->net);
340 break;
341 default:
342 if (netif_msg_rx_err (dev))
343 devdbg (dev, "rx submit, %d", retval);
344 tasklet_schedule (&dev->bh);
345 break;
346 case 0:
347 __skb_queue_tail (&dev->rxq, skb);
348 }
349 } else {
350 if (netif_msg_ifdown (dev))
351 devdbg (dev, "rx: stopped");
352 retval = -ENOLINK;
353 }
354 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
355 if (retval) {
356 dev_kfree_skb_any (skb);
357 usb_free_urb (urb);
358 }
359}
360
361
362/*-------------------------------------------------------------------------*/
363
364static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
365{
366 if (dev->driver_info->rx_fixup
367 && !dev->driver_info->rx_fixup (dev, skb))
368 goto error;
369 // else network stack removes extra byte if we forced a short packet
370
371 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700372 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 else {
374 if (netif_msg_rx_err (dev))
375 devdbg (dev, "drop");
376error:
377 dev->stats.rx_errors++;
378 skb_queue_tail (&dev->done, skb);
379 }
380}
381
382/*-------------------------------------------------------------------------*/
383
David Howells7d12e782006-10-05 14:55:46 +0100384static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct sk_buff *skb = (struct sk_buff *) urb->context;
387 struct skb_data *entry = (struct skb_data *) skb->cb;
388 struct usbnet *dev = entry->dev;
389 int urb_status = urb->status;
390
391 skb_put (skb, urb->actual_length);
392 entry->state = rx_done;
393 entry->urb = NULL;
394
395 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700396 /* success */
397 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700398 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 entry->state = rx_cleanup;
400 dev->stats.rx_errors++;
401 dev->stats.rx_length_errors++;
402 if (netif_msg_rx_err (dev))
403 devdbg (dev, "rx length %d", skb->len);
404 }
405 break;
406
David Brownell18ab4582007-05-25 12:31:32 -0700407 /* stalls need manual reset. this is rare ... except that
408 * when going through USB 2.0 TTs, unplug appears this way.
409 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
410 * storm, recovering as needed.
411 */
412 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700414 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 // FALLTHROUGH
416
David Brownell18ab4582007-05-25 12:31:32 -0700417 /* software-driven interface shutdown */
418 case -ECONNRESET: /* async unlink */
419 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (netif_msg_ifdown (dev))
421 devdbg (dev, "rx shutdown, code %d", urb_status);
422 goto block;
423
David Brownell18ab4582007-05-25 12:31:32 -0700424 /* we get controller i/o faults during khubd disconnect() delays.
425 * throttle down resubmits, to avoid log floods; just temporarily,
426 * so we still recover when the fault isn't a khubd delay.
427 */
428 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
David Brownell18ab4582007-05-25 12:31:32 -0700443 /* data overrun ... flush fifo? */
444 case -EOVERFLOW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 dev->stats.rx_over_errors++;
446 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800447
David Brownell18ab4582007-05-25 12:31:32 -0700448 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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) {
David Brownell18ab4582007-05-25 12:31:32 -0700476 /* success */
477 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 dev->driver_info->status(dev, urb);
479 break;
480
David Brownell18ab4582007-05-25 12:31:32 -0700481 /* software-driven interface shutdown */
482 case -ENOENT: /* urb killed */
483 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (netif_msg_ifdown (dev))
485 devdbg (dev, "intr shutdown, code %d", status);
486 return;
487
David Brownell18ab4582007-05-25 12:31:32 -0700488 /* NOTE: not throttling like RX/TX, since this endpoint
489 * already polls infrequently
490 */
491 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 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",
David Brownellcb1cebb2007-02-15 18:52:30 -0800564 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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 Brownell18ab4582007-05-25 12:31:32 -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;
David Brownellcb1cebb2007-02-15 18:52:30 -0800582 remove_wait_queue (&unlink_wakeup, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
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
David Brownell296c0242007-04-17 16:10:10 -0700739 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800740 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
741 strncpy (info->fw_version, dev->driver_info->description,
742 sizeof info->fw_version);
743 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
744}
745EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
746
David Brownell2e55cc72005-08-31 09:53:10 -0700747u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct usbnet *dev = netdev_priv(net);
750
751 return dev->msg_enable;
752}
David Brownell2e55cc72005-08-31 09:53:10 -0700753EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David Brownell2e55cc72005-08-31 09:53:10 -0700755void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct usbnet *dev = netdev_priv(net);
758
759 dev->msg_enable = level;
760}
David Brownell2e55cc72005-08-31 09:53:10 -0700761EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
David Brownell2e55cc72005-08-31 09:53:10 -0700763/* drivers may override default ethtool_ops in their bind() routine */
764static struct ethtool_ops usbnet_ethtool_ops = {
David Brownell18ee91fa2006-11-02 12:29:12 -0800765#ifdef HAVE_MII
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200766 .get_settings = usbnet_get_settings,
767 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700768 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200769 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800770#endif
771 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700772 .get_msglevel = usbnet_get_msglevel,
773 .set_msglevel = usbnet_set_msglevel,
774};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776/*-------------------------------------------------------------------------*/
777
778/* work that cannot be done in interrupt context uses keventd.
779 *
780 * NOTE: with 2.5 we could do more of this using completion callbacks,
781 * especially now that control transfers can be queued.
782 */
783static void
David Howellsc4028952006-11-22 14:57:56 +0000784kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
David Howellsc4028952006-11-22 14:57:56 +0000786 struct usbnet *dev =
787 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int status;
789
790 /* usb_clear_halt() needs a thread context */
791 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
792 unlink_urbs (dev, &dev->txq);
793 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700794 if (status < 0
795 && status != -EPIPE
796 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (netif_msg_tx_err (dev))
798 deverr (dev, "can't clear tx halt, status %d",
799 status);
800 } else {
801 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700802 if (status != -ESHUTDOWN)
803 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
807 unlink_urbs (dev, &dev->rxq);
808 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700809 if (status < 0
810 && status != -EPIPE
811 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (netif_msg_rx_err (dev))
813 deverr (dev, "can't clear rx halt, status %d",
814 status);
815 } else {
816 clear_bit (EVENT_RX_HALT, &dev->flags);
817 tasklet_schedule (&dev->bh);
818 }
819 }
820
821 /* tasklet could resubmit itself forever if memory is tight */
822 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
823 struct urb *urb = NULL;
824
825 if (netif_running (dev->net))
826 urb = usb_alloc_urb (0, GFP_KERNEL);
827 else
828 clear_bit (EVENT_RX_MEMORY, &dev->flags);
829 if (urb != NULL) {
830 clear_bit (EVENT_RX_MEMORY, &dev->flags);
831 rx_submit (dev, urb, GFP_KERNEL);
832 tasklet_schedule (&dev->bh);
833 }
834 }
835
David Hollis7ea13c92005-04-22 15:07:02 -0700836 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800837 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700838 int retval = 0;
839
840 clear_bit (EVENT_LINK_RESET, &dev->flags);
841 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
842 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
843 retval,
844 dev->udev->bus->bus_name, dev->udev->devpath,
845 info->description);
846 }
847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (dev->flags)
850 devdbg (dev, "kevent done, flags = 0x%lx",
851 dev->flags);
852}
853
854/*-------------------------------------------------------------------------*/
855
David Howells7d12e782006-10-05 14:55:46 +0100856static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct sk_buff *skb = (struct sk_buff *) urb->context;
859 struct skb_data *entry = (struct skb_data *) skb->cb;
860 struct usbnet *dev = entry->dev;
861
862 if (urb->status == 0) {
863 dev->stats.tx_packets++;
864 dev->stats.tx_bytes += entry->length;
865 } else {
866 dev->stats.tx_errors++;
867
868 switch (urb->status) {
869 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700870 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872
873 /* software-driven interface shutdown */
874 case -ECONNRESET: // async unlink
875 case -ESHUTDOWN: // hardware gone
876 break;
877
878 // like rx, tx gets controller i/o faults during khubd delays
879 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700880 case -EPROTO:
881 case -ETIME:
882 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!timer_pending (&dev->delay)) {
884 mod_timer (&dev->delay,
885 jiffies + THROTTLE_JIFFIES);
886 if (netif_msg_link (dev))
887 devdbg (dev, "tx throttle %d",
888 urb->status);
889 }
890 netif_stop_queue (dev->net);
891 break;
892 default:
893 if (netif_msg_tx_err (dev))
894 devdbg (dev, "tx err %d", entry->urb->status);
895 break;
896 }
897 }
898
899 urb->dev = NULL;
900 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700901 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*-------------------------------------------------------------------------*/
905
906static void usbnet_tx_timeout (struct net_device *net)
907{
908 struct usbnet *dev = netdev_priv(net);
909
910 unlink_urbs (dev, &dev->txq);
911 tasklet_schedule (&dev->bh);
912
913 // FIXME: device recovery -- reset?
914}
915
916/*-------------------------------------------------------------------------*/
917
918static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
919{
920 struct usbnet *dev = netdev_priv(net);
921 int length;
922 int retval = NET_XMIT_SUCCESS;
923 struct urb *urb = NULL;
924 struct skb_data *entry;
925 struct driver_info *info = dev->driver_info;
926 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 // some devices want funky USB-level framing, for
929 // win32 driver (usually) and/or hardware quirks
930 if (info->tx_fixup) {
931 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
932 if (!skb) {
933 if (netif_msg_tx_err (dev))
934 devdbg (dev, "can't tx_fixup skb");
935 goto drop;
936 }
937 }
938 length = skb->len;
939
940 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
941 if (netif_msg_tx_err (dev))
942 devdbg (dev, "no urb");
943 goto drop;
944 }
945
946 entry = (struct skb_data *) skb->cb;
947 entry->urb = urb;
948 entry->dev = dev;
949 entry->state = tx_start;
950 entry->length = length;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 usb_fill_bulk_urb (urb, dev->udev, dev->out,
953 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* don't assume the hardware handles USB_ZERO_PACKET
956 * NOTE: strictly conforming cdc-ether devices should expect
957 * the ZLP here, but ignore the one-byte packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200959 if ((length % dev->maxpacket) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 urb->transfer_buffer_length++;
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200961 if (skb_tailroom(skb)) {
962 skb->data[skb->len] = 0;
963 __skb_put(skb, 1);
964 }
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 spin_lock_irqsave (&dev->txq.lock, flags);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
970 case -EPIPE:
971 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -0700972 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 break;
974 default:
975 if (netif_msg_tx_err (dev))
976 devdbg (dev, "tx: submit urb err %d", retval);
977 break;
978 case 0:
979 net->trans_start = jiffies;
980 __skb_queue_tail (&dev->txq, skb);
981 if (dev->txq.qlen >= TX_QLEN (dev))
982 netif_stop_queue (net);
983 }
984 spin_unlock_irqrestore (&dev->txq.lock, flags);
985
986 if (retval) {
987 if (netif_msg_tx_err (dev))
988 devdbg (dev, "drop, code %d", retval);
989drop:
990 retval = NET_XMIT_SUCCESS;
991 dev->stats.tx_dropped++;
992 if (skb)
993 dev_kfree_skb_any (skb);
994 usb_free_urb (urb);
995 } else if (netif_msg_tx_queued (dev)) {
996 devdbg (dev, "> tx, len %d, type 0x%x",
997 length, skb->protocol);
998 }
999 return retval;
1000}
1001
1002
1003/*-------------------------------------------------------------------------*/
1004
1005// tasklet (work deferred from completions, in_irq) or timer
1006
1007static void usbnet_bh (unsigned long param)
1008{
1009 struct usbnet *dev = (struct usbnet *) param;
1010 struct sk_buff *skb;
1011 struct skb_data *entry;
1012
1013 while ((skb = skb_dequeue (&dev->done))) {
1014 entry = (struct skb_data *) skb->cb;
1015 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001016 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 entry->state = rx_cleanup;
1018 rx_process (dev, skb);
1019 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001020 case tx_done:
1021 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 usb_free_urb (entry->urb);
1023 dev_kfree_skb (skb);
1024 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001025 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 devdbg (dev, "bogus skb state %d", entry->state);
1027 }
1028 }
1029
1030 // waiting for all pending urbs to complete?
1031 if (dev->wait) {
1032 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1033 wake_up (dev->wait);
1034 }
1035
1036 // or are we maybe short a few urbs?
1037 } else if (netif_running (dev->net)
1038 && netif_device_present (dev->net)
1039 && !timer_pending (&dev->delay)
1040 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1041 int temp = dev->rxq.qlen;
1042 int qlen = RX_QLEN (dev);
1043
1044 if (temp < qlen) {
1045 struct urb *urb;
1046 int i;
1047
1048 // don't refill the queue all at once
1049 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1050 urb = usb_alloc_urb (0, GFP_ATOMIC);
1051 if (urb != NULL)
1052 rx_submit (dev, urb, GFP_ATOMIC);
1053 }
1054 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1055 devdbg (dev, "rxqlen %d --> %d",
1056 temp, dev->rxq.qlen);
1057 if (dev->rxq.qlen < qlen)
1058 tasklet_schedule (&dev->bh);
1059 }
1060 if (dev->txq.qlen < TX_QLEN (dev))
1061 netif_wake_queue (dev->net);
1062 }
1063}
1064
1065
1066
1067/*-------------------------------------------------------------------------
1068 *
1069 * USB Device Driver support
1070 *
1071 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073// precondition: never called in_interrupt
1074
David Brownell38bde1d2005-08-31 09:52:45 -07001075void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct usbnet *dev;
1078 struct usb_device *xdev;
1079 struct net_device *net;
1080
1081 dev = usb_get_intfdata(intf);
1082 usb_set_intfdata(intf, NULL);
1083 if (!dev)
1084 return;
1085
1086 xdev = interface_to_usbdev (intf);
1087
1088 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001089 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1090 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xdev->bus->bus_name, xdev->devpath,
1092 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 net = dev->net;
1095 unregister_netdev (net);
1096
1097 /* we don't hold rtnl here ... */
1098 flush_scheduled_work ();
1099
1100 if (dev->driver_info->unbind)
1101 dev->driver_info->unbind (dev, intf);
1102
1103 free_netdev(net);
1104 usb_put_dev (xdev);
1105}
David Brownell38bde1d2005-08-31 09:52:45 -07001106EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108
1109/*-------------------------------------------------------------------------*/
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111// precondition: never called in_interrupt
1112
David Brownell38bde1d2005-08-31 09:52:45 -07001113int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1115{
1116 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001117 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct usb_host_interface *interface;
1119 struct driver_info *info;
1120 struct usb_device *xdev;
1121 int status;
David Brownell296c0242007-04-17 16:10:10 -07001122 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David Brownell296c0242007-04-17 16:10:10 -07001124 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 info = (struct driver_info *) prod->driver_info;
1126 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001127 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -ENODEV;
1129 }
1130 xdev = interface_to_usbdev (udev);
1131 interface = udev->cur_altsetting;
1132
1133 usb_get_dev (xdev);
1134
1135 status = -ENOMEM;
1136
1137 // set up our own records
1138 net = alloc_etherdev(sizeof(*dev));
1139 if (!net) {
1140 dbg ("can't kmalloc dev");
1141 goto out;
1142 }
1143
1144 dev = netdev_priv(net);
1145 dev->udev = xdev;
1146 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001147 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1149 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1150 skb_queue_head_init (&dev->rxq);
1151 skb_queue_head_init (&dev->txq);
1152 skb_queue_head_init (&dev->done);
1153 dev->bh.func = usbnet_bh;
1154 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001155 INIT_WORK (&dev->kevent, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dev->delay.function = usbnet_bh;
1157 dev->delay.data = (unsigned long) dev;
1158 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001159 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 SET_MODULE_OWNER (net);
1162 dev->net = net;
1163 strcpy (net->name, "usb%d");
1164 memcpy (net->dev_addr, node_id, sizeof node_id);
1165
David Brownell2e55cc72005-08-31 09:53:10 -07001166 /* rx and tx sides can use different message sizes;
1167 * bind() should set rx_urb_size in that case.
1168 */
1169 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170#if 0
1171// dma_supported() is deeply broken on almost all architectures
1172 // possible with some EHCI controllers
1173 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1174 net->features |= NETIF_F_HIGHDMA;
1175#endif
1176
1177 net->change_mtu = usbnet_change_mtu;
1178 net->get_stats = usbnet_get_stats;
1179 net->hard_start_xmit = usbnet_start_xmit;
1180 net->open = usbnet_open;
1181 net->stop = usbnet_stop;
1182 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1183 net->tx_timeout = usbnet_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 net->ethtool_ops = &usbnet_ethtool_ops;
1185
1186 // allow device-specific bind/init procedures
1187 // NOTE net->name still not usable ...
1188 if (info->bind) {
1189 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001190 if (status < 0)
1191 goto out1;
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 // heuristic: "usb%d" for links we know are two-host,
1194 // else "eth%d" when there's reasonable doubt. userspace
1195 // can rename the link if it knows better.
1196 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1197 && (net->dev_addr [0] & 0x02) == 0)
1198 strcpy (net->name, "eth%d");
David Brownellf29fc252005-08-31 09:52:31 -07001199
1200 /* maybe the remote can't receive an Ethernet MTU */
1201 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1202 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001203 } else if (!info->in || !info->out)
1204 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 else {
1206 dev->in = usb_rcvbulkpipe (xdev, info->in);
1207 dev->out = usb_sndbulkpipe (xdev, info->out);
1208 if (!(info->flags & FLAG_NO_SETINT))
1209 status = usb_set_interface (xdev,
1210 interface->desc.bInterfaceNumber,
1211 interface->desc.bAlternateSetting);
1212 else
1213 status = 0;
1214
1215 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001216 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 status = init_status (dev, udev);
1218 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001219 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
David Brownell2e55cc72005-08-31 09:53:10 -07001221 if (!dev->rx_urb_size)
1222 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 SET_NETDEV_DEV(net, &udev->dev);
1226 status = register_netdev (net);
1227 if (status)
1228 goto out3;
1229 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001230 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 "%02x:%02x:%02x:%02x:%02x:%02x",
David Brownell38bde1d2005-08-31 09:52:45 -07001232 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 xdev->bus->bus_name, xdev->devpath,
1234 dev->driver_info->description,
1235 net->dev_addr [0], net->dev_addr [1],
1236 net->dev_addr [2], net->dev_addr [3],
1237 net->dev_addr [4], net->dev_addr [5]);
1238
1239 // ok, it's ready to go.
1240 usb_set_intfdata (udev, dev);
1241
1242 // start as if the link is up
1243 netif_device_attach (net);
1244
1245 return 0;
1246
1247out3:
1248 if (info->unbind)
1249 info->unbind (dev, udev);
1250out1:
1251 free_netdev(net);
1252out:
1253 usb_put_dev(xdev);
1254 return status;
1255}
David Brownell38bde1d2005-08-31 09:52:45 -07001256EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258/*-------------------------------------------------------------------------*/
1259
Oliver Neukum36433122007-04-30 01:37:44 -07001260/*
1261 * suspend the whole driver as soon as the first interface is suspended
1262 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
David Brownell38bde1d2005-08-31 09:52:45 -07001265int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001268
Oliver Neukum36433122007-04-30 01:37:44 -07001269 if (!dev->suspend_count++) {
1270 /* accelerate emptying of the rx and queues, to avoid
1271 * having everything error out.
1272 */
1273 netif_device_detach (dev->net);
1274 (void) unlink_urbs (dev, &dev->rxq);
1275 (void) unlink_urbs (dev, &dev->txq);
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278}
David Brownell38bde1d2005-08-31 09:52:45 -07001279EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
David Brownell38bde1d2005-08-31 09:52:45 -07001281int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct usbnet *dev = usb_get_intfdata(intf);
1284
Oliver Neukum36433122007-04-30 01:37:44 -07001285 if (!--dev->suspend_count) {
1286 netif_device_attach (dev->net);
1287 tasklet_schedule (&dev->bh);
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return 0;
1290}
David Brownell38bde1d2005-08-31 09:52:45 -07001291EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294/*-------------------------------------------------------------------------*/
1295
David Brownellf29fc252005-08-31 09:52:31 -07001296static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
David Brownell090ffa92005-08-31 09:54:50 -07001298 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001299 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
David Brownellf29fc252005-08-31 09:52:31 -07001305module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
David Brownellf29fc252005-08-31 09:52:31 -07001307static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
David Brownellf29fc252005-08-31 09:52:31 -07001310module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
David Brownellf29fc252005-08-31 09:52:31 -07001312MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001313MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001314MODULE_LICENSE("GPL");