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