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