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