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)) |
Al Viro | 5330e92 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 246 | devdbg (dev, "< rx, len %zu, type 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | skb->len + sizeof (struct ethhdr), skb->protocol); |
| 248 | memset (skb->cb, 0, sizeof (struct skb_data)); |
| 249 | status = netif_rx (skb); |
| 250 | if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev)) |
| 251 | devdbg (dev, "netif_rx status %d", status); |
| 252 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 253 | EXPORT_SYMBOL_GPL(usbnet_skb_return); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /*------------------------------------------------------------------------- |
| 257 | * |
| 258 | * Network Device Driver (peer link to "Host Device", from USB host) |
| 259 | * |
| 260 | *-------------------------------------------------------------------------*/ |
| 261 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 262 | int usbnet_change_mtu (struct net_device *net, int new_mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
| 264 | struct usbnet *dev = netdev_priv(net); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 265 | int ll_mtu = new_mtu + net->hard_header_len; |
Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 266 | int old_hard_mtu = dev->hard_mtu; |
| 267 | int old_rx_urb_size = dev->rx_urb_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 269 | if (new_mtu <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | // no second zero-length packet read wanted after mtu-sized packets |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 272 | if ((ll_mtu % dev->maxpacket) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return -EDOM; |
| 274 | net->mtu = new_mtu; |
Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 275 | |
| 276 | dev->hard_mtu = net->mtu + net->hard_header_len; |
| 277 | if (dev->rx_urb_size == old_hard_mtu) { |
| 278 | dev->rx_urb_size = dev->hard_mtu; |
| 279 | if (dev->rx_urb_size > old_rx_urb_size) |
| 280 | usbnet_unlink_rx_urbs(dev); |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(usbnet_change_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | /*-------------------------------------------------------------------------*/ |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from |
| 290 | * completion callbacks. 2.5 should have fixed those bugs... |
| 291 | */ |
| 292 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 293 | static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | unsigned long flags; |
| 296 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 297 | spin_lock_irqsave(&list->lock, flags); |
| 298 | __skb_unlink(skb, list); |
| 299 | spin_unlock(&list->lock); |
| 300 | spin_lock(&dev->done.lock); |
| 301 | __skb_queue_tail(&dev->done, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (dev->done.qlen == 1) |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 303 | tasklet_schedule(&dev->bh); |
| 304 | spin_unlock_irqrestore(&dev->done.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* some work can't be done in tasklets, so we use keventd |
| 308 | * |
| 309 | * NOTE: annoying asymmetry: if it's active, schedule_work() fails, |
| 310 | * but tasklet_schedule() doesn't. hope the failure is rare. |
| 311 | */ |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 312 | void usbnet_defer_kevent (struct usbnet *dev, int work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | set_bit (work, &dev->flags); |
| 315 | if (!schedule_work (&dev->kevent)) |
| 316 | deverr (dev, "kevent %d may have been dropped", work); |
| 317 | else |
| 318 | devdbg (dev, "kevent %d scheduled", work); |
| 319 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 320 | EXPORT_SYMBOL_GPL(usbnet_defer_kevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | /*-------------------------------------------------------------------------*/ |
| 323 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 324 | static void rx_complete (struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 326 | static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
| 328 | struct sk_buff *skb; |
| 329 | struct skb_data *entry; |
| 330 | int retval = 0; |
| 331 | unsigned long lockflags; |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 332 | size_t size = dev->rx_urb_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) { |
| 335 | if (netif_msg_rx_err (dev)) |
| 336 | devdbg (dev, "no rx skb"); |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 337 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | usb_free_urb (urb); |
| 339 | return; |
| 340 | } |
| 341 | skb_reserve (skb, NET_IP_ALIGN); |
| 342 | |
| 343 | entry = (struct skb_data *) skb->cb; |
| 344 | entry->urb = urb; |
| 345 | entry->dev = dev; |
| 346 | entry->state = rx_start; |
| 347 | entry->length = 0; |
| 348 | |
| 349 | usb_fill_bulk_urb (urb, dev->udev, dev->in, |
| 350 | skb->data, size, rx_complete, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | spin_lock_irqsave (&dev->rxq.lock, lockflags); |
| 353 | |
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 | 69ee472 | 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)) |
| 367 | devdbg (dev, "device gone"); |
| 368 | netif_device_detach (dev->net); |
| 369 | break; |
| 370 | default: |
| 371 | if (netif_msg_rx_err (dev)) |
| 372 | devdbg (dev, "rx submit, %d", retval); |
| 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)) |
| 380 | devdbg (dev, "rx: stopped"); |
| 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)) |
| 404 | devdbg (dev, "drop"); |
| 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)) |
| 432 | devdbg (dev, "rx length %d", skb->len); |
| 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)) |
| 450 | devdbg (dev, "rx shutdown, code %d", urb_status); |
| 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)) |
| 464 | devdbg (dev, "rx throttle %d", urb_status); |
| 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)) |
| 481 | devdbg (dev, "rx status %d", urb_status); |
| 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)) |
| 496 | devdbg (dev, "no read resubmitted"); |
| 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)) |
| 514 | devdbg (dev, "intr shutdown, code %d", status); |
| 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: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | devdbg (dev, "intr status %d", status); |
| 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)) |
| 531 | deverr(dev, "intr resubmit --> %d", status); |
| 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)) |
| 540 | devdbg(dev, "paused rx queue enabled"); |
| 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)) |
| 559 | devdbg(dev, "paused rx queue disabled, %d skbs requeued", num); |
| 560 | } |
| 561 | EXPORT_SYMBOL_GPL(usbnet_resume_rx); |
| 562 | |
| 563 | void usbnet_purge_paused_rxq(struct usbnet *dev) |
| 564 | { |
| 565 | skb_queue_purge(&dev->rxq_pause); |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); |
| 568 | |
| 569 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | // unlink pending rx/tx; completion handlers do all other cleanup |
| 572 | |
| 573 | static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) |
| 574 | { |
| 575 | unsigned long flags; |
| 576 | struct sk_buff *skb, *skbnext; |
| 577 | int count = 0; |
| 578 | |
| 579 | spin_lock_irqsave (&q->lock, flags); |
David S. Miller | 83bfba5 | 2008-09-22 20:18:47 -0700 | [diff] [blame] | 580 | skb_queue_walk_safe(q, skb, skbnext) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | struct skb_data *entry; |
| 582 | struct urb *urb; |
| 583 | int retval; |
| 584 | |
| 585 | entry = (struct skb_data *) skb->cb; |
| 586 | urb = entry->urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | // during some PM-driven resume scenarios, |
| 589 | // these (async) unlinks complete immediately |
| 590 | retval = usb_unlink_urb (urb); |
| 591 | if (retval != -EINPROGRESS && retval != 0) |
| 592 | devdbg (dev, "unlink urb err, %d", retval); |
| 593 | else |
| 594 | count++; |
| 595 | } |
| 596 | spin_unlock_irqrestore (&q->lock, flags); |
| 597 | return count; |
| 598 | } |
| 599 | |
Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 600 | // Flush all pending rx urbs |
| 601 | // minidrivers may need to do this when the MTU changes |
| 602 | |
| 603 | void usbnet_unlink_rx_urbs(struct usbnet *dev) |
| 604 | { |
| 605 | if (netif_running(dev->net)) { |
| 606 | (void) unlink_urbs (dev, &dev->rxq); |
| 607 | tasklet_schedule(&dev->bh); |
| 608 | } |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | /*-------------------------------------------------------------------------*/ |
| 613 | |
| 614 | // precondition: never called in_interrupt |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 615 | static void usbnet_terminate_urbs(struct usbnet *dev) |
| 616 | { |
| 617 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup); |
| 618 | DECLARE_WAITQUEUE(wait, current); |
| 619 | int temp; |
| 620 | |
| 621 | /* ensure there are no more active urbs */ |
| 622 | add_wait_queue(&unlink_wakeup, &wait); |
| 623 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 624 | dev->wait = &unlink_wakeup; |
| 625 | temp = unlink_urbs(dev, &dev->txq) + |
| 626 | unlink_urbs(dev, &dev->rxq); |
| 627 | |
| 628 | /* maybe wait for deletions to finish. */ |
| 629 | while (!skb_queue_empty(&dev->rxq) |
| 630 | && !skb_queue_empty(&dev->txq) |
| 631 | && !skb_queue_empty(&dev->done)) { |
| 632 | schedule_timeout(UNLINK_TIMEOUT_MS); |
| 633 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 634 | if (netif_msg_ifdown(dev)) |
| 635 | devdbg(dev, "waited for %d urb completions", |
| 636 | temp); |
| 637 | } |
| 638 | set_current_state(TASK_RUNNING); |
| 639 | dev->wait = NULL; |
| 640 | remove_wait_queue(&unlink_wakeup, &wait); |
| 641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 643 | int usbnet_stop (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
| 645 | struct usbnet *dev = netdev_priv(net); |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 646 | struct driver_info *info = dev->driver_info; |
Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 647 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | netif_stop_queue (net); |
| 650 | |
| 651 | if (netif_msg_ifdown (dev)) |
| 652 | devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld", |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 653 | net->stats.rx_packets, net->stats.tx_packets, |
| 654 | net->stats.rx_errors, net->stats.tx_errors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | ); |
| 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)) |
| 662 | devinfo(dev, |
| 663 | "stop fail (%d) usbnet usb-%s-%s, %s", |
| 664 | retval, |
| 665 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 666 | info->description); |
| 667 | } |
| 668 | |
Oliver Neukum | 69ee472 | 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 | 69ee472 | 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)) |
| 706 | devinfo (dev, |
| 707 | "resumption fail (%d) usbnet usb-%s-%s, %s", |
| 708 | retval, |
| 709 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 710 | info->description); |
| 711 | goto done_nopm; |
| 712 | } |
| 713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | // put into "known safe" state |
| 715 | if (info->reset && (retval = info->reset (dev)) < 0) { |
| 716 | if (netif_msg_ifup (dev)) |
| 717 | devinfo (dev, |
| 718 | "open reset fail (%d) usbnet usb-%s-%s, %s", |
| 719 | retval, |
| 720 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 721 | info->description); |
| 722 | goto done; |
| 723 | } |
| 724 | |
| 725 | // insist peer be connected |
| 726 | if (info->check_connect && (retval = info->check_connect (dev)) < 0) { |
| 727 | if (netif_msg_ifup (dev)) |
| 728 | devdbg (dev, "can't open; %d", retval); |
| 729 | goto done; |
| 730 | } |
| 731 | |
| 732 | /* start any status interrupt transfer */ |
| 733 | if (dev->interrupt) { |
| 734 | retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); |
| 735 | if (retval < 0) { |
| 736 | if (netif_msg_ifup (dev)) |
| 737 | deverr (dev, "intr submit %d", retval); |
| 738 | goto done; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | netif_start_queue (net); |
| 743 | if (netif_msg_ifup (dev)) { |
| 744 | char *framing; |
| 745 | |
| 746 | if (dev->driver_info->flags & FLAG_FRAMING_NC) |
| 747 | framing = "NetChip"; |
| 748 | else if (dev->driver_info->flags & FLAG_FRAMING_GL) |
| 749 | framing = "GeneSys"; |
| 750 | else if (dev->driver_info->flags & FLAG_FRAMING_Z) |
| 751 | framing = "Zaurus"; |
| 752 | else if (dev->driver_info->flags & FLAG_FRAMING_RN) |
| 753 | framing = "RNDIS"; |
| 754 | else if (dev->driver_info->flags & FLAG_FRAMING_AX) |
| 755 | framing = "ASIX"; |
| 756 | else |
| 757 | framing = "simple"; |
| 758 | |
| 759 | devinfo (dev, "open: enable queueing " |
| 760 | "(rx %d, tx %d) mtu %d %s framing", |
Randy Dunlap | 25d94e6 | 2006-08-07 15:56:40 -0700 | [diff] [blame] | 761 | (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | framing); |
| 763 | } |
| 764 | |
| 765 | // delay posting reads until we're fully open |
| 766 | tasklet_schedule (&dev->bh); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 767 | if (info->manage_power) { |
| 768 | retval = info->manage_power(dev, 1); |
| 769 | if (retval < 0) |
| 770 | goto done; |
| 771 | usb_autopm_put_interface(dev->intf); |
| 772 | } |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 773 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | done: |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 775 | usb_autopm_put_interface(dev->intf); |
| 776 | done_nopm: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | return retval; |
| 778 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 779 | EXPORT_SYMBOL_GPL(usbnet_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | /*-------------------------------------------------------------------------*/ |
| 782 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 783 | /* ethtool methods; minidrivers may need to add some more, but |
| 784 | * they'll probably want to use this base set. |
| 785 | */ |
| 786 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 787 | int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 788 | { |
| 789 | struct usbnet *dev = netdev_priv(net); |
| 790 | |
| 791 | if (!dev->mii.mdio_read) |
| 792 | return -EOPNOTSUPP; |
| 793 | |
| 794 | return mii_ethtool_gset(&dev->mii, cmd); |
| 795 | } |
| 796 | EXPORT_SYMBOL_GPL(usbnet_get_settings); |
| 797 | |
| 798 | int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 799 | { |
| 800 | struct usbnet *dev = netdev_priv(net); |
| 801 | int retval; |
| 802 | |
| 803 | if (!dev->mii.mdio_write) |
| 804 | return -EOPNOTSUPP; |
| 805 | |
| 806 | retval = mii_ethtool_sset(&dev->mii, cmd); |
| 807 | |
| 808 | /* link speed/duplex might have changed */ |
| 809 | if (dev->driver_info->link_reset) |
| 810 | dev->driver_info->link_reset(dev); |
| 811 | |
| 812 | return retval; |
| 813 | |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(usbnet_set_settings); |
| 816 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 817 | u32 usbnet_get_link (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
| 819 | struct usbnet *dev = netdev_priv(net); |
| 820 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 821 | /* If a check_connect is defined, return its result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (dev->driver_info->check_connect) |
| 823 | return dev->driver_info->check_connect (dev) == 0; |
| 824 | |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 825 | /* if the device has mii operations, use those */ |
| 826 | if (dev->mii.mdio_read) |
| 827 | return mii_link_ok(&dev->mii); |
| 828 | |
Bjørn Mork | 05ffb3e | 2009-03-01 20:45:40 -0800 | [diff] [blame] | 829 | /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ |
| 830 | return ethtool_op_get_link(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 832 | EXPORT_SYMBOL_GPL(usbnet_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 834 | int usbnet_nway_reset(struct net_device *net) |
| 835 | { |
| 836 | struct usbnet *dev = netdev_priv(net); |
| 837 | |
| 838 | if (!dev->mii.mdio_write) |
| 839 | return -EOPNOTSUPP; |
| 840 | |
| 841 | return mii_nway_restart(&dev->mii); |
| 842 | } |
| 843 | EXPORT_SYMBOL_GPL(usbnet_nway_reset); |
| 844 | |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 845 | void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) |
| 846 | { |
| 847 | struct usbnet *dev = netdev_priv(net); |
| 848 | |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 849 | strncpy (info->driver, dev->driver_name, sizeof info->driver); |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 850 | strncpy (info->version, DRIVER_VERSION, sizeof info->version); |
| 851 | strncpy (info->fw_version, dev->driver_info->description, |
| 852 | sizeof info->fw_version); |
| 853 | usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); |
| 856 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 857 | u32 usbnet_get_msglevel (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
| 859 | struct usbnet *dev = netdev_priv(net); |
| 860 | |
| 861 | return dev->msg_enable; |
| 862 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 863 | EXPORT_SYMBOL_GPL(usbnet_get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 865 | void usbnet_set_msglevel (struct net_device *net, u32 level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
| 867 | struct usbnet *dev = netdev_priv(net); |
| 868 | |
| 869 | dev->msg_enable = level; |
| 870 | } |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 871 | EXPORT_SYMBOL_GPL(usbnet_set_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 873 | /* drivers may override default ethtool_ops in their bind() routine */ |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 874 | static const struct ethtool_ops usbnet_ethtool_ops = { |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 875 | .get_settings = usbnet_get_settings, |
| 876 | .set_settings = usbnet_set_settings, |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 877 | .get_link = usbnet_get_link, |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 878 | .nway_reset = usbnet_nway_reset, |
David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 879 | .get_drvinfo = usbnet_get_drvinfo, |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 880 | .get_msglevel = usbnet_get_msglevel, |
| 881 | .set_msglevel = usbnet_set_msglevel, |
| 882 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | /*-------------------------------------------------------------------------*/ |
| 885 | |
| 886 | /* work that cannot be done in interrupt context uses keventd. |
| 887 | * |
| 888 | * NOTE: with 2.5 we could do more of this using completion callbacks, |
| 889 | * especially now that control transfers can be queued. |
| 890 | */ |
| 891 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 892 | kevent (struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 894 | struct usbnet *dev = |
| 895 | container_of(work, struct usbnet, kevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | int status; |
| 897 | |
| 898 | /* usb_clear_halt() needs a thread context */ |
| 899 | if (test_bit (EVENT_TX_HALT, &dev->flags)) { |
| 900 | unlink_urbs (dev, &dev->txq); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 901 | status = usb_autopm_get_interface(dev->intf); |
| 902 | if (status < 0) |
| 903 | goto fail_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | status = usb_clear_halt (dev->udev, dev->out); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 905 | usb_autopm_put_interface(dev->intf); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 906 | if (status < 0 && |
| 907 | status != -EPIPE && |
| 908 | status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | if (netif_msg_tx_err (dev)) |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 910 | fail_pipe: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | deverr (dev, "can't clear tx halt, status %d", |
| 912 | status); |
| 913 | } else { |
| 914 | clear_bit (EVENT_TX_HALT, &dev->flags); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 915 | if (status != -ESHUTDOWN) |
| 916 | netif_wake_queue (dev->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | if (test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 920 | unlink_urbs (dev, &dev->rxq); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 921 | status = usb_autopm_get_interface(dev->intf); |
| 922 | if (status < 0) |
| 923 | goto fail_halt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | status = usb_clear_halt (dev->udev, dev->in); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 925 | usb_autopm_put_interface(dev->intf); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 926 | if (status < 0 && |
| 927 | status != -EPIPE && |
| 928 | status != -ESHUTDOWN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | if (netif_msg_rx_err (dev)) |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 930 | fail_halt: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | deverr (dev, "can't clear rx halt, status %d", |
| 932 | status); |
| 933 | } else { |
| 934 | clear_bit (EVENT_RX_HALT, &dev->flags); |
| 935 | tasklet_schedule (&dev->bh); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* tasklet could resubmit itself forever if memory is tight */ |
| 940 | if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { |
| 941 | struct urb *urb = NULL; |
| 942 | |
| 943 | if (netif_running (dev->net)) |
| 944 | urb = usb_alloc_urb (0, GFP_KERNEL); |
| 945 | else |
| 946 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| 947 | if (urb != NULL) { |
| 948 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 949 | status = usb_autopm_get_interface(dev->intf); |
| 950 | if (status < 0) |
| 951 | goto fail_lowmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | rx_submit (dev, urb, GFP_KERNEL); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 953 | usb_autopm_put_interface(dev->intf); |
| 954 | fail_lowmem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | tasklet_schedule (&dev->bh); |
| 956 | } |
| 957 | } |
| 958 | |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 959 | if (test_bit (EVENT_LINK_RESET, &dev->flags)) { |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 960 | struct driver_info *info = dev->driver_info; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 961 | int retval = 0; |
| 962 | |
| 963 | clear_bit (EVENT_LINK_RESET, &dev->flags); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 964 | status = usb_autopm_get_interface(dev->intf); |
| 965 | if (status < 0) |
| 966 | goto skip_reset; |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 967 | if(info->link_reset && (retval = info->link_reset(dev)) < 0) { |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 968 | usb_autopm_put_interface(dev->intf); |
| 969 | skip_reset: |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 970 | devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s", |
| 971 | retval, |
| 972 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 973 | info->description); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 974 | } else { |
| 975 | usb_autopm_put_interface(dev->intf); |
David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (dev->flags) |
| 980 | devdbg (dev, "kevent done, flags = 0x%lx", |
| 981 | dev->flags); |
| 982 | } |
| 983 | |
| 984 | /*-------------------------------------------------------------------------*/ |
| 985 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 986 | static void tx_complete (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
| 988 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 989 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 990 | struct usbnet *dev = entry->dev; |
| 991 | |
| 992 | if (urb->status == 0) { |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 993 | dev->net->stats.tx_packets++; |
| 994 | dev->net->stats.tx_bytes += entry->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } else { |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 996 | dev->net->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
| 998 | switch (urb->status) { |
| 999 | case -EPIPE: |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1000 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | break; |
| 1002 | |
| 1003 | /* software-driven interface shutdown */ |
| 1004 | case -ECONNRESET: // async unlink |
| 1005 | case -ESHUTDOWN: // hardware gone |
| 1006 | break; |
| 1007 | |
| 1008 | // like rx, tx gets controller i/o faults during khubd delays |
| 1009 | // and so it uses the same throttling mechanism. |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 1010 | case -EPROTO: |
| 1011 | case -ETIME: |
| 1012 | case -EILSEQ: |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1013 | usb_mark_last_busy(dev->udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | if (!timer_pending (&dev->delay)) { |
| 1015 | mod_timer (&dev->delay, |
| 1016 | jiffies + THROTTLE_JIFFIES); |
| 1017 | if (netif_msg_link (dev)) |
| 1018 | devdbg (dev, "tx throttle %d", |
| 1019 | urb->status); |
| 1020 | } |
| 1021 | netif_stop_queue (dev->net); |
| 1022 | break; |
| 1023 | default: |
| 1024 | if (netif_msg_tx_err (dev)) |
| 1025 | devdbg (dev, "tx err %d", entry->urb->status); |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1030 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | urb->dev = NULL; |
| 1032 | entry->state = tx_done; |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1033 | defer_bh(dev, skb, &dev->txq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /*-------------------------------------------------------------------------*/ |
| 1037 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1038 | void usbnet_tx_timeout (struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
| 1040 | struct usbnet *dev = netdev_priv(net); |
| 1041 | |
| 1042 | unlink_urbs (dev, &dev->txq); |
| 1043 | tasklet_schedule (&dev->bh); |
| 1044 | |
| 1045 | // FIXME: device recovery -- reset? |
| 1046 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1047 | EXPORT_SYMBOL_GPL(usbnet_tx_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | /*-------------------------------------------------------------------------*/ |
| 1050 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1051 | netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, |
| 1052 | struct net_device *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
| 1054 | struct usbnet *dev = netdev_priv(net); |
| 1055 | int length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | struct urb *urb = NULL; |
| 1057 | struct skb_data *entry; |
| 1058 | struct driver_info *info = dev->driver_info; |
| 1059 | unsigned long flags; |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1060 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | // some devices want funky USB-level framing, for |
| 1063 | // win32 driver (usually) and/or hardware quirks |
| 1064 | if (info->tx_fixup) { |
| 1065 | skb = info->tx_fixup (dev, skb, GFP_ATOMIC); |
| 1066 | if (!skb) { |
| 1067 | if (netif_msg_tx_err (dev)) |
| 1068 | devdbg (dev, "can't tx_fixup skb"); |
| 1069 | goto drop; |
| 1070 | } |
| 1071 | } |
| 1072 | length = skb->len; |
| 1073 | |
| 1074 | if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { |
| 1075 | if (netif_msg_tx_err (dev)) |
| 1076 | devdbg (dev, "no urb"); |
| 1077 | goto drop; |
| 1078 | } |
| 1079 | |
| 1080 | entry = (struct skb_data *) skb->cb; |
| 1081 | entry->urb = urb; |
| 1082 | entry->dev = dev; |
| 1083 | entry->state = tx_start; |
| 1084 | entry->length = length; |
| 1085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | usb_fill_bulk_urb (urb, dev->udev, dev->out, |
| 1087 | skb->data, skb->len, tx_complete, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | /* don't assume the hardware handles USB_ZERO_PACKET |
| 1090 | * NOTE: strictly conforming cdc-ether devices should expect |
| 1091 | * the ZLP here, but ignore the one-byte packet. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | */ |
Steve Glendinning | ec47562 | 2009-09-22 04:00:27 +0000 | [diff] [blame] | 1093 | if (!(info->flags & FLAG_SEND_ZLP) && (length % dev->maxpacket) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | urb->transfer_buffer_length++; |
Peter Korsgaard | 3e323f3 | 2007-06-27 08:48:15 +0200 | [diff] [blame] | 1095 | if (skb_tailroom(skb)) { |
| 1096 | skb->data[skb->len] = 0; |
| 1097 | __skb_put(skb, 1); |
| 1098 | } |
| 1099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1101 | spin_lock_irqsave(&dev->txq.lock, flags); |
| 1102 | retval = usb_autopm_get_interface_async(dev->intf); |
| 1103 | if (retval < 0) { |
| 1104 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
| 1105 | goto drop; |
| 1106 | } |
| 1107 | |
| 1108 | #ifdef CONFIG_PM |
| 1109 | /* if this triggers the device is still a sleep */ |
| 1110 | if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { |
| 1111 | /* transmission will be done in resume */ |
| 1112 | usb_anchor_urb(urb, &dev->deferred); |
| 1113 | /* no use to process more packets */ |
| 1114 | netif_stop_queue(net); |
| 1115 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
| 1116 | devdbg(dev, "Delaying transmission for resumption"); |
| 1117 | goto deferred; |
| 1118 | } |
| 1119 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { |
| 1122 | case -EPIPE: |
| 1123 | netif_stop_queue (net); |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1124 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1125 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | break; |
| 1127 | default: |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1128 | usb_autopm_put_interface_async(dev->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (netif_msg_tx_err (dev)) |
| 1130 | devdbg (dev, "tx: submit urb err %d", retval); |
| 1131 | break; |
| 1132 | case 0: |
| 1133 | net->trans_start = jiffies; |
| 1134 | __skb_queue_tail (&dev->txq, skb); |
| 1135 | if (dev->txq.qlen >= TX_QLEN (dev)) |
| 1136 | netif_stop_queue (net); |
| 1137 | } |
| 1138 | spin_unlock_irqrestore (&dev->txq.lock, flags); |
| 1139 | |
| 1140 | if (retval) { |
| 1141 | if (netif_msg_tx_err (dev)) |
| 1142 | devdbg (dev, "drop, code %d", retval); |
| 1143 | drop: |
Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 1144 | dev->net->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | if (skb) |
| 1146 | dev_kfree_skb_any (skb); |
| 1147 | usb_free_urb (urb); |
| 1148 | } else if (netif_msg_tx_queued (dev)) { |
| 1149 | devdbg (dev, "> tx, len %d, type 0x%x", |
| 1150 | length, skb->protocol); |
| 1151 | } |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1152 | #ifdef CONFIG_PM |
| 1153 | deferred: |
| 1154 | #endif |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1155 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1157 | EXPORT_SYMBOL_GPL(usbnet_start_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
| 1159 | /*-------------------------------------------------------------------------*/ |
| 1160 | |
| 1161 | // tasklet (work deferred from completions, in_irq) or timer |
| 1162 | |
| 1163 | static void usbnet_bh (unsigned long param) |
| 1164 | { |
| 1165 | struct usbnet *dev = (struct usbnet *) param; |
| 1166 | struct sk_buff *skb; |
| 1167 | struct skb_data *entry; |
| 1168 | |
| 1169 | while ((skb = skb_dequeue (&dev->done))) { |
| 1170 | entry = (struct skb_data *) skb->cb; |
| 1171 | switch (entry->state) { |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1172 | case rx_done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | entry->state = rx_cleanup; |
| 1174 | rx_process (dev, skb); |
| 1175 | continue; |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1176 | case tx_done: |
| 1177 | case rx_cleanup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | usb_free_urb (entry->urb); |
| 1179 | dev_kfree_skb (skb); |
| 1180 | continue; |
David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1181 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | devdbg (dev, "bogus skb state %d", entry->state); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | // waiting for all pending urbs to complete? |
| 1187 | if (dev->wait) { |
| 1188 | if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { |
| 1189 | wake_up (dev->wait); |
| 1190 | } |
| 1191 | |
| 1192 | // or are we maybe short a few urbs? |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1193 | } else if (netif_running (dev->net) && |
| 1194 | netif_device_present (dev->net) && |
| 1195 | !timer_pending (&dev->delay) && |
| 1196 | !test_bit (EVENT_RX_HALT, &dev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | int temp = dev->rxq.qlen; |
| 1198 | int qlen = RX_QLEN (dev); |
| 1199 | |
| 1200 | if (temp < qlen) { |
| 1201 | struct urb *urb; |
| 1202 | int i; |
| 1203 | |
| 1204 | // don't refill the queue all at once |
| 1205 | for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { |
| 1206 | urb = usb_alloc_urb (0, GFP_ATOMIC); |
| 1207 | if (urb != NULL) |
| 1208 | rx_submit (dev, urb, GFP_ATOMIC); |
| 1209 | } |
| 1210 | if (temp != dev->rxq.qlen && netif_msg_link (dev)) |
| 1211 | devdbg (dev, "rxqlen %d --> %d", |
| 1212 | temp, dev->rxq.qlen); |
| 1213 | if (dev->rxq.qlen < qlen) |
| 1214 | tasklet_schedule (&dev->bh); |
| 1215 | } |
| 1216 | if (dev->txq.qlen < TX_QLEN (dev)) |
| 1217 | netif_wake_queue (dev->net); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | /*------------------------------------------------------------------------- |
| 1223 | * |
| 1224 | * USB Device Driver support |
| 1225 | * |
| 1226 | *-------------------------------------------------------------------------*/ |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | // precondition: never called in_interrupt |
| 1229 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1230 | void usbnet_disconnect (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { |
| 1232 | struct usbnet *dev; |
| 1233 | struct usb_device *xdev; |
| 1234 | struct net_device *net; |
| 1235 | |
| 1236 | dev = usb_get_intfdata(intf); |
| 1237 | usb_set_intfdata(intf, NULL); |
| 1238 | if (!dev) |
| 1239 | return; |
| 1240 | |
| 1241 | xdev = interface_to_usbdev (intf); |
| 1242 | |
| 1243 | if (netif_msg_probe (dev)) |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1244 | devinfo (dev, "unregister '%s' usb-%s-%s, %s", |
| 1245 | intf->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | xdev->bus->bus_name, xdev->devpath, |
| 1247 | dev->driver_info->description); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | net = dev->net; |
| 1250 | unregister_netdev (net); |
| 1251 | |
| 1252 | /* we don't hold rtnl here ... */ |
| 1253 | flush_scheduled_work (); |
| 1254 | |
| 1255 | if (dev->driver_info->unbind) |
| 1256 | dev->driver_info->unbind (dev, intf); |
| 1257 | |
| 1258 | free_netdev(net); |
| 1259 | usb_put_dev (xdev); |
| 1260 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1261 | EXPORT_SYMBOL_GPL(usbnet_disconnect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1263 | static const struct net_device_ops usbnet_netdev_ops = { |
| 1264 | .ndo_open = usbnet_open, |
| 1265 | .ndo_stop = usbnet_stop, |
| 1266 | .ndo_start_xmit = usbnet_start_xmit, |
| 1267 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 1268 | .ndo_change_mtu = usbnet_change_mtu, |
| 1269 | .ndo_set_mac_address = eth_mac_addr, |
| 1270 | .ndo_validate_addr = eth_validate_addr, |
| 1271 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
| 1273 | /*-------------------------------------------------------------------------*/ |
| 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | // precondition: never called in_interrupt |
| 1276 | |
Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1277 | static struct device_type wlan_type = { |
| 1278 | .name = "wlan", |
| 1279 | }; |
| 1280 | |
| 1281 | static struct device_type wwan_type = { |
| 1282 | .name = "wwan", |
| 1283 | }; |
| 1284 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1285 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) |
| 1287 | { |
| 1288 | struct usbnet *dev; |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1289 | struct net_device *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | struct usb_host_interface *interface; |
| 1291 | struct driver_info *info; |
| 1292 | struct usb_device *xdev; |
| 1293 | int status; |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1294 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1296 | name = udev->dev.driver->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | info = (struct driver_info *) prod->driver_info; |
| 1298 | if (!info) { |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1299 | dev_dbg (&udev->dev, "blacklisted by %s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | return -ENODEV; |
| 1301 | } |
| 1302 | xdev = interface_to_usbdev (udev); |
| 1303 | interface = udev->cur_altsetting; |
| 1304 | |
| 1305 | usb_get_dev (xdev); |
| 1306 | |
| 1307 | status = -ENOMEM; |
| 1308 | |
| 1309 | // set up our own records |
| 1310 | net = alloc_etherdev(sizeof(*dev)); |
| 1311 | if (!net) { |
| 1312 | dbg ("can't kmalloc dev"); |
| 1313 | goto out; |
| 1314 | } |
| 1315 | |
| 1316 | dev = netdev_priv(net); |
| 1317 | dev->udev = xdev; |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1318 | dev->intf = udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | dev->driver_info = info; |
David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1320 | dev->driver_name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV |
| 1322 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); |
| 1323 | skb_queue_head_init (&dev->rxq); |
| 1324 | skb_queue_head_init (&dev->txq); |
| 1325 | skb_queue_head_init (&dev->done); |
Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 1326 | skb_queue_head_init(&dev->rxq_pause); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | dev->bh.func = usbnet_bh; |
| 1328 | dev->bh.data = (unsigned long) dev; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1329 | INIT_WORK (&dev->kevent, kevent); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1330 | init_usb_anchor(&dev->deferred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | dev->delay.function = usbnet_bh; |
| 1332 | dev->delay.data = (unsigned long) dev; |
| 1333 | init_timer (&dev->delay); |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 1334 | mutex_init (&dev->phy_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | dev->net = net; |
| 1337 | strcpy (net->name, "usb%d"); |
| 1338 | memcpy (net->dev_addr, node_id, sizeof node_id); |
| 1339 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1340 | /* rx and tx sides can use different message sizes; |
| 1341 | * bind() should set rx_urb_size in that case. |
| 1342 | */ |
| 1343 | dev->hard_mtu = net->mtu + net->hard_header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | #if 0 |
| 1345 | // dma_supported() is deeply broken on almost all architectures |
| 1346 | // possible with some EHCI controllers |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 1347 | if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | net->features |= NETIF_F_HIGHDMA; |
| 1349 | #endif |
| 1350 | |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1351 | net->netdev_ops = &usbnet_netdev_ops; |
Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1352 | net->watchdog_timeo = TX_TIMEOUT_JIFFIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | net->ethtool_ops = &usbnet_ethtool_ops; |
| 1354 | |
| 1355 | // allow device-specific bind/init procedures |
| 1356 | // NOTE net->name still not usable ... |
| 1357 | if (info->bind) { |
| 1358 | status = info->bind (dev, udev); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1359 | if (status < 0) |
| 1360 | goto out1; |
| 1361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | // heuristic: "usb%d" for links we know are two-host, |
| 1363 | // else "eth%d" when there's reasonable doubt. userspace |
| 1364 | // can rename the link if it knows better. |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1365 | if ((dev->driver_info->flags & FLAG_ETHER) != 0 && |
| 1366 | (net->dev_addr [0] & 0x02) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | strcpy (net->name, "eth%d"); |
Jussi Kivilinna | 6e3bbcc | 2008-01-26 00:51:06 +0200 | [diff] [blame] | 1368 | /* WLAN devices should always be named "wlan%d" */ |
| 1369 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1370 | strcpy(net->name, "wlan%d"); |
Marcel Holtmann | e1e499e | 2009-10-02 05:15:25 +0000 | [diff] [blame] | 1371 | /* WWAN devices should always be named "wwan%d" */ |
| 1372 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1373 | strcpy(net->name, "wwan%d"); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1374 | |
| 1375 | /* maybe the remote can't receive an Ethernet MTU */ |
| 1376 | if (net->mtu > (dev->hard_mtu - net->hard_header_len)) |
| 1377 | net->mtu = dev->hard_mtu - net->hard_header_len; |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1378 | } else if (!info->in || !info->out) |
| 1379 | status = usbnet_get_endpoints (dev, udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | else { |
| 1381 | dev->in = usb_rcvbulkpipe (xdev, info->in); |
| 1382 | dev->out = usb_sndbulkpipe (xdev, info->out); |
| 1383 | if (!(info->flags & FLAG_NO_SETINT)) |
| 1384 | status = usb_set_interface (xdev, |
| 1385 | interface->desc.bInterfaceNumber, |
| 1386 | interface->desc.bAlternateSetting); |
| 1387 | else |
| 1388 | status = 0; |
| 1389 | |
| 1390 | } |
Peter Korsgaard | 9514bfe | 2007-07-03 00:46:42 +0200 | [diff] [blame] | 1391 | if (status >= 0 && dev->status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | status = init_status (dev, udev); |
| 1393 | if (status < 0) |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1394 | goto out3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1396 | if (!dev->rx_urb_size) |
| 1397 | dev->rx_urb_size = dev->hard_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | SET_NETDEV_DEV(net, &udev->dev); |
Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1401 | |
| 1402 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1403 | SET_NETDEV_DEVTYPE(net, &wlan_type); |
| 1404 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1405 | SET_NETDEV_DEVTYPE(net, &wwan_type); |
| 1406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | status = register_netdev (net); |
| 1408 | if (status) |
| 1409 | goto out3; |
| 1410 | if (netif_msg_probe (dev)) |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 1411 | devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM", |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1412 | udev->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | xdev->bus->bus_name, xdev->devpath, |
| 1414 | dev->driver_info->description, |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 1415 | net->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | // ok, it's ready to go. |
| 1418 | usb_set_intfdata (udev, dev); |
| 1419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | netif_device_attach (net); |
| 1421 | |
Ben Hutchings | 37e8273 | 2009-11-04 15:29:52 +0000 | [diff] [blame] | 1422 | if (dev->driver_info->flags & FLAG_LINK_INTR) |
| 1423 | netif_carrier_off(net); |
| 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | return 0; |
| 1426 | |
| 1427 | out3: |
| 1428 | if (info->unbind) |
| 1429 | info->unbind (dev, udev); |
| 1430 | out1: |
| 1431 | free_netdev(net); |
| 1432 | out: |
| 1433 | usb_put_dev(xdev); |
| 1434 | return status; |
| 1435 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1436 | EXPORT_SYMBOL_GPL(usbnet_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
| 1438 | /*-------------------------------------------------------------------------*/ |
| 1439 | |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1440 | /* |
| 1441 | * suspend the whole driver as soon as the first interface is suspended |
| 1442 | * resume only when the last interface is resumed |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1443 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1445 | int usbnet_suspend (struct usb_interface *intf, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | { |
| 1447 | struct usbnet *dev = usb_get_intfdata(intf); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1448 | |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1449 | if (!dev->suspend_count++) { |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1450 | spin_lock_irq(&dev->txq.lock); |
| 1451 | /* don't autosuspend while transmitting */ |
| 1452 | if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) { |
| 1453 | spin_unlock_irq(&dev->txq.lock); |
| 1454 | return -EBUSY; |
| 1455 | } else { |
| 1456 | set_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1457 | spin_unlock_irq(&dev->txq.lock); |
| 1458 | } |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1459 | /* |
| 1460 | * accelerate emptying of the rx and queues, to avoid |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1461 | * having everything error out. |
| 1462 | */ |
| 1463 | netif_device_detach (dev->net); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1464 | usbnet_terminate_urbs(dev); |
| 1465 | usb_kill_urb(dev->interrupt); |
| 1466 | |
Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1467 | /* |
| 1468 | * reattach so runtime management can use and |
| 1469 | * wake the device |
| 1470 | */ |
| 1471 | netif_device_attach (dev->net); |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1475 | EXPORT_SYMBOL_GPL(usbnet_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1477 | int usbnet_resume (struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | { |
| 1479 | struct usbnet *dev = usb_get_intfdata(intf); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1480 | struct sk_buff *skb; |
| 1481 | struct urb *res; |
| 1482 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1484 | if (!--dev->suspend_count) { |
| 1485 | spin_lock_irq(&dev->txq.lock); |
| 1486 | while ((res = usb_get_from_anchor(&dev->deferred))) { |
| 1487 | |
| 1488 | printk(KERN_INFO"%s has delayed data\n", __func__); |
| 1489 | skb = (struct sk_buff *)res->context; |
| 1490 | retval = usb_submit_urb(res, GFP_ATOMIC); |
| 1491 | if (retval < 0) { |
| 1492 | dev_kfree_skb_any(skb); |
| 1493 | usb_free_urb(res); |
| 1494 | usb_autopm_put_interface_async(dev->intf); |
| 1495 | } else { |
| 1496 | dev->net->trans_start = jiffies; |
| 1497 | __skb_queue_tail(&dev->txq, skb); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | smp_mb(); |
| 1502 | clear_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1503 | spin_unlock_irq(&dev->txq.lock); |
| 1504 | if (!(dev->txq.qlen >= TX_QLEN(dev))) |
| 1505 | netif_start_queue(dev->net); |
Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1506 | tasklet_schedule (&dev->bh); |
Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame^] | 1507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1510 | EXPORT_SYMBOL_GPL(usbnet_resume); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | /*-------------------------------------------------------------------------*/ |
| 1514 | |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1515 | static int __init usbnet_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | { |
David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 1517 | /* compiler should optimize this out */ |
Alexey Dobriyan | f8ac232 | 2006-10-08 16:02:00 +0400 | [diff] [blame] | 1518 | BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | < sizeof (struct skb_data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | random_ether_addr(node_id); |
David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1522 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1524 | module_init(usbnet_init); |
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 | static void __exit usbnet_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | } |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1529 | module_exit(usbnet_exit); |
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_AUTHOR("David Brownell"); |
David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 1532 | MODULE_DESCRIPTION("USB network driver framework"); |
David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1533 | MODULE_LICENSE("GPL"); |