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