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