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