David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 6 | * Copyright (C) 2008 Nokia Corporation |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* #define VERBOSE_DEBUG */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | 9830317 | 2012-09-10 16:30:50 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/etherdevice.h> |
| 22 | #include <linux/ethtool.h> |
Ian Coolidge | 4fe5f07 | 2012-11-07 14:39:18 +0000 | [diff] [blame] | 23 | #include <linux/if_vlan.h> |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 24 | |
| 25 | #include "u_ether.h" |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * This component encapsulates the Ethernet link glue needed to provide |
| 30 | * one (!) network link through the USB gadget stack, normally "usb0". |
| 31 | * |
| 32 | * The control and data models are handled by the function driver which |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 33 | * connects to this code; such as CDC Ethernet (ECM or EEM), |
| 34 | * "CDC Subset", or RNDIS. That includes all descriptor and endpoint |
| 35 | * management. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 36 | * |
| 37 | * Link level addressing is handled by this component using module |
| 38 | * parameters; if no such parameters are provided, random link level |
| 39 | * addresses are used. Each end of the link uses one address. The |
| 40 | * host end address is exported in various ways, and is often recorded |
| 41 | * in configuration databases. |
| 42 | * |
| 43 | * The driver which assembles each configuration using such a link is |
| 44 | * responsible for ensuring that each configuration includes at most one |
| 45 | * instance of is network link. (The network layer provides ways for |
| 46 | * this single "physical" link to be used by multiple virtual links.) |
| 47 | */ |
| 48 | |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 49 | #define UETH__VERSION "29-May-2008" |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 50 | |
| 51 | struct eth_dev { |
| 52 | /* lock is held while accessing port_usb |
| 53 | * or updating its backlink port_usb->ioport |
| 54 | */ |
| 55 | spinlock_t lock; |
| 56 | struct gether *port_usb; |
| 57 | |
| 58 | struct net_device *net; |
| 59 | struct usb_gadget *gadget; |
| 60 | |
| 61 | spinlock_t req_lock; /* guard {rx,tx}_reqs */ |
| 62 | struct list_head tx_reqs, rx_reqs; |
| 63 | atomic_t tx_qlen; |
| 64 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 65 | struct sk_buff_head rx_frames; |
| 66 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 67 | unsigned header_len; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 68 | struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb); |
| 69 | int (*unwrap)(struct gether *, |
| 70 | struct sk_buff *skb, |
| 71 | struct sk_buff_head *list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 72 | |
| 73 | struct work_struct work; |
| 74 | |
| 75 | unsigned long todo; |
| 76 | #define WORK_RX_MEMORY 0 |
| 77 | |
| 78 | bool zlp; |
| 79 | u8 host_mac[ETH_ALEN]; |
| 80 | }; |
| 81 | |
| 82 | /*-------------------------------------------------------------------------*/ |
| 83 | |
| 84 | #define RX_EXTRA 20 /* bytes guarding against rx overflows */ |
| 85 | |
| 86 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 87 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 88 | static unsigned qmult = 5; |
| 89 | module_param(qmult, uint, S_IRUGO|S_IWUSR); |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 90 | MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed"); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 91 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 92 | /* for dual-speed hardware, use deeper queues at high/super speed */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 93 | static inline int qlen(struct usb_gadget *gadget) |
| 94 | { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 95 | if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH || |
| 96 | gadget->speed == USB_SPEED_SUPER)) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 97 | return qmult * DEFAULT_QLEN; |
| 98 | else |
| 99 | return DEFAULT_QLEN; |
| 100 | } |
| 101 | |
| 102 | /*-------------------------------------------------------------------------*/ |
| 103 | |
| 104 | /* REVISIT there must be a better way than having two sets |
| 105 | * of debug calls ... |
| 106 | */ |
| 107 | |
| 108 | #undef DBG |
| 109 | #undef VDBG |
| 110 | #undef ERROR |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 111 | #undef INFO |
| 112 | |
| 113 | #define xprintk(d, level, fmt, args...) \ |
| 114 | printk(level "%s: " fmt , (d)->net->name , ## args) |
| 115 | |
| 116 | #ifdef DEBUG |
| 117 | #undef DEBUG |
| 118 | #define DBG(dev, fmt, args...) \ |
| 119 | xprintk(dev , KERN_DEBUG , fmt , ## args) |
| 120 | #else |
| 121 | #define DBG(dev, fmt, args...) \ |
| 122 | do { } while (0) |
| 123 | #endif /* DEBUG */ |
| 124 | |
| 125 | #ifdef VERBOSE_DEBUG |
| 126 | #define VDBG DBG |
| 127 | #else |
| 128 | #define VDBG(dev, fmt, args...) \ |
| 129 | do { } while (0) |
| 130 | #endif /* DEBUG */ |
| 131 | |
| 132 | #define ERROR(dev, fmt, args...) \ |
| 133 | xprintk(dev , KERN_ERR , fmt , ## args) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 134 | #define INFO(dev, fmt, args...) \ |
| 135 | xprintk(dev , KERN_INFO , fmt , ## args) |
| 136 | |
| 137 | /*-------------------------------------------------------------------------*/ |
| 138 | |
| 139 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ |
| 140 | |
Stephen Hemminger | ccad637 | 2008-11-19 22:42:31 -0800 | [diff] [blame] | 141 | static int ueth_change_mtu(struct net_device *net, int new_mtu) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 142 | { |
| 143 | struct eth_dev *dev = netdev_priv(net); |
| 144 | unsigned long flags; |
| 145 | int status = 0; |
| 146 | |
| 147 | /* don't change MTU on "live" link (peer won't know) */ |
| 148 | spin_lock_irqsave(&dev->lock, flags); |
| 149 | if (dev->port_usb) |
| 150 | status = -EBUSY; |
| 151 | else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) |
| 152 | status = -ERANGE; |
| 153 | else |
| 154 | net->mtu = new_mtu; |
| 155 | spin_unlock_irqrestore(&dev->lock, flags); |
| 156 | |
| 157 | return status; |
| 158 | } |
| 159 | |
| 160 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) |
| 161 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 162 | struct eth_dev *dev = netdev_priv(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 163 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 164 | strlcpy(p->driver, "g_ether", sizeof(p->driver)); |
| 165 | strlcpy(p->version, UETH__VERSION, sizeof(p->version)); |
| 166 | strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version)); |
| 167 | strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 168 | } |
| 169 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 170 | /* REVISIT can also support: |
| 171 | * - WOL (by tracking suspends and issuing remote wakeup) |
| 172 | * - msglevel (implies updated messaging) |
| 173 | * - ... probably more ethtool ops |
| 174 | */ |
| 175 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 176 | static const struct ethtool_ops ops = { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 177 | .get_drvinfo = eth_get_drvinfo, |
Jonathan McDowell | 237e75b | 2009-03-26 00:45:27 -0700 | [diff] [blame] | 178 | .get_link = ethtool_op_get_link, |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | static void defer_kevent(struct eth_dev *dev, int flag) |
| 182 | { |
| 183 | if (test_and_set_bit(flag, &dev->todo)) |
| 184 | return; |
| 185 | if (!schedule_work(&dev->work)) |
| 186 | ERROR(dev, "kevent %d may have been dropped\n", flag); |
| 187 | else |
| 188 | DBG(dev, "kevent %d scheduled\n", flag); |
| 189 | } |
| 190 | |
| 191 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
| 192 | |
| 193 | static int |
| 194 | rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) |
| 195 | { |
| 196 | struct sk_buff *skb; |
| 197 | int retval = -ENOMEM; |
| 198 | size_t size = 0; |
| 199 | struct usb_ep *out; |
| 200 | unsigned long flags; |
| 201 | |
| 202 | spin_lock_irqsave(&dev->lock, flags); |
| 203 | if (dev->port_usb) |
| 204 | out = dev->port_usb->out_ep; |
| 205 | else |
| 206 | out = NULL; |
| 207 | spin_unlock_irqrestore(&dev->lock, flags); |
| 208 | |
| 209 | if (!out) |
| 210 | return -ENOTCONN; |
| 211 | |
| 212 | |
| 213 | /* Padding up to RX_EXTRA handles minor disagreements with host. |
| 214 | * Normally we use the USB "terminate on short read" convention; |
| 215 | * so allow up to (N*maxpacket), since that memory is normally |
| 216 | * already allocated. Some hardware doesn't deal well with short |
| 217 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 218 | * byte off the end (to force hardware errors on overflow). |
| 219 | * |
| 220 | * RNDIS uses internal framing, and explicitly allows senders to |
| 221 | * pad to end-of-packet. That's potentially nice for speed, but |
| 222 | * means receivers can't recover lost synch on their own (because |
| 223 | * new packets don't only start after a short RX). |
| 224 | */ |
| 225 | size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA; |
| 226 | size += dev->port_usb->header_len; |
| 227 | size += out->maxpacket - 1; |
| 228 | size -= size % out->maxpacket; |
| 229 | |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 230 | if (dev->port_usb->is_fixed) |
Stephen Hemminger | 45d1b7a | 2011-03-01 22:40:57 -0800 | [diff] [blame] | 231 | size = max_t(size_t, size, dev->port_usb->fixed_out_len); |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 232 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 233 | skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); |
| 234 | if (skb == NULL) { |
| 235 | DBG(dev, "no rx skb\n"); |
| 236 | goto enomem; |
| 237 | } |
| 238 | |
| 239 | /* Some platforms perform better when IP packets are aligned, |
| 240 | * but on at least one, checksumming fails otherwise. Note: |
| 241 | * RNDIS headers involve variable numbers of LE32 values. |
| 242 | */ |
| 243 | skb_reserve(skb, NET_IP_ALIGN); |
| 244 | |
| 245 | req->buf = skb->data; |
| 246 | req->length = size; |
| 247 | req->complete = rx_complete; |
| 248 | req->context = skb; |
| 249 | |
| 250 | retval = usb_ep_queue(out, req, gfp_flags); |
| 251 | if (retval == -ENOMEM) |
| 252 | enomem: |
| 253 | defer_kevent(dev, WORK_RX_MEMORY); |
| 254 | if (retval) { |
| 255 | DBG(dev, "rx submit --> %d\n", retval); |
| 256 | if (skb) |
| 257 | dev_kfree_skb_any(skb); |
| 258 | spin_lock_irqsave(&dev->req_lock, flags); |
| 259 | list_add(&req->list, &dev->rx_reqs); |
| 260 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 261 | } |
| 262 | return retval; |
| 263 | } |
| 264 | |
| 265 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
| 266 | { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 267 | struct sk_buff *skb = req->context, *skb2; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 268 | struct eth_dev *dev = ep->driver_data; |
| 269 | int status = req->status; |
| 270 | |
| 271 | switch (status) { |
| 272 | |
| 273 | /* normal completion */ |
| 274 | case 0: |
| 275 | skb_put(skb, req->actual); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 276 | |
| 277 | if (dev->unwrap) { |
| 278 | unsigned long flags; |
| 279 | |
| 280 | spin_lock_irqsave(&dev->lock, flags); |
| 281 | if (dev->port_usb) { |
| 282 | status = dev->unwrap(dev->port_usb, |
| 283 | skb, |
| 284 | &dev->rx_frames); |
| 285 | } else { |
| 286 | dev_kfree_skb_any(skb); |
| 287 | status = -ENOTCONN; |
| 288 | } |
| 289 | spin_unlock_irqrestore(&dev->lock, flags); |
| 290 | } else { |
| 291 | skb_queue_tail(&dev->rx_frames, skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 292 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 293 | skb = NULL; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 294 | |
| 295 | skb2 = skb_dequeue(&dev->rx_frames); |
| 296 | while (skb2) { |
| 297 | if (status < 0 |
| 298 | || ETH_HLEN > skb2->len |
Ian Coolidge | 4fe5f07 | 2012-11-07 14:39:18 +0000 | [diff] [blame] | 299 | || skb2->len > VLAN_ETH_FRAME_LEN) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 300 | dev->net->stats.rx_errors++; |
| 301 | dev->net->stats.rx_length_errors++; |
| 302 | DBG(dev, "rx length %d\n", skb2->len); |
| 303 | dev_kfree_skb_any(skb2); |
| 304 | goto next_frame; |
| 305 | } |
| 306 | skb2->protocol = eth_type_trans(skb2, dev->net); |
| 307 | dev->net->stats.rx_packets++; |
| 308 | dev->net->stats.rx_bytes += skb2->len; |
| 309 | |
| 310 | /* no buffer copies needed, unless hardware can't |
| 311 | * use skb buffers. |
| 312 | */ |
| 313 | status = netif_rx(skb2); |
| 314 | next_frame: |
| 315 | skb2 = skb_dequeue(&dev->rx_frames); |
| 316 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 317 | break; |
| 318 | |
| 319 | /* software-driven interface shutdown */ |
| 320 | case -ECONNRESET: /* unlink */ |
| 321 | case -ESHUTDOWN: /* disconnect etc */ |
| 322 | VDBG(dev, "rx shutdown, code %d\n", status); |
| 323 | goto quiesce; |
| 324 | |
| 325 | /* for hardware automagic (such as pxa) */ |
| 326 | case -ECONNABORTED: /* endpoint reset */ |
| 327 | DBG(dev, "rx %s reset\n", ep->name); |
| 328 | defer_kevent(dev, WORK_RX_MEMORY); |
| 329 | quiesce: |
| 330 | dev_kfree_skb_any(skb); |
| 331 | goto clean; |
| 332 | |
| 333 | /* data overrun */ |
| 334 | case -EOVERFLOW: |
| 335 | dev->net->stats.rx_over_errors++; |
| 336 | /* FALLTHROUGH */ |
| 337 | |
| 338 | default: |
| 339 | dev->net->stats.rx_errors++; |
| 340 | DBG(dev, "rx status %d\n", status); |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if (skb) |
| 345 | dev_kfree_skb_any(skb); |
| 346 | if (!netif_running(dev->net)) { |
| 347 | clean: |
| 348 | spin_lock(&dev->req_lock); |
| 349 | list_add(&req->list, &dev->rx_reqs); |
| 350 | spin_unlock(&dev->req_lock); |
| 351 | req = NULL; |
| 352 | } |
| 353 | if (req) |
| 354 | rx_submit(dev, req, GFP_ATOMIC); |
| 355 | } |
| 356 | |
| 357 | static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n) |
| 358 | { |
| 359 | unsigned i; |
| 360 | struct usb_request *req; |
| 361 | |
| 362 | if (!n) |
| 363 | return -ENOMEM; |
| 364 | |
| 365 | /* queue/recycle up to N requests */ |
| 366 | i = n; |
| 367 | list_for_each_entry(req, list, list) { |
| 368 | if (i-- == 0) |
| 369 | goto extra; |
| 370 | } |
| 371 | while (i--) { |
| 372 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| 373 | if (!req) |
| 374 | return list_empty(list) ? -ENOMEM : 0; |
| 375 | list_add(&req->list, list); |
| 376 | } |
| 377 | return 0; |
| 378 | |
| 379 | extra: |
| 380 | /* free extras */ |
| 381 | for (;;) { |
| 382 | struct list_head *next; |
| 383 | |
| 384 | next = req->list.next; |
| 385 | list_del(&req->list); |
| 386 | usb_ep_free_request(ep, req); |
| 387 | |
| 388 | if (next == list) |
| 389 | break; |
| 390 | |
| 391 | req = container_of(next, struct usb_request, list); |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n) |
| 397 | { |
| 398 | int status; |
| 399 | |
| 400 | spin_lock(&dev->req_lock); |
| 401 | status = prealloc(&dev->tx_reqs, link->in_ep, n); |
| 402 | if (status < 0) |
| 403 | goto fail; |
| 404 | status = prealloc(&dev->rx_reqs, link->out_ep, n); |
| 405 | if (status < 0) |
| 406 | goto fail; |
| 407 | goto done; |
| 408 | fail: |
| 409 | DBG(dev, "can't alloc requests\n"); |
| 410 | done: |
| 411 | spin_unlock(&dev->req_lock); |
| 412 | return status; |
| 413 | } |
| 414 | |
| 415 | static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags) |
| 416 | { |
| 417 | struct usb_request *req; |
| 418 | unsigned long flags; |
| 419 | |
| 420 | /* fill unused rxq slots with some skb */ |
| 421 | spin_lock_irqsave(&dev->req_lock, flags); |
| 422 | while (!list_empty(&dev->rx_reqs)) { |
| 423 | req = container_of(dev->rx_reqs.next, |
| 424 | struct usb_request, list); |
| 425 | list_del_init(&req->list); |
| 426 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 427 | |
| 428 | if (rx_submit(dev, req, gfp_flags) < 0) { |
| 429 | defer_kevent(dev, WORK_RX_MEMORY); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | spin_lock_irqsave(&dev->req_lock, flags); |
| 434 | } |
| 435 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 436 | } |
| 437 | |
| 438 | static void eth_work(struct work_struct *work) |
| 439 | { |
| 440 | struct eth_dev *dev = container_of(work, struct eth_dev, work); |
| 441 | |
| 442 | if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) { |
| 443 | if (netif_running(dev->net)) |
| 444 | rx_fill(dev, GFP_KERNEL); |
| 445 | } |
| 446 | |
| 447 | if (dev->todo) |
| 448 | DBG(dev, "work done, flags = 0x%lx\n", dev->todo); |
| 449 | } |
| 450 | |
| 451 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 452 | { |
| 453 | struct sk_buff *skb = req->context; |
| 454 | struct eth_dev *dev = ep->driver_data; |
| 455 | |
| 456 | switch (req->status) { |
| 457 | default: |
| 458 | dev->net->stats.tx_errors++; |
| 459 | VDBG(dev, "tx err %d\n", req->status); |
| 460 | /* FALLTHROUGH */ |
| 461 | case -ECONNRESET: /* unlink */ |
| 462 | case -ESHUTDOWN: /* disconnect etc */ |
| 463 | break; |
| 464 | case 0: |
| 465 | dev->net->stats.tx_bytes += skb->len; |
| 466 | } |
| 467 | dev->net->stats.tx_packets++; |
| 468 | |
| 469 | spin_lock(&dev->req_lock); |
| 470 | list_add(&req->list, &dev->tx_reqs); |
| 471 | spin_unlock(&dev->req_lock); |
| 472 | dev_kfree_skb_any(skb); |
| 473 | |
| 474 | atomic_dec(&dev->tx_qlen); |
| 475 | if (netif_carrier_ok(dev->net)) |
| 476 | netif_wake_queue(dev->net); |
| 477 | } |
| 478 | |
| 479 | static inline int is_promisc(u16 cdc_filter) |
| 480 | { |
| 481 | return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 482 | } |
| 483 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 484 | static netdev_tx_t eth_start_xmit(struct sk_buff *skb, |
| 485 | struct net_device *net) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 486 | { |
| 487 | struct eth_dev *dev = netdev_priv(net); |
| 488 | int length = skb->len; |
| 489 | int retval; |
| 490 | struct usb_request *req = NULL; |
| 491 | unsigned long flags; |
| 492 | struct usb_ep *in; |
| 493 | u16 cdc_filter; |
| 494 | |
| 495 | spin_lock_irqsave(&dev->lock, flags); |
| 496 | if (dev->port_usb) { |
| 497 | in = dev->port_usb->in_ep; |
| 498 | cdc_filter = dev->port_usb->cdc_filter; |
| 499 | } else { |
| 500 | in = NULL; |
| 501 | cdc_filter = 0; |
| 502 | } |
| 503 | spin_unlock_irqrestore(&dev->lock, flags); |
| 504 | |
| 505 | if (!in) { |
| 506 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 507 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* apply outgoing CDC or RNDIS filters */ |
| 511 | if (!is_promisc(cdc_filter)) { |
| 512 | u8 *dest = skb->data; |
| 513 | |
| 514 | if (is_multicast_ether_addr(dest)) { |
| 515 | u16 type; |
| 516 | |
| 517 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 518 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 519 | */ |
| 520 | if (is_broadcast_ether_addr(dest)) |
| 521 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 522 | else |
| 523 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 524 | if (!(cdc_filter & type)) { |
| 525 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 526 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 530 | } |
| 531 | |
| 532 | spin_lock_irqsave(&dev->req_lock, flags); |
| 533 | /* |
| 534 | * this freelist can be empty if an interrupt triggered disconnect() |
| 535 | * and reconfigured the gadget (shutting down this queue) after the |
| 536 | * network stack decided to xmit but before we got the spinlock. |
| 537 | */ |
| 538 | if (list_empty(&dev->tx_reqs)) { |
| 539 | spin_unlock_irqrestore(&dev->req_lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 540 | return NETDEV_TX_BUSY; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | req = container_of(dev->tx_reqs.next, struct usb_request, list); |
| 544 | list_del(&req->list); |
| 545 | |
| 546 | /* temporarily stop TX queue when the freelist empties */ |
| 547 | if (list_empty(&dev->tx_reqs)) |
| 548 | netif_stop_queue(net); |
| 549 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 550 | |
| 551 | /* no buffer copies needed, unless the network stack did it |
| 552 | * or the hardware can't use skb buffers. |
| 553 | * or there's not enough space for extra headers we need |
| 554 | */ |
| 555 | if (dev->wrap) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 556 | unsigned long flags; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 557 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 558 | spin_lock_irqsave(&dev->lock, flags); |
| 559 | if (dev->port_usb) |
| 560 | skb = dev->wrap(dev->port_usb, skb); |
| 561 | spin_unlock_irqrestore(&dev->lock, flags); |
| 562 | if (!skb) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 563 | goto drop; |
| 564 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 565 | length = skb->len; |
| 566 | } |
| 567 | req->buf = skb->data; |
| 568 | req->context = skb; |
| 569 | req->complete = tx_complete; |
| 570 | |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 571 | /* NCM requires no zlp if transfer is dwNtbInMaxSize */ |
| 572 | if (dev->port_usb->is_fixed && |
| 573 | length == dev->port_usb->fixed_in_len && |
| 574 | (length % in->maxpacket) == 0) |
| 575 | req->zero = 0; |
| 576 | else |
| 577 | req->zero = 1; |
| 578 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 579 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 580 | * though any robust network rx path ignores extra padding. |
| 581 | * and some hardware doesn't like to write zlps. |
| 582 | */ |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 583 | if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 584 | length++; |
| 585 | |
| 586 | req->length = length; |
| 587 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 588 | /* throttle high/super speed IRQ rate back slightly */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 589 | if (gadget_is_dualspeed(dev->gadget)) |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 590 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH || |
| 591 | dev->gadget->speed == USB_SPEED_SUPER) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 592 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) |
| 593 | : 0; |
| 594 | |
| 595 | retval = usb_ep_queue(in, req, GFP_ATOMIC); |
| 596 | switch (retval) { |
| 597 | default: |
| 598 | DBG(dev, "tx queue err %d\n", retval); |
| 599 | break; |
| 600 | case 0: |
| 601 | net->trans_start = jiffies; |
| 602 | atomic_inc(&dev->tx_qlen); |
| 603 | } |
| 604 | |
| 605 | if (retval) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 606 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 607 | drop: |
| 608 | dev->net->stats.tx_dropped++; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 609 | spin_lock_irqsave(&dev->req_lock, flags); |
| 610 | if (list_empty(&dev->tx_reqs)) |
| 611 | netif_start_queue(net); |
| 612 | list_add(&req->list, &dev->tx_reqs); |
| 613 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 614 | } |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 615 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /*-------------------------------------------------------------------------*/ |
| 619 | |
| 620 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) |
| 621 | { |
| 622 | DBG(dev, "%s\n", __func__); |
| 623 | |
| 624 | /* fill the rx queue */ |
| 625 | rx_fill(dev, gfp_flags); |
| 626 | |
| 627 | /* and open the tx floodgates */ |
| 628 | atomic_set(&dev->tx_qlen, 0); |
| 629 | netif_wake_queue(dev->net); |
| 630 | } |
| 631 | |
| 632 | static int eth_open(struct net_device *net) |
| 633 | { |
| 634 | struct eth_dev *dev = netdev_priv(net); |
| 635 | struct gether *link; |
| 636 | |
| 637 | DBG(dev, "%s\n", __func__); |
| 638 | if (netif_carrier_ok(dev->net)) |
| 639 | eth_start(dev, GFP_KERNEL); |
| 640 | |
| 641 | spin_lock_irq(&dev->lock); |
| 642 | link = dev->port_usb; |
| 643 | if (link && link->open) |
| 644 | link->open(link); |
| 645 | spin_unlock_irq(&dev->lock); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int eth_stop(struct net_device *net) |
| 651 | { |
| 652 | struct eth_dev *dev = netdev_priv(net); |
| 653 | unsigned long flags; |
| 654 | |
| 655 | VDBG(dev, "%s\n", __func__); |
| 656 | netif_stop_queue(net); |
| 657 | |
| 658 | DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", |
| 659 | dev->net->stats.rx_packets, dev->net->stats.tx_packets, |
| 660 | dev->net->stats.rx_errors, dev->net->stats.tx_errors |
| 661 | ); |
| 662 | |
| 663 | /* ensure there are no more active requests */ |
| 664 | spin_lock_irqsave(&dev->lock, flags); |
| 665 | if (dev->port_usb) { |
| 666 | struct gether *link = dev->port_usb; |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 667 | const struct usb_endpoint_descriptor *in; |
| 668 | const struct usb_endpoint_descriptor *out; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 669 | |
| 670 | if (link->close) |
| 671 | link->close(link); |
| 672 | |
| 673 | /* NOTE: we have no abort-queue primitive we could use |
| 674 | * to cancel all pending I/O. Instead, we disable then |
| 675 | * reenable the endpoints ... this idiom may leave toggle |
| 676 | * wrong, but that's a self-correcting error. |
| 677 | * |
| 678 | * REVISIT: we *COULD* just let the transfers complete at |
| 679 | * their own pace; the network stack can handle old packets. |
| 680 | * For the moment we leave this here, since it works. |
| 681 | */ |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 682 | in = link->in_ep->desc; |
| 683 | out = link->out_ep->desc; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 684 | usb_ep_disable(link->in_ep); |
| 685 | usb_ep_disable(link->out_ep); |
| 686 | if (netif_carrier_ok(net)) { |
| 687 | DBG(dev, "host still using in/out endpoints\n"); |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 688 | link->in_ep->desc = in; |
| 689 | link->out_ep->desc = out; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 690 | usb_ep_enable(link->in_ep); |
| 691 | usb_ep_enable(link->out_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | spin_unlock_irqrestore(&dev->lock, flags); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /*-------------------------------------------------------------------------*/ |
| 700 | |
| 701 | /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ |
| 702 | static char *dev_addr; |
| 703 | module_param(dev_addr, charp, S_IRUGO); |
| 704 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); |
| 705 | |
| 706 | /* this address is invisible to ifconfig */ |
| 707 | static char *host_addr; |
| 708 | module_param(host_addr, charp, S_IRUGO); |
| 709 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); |
| 710 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 711 | static int get_ether_addr(const char *str, u8 *dev_addr) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 712 | { |
| 713 | if (str) { |
| 714 | unsigned i; |
| 715 | |
| 716 | for (i = 0; i < 6; i++) { |
| 717 | unsigned char num; |
| 718 | |
| 719 | if ((*str == '.') || (*str == ':')) |
| 720 | str++; |
Andy Shevchenko | e644814 | 2010-06-15 17:04:44 +0300 | [diff] [blame] | 721 | num = hex_to_bin(*str++) << 4; |
| 722 | num |= hex_to_bin(*str++); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 723 | dev_addr [i] = num; |
| 724 | } |
| 725 | if (is_valid_ether_addr(dev_addr)) |
| 726 | return 0; |
| 727 | } |
Joe Perches | 006c913 | 2012-07-12 22:33:11 -0700 | [diff] [blame] | 728 | eth_random_addr(dev_addr); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | static struct eth_dev *the_dev; |
| 733 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 734 | static const struct net_device_ops eth_netdev_ops = { |
| 735 | .ndo_open = eth_open, |
| 736 | .ndo_stop = eth_stop, |
| 737 | .ndo_start_xmit = eth_start_xmit, |
| 738 | .ndo_change_mtu = ueth_change_mtu, |
| 739 | .ndo_set_mac_address = eth_mac_addr, |
| 740 | .ndo_validate_addr = eth_validate_addr, |
| 741 | }; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 742 | |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 743 | static struct device_type gadget_type = { |
| 744 | .name = "gadget", |
| 745 | }; |
| 746 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 747 | /** |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 748 | * gether_setup_name - initialize one ethernet-over-usb link |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 749 | * @g: gadget to associated with these links |
| 750 | * @ethaddr: NULL, or a buffer in which the ethernet address of the |
| 751 | * host side of the link is recorded |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 752 | * @netname: name for network device (for example, "usb") |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 753 | * Context: may sleep |
| 754 | * |
| 755 | * This sets up the single network link that may be exported by a |
| 756 | * gadget driver using this framework. The link layer addresses are |
| 757 | * set up using module parameters. |
| 758 | * |
| 759 | * Returns negative errno, or zero on success |
| 760 | */ |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 761 | int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN], |
| 762 | const char *netname) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 763 | { |
| 764 | struct eth_dev *dev; |
| 765 | struct net_device *net; |
| 766 | int status; |
| 767 | |
| 768 | if (the_dev) |
| 769 | return -EBUSY; |
| 770 | |
| 771 | net = alloc_etherdev(sizeof *dev); |
| 772 | if (!net) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | dev = netdev_priv(net); |
| 776 | spin_lock_init(&dev->lock); |
| 777 | spin_lock_init(&dev->req_lock); |
| 778 | INIT_WORK(&dev->work, eth_work); |
| 779 | INIT_LIST_HEAD(&dev->tx_reqs); |
| 780 | INIT_LIST_HEAD(&dev->rx_reqs); |
| 781 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 782 | skb_queue_head_init(&dev->rx_frames); |
| 783 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 784 | /* network device setup */ |
| 785 | dev->net = net; |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 786 | snprintf(net->name, sizeof(net->name), "%s%%d", netname); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 787 | |
| 788 | if (get_ether_addr(dev_addr, net->dev_addr)) |
| 789 | dev_warn(&g->dev, |
| 790 | "using random %s ethernet address\n", "self"); |
| 791 | if (get_ether_addr(host_addr, dev->host_mac)) |
| 792 | dev_warn(&g->dev, |
| 793 | "using random %s ethernet address\n", "host"); |
| 794 | |
| 795 | if (ethaddr) |
| 796 | memcpy(ethaddr, dev->host_mac, ETH_ALEN); |
| 797 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 798 | net->netdev_ops = ð_netdev_ops; |
| 799 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 800 | SET_ETHTOOL_OPS(net, &ops); |
| 801 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 802 | dev->gadget = g; |
| 803 | SET_NETDEV_DEV(net, &g->dev); |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 804 | SET_NETDEV_DEVTYPE(net, &gadget_type); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 805 | |
| 806 | status = register_netdev(net); |
| 807 | if (status < 0) { |
| 808 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); |
| 809 | free_netdev(net); |
| 810 | } else { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 811 | INFO(dev, "MAC %pM\n", net->dev_addr); |
| 812 | INFO(dev, "HOST MAC %pM\n", dev->host_mac); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 813 | |
| 814 | the_dev = dev; |
Kevin Cernekee | 31bde1c | 2012-06-24 21:11:22 -0700 | [diff] [blame] | 815 | |
| 816 | /* two kinds of host-initiated state changes: |
| 817 | * - iff DATA transfer is active, carrier is "on" |
| 818 | * - tx queueing enabled if open *and* carrier is "on" |
| 819 | */ |
| 820 | netif_carrier_off(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return status; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * gether_cleanup - remove Ethernet-over-USB device |
| 828 | * Context: may sleep |
| 829 | * |
| 830 | * This is called to free all resources allocated by @gether_setup(). |
| 831 | */ |
| 832 | void gether_cleanup(void) |
| 833 | { |
| 834 | if (!the_dev) |
| 835 | return; |
| 836 | |
| 837 | unregister_netdev(the_dev->net); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 838 | flush_work(&the_dev->work); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 839 | free_netdev(the_dev->net); |
| 840 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 841 | the_dev = NULL; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /** |
| 846 | * gether_connect - notify network layer that USB link is active |
| 847 | * @link: the USB link, set up with endpoints, descriptors matching |
| 848 | * current device speed, and any framing wrapper(s) set up. |
| 849 | * Context: irqs blocked |
| 850 | * |
| 851 | * This is called to activate endpoints and let the network layer know |
| 852 | * the connection is active ("carrier detect"). It may cause the I/O |
| 853 | * queues to open and start letting network packets flow, but will in |
| 854 | * any case activate the endpoints so that they respond properly to the |
| 855 | * USB host. |
| 856 | * |
| 857 | * Verify net_device pointer returned using IS_ERR(). If it doesn't |
| 858 | * indicate some error code (negative errno), ep->driver_data values |
| 859 | * have been overwritten. |
| 860 | */ |
| 861 | struct net_device *gether_connect(struct gether *link) |
| 862 | { |
| 863 | struct eth_dev *dev = the_dev; |
| 864 | int result = 0; |
| 865 | |
| 866 | if (!dev) |
| 867 | return ERR_PTR(-EINVAL); |
| 868 | |
| 869 | link->in_ep->driver_data = dev; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 870 | result = usb_ep_enable(link->in_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 871 | if (result != 0) { |
| 872 | DBG(dev, "enable %s --> %d\n", |
| 873 | link->in_ep->name, result); |
| 874 | goto fail0; |
| 875 | } |
| 876 | |
| 877 | link->out_ep->driver_data = dev; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 878 | result = usb_ep_enable(link->out_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 879 | if (result != 0) { |
| 880 | DBG(dev, "enable %s --> %d\n", |
| 881 | link->out_ep->name, result); |
| 882 | goto fail1; |
| 883 | } |
| 884 | |
| 885 | if (result == 0) |
| 886 | result = alloc_requests(dev, link, qlen(dev->gadget)); |
| 887 | |
| 888 | if (result == 0) { |
| 889 | dev->zlp = link->is_zlp_ok; |
| 890 | DBG(dev, "qlen %d\n", qlen(dev->gadget)); |
| 891 | |
| 892 | dev->header_len = link->header_len; |
| 893 | dev->unwrap = link->unwrap; |
| 894 | dev->wrap = link->wrap; |
| 895 | |
| 896 | spin_lock(&dev->lock); |
| 897 | dev->port_usb = link; |
| 898 | link->ioport = dev; |
David Brownell | 29bac7b | 2008-09-06 21:33:49 -0700 | [diff] [blame] | 899 | if (netif_running(dev->net)) { |
| 900 | if (link->open) |
| 901 | link->open(link); |
| 902 | } else { |
| 903 | if (link->close) |
| 904 | link->close(link); |
| 905 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 906 | spin_unlock(&dev->lock); |
| 907 | |
| 908 | netif_carrier_on(dev->net); |
| 909 | if (netif_running(dev->net)) |
| 910 | eth_start(dev, GFP_ATOMIC); |
| 911 | |
| 912 | /* on error, disable any endpoints */ |
| 913 | } else { |
| 914 | (void) usb_ep_disable(link->out_ep); |
| 915 | fail1: |
| 916 | (void) usb_ep_disable(link->in_ep); |
| 917 | } |
| 918 | fail0: |
| 919 | /* caller is responsible for cleanup on error */ |
| 920 | if (result < 0) |
| 921 | return ERR_PTR(result); |
| 922 | return dev->net; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * gether_disconnect - notify network layer that USB link is inactive |
| 927 | * @link: the USB link, on which gether_connect() was called |
| 928 | * Context: irqs blocked |
| 929 | * |
| 930 | * This is called to deactivate endpoints and let the network layer know |
| 931 | * the connection went inactive ("no carrier"). |
| 932 | * |
| 933 | * On return, the state is as if gether_connect() had never been called. |
| 934 | * The endpoints are inactive, and accordingly without active USB I/O. |
| 935 | * Pointers to endpoint descriptors and endpoint private data are nulled. |
| 936 | */ |
| 937 | void gether_disconnect(struct gether *link) |
| 938 | { |
| 939 | struct eth_dev *dev = link->ioport; |
| 940 | struct usb_request *req; |
| 941 | |
| 942 | WARN_ON(!dev); |
| 943 | if (!dev) |
| 944 | return; |
| 945 | |
| 946 | DBG(dev, "%s\n", __func__); |
| 947 | |
| 948 | netif_stop_queue(dev->net); |
| 949 | netif_carrier_off(dev->net); |
| 950 | |
| 951 | /* disable endpoints, forcing (synchronous) completion |
| 952 | * of all pending i/o. then free the request objects |
| 953 | * and forget about the endpoints. |
| 954 | */ |
| 955 | usb_ep_disable(link->in_ep); |
| 956 | spin_lock(&dev->req_lock); |
| 957 | while (!list_empty(&dev->tx_reqs)) { |
| 958 | req = container_of(dev->tx_reqs.next, |
| 959 | struct usb_request, list); |
| 960 | list_del(&req->list); |
| 961 | |
| 962 | spin_unlock(&dev->req_lock); |
| 963 | usb_ep_free_request(link->in_ep, req); |
| 964 | spin_lock(&dev->req_lock); |
| 965 | } |
| 966 | spin_unlock(&dev->req_lock); |
| 967 | link->in_ep->driver_data = NULL; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 968 | link->in_ep->desc = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 969 | |
| 970 | usb_ep_disable(link->out_ep); |
| 971 | spin_lock(&dev->req_lock); |
| 972 | while (!list_empty(&dev->rx_reqs)) { |
| 973 | req = container_of(dev->rx_reqs.next, |
| 974 | struct usb_request, list); |
| 975 | list_del(&req->list); |
| 976 | |
| 977 | spin_unlock(&dev->req_lock); |
| 978 | usb_ep_free_request(link->out_ep, req); |
| 979 | spin_lock(&dev->req_lock); |
| 980 | } |
| 981 | spin_unlock(&dev->req_lock); |
| 982 | link->out_ep->driver_data = NULL; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 983 | link->out_ep->desc = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 984 | |
| 985 | /* finish forgetting about this USB link episode */ |
| 986 | dev->header_len = 0; |
| 987 | dev->unwrap = NULL; |
| 988 | dev->wrap = NULL; |
| 989 | |
| 990 | spin_lock(&dev->lock); |
| 991 | dev->port_usb = NULL; |
| 992 | link->ioport = NULL; |
| 993 | spin_unlock(&dev->lock); |
| 994 | } |