1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/ctype.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#include <linux/slab.h>
46#include <linux/kernel.h>
47#include <linux/pm_runtime.h>
48
49#define DRIVER_VERSION "22-Aug-2005"
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65#define MAX_QUEUE_MEMORY (60 * 1518)
66#define RX_QLEN(dev) ((dev)->rx_qlen)
67#define TX_QLEN(dev) ((dev)->tx_qlen)
68
69
70#define TX_TIMEOUT_JIFFIES (5*HZ)
71
72
73
74
75#define THROTTLE_JIFFIES (HZ/8)
76
77
78#define UNLINK_TIMEOUT_MS 3
79
80
81
82
83static u8 node_id [ETH_ALEN];
84
85
86static int msg_level = -1;
87module_param (msg_level, int, 0);
88MODULE_PARM_DESC (msg_level, "Override default message level");
89
90
91
92
93int 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
107
108
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
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}
157EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
158
159int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
160{
161 int tmp = -1, ret;
162 unsigned char buf [13];
163
164 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
165 if (ret == 12)
166 tmp = hex2bin(dev->net->dev_addr, buf, 6);
167 if (tmp < 0) {
168 dev_dbg(&dev->udev->dev,
169 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
170 if (ret >= 0)
171 ret = -EINVAL;
172 return ret;
173 }
174 return 0;
175}
176EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
177
178static void intr_complete (struct urb *urb)
179{
180 struct usbnet *dev = urb->context;
181 int status = urb->status;
182
183 switch (status) {
184
185 case 0:
186 dev->driver_info->status(dev, urb);
187 break;
188
189
190 case -ENOENT:
191 case -ESHUTDOWN:
192 netif_dbg(dev, ifdown, dev->net,
193 "intr shutdown, code %d\n", status);
194 return;
195
196
197
198
199 default:
200 netdev_dbg(dev->net, "intr status %d\n", status);
201 break;
202 }
203
204 status = usb_submit_urb (urb, GFP_ATOMIC);
205 if (status != 0)
206 netif_err(dev, timer, dev->net,
207 "intr resubmit --> %d\n", status);
208}
209
210static int init_status (struct usbnet *dev, struct usb_interface *intf)
211{
212 char *buf = NULL;
213 unsigned pipe = 0;
214 unsigned maxp;
215 unsigned period;
216
217 if (!dev->driver_info->status)
218 return 0;
219
220 pipe = usb_rcvintpipe (dev->udev,
221 dev->status->desc.bEndpointAddress
222 & USB_ENDPOINT_NUMBER_MASK);
223 maxp = usb_maxpacket (dev->udev, pipe, 0);
224
225
226 period = max ((int) dev->status->desc.bInterval,
227 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
228
229 buf = kmalloc (maxp, GFP_KERNEL);
230 if (buf) {
231 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
232 if (!dev->interrupt) {
233 kfree (buf);
234 return -ENOMEM;
235 } else {
236 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
237 buf, maxp, intr_complete, dev, period);
238 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
239 dev_dbg(&intf->dev,
240 "status ep%din, %d bytes period %d\n",
241 usb_pipeendpoint(pipe), maxp, period);
242 }
243 }
244 return 0;
245}
246
247
248int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
249{
250 int ret = 0;
251
252 WARN_ON_ONCE(dev->interrupt == NULL);
253 if (dev->interrupt) {
254 mutex_lock(&dev->interrupt_mutex);
255
256 if (++dev->interrupt_count == 1)
257 ret = usb_submit_urb(dev->interrupt, mem_flags);
258
259 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
260 dev->interrupt_count);
261 mutex_unlock(&dev->interrupt_mutex);
262 }
263 return ret;
264}
265EXPORT_SYMBOL_GPL(usbnet_status_start);
266
267
268static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
269{
270 int ret = 0;
271
272 mutex_lock(&dev->interrupt_mutex);
273 if (dev->interrupt_count) {
274 ret = usb_submit_urb(dev->interrupt, mem_flags);
275 dev_dbg(&dev->udev->dev,
276 "submitted interrupt URB for resume\n");
277 }
278 mutex_unlock(&dev->interrupt_mutex);
279 return ret;
280}
281
282
283void usbnet_status_stop(struct usbnet *dev)
284{
285 if (dev->interrupt) {
286 mutex_lock(&dev->interrupt_mutex);
287 WARN_ON(dev->interrupt_count == 0);
288
289 if (dev->interrupt_count && --dev->interrupt_count == 0)
290 usb_kill_urb(dev->interrupt);
291
292 dev_dbg(&dev->udev->dev,
293 "decremented interrupt URB count to %d\n",
294 dev->interrupt_count);
295 mutex_unlock(&dev->interrupt_mutex);
296 }
297}
298EXPORT_SYMBOL_GPL(usbnet_status_stop);
299
300
301static void __usbnet_status_stop_force(struct usbnet *dev)
302{
303 if (dev->interrupt) {
304 mutex_lock(&dev->interrupt_mutex);
305 usb_kill_urb(dev->interrupt);
306 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
307 mutex_unlock(&dev->interrupt_mutex);
308 }
309}
310
311
312
313
314
315void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
316{
317 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
318 unsigned long flags;
319 int status;
320
321 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
322 skb_queue_tail(&dev->rxq_pause, skb);
323 return;
324 }
325
326
327 if (skb->protocol == 0)
328 skb->protocol = eth_type_trans (skb, dev->net);
329
330 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
331 stats64->rx_packets++;
332 stats64->rx_bytes += skb->len;
333 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
334
335 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
336 skb->len + sizeof (struct ethhdr), skb->protocol);
337 memset (skb->cb, 0, sizeof (struct skb_data));
338
339 if (skb_defer_rx_timestamp(skb))
340 return;
341
342 status = netif_rx (skb);
343 if (status != NET_RX_SUCCESS)
344 netif_dbg(dev, rx_err, dev->net,
345 "netif_rx status %d\n", status);
346}
347EXPORT_SYMBOL_GPL(usbnet_skb_return);
348
349
350void usbnet_update_max_qlen(struct usbnet *dev)
351{
352 enum usb_device_speed speed = dev->udev->speed;
353
354 switch (speed) {
355 case USB_SPEED_HIGH:
356 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
357 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
358 break;
359 case USB_SPEED_SUPER:
360 case USB_SPEED_SUPER_PLUS:
361
362
363
364
365
366 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
367 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
368 break;
369 default:
370 dev->rx_qlen = dev->tx_qlen = 4;
371 }
372}
373EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
374
375
376
377
378
379
380
381
382int usbnet_change_mtu (struct net_device *net, int new_mtu)
383{
384 struct usbnet *dev = netdev_priv(net);
385 int ll_mtu = new_mtu + net->hard_header_len;
386 int old_hard_mtu = dev->hard_mtu;
387 int old_rx_urb_size = dev->rx_urb_size;
388
389
390 if ((ll_mtu % dev->maxpacket) == 0)
391 return -EDOM;
392 net->mtu = new_mtu;
393
394 dev->hard_mtu = net->mtu + net->hard_header_len;
395 if (dev->rx_urb_size == old_hard_mtu) {
396 dev->rx_urb_size = dev->hard_mtu;
397 if (dev->rx_urb_size > old_rx_urb_size) {
398 usbnet_pause_rx(dev);
399 usbnet_unlink_rx_urbs(dev);
400 usbnet_resume_rx(dev);
401 }
402 }
403
404
405 usbnet_update_max_qlen(dev);
406
407 return 0;
408}
409EXPORT_SYMBOL_GPL(usbnet_change_mtu);
410
411
412static void __usbnet_queue_skb(struct sk_buff_head *list,
413 struct sk_buff *newsk, enum skb_state state)
414{
415 struct skb_data *entry = (struct skb_data *) newsk->cb;
416
417 __skb_queue_tail(list, newsk);
418 entry->state = state;
419}
420
421
422
423
424
425
426
427static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
428 struct sk_buff_head *list, enum skb_state state)
429{
430 unsigned long flags;
431 enum skb_state old_state;
432 struct skb_data *entry = (struct skb_data *) skb->cb;
433
434 spin_lock_irqsave(&list->lock, flags);
435 old_state = entry->state;
436 entry->state = state;
437 __skb_unlink(skb, list);
438
439
440
441
442
443 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
444
445 __skb_queue_tail(&dev->done, skb);
446 if (dev->done.qlen == 1)
447 tasklet_schedule(&dev->bh);
448 spin_unlock(&dev->done.lock);
449 spin_unlock_irqrestore(&list->lock, flags);
450 return old_state;
451}
452
453
454
455
456
457
458void usbnet_defer_kevent (struct usbnet *dev, int work)
459{
460 set_bit (work, &dev->flags);
461 if (!schedule_work (&dev->kevent))
462 netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
463 else
464 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
465}
466EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
467
468
469
470static void rx_complete (struct urb *urb);
471
472static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
473{
474 struct sk_buff *skb;
475 struct skb_data *entry;
476 int retval = 0;
477 unsigned long lockflags;
478 size_t size = dev->rx_urb_size;
479
480
481 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
482 usb_free_urb(urb);
483 return -ENOLINK;
484 }
485
486 if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
487 skb = __netdev_alloc_skb(dev->net, size, flags);
488 else
489 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
490 if (!skb) {
491 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
492 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
493 usb_free_urb (urb);
494 return -ENOMEM;
495 }
496
497 entry = (struct skb_data *) skb->cb;
498 entry->urb = urb;
499 entry->dev = dev;
500 entry->length = 0;
501
502 usb_fill_bulk_urb (urb, dev->udev, dev->in,
503 skb->data, size, rx_complete, skb);
504
505 spin_lock_irqsave (&dev->rxq.lock, lockflags);
506
507 if (netif_running (dev->net) &&
508 netif_device_present (dev->net) &&
509 test_bit(EVENT_DEV_OPEN, &dev->flags) &&
510 !test_bit (EVENT_RX_HALT, &dev->flags) &&
511 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
512 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
513 case -EPIPE:
514 usbnet_defer_kevent (dev, EVENT_RX_HALT);
515 break;
516 case -ENOMEM:
517 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
518 break;
519 case -ENODEV:
520 netif_dbg(dev, ifdown, dev->net, "device gone\n");
521 netif_device_detach (dev->net);
522 break;
523 case -EHOSTUNREACH:
524 retval = -ENOLINK;
525 break;
526 default:
527 netif_dbg(dev, rx_err, dev->net,
528 "rx submit, %d\n", retval);
529 tasklet_schedule (&dev->bh);
530 break;
531 case 0:
532 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
533 }
534 } else {
535 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
536 retval = -ENOLINK;
537 }
538 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
539 if (retval) {
540 dev_kfree_skb_any (skb);
541 usb_free_urb (urb);
542 }
543 return retval;
544}
545
546
547
548
549static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
550{
551 if (dev->driver_info->rx_fixup &&
552 !dev->driver_info->rx_fixup (dev, skb)) {
553
554 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
555 dev->net->stats.rx_errors++;
556 goto done;
557 }
558
559
560
561 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
562 goto done;
563
564 if (skb->len < ETH_HLEN) {
565 dev->net->stats.rx_errors++;
566 dev->net->stats.rx_length_errors++;
567 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
568 } else {
569 usbnet_skb_return(dev, skb);
570 return;
571 }
572
573done:
574 skb_queue_tail(&dev->done, skb);
575}
576
577
578
579static void rx_complete (struct urb *urb)
580{
581 struct sk_buff *skb = (struct sk_buff *) urb->context;
582 struct skb_data *entry = (struct skb_data *) skb->cb;
583 struct usbnet *dev = entry->dev;
584 int urb_status = urb->status;
585 enum skb_state state;
586
587 skb_put (skb, urb->actual_length);
588 state = rx_done;
589 entry->urb = NULL;
590
591 switch (urb_status) {
592
593 case 0:
594 break;
595
596
597
598
599
600
601 case -EPIPE:
602 dev->net->stats.rx_errors++;
603 usbnet_defer_kevent (dev, EVENT_RX_HALT);
604
605
606
607 case -ECONNRESET:
608 case -ESHUTDOWN:
609 netif_dbg(dev, ifdown, dev->net,
610 "rx shutdown, code %d\n", urb_status);
611 goto block;
612
613
614
615
616
617 case -EPROTO:
618 case -ETIME:
619 case -EILSEQ:
620 dev->net->stats.rx_errors++;
621 if (!timer_pending (&dev->delay)) {
622 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
623 netif_dbg(dev, link, dev->net,
624 "rx throttle %d\n", urb_status);
625 }
626block:
627 state = rx_cleanup;
628 entry->urb = urb;
629 urb = NULL;
630 break;
631
632
633 case -EOVERFLOW:
634 dev->net->stats.rx_over_errors++;
635
636
637 default:
638 state = rx_cleanup;
639 dev->net->stats.rx_errors++;
640 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
641 break;
642 }
643
644
645 if (++dev->pkt_cnt > 30) {
646 dev->pkt_cnt = 0;
647 dev->pkt_err = 0;
648 } else {
649 if (state == rx_cleanup)
650 dev->pkt_err++;
651 if (dev->pkt_err > 20)
652 set_bit(EVENT_RX_KILL, &dev->flags);
653 }
654
655 state = defer_bh(dev, skb, &dev->rxq, state);
656
657 if (urb) {
658 if (netif_running (dev->net) &&
659 !test_bit (EVENT_RX_HALT, &dev->flags) &&
660 state != unlink_start) {
661 rx_submit (dev, urb, GFP_ATOMIC);
662 usb_mark_last_busy(dev->udev);
663 return;
664 }
665 usb_free_urb (urb);
666 }
667 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
668}
669
670
671void usbnet_pause_rx(struct usbnet *dev)
672{
673 set_bit(EVENT_RX_PAUSED, &dev->flags);
674
675 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
676}
677EXPORT_SYMBOL_GPL(usbnet_pause_rx);
678
679void usbnet_resume_rx(struct usbnet *dev)
680{
681 struct sk_buff *skb;
682 int num = 0;
683
684 clear_bit(EVENT_RX_PAUSED, &dev->flags);
685
686 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
687 usbnet_skb_return(dev, skb);
688 num++;
689 }
690
691 tasklet_schedule(&dev->bh);
692
693 netif_dbg(dev, rx_status, dev->net,
694 "paused rx queue disabled, %d skbs requeued\n", num);
695}
696EXPORT_SYMBOL_GPL(usbnet_resume_rx);
697
698void usbnet_purge_paused_rxq(struct usbnet *dev)
699{
700 skb_queue_purge(&dev->rxq_pause);
701}
702EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
703
704
705
706
707
708static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
709{
710 unsigned long flags;
711 struct sk_buff *skb;
712 int count = 0;
713
714 spin_lock_irqsave (&q->lock, flags);
715 while (!skb_queue_empty(q)) {
716 struct skb_data *entry;
717 struct urb *urb;
718 int retval;
719
720 skb_queue_walk(q, skb) {
721 entry = (struct skb_data *) skb->cb;
722 if (entry->state != unlink_start)
723 goto found;
724 }
725 break;
726found:
727 entry->state = unlink_start;
728 urb = entry->urb;
729
730
731
732
733
734
735
736
737 usb_get_urb(urb);
738 spin_unlock_irqrestore(&q->lock, flags);
739
740
741 retval = usb_unlink_urb (urb);
742 if (retval != -EINPROGRESS && retval != 0)
743 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
744 else
745 count++;
746 usb_put_urb(urb);
747 spin_lock_irqsave(&q->lock, flags);
748 }
749 spin_unlock_irqrestore (&q->lock, flags);
750 return count;
751}
752
753
754
755
756void usbnet_unlink_rx_urbs(struct usbnet *dev)
757{
758 if (netif_running(dev->net)) {
759 (void) unlink_urbs (dev, &dev->rxq);
760 tasklet_schedule(&dev->bh);
761 }
762}
763EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
764
765
766
767static void wait_skb_queue_empty(struct sk_buff_head *q)
768{
769 unsigned long flags;
770
771 spin_lock_irqsave(&q->lock, flags);
772 while (!skb_queue_empty(q)) {
773 spin_unlock_irqrestore(&q->lock, flags);
774 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
775 set_current_state(TASK_UNINTERRUPTIBLE);
776 spin_lock_irqsave(&q->lock, flags);
777 }
778 spin_unlock_irqrestore(&q->lock, flags);
779}
780
781
782static void usbnet_terminate_urbs(struct usbnet *dev)
783{
784 DECLARE_WAITQUEUE(wait, current);
785 int temp;
786
787
788 add_wait_queue(&dev->wait, &wait);
789 set_current_state(TASK_UNINTERRUPTIBLE);
790 temp = unlink_urbs(dev, &dev->txq) +
791 unlink_urbs(dev, &dev->rxq);
792
793
794 wait_skb_queue_empty(&dev->rxq);
795 wait_skb_queue_empty(&dev->txq);
796 wait_skb_queue_empty(&dev->done);
797 netif_dbg(dev, ifdown, dev->net,
798 "waited for %d urb completions\n", temp);
799 set_current_state(TASK_RUNNING);
800 remove_wait_queue(&dev->wait, &wait);
801}
802
803int usbnet_stop (struct net_device *net)
804{
805 struct usbnet *dev = netdev_priv(net);
806 const struct driver_info *info = dev->driver_info;
807 int retval, pm, mpn;
808
809 clear_bit(EVENT_DEV_OPEN, &dev->flags);
810 netif_stop_queue (net);
811
812 netif_info(dev, ifdown, dev->net,
813 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
814 net->stats.rx_packets, net->stats.tx_packets,
815 net->stats.rx_errors, net->stats.tx_errors);
816
817
818 pm = usb_autopm_get_interface(dev->intf);
819
820
821 if (info->stop) {
822 retval = info->stop(dev);
823 if (retval < 0)
824 netif_info(dev, ifdown, dev->net,
825 "stop fail (%d) usbnet usb-%s-%s, %s\n",
826 retval,
827 dev->udev->bus->bus_name, dev->udev->devpath,
828 info->description);
829 }
830
831 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
832 usbnet_terminate_urbs(dev);
833
834 usbnet_status_stop(dev);
835
836 usbnet_purge_paused_rxq(dev);
837
838 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
839
840
841
842
843
844 dev->flags = 0;
845 del_timer_sync (&dev->delay);
846 tasklet_kill (&dev->bh);
847 if (!pm)
848 usb_autopm_put_interface(dev->intf);
849
850 if (info->manage_power && mpn)
851 info->manage_power(dev, 0);
852 else
853 usb_autopm_put_interface(dev->intf);
854
855 return 0;
856}
857EXPORT_SYMBOL_GPL(usbnet_stop);
858
859
860
861
862
863
864
865int usbnet_open (struct net_device *net)
866{
867 struct usbnet *dev = netdev_priv(net);
868 int retval;
869 const struct driver_info *info = dev->driver_info;
870
871 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
872 netif_info(dev, ifup, dev->net,
873 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
874 retval,
875 dev->udev->bus->bus_name,
876 dev->udev->devpath,
877 info->description);
878 goto done_nopm;
879 }
880
881
882 if (info->reset && (retval = info->reset (dev)) < 0) {
883 netif_info(dev, ifup, dev->net,
884 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
885 retval,
886 dev->udev->bus->bus_name,
887 dev->udev->devpath,
888 info->description);
889 goto done;
890 }
891
892
893 usbnet_update_max_qlen(dev);
894
895
896 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
897 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
898 goto done;
899 }
900
901
902 if (dev->interrupt) {
903 retval = usbnet_status_start(dev, GFP_KERNEL);
904 if (retval < 0) {
905 netif_err(dev, ifup, dev->net,
906 "intr submit %d\n", retval);
907 goto done;
908 }
909 }
910
911 set_bit(EVENT_DEV_OPEN, &dev->flags);
912 netif_start_queue (net);
913 netif_info(dev, ifup, dev->net,
914 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
915 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
916 dev->net->mtu,
917 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
918 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
919 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
920 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
921 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
922 "simple");
923
924
925 dev->pkt_cnt = 0;
926 dev->pkt_err = 0;
927 clear_bit(EVENT_RX_KILL, &dev->flags);
928
929
930 tasklet_schedule (&dev->bh);
931 if (info->manage_power) {
932 retval = info->manage_power(dev, 1);
933 if (retval < 0) {
934 retval = 0;
935 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
936 } else {
937 usb_autopm_put_interface(dev->intf);
938 }
939 }
940 return retval;
941done:
942 usb_autopm_put_interface(dev->intf);
943done_nopm:
944 return retval;
945}
946EXPORT_SYMBOL_GPL(usbnet_open);
947
948
949
950
951
952
953
954int usbnet_get_link_ksettings(struct net_device *net,
955 struct ethtool_link_ksettings *cmd)
956{
957 struct usbnet *dev = netdev_priv(net);
958
959 if (!dev->mii.mdio_read)
960 return -EOPNOTSUPP;
961
962 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
963
964 return 0;
965}
966EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
967
968int usbnet_set_link_ksettings(struct net_device *net,
969 const struct ethtool_link_ksettings *cmd)
970{
971 struct usbnet *dev = netdev_priv(net);
972 int retval;
973
974 if (!dev->mii.mdio_write)
975 return -EOPNOTSUPP;
976
977 retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
978
979
980 if (dev->driver_info->link_reset)
981 dev->driver_info->link_reset(dev);
982
983
984 usbnet_update_max_qlen(dev);
985
986 return retval;
987}
988EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
989
990void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
991{
992 struct usbnet *dev = netdev_priv(net);
993 unsigned int start;
994 int cpu;
995
996 netdev_stats_to_stats64(stats, &net->stats);
997
998 for_each_possible_cpu(cpu) {
999 struct pcpu_sw_netstats *stats64;
1000 u64 rx_packets, rx_bytes;
1001 u64 tx_packets, tx_bytes;
1002
1003 stats64 = per_cpu_ptr(dev->stats64, cpu);
1004
1005 do {
1006 start = u64_stats_fetch_begin_irq(&stats64->syncp);
1007 rx_packets = stats64->rx_packets;
1008 rx_bytes = stats64->rx_bytes;
1009 tx_packets = stats64->tx_packets;
1010 tx_bytes = stats64->tx_bytes;
1011 } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
1012
1013 stats->rx_packets += rx_packets;
1014 stats->rx_bytes += rx_bytes;
1015 stats->tx_packets += tx_packets;
1016 stats->tx_bytes += tx_bytes;
1017 }
1018}
1019EXPORT_SYMBOL_GPL(usbnet_get_stats64);
1020
1021u32 usbnet_get_link (struct net_device *net)
1022{
1023 struct usbnet *dev = netdev_priv(net);
1024
1025
1026 if (dev->driver_info->check_connect)
1027 return dev->driver_info->check_connect (dev) == 0;
1028
1029
1030 if (dev->mii.mdio_read)
1031 return mii_link_ok(&dev->mii);
1032
1033
1034 return ethtool_op_get_link(net);
1035}
1036EXPORT_SYMBOL_GPL(usbnet_get_link);
1037
1038int usbnet_nway_reset(struct net_device *net)
1039{
1040 struct usbnet *dev = netdev_priv(net);
1041
1042 if (!dev->mii.mdio_write)
1043 return -EOPNOTSUPP;
1044
1045 return mii_nway_restart(&dev->mii);
1046}
1047EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1048
1049void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1050{
1051 struct usbnet *dev = netdev_priv(net);
1052
1053 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1054 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
1055 strlcpy (info->fw_version, dev->driver_info->description,
1056 sizeof info->fw_version);
1057 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1058}
1059EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1060
1061u32 usbnet_get_msglevel (struct net_device *net)
1062{
1063 struct usbnet *dev = netdev_priv(net);
1064
1065 return dev->msg_enable;
1066}
1067EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1068
1069void usbnet_set_msglevel (struct net_device *net, u32 level)
1070{
1071 struct usbnet *dev = netdev_priv(net);
1072
1073 dev->msg_enable = level;
1074}
1075EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1076
1077
1078static const struct ethtool_ops usbnet_ethtool_ops = {
1079 .get_link = usbnet_get_link,
1080 .nway_reset = usbnet_nway_reset,
1081 .get_drvinfo = usbnet_get_drvinfo,
1082 .get_msglevel = usbnet_get_msglevel,
1083 .set_msglevel = usbnet_set_msglevel,
1084 .get_ts_info = ethtool_op_get_ts_info,
1085 .get_link_ksettings = usbnet_get_link_ksettings,
1086 .set_link_ksettings = usbnet_set_link_ksettings,
1087};
1088
1089
1090
1091static void __handle_link_change(struct usbnet *dev)
1092{
1093 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1094 return;
1095
1096 if (!netif_carrier_ok(dev->net)) {
1097
1098 unlink_urbs(dev, &dev->rxq);
1099
1100
1101
1102
1103
1104 } else {
1105
1106 tasklet_schedule(&dev->bh);
1107 }
1108
1109
1110 usbnet_update_max_qlen(dev);
1111
1112 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1113}
1114
1115static void usbnet_set_rx_mode(struct net_device *net)
1116{
1117 struct usbnet *dev = netdev_priv(net);
1118
1119 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1120}
1121
1122static void __handle_set_rx_mode(struct usbnet *dev)
1123{
1124 if (dev->driver_info->set_rx_mode)
1125 (dev->driver_info->set_rx_mode)(dev);
1126
1127 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1128}
1129
1130
1131
1132
1133
1134
1135static void
1136usbnet_deferred_kevent (struct work_struct *work)
1137{
1138 struct usbnet *dev =
1139 container_of(work, struct usbnet, kevent);
1140 int status;
1141
1142
1143 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1144 unlink_urbs (dev, &dev->txq);
1145 status = usb_autopm_get_interface(dev->intf);
1146 if (status < 0)
1147 goto fail_pipe;
1148 status = usb_clear_halt (dev->udev, dev->out);
1149 usb_autopm_put_interface(dev->intf);
1150 if (status < 0 &&
1151 status != -EPIPE &&
1152 status != -ESHUTDOWN) {
1153 if (netif_msg_tx_err (dev))
1154fail_pipe:
1155 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1156 status);
1157 } else {
1158 clear_bit (EVENT_TX_HALT, &dev->flags);
1159 if (status != -ESHUTDOWN)
1160 netif_wake_queue (dev->net);
1161 }
1162 }
1163 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1164 unlink_urbs (dev, &dev->rxq);
1165 status = usb_autopm_get_interface(dev->intf);
1166 if (status < 0)
1167 goto fail_halt;
1168 status = usb_clear_halt (dev->udev, dev->in);
1169 usb_autopm_put_interface(dev->intf);
1170 if (status < 0 &&
1171 status != -EPIPE &&
1172 status != -ESHUTDOWN) {
1173 if (netif_msg_rx_err (dev))
1174fail_halt:
1175 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1176 status);
1177 } else {
1178 clear_bit (EVENT_RX_HALT, &dev->flags);
1179 tasklet_schedule (&dev->bh);
1180 }
1181 }
1182
1183
1184 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1185 struct urb *urb = NULL;
1186 int resched = 1;
1187
1188 if (netif_running (dev->net))
1189 urb = usb_alloc_urb (0, GFP_KERNEL);
1190 else
1191 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1192 if (urb != NULL) {
1193 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1194 status = usb_autopm_get_interface(dev->intf);
1195 if (status < 0) {
1196 usb_free_urb(urb);
1197 goto fail_lowmem;
1198 }
1199 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1200 resched = 0;
1201 usb_autopm_put_interface(dev->intf);
1202fail_lowmem:
1203 if (resched)
1204 tasklet_schedule (&dev->bh);
1205 }
1206 }
1207
1208 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1209 const struct driver_info *info = dev->driver_info;
1210 int retval = 0;
1211
1212 clear_bit (EVENT_LINK_RESET, &dev->flags);
1213 status = usb_autopm_get_interface(dev->intf);
1214 if (status < 0)
1215 goto skip_reset;
1216 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1217 usb_autopm_put_interface(dev->intf);
1218skip_reset:
1219 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1220 retval,
1221 dev->udev->bus->bus_name,
1222 dev->udev->devpath,
1223 info->description);
1224 } else {
1225 usb_autopm_put_interface(dev->intf);
1226 }
1227
1228
1229 __handle_link_change(dev);
1230 }
1231
1232 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1233 __handle_link_change(dev);
1234
1235 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1236 __handle_set_rx_mode(dev);
1237
1238
1239 if (dev->flags)
1240 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1241}
1242
1243
1244
1245static void tx_complete (struct urb *urb)
1246{
1247 struct sk_buff *skb = (struct sk_buff *) urb->context;
1248 struct skb_data *entry = (struct skb_data *) skb->cb;
1249 struct usbnet *dev = entry->dev;
1250
1251 if (urb->status == 0) {
1252 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
1253 unsigned long flags;
1254
1255 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
1256 stats64->tx_packets += entry->packets;
1257 stats64->tx_bytes += entry->length;
1258 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
1259 } else {
1260 dev->net->stats.tx_errors++;
1261
1262 switch (urb->status) {
1263 case -EPIPE:
1264 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1265 break;
1266
1267
1268 case -ECONNRESET:
1269 case -ESHUTDOWN:
1270 break;
1271
1272
1273
1274
1275 case -EPROTO:
1276 case -ETIME:
1277 case -EILSEQ:
1278 usb_mark_last_busy(dev->udev);
1279 if (!timer_pending (&dev->delay)) {
1280 mod_timer (&dev->delay,
1281 jiffies + THROTTLE_JIFFIES);
1282 netif_dbg(dev, link, dev->net,
1283 "tx throttle %d\n", urb->status);
1284 }
1285 netif_stop_queue (dev->net);
1286 break;
1287 default:
1288 netif_dbg(dev, tx_err, dev->net,
1289 "tx err %d\n", entry->urb->status);
1290 break;
1291 }
1292 }
1293
1294 usb_autopm_put_interface_async(dev->intf);
1295 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1296}
1297
1298
1299
1300void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue)
1301{
1302 struct usbnet *dev = netdev_priv(net);
1303
1304 unlink_urbs (dev, &dev->txq);
1305 tasklet_schedule (&dev->bh);
1306
1307
1308
1309
1310 if (dev->driver_info->recover)
1311 (dev->driver_info->recover)(dev);
1312}
1313EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1314
1315
1316
1317static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1318{
1319 unsigned num_sgs, total_len = 0;
1320 int i, s = 0;
1321
1322 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1323 if (num_sgs == 1)
1324 return 0;
1325
1326
1327 urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
1328 GFP_ATOMIC);
1329 if (!urb->sg)
1330 return -ENOMEM;
1331
1332 urb->num_sgs = num_sgs;
1333 sg_init_table(urb->sg, urb->num_sgs + 1);
1334
1335 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1336 total_len += skb_headlen(skb);
1337
1338 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1339 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1340
1341 total_len += skb_frag_size(f);
1342 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1343 f->page_offset);
1344 }
1345 urb->transfer_buffer_length = total_len;
1346
1347 return 1;
1348}
1349
1350netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1351 struct net_device *net)
1352{
1353 struct usbnet *dev = netdev_priv(net);
1354 unsigned int length;
1355 struct urb *urb = NULL;
1356 struct skb_data *entry;
1357 const struct driver_info *info = dev->driver_info;
1358 unsigned long flags;
1359 int retval;
1360
1361 if (skb)
1362 skb_tx_timestamp(skb);
1363
1364
1365
1366 if (info->tx_fixup) {
1367 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1368 if (!skb) {
1369
1370 if (info->flags & FLAG_MULTI_PACKET)
1371 goto not_drop;
1372 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1373 goto drop;
1374 }
1375 }
1376
1377 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1378 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1379 goto drop;
1380 }
1381
1382 entry = (struct skb_data *) skb->cb;
1383 entry->urb = urb;
1384 entry->dev = dev;
1385
1386 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1387 skb->data, skb->len, tx_complete, skb);
1388 if (dev->can_dma_sg) {
1389 if (build_dma_sg(skb, urb) < 0)
1390 goto drop;
1391 }
1392 length = urb->transfer_buffer_length;
1393
1394
1395
1396
1397
1398
1399
1400
1401 if (length % dev->maxpacket == 0) {
1402 if (!(info->flags & FLAG_SEND_ZLP)) {
1403 if (!(info->flags & FLAG_MULTI_PACKET)) {
1404 length++;
1405 if (skb_tailroom(skb) && !urb->num_sgs) {
1406 skb->data[skb->len] = 0;
1407 __skb_put(skb, 1);
1408 } else if (urb->num_sgs)
1409 sg_set_buf(&urb->sg[urb->num_sgs++],
1410 dev->padding_pkt, 1);
1411 }
1412 } else
1413 urb->transfer_flags |= URB_ZERO_PACKET;
1414 }
1415 urb->transfer_buffer_length = length;
1416
1417 if (info->flags & FLAG_MULTI_PACKET) {
1418
1419
1420
1421
1422 entry->length += length;
1423 if (WARN_ON_ONCE(entry->length <= 0))
1424 entry->length = length;
1425 } else {
1426 usbnet_set_skb_tx_stats(skb, 1, length);
1427 }
1428
1429 spin_lock_irqsave(&dev->txq.lock, flags);
1430 retval = usb_autopm_get_interface_async(dev->intf);
1431 if (retval < 0) {
1432 spin_unlock_irqrestore(&dev->txq.lock, flags);
1433 goto drop;
1434 }
1435 if (netif_queue_stopped(net)) {
1436 usb_autopm_put_interface_async(dev->intf);
1437 spin_unlock_irqrestore(&dev->txq.lock, flags);
1438 goto drop;
1439 }
1440
1441#ifdef CONFIG_PM
1442
1443 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1444
1445 usb_anchor_urb(urb, &dev->deferred);
1446
1447 netif_stop_queue(net);
1448 usb_put_urb(urb);
1449 spin_unlock_irqrestore(&dev->txq.lock, flags);
1450 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1451 goto deferred;
1452 }
1453#endif
1454
1455 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1456 case -EPIPE:
1457 netif_stop_queue (net);
1458 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1459 usb_autopm_put_interface_async(dev->intf);
1460 break;
1461 default:
1462 usb_autopm_put_interface_async(dev->intf);
1463 netif_dbg(dev, tx_err, dev->net,
1464 "tx: submit urb err %d\n", retval);
1465 break;
1466 case 0:
1467 netif_trans_update(net);
1468 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1469 if (dev->txq.qlen >= TX_QLEN (dev))
1470 netif_stop_queue (net);
1471 }
1472 spin_unlock_irqrestore (&dev->txq.lock, flags);
1473
1474 if (retval) {
1475 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1476drop:
1477 dev->net->stats.tx_dropped++;
1478not_drop:
1479 if (skb)
1480 dev_kfree_skb_any (skb);
1481 if (urb) {
1482 kfree(urb->sg);
1483 usb_free_urb(urb);
1484 }
1485 } else
1486 netif_dbg(dev, tx_queued, dev->net,
1487 "> tx, len %u, type 0x%x\n", length, skb->protocol);
1488#ifdef CONFIG_PM
1489deferred:
1490#endif
1491 return NETDEV_TX_OK;
1492}
1493EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1494
1495static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
1496{
1497 struct urb *urb;
1498 int i;
1499 int ret = 0;
1500
1501
1502 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1503 urb = usb_alloc_urb(0, flags);
1504 if (urb != NULL) {
1505 ret = rx_submit(dev, urb, flags);
1506 if (ret)
1507 goto err;
1508 } else {
1509 ret = -ENOMEM;
1510 goto err;
1511 }
1512 }
1513err:
1514 return ret;
1515}
1516
1517
1518
1519
1520
1521static void usbnet_bh (struct timer_list *t)
1522{
1523 struct usbnet *dev = from_timer(dev, t, delay);
1524 struct sk_buff *skb;
1525 struct skb_data *entry;
1526
1527 while ((skb = skb_dequeue (&dev->done))) {
1528 entry = (struct skb_data *) skb->cb;
1529 switch (entry->state) {
1530 case rx_done:
1531 entry->state = rx_cleanup;
1532 rx_process (dev, skb);
1533 continue;
1534 case tx_done:
1535 kfree(entry->urb->sg);
1536
1537 case rx_cleanup:
1538 usb_free_urb (entry->urb);
1539 dev_kfree_skb (skb);
1540 continue;
1541 default:
1542 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1543 }
1544 }
1545
1546
1547 clear_bit(EVENT_RX_KILL, &dev->flags);
1548
1549
1550
1551
1552 if (waitqueue_active(&dev->wait)) {
1553 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1554 wake_up_all(&dev->wait);
1555
1556
1557 } else if (netif_running (dev->net) &&
1558 netif_device_present (dev->net) &&
1559 netif_carrier_ok(dev->net) &&
1560 !timer_pending(&dev->delay) &&
1561 !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
1562 !test_bit(EVENT_RX_HALT, &dev->flags)) {
1563 int temp = dev->rxq.qlen;
1564
1565 if (temp < RX_QLEN(dev)) {
1566 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1567 return;
1568 if (temp != dev->rxq.qlen)
1569 netif_dbg(dev, link, dev->net,
1570 "rxqlen %d --> %d\n",
1571 temp, dev->rxq.qlen);
1572 if (dev->rxq.qlen < RX_QLEN(dev))
1573 tasklet_schedule (&dev->bh);
1574 }
1575 if (dev->txq.qlen < TX_QLEN (dev))
1576 netif_wake_queue (dev->net);
1577 }
1578}
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589void usbnet_disconnect (struct usb_interface *intf)
1590{
1591 struct usbnet *dev;
1592 struct usb_device *xdev;
1593 struct net_device *net;
1594
1595 dev = usb_get_intfdata(intf);
1596 usb_set_intfdata(intf, NULL);
1597 if (!dev)
1598 return;
1599
1600 xdev = interface_to_usbdev (intf);
1601
1602 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1603 intf->dev.driver->name,
1604 xdev->bus->bus_name, xdev->devpath,
1605 dev->driver_info->description);
1606
1607 net = dev->net;
1608 unregister_netdev (net);
1609
1610 cancel_work_sync(&dev->kevent);
1611
1612 usb_scuttle_anchored_urbs(&dev->deferred);
1613
1614 if (dev->driver_info->unbind)
1615 dev->driver_info->unbind (dev, intf);
1616
1617 usb_kill_urb(dev->interrupt);
1618 usb_free_urb(dev->interrupt);
1619 kfree(dev->padding_pkt);
1620
1621 free_percpu(dev->stats64);
1622 free_netdev(net);
1623}
1624EXPORT_SYMBOL_GPL(usbnet_disconnect);
1625
1626static const struct net_device_ops usbnet_netdev_ops = {
1627 .ndo_open = usbnet_open,
1628 .ndo_stop = usbnet_stop,
1629 .ndo_start_xmit = usbnet_start_xmit,
1630 .ndo_tx_timeout = usbnet_tx_timeout,
1631 .ndo_set_rx_mode = usbnet_set_rx_mode,
1632 .ndo_change_mtu = usbnet_change_mtu,
1633 .ndo_get_stats64 = usbnet_get_stats64,
1634 .ndo_set_mac_address = eth_mac_addr,
1635 .ndo_validate_addr = eth_validate_addr,
1636};
1637
1638
1639
1640
1641
1642static struct device_type wlan_type = {
1643 .name = "wlan",
1644};
1645
1646static struct device_type wwan_type = {
1647 .name = "wwan",
1648};
1649
1650int
1651usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1652{
1653 struct usbnet *dev;
1654 struct net_device *net;
1655 struct usb_host_interface *interface;
1656 const struct driver_info *info;
1657 struct usb_device *xdev;
1658 int status;
1659 const char *name;
1660 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1661
1662
1663
1664
1665
1666 if (!driver->supports_autosuspend) {
1667 driver->supports_autosuspend = 1;
1668 pm_runtime_enable(&udev->dev);
1669 }
1670
1671 name = udev->dev.driver->name;
1672 info = (const struct driver_info *) prod->driver_info;
1673 if (!info) {
1674 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1675 return -ENODEV;
1676 }
1677 xdev = interface_to_usbdev (udev);
1678 interface = udev->cur_altsetting;
1679
1680 status = -ENOMEM;
1681
1682
1683 net = alloc_etherdev(sizeof(*dev));
1684 if (!net)
1685 goto out;
1686
1687
1688 SET_NETDEV_DEV(net, &udev->dev);
1689
1690 dev = netdev_priv(net);
1691 dev->udev = xdev;
1692 dev->intf = udev;
1693 dev->driver_info = info;
1694 dev->driver_name = name;
1695
1696 dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1697 if (!dev->stats64)
1698 goto out0;
1699
1700 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1701 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1702 init_waitqueue_head(&dev->wait);
1703 skb_queue_head_init (&dev->rxq);
1704 skb_queue_head_init (&dev->txq);
1705 skb_queue_head_init (&dev->done);
1706 skb_queue_head_init(&dev->rxq_pause);
1707 dev->bh.func = (void (*)(unsigned long))usbnet_bh;
1708 dev->bh.data = (unsigned long)&dev->delay;
1709 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
1710 init_usb_anchor(&dev->deferred);
1711 timer_setup(&dev->delay, usbnet_bh, 0);
1712 mutex_init (&dev->phy_mutex);
1713 mutex_init(&dev->interrupt_mutex);
1714 dev->interrupt_count = 0;
1715
1716 dev->net = net;
1717 strcpy (net->name, "usb%d");
1718 memcpy (net->dev_addr, node_id, sizeof node_id);
1719
1720
1721
1722
1723 dev->hard_mtu = net->mtu + net->hard_header_len;
1724 net->min_mtu = 0;
1725 net->max_mtu = ETH_MAX_MTU;
1726
1727 net->netdev_ops = &usbnet_netdev_ops;
1728 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1729 net->ethtool_ops = &usbnet_ethtool_ops;
1730
1731
1732
1733 if (info->bind) {
1734 status = info->bind (dev, udev);
1735 if (status < 0)
1736 goto out1;
1737
1738
1739
1740
1741 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1742 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1743 (net->dev_addr [0] & 0x02) == 0))
1744 strcpy (net->name, "eth%d");
1745
1746 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1747 strcpy(net->name, "wlan%d");
1748
1749 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1750 strcpy(net->name, "wwan%d");
1751
1752
1753 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1754 net->flags |= IFF_NOARP;
1755
1756
1757 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1758 net->mtu = dev->hard_mtu - net->hard_header_len;
1759 } else if (!info->in || !info->out)
1760 status = usbnet_get_endpoints (dev, udev);
1761 else {
1762 dev->in = usb_rcvbulkpipe (xdev, info->in);
1763 dev->out = usb_sndbulkpipe (xdev, info->out);
1764 if (!(info->flags & FLAG_NO_SETINT))
1765 status = usb_set_interface (xdev,
1766 interface->desc.bInterfaceNumber,
1767 interface->desc.bAlternateSetting);
1768 else
1769 status = 0;
1770
1771 }
1772 if (status >= 0 && dev->status)
1773 status = init_status (dev, udev);
1774 if (status < 0)
1775 goto out3;
1776
1777 if (!dev->rx_urb_size)
1778 dev->rx_urb_size = dev->hard_mtu;
1779 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1780
1781
1782 if (ether_addr_equal(net->dev_addr, node_id))
1783 net->addr_assign_type = NET_ADDR_RANDOM;
1784
1785 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1786 SET_NETDEV_DEVTYPE(net, &wlan_type);
1787 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1788 SET_NETDEV_DEVTYPE(net, &wwan_type);
1789
1790
1791 usbnet_update_max_qlen(dev);
1792
1793 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1794 !(info->flags & FLAG_MULTI_PACKET)) {
1795 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
1796 if (!dev->padding_pkt) {
1797 status = -ENOMEM;
1798 goto out4;
1799 }
1800 }
1801
1802 status = register_netdev (net);
1803 if (status)
1804 goto out5;
1805 netif_info(dev, probe, dev->net,
1806 "register '%s' at usb-%s-%s, %s, %pM\n",
1807 udev->dev.driver->name,
1808 xdev->bus->bus_name, xdev->devpath,
1809 dev->driver_info->description,
1810 net->dev_addr);
1811
1812
1813 usb_set_intfdata (udev, dev);
1814
1815 netif_device_attach (net);
1816
1817 if (dev->driver_info->flags & FLAG_LINK_INTR)
1818 usbnet_link_change(dev, 0, 0);
1819
1820 return 0;
1821
1822out5:
1823 kfree(dev->padding_pkt);
1824out4:
1825 usb_free_urb(dev->interrupt);
1826out3:
1827 if (info->unbind)
1828 info->unbind (dev, udev);
1829out1:
1830
1831
1832
1833
1834
1835 cancel_work_sync(&dev->kevent);
1836 del_timer_sync(&dev->delay);
1837 free_percpu(dev->stats64);
1838out0:
1839 free_netdev(net);
1840out:
1841 return status;
1842}
1843EXPORT_SYMBOL_GPL(usbnet_probe);
1844
1845
1846
1847
1848
1849
1850
1851
1852int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1853{
1854 struct usbnet *dev = usb_get_intfdata(intf);
1855
1856 if (!dev->suspend_count++) {
1857 spin_lock_irq(&dev->txq.lock);
1858
1859 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1860 dev->suspend_count--;
1861 spin_unlock_irq(&dev->txq.lock);
1862 return -EBUSY;
1863 } else {
1864 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1865 spin_unlock_irq(&dev->txq.lock);
1866 }
1867
1868
1869
1870
1871 netif_device_detach (dev->net);
1872 usbnet_terminate_urbs(dev);
1873 __usbnet_status_stop_force(dev);
1874
1875
1876
1877
1878
1879 netif_device_attach (dev->net);
1880 }
1881 return 0;
1882}
1883EXPORT_SYMBOL_GPL(usbnet_suspend);
1884
1885int usbnet_resume (struct usb_interface *intf)
1886{
1887 struct usbnet *dev = usb_get_intfdata(intf);
1888 struct sk_buff *skb;
1889 struct urb *res;
1890 int retval;
1891
1892 if (!--dev->suspend_count) {
1893
1894 __usbnet_status_start_force(dev, GFP_NOIO);
1895
1896 spin_lock_irq(&dev->txq.lock);
1897 while ((res = usb_get_from_anchor(&dev->deferred))) {
1898
1899 skb = (struct sk_buff *)res->context;
1900 retval = usb_submit_urb(res, GFP_ATOMIC);
1901 if (retval < 0) {
1902 dev_kfree_skb_any(skb);
1903 kfree(res->sg);
1904 usb_free_urb(res);
1905 usb_autopm_put_interface_async(dev->intf);
1906 } else {
1907 netif_trans_update(dev->net);
1908 __skb_queue_tail(&dev->txq, skb);
1909 }
1910 }
1911
1912 smp_mb();
1913 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1914 spin_unlock_irq(&dev->txq.lock);
1915
1916 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1917
1918
1919
1920 if (netif_device_present(dev->net) &&
1921 !timer_pending(&dev->delay) &&
1922 !test_bit(EVENT_RX_HALT, &dev->flags))
1923 rx_alloc_submit(dev, GFP_NOIO);
1924
1925 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1926 netif_tx_wake_all_queues(dev->net);
1927 tasklet_schedule (&dev->bh);
1928 }
1929 }
1930
1931 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1932 usb_autopm_get_interface_no_resume(intf);
1933
1934 return 0;
1935}
1936EXPORT_SYMBOL_GPL(usbnet_resume);
1937
1938
1939
1940
1941
1942
1943void usbnet_device_suggests_idle(struct usbnet *dev)
1944{
1945 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1946 dev->intf->needs_remote_wakeup = 1;
1947 usb_autopm_put_interface_async(dev->intf);
1948 }
1949}
1950EXPORT_SYMBOL(usbnet_device_suggests_idle);
1951
1952
1953
1954
1955int usbnet_manage_power(struct usbnet *dev, int on)
1956{
1957 dev->intf->needs_remote_wakeup = on;
1958 return 0;
1959}
1960EXPORT_SYMBOL(usbnet_manage_power);
1961
1962void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1963{
1964
1965 if (link && !need_reset)
1966 netif_carrier_on(dev->net);
1967 else
1968 netif_carrier_off(dev->net);
1969
1970 if (need_reset && link)
1971 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
1972 else
1973 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1974}
1975EXPORT_SYMBOL(usbnet_link_change);
1976
1977
1978static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1979 u16 value, u16 index, void *data, u16 size)
1980{
1981 void *buf = NULL;
1982 int err = -ENOMEM;
1983
1984 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1985 " value=0x%04x index=0x%04x size=%d\n",
1986 cmd, reqtype, value, index, size);
1987
1988 if (size) {
1989 buf = kmalloc(size, GFP_KERNEL);
1990 if (!buf)
1991 goto out;
1992 }
1993
1994 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1995 cmd, reqtype, value, index, buf, size,
1996 USB_CTRL_GET_TIMEOUT);
1997 if (err > 0 && err <= size) {
1998 if (data)
1999 memcpy(data, buf, err);
2000 else
2001 netdev_dbg(dev->net,
2002 "Huh? Data requested but thrown away.\n");
2003 }
2004 kfree(buf);
2005out:
2006 return err;
2007}
2008
2009static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2010 u16 value, u16 index, const void *data,
2011 u16 size)
2012{
2013 void *buf = NULL;
2014 int err = -ENOMEM;
2015
2016 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2017 " value=0x%04x index=0x%04x size=%d\n",
2018 cmd, reqtype, value, index, size);
2019
2020 if (data) {
2021 buf = kmemdup(data, size, GFP_KERNEL);
2022 if (!buf)
2023 goto out;
2024 } else {
2025 if (size) {
2026 WARN_ON_ONCE(1);
2027 err = -EINVAL;
2028 goto out;
2029 }
2030 }
2031
2032 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2033 cmd, reqtype, value, index, buf, size,
2034 USB_CTRL_SET_TIMEOUT);
2035 kfree(buf);
2036
2037out:
2038 return err;
2039}
2040
2041
2042
2043
2044
2045int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2046 u16 value, u16 index, void *data, u16 size)
2047{
2048 int ret;
2049
2050 if (usb_autopm_get_interface(dev->intf) < 0)
2051 return -ENODEV;
2052 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2053 data, size);
2054 usb_autopm_put_interface(dev->intf);
2055 return ret;
2056}
2057EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2058
2059
2060
2061
2062
2063int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2064 u16 value, u16 index, const void *data, u16 size)
2065{
2066 int ret;
2067
2068 if (usb_autopm_get_interface(dev->intf) < 0)
2069 return -ENODEV;
2070 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2071 data, size);
2072 usb_autopm_put_interface(dev->intf);
2073 return ret;
2074}
2075EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2076
2077
2078
2079
2080
2081int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2082 u16 value, u16 index, void *data, u16 size)
2083{
2084 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2085 data, size);
2086}
2087EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2088
2089
2090
2091
2092
2093int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2094 u16 value, u16 index, const void *data,
2095 u16 size)
2096{
2097 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2098 data, size);
2099}
2100EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2101
2102static void usbnet_async_cmd_cb(struct urb *urb)
2103{
2104 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2105 int status = urb->status;
2106
2107 if (status < 0)
2108 dev_dbg(&urb->dev->dev, "%s failed with %d",
2109 __func__, status);
2110
2111 kfree(req);
2112 usb_free_urb(urb);
2113}
2114
2115
2116
2117
2118
2119int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2120 u16 value, u16 index, const void *data, u16 size)
2121{
2122 struct usb_ctrlrequest *req = NULL;
2123 struct urb *urb;
2124 int err = -ENOMEM;
2125 void *buf = NULL;
2126
2127 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2128 " value=0x%04x index=0x%04x size=%d\n",
2129 cmd, reqtype, value, index, size);
2130
2131 urb = usb_alloc_urb(0, GFP_ATOMIC);
2132 if (!urb)
2133 goto fail;
2134
2135 if (data) {
2136 buf = kmemdup(data, size, GFP_ATOMIC);
2137 if (!buf) {
2138 netdev_err(dev->net, "Error allocating buffer"
2139 " in %s!\n", __func__);
2140 goto fail_free;
2141 }
2142 }
2143
2144 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
2145 if (!req)
2146 goto fail_free_buf;
2147
2148 req->bRequestType = reqtype;
2149 req->bRequest = cmd;
2150 req->wValue = cpu_to_le16(value);
2151 req->wIndex = cpu_to_le16(index);
2152 req->wLength = cpu_to_le16(size);
2153
2154 usb_fill_control_urb(urb, dev->udev,
2155 usb_sndctrlpipe(dev->udev, 0),
2156 (void *)req, buf, size,
2157 usbnet_async_cmd_cb, req);
2158 urb->transfer_flags |= URB_FREE_BUFFER;
2159
2160 err = usb_submit_urb(urb, GFP_ATOMIC);
2161 if (err < 0) {
2162 netdev_err(dev->net, "Error submitting the control"
2163 " message: status=%d\n", err);
2164 goto fail_free;
2165 }
2166 return 0;
2167
2168fail_free_buf:
2169 kfree(buf);
2170fail_free:
2171 kfree(req);
2172 usb_free_urb(urb);
2173fail:
2174 return err;
2175
2176}
2177EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2178
2179
2180static int __init usbnet_init(void)
2181{
2182
2183 BUILD_BUG_ON(
2184 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
2185
2186 eth_random_addr(node_id);
2187 return 0;
2188}
2189module_init(usbnet_init);
2190
2191static void __exit usbnet_exit(void)
2192{
2193}
2194module_exit(usbnet_exit);
2195
2196MODULE_AUTHOR("David Brownell");
2197MODULE_DESCRIPTION("USB network driver framework");
2198MODULE_LICENSE("GPL");
2199