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 | |
Mike Looijmans | bba787a | 2015-08-05 08:54:55 +0200 | [diff] [blame] | 51 | /* Experiments show that both Linux and Windows hosts allow up to 16k |
| 52 | * frame sizes. Set the max size to 15k+52 to prevent allocating 32k |
| 53 | * blocks and still have efficient handling. */ |
| 54 | #define GETHER_MAX_ETH_FRAME_LEN 15412 |
| 55 | |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 56 | static struct workqueue_struct *uether_wq; |
| 57 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 58 | struct eth_dev { |
| 59 | /* lock is held while accessing port_usb |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 60 | */ |
| 61 | spinlock_t lock; |
| 62 | struct gether *port_usb; |
| 63 | |
| 64 | struct net_device *net; |
| 65 | struct usb_gadget *gadget; |
| 66 | |
| 67 | spinlock_t req_lock; /* guard {rx,tx}_reqs */ |
| 68 | struct list_head tx_reqs, rx_reqs; |
| 69 | atomic_t tx_qlen; |
| 70 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 71 | struct sk_buff_head rx_frames; |
| 72 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 73 | unsigned qmult; |
| 74 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 75 | unsigned header_len; |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 76 | unsigned ul_max_pkts_per_xfer; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 77 | struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb); |
| 78 | int (*unwrap)(struct gether *, |
| 79 | struct sk_buff *skb, |
| 80 | struct sk_buff_head *list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 81 | |
| 82 | struct work_struct work; |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 83 | struct work_struct rx_work; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 84 | |
| 85 | unsigned long todo; |
| 86 | #define WORK_RX_MEMORY 0 |
| 87 | |
| 88 | bool zlp; |
| 89 | u8 host_mac[ETH_ALEN]; |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 90 | u8 dev_mac[ETH_ALEN]; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /*-------------------------------------------------------------------------*/ |
| 94 | |
| 95 | #define RX_EXTRA 20 /* bytes guarding against rx overflows */ |
| 96 | |
| 97 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 98 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 99 | /* for dual-speed hardware, use deeper queues at high/super speed */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 100 | static inline int qlen(struct usb_gadget *gadget, unsigned qmult) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 101 | { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 102 | if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH || |
| 103 | gadget->speed == USB_SPEED_SUPER)) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 104 | return qmult * DEFAULT_QLEN; |
| 105 | else |
| 106 | return DEFAULT_QLEN; |
| 107 | } |
| 108 | |
| 109 | /*-------------------------------------------------------------------------*/ |
| 110 | |
| 111 | /* REVISIT there must be a better way than having two sets |
| 112 | * of debug calls ... |
| 113 | */ |
| 114 | |
| 115 | #undef DBG |
| 116 | #undef VDBG |
| 117 | #undef ERROR |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 118 | #undef INFO |
| 119 | |
| 120 | #define xprintk(d, level, fmt, args...) \ |
| 121 | printk(level "%s: " fmt , (d)->net->name , ## args) |
| 122 | |
| 123 | #ifdef DEBUG |
| 124 | #undef DEBUG |
| 125 | #define DBG(dev, fmt, args...) \ |
| 126 | xprintk(dev , KERN_DEBUG , fmt , ## args) |
| 127 | #else |
| 128 | #define DBG(dev, fmt, args...) \ |
| 129 | do { } while (0) |
| 130 | #endif /* DEBUG */ |
| 131 | |
| 132 | #ifdef VERBOSE_DEBUG |
| 133 | #define VDBG DBG |
| 134 | #else |
| 135 | #define VDBG(dev, fmt, args...) \ |
| 136 | do { } while (0) |
| 137 | #endif /* DEBUG */ |
| 138 | |
| 139 | #define ERROR(dev, fmt, args...) \ |
| 140 | xprintk(dev , KERN_ERR , fmt , ## args) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 141 | #define INFO(dev, fmt, args...) \ |
| 142 | xprintk(dev , KERN_INFO , fmt , ## args) |
| 143 | |
| 144 | /*-------------------------------------------------------------------------*/ |
| 145 | |
| 146 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ |
| 147 | |
Stephen Hemminger | ccad637 | 2008-11-19 22:42:31 -0800 | [diff] [blame] | 148 | static int ueth_change_mtu(struct net_device *net, int new_mtu) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 149 | { |
Mike Looijmans | ab738ff | 2015-11-30 12:18:23 +0100 | [diff] [blame] | 150 | if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN) |
| 151 | return -ERANGE; |
| 152 | net->mtu = new_mtu; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 153 | |
Mike Looijmans | ab738ff | 2015-11-30 12:18:23 +0100 | [diff] [blame] | 154 | return 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) |
| 158 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 159 | struct eth_dev *dev = netdev_priv(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 160 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 161 | strlcpy(p->driver, "g_ether", sizeof(p->driver)); |
| 162 | strlcpy(p->version, UETH__VERSION, sizeof(p->version)); |
| 163 | strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version)); |
| 164 | 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] | 165 | } |
| 166 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 167 | /* REVISIT can also support: |
| 168 | * - WOL (by tracking suspends and issuing remote wakeup) |
| 169 | * - msglevel (implies updated messaging) |
| 170 | * - ... probably more ethtool ops |
| 171 | */ |
| 172 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 173 | static const struct ethtool_ops ops = { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 174 | .get_drvinfo = eth_get_drvinfo, |
Jonathan McDowell | 237e75b | 2009-03-26 00:45:27 -0700 | [diff] [blame] | 175 | .get_link = ethtool_op_get_link, |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static void defer_kevent(struct eth_dev *dev, int flag) |
| 179 | { |
| 180 | if (test_and_set_bit(flag, &dev->todo)) |
| 181 | return; |
| 182 | if (!schedule_work(&dev->work)) |
| 183 | ERROR(dev, "kevent %d may have been dropped\n", flag); |
| 184 | else |
| 185 | DBG(dev, "kevent %d scheduled\n", flag); |
| 186 | } |
| 187 | |
| 188 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
| 189 | |
| 190 | static int |
| 191 | rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) |
| 192 | { |
| 193 | struct sk_buff *skb; |
| 194 | int retval = -ENOMEM; |
| 195 | size_t size = 0; |
| 196 | struct usb_ep *out; |
| 197 | unsigned long flags; |
| 198 | |
| 199 | spin_lock_irqsave(&dev->lock, flags); |
| 200 | if (dev->port_usb) |
| 201 | out = dev->port_usb->out_ep; |
| 202 | else |
| 203 | out = NULL; |
| 204 | spin_unlock_irqrestore(&dev->lock, flags); |
| 205 | |
| 206 | if (!out) |
| 207 | return -ENOTCONN; |
| 208 | |
| 209 | |
| 210 | /* Padding up to RX_EXTRA handles minor disagreements with host. |
| 211 | * Normally we use the USB "terminate on short read" convention; |
| 212 | * so allow up to (N*maxpacket), since that memory is normally |
| 213 | * already allocated. Some hardware doesn't deal well with short |
| 214 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 215 | * byte off the end (to force hardware errors on overflow). |
| 216 | * |
| 217 | * RNDIS uses internal framing, and explicitly allows senders to |
| 218 | * pad to end-of-packet. That's potentially nice for speed, but |
| 219 | * means receivers can't recover lost synch on their own (because |
| 220 | * new packets don't only start after a short RX). |
| 221 | */ |
| 222 | size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA; |
| 223 | size += dev->port_usb->header_len; |
| 224 | size += out->maxpacket - 1; |
| 225 | size -= size % out->maxpacket; |
| 226 | |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 227 | if (dev->ul_max_pkts_per_xfer) |
| 228 | size *= dev->ul_max_pkts_per_xfer; |
| 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 | |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 233 | DBG(dev, "%s: size: %d\n", __func__, size); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 234 | skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); |
| 235 | if (skb == NULL) { |
| 236 | DBG(dev, "no rx skb\n"); |
| 237 | goto enomem; |
| 238 | } |
| 239 | |
| 240 | /* Some platforms perform better when IP packets are aligned, |
| 241 | * but on at least one, checksumming fails otherwise. Note: |
| 242 | * RNDIS headers involve variable numbers of LE32 values. |
| 243 | */ |
| 244 | skb_reserve(skb, NET_IP_ALIGN); |
| 245 | |
| 246 | req->buf = skb->data; |
| 247 | req->length = size; |
| 248 | req->complete = rx_complete; |
| 249 | req->context = skb; |
| 250 | |
| 251 | retval = usb_ep_queue(out, req, gfp_flags); |
| 252 | if (retval == -ENOMEM) |
| 253 | enomem: |
| 254 | defer_kevent(dev, WORK_RX_MEMORY); |
| 255 | if (retval) { |
| 256 | DBG(dev, "rx submit --> %d\n", retval); |
| 257 | if (skb) |
| 258 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 259 | } |
| 260 | return retval; |
| 261 | } |
| 262 | |
| 263 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
| 264 | { |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 265 | struct sk_buff *skb = req->context; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 266 | struct eth_dev *dev = ep->driver_data; |
| 267 | int status = req->status; |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 268 | bool queue = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 269 | |
| 270 | switch (status) { |
| 271 | |
| 272 | /* normal completion */ |
| 273 | case 0: |
| 274 | skb_put(skb, req->actual); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 275 | |
| 276 | if (dev->unwrap) { |
| 277 | unsigned long flags; |
| 278 | |
| 279 | spin_lock_irqsave(&dev->lock, flags); |
| 280 | if (dev->port_usb) { |
| 281 | status = dev->unwrap(dev->port_usb, |
| 282 | skb, |
| 283 | &dev->rx_frames); |
| 284 | } else { |
| 285 | dev_kfree_skb_any(skb); |
| 286 | status = -ENOTCONN; |
| 287 | } |
| 288 | spin_unlock_irqrestore(&dev->lock, flags); |
| 289 | } else { |
| 290 | skb_queue_tail(&dev->rx_frames, skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 291 | } |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 292 | if (!status) |
| 293 | queue = 1; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 294 | break; |
| 295 | |
| 296 | /* software-driven interface shutdown */ |
| 297 | case -ECONNRESET: /* unlink */ |
| 298 | case -ESHUTDOWN: /* disconnect etc */ |
| 299 | VDBG(dev, "rx shutdown, code %d\n", status); |
| 300 | goto quiesce; |
| 301 | |
| 302 | /* for hardware automagic (such as pxa) */ |
| 303 | case -ECONNABORTED: /* endpoint reset */ |
| 304 | DBG(dev, "rx %s reset\n", ep->name); |
| 305 | defer_kevent(dev, WORK_RX_MEMORY); |
| 306 | quiesce: |
| 307 | dev_kfree_skb_any(skb); |
| 308 | goto clean; |
| 309 | |
| 310 | /* data overrun */ |
| 311 | case -EOVERFLOW: |
| 312 | dev->net->stats.rx_over_errors++; |
| 313 | /* FALLTHROUGH */ |
| 314 | |
| 315 | default: |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 316 | queue = 1; |
| 317 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 318 | dev->net->stats.rx_errors++; |
| 319 | DBG(dev, "rx status %d\n", status); |
| 320 | break; |
| 321 | } |
| 322 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 323 | clean: |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 324 | spin_lock(&dev->req_lock); |
| 325 | list_add(&req->list, &dev->rx_reqs); |
| 326 | spin_unlock(&dev->req_lock); |
| 327 | |
| 328 | if (queue) |
| 329 | queue_work(uether_wq, &dev->rx_work); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n) |
| 333 | { |
| 334 | unsigned i; |
| 335 | struct usb_request *req; |
| 336 | |
| 337 | if (!n) |
| 338 | return -ENOMEM; |
| 339 | |
| 340 | /* queue/recycle up to N requests */ |
| 341 | i = n; |
| 342 | list_for_each_entry(req, list, list) { |
| 343 | if (i-- == 0) |
| 344 | goto extra; |
| 345 | } |
| 346 | while (i--) { |
| 347 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| 348 | if (!req) |
| 349 | return list_empty(list) ? -ENOMEM : 0; |
| 350 | list_add(&req->list, list); |
| 351 | } |
| 352 | return 0; |
| 353 | |
| 354 | extra: |
| 355 | /* free extras */ |
| 356 | for (;;) { |
| 357 | struct list_head *next; |
| 358 | |
| 359 | next = req->list.next; |
| 360 | list_del(&req->list); |
| 361 | usb_ep_free_request(ep, req); |
| 362 | |
| 363 | if (next == list) |
| 364 | break; |
| 365 | |
| 366 | req = container_of(next, struct usb_request, list); |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n) |
| 372 | { |
| 373 | int status; |
| 374 | |
| 375 | spin_lock(&dev->req_lock); |
| 376 | status = prealloc(&dev->tx_reqs, link->in_ep, n); |
| 377 | if (status < 0) |
| 378 | goto fail; |
| 379 | status = prealloc(&dev->rx_reqs, link->out_ep, n); |
| 380 | if (status < 0) |
| 381 | goto fail; |
| 382 | goto done; |
| 383 | fail: |
| 384 | DBG(dev, "can't alloc requests\n"); |
| 385 | done: |
| 386 | spin_unlock(&dev->req_lock); |
| 387 | return status; |
| 388 | } |
| 389 | |
| 390 | static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags) |
| 391 | { |
| 392 | struct usb_request *req; |
| 393 | unsigned long flags; |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 394 | int req_cnt = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 395 | |
| 396 | /* fill unused rxq slots with some skb */ |
| 397 | spin_lock_irqsave(&dev->req_lock, flags); |
| 398 | while (!list_empty(&dev->rx_reqs)) { |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 399 | /* break the nexus of continuous completion and re-submission*/ |
| 400 | if (++req_cnt > qlen(dev->gadget)) |
| 401 | break; |
| 402 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 403 | req = container_of(dev->rx_reqs.next, |
| 404 | struct usb_request, list); |
| 405 | list_del_init(&req->list); |
| 406 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 407 | |
| 408 | if (rx_submit(dev, req, gfp_flags) < 0) { |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 409 | spin_lock_irqsave(&dev->req_lock, flags); |
| 410 | list_add(&req->list, &dev->rx_reqs); |
| 411 | spin_unlock_irqrestore(&dev->req_lock, flags); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 412 | defer_kevent(dev, WORK_RX_MEMORY); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | spin_lock_irqsave(&dev->req_lock, flags); |
| 417 | } |
| 418 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 419 | } |
| 420 | |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 421 | static void process_rx_w(struct work_struct *work) |
| 422 | { |
| 423 | struct eth_dev *dev = container_of(work, struct eth_dev, rx_work); |
| 424 | struct sk_buff *skb; |
| 425 | int status = 0; |
| 426 | |
| 427 | if (!dev->port_usb) |
| 428 | return; |
| 429 | |
| 430 | while ((skb = skb_dequeue(&dev->rx_frames))) { |
| 431 | if (status < 0 |
| 432 | || ETH_HLEN > skb->len |
| 433 | || skb->len > ETH_FRAME_LEN) { |
| 434 | dev->net->stats.rx_errors++; |
| 435 | dev->net->stats.rx_length_errors++; |
| 436 | DBG(dev, "rx length %d\n", skb->len); |
| 437 | dev_kfree_skb_any(skb); |
| 438 | continue; |
| 439 | } |
| 440 | skb->protocol = eth_type_trans(skb, dev->net); |
| 441 | dev->net->stats.rx_packets++; |
| 442 | dev->net->stats.rx_bytes += skb->len; |
| 443 | |
| 444 | status = netif_rx_ni(skb); |
| 445 | } |
| 446 | |
| 447 | if (netif_running(dev->net)) |
| 448 | rx_fill(dev, GFP_KERNEL); |
| 449 | } |
| 450 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 451 | static void eth_work(struct work_struct *work) |
| 452 | { |
| 453 | struct eth_dev *dev = container_of(work, struct eth_dev, work); |
| 454 | |
| 455 | if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) { |
| 456 | if (netif_running(dev->net)) |
| 457 | rx_fill(dev, GFP_KERNEL); |
| 458 | } |
| 459 | |
| 460 | if (dev->todo) |
| 461 | DBG(dev, "work done, flags = 0x%lx\n", dev->todo); |
| 462 | } |
| 463 | |
| 464 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 465 | { |
| 466 | struct sk_buff *skb = req->context; |
| 467 | struct eth_dev *dev = ep->driver_data; |
| 468 | |
| 469 | switch (req->status) { |
| 470 | default: |
| 471 | dev->net->stats.tx_errors++; |
| 472 | VDBG(dev, "tx err %d\n", req->status); |
| 473 | /* FALLTHROUGH */ |
| 474 | case -ECONNRESET: /* unlink */ |
| 475 | case -ESHUTDOWN: /* disconnect etc */ |
| 476 | break; |
| 477 | case 0: |
| 478 | dev->net->stats.tx_bytes += skb->len; |
| 479 | } |
| 480 | dev->net->stats.tx_packets++; |
| 481 | |
| 482 | spin_lock(&dev->req_lock); |
| 483 | list_add(&req->list, &dev->tx_reqs); |
| 484 | spin_unlock(&dev->req_lock); |
| 485 | dev_kfree_skb_any(skb); |
| 486 | |
| 487 | atomic_dec(&dev->tx_qlen); |
| 488 | if (netif_carrier_ok(dev->net)) |
| 489 | netif_wake_queue(dev->net); |
| 490 | } |
| 491 | |
| 492 | static inline int is_promisc(u16 cdc_filter) |
| 493 | { |
| 494 | return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 495 | } |
| 496 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 497 | static netdev_tx_t eth_start_xmit(struct sk_buff *skb, |
| 498 | struct net_device *net) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 499 | { |
| 500 | struct eth_dev *dev = netdev_priv(net); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 501 | int length = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 502 | int retval; |
| 503 | struct usb_request *req = NULL; |
| 504 | unsigned long flags; |
| 505 | struct usb_ep *in; |
| 506 | u16 cdc_filter; |
| 507 | |
| 508 | spin_lock_irqsave(&dev->lock, flags); |
| 509 | if (dev->port_usb) { |
| 510 | in = dev->port_usb->in_ep; |
| 511 | cdc_filter = dev->port_usb->cdc_filter; |
| 512 | } else { |
| 513 | in = NULL; |
| 514 | cdc_filter = 0; |
| 515 | } |
| 516 | spin_unlock_irqrestore(&dev->lock, flags); |
| 517 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 518 | if (skb && !in) { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 519 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 520 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* apply outgoing CDC or RNDIS filters */ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 524 | if (skb && !is_promisc(cdc_filter)) { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 525 | u8 *dest = skb->data; |
| 526 | |
| 527 | if (is_multicast_ether_addr(dest)) { |
| 528 | u16 type; |
| 529 | |
| 530 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 531 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 532 | */ |
| 533 | if (is_broadcast_ether_addr(dest)) |
| 534 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 535 | else |
| 536 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 537 | if (!(cdc_filter & type)) { |
| 538 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 539 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 543 | } |
| 544 | |
| 545 | spin_lock_irqsave(&dev->req_lock, flags); |
| 546 | /* |
| 547 | * this freelist can be empty if an interrupt triggered disconnect() |
| 548 | * and reconfigured the gadget (shutting down this queue) after the |
| 549 | * network stack decided to xmit but before we got the spinlock. |
| 550 | */ |
| 551 | if (list_empty(&dev->tx_reqs)) { |
| 552 | spin_unlock_irqrestore(&dev->req_lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 553 | return NETDEV_TX_BUSY; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | req = container_of(dev->tx_reqs.next, struct usb_request, list); |
| 557 | list_del(&req->list); |
| 558 | |
| 559 | /* temporarily stop TX queue when the freelist empties */ |
| 560 | if (list_empty(&dev->tx_reqs)) |
| 561 | netif_stop_queue(net); |
| 562 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 563 | |
| 564 | /* no buffer copies needed, unless the network stack did it |
| 565 | * or the hardware can't use skb buffers. |
| 566 | * or there's not enough space for extra headers we need |
| 567 | */ |
| 568 | if (dev->wrap) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 569 | unsigned long flags; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 570 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 571 | spin_lock_irqsave(&dev->lock, flags); |
| 572 | if (dev->port_usb) |
| 573 | skb = dev->wrap(dev->port_usb, skb); |
| 574 | spin_unlock_irqrestore(&dev->lock, flags); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 575 | if (!skb) { |
| 576 | /* Multi frame CDC protocols may store the frame for |
| 577 | * later which is not a dropped frame. |
| 578 | */ |
| 579 | if (dev->port_usb->supports_multi_frame) |
| 580 | goto multiframe; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 581 | goto drop; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 582 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 583 | } |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 584 | |
| 585 | length = skb->len; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 586 | req->buf = skb->data; |
| 587 | req->context = skb; |
| 588 | req->complete = tx_complete; |
| 589 | |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 590 | /* NCM requires no zlp if transfer is dwNtbInMaxSize */ |
| 591 | if (dev->port_usb->is_fixed && |
| 592 | length == dev->port_usb->fixed_in_len && |
| 593 | (length % in->maxpacket) == 0) |
| 594 | req->zero = 0; |
| 595 | else |
| 596 | req->zero = 1; |
| 597 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 598 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 599 | * though any robust network rx path ignores extra padding. |
| 600 | * and some hardware doesn't like to write zlps. |
| 601 | */ |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 602 | if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 603 | length++; |
| 604 | |
| 605 | req->length = length; |
| 606 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 607 | /* throttle high/super speed IRQ rate back slightly */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 608 | if (gadget_is_dualspeed(dev->gadget)) |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 609 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH || |
| 610 | dev->gadget->speed == USB_SPEED_SUPER) |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 611 | ? ((atomic_read(&dev->tx_qlen) % dev->qmult) != 0) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 612 | : 0; |
| 613 | |
| 614 | retval = usb_ep_queue(in, req, GFP_ATOMIC); |
| 615 | switch (retval) { |
| 616 | default: |
| 617 | DBG(dev, "tx queue err %d\n", retval); |
| 618 | break; |
| 619 | case 0: |
Florian Westphal | 860e953 | 2016-05-03 16:33:13 +0200 | [diff] [blame] | 620 | netif_trans_update(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 621 | atomic_inc(&dev->tx_qlen); |
| 622 | } |
| 623 | |
| 624 | if (retval) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 625 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 626 | drop: |
| 627 | dev->net->stats.tx_dropped++; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 628 | multiframe: |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 629 | spin_lock_irqsave(&dev->req_lock, flags); |
| 630 | if (list_empty(&dev->tx_reqs)) |
| 631 | netif_start_queue(net); |
| 632 | list_add(&req->list, &dev->tx_reqs); |
| 633 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 634 | } |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 635 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /*-------------------------------------------------------------------------*/ |
| 639 | |
| 640 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) |
| 641 | { |
| 642 | DBG(dev, "%s\n", __func__); |
| 643 | |
| 644 | /* fill the rx queue */ |
| 645 | rx_fill(dev, gfp_flags); |
| 646 | |
| 647 | /* and open the tx floodgates */ |
| 648 | atomic_set(&dev->tx_qlen, 0); |
| 649 | netif_wake_queue(dev->net); |
| 650 | } |
| 651 | |
| 652 | static int eth_open(struct net_device *net) |
| 653 | { |
| 654 | struct eth_dev *dev = netdev_priv(net); |
| 655 | struct gether *link; |
| 656 | |
| 657 | DBG(dev, "%s\n", __func__); |
| 658 | if (netif_carrier_ok(dev->net)) |
| 659 | eth_start(dev, GFP_KERNEL); |
| 660 | |
| 661 | spin_lock_irq(&dev->lock); |
| 662 | link = dev->port_usb; |
| 663 | if (link && link->open) |
| 664 | link->open(link); |
| 665 | spin_unlock_irq(&dev->lock); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int eth_stop(struct net_device *net) |
| 671 | { |
| 672 | struct eth_dev *dev = netdev_priv(net); |
| 673 | unsigned long flags; |
| 674 | |
| 675 | VDBG(dev, "%s\n", __func__); |
| 676 | netif_stop_queue(net); |
| 677 | |
| 678 | DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", |
| 679 | dev->net->stats.rx_packets, dev->net->stats.tx_packets, |
| 680 | dev->net->stats.rx_errors, dev->net->stats.tx_errors |
| 681 | ); |
| 682 | |
| 683 | /* ensure there are no more active requests */ |
| 684 | spin_lock_irqsave(&dev->lock, flags); |
| 685 | if (dev->port_usb) { |
| 686 | struct gether *link = dev->port_usb; |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 687 | const struct usb_endpoint_descriptor *in; |
| 688 | const struct usb_endpoint_descriptor *out; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 689 | |
| 690 | if (link->close) |
| 691 | link->close(link); |
| 692 | |
| 693 | /* NOTE: we have no abort-queue primitive we could use |
| 694 | * to cancel all pending I/O. Instead, we disable then |
| 695 | * reenable the endpoints ... this idiom may leave toggle |
| 696 | * wrong, but that's a self-correcting error. |
| 697 | * |
| 698 | * REVISIT: we *COULD* just let the transfers complete at |
| 699 | * their own pace; the network stack can handle old packets. |
| 700 | * For the moment we leave this here, since it works. |
| 701 | */ |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 702 | in = link->in_ep->desc; |
| 703 | out = link->out_ep->desc; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 704 | usb_ep_disable(link->in_ep); |
| 705 | usb_ep_disable(link->out_ep); |
| 706 | if (netif_carrier_ok(net)) { |
| 707 | DBG(dev, "host still using in/out endpoints\n"); |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 708 | link->in_ep->desc = in; |
| 709 | link->out_ep->desc = out; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 710 | usb_ep_enable(link->in_ep); |
| 711 | usb_ep_enable(link->out_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | spin_unlock_irqrestore(&dev->lock, flags); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /*-------------------------------------------------------------------------*/ |
| 720 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 721 | static int get_ether_addr(const char *str, u8 *dev_addr) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 722 | { |
| 723 | if (str) { |
| 724 | unsigned i; |
| 725 | |
| 726 | for (i = 0; i < 6; i++) { |
| 727 | unsigned char num; |
| 728 | |
| 729 | if ((*str == '.') || (*str == ':')) |
| 730 | str++; |
Andy Shevchenko | e644814 | 2010-06-15 17:04:44 +0300 | [diff] [blame] | 731 | num = hex_to_bin(*str++) << 4; |
| 732 | num |= hex_to_bin(*str++); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 733 | dev_addr [i] = num; |
| 734 | } |
| 735 | if (is_valid_ether_addr(dev_addr)) |
| 736 | return 0; |
| 737 | } |
Joe Perches | 006c913 | 2012-07-12 22:33:11 -0700 | [diff] [blame] | 738 | eth_random_addr(dev_addr); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 739 | return 1; |
| 740 | } |
| 741 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 742 | static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len) |
| 743 | { |
| 744 | if (len < 18) |
| 745 | return -EINVAL; |
| 746 | |
Andy Shevchenko | 27f3870 | 2015-01-15 13:40:04 +0200 | [diff] [blame] | 747 | snprintf(str, len, "%pM", dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 748 | return 18; |
| 749 | } |
| 750 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 751 | static const struct net_device_ops eth_netdev_ops = { |
| 752 | .ndo_open = eth_open, |
| 753 | .ndo_stop = eth_stop, |
| 754 | .ndo_start_xmit = eth_start_xmit, |
| 755 | .ndo_change_mtu = ueth_change_mtu, |
| 756 | .ndo_set_mac_address = eth_mac_addr, |
| 757 | .ndo_validate_addr = eth_validate_addr, |
| 758 | }; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 759 | |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 760 | static struct device_type gadget_type = { |
| 761 | .name = "gadget", |
| 762 | }; |
| 763 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 764 | /** |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 765 | * gether_setup_name - initialize one ethernet-over-usb link |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 766 | * @g: gadget to associated with these links |
| 767 | * @ethaddr: NULL, or a buffer in which the ethernet address of the |
| 768 | * host side of the link is recorded |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 769 | * @netname: name for network device (for example, "usb") |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 770 | * Context: may sleep |
| 771 | * |
| 772 | * This sets up the single network link that may be exported by a |
| 773 | * gadget driver using this framework. The link layer addresses are |
| 774 | * set up using module parameters. |
| 775 | * |
Dan Carpenter | 574f24f | 2013-11-14 11:42:11 +0300 | [diff] [blame] | 776 | * Returns an eth_dev pointer on success, or an ERR_PTR on failure. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 777 | */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 778 | struct eth_dev *gether_setup_name(struct usb_gadget *g, |
| 779 | const char *dev_addr, const char *host_addr, |
| 780 | u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 781 | { |
| 782 | struct eth_dev *dev; |
| 783 | struct net_device *net; |
| 784 | int status; |
| 785 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 786 | net = alloc_etherdev(sizeof *dev); |
| 787 | if (!net) |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 788 | return ERR_PTR(-ENOMEM); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 789 | |
| 790 | dev = netdev_priv(net); |
| 791 | spin_lock_init(&dev->lock); |
| 792 | spin_lock_init(&dev->req_lock); |
| 793 | INIT_WORK(&dev->work, eth_work); |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 794 | INIT_WORK(&dev->rx_work, process_rx_w); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 795 | INIT_LIST_HEAD(&dev->tx_reqs); |
| 796 | INIT_LIST_HEAD(&dev->rx_reqs); |
| 797 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 798 | skb_queue_head_init(&dev->rx_frames); |
| 799 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 800 | /* network device setup */ |
| 801 | dev->net = net; |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 802 | dev->qmult = qmult; |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 803 | snprintf(net->name, sizeof(net->name), "%s%%d", netname); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 804 | |
| 805 | if (get_ether_addr(dev_addr, net->dev_addr)) |
| 806 | dev_warn(&g->dev, |
| 807 | "using random %s ethernet address\n", "self"); |
| 808 | if (get_ether_addr(host_addr, dev->host_mac)) |
| 809 | dev_warn(&g->dev, |
| 810 | "using random %s ethernet address\n", "host"); |
| 811 | |
| 812 | if (ethaddr) |
| 813 | memcpy(ethaddr, dev->host_mac, ETH_ALEN); |
| 814 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 815 | net->netdev_ops = ð_netdev_ops; |
| 816 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 817 | net->ethtool_ops = &ops; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 818 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 819 | dev->gadget = g; |
| 820 | SET_NETDEV_DEV(net, &g->dev); |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 821 | SET_NETDEV_DEVTYPE(net, &gadget_type); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 822 | |
| 823 | status = register_netdev(net); |
| 824 | if (status < 0) { |
| 825 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); |
| 826 | free_netdev(net); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 827 | dev = ERR_PTR(status); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 828 | } else { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 829 | INFO(dev, "MAC %pM\n", net->dev_addr); |
| 830 | INFO(dev, "HOST MAC %pM\n", dev->host_mac); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 831 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 832 | /* |
| 833 | * two kinds of host-initiated state changes: |
Kevin Cernekee | 31bde1c | 2012-06-24 21:11:22 -0700 | [diff] [blame] | 834 | * - iff DATA transfer is active, carrier is "on" |
| 835 | * - tx queueing enabled if open *and* carrier is "on" |
| 836 | */ |
| 837 | netif_carrier_off(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 840 | return dev; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 841 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 842 | EXPORT_SYMBOL_GPL(gether_setup_name); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 843 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 844 | struct net_device *gether_setup_name_default(const char *netname) |
| 845 | { |
| 846 | struct net_device *net; |
| 847 | struct eth_dev *dev; |
| 848 | |
| 849 | net = alloc_etherdev(sizeof(*dev)); |
| 850 | if (!net) |
| 851 | return ERR_PTR(-ENOMEM); |
| 852 | |
| 853 | dev = netdev_priv(net); |
| 854 | spin_lock_init(&dev->lock); |
| 855 | spin_lock_init(&dev->req_lock); |
| 856 | INIT_WORK(&dev->work, eth_work); |
| 857 | INIT_LIST_HEAD(&dev->tx_reqs); |
| 858 | INIT_LIST_HEAD(&dev->rx_reqs); |
| 859 | |
| 860 | skb_queue_head_init(&dev->rx_frames); |
| 861 | |
| 862 | /* network device setup */ |
| 863 | dev->net = net; |
| 864 | dev->qmult = QMULT_DEFAULT; |
| 865 | snprintf(net->name, sizeof(net->name), "%s%%d", netname); |
| 866 | |
| 867 | eth_random_addr(dev->dev_mac); |
| 868 | pr_warn("using random %s ethernet address\n", "self"); |
| 869 | eth_random_addr(dev->host_mac); |
| 870 | pr_warn("using random %s ethernet address\n", "host"); |
| 871 | |
| 872 | net->netdev_ops = ð_netdev_ops; |
| 873 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 874 | net->ethtool_ops = &ops; |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 875 | SET_NETDEV_DEVTYPE(net, &gadget_type); |
| 876 | |
| 877 | return net; |
| 878 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 879 | EXPORT_SYMBOL_GPL(gether_setup_name_default); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 880 | |
| 881 | int gether_register_netdev(struct net_device *net) |
| 882 | { |
| 883 | struct eth_dev *dev; |
| 884 | struct usb_gadget *g; |
| 885 | struct sockaddr sa; |
| 886 | int status; |
| 887 | |
| 888 | if (!net->dev.parent) |
| 889 | return -EINVAL; |
| 890 | dev = netdev_priv(net); |
| 891 | g = dev->gadget; |
| 892 | status = register_netdev(net); |
| 893 | if (status < 0) { |
| 894 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); |
| 895 | return status; |
| 896 | } else { |
| 897 | INFO(dev, "HOST MAC %pM\n", dev->host_mac); |
| 898 | |
| 899 | /* two kinds of host-initiated state changes: |
| 900 | * - iff DATA transfer is active, carrier is "on" |
| 901 | * - tx queueing enabled if open *and* carrier is "on" |
| 902 | */ |
| 903 | netif_carrier_off(net); |
| 904 | } |
| 905 | sa.sa_family = net->type; |
| 906 | memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN); |
| 907 | rtnl_lock(); |
| 908 | status = dev_set_mac_address(net, &sa); |
| 909 | rtnl_unlock(); |
| 910 | if (status) |
| 911 | pr_warn("cannot set self ethernet address: %d\n", status); |
| 912 | else |
| 913 | INFO(dev, "MAC %pM\n", dev->dev_mac); |
| 914 | |
| 915 | return status; |
| 916 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 917 | EXPORT_SYMBOL_GPL(gether_register_netdev); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 918 | |
| 919 | void gether_set_gadget(struct net_device *net, struct usb_gadget *g) |
| 920 | { |
| 921 | struct eth_dev *dev; |
| 922 | |
| 923 | dev = netdev_priv(net); |
| 924 | dev->gadget = g; |
| 925 | SET_NETDEV_DEV(net, &g->dev); |
| 926 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 927 | EXPORT_SYMBOL_GPL(gether_set_gadget); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 928 | |
| 929 | int gether_set_dev_addr(struct net_device *net, const char *dev_addr) |
| 930 | { |
| 931 | struct eth_dev *dev; |
| 932 | u8 new_addr[ETH_ALEN]; |
| 933 | |
| 934 | dev = netdev_priv(net); |
| 935 | if (get_ether_addr(dev_addr, new_addr)) |
| 936 | return -EINVAL; |
| 937 | memcpy(dev->dev_mac, new_addr, ETH_ALEN); |
| 938 | return 0; |
| 939 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 940 | EXPORT_SYMBOL_GPL(gether_set_dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 941 | |
| 942 | int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len) |
| 943 | { |
| 944 | struct eth_dev *dev; |
| 945 | |
| 946 | dev = netdev_priv(net); |
| 947 | return get_ether_addr_str(dev->dev_mac, dev_addr, len); |
| 948 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 949 | EXPORT_SYMBOL_GPL(gether_get_dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 950 | |
| 951 | int gether_set_host_addr(struct net_device *net, const char *host_addr) |
| 952 | { |
| 953 | struct eth_dev *dev; |
| 954 | u8 new_addr[ETH_ALEN]; |
| 955 | |
| 956 | dev = netdev_priv(net); |
| 957 | if (get_ether_addr(host_addr, new_addr)) |
| 958 | return -EINVAL; |
| 959 | memcpy(dev->host_mac, new_addr, ETH_ALEN); |
| 960 | return 0; |
| 961 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 962 | EXPORT_SYMBOL_GPL(gether_set_host_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 963 | |
| 964 | int gether_get_host_addr(struct net_device *net, char *host_addr, int len) |
| 965 | { |
| 966 | struct eth_dev *dev; |
| 967 | |
| 968 | dev = netdev_priv(net); |
| 969 | return get_ether_addr_str(dev->host_mac, host_addr, len); |
| 970 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 971 | EXPORT_SYMBOL_GPL(gether_get_host_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 972 | |
| 973 | int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len) |
| 974 | { |
| 975 | struct eth_dev *dev; |
| 976 | |
| 977 | if (len < 13) |
| 978 | return -EINVAL; |
| 979 | |
| 980 | dev = netdev_priv(net); |
| 981 | snprintf(host_addr, len, "%pm", dev->host_mac); |
| 982 | |
| 983 | return strlen(host_addr); |
| 984 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 985 | EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 986 | |
Andrzej Pietrasiewicz | bf4277c | 2013-05-28 09:15:45 +0200 | [diff] [blame] | 987 | void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]) |
| 988 | { |
| 989 | struct eth_dev *dev; |
| 990 | |
| 991 | dev = netdev_priv(net); |
| 992 | memcpy(host_mac, dev->host_mac, ETH_ALEN); |
| 993 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 994 | EXPORT_SYMBOL_GPL(gether_get_host_addr_u8); |
Andrzej Pietrasiewicz | bf4277c | 2013-05-28 09:15:45 +0200 | [diff] [blame] | 995 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 996 | void gether_set_qmult(struct net_device *net, unsigned qmult) |
| 997 | { |
| 998 | struct eth_dev *dev; |
| 999 | |
| 1000 | dev = netdev_priv(net); |
| 1001 | dev->qmult = qmult; |
| 1002 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1003 | EXPORT_SYMBOL_GPL(gether_set_qmult); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1004 | |
| 1005 | unsigned gether_get_qmult(struct net_device *net) |
| 1006 | { |
| 1007 | struct eth_dev *dev; |
| 1008 | |
| 1009 | dev = netdev_priv(net); |
| 1010 | return dev->qmult; |
| 1011 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1012 | EXPORT_SYMBOL_GPL(gether_get_qmult); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1013 | |
| 1014 | int gether_get_ifname(struct net_device *net, char *name, int len) |
| 1015 | { |
| 1016 | rtnl_lock(); |
| 1017 | strlcpy(name, netdev_name(net), len); |
| 1018 | rtnl_unlock(); |
| 1019 | return strlen(name); |
| 1020 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1021 | EXPORT_SYMBOL_GPL(gether_get_ifname); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1022 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1023 | /** |
| 1024 | * gether_cleanup - remove Ethernet-over-USB device |
| 1025 | * Context: may sleep |
| 1026 | * |
| 1027 | * This is called to free all resources allocated by @gether_setup(). |
| 1028 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1029 | void gether_cleanup(struct eth_dev *dev) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1030 | { |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1031 | if (!dev) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1032 | return; |
| 1033 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1034 | unregister_netdev(dev->net); |
| 1035 | flush_work(&dev->work); |
| 1036 | free_netdev(dev->net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1037 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1038 | EXPORT_SYMBOL_GPL(gether_cleanup); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1039 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1040 | /** |
| 1041 | * gether_connect - notify network layer that USB link is active |
| 1042 | * @link: the USB link, set up with endpoints, descriptors matching |
| 1043 | * current device speed, and any framing wrapper(s) set up. |
| 1044 | * Context: irqs blocked |
| 1045 | * |
| 1046 | * This is called to activate endpoints and let the network layer know |
| 1047 | * the connection is active ("carrier detect"). It may cause the I/O |
| 1048 | * queues to open and start letting network packets flow, but will in |
| 1049 | * any case activate the endpoints so that they respond properly to the |
| 1050 | * USB host. |
| 1051 | * |
| 1052 | * Verify net_device pointer returned using IS_ERR(). If it doesn't |
| 1053 | * indicate some error code (negative errno), ep->driver_data values |
| 1054 | * have been overwritten. |
| 1055 | */ |
| 1056 | struct net_device *gether_connect(struct gether *link) |
| 1057 | { |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1058 | struct eth_dev *dev = link->ioport; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1059 | int result = 0; |
| 1060 | |
| 1061 | if (!dev) |
| 1062 | return ERR_PTR(-EINVAL); |
| 1063 | |
| 1064 | link->in_ep->driver_data = dev; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1065 | result = usb_ep_enable(link->in_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1066 | if (result != 0) { |
| 1067 | DBG(dev, "enable %s --> %d\n", |
| 1068 | link->in_ep->name, result); |
| 1069 | goto fail0; |
| 1070 | } |
| 1071 | |
| 1072 | link->out_ep->driver_data = dev; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1073 | result = usb_ep_enable(link->out_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1074 | if (result != 0) { |
| 1075 | DBG(dev, "enable %s --> %d\n", |
| 1076 | link->out_ep->name, result); |
| 1077 | goto fail1; |
| 1078 | } |
| 1079 | |
| 1080 | if (result == 0) |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1081 | result = alloc_requests(dev, link, qlen(dev->gadget, |
| 1082 | dev->qmult)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (result == 0) { |
| 1085 | dev->zlp = link->is_zlp_ok; |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1086 | DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1087 | |
| 1088 | dev->header_len = link->header_len; |
| 1089 | dev->unwrap = link->unwrap; |
| 1090 | dev->wrap = link->wrap; |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 1091 | dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1092 | |
| 1093 | spin_lock(&dev->lock); |
| 1094 | dev->port_usb = link; |
David Brownell | 29bac7b | 2008-09-06 21:33:49 -0700 | [diff] [blame] | 1095 | if (netif_running(dev->net)) { |
| 1096 | if (link->open) |
| 1097 | link->open(link); |
| 1098 | } else { |
| 1099 | if (link->close) |
| 1100 | link->close(link); |
| 1101 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1102 | spin_unlock(&dev->lock); |
| 1103 | |
| 1104 | netif_carrier_on(dev->net); |
| 1105 | if (netif_running(dev->net)) |
| 1106 | eth_start(dev, GFP_ATOMIC); |
| 1107 | |
| 1108 | /* on error, disable any endpoints */ |
| 1109 | } else { |
| 1110 | (void) usb_ep_disable(link->out_ep); |
| 1111 | fail1: |
| 1112 | (void) usb_ep_disable(link->in_ep); |
| 1113 | } |
| 1114 | fail0: |
| 1115 | /* caller is responsible for cleanup on error */ |
| 1116 | if (result < 0) |
| 1117 | return ERR_PTR(result); |
| 1118 | return dev->net; |
| 1119 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1120 | EXPORT_SYMBOL_GPL(gether_connect); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1121 | |
| 1122 | /** |
| 1123 | * gether_disconnect - notify network layer that USB link is inactive |
| 1124 | * @link: the USB link, on which gether_connect() was called |
| 1125 | * Context: irqs blocked |
| 1126 | * |
| 1127 | * This is called to deactivate endpoints and let the network layer know |
| 1128 | * the connection went inactive ("no carrier"). |
| 1129 | * |
| 1130 | * On return, the state is as if gether_connect() had never been called. |
| 1131 | * The endpoints are inactive, and accordingly without active USB I/O. |
| 1132 | * Pointers to endpoint descriptors and endpoint private data are nulled. |
| 1133 | */ |
| 1134 | void gether_disconnect(struct gether *link) |
| 1135 | { |
| 1136 | struct eth_dev *dev = link->ioport; |
| 1137 | struct usb_request *req; |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 1138 | struct sk_buff *skb; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1139 | |
| 1140 | WARN_ON(!dev); |
| 1141 | if (!dev) |
| 1142 | return; |
| 1143 | |
| 1144 | DBG(dev, "%s\n", __func__); |
| 1145 | |
| 1146 | netif_stop_queue(dev->net); |
| 1147 | netif_carrier_off(dev->net); |
| 1148 | |
| 1149 | /* disable endpoints, forcing (synchronous) completion |
| 1150 | * of all pending i/o. then free the request objects |
| 1151 | * and forget about the endpoints. |
| 1152 | */ |
| 1153 | usb_ep_disable(link->in_ep); |
| 1154 | spin_lock(&dev->req_lock); |
| 1155 | while (!list_empty(&dev->tx_reqs)) { |
| 1156 | req = container_of(dev->tx_reqs.next, |
| 1157 | struct usb_request, list); |
| 1158 | list_del(&req->list); |
| 1159 | |
| 1160 | spin_unlock(&dev->req_lock); |
| 1161 | usb_ep_free_request(link->in_ep, req); |
| 1162 | spin_lock(&dev->req_lock); |
| 1163 | } |
| 1164 | spin_unlock(&dev->req_lock); |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1165 | link->in_ep->desc = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1166 | |
| 1167 | usb_ep_disable(link->out_ep); |
| 1168 | spin_lock(&dev->req_lock); |
| 1169 | while (!list_empty(&dev->rx_reqs)) { |
| 1170 | req = container_of(dev->rx_reqs.next, |
| 1171 | struct usb_request, list); |
| 1172 | list_del(&req->list); |
| 1173 | |
| 1174 | spin_unlock(&dev->req_lock); |
| 1175 | usb_ep_free_request(link->out_ep, req); |
| 1176 | spin_lock(&dev->req_lock); |
| 1177 | } |
| 1178 | spin_unlock(&dev->req_lock); |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 1179 | |
| 1180 | spin_lock(&dev->rx_frames.lock); |
| 1181 | while ((skb = __skb_dequeue(&dev->rx_frames))) |
| 1182 | dev_kfree_skb_any(skb); |
| 1183 | spin_unlock(&dev->rx_frames.lock); |
| 1184 | |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1185 | link->out_ep->desc = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1186 | |
| 1187 | /* finish forgetting about this USB link episode */ |
| 1188 | dev->header_len = 0; |
| 1189 | dev->unwrap = NULL; |
| 1190 | dev->wrap = NULL; |
| 1191 | |
| 1192 | spin_lock(&dev->lock); |
| 1193 | dev->port_usb = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1194 | spin_unlock(&dev->lock); |
| 1195 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1196 | EXPORT_SYMBOL_GPL(gether_disconnect); |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1197 | |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 1198 | static int __init gether_init(void) |
| 1199 | { |
| 1200 | uether_wq = create_singlethread_workqueue("uether"); |
| 1201 | if (!uether_wq) { |
| 1202 | pr_err("%s: Unable to create workqueue: uether\n", __func__); |
| 1203 | return -ENOMEM; |
| 1204 | } |
| 1205 | return 0; |
| 1206 | } |
| 1207 | module_init(gether_init); |
| 1208 | |
| 1209 | static void __exit gether_exit(void) |
| 1210 | { |
| 1211 | destroy_workqueue(uether_wq); |
| 1212 | |
| 1213 | } |
| 1214 | module_exit(gether_exit); |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1215 | MODULE_AUTHOR("David Brownell"); |
Badhri Jagan Sridharan | 16a203f | 2014-09-24 18:58:23 -0700 | [diff] [blame^] | 1216 | MODULE_DESCRIPTION("ethernet over USB driver"); |
| 1217 | MODULE_LICENSE("GPL v2"); |