net: cdc_ncm: cleanup a type issue in cdc_ncm_setup()
[deliverable/linux.git] / drivers / net / usb / usbnet.c
CommitLineData
1da177e4 1/*
090ffa9d 2 * USB Network driver infrastructure
f29fc259 3 * Copyright (C) 2000-2005 by David Brownell
1da177e4 4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * This is a generic "USB networking" framework that works with several
090ffa9d
DB
23 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
1da177e4 25 *
090ffa9d
DB
26 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
31 */
1da177e4
LT
32
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
1da177e4 36#include <linux/module.h>
1da177e4
LT
37#include <linux/init.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
03ad032b 40#include <linux/ctype.h>
1da177e4
LT
41#include <linux/ethtool.h>
42#include <linux/workqueue.h>
43#include <linux/mii.h>
1da177e4 44#include <linux/usb.h>
3692e94f 45#include <linux/usb/usbnet.h>
5a0e3ad6 46#include <linux/slab.h>
fd1f170d 47#include <linux/kernel.h>
b0786b43 48#include <linux/pm_runtime.h>
f29fc259
DB
49
50#define DRIVER_VERSION "22-Aug-2005"
1da177e4
LT
51
52
53/*-------------------------------------------------------------------------*/
54
55/*
56 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
57 * Several dozen bytes of IPv4 data can fit in two such transactions.
58 * One maximum size Ethernet packet takes twenty four of them.
59 * For high speed, each frame comfortably fits almost 36 max size
60 * Ethernet packets (so queues should be bigger).
f29fc259 61 *
a88c32ae
ML
62 * The goal is to let the USB host controller be busy for 5msec or
63 * more before an irq is required, under load. Jumbograms change
64 * the equation.
1da177e4 65 */
a88c32ae
ML
66#define MAX_QUEUE_MEMORY (60 * 1518)
67#define RX_QLEN(dev) ((dev)->rx_qlen)
68#define TX_QLEN(dev) ((dev)->tx_qlen)
1da177e4 69
1da177e4
LT
70// 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
1da177e4
LT
77// between wakeups
78#define UNLINK_TIMEOUT_MS 3
79
80/*-------------------------------------------------------------------------*/
81
82// randomly generated ethernet address
83static u8 node_id [ETH_ALEN];
84
1da177e4
LT
85static 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
1da177e4
LT
92/*-------------------------------------------------------------------------*/
93
1da177e4 94/* handles CDC Ethernet and many other network "bulk data" interfaces */
2e55cc72 95int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
1da177e4
LT
96{
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;
70f23fd6 110 * ignore other endpoints and altsettings.
1da177e4
LT
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:
fc6e2544 119 if (!usb_endpoint_dir_in(&e->desc))
1da177e4
LT
120 continue;
121 intr = 1;
122 /* FALLTHROUGH */
123 case USB_ENDPOINT_XFER_BULK:
124 break;
125 default:
126 continue;
127 }
fc6e2544 128 if (usb_endpoint_dir_in(&e->desc)) {
1da177e4
LT
129 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
8e95a202
JP
144 if (alt->desc.bAlternateSetting != 0 ||
145 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1da177e4
LT
146 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
147 alt->desc.bAlternateSetting);
148 if (tmp < 0)
149 return tmp;
150 }
cb1cebbe 151
1da177e4
LT
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}
2e55cc72 159EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1da177e4 160
03ad032b
PH
161int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
162{
163 int tmp, i;
164 unsigned char buf [13];
165
166 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
167 if (tmp != 12) {
168 dev_dbg(&dev->udev->dev,
169 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
170 if (tmp >= 0)
171 tmp = -EINVAL;
172 return tmp;
173 }
174 for (i = tmp = 0; i < 6; i++, tmp += 2)
175 dev->net->dev_addr [i] =
fd1f170d 176 (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
03ad032b
PH
177 return 0;
178}
179EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
180
24ead299 181static void intr_complete (struct urb *urb)
182{
183 struct usbnet *dev = urb->context;
184 int status = urb->status;
185
186 switch (status) {
187 /* success */
188 case 0:
189 dev->driver_info->status(dev, urb);
190 break;
191
192 /* software-driven interface shutdown */
193 case -ENOENT: /* urb killed */
194 case -ESHUTDOWN: /* hardware gone */
195 netif_dbg(dev, ifdown, dev->net,
196 "intr shutdown, code %d\n", status);
197 return;
198
199 /* NOTE: not throttling like RX/TX, since this endpoint
200 * already polls infrequently
201 */
202 default:
203 netdev_dbg(dev->net, "intr status %d\n", status);
204 break;
205 }
206
24ead299 207 status = usb_submit_urb (urb, GFP_ATOMIC);
208 if (status != 0)
209 netif_err(dev, timer, dev->net,
210 "intr resubmit --> %d\n", status);
211}
1da177e4
LT
212
213static int init_status (struct usbnet *dev, struct usb_interface *intf)
214{
215 char *buf = NULL;
216 unsigned pipe = 0;
217 unsigned maxp;
218 unsigned period;
219
220 if (!dev->driver_info->status)
221 return 0;
222
223 pipe = usb_rcvintpipe (dev->udev,
224 dev->status->desc.bEndpointAddress
225 & USB_ENDPOINT_NUMBER_MASK);
226 maxp = usb_maxpacket (dev->udev, pipe, 0);
227
228 /* avoid 1 msec chatter: min 8 msec poll rate */
229 period = max ((int) dev->status->desc.bInterval,
230 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
231
e94b1766 232 buf = kmalloc (maxp, GFP_KERNEL);
1da177e4 233 if (buf) {
e94b1766 234 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
1da177e4
LT
235 if (!dev->interrupt) {
236 kfree (buf);
237 return -ENOMEM;
238 } else {
239 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
240 buf, maxp, intr_complete, dev, period);
720f3d7c 241 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
1da177e4
LT
242 dev_dbg(&intf->dev,
243 "status ep%din, %d bytes period %d\n",
244 usb_pipeendpoint(pipe), maxp, period);
245 }
246 }
18ab458f 247 return 0;
1da177e4
LT
248}
249
6eecdc5f
DW
250/* Submit the interrupt URB if not previously submitted, increasing refcount */
251int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
252{
253 int ret = 0;
254
255 WARN_ON_ONCE(dev->interrupt == NULL);
256 if (dev->interrupt) {
257 mutex_lock(&dev->interrupt_mutex);
258
259 if (++dev->interrupt_count == 1)
260 ret = usb_submit_urb(dev->interrupt, mem_flags);
261
262 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
263 dev->interrupt_count);
264 mutex_unlock(&dev->interrupt_mutex);
265 }
266 return ret;
267}
268EXPORT_SYMBOL_GPL(usbnet_status_start);
269
270/* For resume; submit interrupt URB if previously submitted */
271static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
272{
273 int ret = 0;
274
275 mutex_lock(&dev->interrupt_mutex);
276 if (dev->interrupt_count) {
277 ret = usb_submit_urb(dev->interrupt, mem_flags);
278 dev_dbg(&dev->udev->dev,
279 "submitted interrupt URB for resume\n");
280 }
281 mutex_unlock(&dev->interrupt_mutex);
282 return ret;
283}
284
285/* Kill the interrupt URB if all submitters want it killed */
286void usbnet_status_stop(struct usbnet *dev)
287{
288 if (dev->interrupt) {
289 mutex_lock(&dev->interrupt_mutex);
290 WARN_ON(dev->interrupt_count == 0);
291
292 if (dev->interrupt_count && --dev->interrupt_count == 0)
293 usb_kill_urb(dev->interrupt);
294
295 dev_dbg(&dev->udev->dev,
296 "decremented interrupt URB count to %d\n",
297 dev->interrupt_count);
298 mutex_unlock(&dev->interrupt_mutex);
299 }
300}
301EXPORT_SYMBOL_GPL(usbnet_status_stop);
302
303/* For suspend; always kill interrupt URB */
304static void __usbnet_status_stop_force(struct usbnet *dev)
305{
306 if (dev->interrupt) {
307 mutex_lock(&dev->interrupt_mutex);
308 usb_kill_urb(dev->interrupt);
309 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
310 mutex_unlock(&dev->interrupt_mutex);
311 }
312}
313
2e55cc72
DB
314/* Passes this packet up the stack, updating its accounting.
315 * Some link protocols batch packets, so their rx_fixup paths
316 * can return clones as well as just modify the original skb.
317 */
318void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
1da177e4
LT
319{
320 int status;
321
7834ddbc
JK
322 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
323 skb_queue_tail(&dev->rxq_pause, skb);
324 return;
325 }
326
1da177e4 327 skb->protocol = eth_type_trans (skb, dev->net);
7963837f
HX
328 dev->net->stats.rx_packets++;
329 dev->net->stats.rx_bytes += skb->len;
1da177e4 330
a475f603
JP
331 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
332 skb->len + sizeof (struct ethhdr), skb->protocol);
1da177e4 333 memset (skb->cb, 0, sizeof (struct skb_data));
f9b491ec
MR
334
335 if (skb_defer_rx_timestamp(skb))
336 return;
337
1da177e4 338 status = netif_rx (skb);
a475f603
JP
339 if (status != NET_RX_SUCCESS)
340 netif_dbg(dev, rx_err, dev->net,
341 "netif_rx status %d\n", status);
1da177e4 342}
2e55cc72 343EXPORT_SYMBOL_GPL(usbnet_skb_return);
1da177e4 344
a88c32ae
ML
345/* must be called if hard_mtu or rx_urb_size changed */
346void usbnet_update_max_qlen(struct usbnet *dev)
347{
348 enum usb_device_speed speed = dev->udev->speed;
349
350 switch (speed) {
351 case USB_SPEED_HIGH:
352 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
353 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
354 break;
452c447a
ML
355 case USB_SPEED_SUPER:
356 /*
357 * Not take default 5ms qlen for super speed HC to
358 * save memory, and iperf tests show 2.5ms qlen can
359 * work well
360 */
361 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
362 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
363 break;
a88c32ae
ML
364 default:
365 dev->rx_qlen = dev->tx_qlen = 4;
366 }
367}
368EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
369
1da177e4
LT
370\f
371/*-------------------------------------------------------------------------
372 *
373 * Network Device Driver (peer link to "Host Device", from USB host)
374 *
375 *-------------------------------------------------------------------------*/
376
777baa47 377int usbnet_change_mtu (struct net_device *net, int new_mtu)
1da177e4
LT
378{
379 struct usbnet *dev = netdev_priv(net);
f29fc259 380 int ll_mtu = new_mtu + net->hard_header_len;
a99c1949
JP
381 int old_hard_mtu = dev->hard_mtu;
382 int old_rx_urb_size = dev->rx_urb_size;
1da177e4 383
a99c1949 384 if (new_mtu <= 0)
1da177e4 385 return -EINVAL;
1da177e4 386 // no second zero-length packet read wanted after mtu-sized packets
f29fc259 387 if ((ll_mtu % dev->maxpacket) == 0)
1da177e4
LT
388 return -EDOM;
389 net->mtu = new_mtu;
a99c1949
JP
390
391 dev->hard_mtu = net->mtu + net->hard_header_len;
392 if (dev->rx_urb_size == old_hard_mtu) {
393 dev->rx_urb_size = dev->hard_mtu;
394 if (dev->rx_urb_size > old_rx_urb_size)
395 usbnet_unlink_rx_urbs(dev);
396 }
397
a88c32ae
ML
398 /* max qlen depend on hard_mtu and rx_urb_size */
399 usbnet_update_max_qlen(dev);
400
1da177e4
LT
401 return 0;
402}
777baa47 403EXPORT_SYMBOL_GPL(usbnet_change_mtu);
1da177e4 404
5b6e9bcd
ML
405/* The caller must hold list->lock */
406static void __usbnet_queue_skb(struct sk_buff_head *list,
407 struct sk_buff *newsk, enum skb_state state)
408{
409 struct skb_data *entry = (struct skb_data *) newsk->cb;
410
411 __skb_queue_tail(list, newsk);
412 entry->state = state;
413}
414
1da177e4
LT
415/*-------------------------------------------------------------------------*/
416
1da177e4
LT
417/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
418 * completion callbacks. 2.5 should have fixed those bugs...
419 */
420
5b6e9bcd
ML
421static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
422 struct sk_buff_head *list, enum skb_state state)
1da177e4 423{
1da177e4 424 unsigned long flags;
5b6e9bcd
ML
425 enum skb_state old_state;
426 struct skb_data *entry = (struct skb_data *) skb->cb;
1da177e4 427
8728b834 428 spin_lock_irqsave(&list->lock, flags);
5b6e9bcd
ML
429 old_state = entry->state;
430 entry->state = state;
8728b834
DM
431 __skb_unlink(skb, list);
432 spin_unlock(&list->lock);
433 spin_lock(&dev->done.lock);
434 __skb_queue_tail(&dev->done, skb);
1da177e4 435 if (dev->done.qlen == 1)
8728b834
DM
436 tasklet_schedule(&dev->bh);
437 spin_unlock_irqrestore(&dev->done.lock, flags);
5b6e9bcd 438 return old_state;
1da177e4
LT
439}
440
441/* some work can't be done in tasklets, so we use keventd
442 *
443 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
444 * but tasklet_schedule() doesn't. hope the failure is rare.
445 */
2e55cc72 446void usbnet_defer_kevent (struct usbnet *dev, int work)
1da177e4
LT
447{
448 set_bit (work, &dev->flags);
9532021d
SG
449 if (!schedule_work (&dev->kevent)) {
450 if (net_ratelimit())
451 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
452 } else {
60b86755 453 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
9532021d 454 }
1da177e4 455}
2e55cc72 456EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1da177e4
LT
457
458/*-------------------------------------------------------------------------*/
459
7d12e780 460static void rx_complete (struct urb *urb);
1da177e4 461
dacb3975 462static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
1da177e4
LT
463{
464 struct sk_buff *skb;
465 struct skb_data *entry;
466 int retval = 0;
467 unsigned long lockflags;
2e55cc72 468 size_t size = dev->rx_urb_size;
1da177e4 469
70c37bf9
BM
470 /* prevent rx skb allocation when error ratio is high */
471 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
472 usb_free_urb(urb);
473 return -ENOLINK;
474 }
475
7bdd4027
ED
476 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
477 if (!skb) {
a475f603 478 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
2e55cc72 479 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4 480 usb_free_urb (urb);
dacb3975 481 return -ENOMEM;
1da177e4 482 }
1da177e4
LT
483
484 entry = (struct skb_data *) skb->cb;
485 entry->urb = urb;
486 entry->dev = dev;
1da177e4
LT
487 entry->length = 0;
488
489 usb_fill_bulk_urb (urb, dev->udev, dev->in,
490 skb->data, size, rx_complete, skb);
1da177e4
LT
491
492 spin_lock_irqsave (&dev->rxq.lock, lockflags);
493
8e95a202
JP
494 if (netif_running (dev->net) &&
495 netif_device_present (dev->net) &&
69ee472f
ON
496 !test_bit (EVENT_RX_HALT, &dev->flags) &&
497 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
18ab458f 498 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
1da177e4 499 case -EPIPE:
2e55cc72 500 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
501 break;
502 case -ENOMEM:
2e55cc72 503 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
504 break;
505 case -ENODEV:
a475f603 506 netif_dbg(dev, ifdown, dev->net, "device gone\n");
1da177e4
LT
507 netif_device_detach (dev->net);
508 break;
dacb3975
DM
509 case -EHOSTUNREACH:
510 retval = -ENOLINK;
511 break;
1da177e4 512 default:
a475f603
JP
513 netif_dbg(dev, rx_err, dev->net,
514 "rx submit, %d\n", retval);
1da177e4
LT
515 tasklet_schedule (&dev->bh);
516 break;
517 case 0:
5b6e9bcd 518 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
1da177e4
LT
519 }
520 } else {
a475f603 521 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
1da177e4
LT
522 retval = -ENOLINK;
523 }
524 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
525 if (retval) {
526 dev_kfree_skb_any (skb);
527 usb_free_urb (urb);
528 }
dacb3975 529 return retval;
1da177e4
LT
530}
531
532
533/*-------------------------------------------------------------------------*/
534
535static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
536{
8e95a202 537 if (dev->driver_info->rx_fixup &&
7a635ea9
AZ
538 !dev->driver_info->rx_fixup (dev, skb)) {
539 /* With RX_ASSEMBLE, rx_fixup() must update counters */
540 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
541 dev->net->stats.rx_errors++;
542 goto done;
543 }
1da177e4
LT
544 // else network stack removes extra byte if we forced a short packet
545
073285fd
AO
546 if (skb->len) {
547 /* all data was already cloned from skb inside the driver */
548 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
549 dev_kfree_skb_any(skb);
550 else
551 usbnet_skb_return(dev, skb);
552 return;
1da177e4 553 }
073285fd
AO
554
555 netif_dbg(dev, rx_err, dev->net, "drop\n");
073285fd 556 dev->net->stats.rx_errors++;
7a635ea9 557done:
073285fd 558 skb_queue_tail(&dev->done, skb);
1da177e4
LT
559}
560
561/*-------------------------------------------------------------------------*/
562
7d12e780 563static void rx_complete (struct urb *urb)
1da177e4
LT
564{
565 struct sk_buff *skb = (struct sk_buff *) urb->context;
566 struct skb_data *entry = (struct skb_data *) skb->cb;
567 struct usbnet *dev = entry->dev;
568 int urb_status = urb->status;
5b6e9bcd 569 enum skb_state state;
1da177e4
LT
570
571 skb_put (skb, urb->actual_length);
5b6e9bcd 572 state = rx_done;
1da177e4
LT
573 entry->urb = NULL;
574
575 switch (urb_status) {
18ab458f
DB
576 /* success */
577 case 0:
f29fc259 578 if (skb->len < dev->net->hard_header_len) {
5b6e9bcd 579 state = rx_cleanup;
7963837f
HX
580 dev->net->stats.rx_errors++;
581 dev->net->stats.rx_length_errors++;
a475f603
JP
582 netif_dbg(dev, rx_err, dev->net,
583 "rx length %d\n", skb->len);
1da177e4
LT
584 }
585 break;
586
18ab458f
DB
587 /* stalls need manual reset. this is rare ... except that
588 * when going through USB 2.0 TTs, unplug appears this way.
3ac49a1c 589 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
18ab458f
DB
590 * storm, recovering as needed.
591 */
592 case -EPIPE:
7963837f 593 dev->net->stats.rx_errors++;
2e55cc72 594 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
595 // FALLTHROUGH
596
18ab458f
DB
597 /* software-driven interface shutdown */
598 case -ECONNRESET: /* async unlink */
599 case -ESHUTDOWN: /* hardware gone */
a475f603
JP
600 netif_dbg(dev, ifdown, dev->net,
601 "rx shutdown, code %d\n", urb_status);
1da177e4
LT
602 goto block;
603
18ab458f
DB
604 /* we get controller i/o faults during khubd disconnect() delays.
605 * throttle down resubmits, to avoid log floods; just temporarily,
606 * so we still recover when the fault isn't a khubd delay.
607 */
608 case -EPROTO:
609 case -ETIME:
610 case -EILSEQ:
7963837f 611 dev->net->stats.rx_errors++;
1da177e4
LT
612 if (!timer_pending (&dev->delay)) {
613 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
a475f603
JP
614 netif_dbg(dev, link, dev->net,
615 "rx throttle %d\n", urb_status);
1da177e4
LT
616 }
617block:
5b6e9bcd 618 state = rx_cleanup;
1da177e4
LT
619 entry->urb = urb;
620 urb = NULL;
621 break;
622
18ab458f
DB
623 /* data overrun ... flush fifo? */
624 case -EOVERFLOW:
7963837f 625 dev->net->stats.rx_over_errors++;
1da177e4 626 // FALLTHROUGH
cb1cebbe 627
18ab458f 628 default:
5b6e9bcd 629 state = rx_cleanup;
7963837f 630 dev->net->stats.rx_errors++;
a475f603 631 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
1da177e4
LT
632 break;
633 }
634
70c37bf9
BM
635 /* stop rx if packet error rate is high */
636 if (++dev->pkt_cnt > 30) {
637 dev->pkt_cnt = 0;
638 dev->pkt_err = 0;
639 } else {
640 if (state == rx_cleanup)
641 dev->pkt_err++;
642 if (dev->pkt_err > 20)
643 set_bit(EVENT_RX_KILL, &dev->flags);
644 }
645
5b6e9bcd 646 state = defer_bh(dev, skb, &dev->rxq, state);
1da177e4
LT
647
648 if (urb) {
8e95a202 649 if (netif_running (dev->net) &&
5b6e9bcd
ML
650 !test_bit (EVENT_RX_HALT, &dev->flags) &&
651 state != unlink_start) {
1da177e4 652 rx_submit (dev, urb, GFP_ATOMIC);
8a783354 653 usb_mark_last_busy(dev->udev);
1da177e4
LT
654 return;
655 }
656 usb_free_urb (urb);
657 }
a475f603 658 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
1da177e4
LT
659}
660
7834ddbc
JK
661/*-------------------------------------------------------------------------*/
662void usbnet_pause_rx(struct usbnet *dev)
663{
664 set_bit(EVENT_RX_PAUSED, &dev->flags);
665
a475f603 666 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
7834ddbc
JK
667}
668EXPORT_SYMBOL_GPL(usbnet_pause_rx);
669
670void usbnet_resume_rx(struct usbnet *dev)
671{
672 struct sk_buff *skb;
673 int num = 0;
674
675 clear_bit(EVENT_RX_PAUSED, &dev->flags);
676
677 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
678 usbnet_skb_return(dev, skb);
679 num++;
680 }
681
682 tasklet_schedule(&dev->bh);
683
a475f603
JP
684 netif_dbg(dev, rx_status, dev->net,
685 "paused rx queue disabled, %d skbs requeued\n", num);
7834ddbc
JK
686}
687EXPORT_SYMBOL_GPL(usbnet_resume_rx);
688
689void usbnet_purge_paused_rxq(struct usbnet *dev)
690{
691 skb_queue_purge(&dev->rxq_pause);
692}
693EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
694
1da177e4
LT
695/*-------------------------------------------------------------------------*/
696
697// unlink pending rx/tx; completion handlers do all other cleanup
698
699static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
700{
701 unsigned long flags;
5b6e9bcd 702 struct sk_buff *skb;
1da177e4
LT
703 int count = 0;
704
705 spin_lock_irqsave (&q->lock, flags);
5b6e9bcd 706 while (!skb_queue_empty(q)) {
1da177e4
LT
707 struct skb_data *entry;
708 struct urb *urb;
709 int retval;
710
5b6e9bcd
ML
711 skb_queue_walk(q, skb) {
712 entry = (struct skb_data *) skb->cb;
713 if (entry->state != unlink_start)
714 goto found;
715 }
716 break;
717found:
718 entry->state = unlink_start;
1da177e4 719 urb = entry->urb;
1da177e4 720
0956a8c2 721 /*
722 * Get reference count of the URB to avoid it to be
723 * freed during usb_unlink_urb, which may trigger
724 * use-after-free problem inside usb_unlink_urb since
725 * usb_unlink_urb is always racing with .complete
726 * handler(include defer_bh).
727 */
728 usb_get_urb(urb);
4231d47e 729 spin_unlock_irqrestore(&q->lock, flags);
1da177e4
LT
730 // during some PM-driven resume scenarios,
731 // these (async) unlinks complete immediately
732 retval = usb_unlink_urb (urb);
733 if (retval != -EINPROGRESS && retval != 0)
60b86755 734 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
1da177e4
LT
735 else
736 count++;
0956a8c2 737 usb_put_urb(urb);
4231d47e 738 spin_lock_irqsave(&q->lock, flags);
1da177e4
LT
739 }
740 spin_unlock_irqrestore (&q->lock, flags);
741 return count;
742}
743
a99c1949
JP
744// Flush all pending rx urbs
745// minidrivers may need to do this when the MTU changes
746
747void usbnet_unlink_rx_urbs(struct usbnet *dev)
748{
749 if (netif_running(dev->net)) {
750 (void) unlink_urbs (dev, &dev->rxq);
751 tasklet_schedule(&dev->bh);
752 }
753}
754EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
1da177e4
LT
755
756/*-------------------------------------------------------------------------*/
757
758// precondition: never called in_interrupt
69ee472f
ON
759static void usbnet_terminate_urbs(struct usbnet *dev)
760{
761 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
762 DECLARE_WAITQUEUE(wait, current);
763 int temp;
764
765 /* ensure there are no more active urbs */
766 add_wait_queue(&unlink_wakeup, &wait);
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 dev->wait = &unlink_wakeup;
769 temp = unlink_urbs(dev, &dev->txq) +
770 unlink_urbs(dev, &dev->rxq);
771
772 /* maybe wait for deletions to finish. */
773 while (!skb_queue_empty(&dev->rxq)
774 && !skb_queue_empty(&dev->txq)
775 && !skb_queue_empty(&dev->done)) {
66cc42a4 776 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
69ee472f 777 set_current_state(TASK_UNINTERRUPTIBLE);
a475f603
JP
778 netif_dbg(dev, ifdown, dev->net,
779 "waited for %d urb completions\n", temp);
69ee472f
ON
780 }
781 set_current_state(TASK_RUNNING);
782 dev->wait = NULL;
783 remove_wait_queue(&unlink_wakeup, &wait);
784}
1da177e4 785
777baa47 786int usbnet_stop (struct net_device *net)
1da177e4
LT
787{
788 struct usbnet *dev = netdev_priv(net);
a33e9e7f 789 struct driver_info *info = dev->driver_info;
a33e9e7f 790 int retval;
1da177e4 791
75bd0cbd 792 clear_bit(EVENT_DEV_OPEN, &dev->flags);
1da177e4
LT
793 netif_stop_queue (net);
794
a475f603 795 netif_info(dev, ifdown, dev->net,
8ccef431 796 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
a475f603
JP
797 net->stats.rx_packets, net->stats.tx_packets,
798 net->stats.rx_errors, net->stats.tx_errors);
1da177e4 799
a33e9e7f
JK
800 /* allow minidriver to stop correctly (wireless devices to turn off
801 * radio etc) */
802 if (info->stop) {
803 retval = info->stop(dev);
a475f603
JP
804 if (retval < 0)
805 netif_info(dev, ifdown, dev->net,
806 "stop fail (%d) usbnet usb-%s-%s, %s\n",
807 retval,
808 dev->udev->bus->bus_name, dev->udev->devpath,
809 info->description);
a33e9e7f
JK
810 }
811
69ee472f
ON
812 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
813 usbnet_terminate_urbs(dev);
1da177e4 814
6eecdc5f 815 usbnet_status_stop(dev);
1da177e4 816
7834ddbc
JK
817 usbnet_purge_paused_rxq(dev);
818
1da177e4
LT
819 /* deferred work (task, timer, softirq) must also stop.
820 * can't flush_scheduled_work() until we drop rtnl (later),
821 * else workers could deadlock; so make workers a NOP.
822 */
823 dev->flags = 0;
824 del_timer_sync (&dev->delay);
825 tasklet_kill (&dev->bh);
a1c088e0
ON
826 if (info->manage_power &&
827 !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags))
69ee472f
ON
828 info->manage_power(dev, 0);
829 else
830 usb_autopm_put_interface(dev->intf);
1da177e4
LT
831
832 return 0;
833}
777baa47 834EXPORT_SYMBOL_GPL(usbnet_stop);
1da177e4
LT
835
836/*-------------------------------------------------------------------------*/
837
838// posts reads, and enables write queuing
839
840// precondition: never called in_interrupt
841
777baa47 842int usbnet_open (struct net_device *net)
1da177e4
LT
843{
844 struct usbnet *dev = netdev_priv(net);
a11a6544 845 int retval;
1da177e4
LT
846 struct driver_info *info = dev->driver_info;
847
a11a6544 848 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
a475f603
JP
849 netif_info(dev, ifup, dev->net,
850 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
851 retval,
852 dev->udev->bus->bus_name,
853 dev->udev->devpath,
854 info->description);
a11a6544
ON
855 goto done_nopm;
856 }
857
1da177e4
LT
858 // put into "known safe" state
859 if (info->reset && (retval = info->reset (dev)) < 0) {
a475f603
JP
860 netif_info(dev, ifup, dev->net,
861 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
862 retval,
863 dev->udev->bus->bus_name,
864 dev->udev->devpath,
865 info->description);
1da177e4
LT
866 goto done;
867 }
868
a88c32ae
ML
869 /* hard_mtu or rx_urb_size may change in reset() */
870 usbnet_update_max_qlen(dev);
871
1da177e4
LT
872 // insist peer be connected
873 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
a475f603 874 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
1da177e4
LT
875 goto done;
876 }
877
878 /* start any status interrupt transfer */
879 if (dev->interrupt) {
6eecdc5f 880 retval = usbnet_status_start(dev, GFP_KERNEL);
1da177e4 881 if (retval < 0) {
a475f603
JP
882 netif_err(dev, ifup, dev->net,
883 "intr submit %d\n", retval);
1da177e4
LT
884 goto done;
885 }
886 }
887
68972efa 888 set_bit(EVENT_DEV_OPEN, &dev->flags);
1da177e4 889 netif_start_queue (net);
a475f603
JP
890 netif_info(dev, ifup, dev->net,
891 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
892 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
893 dev->net->mtu,
894 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
895 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
896 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
897 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
898 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
899 "simple");
1da177e4 900
70c37bf9
BM
901 /* reset rx error state */
902 dev->pkt_cnt = 0;
903 dev->pkt_err = 0;
904 clear_bit(EVENT_RX_KILL, &dev->flags);
905
1da177e4
LT
906 // delay posting reads until we're fully open
907 tasklet_schedule (&dev->bh);
69ee472f
ON
908 if (info->manage_power) {
909 retval = info->manage_power(dev, 1);
a1c088e0
ON
910 if (retval < 0) {
911 retval = 0;
912 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
913 } else {
914 usb_autopm_put_interface(dev->intf);
915 }
69ee472f 916 }
a11a6544 917 return retval;
1da177e4 918done:
a11a6544
ON
919 usb_autopm_put_interface(dev->intf);
920done_nopm:
1da177e4
LT
921 return retval;
922}
777baa47 923EXPORT_SYMBOL_GPL(usbnet_open);
1da177e4
LT
924
925/*-------------------------------------------------------------------------*/
926
2e55cc72
DB
927/* ethtool methods; minidrivers may need to add some more, but
928 * they'll probably want to use this base set.
929 */
930
c41286fd
AB
931int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
932{
933 struct usbnet *dev = netdev_priv(net);
934
935 if (!dev->mii.mdio_read)
936 return -EOPNOTSUPP;
937
938 return mii_ethtool_gset(&dev->mii, cmd);
939}
940EXPORT_SYMBOL_GPL(usbnet_get_settings);
941
942int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
943{
944 struct usbnet *dev = netdev_priv(net);
945 int retval;
946
947 if (!dev->mii.mdio_write)
948 return -EOPNOTSUPP;
949
950 retval = mii_ethtool_sset(&dev->mii, cmd);
951
952 /* link speed/duplex might have changed */
953 if (dev->driver_info->link_reset)
954 dev->driver_info->link_reset(dev);
955
a88c32ae
ML
956 /* hard_mtu or rx_urb_size may change in link_reset() */
957 usbnet_update_max_qlen(dev);
958
c41286fd
AB
959 return retval;
960
961}
962EXPORT_SYMBOL_GPL(usbnet_set_settings);
963
c41286fd 964u32 usbnet_get_link (struct net_device *net)
1da177e4
LT
965{
966 struct usbnet *dev = netdev_priv(net);
967
f29fc259 968 /* If a check_connect is defined, return its result */
1da177e4
LT
969 if (dev->driver_info->check_connect)
970 return dev->driver_info->check_connect (dev) == 0;
971
c41286fd
AB
972 /* if the device has mii operations, use those */
973 if (dev->mii.mdio_read)
974 return mii_link_ok(&dev->mii);
975
05ffb3e2
BM
976 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
977 return ethtool_op_get_link(net);
1da177e4 978}
c41286fd 979EXPORT_SYMBOL_GPL(usbnet_get_link);
1da177e4 980
18ee91fa 981int usbnet_nway_reset(struct net_device *net)
1da177e4
LT
982{
983 struct usbnet *dev = netdev_priv(net);
984
18ee91fa
DB
985 if (!dev->mii.mdio_write)
986 return -EOPNOTSUPP;
987
988 return mii_nway_restart(&dev->mii);
1da177e4 989}
18ee91fa 990EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1da177e4 991
18ee91fa 992void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1da177e4
LT
993{
994 struct usbnet *dev = netdev_priv(net);
995
86a2f415
PS
996 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
997 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
998 strlcpy (info->fw_version, dev->driver_info->description,
18ee91fa
DB
999 sizeof info->fw_version);
1000 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1da177e4 1001}
18ee91fa 1002EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1da177e4 1003
18ee91fa 1004u32 usbnet_get_msglevel (struct net_device *net)
c41286fd
AB
1005{
1006 struct usbnet *dev = netdev_priv(net);
1007
18ee91fa
DB
1008 return dev->msg_enable;
1009}
1010EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
c41286fd 1011
18ee91fa
DB
1012void usbnet_set_msglevel (struct net_device *net, u32 level)
1013{
1014 struct usbnet *dev = netdev_priv(net);
1015
1016 dev->msg_enable = level;
c41286fd 1017}
18ee91fa 1018EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
c41286fd 1019
2e55cc72 1020/* drivers may override default ethtool_ops in their bind() routine */
0fc0b732 1021static const struct ethtool_ops usbnet_ethtool_ops = {
c41286fd
AB
1022 .get_settings = usbnet_get_settings,
1023 .set_settings = usbnet_set_settings,
2e55cc72 1024 .get_link = usbnet_get_link,
c41286fd 1025 .nway_reset = usbnet_nway_reset,
18ee91fa 1026 .get_drvinfo = usbnet_get_drvinfo,
2e55cc72
DB
1027 .get_msglevel = usbnet_get_msglevel,
1028 .set_msglevel = usbnet_set_msglevel,
123edb18 1029 .get_ts_info = ethtool_op_get_ts_info,
2e55cc72 1030};
1da177e4
LT
1031
1032/*-------------------------------------------------------------------------*/
1033
4b49f58f
ML
1034static void __handle_link_change(struct usbnet *dev)
1035{
1036 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1037 return;
1038
1039 if (!netif_carrier_ok(dev->net)) {
1040 /* kill URBs for reading packets to save bus bandwidth */
1041 unlink_urbs(dev, &dev->rxq);
1042
1043 /*
1044 * tx_timeout will unlink URBs for sending packets and
1045 * tx queue is stopped by netcore after link becomes off
1046 */
1047 } else {
1048 /* submitting URBs for reading packets */
1049 tasklet_schedule(&dev->bh);
1050 }
1051
a88c32ae
ML
1052 /* hard_mtu or rx_urb_size may change during link change */
1053 usbnet_update_max_qlen(dev);
1054
4b49f58f
ML
1055 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1056}
1057
1da177e4
LT
1058/* work that cannot be done in interrupt context uses keventd.
1059 *
1060 * NOTE: with 2.5 we could do more of this using completion callbacks,
1061 * especially now that control transfers can be queued.
1062 */
1063static void
c4028958 1064kevent (struct work_struct *work)
1da177e4 1065{
c4028958
DH
1066 struct usbnet *dev =
1067 container_of(work, struct usbnet, kevent);
1da177e4
LT
1068 int status;
1069
1070 /* usb_clear_halt() needs a thread context */
1071 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1072 unlink_urbs (dev, &dev->txq);
69ee472f
ON
1073 status = usb_autopm_get_interface(dev->intf);
1074 if (status < 0)
1075 goto fail_pipe;
1da177e4 1076 status = usb_clear_halt (dev->udev, dev->out);
69ee472f 1077 usb_autopm_put_interface(dev->intf);
8e95a202
JP
1078 if (status < 0 &&
1079 status != -EPIPE &&
1080 status != -ESHUTDOWN) {
1da177e4 1081 if (netif_msg_tx_err (dev))
69ee472f 1082fail_pipe:
60b86755
JP
1083 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1084 status);
1da177e4
LT
1085 } else {
1086 clear_bit (EVENT_TX_HALT, &dev->flags);
f29fc259
DB
1087 if (status != -ESHUTDOWN)
1088 netif_wake_queue (dev->net);
1da177e4
LT
1089 }
1090 }
1091 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1092 unlink_urbs (dev, &dev->rxq);
69ee472f
ON
1093 status = usb_autopm_get_interface(dev->intf);
1094 if (status < 0)
1095 goto fail_halt;
1da177e4 1096 status = usb_clear_halt (dev->udev, dev->in);
69ee472f 1097 usb_autopm_put_interface(dev->intf);
8e95a202
JP
1098 if (status < 0 &&
1099 status != -EPIPE &&
1100 status != -ESHUTDOWN) {
1da177e4 1101 if (netif_msg_rx_err (dev))
69ee472f 1102fail_halt:
60b86755
JP
1103 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1104 status);
1da177e4
LT
1105 } else {
1106 clear_bit (EVENT_RX_HALT, &dev->flags);
1107 tasklet_schedule (&dev->bh);
1108 }
1109 }
1110
1111 /* tasklet could resubmit itself forever if memory is tight */
1112 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1113 struct urb *urb = NULL;
dacb3975 1114 int resched = 1;
1da177e4
LT
1115
1116 if (netif_running (dev->net))
1117 urb = usb_alloc_urb (0, GFP_KERNEL);
1118 else
1119 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1120 if (urb != NULL) {
1121 clear_bit (EVENT_RX_MEMORY, &dev->flags);
69ee472f 1122 status = usb_autopm_get_interface(dev->intf);
ab60707f
JJ
1123 if (status < 0) {
1124 usb_free_urb(urb);
69ee472f 1125 goto fail_lowmem;
ab60707f 1126 }
dacb3975
DM
1127 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1128 resched = 0;
69ee472f
ON
1129 usb_autopm_put_interface(dev->intf);
1130fail_lowmem:
dacb3975
DM
1131 if (resched)
1132 tasklet_schedule (&dev->bh);
1da177e4
LT
1133 }
1134 }
1135
7ea13c9c 1136 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
cb1cebbe 1137 struct driver_info *info = dev->driver_info;
7ea13c9c
DH
1138 int retval = 0;
1139
1140 clear_bit (EVENT_LINK_RESET, &dev->flags);
69ee472f
ON
1141 status = usb_autopm_get_interface(dev->intf);
1142 if (status < 0)
1143 goto skip_reset;
7ea13c9c 1144 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
69ee472f
ON
1145 usb_autopm_put_interface(dev->intf);
1146skip_reset:
60b86755
JP
1147 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1148 retval,
1149 dev->udev->bus->bus_name,
1150 dev->udev->devpath,
1151 info->description);
69ee472f
ON
1152 } else {
1153 usb_autopm_put_interface(dev->intf);
7ea13c9c 1154 }
4b49f58f
ML
1155
1156 /* handle link change from link resetting */
1157 __handle_link_change(dev);
7ea13c9c
DH
1158 }
1159
4b49f58f
ML
1160 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1161 __handle_link_change(dev);
1162
1da177e4 1163 if (dev->flags)
60b86755 1164 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1da177e4
LT
1165}
1166
1167/*-------------------------------------------------------------------------*/
1168
7d12e780 1169static void tx_complete (struct urb *urb)
1da177e4
LT
1170{
1171 struct sk_buff *skb = (struct sk_buff *) urb->context;
1172 struct skb_data *entry = (struct skb_data *) skb->cb;
1173 struct usbnet *dev = entry->dev;
1174
1175 if (urb->status == 0) {
073285fd
AO
1176 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
1177 dev->net->stats.tx_packets++;
7963837f 1178 dev->net->stats.tx_bytes += entry->length;
1da177e4 1179 } else {
7963837f 1180 dev->net->stats.tx_errors++;
1da177e4
LT
1181
1182 switch (urb->status) {
1183 case -EPIPE:
2e55cc72 1184 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
1185 break;
1186
1187 /* software-driven interface shutdown */
1188 case -ECONNRESET: // async unlink
1189 case -ESHUTDOWN: // hardware gone
1190 break;
1191
1192 // like rx, tx gets controller i/o faults during khubd delays
1193 // and so it uses the same throttling mechanism.
38e2bfc9
PZ
1194 case -EPROTO:
1195 case -ETIME:
1196 case -EILSEQ:
69ee472f 1197 usb_mark_last_busy(dev->udev);
1da177e4
LT
1198 if (!timer_pending (&dev->delay)) {
1199 mod_timer (&dev->delay,
1200 jiffies + THROTTLE_JIFFIES);
a475f603
JP
1201 netif_dbg(dev, link, dev->net,
1202 "tx throttle %d\n", urb->status);
1da177e4
LT
1203 }
1204 netif_stop_queue (dev->net);
1205 break;
1206 default:
a475f603
JP
1207 netif_dbg(dev, tx_err, dev->net,
1208 "tx err %d\n", entry->urb->status);
1da177e4
LT
1209 break;
1210 }
1211 }
1212
69ee472f 1213 usb_autopm_put_interface_async(dev->intf);
5b6e9bcd 1214 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1da177e4
LT
1215}
1216
1217/*-------------------------------------------------------------------------*/
1218
777baa47 1219void usbnet_tx_timeout (struct net_device *net)
1da177e4
LT
1220{
1221 struct usbnet *dev = netdev_priv(net);
1222
1223 unlink_urbs (dev, &dev->txq);
1224 tasklet_schedule (&dev->bh);
1225
1226 // FIXME: device recovery -- reset?
1227}
777baa47 1228EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1da177e4
LT
1229
1230/*-------------------------------------------------------------------------*/
1231
638c5115
ML
1232static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1233{
1234 unsigned num_sgs, total_len = 0;
1235 int i, s = 0;
1236
1237 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1238 if (num_sgs == 1)
1239 return 0;
1240
60e453a9
ML
1241 /* reserve one for zero packet */
1242 urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
1243 GFP_ATOMIC);
638c5115
ML
1244 if (!urb->sg)
1245 return -ENOMEM;
1246
1247 urb->num_sgs = num_sgs;
1248 sg_init_table(urb->sg, urb->num_sgs);
1249
1250 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1251 total_len += skb_headlen(skb);
1252
1253 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1254 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1255
1256 total_len += skb_frag_size(f);
1257 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1258 f->page_offset);
1259 }
1260 urb->transfer_buffer_length = total_len;
1261
1262 return 1;
1263}
1264
25a79c41
SH
1265netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1266 struct net_device *net)
1da177e4
LT
1267{
1268 struct usbnet *dev = netdev_priv(net);
1269 int length;
1da177e4
LT
1270 struct urb *urb = NULL;
1271 struct skb_data *entry;
1272 struct driver_info *info = dev->driver_info;
1273 unsigned long flags;
25a79c41 1274 int retval;
1da177e4 1275
23ba0799
KK
1276 if (skb)
1277 skb_tx_timestamp(skb);
f9b491ec 1278
1da177e4
LT
1279 // some devices want funky USB-level framing, for
1280 // win32 driver (usually) and/or hardware quirks
1281 if (info->tx_fixup) {
1282 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1283 if (!skb) {
bf414b36
BM
1284 /* packet collected; minidriver waiting for more */
1285 if (info->flags & FLAG_MULTI_PACKET)
073285fd 1286 goto not_drop;
bf414b36
BM
1287 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1288 goto drop;
1da177e4
LT
1289 }
1290 }
1da177e4
LT
1291
1292 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
a475f603 1293 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1da177e4
LT
1294 goto drop;
1295 }
1296
1297 entry = (struct skb_data *) skb->cb;
1298 entry->urb = urb;
1299 entry->dev = dev;
1da177e4 1300
1da177e4
LT
1301 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1302 skb->data, skb->len, tx_complete, skb);
638c5115
ML
1303 if (dev->can_dma_sg) {
1304 if (build_dma_sg(skb, urb) < 0)
1305 goto drop;
1306 }
60e453a9 1307 length = urb->transfer_buffer_length;
1da177e4
LT
1308
1309 /* don't assume the hardware handles USB_ZERO_PACKET
1310 * NOTE: strictly conforming cdc-ether devices should expect
1311 * the ZLP here, but ignore the one-byte packet.
073285fd
AO
1312 * NOTE2: CDC NCM specification is different from CDC ECM when
1313 * handling ZLP/short packets, so cdc_ncm driver will make short
1314 * packet itself if needed.
1da177e4 1315 */
b4d562e3
EP
1316 if (length % dev->maxpacket == 0) {
1317 if (!(info->flags & FLAG_SEND_ZLP)) {
073285fd 1318 if (!(info->flags & FLAG_MULTI_PACKET)) {
60e453a9
ML
1319 length++;
1320 if (skb_tailroom(skb) && !urb->num_sgs) {
073285fd
AO
1321 skb->data[skb->len] = 0;
1322 __skb_put(skb, 1);
60e453a9
ML
1323 } else if (urb->num_sgs)
1324 sg_set_buf(&urb->sg[urb->num_sgs++],
1325 dev->padding_pkt, 1);
b4d562e3
EP
1326 }
1327 } else
1328 urb->transfer_flags |= URB_ZERO_PACKET;
3e323f3e 1329 }
60e453a9 1330 entry->length = urb->transfer_buffer_length = length;
1da177e4 1331
69ee472f
ON
1332 spin_lock_irqsave(&dev->txq.lock, flags);
1333 retval = usb_autopm_get_interface_async(dev->intf);
1334 if (retval < 0) {
1335 spin_unlock_irqrestore(&dev->txq.lock, flags);
1336 goto drop;
1337 }
1338
1339#ifdef CONFIG_PM
1340 /* if this triggers the device is still a sleep */
1341 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1342 /* transmission will be done in resume */
1343 usb_anchor_urb(urb, &dev->deferred);
1344 /* no use to process more packets */
1345 netif_stop_queue(net);
39707c2a 1346 usb_put_urb(urb);
69ee472f 1347 spin_unlock_irqrestore(&dev->txq.lock, flags);
60b86755 1348 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
69ee472f
ON
1349 goto deferred;
1350 }
1351#endif
1da177e4 1352
1da177e4
LT
1353 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1354 case -EPIPE:
1355 netif_stop_queue (net);
2e55cc72 1356 usbnet_defer_kevent (dev, EVENT_TX_HALT);
69ee472f 1357 usb_autopm_put_interface_async(dev->intf);
1da177e4
LT
1358 break;
1359 default:
69ee472f 1360 usb_autopm_put_interface_async(dev->intf);
a475f603
JP
1361 netif_dbg(dev, tx_err, dev->net,
1362 "tx: submit urb err %d\n", retval);
1da177e4
LT
1363 break;
1364 case 0:
1365 net->trans_start = jiffies;
5b6e9bcd 1366 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1da177e4
LT
1367 if (dev->txq.qlen >= TX_QLEN (dev))
1368 netif_stop_queue (net);
1369 }
1370 spin_unlock_irqrestore (&dev->txq.lock, flags);
1371
1372 if (retval) {
a475f603 1373 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1da177e4 1374drop:
7963837f 1375 dev->net->stats.tx_dropped++;
073285fd 1376not_drop:
1da177e4
LT
1377 if (skb)
1378 dev_kfree_skb_any (skb);
638c5115
ML
1379 if (urb) {
1380 kfree(urb->sg);
1381 usb_free_urb(urb);
1382 }
a475f603
JP
1383 } else
1384 netif_dbg(dev, tx_queued, dev->net,
1385 "> tx, len %d, type 0x%x\n", length, skb->protocol);
69ee472f
ON
1386#ifdef CONFIG_PM
1387deferred:
1388#endif
25a79c41 1389 return NETDEV_TX_OK;
1da177e4 1390}
777baa47 1391EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1da177e4 1392
85e87870 1393static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
65841fd5
ML
1394{
1395 struct urb *urb;
1396 int i;
85e87870 1397 int ret = 0;
65841fd5
ML
1398
1399 /* don't refill the queue all at once */
1400 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1401 urb = usb_alloc_urb(0, flags);
1402 if (urb != NULL) {
85e87870
BM
1403 ret = rx_submit(dev, urb, flags);
1404 if (ret)
1405 goto err;
1406 } else {
1407 ret = -ENOMEM;
1408 goto err;
65841fd5
ML
1409 }
1410 }
85e87870
BM
1411err:
1412 return ret;
65841fd5
ML
1413}
1414
1da177e4
LT
1415/*-------------------------------------------------------------------------*/
1416
1417// tasklet (work deferred from completions, in_irq) or timer
1418
1419static void usbnet_bh (unsigned long param)
1420{
1421 struct usbnet *dev = (struct usbnet *) param;
1422 struct sk_buff *skb;
1423 struct skb_data *entry;
1424
1425 while ((skb = skb_dequeue (&dev->done))) {
1426 entry = (struct skb_data *) skb->cb;
1427 switch (entry->state) {
18ab458f 1428 case rx_done:
1da177e4
LT
1429 entry->state = rx_cleanup;
1430 rx_process (dev, skb);
1431 continue;
18ab458f 1432 case tx_done:
638c5115 1433 kfree(entry->urb->sg);
18ab458f 1434 case rx_cleanup:
1da177e4
LT
1435 usb_free_urb (entry->urb);
1436 dev_kfree_skb (skb);
1437 continue;
18ab458f 1438 default:
60b86755 1439 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1da177e4
LT
1440 }
1441 }
1442
70c37bf9
BM
1443 /* restart RX again after disabling due to high error rate */
1444 clear_bit(EVENT_RX_KILL, &dev->flags);
1445
1da177e4
LT
1446 // waiting for all pending urbs to complete?
1447 if (dev->wait) {
1448 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1449 wake_up (dev->wait);
1450 }
1451
1452 // or are we maybe short a few urbs?
8e95a202
JP
1453 } else if (netif_running (dev->net) &&
1454 netif_device_present (dev->net) &&
4b49f58f 1455 netif_carrier_ok(dev->net) &&
8e95a202
JP
1456 !timer_pending (&dev->delay) &&
1457 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1da177e4 1458 int temp = dev->rxq.qlen;
65841fd5
ML
1459
1460 if (temp < RX_QLEN(dev)) {
85e87870
BM
1461 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1462 return;
a475f603
JP
1463 if (temp != dev->rxq.qlen)
1464 netif_dbg(dev, link, dev->net,
1465 "rxqlen %d --> %d\n",
1466 temp, dev->rxq.qlen);
65841fd5 1467 if (dev->rxq.qlen < RX_QLEN(dev))
1da177e4
LT
1468 tasklet_schedule (&dev->bh);
1469 }
1470 if (dev->txq.qlen < TX_QLEN (dev))
1471 netif_wake_queue (dev->net);
1472 }
1473}
1474
1475
1da177e4
LT
1476/*-------------------------------------------------------------------------
1477 *
1478 * USB Device Driver support
1479 *
1480 *-------------------------------------------------------------------------*/
cb1cebbe 1481
1da177e4
LT
1482// precondition: never called in_interrupt
1483
38bde1d4 1484void usbnet_disconnect (struct usb_interface *intf)
1da177e4
LT
1485{
1486 struct usbnet *dev;
1487 struct usb_device *xdev;
1488 struct net_device *net;
1489
1490 dev = usb_get_intfdata(intf);
1491 usb_set_intfdata(intf, NULL);
1492 if (!dev)
1493 return;
1494
1495 xdev = interface_to_usbdev (intf);
1496
a475f603
JP
1497 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1498 intf->dev.driver->name,
1499 xdev->bus->bus_name, xdev->devpath,
1500 dev->driver_info->description);
cb1cebbe 1501
1da177e4
LT
1502 net = dev->net;
1503 unregister_netdev (net);
1504
23f333a2 1505 cancel_work_sync(&dev->kevent);
1da177e4 1506
39707c2a
HK
1507 usb_scuttle_anchored_urbs(&dev->deferred);
1508
1da177e4
LT
1509 if (dev->driver_info->unbind)
1510 dev->driver_info->unbind (dev, intf);
1511
68972efa
PS
1512 usb_kill_urb(dev->interrupt);
1513 usb_free_urb(dev->interrupt);
60e453a9 1514 kfree(dev->padding_pkt);
68972efa 1515
1da177e4 1516 free_netdev(net);
1da177e4 1517}
38bde1d4 1518EXPORT_SYMBOL_GPL(usbnet_disconnect);
1da177e4 1519
777baa47
SH
1520static const struct net_device_ops usbnet_netdev_ops = {
1521 .ndo_open = usbnet_open,
1522 .ndo_stop = usbnet_stop,
1523 .ndo_start_xmit = usbnet_start_xmit,
1524 .ndo_tx_timeout = usbnet_tx_timeout,
1525 .ndo_change_mtu = usbnet_change_mtu,
1526 .ndo_set_mac_address = eth_mac_addr,
1527 .ndo_validate_addr = eth_validate_addr,
1528};
1da177e4
LT
1529
1530/*-------------------------------------------------------------------------*/
1531
1da177e4
LT
1532// precondition: never called in_interrupt
1533
225794f8
MH
1534static struct device_type wlan_type = {
1535 .name = "wlan",
1536};
1537
1538static struct device_type wwan_type = {
1539 .name = "wwan",
1540};
1541
38bde1d4 1542int
1da177e4
LT
1543usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1544{
1545 struct usbnet *dev;
cb1cebbe 1546 struct net_device *net;
1da177e4
LT
1547 struct usb_host_interface *interface;
1548 struct driver_info *info;
1549 struct usb_device *xdev;
1550 int status;
296c0242 1551 const char *name;
b0786b43
ML
1552 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1553
1554 /* usbnet already took usb runtime pm, so have to enable the feature
1555 * for usb interface, otherwise usb_autopm_get_interface may return
98f541c6 1556 * failure if RUNTIME_PM is enabled.
b0786b43
ML
1557 */
1558 if (!driver->supports_autosuspend) {
1559 driver->supports_autosuspend = 1;
1560 pm_runtime_enable(&udev->dev);
1561 }
1da177e4 1562
296c0242 1563 name = udev->dev.driver->name;
1da177e4
LT
1564 info = (struct driver_info *) prod->driver_info;
1565 if (!info) {
296c0242 1566 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1da177e4
LT
1567 return -ENODEV;
1568 }
1569 xdev = interface_to_usbdev (udev);
1570 interface = udev->cur_altsetting;
1571
1da177e4
LT
1572 status = -ENOMEM;
1573
1574 // set up our own records
1575 net = alloc_etherdev(sizeof(*dev));
41de8d4c 1576 if (!net)
1da177e4 1577 goto out;
1da177e4 1578
0dacca73
BH
1579 /* netdev_printk() needs this so do it as early as possible */
1580 SET_NETDEV_DEV(net, &udev->dev);
1581
1da177e4
LT
1582 dev = netdev_priv(net);
1583 dev->udev = xdev;
a11a6544 1584 dev->intf = udev;
1da177e4 1585 dev->driver_info = info;
296c0242 1586 dev->driver_name = name;
1da177e4
LT
1587 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1588 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1589 skb_queue_head_init (&dev->rxq);
1590 skb_queue_head_init (&dev->txq);
1591 skb_queue_head_init (&dev->done);
7834ddbc 1592 skb_queue_head_init(&dev->rxq_pause);
1da177e4
LT
1593 dev->bh.func = usbnet_bh;
1594 dev->bh.data = (unsigned long) dev;
c4028958 1595 INIT_WORK (&dev->kevent, kevent);
69ee472f 1596 init_usb_anchor(&dev->deferred);
1da177e4
LT
1597 dev->delay.function = usbnet_bh;
1598 dev->delay.data = (unsigned long) dev;
1599 init_timer (&dev->delay);
a9fc6338 1600 mutex_init (&dev->phy_mutex);
6eecdc5f
DW
1601 mutex_init(&dev->interrupt_mutex);
1602 dev->interrupt_count = 0;
1da177e4 1603
1da177e4
LT
1604 dev->net = net;
1605 strcpy (net->name, "usb%d");
1606 memcpy (net->dev_addr, node_id, sizeof node_id);
1607
2e55cc72
DB
1608 /* rx and tx sides can use different message sizes;
1609 * bind() should set rx_urb_size in that case.
1610 */
1611 dev->hard_mtu = net->mtu + net->hard_header_len;
1da177e4
LT
1612#if 0
1613// dma_supported() is deeply broken on almost all architectures
1614 // possible with some EHCI controllers
6a35528a 1615 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1da177e4
LT
1616 net->features |= NETIF_F_HIGHDMA;
1617#endif
1618
777baa47 1619 net->netdev_ops = &usbnet_netdev_ops;
777baa47 1620 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1da177e4
LT
1621 net->ethtool_ops = &usbnet_ethtool_ops;
1622
1623 // allow device-specific bind/init procedures
1624 // NOTE net->name still not usable ...
1625 if (info->bind) {
1626 status = info->bind (dev, udev);
cb1cebbe
DB
1627 if (status < 0)
1628 goto out1;
1629
1da177e4
LT
1630 // heuristic: "usb%d" for links we know are two-host,
1631 // else "eth%d" when there's reasonable doubt. userspace
1632 // can rename the link if it knows better.
8e95a202 1633 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
c261344d
AB
1634 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1635 (net->dev_addr [0] & 0x02) == 0))
1da177e4 1636 strcpy (net->name, "eth%d");
6e3bbcc5
JK
1637 /* WLAN devices should always be named "wlan%d" */
1638 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1639 strcpy(net->name, "wlan%d");
e1e499ee
MH
1640 /* WWAN devices should always be named "wwan%d" */
1641 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1642 strcpy(net->name, "wwan%d");
f29fc259 1643
6509141f
WS
1644 /* devices that cannot do ARP */
1645 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1646 net->flags |= IFF_NOARP;
1647
f29fc259
DB
1648 /* maybe the remote can't receive an Ethernet MTU */
1649 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1650 net->mtu = dev->hard_mtu - net->hard_header_len;
2e55cc72
DB
1651 } else if (!info->in || !info->out)
1652 status = usbnet_get_endpoints (dev, udev);
1da177e4
LT
1653 else {
1654 dev->in = usb_rcvbulkpipe (xdev, info->in);
1655 dev->out = usb_sndbulkpipe (xdev, info->out);
1656 if (!(info->flags & FLAG_NO_SETINT))
1657 status = usb_set_interface (xdev,
1658 interface->desc.bInterfaceNumber,
1659 interface->desc.bAlternateSetting);
1660 else
1661 status = 0;
1662
1663 }
9514bfe5 1664 if (status >= 0 && dev->status)
1da177e4
LT
1665 status = init_status (dev, udev);
1666 if (status < 0)
cb1cebbe 1667 goto out3;
1da177e4 1668
2e55cc72
DB
1669 if (!dev->rx_urb_size)
1670 dev->rx_urb_size = dev->hard_mtu;
1da177e4 1671 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
cb1cebbe 1672
eef23b53
BM
1673 /* let userspace know we have a random address */
1674 if (ether_addr_equal(net->dev_addr, node_id))
1675 net->addr_assign_type = NET_ADDR_RANDOM;
1676
225794f8
MH
1677 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1678 SET_NETDEV_DEVTYPE(net, &wlan_type);
1679 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1680 SET_NETDEV_DEVTYPE(net, &wwan_type);
1681
a88c32ae
ML
1682 /* initialize max rx_qlen and tx_qlen */
1683 usbnet_update_max_qlen(dev);
1684
60e453a9
ML
1685 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1686 !(info->flags & FLAG_MULTI_PACKET)) {
1687 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
09b69024
WY
1688 if (!dev->padding_pkt) {
1689 status = -ENOMEM;
60e453a9 1690 goto out4;
09b69024 1691 }
60e453a9
ML
1692 }
1693
1da177e4
LT
1694 status = register_netdev (net);
1695 if (status)
60e453a9 1696 goto out5;
a475f603
JP
1697 netif_info(dev, probe, dev->net,
1698 "register '%s' at usb-%s-%s, %s, %pM\n",
1699 udev->dev.driver->name,
1700 xdev->bus->bus_name, xdev->devpath,
1701 dev->driver_info->description,
1702 net->dev_addr);
1da177e4
LT
1703
1704 // ok, it's ready to go.
1705 usb_set_intfdata (udev, dev);
1706
1da177e4
LT
1707 netif_device_attach (net);
1708
37e8273c 1709 if (dev->driver_info->flags & FLAG_LINK_INTR)
0162c554 1710 usbnet_link_change(dev, 0, 0);
37e8273c 1711
1da177e4
LT
1712 return 0;
1713
60e453a9
ML
1714out5:
1715 kfree(dev->padding_pkt);
a4723848 1716out4:
1717 usb_free_urb(dev->interrupt);
1da177e4
LT
1718out3:
1719 if (info->unbind)
1720 info->unbind (dev, udev);
1721out1:
1722 free_netdev(net);
1723out:
1da177e4
LT
1724 return status;
1725}
38bde1d4 1726EXPORT_SYMBOL_GPL(usbnet_probe);
1da177e4
LT
1727
1728/*-------------------------------------------------------------------------*/
1729
36433127
ON
1730/*
1731 * suspend the whole driver as soon as the first interface is suspended
1732 * resume only when the last interface is resumed
38bde1d4 1733 */
1da177e4 1734
38bde1d4 1735int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1736{
1737 struct usbnet *dev = usb_get_intfdata(intf);
cb1cebbe 1738
36433127 1739 if (!dev->suspend_count++) {
69ee472f
ON
1740 spin_lock_irq(&dev->txq.lock);
1741 /* don't autosuspend while transmitting */
5b1b0b81 1742 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
5eeb3132 1743 dev->suspend_count--;
69ee472f
ON
1744 spin_unlock_irq(&dev->txq.lock);
1745 return -EBUSY;
1746 } else {
1747 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1748 spin_unlock_irq(&dev->txq.lock);
1749 }
a11a6544
ON
1750 /*
1751 * accelerate emptying of the rx and queues, to avoid
36433127
ON
1752 * having everything error out.
1753 */
1754 netif_device_detach (dev->net);
69ee472f 1755 usbnet_terminate_urbs(dev);
6eecdc5f 1756 __usbnet_status_stop_force(dev);
69ee472f 1757
a11a6544
ON
1758 /*
1759 * reattach so runtime management can use and
1760 * wake the device
1761 */
1762 netif_device_attach (dev->net);
36433127 1763 }
1da177e4
LT
1764 return 0;
1765}
38bde1d4 1766EXPORT_SYMBOL_GPL(usbnet_suspend);
1da177e4 1767
38bde1d4 1768int usbnet_resume (struct usb_interface *intf)
1da177e4
LT
1769{
1770 struct usbnet *dev = usb_get_intfdata(intf);
69ee472f
ON
1771 struct sk_buff *skb;
1772 struct urb *res;
1773 int retval;
1774
1775 if (!--dev->suspend_count) {
6eecdc5f
DW
1776 /* resume interrupt URB if it was previously submitted */
1777 __usbnet_status_start_force(dev, GFP_NOIO);
68972efa 1778
69ee472f
ON
1779 spin_lock_irq(&dev->txq.lock);
1780 while ((res = usb_get_from_anchor(&dev->deferred))) {
1781
69ee472f
ON
1782 skb = (struct sk_buff *)res->context;
1783 retval = usb_submit_urb(res, GFP_ATOMIC);
1784 if (retval < 0) {
1785 dev_kfree_skb_any(skb);
638c5115 1786 kfree(res->sg);
69ee472f
ON
1787 usb_free_urb(res);
1788 usb_autopm_put_interface_async(dev->intf);
1789 } else {
1790 dev->net->trans_start = jiffies;
1791 __skb_queue_tail(&dev->txq, skb);
1792 }
1793 }
1da177e4 1794
69ee472f
ON
1795 smp_mb();
1796 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1797 spin_unlock_irq(&dev->txq.lock);
75bd0cbd
ML
1798
1799 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
65841fd5
ML
1800 /* handle remote wakeup ASAP */
1801 if (!dev->wait &&
1802 netif_device_present(dev->net) &&
1803 !timer_pending(&dev->delay) &&
1804 !test_bit(EVENT_RX_HALT, &dev->flags))
ab6f148d 1805 rx_alloc_submit(dev, GFP_NOIO);
65841fd5 1806
75bd0cbd 1807 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1aa9bc5b 1808 netif_tx_wake_all_queues(dev->net);
75bd0cbd
ML
1809 tasklet_schedule (&dev->bh);
1810 }
69ee472f 1811 }
5d9d01a3
ON
1812
1813 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1814 usb_autopm_get_interface_no_resume(intf);
1815
1da177e4
LT
1816 return 0;
1817}
38bde1d4 1818EXPORT_SYMBOL_GPL(usbnet_resume);
1da177e4 1819
5d9d01a3
ON
1820/*
1821 * Either a subdriver implements manage_power, then it is assumed to always
1822 * be ready to be suspended or it reports the readiness to be suspended
1823 * explicitly
1824 */
1825void usbnet_device_suggests_idle(struct usbnet *dev)
1826{
1827 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1828 dev->intf->needs_remote_wakeup = 1;
1829 usb_autopm_put_interface_async(dev->intf);
1830 }
1831}
1832EXPORT_SYMBOL(usbnet_device_suggests_idle);
1da177e4 1833
2dd7c8cf
ON
1834/*
1835 * For devices that can do without special commands
1836 */
1837int usbnet_manage_power(struct usbnet *dev, int on)
1838{
1839 dev->intf->needs_remote_wakeup = on;
1840 return 0;
1841}
1842EXPORT_SYMBOL(usbnet_manage_power);
1843
ac64995d
ML
1844void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1845{
1846 /* update link after link is reseted */
1847 if (link && !need_reset)
1848 netif_carrier_on(dev->net);
1849 else
1850 netif_carrier_off(dev->net);
1851
1852 if (need_reset && link)
1853 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
4b49f58f
ML
1854 else
1855 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
ac64995d
ML
1856}
1857EXPORT_SYMBOL(usbnet_link_change);
1858
877bd862 1859/*-------------------------------------------------------------------------*/
0547fad2
ML
1860static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1861 u16 value, u16 index, void *data, u16 size)
877bd862
ML
1862{
1863 void *buf = NULL;
1864 int err = -ENOMEM;
1865
1866 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1867 " value=0x%04x index=0x%04x size=%d\n",
1868 cmd, reqtype, value, index, size);
1869
1870 if (data) {
1871 buf = kmalloc(size, GFP_KERNEL);
1872 if (!buf)
1873 goto out;
1874 }
1875
1876 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1877 cmd, reqtype, value, index, buf, size,
1878 USB_CTRL_GET_TIMEOUT);
1879 if (err > 0 && err <= size)
1880 memcpy(data, buf, err);
1881 kfree(buf);
1882out:
1883 return err;
1884}
877bd862 1885
0547fad2
ML
1886static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1887 u16 value, u16 index, const void *data,
1888 u16 size)
877bd862
ML
1889{
1890 void *buf = NULL;
1891 int err = -ENOMEM;
1892
1893 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1894 " value=0x%04x index=0x%04x size=%d\n",
1895 cmd, reqtype, value, index, size);
1896
1897 if (data) {
1898 buf = kmemdup(data, size, GFP_KERNEL);
1899 if (!buf)
1900 goto out;
1901 }
1902
1903 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1904 cmd, reqtype, value, index, buf, size,
1905 USB_CTRL_SET_TIMEOUT);
1906 kfree(buf);
1907
1908out:
1909 return err;
1910}
0547fad2
ML
1911
1912/*
1913 * The function can't be called inside suspend/resume callback,
1914 * otherwise deadlock will be caused.
1915 */
1916int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1917 u16 value, u16 index, void *data, u16 size)
1918{
6f0a0986
ML
1919 int ret;
1920
1921 if (usb_autopm_get_interface(dev->intf) < 0)
1922 return -ENODEV;
1923 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1924 data, size);
1925 usb_autopm_put_interface(dev->intf);
1926 return ret;
0547fad2
ML
1927}
1928EXPORT_SYMBOL_GPL(usbnet_read_cmd);
1929
1930/*
1931 * The function can't be called inside suspend/resume callback,
1932 * otherwise deadlock will be caused.
1933 */
1934int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1935 u16 value, u16 index, const void *data, u16 size)
1936{
6f0a0986
ML
1937 int ret;
1938
1939 if (usb_autopm_get_interface(dev->intf) < 0)
1940 return -ENODEV;
1941 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1942 data, size);
1943 usb_autopm_put_interface(dev->intf);
1944 return ret;
0547fad2 1945}
877bd862
ML
1946EXPORT_SYMBOL_GPL(usbnet_write_cmd);
1947
0547fad2
ML
1948/*
1949 * The function can be called inside suspend/resume callback safely
1950 * and should only be called by suspend/resume callback generally.
1951 */
1952int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1953 u16 value, u16 index, void *data, u16 size)
1954{
1955 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1956 data, size);
1957}
1958EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
1959
1960/*
1961 * The function can be called inside suspend/resume callback safely
1962 * and should only be called by suspend/resume callback generally.
1963 */
1964int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1965 u16 value, u16 index, const void *data,
1966 u16 size)
1967{
1968 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1969 data, size);
1970}
1971EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
1972
877bd862
ML
1973static void usbnet_async_cmd_cb(struct urb *urb)
1974{
1975 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
1976 int status = urb->status;
1977
1978 if (status < 0)
1979 dev_dbg(&urb->dev->dev, "%s failed with %d",
1980 __func__, status);
1981
1982 kfree(req);
1983 usb_free_urb(urb);
1984}
1985
0547fad2
ML
1986/*
1987 * The caller must make sure that device can't be put into suspend
1988 * state until the control URB completes.
1989 */
877bd862
ML
1990int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
1991 u16 value, u16 index, const void *data, u16 size)
1992{
1993 struct usb_ctrlrequest *req = NULL;
1994 struct urb *urb;
1995 int err = -ENOMEM;
1996 void *buf = NULL;
1997
1998 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1999 " value=0x%04x index=0x%04x size=%d\n",
2000 cmd, reqtype, value, index, size);
2001
2002 urb = usb_alloc_urb(0, GFP_ATOMIC);
2003 if (!urb) {
2004 netdev_err(dev->net, "Error allocating URB in"
2005 " %s!\n", __func__);
2006 goto fail;
2007 }
2008
2009 if (data) {
2010 buf = kmemdup(data, size, GFP_ATOMIC);
2011 if (!buf) {
2012 netdev_err(dev->net, "Error allocating buffer"
2013 " in %s!\n", __func__);
2014 goto fail_free;
2015 }
2016 }
2017
2018 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
38673c82 2019 if (!req)
877bd862 2020 goto fail_free_buf;
877bd862
ML
2021
2022 req->bRequestType = reqtype;
2023 req->bRequest = cmd;
2024 req->wValue = cpu_to_le16(value);
2025 req->wIndex = cpu_to_le16(index);
2026 req->wLength = cpu_to_le16(size);
2027
2028 usb_fill_control_urb(urb, dev->udev,
2029 usb_sndctrlpipe(dev->udev, 0),
2030 (void *)req, buf, size,
2031 usbnet_async_cmd_cb, req);
2032 urb->transfer_flags |= URB_FREE_BUFFER;
2033
2034 err = usb_submit_urb(urb, GFP_ATOMIC);
2035 if (err < 0) {
2036 netdev_err(dev->net, "Error submitting the control"
2037 " message: status=%d\n", err);
2038 goto fail_free;
2039 }
2040 return 0;
2041
2042fail_free_buf:
2043 kfree(buf);
2044fail_free:
2045 kfree(req);
2046 usb_free_urb(urb);
2047fail:
2048 return err;
2049
2050}
2051EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
1da177e4
LT
2052/*-------------------------------------------------------------------------*/
2053
f29fc259 2054static int __init usbnet_init(void)
1da177e4 2055{
c582a950
TF
2056 /* Compiler should optimize this out. */
2057 BUILD_BUG_ON(
2058 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
1da177e4 2059
c7e12ead 2060 eth_random_addr(node_id);
cb1cebbe 2061 return 0;
1da177e4 2062}
f29fc259 2063module_init(usbnet_init);
1da177e4 2064
f29fc259 2065static void __exit usbnet_exit(void)
1da177e4 2066{
1da177e4 2067}
f29fc259 2068module_exit(usbnet_exit);
1da177e4 2069
f29fc259 2070MODULE_AUTHOR("David Brownell");
090ffa9d 2071MODULE_DESCRIPTION("USB network driver framework");
f29fc259 2072MODULE_LICENSE("GPL");
This page took 1.392361 seconds and 5 git commands to generate.