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