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 | |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 143 | if (alt->desc.bAlternateSetting != 0 || |
| 144 | !(dev->driver_info->flags & FLAG_NO_SETINT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 246 | netdev_dbg(dev->net, "< rx, len %zu, type 0x%x\n", |
| 247 | skb->len + sizeof (struct ethhdr), skb->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 251 | netdev_dbg(dev->net, "netif_rx status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 316 | netdev_err(dev->net, "kevent %d may have been dropped\n", work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | else |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 318 | netdev_dbg(dev->net, "kevent %d scheduled\n", work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 336 | netdev_dbg(dev->net, "no rx skb\n"); |
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 | |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 354 | if (netif_running (dev->net) && |
| 355 | netif_device_present (dev->net) && |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 356 | !test_bit (EVENT_RX_HALT, &dev->flags) && |
| 357 | !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 358 | switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | case -EPIPE: |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 360 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | break; |
| 362 | case -ENOMEM: |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 363 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | break; |
| 365 | case -ENODEV: |
| 366 | if (netif_msg_ifdown (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 367 | netdev_dbg(dev->net, "device gone\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | netif_device_detach (dev->net); |
| 369 | break; |
| 370 | default: |
| 371 | if (netif_msg_rx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 372 | netdev_dbg(dev->net, "rx submit, %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | tasklet_schedule (&dev->bh); |
| 374 | break; |
| 375 | case 0: |
| 376 | __skb_queue_tail (&dev->rxq, skb); |
| 377 | } |
| 378 | } else { |
| 379 | if (netif_msg_ifdown (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 380 | netdev_dbg(dev->net, "rx: stopped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | retval = -ENOLINK; |
| 382 | } |
| 383 | spin_unlock_irqrestore (&dev->rxq.lock, lockflags); |
| 384 | if (retval) { |
| 385 | dev_kfree_skb_any (skb); |
| 386 | usb_free_urb (urb); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /*-------------------------------------------------------------------------*/ |
| 392 | |
| 393 | static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) |
| 394 | { |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 395 | if (dev->driver_info->rx_fixup && |
| 396 | !dev->driver_info->rx_fixup (dev, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | goto error; |
| 398 | // else network stack removes extra byte if we forced a short packet |
| 399 | |
| 400 | if (skb->len) |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 401 | usbnet_skb_return (dev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | else { |
| 403 | if (netif_msg_rx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 404 | netdev_dbg(dev->net, "drop\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | error: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 406 | dev->net->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | skb_queue_tail (&dev->done, skb); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /*-------------------------------------------------------------------------*/ |
| 412 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 413 | static void rx_complete (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 416 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 417 | struct usbnet *dev = entry->dev; |
| 418 | int urb_status = urb->status; |
| 419 | |
| 420 | skb_put (skb, urb->actual_length); |
| 421 | entry->state = rx_done; |
| 422 | entry->urb = NULL; |
| 423 | |
| 424 | switch (urb_status) { |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 425 | /* success */ |
| 426 | case 0: |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 427 | if (skb->len < dev->net->hard_header_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | entry->state = rx_cleanup; |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 429 | dev->net->stats.rx_errors++; |
| 430 | dev->net->stats.rx_length_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (netif_msg_rx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 432 | netdev_dbg(dev->net, "rx length %d\n", skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | break; |
| 435 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 436 | /* stalls need manual reset. this is rare ... except that |
| 437 | * when going through USB 2.0 TTs, unplug appears this way. |
Jean Delvare | 3ac49a1 | 2009-06-04 16:20:28 +0200 | [diff] [blame] | 438 | * we avoid the highspeed version of the ETIMEDOUT/EILSEQ |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 439 | * storm, recovering as needed. |
| 440 | */ |
| 441 | case -EPIPE: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 442 | dev->net->stats.rx_errors++; |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 443 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | // FALLTHROUGH |
| 445 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 446 | /* software-driven interface shutdown */ |
| 447 | case -ECONNRESET: /* async unlink */ |
| 448 | case -ESHUTDOWN: /* hardware gone */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (netif_msg_ifdown (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 450 | netdev_dbg(dev->net, "rx shutdown, code %d\n", urb_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | goto block; |
| 452 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 453 | /* we get controller i/o faults during khubd disconnect() delays. |
| 454 | * throttle down resubmits, to avoid log floods; just temporarily, |
| 455 | * so we still recover when the fault isn't a khubd delay. |
| 456 | */ |
| 457 | case -EPROTO: |
| 458 | case -ETIME: |
| 459 | case -EILSEQ: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 460 | dev->net->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (!timer_pending (&dev->delay)) { |
| 462 | mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); |
| 463 | if (netif_msg_link (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 464 | netdev_dbg(dev->net, "rx throttle %d\n", urb_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | block: |
| 467 | entry->state = rx_cleanup; |
| 468 | entry->urb = urb; |
| 469 | urb = NULL; |
| 470 | break; |
| 471 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 472 | /* data overrun ... flush fifo? */ |
| 473 | case -EOVERFLOW: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 474 | dev->net->stats.rx_over_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | // FALLTHROUGH |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 476 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 477 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | entry->state = rx_cleanup; |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 479 | dev->net->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (netif_msg_rx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 481 | netdev_dbg(dev->net, "rx status %d\n", urb_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 485 | defer_bh(dev, skb, &dev->rxq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | if (urb) { |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 488 | if (netif_running (dev->net) && |
| 489 | !test_bit (EVENT_RX_HALT, &dev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | rx_submit (dev, urb, GFP_ATOMIC); |
| 491 | return; |
| 492 | } |
| 493 | usb_free_urb (urb); |
| 494 | } |
| 495 | if (netif_msg_rx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 496 | netdev_dbg(dev->net, "no read resubmitted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 499 | static void intr_complete (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | struct usbnet *dev = urb->context; |
| 502 | int status = urb->status; |
| 503 | |
| 504 | switch (status) { |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 505 | /* success */ |
| 506 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | dev->driver_info->status(dev, urb); |
| 508 | break; |
| 509 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 510 | /* software-driven interface shutdown */ |
| 511 | case -ENOENT: /* urb killed */ |
| 512 | case -ESHUTDOWN: /* hardware gone */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (netif_msg_ifdown (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 514 | netdev_dbg(dev->net, "intr shutdown, code %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return; |
| 516 | |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 517 | /* NOTE: not throttling like RX/TX, since this endpoint |
| 518 | * already polls infrequently |
| 519 | */ |
| 520 | default: |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 521 | netdev_dbg(dev->net, "intr status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | |
| 525 | if (!netif_running (dev->net)) |
| 526 | return; |
| 527 | |
| 528 | memset(urb->transfer_buffer, 0, urb->transfer_buffer_length); |
| 529 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 530 | if (status != 0 && netif_msg_timer (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 531 | netdev_err(dev->net, "intr resubmit --> %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /*-------------------------------------------------------------------------*/ |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 535 | void usbnet_pause_rx(struct usbnet *dev) |
| 536 | { |
| 537 | set_bit(EVENT_RX_PAUSED, &dev->flags); |
| 538 | |
| 539 | if (netif_msg_rx_status(dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 540 | netdev_dbg(dev->net, "paused rx queue enabled\n"); |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 541 | } |
| 542 | EXPORT_SYMBOL_GPL(usbnet_pause_rx); |
| 543 | |
| 544 | void usbnet_resume_rx(struct usbnet *dev) |
| 545 | { |
| 546 | struct sk_buff *skb; |
| 547 | int num = 0; |
| 548 | |
| 549 | clear_bit(EVENT_RX_PAUSED, &dev->flags); |
| 550 | |
| 551 | while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { |
| 552 | usbnet_skb_return(dev, skb); |
| 553 | num++; |
| 554 | } |
| 555 | |
| 556 | tasklet_schedule(&dev->bh); |
| 557 | |
| 558 | if (netif_msg_rx_status(dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 559 | netdev_dbg(dev->net, "paused rx queue disabled, %d skbs requeued\n", |
| 560 | num); |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 561 | } |
| 562 | EXPORT_SYMBOL_GPL(usbnet_resume_rx); |
| 563 | |
| 564 | void usbnet_purge_paused_rxq(struct usbnet *dev) |
| 565 | { |
| 566 | skb_queue_purge(&dev->rxq_pause); |
| 567 | } |
| 568 | EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); |
| 569 | |
| 570 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | // unlink pending rx/tx; completion handlers do all other cleanup |
| 573 | |
| 574 | static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) |
| 575 | { |
| 576 | unsigned long flags; |
| 577 | struct sk_buff *skb, *skbnext; |
| 578 | int count = 0; |
| 579 | |
| 580 | spin_lock_irqsave (&q->lock, flags); |
David S. Miller | 83bfba5 | 2008-09-22 20:18:47 -0700 | [diff] [blame] | 581 | skb_queue_walk_safe(q, skb, skbnext) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | struct skb_data *entry; |
| 583 | struct urb *urb; |
| 584 | int retval; |
| 585 | |
| 586 | entry = (struct skb_data *) skb->cb; |
| 587 | urb = entry->urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | // during some PM-driven resume scenarios, |
| 590 | // these (async) unlinks complete immediately |
| 591 | retval = usb_unlink_urb (urb); |
| 592 | if (retval != -EINPROGRESS && retval != 0) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 593 | netdev_dbg(dev->net, "unlink urb err, %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | else |
| 595 | count++; |
| 596 | } |
| 597 | spin_unlock_irqrestore (&q->lock, flags); |
| 598 | return count; |
| 599 | } |
| 600 | |
Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 601 | // Flush all pending rx urbs |
| 602 | // minidrivers may need to do this when the MTU changes |
| 603 | |
| 604 | void usbnet_unlink_rx_urbs(struct usbnet *dev) |
| 605 | { |
| 606 | if (netif_running(dev->net)) { |
| 607 | (void) unlink_urbs (dev, &dev->rxq); |
| 608 | tasklet_schedule(&dev->bh); |
| 609 | } |
| 610 | } |
| 611 | EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | /*-------------------------------------------------------------------------*/ |
| 614 | |
| 615 | // precondition: never called in_interrupt |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 616 | static void usbnet_terminate_urbs(struct usbnet *dev) |
| 617 | { |
| 618 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup); |
| 619 | DECLARE_WAITQUEUE(wait, current); |
| 620 | int temp; |
| 621 | |
| 622 | /* ensure there are no more active urbs */ |
| 623 | add_wait_queue(&unlink_wakeup, &wait); |
| 624 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 625 | dev->wait = &unlink_wakeup; |
| 626 | temp = unlink_urbs(dev, &dev->txq) + |
| 627 | unlink_urbs(dev, &dev->rxq); |
| 628 | |
| 629 | /* maybe wait for deletions to finish. */ |
| 630 | while (!skb_queue_empty(&dev->rxq) |
| 631 | && !skb_queue_empty(&dev->txq) |
| 632 | && !skb_queue_empty(&dev->done)) { |
| 633 | schedule_timeout(UNLINK_TIMEOUT_MS); |
| 634 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 635 | if (netif_msg_ifdown(dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 636 | netdev_dbg(dev->net, "waited for %d urb completions\n", |
| 637 | temp); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 638 | } |
| 639 | set_current_state(TASK_RUNNING); |
| 640 | dev->wait = NULL; |
| 641 | remove_wait_queue(&unlink_wakeup, &wait); |
| 642 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 644 | int usbnet_stop (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
| 646 | struct usbnet *dev = netdev_priv(net); |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 647 | struct driver_info *info = dev->driver_info; |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 648 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | netif_stop_queue (net); |
| 651 | |
| 652 | if (netif_msg_ifdown (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 653 | netdev_info(dev->net, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", |
| 654 | net->stats.rx_packets, net->stats.tx_packets, |
| 655 | net->stats.rx_errors, net->stats.tx_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 657 | /* allow minidriver to stop correctly (wireless devices to turn off |
| 658 | * radio etc) */ |
| 659 | if (info->stop) { |
| 660 | retval = info->stop(dev); |
| 661 | if (retval < 0 && netif_msg_ifdown(dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 662 | netdev_info(dev->net, |
| 663 | "stop fail (%d) usbnet usb-%s-%s, %s\n", |
| 664 | retval, |
| 665 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 666 | info->description); |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 667 | } |
| 668 | |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 669 | if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) |
| 670 | usbnet_terminate_urbs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | usb_kill_urb(dev->interrupt); |
| 673 | |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 674 | usbnet_purge_paused_rxq(dev); |
| 675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | /* deferred work (task, timer, softirq) must also stop. |
| 677 | * can't flush_scheduled_work() until we drop rtnl (later), |
| 678 | * else workers could deadlock; so make workers a NOP. |
| 679 | */ |
| 680 | dev->flags = 0; |
| 681 | del_timer_sync (&dev->delay); |
| 682 | tasklet_kill (&dev->bh); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 683 | if (info->manage_power) |
| 684 | info->manage_power(dev, 0); |
| 685 | else |
| 686 | usb_autopm_put_interface(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | return 0; |
| 689 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 690 | EXPORT_SYMBOL_GPL(usbnet_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | /*-------------------------------------------------------------------------*/ |
| 693 | |
| 694 | // posts reads, and enables write queuing |
| 695 | |
| 696 | // precondition: never called in_interrupt |
| 697 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 698 | int usbnet_open (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
| 700 | struct usbnet *dev = netdev_priv(net); |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 701 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | struct driver_info *info = dev->driver_info; |
| 703 | |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 704 | if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { |
| 705 | if (netif_msg_ifup (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 706 | netdev_info(dev->net, |
| 707 | "resumption fail (%d) usbnet usb-%s-%s, %s\n", |
| 708 | retval, |
| 709 | dev->udev->bus->bus_name, |
| 710 | dev->udev->devpath, |
| 711 | info->description); |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 712 | goto done_nopm; |
| 713 | } |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | // put into "known safe" state |
| 716 | if (info->reset && (retval = info->reset (dev)) < 0) { |
| 717 | if (netif_msg_ifup (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 718 | netdev_info(dev->net, |
| 719 | "open reset fail (%d) usbnet usb-%s-%s, %s\n", |
| 720 | retval, |
| 721 | dev->udev->bus->bus_name, |
| 722 | dev->udev->devpath, |
| 723 | info->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | goto done; |
| 725 | } |
| 726 | |
| 727 | // insist peer be connected |
| 728 | if (info->check_connect && (retval = info->check_connect (dev)) < 0) { |
| 729 | if (netif_msg_ifup (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 730 | netdev_dbg(dev->net, "can't open; %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | goto done; |
| 732 | } |
| 733 | |
| 734 | /* start any status interrupt transfer */ |
| 735 | if (dev->interrupt) { |
| 736 | retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); |
| 737 | if (retval < 0) { |
| 738 | if (netif_msg_ifup (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 739 | netdev_err(dev->net, "intr submit %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | goto done; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | netif_start_queue (net); |
| 745 | if (netif_msg_ifup (dev)) { |
| 746 | char *framing; |
| 747 | |
| 748 | if (dev->driver_info->flags & FLAG_FRAMING_NC) |
| 749 | framing = "NetChip"; |
| 750 | else if (dev->driver_info->flags & FLAG_FRAMING_GL) |
| 751 | framing = "GeneSys"; |
| 752 | else if (dev->driver_info->flags & FLAG_FRAMING_Z) |
| 753 | framing = "Zaurus"; |
| 754 | else if (dev->driver_info->flags & FLAG_FRAMING_RN) |
| 755 | framing = "RNDIS"; |
| 756 | else if (dev->driver_info->flags & FLAG_FRAMING_AX) |
| 757 | framing = "ASIX"; |
| 758 | else |
| 759 | framing = "simple"; |
| 760 | |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 761 | netdev_info(dev->net, "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", |
| 762 | (int)RX_QLEN(dev), (int)TX_QLEN(dev), |
| 763 | dev->net->mtu, framing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // delay posting reads until we're fully open |
| 767 | tasklet_schedule (&dev->bh); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 768 | if (info->manage_power) { |
| 769 | retval = info->manage_power(dev, 1); |
| 770 | if (retval < 0) |
| 771 | goto done; |
| 772 | usb_autopm_put_interface(dev->intf); |
| 773 | } |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 774 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | done: |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 776 | usb_autopm_put_interface(dev->intf); |
| 777 | done_nopm: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return retval; |
| 779 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 780 | EXPORT_SYMBOL_GPL(usbnet_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | /*-------------------------------------------------------------------------*/ |
| 783 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 784 | /* ethtool methods; minidrivers may need to add some more, but |
| 785 | * they'll probably want to use this base set. |
| 786 | */ |
| 787 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 788 | int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 789 | { |
| 790 | struct usbnet *dev = netdev_priv(net); |
| 791 | |
| 792 | if (!dev->mii.mdio_read) |
| 793 | return -EOPNOTSUPP; |
| 794 | |
| 795 | return mii_ethtool_gset(&dev->mii, cmd); |
| 796 | } |
| 797 | EXPORT_SYMBOL_GPL(usbnet_get_settings); |
| 798 | |
| 799 | int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 800 | { |
| 801 | struct usbnet *dev = netdev_priv(net); |
| 802 | int retval; |
| 803 | |
| 804 | if (!dev->mii.mdio_write) |
| 805 | return -EOPNOTSUPP; |
| 806 | |
| 807 | retval = mii_ethtool_sset(&dev->mii, cmd); |
| 808 | |
| 809 | /* link speed/duplex might have changed */ |
| 810 | if (dev->driver_info->link_reset) |
| 811 | dev->driver_info->link_reset(dev); |
| 812 | |
| 813 | return retval; |
| 814 | |
| 815 | } |
| 816 | EXPORT_SYMBOL_GPL(usbnet_set_settings); |
| 817 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 818 | u32 usbnet_get_link (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
| 820 | struct usbnet *dev = netdev_priv(net); |
| 821 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 822 | /* If a check_connect is defined, return its result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (dev->driver_info->check_connect) |
| 824 | return dev->driver_info->check_connect (dev) == 0; |
| 825 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 826 | /* if the device has mii operations, use those */ |
| 827 | if (dev->mii.mdio_read) |
| 828 | return mii_link_ok(&dev->mii); |
| 829 | |
Bjørn Mork | 05ffb3e | 2009-03-01 20:45:40 -0800 | [diff] [blame] | 830 | /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ |
| 831 | return ethtool_op_get_link(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 833 | EXPORT_SYMBOL_GPL(usbnet_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 835 | int usbnet_nway_reset(struct net_device *net) |
| 836 | { |
| 837 | struct usbnet *dev = netdev_priv(net); |
| 838 | |
| 839 | if (!dev->mii.mdio_write) |
| 840 | return -EOPNOTSUPP; |
| 841 | |
| 842 | return mii_nway_restart(&dev->mii); |
| 843 | } |
| 844 | EXPORT_SYMBOL_GPL(usbnet_nway_reset); |
| 845 | |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 846 | void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) |
| 847 | { |
| 848 | struct usbnet *dev = netdev_priv(net); |
| 849 | |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 850 | strncpy (info->driver, dev->driver_name, sizeof info->driver); |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 851 | strncpy (info->version, DRIVER_VERSION, sizeof info->version); |
| 852 | strncpy (info->fw_version, dev->driver_info->description, |
| 853 | sizeof info->fw_version); |
| 854 | usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); |
| 855 | } |
| 856 | EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); |
| 857 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 858 | u32 usbnet_get_msglevel (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | { |
| 860 | struct usbnet *dev = netdev_priv(net); |
| 861 | |
| 862 | return dev->msg_enable; |
| 863 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(usbnet_get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 866 | void usbnet_set_msglevel (struct net_device *net, u32 level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | struct usbnet *dev = netdev_priv(net); |
| 869 | |
| 870 | dev->msg_enable = level; |
| 871 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 872 | EXPORT_SYMBOL_GPL(usbnet_set_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 874 | /* drivers may override default ethtool_ops in their bind() routine */ |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 875 | static const struct ethtool_ops usbnet_ethtool_ops = { |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 876 | .get_settings = usbnet_get_settings, |
| 877 | .set_settings = usbnet_set_settings, |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 878 | .get_link = usbnet_get_link, |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 879 | .nway_reset = usbnet_nway_reset, |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 880 | .get_drvinfo = usbnet_get_drvinfo, |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 881 | .get_msglevel = usbnet_get_msglevel, |
| 882 | .set_msglevel = usbnet_set_msglevel, |
| 883 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | /*-------------------------------------------------------------------------*/ |
| 886 | |
| 887 | /* work that cannot be done in interrupt context uses keventd. |
| 888 | * |
| 889 | * NOTE: with 2.5 we could do more of this using completion callbacks, |
| 890 | * especially now that control transfers can be queued. |
| 891 | */ |
| 892 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 893 | kevent (struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 895 | struct usbnet *dev = |
| 896 | container_of(work, struct usbnet, kevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | int status; |
| 898 | |
| 899 | /* usb_clear_halt() needs a thread context */ |
| 900 | if (test_bit (EVENT_TX_HALT, &dev->flags)) { |
| 901 | unlink_urbs (dev, &dev->txq); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 902 | status = usb_autopm_get_interface(dev->intf); |
| 903 | if (status < 0) |
| 904 | goto fail_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | status = usb_clear_halt (dev->udev, dev->out); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 906 | usb_autopm_put_interface(dev->intf); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 907 | if (status < 0 && |
| 908 | status != -EPIPE && |
| 909 | status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (netif_msg_tx_err (dev)) |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 911 | fail_pipe: |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 912 | netdev_err(dev->net, "can't clear tx halt, status %d\n", |
| 913 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } else { |
| 915 | clear_bit (EVENT_TX_HALT, &dev->flags); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 916 | if (status != -ESHUTDOWN) |
| 917 | netif_wake_queue (dev->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | if (test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 921 | unlink_urbs (dev, &dev->rxq); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 922 | status = usb_autopm_get_interface(dev->intf); |
| 923 | if (status < 0) |
| 924 | goto fail_halt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | status = usb_clear_halt (dev->udev, dev->in); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 926 | usb_autopm_put_interface(dev->intf); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 927 | if (status < 0 && |
| 928 | status != -EPIPE && |
| 929 | status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (netif_msg_rx_err (dev)) |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 931 | fail_halt: |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 932 | netdev_err(dev->net, "can't clear rx halt, status %d\n", |
| 933 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } else { |
| 935 | clear_bit (EVENT_RX_HALT, &dev->flags); |
| 936 | tasklet_schedule (&dev->bh); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /* tasklet could resubmit itself forever if memory is tight */ |
| 941 | if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { |
| 942 | struct urb *urb = NULL; |
| 943 | |
| 944 | if (netif_running (dev->net)) |
| 945 | urb = usb_alloc_urb (0, GFP_KERNEL); |
| 946 | else |
| 947 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| 948 | if (urb != NULL) { |
| 949 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 950 | status = usb_autopm_get_interface(dev->intf); |
| 951 | if (status < 0) |
| 952 | goto fail_lowmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | rx_submit (dev, urb, GFP_KERNEL); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 954 | usb_autopm_put_interface(dev->intf); |
| 955 | fail_lowmem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | tasklet_schedule (&dev->bh); |
| 957 | } |
| 958 | } |
| 959 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 960 | if (test_bit (EVENT_LINK_RESET, &dev->flags)) { |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 961 | struct driver_info *info = dev->driver_info; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 962 | int retval = 0; |
| 963 | |
| 964 | clear_bit (EVENT_LINK_RESET, &dev->flags); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 965 | status = usb_autopm_get_interface(dev->intf); |
| 966 | if (status < 0) |
| 967 | goto skip_reset; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 968 | if(info->link_reset && (retval = info->link_reset(dev)) < 0) { |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 969 | usb_autopm_put_interface(dev->intf); |
| 970 | skip_reset: |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 971 | netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", |
| 972 | retval, |
| 973 | dev->udev->bus->bus_name, |
| 974 | dev->udev->devpath, |
| 975 | info->description); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 976 | } else { |
| 977 | usb_autopm_put_interface(dev->intf); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | if (dev->flags) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 982 | netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /*-------------------------------------------------------------------------*/ |
| 986 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 987 | static void tx_complete (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
| 989 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 990 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 991 | struct usbnet *dev = entry->dev; |
| 992 | |
| 993 | if (urb->status == 0) { |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 994 | dev->net->stats.tx_packets++; |
| 995 | dev->net->stats.tx_bytes += entry->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | } else { |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 997 | dev->net->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | switch (urb->status) { |
| 1000 | case -EPIPE: |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1001 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | |
| 1004 | /* software-driven interface shutdown */ |
| 1005 | case -ECONNRESET: // async unlink |
| 1006 | case -ESHUTDOWN: // hardware gone |
| 1007 | break; |
| 1008 | |
| 1009 | // like rx, tx gets controller i/o faults during khubd delays |
| 1010 | // and so it uses the same throttling mechanism. |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 1011 | case -EPROTO: |
| 1012 | case -ETIME: |
| 1013 | case -EILSEQ: |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1014 | usb_mark_last_busy(dev->udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (!timer_pending (&dev->delay)) { |
| 1016 | mod_timer (&dev->delay, |
| 1017 | jiffies + THROTTLE_JIFFIES); |
| 1018 | if (netif_msg_link (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1019 | netdev_dbg(dev->net, "tx throttle %d\n", |
| 1020 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
| 1022 | netif_stop_queue (dev->net); |
| 1023 | break; |
| 1024 | default: |
| 1025 | if (netif_msg_tx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1026 | netdev_dbg(dev->net, "tx err %d\n", |
| 1027 | entry->urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | break; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1032 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | urb->dev = NULL; |
| 1034 | entry->state = tx_done; |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1035 | defer_bh(dev, skb, &dev->txq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /*-------------------------------------------------------------------------*/ |
| 1039 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1040 | void usbnet_tx_timeout (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
| 1042 | struct usbnet *dev = netdev_priv(net); |
| 1043 | |
| 1044 | unlink_urbs (dev, &dev->txq); |
| 1045 | tasklet_schedule (&dev->bh); |
| 1046 | |
| 1047 | // FIXME: device recovery -- reset? |
| 1048 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1049 | EXPORT_SYMBOL_GPL(usbnet_tx_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | /*-------------------------------------------------------------------------*/ |
| 1052 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1053 | netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, |
| 1054 | struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
| 1056 | struct usbnet *dev = netdev_priv(net); |
| 1057 | int length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct urb *urb = NULL; |
| 1059 | struct skb_data *entry; |
| 1060 | struct driver_info *info = dev->driver_info; |
| 1061 | unsigned long flags; |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1062 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | // some devices want funky USB-level framing, for |
| 1065 | // win32 driver (usually) and/or hardware quirks |
| 1066 | if (info->tx_fixup) { |
| 1067 | skb = info->tx_fixup (dev, skb, GFP_ATOMIC); |
| 1068 | if (!skb) { |
| 1069 | if (netif_msg_tx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1070 | netdev_dbg(dev->net, "can't tx_fixup skb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | goto drop; |
| 1072 | } |
| 1073 | } |
| 1074 | length = skb->len; |
| 1075 | |
| 1076 | if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { |
| 1077 | if (netif_msg_tx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1078 | netdev_dbg(dev->net, "no urb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | goto drop; |
| 1080 | } |
| 1081 | |
| 1082 | entry = (struct skb_data *) skb->cb; |
| 1083 | entry->urb = urb; |
| 1084 | entry->dev = dev; |
| 1085 | entry->state = tx_start; |
| 1086 | entry->length = length; |
| 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | usb_fill_bulk_urb (urb, dev->udev, dev->out, |
| 1089 | skb->data, skb->len, tx_complete, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
| 1091 | /* don't assume the hardware handles USB_ZERO_PACKET |
| 1092 | * NOTE: strictly conforming cdc-ether devices should expect |
| 1093 | * the ZLP here, but ignore the one-byte packet. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | */ |
Steve Glendinning | ec47562 | 2009-09-22 04:00:27 +0000 | [diff] [blame] | 1095 | if (!(info->flags & FLAG_SEND_ZLP) && (length % dev->maxpacket) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | urb->transfer_buffer_length++; |
Peter Korsgaard | 3e323f3 | 2007-06-27 08:48:15 +0200 | [diff] [blame] | 1097 | if (skb_tailroom(skb)) { |
| 1098 | skb->data[skb->len] = 0; |
| 1099 | __skb_put(skb, 1); |
| 1100 | } |
| 1101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1103 | spin_lock_irqsave(&dev->txq.lock, flags); |
| 1104 | retval = usb_autopm_get_interface_async(dev->intf); |
| 1105 | if (retval < 0) { |
| 1106 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
| 1107 | goto drop; |
| 1108 | } |
| 1109 | |
| 1110 | #ifdef CONFIG_PM |
| 1111 | /* if this triggers the device is still a sleep */ |
| 1112 | if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { |
| 1113 | /* transmission will be done in resume */ |
| 1114 | usb_anchor_urb(urb, &dev->deferred); |
| 1115 | /* no use to process more packets */ |
| 1116 | netif_stop_queue(net); |
| 1117 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1118 | netdev_dbg(dev->net, "Delaying transmission for resumption\n"); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1119 | goto deferred; |
| 1120 | } |
| 1121 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { |
| 1124 | case -EPIPE: |
| 1125 | netif_stop_queue (net); |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1126 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1127 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | break; |
| 1129 | default: |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1130 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | if (netif_msg_tx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1132 | netdev_dbg(dev->net, "tx: submit urb err %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | break; |
| 1134 | case 0: |
| 1135 | net->trans_start = jiffies; |
| 1136 | __skb_queue_tail (&dev->txq, skb); |
| 1137 | if (dev->txq.qlen >= TX_QLEN (dev)) |
| 1138 | netif_stop_queue (net); |
| 1139 | } |
| 1140 | spin_unlock_irqrestore (&dev->txq.lock, flags); |
| 1141 | |
| 1142 | if (retval) { |
| 1143 | if (netif_msg_tx_err (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1144 | netdev_dbg(dev->net, "drop, code %d\n", retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | drop: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 1146 | dev->net->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | if (skb) |
| 1148 | dev_kfree_skb_any (skb); |
| 1149 | usb_free_urb (urb); |
| 1150 | } else if (netif_msg_tx_queued (dev)) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1151 | netdev_dbg(dev->net, "> tx, len %d, type 0x%x\n", |
| 1152 | length, skb->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1154 | #ifdef CONFIG_PM |
| 1155 | deferred: |
| 1156 | #endif |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1157 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1159 | EXPORT_SYMBOL_GPL(usbnet_start_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
| 1161 | /*-------------------------------------------------------------------------*/ |
| 1162 | |
| 1163 | // tasklet (work deferred from completions, in_irq) or timer |
| 1164 | |
| 1165 | static void usbnet_bh (unsigned long param) |
| 1166 | { |
| 1167 | struct usbnet *dev = (struct usbnet *) param; |
| 1168 | struct sk_buff *skb; |
| 1169 | struct skb_data *entry; |
| 1170 | |
| 1171 | while ((skb = skb_dequeue (&dev->done))) { |
| 1172 | entry = (struct skb_data *) skb->cb; |
| 1173 | switch (entry->state) { |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1174 | case rx_done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | entry->state = rx_cleanup; |
| 1176 | rx_process (dev, skb); |
| 1177 | continue; |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1178 | case tx_done: |
| 1179 | case rx_cleanup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | usb_free_urb (entry->urb); |
| 1181 | dev_kfree_skb (skb); |
| 1182 | continue; |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1183 | default: |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1184 | netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | // waiting for all pending urbs to complete? |
| 1189 | if (dev->wait) { |
| 1190 | if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { |
| 1191 | wake_up (dev->wait); |
| 1192 | } |
| 1193 | |
| 1194 | // or are we maybe short a few urbs? |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1195 | } else if (netif_running (dev->net) && |
| 1196 | netif_device_present (dev->net) && |
| 1197 | !timer_pending (&dev->delay) && |
| 1198 | !test_bit (EVENT_RX_HALT, &dev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | int temp = dev->rxq.qlen; |
| 1200 | int qlen = RX_QLEN (dev); |
| 1201 | |
| 1202 | if (temp < qlen) { |
| 1203 | struct urb *urb; |
| 1204 | int i; |
| 1205 | |
| 1206 | // don't refill the queue all at once |
| 1207 | for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { |
| 1208 | urb = usb_alloc_urb (0, GFP_ATOMIC); |
| 1209 | if (urb != NULL) |
| 1210 | rx_submit (dev, urb, GFP_ATOMIC); |
| 1211 | } |
| 1212 | if (temp != dev->rxq.qlen && netif_msg_link (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1213 | netdev_dbg(dev->net, "rxqlen %d --> %d\n", |
| 1214 | temp, dev->rxq.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | if (dev->rxq.qlen < qlen) |
| 1216 | tasklet_schedule (&dev->bh); |
| 1217 | } |
| 1218 | if (dev->txq.qlen < TX_QLEN (dev)) |
| 1219 | netif_wake_queue (dev->net); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | /*------------------------------------------------------------------------- |
| 1225 | * |
| 1226 | * USB Device Driver support |
| 1227 | * |
| 1228 | *-------------------------------------------------------------------------*/ |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | // precondition: never called in_interrupt |
| 1231 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1232 | void usbnet_disconnect (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | { |
| 1234 | struct usbnet *dev; |
| 1235 | struct usb_device *xdev; |
| 1236 | struct net_device *net; |
| 1237 | |
| 1238 | dev = usb_get_intfdata(intf); |
| 1239 | usb_set_intfdata(intf, NULL); |
| 1240 | if (!dev) |
| 1241 | return; |
| 1242 | |
| 1243 | xdev = interface_to_usbdev (intf); |
| 1244 | |
| 1245 | if (netif_msg_probe (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1246 | netdev_info(dev->net, "unregister '%s' usb-%s-%s, %s\n", |
| 1247 | intf->dev.driver->name, |
| 1248 | xdev->bus->bus_name, xdev->devpath, |
| 1249 | dev->driver_info->description); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | net = dev->net; |
| 1252 | unregister_netdev (net); |
| 1253 | |
| 1254 | /* we don't hold rtnl here ... */ |
| 1255 | flush_scheduled_work (); |
| 1256 | |
| 1257 | if (dev->driver_info->unbind) |
| 1258 | dev->driver_info->unbind (dev, intf); |
| 1259 | |
| 1260 | free_netdev(net); |
| 1261 | usb_put_dev (xdev); |
| 1262 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1263 | EXPORT_SYMBOL_GPL(usbnet_disconnect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1265 | static const struct net_device_ops usbnet_netdev_ops = { |
| 1266 | .ndo_open = usbnet_open, |
| 1267 | .ndo_stop = usbnet_stop, |
| 1268 | .ndo_start_xmit = usbnet_start_xmit, |
| 1269 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 1270 | .ndo_change_mtu = usbnet_change_mtu, |
| 1271 | .ndo_set_mac_address = eth_mac_addr, |
| 1272 | .ndo_validate_addr = eth_validate_addr, |
| 1273 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
| 1275 | /*-------------------------------------------------------------------------*/ |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | // precondition: never called in_interrupt |
| 1278 | |
Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1279 | static struct device_type wlan_type = { |
| 1280 | .name = "wlan", |
| 1281 | }; |
| 1282 | |
| 1283 | static struct device_type wwan_type = { |
| 1284 | .name = "wwan", |
| 1285 | }; |
| 1286 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1287 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) |
| 1289 | { |
| 1290 | struct usbnet *dev; |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1291 | struct net_device *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | struct usb_host_interface *interface; |
| 1293 | struct driver_info *info; |
| 1294 | struct usb_device *xdev; |
| 1295 | int status; |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1296 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1298 | name = udev->dev.driver->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | info = (struct driver_info *) prod->driver_info; |
| 1300 | if (!info) { |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1301 | dev_dbg (&udev->dev, "blacklisted by %s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | return -ENODEV; |
| 1303 | } |
| 1304 | xdev = interface_to_usbdev (udev); |
| 1305 | interface = udev->cur_altsetting; |
| 1306 | |
| 1307 | usb_get_dev (xdev); |
| 1308 | |
| 1309 | status = -ENOMEM; |
| 1310 | |
| 1311 | // set up our own records |
| 1312 | net = alloc_etherdev(sizeof(*dev)); |
| 1313 | if (!net) { |
| 1314 | dbg ("can't kmalloc dev"); |
| 1315 | goto out; |
| 1316 | } |
| 1317 | |
| 1318 | dev = netdev_priv(net); |
| 1319 | dev->udev = xdev; |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1320 | dev->intf = udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | dev->driver_info = info; |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1322 | dev->driver_name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV |
| 1324 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); |
| 1325 | skb_queue_head_init (&dev->rxq); |
| 1326 | skb_queue_head_init (&dev->txq); |
| 1327 | skb_queue_head_init (&dev->done); |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 1328 | skb_queue_head_init(&dev->rxq_pause); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | dev->bh.func = usbnet_bh; |
| 1330 | dev->bh.data = (unsigned long) dev; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1331 | INIT_WORK (&dev->kevent, kevent); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1332 | init_usb_anchor(&dev->deferred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | dev->delay.function = usbnet_bh; |
| 1334 | dev->delay.data = (unsigned long) dev; |
| 1335 | init_timer (&dev->delay); |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 1336 | mutex_init (&dev->phy_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | dev->net = net; |
| 1339 | strcpy (net->name, "usb%d"); |
| 1340 | memcpy (net->dev_addr, node_id, sizeof node_id); |
| 1341 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1342 | /* rx and tx sides can use different message sizes; |
| 1343 | * bind() should set rx_urb_size in that case. |
| 1344 | */ |
| 1345 | dev->hard_mtu = net->mtu + net->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | #if 0 |
| 1347 | // dma_supported() is deeply broken on almost all architectures |
| 1348 | // possible with some EHCI controllers |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 1349 | if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | net->features |= NETIF_F_HIGHDMA; |
| 1351 | #endif |
| 1352 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1353 | net->netdev_ops = &usbnet_netdev_ops; |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1354 | net->watchdog_timeo = TX_TIMEOUT_JIFFIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | net->ethtool_ops = &usbnet_ethtool_ops; |
| 1356 | |
| 1357 | // allow device-specific bind/init procedures |
| 1358 | // NOTE net->name still not usable ... |
| 1359 | if (info->bind) { |
| 1360 | status = info->bind (dev, udev); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1361 | if (status < 0) |
| 1362 | goto out1; |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | // heuristic: "usb%d" for links we know are two-host, |
| 1365 | // else "eth%d" when there's reasonable doubt. userspace |
| 1366 | // can rename the link if it knows better. |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1367 | if ((dev->driver_info->flags & FLAG_ETHER) != 0 && |
| 1368 | (net->dev_addr [0] & 0x02) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | strcpy (net->name, "eth%d"); |
Jussi Kivilinna | 6e3bbcc | 2008-01-26 00:51:06 +0200 | [diff] [blame] | 1370 | /* WLAN devices should always be named "wlan%d" */ |
| 1371 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1372 | strcpy(net->name, "wlan%d"); |
Marcel Holtmann | e1e499e | 2009-10-02 05:15:25 +0000 | [diff] [blame] | 1373 | /* WWAN devices should always be named "wwan%d" */ |
| 1374 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1375 | strcpy(net->name, "wwan%d"); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1376 | |
| 1377 | /* maybe the remote can't receive an Ethernet MTU */ |
| 1378 | if (net->mtu > (dev->hard_mtu - net->hard_header_len)) |
| 1379 | net->mtu = dev->hard_mtu - net->hard_header_len; |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1380 | } else if (!info->in || !info->out) |
| 1381 | status = usbnet_get_endpoints (dev, udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | else { |
| 1383 | dev->in = usb_rcvbulkpipe (xdev, info->in); |
| 1384 | dev->out = usb_sndbulkpipe (xdev, info->out); |
| 1385 | if (!(info->flags & FLAG_NO_SETINT)) |
| 1386 | status = usb_set_interface (xdev, |
| 1387 | interface->desc.bInterfaceNumber, |
| 1388 | interface->desc.bAlternateSetting); |
| 1389 | else |
| 1390 | status = 0; |
| 1391 | |
| 1392 | } |
Peter Korsgaard | 9514bfe | 2007-07-03 00:46:42 +0200 | [diff] [blame] | 1393 | if (status >= 0 && dev->status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | status = init_status (dev, udev); |
| 1395 | if (status < 0) |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1396 | goto out3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1398 | if (!dev->rx_urb_size) |
| 1399 | dev->rx_urb_size = dev->hard_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | SET_NETDEV_DEV(net, &udev->dev); |
Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1403 | |
| 1404 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1405 | SET_NETDEV_DEVTYPE(net, &wlan_type); |
| 1406 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1407 | SET_NETDEV_DEVTYPE(net, &wwan_type); |
| 1408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | status = register_netdev (net); |
| 1410 | if (status) |
| 1411 | goto out3; |
| 1412 | if (netif_msg_probe (dev)) |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame^] | 1413 | netdev_info(dev->net, "register '%s' at usb-%s-%s, %s, %pM\n", |
| 1414 | udev->dev.driver->name, |
| 1415 | xdev->bus->bus_name, xdev->devpath, |
| 1416 | dev->driver_info->description, |
| 1417 | net->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| 1419 | // ok, it's ready to go. |
| 1420 | usb_set_intfdata (udev, dev); |
| 1421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | netif_device_attach (net); |
| 1423 | |
Ben Hutchings | 37e8273 | 2009-11-04 15:29:52 +0000 | [diff] [blame] | 1424 | if (dev->driver_info->flags & FLAG_LINK_INTR) |
| 1425 | netif_carrier_off(net); |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return 0; |
| 1428 | |
| 1429 | out3: |
| 1430 | if (info->unbind) |
| 1431 | info->unbind (dev, udev); |
| 1432 | out1: |
| 1433 | free_netdev(net); |
| 1434 | out: |
| 1435 | usb_put_dev(xdev); |
| 1436 | return status; |
| 1437 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1438 | EXPORT_SYMBOL_GPL(usbnet_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | /*-------------------------------------------------------------------------*/ |
| 1441 | |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1442 | /* |
| 1443 | * suspend the whole driver as soon as the first interface is suspended |
| 1444 | * resume only when the last interface is resumed |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1445 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1447 | int usbnet_suspend (struct usb_interface *intf, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | { |
| 1449 | struct usbnet *dev = usb_get_intfdata(intf); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1450 | |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1451 | if (!dev->suspend_count++) { |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1452 | spin_lock_irq(&dev->txq.lock); |
| 1453 | /* don't autosuspend while transmitting */ |
| 1454 | if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) { |
| 1455 | spin_unlock_irq(&dev->txq.lock); |
| 1456 | return -EBUSY; |
| 1457 | } else { |
| 1458 | set_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1459 | spin_unlock_irq(&dev->txq.lock); |
| 1460 | } |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1461 | /* |
| 1462 | * accelerate emptying of the rx and queues, to avoid |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1463 | * having everything error out. |
| 1464 | */ |
| 1465 | netif_device_detach (dev->net); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1466 | usbnet_terminate_urbs(dev); |
| 1467 | usb_kill_urb(dev->interrupt); |
| 1468 | |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1469 | /* |
| 1470 | * reattach so runtime management can use and |
| 1471 | * wake the device |
| 1472 | */ |
| 1473 | netif_device_attach (dev->net); |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1477 | EXPORT_SYMBOL_GPL(usbnet_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1479 | int usbnet_resume (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | { |
| 1481 | struct usbnet *dev = usb_get_intfdata(intf); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1482 | struct sk_buff *skb; |
| 1483 | struct urb *res; |
| 1484 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1486 | if (!--dev->suspend_count) { |
| 1487 | spin_lock_irq(&dev->txq.lock); |
| 1488 | while ((res = usb_get_from_anchor(&dev->deferred))) { |
| 1489 | |
| 1490 | printk(KERN_INFO"%s has delayed data\n", __func__); |
| 1491 | skb = (struct sk_buff *)res->context; |
| 1492 | retval = usb_submit_urb(res, GFP_ATOMIC); |
| 1493 | if (retval < 0) { |
| 1494 | dev_kfree_skb_any(skb); |
| 1495 | usb_free_urb(res); |
| 1496 | usb_autopm_put_interface_async(dev->intf); |
| 1497 | } else { |
| 1498 | dev->net->trans_start = jiffies; |
| 1499 | __skb_queue_tail(&dev->txq, skb); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | smp_mb(); |
| 1504 | clear_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1505 | spin_unlock_irq(&dev->txq.lock); |
| 1506 | if (!(dev->txq.qlen >= TX_QLEN(dev))) |
| 1507 | netif_start_queue(dev->net); |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1508 | tasklet_schedule (&dev->bh); |
Oliver Neukum | 69ee472f | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | return 0; |
| 1511 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1512 | EXPORT_SYMBOL_GPL(usbnet_resume); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
| 1515 | /*-------------------------------------------------------------------------*/ |
| 1516 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1517 | static int __init usbnet_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | { |
David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 1519 | /* compiler should optimize this out */ |
Alexey Dobriyan | f8ac232 | 2006-10-08 16:02:00 +0400 | [diff] [blame] | 1520 | BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | < sizeof (struct skb_data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
| 1523 | random_ether_addr(node_id); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1524 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1526 | module_init(usbnet_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1528 | static void __exit usbnet_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1531 | module_exit(usbnet_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1533 | MODULE_AUTHOR("David Brownell"); |
David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 1534 | MODULE_DESCRIPTION("USB network driver framework"); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1535 | MODULE_LICENSE("GPL"); |