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