36 #include <linux/module.h>
38 #include <linux/netdevice.h>
40 #include <linux/ctype.h>
41 #include <linux/ethtool.h>
43 #include <linux/mii.h>
46 #include <linux/slab.h>
47 #include <linux/kernel.h>
50 #define DRIVER_VERSION "22-Aug-2005"
66 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
67 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
68 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
69 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
70 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
73 #define TX_TIMEOUT_JIFFIES (5*HZ)
77 #define THROTTLE_JIFFIES (HZ/8)
80 #define UNLINK_TIMEOUT_MS 3
90 static int msg_level = -1;
100 struct usb_host_interface *alt =
NULL;
104 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
107 in = out = status =
NULL;
108 alt = intf->altsetting +
tmp;
114 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
115 struct usb_host_endpoint *
e;
118 e = alt->endpoint + ep;
119 switch (e->desc.bmAttributes) {
121 if (!usb_endpoint_dir_in(&e->desc))
130 if (usb_endpoint_dir_in(&e->desc)) {
133 else if (intr && !status)
143 if (!alt || !in || !out)
146 if (alt->desc.bAlternateSetting != 0 ||
149 alt->desc.bAlternateSetting);
154 dev->
in = usb_rcvbulkpipe (dev->
udev,
156 dev->
out = usb_sndbulkpipe (dev->
udev,
166 unsigned char buf [13];
171 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
176 for (i = tmp = 0; i < 6; i++, tmp += 2)
177 dev->
net->dev_addr [i] =
183 static void intr_complete (
struct urb *
urb)
198 "intr shutdown, code %d\n", status);
209 if (!netif_running (dev->
net))
215 "intr resubmit --> %d\n", status);
228 pipe = usb_rcvintpipe (dev->
udev,
229 dev->
status->desc.bEndpointAddress
231 maxp = usb_maxpacket (dev->
udev, pipe, 0);
234 period =
max ((
int) dev->
status->desc.bInterval,
245 buf, maxp, intr_complete, dev, period);
246 dev->
interrupt->transfer_flags |= URB_FREE_BUFFER;
248 "status ep%din, %d bytes period %d\n",
249 usb_pipeendpoint(pipe), maxp, period);
269 dev->
net->stats.rx_packets++;
270 dev->
net->stats.rx_bytes += skb->
len;
282 "netif_rx status %d\n", status);
295 struct usbnet *dev = netdev_priv(net);
324 __skb_queue_tail(list, newsk);
342 old_state = entry->
state;
344 __skb_unlink(skb, list);
345 spin_unlock(&list->
lock);
346 spin_lock(&dev->
done.lock);
347 __skb_queue_tail(&dev->
done, skb);
348 if (dev->
done.qlen == 1)
349 tasklet_schedule(&dev->
bh);
350 spin_unlock_irqrestore(&dev->
done.lock, flags);
364 netdev_err(dev->
net,
"kevent %d may have been dropped\n", work);
373 static void rx_complete (
struct urb *urb);
375 static int rx_submit (
struct usbnet *dev,
struct urb *urb,
gfp_t flags)
380 unsigned long lockflags;
383 skb = __netdev_alloc_skb_ip_align(dev->
net, size, flags);
391 entry = (
struct skb_data *) skb->cb;
396 usb_fill_bulk_urb (urb, dev->
udev, dev->
in,
397 skb->
data, size, rx_complete, skb);
401 if (netif_running (dev->
net) &&
402 netif_device_present (dev->
net) &&
421 "rx submit, %d\n", retval);
422 tasklet_schedule (&dev->
bh);
431 spin_unlock_irqrestore (&dev->
rxq.lock, lockflags);
442 static inline void rx_process (
struct usbnet *dev,
struct sk_buff *skb)
448 dev->
net->stats.rx_errors++;
463 dev->
net->stats.rx_errors++;
470 static void rx_complete (
struct urb *urb)
475 int urb_status = urb->status;
478 skb_put (skb, urb->actual_length);
482 switch (urb_status) {
485 if (skb->
len < dev->
net->hard_header_len) {
487 dev->
net->stats.rx_errors++;
488 dev->
net->stats.rx_length_errors++;
490 "rx length %d\n", skb->
len);
500 dev->
net->stats.rx_errors++;
508 "rx shutdown, code %d\n", urb_status);
518 dev->
net->stats.rx_errors++;
519 if (!timer_pending (&dev->
delay)) {
522 "rx throttle %d\n", urb_status);
532 dev->
net->stats.rx_over_errors++;
537 dev->
net->stats.rx_errors++;
538 netif_dbg(dev, rx_err, dev->
net,
"rx status %d\n", urb_status);
542 state = defer_bh(dev, skb, &dev->
rxq, state);
545 if (netif_running (dev->
net) &&
549 usb_mark_last_busy(dev->
udev);
554 netif_dbg(dev, rx_err, dev->
net,
"no read resubmitted\n");
578 tasklet_schedule(&dev->
bh);
581 "paused rx queue disabled, %d skbs requeued\n", num);
602 while (!skb_queue_empty(q)) {
607 skb_queue_walk(q, skb) {
608 entry = (
struct skb_data *) skb->cb;
625 spin_unlock_irqrestore(&q->
lock, flags);
636 spin_unlock_irqrestore (&q->
lock, flags);
645 if (netif_running(dev->
net)) {
646 (
void) unlink_urbs (dev, &dev->
rxq);
647 tasklet_schedule(&dev->
bh);
655 static void usbnet_terminate_urbs(
struct usbnet *dev)
664 dev->
wait = &unlink_wakeup;
665 temp = unlink_urbs(dev, &dev->
txq) +
666 unlink_urbs(dev, &dev->
rxq);
669 while (!skb_queue_empty(&dev->
rxq)
670 && !skb_queue_empty(&dev->
txq)
671 && !skb_queue_empty(&dev->
done)) {
675 "waited for %d urb completions\n", temp);
684 struct usbnet *dev = netdev_priv(net);
689 netif_stop_queue (net);
692 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
699 retval = info->
stop(dev);
702 "stop fail (%d) usbnet usb-%s-%s, %s\n",
704 dev->
udev->bus->bus_name, dev->
udev->devpath,
709 usbnet_terminate_urbs(dev);
725 usb_autopm_put_interface(dev->
intf);
739 struct usbnet *dev = netdev_priv(net);
743 if ((retval = usb_autopm_get_interface(dev->
intf)) < 0) {
745 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
747 dev->
udev->bus->bus_name,
754 if (info->
reset && (retval = info->
reset (dev)) < 0) {
756 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
758 dev->
udev->bus->bus_name,
766 netif_dbg(dev, ifup, dev->
net,
"can't open; %d\n", retval);
775 "intr submit %d\n", retval);
781 netif_start_queue (net);
783 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
794 tasklet_schedule (&dev->
bh);
798 goto done_manage_power_error;
799 usb_autopm_put_interface(dev->
intf);
803 done_manage_power_error:
806 usb_autopm_put_interface(dev->
intf);
820 struct usbnet *dev = netdev_priv(net);
822 if (!dev->
mii.mdio_read)
831 struct usbnet *dev = netdev_priv(net);
834 if (!dev->
mii.mdio_write)
850 struct usbnet *dev = netdev_priv(net);
857 if (dev->
mii.mdio_read)
867 struct usbnet *dev = netdev_priv(net);
869 if (!dev->
mii.mdio_write)
878 struct usbnet *dev = netdev_priv(net);
890 struct usbnet *dev = netdev_priv(net);
898 struct usbnet *dev = netdev_priv(net);
905 static const struct ethtool_ops usbnet_ethtool_ops = {
932 unlink_urbs (dev, &dev->
txq);
933 status = usb_autopm_get_interface(dev->
intf);
937 usb_autopm_put_interface(dev->
intf);
943 netdev_err(dev->
net,
"can't clear tx halt, status %d\n",
948 netif_wake_queue (dev->
net);
952 unlink_urbs (dev, &dev->
rxq);
953 status = usb_autopm_get_interface(dev->
intf);
957 usb_autopm_put_interface(dev->
intf);
963 netdev_err(dev->
net,
"can't clear rx halt, status %d\n",
967 tasklet_schedule (&dev->
bh);
973 struct urb *urb =
NULL;
976 if (netif_running (dev->
net))
982 status = usb_autopm_get_interface(dev->
intf);
989 usb_autopm_put_interface(dev->
intf);
992 tasklet_schedule (&dev->
bh);
1001 status = usb_autopm_get_interface(dev->
intf);
1005 usb_autopm_put_interface(dev->
intf);
1007 netdev_info(dev->
net,
"link reset failed (%d) usbnet usb-%s-%s, %s\n",
1009 dev->
udev->bus->bus_name,
1013 usb_autopm_put_interface(dev->
intf);
1023 static void tx_complete (
struct urb *urb)
1029 if (urb->status == 0) {
1031 dev->
net->stats.tx_packets++;
1032 dev->
net->stats.tx_bytes += entry->
length;
1034 dev->
net->stats.tx_errors++;
1036 switch (urb->status) {
1051 usb_mark_last_busy(dev->
udev);
1052 if (!timer_pending (&dev->
delay)) {
1056 "tx throttle %d\n", urb->status);
1058 netif_stop_queue (dev->
net);
1062 "tx err %d\n", entry->
urb->status);
1067 usb_autopm_put_interface_async(dev->
intf);
1075 struct usbnet *dev = netdev_priv(net);
1077 unlink_urbs (dev, &dev->
txq);
1078 tasklet_schedule (&dev->
bh);
1089 struct usbnet *dev = netdev_priv(net);
1091 struct urb *urb =
NULL;
1094 unsigned long flags;
1098 skb_tx_timestamp(skb);
1106 netif_dbg(dev, tx_err, dev->
net,
"can't tx_fixup skb\n");
1121 entry = (
struct skb_data *) skb->cb;
1126 usb_fill_bulk_urb (urb, dev->
udev, dev->
out,
1127 skb->
data, skb->
len, tx_complete, skb);
1139 urb->transfer_buffer_length++;
1140 if (skb_tailroom(skb)) {
1146 urb->transfer_flags |= URB_ZERO_PACKET;
1150 retval = usb_autopm_get_interface_async(dev->
intf);
1152 spin_unlock_irqrestore(&dev->
txq.lock, flags);
1162 netif_stop_queue(net);
1164 spin_unlock_irqrestore(&dev->
txq.lock, flags);
1165 netdev_dbg(dev->
net,
"Delaying transmission for resumption\n");
1172 netif_stop_queue (net);
1174 usb_autopm_put_interface_async(dev->
intf);
1177 usb_autopm_put_interface_async(dev->
intf);
1179 "tx: submit urb err %d\n", retval);
1185 netif_stop_queue (net);
1187 spin_unlock_irqrestore (&dev->
txq.lock, flags);
1190 netif_dbg(dev, tx_err, dev->
net,
"drop, code %d\n", retval);
1192 dev->
net->stats.tx_dropped++;
1199 "> tx, len %d, type 0x%x\n", length, skb->
protocol);
1207 static int rx_alloc_submit(
struct usbnet *dev,
gfp_t flags)
1214 for (i = 0; i < 10 && dev->
rxq.qlen <
RX_QLEN(dev); i++) {
1217 ret = rx_submit(dev, urb, flags);
1233 static void usbnet_bh (
unsigned long param)
1240 entry = (
struct skb_data *) skb->cb;
1241 switch (entry->
state) {
1244 rx_process (dev, skb);
1249 dev_kfree_skb (skb);
1258 if ((dev->
txq.qlen + dev->
rxq.qlen + dev->
done.qlen) == 0) {
1263 }
else if (netif_running (dev->
net) &&
1264 netif_device_present (dev->
net) &&
1265 !timer_pending (&dev->
delay) &&
1267 int temp = dev->
rxq.qlen;
1272 if (temp != dev->
rxq.qlen)
1274 "rxqlen %d --> %d\n",
1275 temp, dev->
rxq.qlen);
1277 tasklet_schedule (&dev->
bh);
1280 netif_wake_queue (dev->
net);
1296 struct usb_device *xdev;
1299 dev = usb_get_intfdata(intf);
1300 usb_set_intfdata(intf,
NULL);
1304 xdev = interface_to_usbdev (intf);
1306 netif_info(dev, probe, dev->
net,
"unregister '%s' usb-%s-%s, %s\n",
1307 intf->dev.driver->name,
1308 xdev->bus->bus_name, xdev->devpath,
1357 struct usb_device *xdev;
1360 struct usb_driver *
driver = to_usb_driver(udev->dev.driver);
1366 if (!driver->supports_autosuspend) {
1367 driver->supports_autosuspend = 1;
1371 name = udev->dev.driver->name;
1374 dev_dbg (&udev->dev,
"blacklisted by %s\n", name);
1377 xdev = interface_to_usbdev (udev);
1378 interface = udev->cur_altsetting;
1383 net = alloc_etherdev(
sizeof(*dev));
1390 dev = netdev_priv(net);
1397 skb_queue_head_init (&dev->
rxq);
1398 skb_queue_head_init (&dev->
txq);
1399 skb_queue_head_init (&dev->
done);
1401 dev->
bh.func = usbnet_bh;
1402 dev->
bh.data = (
unsigned long) dev;
1405 dev->
delay.function = usbnet_bh;
1432 status = info->
bind (dev, udev);
1453 }
else if (!info->
in || !info->
out)
1456 dev->
in = usb_rcvbulkpipe (xdev, info->
in);
1457 dev->
out = usb_sndbulkpipe (xdev, info->
out);
1460 interface->desc.bInterfaceNumber,
1461 interface->desc.bAlternateSetting);
1466 if (status >= 0 && dev->
status)
1467 status = init_status (dev, udev);
1484 "register '%s' at usb-%s-%s, %s, %pM\n",
1485 udev->dev.driver->name,
1486 xdev->bus->bus_name, xdev->devpath,
1491 usb_set_intfdata (udev, dev);
1504 info->
unbind (dev, udev);
1521 struct usbnet *dev = usb_get_intfdata(intf);
1524 spin_lock_irq(&dev->
txq.lock);
1528 spin_unlock_irq(&dev->
txq.lock);
1532 spin_unlock_irq(&dev->
txq.lock);
1539 usbnet_terminate_urbs(dev);
1554 struct usbnet *dev = usb_get_intfdata(intf);
1564 spin_lock_irq(&dev->
txq.lock);
1567 skb = (
struct sk_buff *)res->context;
1572 usb_autopm_put_interface_async(dev->
intf);
1575 __skb_queue_tail(&dev->
txq, skb);
1581 spin_unlock_irq(&dev->
txq.lock);
1586 netif_device_present(dev->
net) &&
1587 !timer_pending(&dev->
delay) &&
1592 netif_tx_wake_all_queues(dev->
net);
1593 tasklet_schedule (&dev->
bh);
1598 usb_autopm_get_interface_no_resume(intf);
1612 dev->
intf->needs_remote_wakeup = 1;
1613 usb_autopm_put_interface_async(dev->
intf);
1620 static int __init usbnet_init(
void)
1631 static void __exit usbnet_exit(
void)