blob: 57b0e138141dfee6d6dd955da855143670515683 [file] [log] [blame]
David Brownell2b3d9422008-06-19 18:19:28 -07001/*
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 Brownell2b3d9422008-06-19 18:19:28 -070012 */
13
14/* #define VERBOSE_DEBUG */
15
16#include <linux/kernel.h>
Sebastian Andrzej Siewior98303172012-09-10 16:30:50 +020017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
David Brownell2b3d9422008-06-19 18:19:28 -070019#include <linux/device.h>
20#include <linux/ctype.h>
21#include <linux/etherdevice.h>
22#include <linux/ethtool.h>
Ian Coolidge4fe5f072012-11-07 14:39:18 +000023#include <linux/if_vlan.h>
David Brownell2b3d9422008-06-19 18:19:28 -070024
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 Niebuhr9b39e9d2009-08-14 10:04:22 -050033 * 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 Brownell2b3d9422008-06-19 18:19:28 -070036 *
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 Brownell8a1ce2c2008-08-18 17:43:56 -070049#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070050
Mike Looijmansbba787a2015-08-05 08:54:55 +020051/* 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 Sridharana096ba52014-09-24 18:58:23 -070056static struct workqueue_struct *uether_wq;
57
David Brownell2b3d9422008-06-19 18:19:28 -070058struct eth_dev {
59 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070060 */
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;
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -070069 unsigned tx_qlen;
David Brownell2b3d9422008-06-19 18:19:28 -070070
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050071 struct sk_buff_head rx_frames;
72
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020073 unsigned qmult;
74
David Brownell2b3d9422008-06-19 18:19:28 -070075 unsigned header_len;
xerox_lin87bebf82014-08-14 14:48:44 +080076 unsigned ul_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050077 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 Brownell2b3d9422008-06-19 18:19:28 -070081
82 struct work_struct work;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -070083 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070084
85 unsigned long todo;
86#define WORK_RX_MEMORY 0
87
88 bool zlp;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +090089 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -070090 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +020091 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -070092};
93
94/*-------------------------------------------------------------------------*/
95
96#define RX_EXTRA 20 /* bytes guarding against rx overflows */
97
98#define DEFAULT_QLEN 2 /* double buffering by default */
99
Paul Zimmerman04617db2011-06-27 14:13:18 -0700100/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200101static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700102{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700103 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
104 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700105 return qmult * DEFAULT_QLEN;
106 else
107 return DEFAULT_QLEN;
108}
109
110/*-------------------------------------------------------------------------*/
111
112/* REVISIT there must be a better way than having two sets
113 * of debug calls ...
114 */
115
116#undef DBG
117#undef VDBG
118#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700119#undef INFO
120
121#define xprintk(d, level, fmt, args...) \
122 printk(level "%s: " fmt , (d)->net->name , ## args)
123
124#ifdef DEBUG
125#undef DEBUG
126#define DBG(dev, fmt, args...) \
127 xprintk(dev , KERN_DEBUG , fmt , ## args)
128#else
129#define DBG(dev, fmt, args...) \
130 do { } while (0)
131#endif /* DEBUG */
132
133#ifdef VERBOSE_DEBUG
134#define VDBG DBG
135#else
136#define VDBG(dev, fmt, args...) \
137 do { } while (0)
138#endif /* DEBUG */
139
140#define ERROR(dev, fmt, args...) \
141 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700142#define INFO(dev, fmt, args...) \
143 xprintk(dev , KERN_INFO , fmt , ## args)
144
145/*-------------------------------------------------------------------------*/
146
147/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
148
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800149static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700150{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100151 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
152 return -ERANGE;
153 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700154
Mike Looijmansab738ff2015-11-30 12:18:23 +0100155 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700156}
157
158static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
159{
Jiri Pirko7826d432013-01-06 00:44:26 +0000160 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700161
Jiri Pirko7826d432013-01-06 00:44:26 +0000162 strlcpy(p->driver, "g_ether", sizeof(p->driver));
163 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
164 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
165 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700166}
167
David Brownell2b3d9422008-06-19 18:19:28 -0700168/* REVISIT can also support:
169 * - WOL (by tracking suspends and issuing remote wakeup)
170 * - msglevel (implies updated messaging)
171 * - ... probably more ethtool ops
172 */
173
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700174static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700175 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700176 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700177};
178
179static void defer_kevent(struct eth_dev *dev, int flag)
180{
181 if (test_and_set_bit(flag, &dev->todo))
182 return;
183 if (!schedule_work(&dev->work))
184 ERROR(dev, "kevent %d may have been dropped\n", flag);
185 else
186 DBG(dev, "kevent %d scheduled\n", flag);
187}
188
189static void rx_complete(struct usb_ep *ep, struct usb_request *req);
190
191static int
192rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
193{
194 struct sk_buff *skb;
195 int retval = -ENOMEM;
196 size_t size = 0;
197 struct usb_ep *out;
198 unsigned long flags;
199
200 spin_lock_irqsave(&dev->lock, flags);
201 if (dev->port_usb)
202 out = dev->port_usb->out_ep;
203 else
204 out = NULL;
205 spin_unlock_irqrestore(&dev->lock, flags);
206
207 if (!out)
208 return -ENOTCONN;
209
210
211 /* Padding up to RX_EXTRA handles minor disagreements with host.
212 * Normally we use the USB "terminate on short read" convention;
213 * so allow up to (N*maxpacket), since that memory is normally
214 * already allocated. Some hardware doesn't deal well with short
215 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
216 * byte off the end (to force hardware errors on overflow).
217 *
218 * RNDIS uses internal framing, and explicitly allows senders to
219 * pad to end-of-packet. That's potentially nice for speed, but
220 * means receivers can't recover lost synch on their own (because
221 * new packets don't only start after a short RX).
222 */
223 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
224 size += dev->port_usb->header_len;
225 size += out->maxpacket - 1;
226 size -= size % out->maxpacket;
227
xerox_lin87bebf82014-08-14 14:48:44 +0800228 if (dev->ul_max_pkts_per_xfer)
229 size *= dev->ul_max_pkts_per_xfer;
230
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200231 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800232 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200233
xerox_lin87bebf82014-08-14 14:48:44 +0800234 DBG(dev, "%s: size: %d\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700235 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
236 if (skb == NULL) {
237 DBG(dev, "no rx skb\n");
238 goto enomem;
239 }
240
241 /* Some platforms perform better when IP packets are aligned,
242 * but on at least one, checksumming fails otherwise. Note:
243 * RNDIS headers involve variable numbers of LE32 values.
244 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900245 if (likely(!dev->no_skb_reserve))
246 skb_reserve(skb, NET_IP_ALIGN);
David Brownell2b3d9422008-06-19 18:19:28 -0700247
248 req->buf = skb->data;
249 req->length = size;
250 req->complete = rx_complete;
251 req->context = skb;
252
253 retval = usb_ep_queue(out, req, gfp_flags);
254 if (retval == -ENOMEM)
255enomem:
256 defer_kevent(dev, WORK_RX_MEMORY);
257 if (retval) {
258 DBG(dev, "rx submit --> %d\n", retval);
259 if (skb)
260 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700261 }
262 return retval;
263}
264
265static void rx_complete(struct usb_ep *ep, struct usb_request *req)
266{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700267 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700268 struct eth_dev *dev = ep->driver_data;
269 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700270 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700271
272 switch (status) {
273
274 /* normal completion */
275 case 0:
276 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500277
278 if (dev->unwrap) {
279 unsigned long flags;
280
281 spin_lock_irqsave(&dev->lock, flags);
282 if (dev->port_usb) {
283 status = dev->unwrap(dev->port_usb,
284 skb,
285 &dev->rx_frames);
286 } else {
287 dev_kfree_skb_any(skb);
288 status = -ENOTCONN;
289 }
290 spin_unlock_irqrestore(&dev->lock, flags);
291 } else {
292 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700293 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700294 if (!status)
295 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700296 break;
297
298 /* software-driven interface shutdown */
299 case -ECONNRESET: /* unlink */
300 case -ESHUTDOWN: /* disconnect etc */
301 VDBG(dev, "rx shutdown, code %d\n", status);
302 goto quiesce;
303
304 /* for hardware automagic (such as pxa) */
305 case -ECONNABORTED: /* endpoint reset */
306 DBG(dev, "rx %s reset\n", ep->name);
307 defer_kevent(dev, WORK_RX_MEMORY);
308quiesce:
309 dev_kfree_skb_any(skb);
310 goto clean;
311
312 /* data overrun */
313 case -EOVERFLOW:
314 dev->net->stats.rx_over_errors++;
315 /* FALLTHROUGH */
316
317 default:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700318 queue = 1;
319 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700320 dev->net->stats.rx_errors++;
321 DBG(dev, "rx status %d\n", status);
322 break;
323 }
324
David Brownell2b3d9422008-06-19 18:19:28 -0700325clean:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700326 spin_lock(&dev->req_lock);
327 list_add(&req->list, &dev->rx_reqs);
328 spin_unlock(&dev->req_lock);
329
330 if (queue)
331 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700332}
333
334static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
335{
336 unsigned i;
337 struct usb_request *req;
338
339 if (!n)
340 return -ENOMEM;
341
342 /* queue/recycle up to N requests */
343 i = n;
344 list_for_each_entry(req, list, list) {
345 if (i-- == 0)
346 goto extra;
347 }
348 while (i--) {
349 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
350 if (!req)
351 return list_empty(list) ? -ENOMEM : 0;
352 list_add(&req->list, list);
353 }
354 return 0;
355
356extra:
357 /* free extras */
358 for (;;) {
359 struct list_head *next;
360
361 next = req->list.next;
362 list_del(&req->list);
363 usb_ep_free_request(ep, req);
364
365 if (next == list)
366 break;
367
368 req = container_of(next, struct usb_request, list);
369 }
370 return 0;
371}
372
373static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
374{
375 int status;
376
377 spin_lock(&dev->req_lock);
378 status = prealloc(&dev->tx_reqs, link->in_ep, n);
379 if (status < 0)
380 goto fail;
381 status = prealloc(&dev->rx_reqs, link->out_ep, n);
382 if (status < 0)
383 goto fail;
384 goto done;
385fail:
386 DBG(dev, "can't alloc requests\n");
387done:
388 spin_unlock(&dev->req_lock);
389 return status;
390}
391
392static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
393{
394 struct usb_request *req;
395 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700396 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700397
398 /* fill unused rxq slots with some skb */
399 spin_lock_irqsave(&dev->req_lock, flags);
400 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700401 /* break the nexus of continuous completion and re-submission*/
402 if (++req_cnt > qlen(dev->gadget))
403 break;
404
David Brownell2b3d9422008-06-19 18:19:28 -0700405 req = container_of(dev->rx_reqs.next,
406 struct usb_request, list);
407 list_del_init(&req->list);
408 spin_unlock_irqrestore(&dev->req_lock, flags);
409
410 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700411 spin_lock_irqsave(&dev->req_lock, flags);
412 list_add(&req->list, &dev->rx_reqs);
413 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700414 defer_kevent(dev, WORK_RX_MEMORY);
415 return;
416 }
417
418 spin_lock_irqsave(&dev->req_lock, flags);
419 }
420 spin_unlock_irqrestore(&dev->req_lock, flags);
421}
422
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700423static void process_rx_w(struct work_struct *work)
424{
425 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
426 struct sk_buff *skb;
427 int status = 0;
428
429 if (!dev->port_usb)
430 return;
431
432 while ((skb = skb_dequeue(&dev->rx_frames))) {
433 if (status < 0
434 || ETH_HLEN > skb->len
435 || skb->len > ETH_FRAME_LEN) {
436 dev->net->stats.rx_errors++;
437 dev->net->stats.rx_length_errors++;
438 DBG(dev, "rx length %d\n", skb->len);
439 dev_kfree_skb_any(skb);
440 continue;
441 }
442 skb->protocol = eth_type_trans(skb, dev->net);
443 dev->net->stats.rx_packets++;
444 dev->net->stats.rx_bytes += skb->len;
445
446 status = netif_rx_ni(skb);
447 }
448
449 if (netif_running(dev->net))
450 rx_fill(dev, GFP_KERNEL);
451}
452
David Brownell2b3d9422008-06-19 18:19:28 -0700453static void eth_work(struct work_struct *work)
454{
455 struct eth_dev *dev = container_of(work, struct eth_dev, work);
456
457 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
458 if (netif_running(dev->net))
459 rx_fill(dev, GFP_KERNEL);
460 }
461
462 if (dev->todo)
463 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
464}
465
466static void tx_complete(struct usb_ep *ep, struct usb_request *req)
467{
468 struct sk_buff *skb = req->context;
469 struct eth_dev *dev = ep->driver_data;
470
471 switch (req->status) {
472 default:
473 dev->net->stats.tx_errors++;
474 VDBG(dev, "tx err %d\n", req->status);
475 /* FALLTHROUGH */
476 case -ECONNRESET: /* unlink */
477 case -ESHUTDOWN: /* disconnect etc */
478 break;
479 case 0:
480 dev->net->stats.tx_bytes += skb->len;
481 }
482 dev->net->stats.tx_packets++;
483
484 spin_lock(&dev->req_lock);
485 list_add(&req->list, &dev->tx_reqs);
486 spin_unlock(&dev->req_lock);
487 dev_kfree_skb_any(skb);
488
David Brownell2b3d9422008-06-19 18:19:28 -0700489 if (netif_carrier_ok(dev->net))
490 netif_wake_queue(dev->net);
491}
492
493static inline int is_promisc(u16 cdc_filter)
494{
495 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
496}
497
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000498static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
499 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700500{
501 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100502 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700503 int retval;
504 struct usb_request *req = NULL;
505 unsigned long flags;
506 struct usb_ep *in;
507 u16 cdc_filter;
508
509 spin_lock_irqsave(&dev->lock, flags);
510 if (dev->port_usb) {
511 in = dev->port_usb->in_ep;
512 cdc_filter = dev->port_usb->cdc_filter;
513 } else {
514 in = NULL;
515 cdc_filter = 0;
516 }
517 spin_unlock_irqrestore(&dev->lock, flags);
518
Jim Baxter6d3865f2014-07-07 18:33:18 +0100519 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700520 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000521 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700522 }
523
524 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100525 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700526 u8 *dest = skb->data;
527
528 if (is_multicast_ether_addr(dest)) {
529 u16 type;
530
531 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
532 * SET_ETHERNET_MULTICAST_FILTERS requests
533 */
534 if (is_broadcast_ether_addr(dest))
535 type = USB_CDC_PACKET_TYPE_BROADCAST;
536 else
537 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
538 if (!(cdc_filter & type)) {
539 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000540 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700541 }
542 }
543 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
544 }
545
546 spin_lock_irqsave(&dev->req_lock, flags);
547 /*
548 * this freelist can be empty if an interrupt triggered disconnect()
549 * and reconfigured the gadget (shutting down this queue) after the
550 * network stack decided to xmit but before we got the spinlock.
551 */
552 if (list_empty(&dev->tx_reqs)) {
553 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000554 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700555 }
556
557 req = container_of(dev->tx_reqs.next, struct usb_request, list);
558 list_del(&req->list);
559
560 /* temporarily stop TX queue when the freelist empties */
561 if (list_empty(&dev->tx_reqs))
562 netif_stop_queue(net);
563 spin_unlock_irqrestore(&dev->req_lock, flags);
564
565 /* no buffer copies needed, unless the network stack did it
566 * or the hardware can't use skb buffers.
567 * or there's not enough space for extra headers we need
568 */
569 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500570 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700571
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500572 spin_lock_irqsave(&dev->lock, flags);
573 if (dev->port_usb)
574 skb = dev->wrap(dev->port_usb, skb);
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200575 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100576 if (!skb) {
577 /* Multi frame CDC protocols may store the frame for
578 * later which is not a dropped frame.
579 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800580 if (dev->port_usb &&
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200581 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100582 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700583 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100584 }
David Brownell2b3d9422008-06-19 18:19:28 -0700585 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100586
587 length = skb->len;
David Brownell2b3d9422008-06-19 18:19:28 -0700588 req->buf = skb->data;
589 req->context = skb;
590 req->complete = tx_complete;
591
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200592 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200593 if (dev->port_usb &&
594 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200595 length == dev->port_usb->fixed_in_len &&
596 (length % in->maxpacket) == 0)
597 req->zero = 0;
598 else
599 req->zero = 1;
600
David Brownell2b3d9422008-06-19 18:19:28 -0700601 /* use zlp framing on tx for strict CDC-Ether conformance,
602 * though any robust network rx path ignores extra padding.
603 * and some hardware doesn't like to write zlps.
604 */
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200605 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700606 length++;
607
608 req->length = length;
609
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700610 /* throttle highspeed IRQ rate back slightly */
611 if (gadget_is_dualspeed(dev->gadget) &&
612 (dev->gadget->speed == USB_SPEED_HIGH)) {
613 dev->tx_qlen++;
614 if (dev->tx_qlen == qmult) {
615 req->no_interrupt = 0;
616 dev->tx_qlen = 0;
617 } else {
618 req->no_interrupt = 1;
619 }
620 } else {
621 req->no_interrupt = 0;
622 }
David Brownell2b3d9422008-06-19 18:19:28 -0700623
624 retval = usb_ep_queue(in, req, GFP_ATOMIC);
625 switch (retval) {
626 default:
627 DBG(dev, "tx queue err %d\n", retval);
628 break;
629 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200630 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700631 }
632
633 if (retval) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500634 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700635drop:
636 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100637multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700638 spin_lock_irqsave(&dev->req_lock, flags);
639 if (list_empty(&dev->tx_reqs))
640 netif_start_queue(net);
641 list_add(&req->list, &dev->tx_reqs);
642 spin_unlock_irqrestore(&dev->req_lock, flags);
643 }
Patrick McHardy6ed10652009-06-23 06:03:08 +0000644 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700645}
646
647/*-------------------------------------------------------------------------*/
648
649static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
650{
651 DBG(dev, "%s\n", __func__);
652
653 /* fill the rx queue */
654 rx_fill(dev, gfp_flags);
655
656 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700657 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700658 netif_wake_queue(dev->net);
659}
660
661static int eth_open(struct net_device *net)
662{
663 struct eth_dev *dev = netdev_priv(net);
664 struct gether *link;
665
666 DBG(dev, "%s\n", __func__);
667 if (netif_carrier_ok(dev->net))
668 eth_start(dev, GFP_KERNEL);
669
670 spin_lock_irq(&dev->lock);
671 link = dev->port_usb;
672 if (link && link->open)
673 link->open(link);
674 spin_unlock_irq(&dev->lock);
675
676 return 0;
677}
678
679static int eth_stop(struct net_device *net)
680{
681 struct eth_dev *dev = netdev_priv(net);
682 unsigned long flags;
683
684 VDBG(dev, "%s\n", __func__);
685 netif_stop_queue(net);
686
687 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
688 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
689 dev->net->stats.rx_errors, dev->net->stats.tx_errors
690 );
691
692 /* ensure there are no more active requests */
693 spin_lock_irqsave(&dev->lock, flags);
694 if (dev->port_usb) {
695 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200696 const struct usb_endpoint_descriptor *in;
697 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700698
699 if (link->close)
700 link->close(link);
701
702 /* NOTE: we have no abort-queue primitive we could use
703 * to cancel all pending I/O. Instead, we disable then
704 * reenable the endpoints ... this idiom may leave toggle
705 * wrong, but that's a self-correcting error.
706 *
707 * REVISIT: we *COULD* just let the transfers complete at
708 * their own pace; the network stack can handle old packets.
709 * For the moment we leave this here, since it works.
710 */
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200711 in = link->in_ep->desc;
712 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700713 usb_ep_disable(link->in_ep);
714 usb_ep_disable(link->out_ep);
715 if (netif_carrier_ok(net)) {
716 DBG(dev, "host still using in/out endpoints\n");
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200717 link->in_ep->desc = in;
718 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300719 usb_ep_enable(link->in_ep);
720 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700721 }
722 }
723 spin_unlock_irqrestore(&dev->lock, flags);
724
725 return 0;
726}
727
728/*-------------------------------------------------------------------------*/
729
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200730static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700731{
732 if (str) {
733 unsigned i;
734
735 for (i = 0; i < 6; i++) {
736 unsigned char num;
737
738 if ((*str == '.') || (*str == ':'))
739 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300740 num = hex_to_bin(*str++) << 4;
741 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700742 dev_addr [i] = num;
743 }
744 if (is_valid_ether_addr(dev_addr))
745 return 0;
746 }
Joe Perches006c9132012-07-12 22:33:11 -0700747 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700748 return 1;
749}
750
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200751static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
752{
753 if (len < 18)
754 return -EINVAL;
755
Andy Shevchenko27f38702015-01-15 13:40:04 +0200756 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200757 return 18;
758}
759
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800760static const struct net_device_ops eth_netdev_ops = {
761 .ndo_open = eth_open,
762 .ndo_stop = eth_stop,
763 .ndo_start_xmit = eth_start_xmit,
764 .ndo_change_mtu = ueth_change_mtu,
765 .ndo_set_mac_address = eth_mac_addr,
766 .ndo_validate_addr = eth_validate_addr,
767};
David Brownell2b3d9422008-06-19 18:19:28 -0700768
Marcel Holtmannaa790742010-01-15 22:13:58 -0800769static struct device_type gadget_type = {
770 .name = "gadget",
771};
772
David Brownell2b3d9422008-06-19 18:19:28 -0700773/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200774 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700775 * @g: gadget to associated with these links
776 * @ethaddr: NULL, or a buffer in which the ethernet address of the
777 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +0200778 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700779 * Context: may sleep
780 *
781 * This sets up the single network link that may be exported by a
782 * gadget driver using this framework. The link layer addresses are
783 * set up using module parameters.
784 *
Dan Carpenter574f24f2013-11-14 11:42:11 +0300785 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -0700786 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200787struct eth_dev *gether_setup_name(struct usb_gadget *g,
788 const char *dev_addr, const char *host_addr,
789 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -0700790{
791 struct eth_dev *dev;
792 struct net_device *net;
793 int status;
794
David Brownell2b3d9422008-06-19 18:19:28 -0700795 net = alloc_etherdev(sizeof *dev);
796 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100797 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700798
799 dev = netdev_priv(net);
800 spin_lock_init(&dev->lock);
801 spin_lock_init(&dev->req_lock);
802 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700803 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700804 INIT_LIST_HEAD(&dev->tx_reqs);
805 INIT_LIST_HEAD(&dev->rx_reqs);
806
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500807 skb_queue_head_init(&dev->rx_frames);
808
David Brownell2b3d9422008-06-19 18:19:28 -0700809 /* network device setup */
810 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200811 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200812 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700813
814 if (get_ether_addr(dev_addr, net->dev_addr))
815 dev_warn(&g->dev,
816 "using random %s ethernet address\n", "self");
817 if (get_ether_addr(host_addr, dev->host_mac))
818 dev_warn(&g->dev,
819 "using random %s ethernet address\n", "host");
820
821 if (ethaddr)
822 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
823
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800824 net->netdev_ops = &eth_netdev_ops;
825
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000826 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -0700827
David Brownell2b3d9422008-06-19 18:19:28 -0700828 dev->gadget = g;
829 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800830 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700831
832 status = register_netdev(net);
833 if (status < 0) {
834 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
835 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100836 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700837 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700838 INFO(dev, "MAC %pM\n", net->dev_addr);
839 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700840
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200841 /*
842 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700843 * - iff DATA transfer is active, carrier is "on"
844 * - tx queueing enabled if open *and* carrier is "on"
845 */
846 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700847 }
848
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100849 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700850}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500851EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700852
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200853struct net_device *gether_setup_name_default(const char *netname)
854{
855 struct net_device *net;
856 struct eth_dev *dev;
857
858 net = alloc_etherdev(sizeof(*dev));
859 if (!net)
860 return ERR_PTR(-ENOMEM);
861
862 dev = netdev_priv(net);
863 spin_lock_init(&dev->lock);
864 spin_lock_init(&dev->req_lock);
865 INIT_WORK(&dev->work, eth_work);
866 INIT_LIST_HEAD(&dev->tx_reqs);
867 INIT_LIST_HEAD(&dev->rx_reqs);
868
869 skb_queue_head_init(&dev->rx_frames);
870
871 /* network device setup */
872 dev->net = net;
873 dev->qmult = QMULT_DEFAULT;
874 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
875
876 eth_random_addr(dev->dev_mac);
877 pr_warn("using random %s ethernet address\n", "self");
878 eth_random_addr(dev->host_mac);
879 pr_warn("using random %s ethernet address\n", "host");
880
881 net->netdev_ops = &eth_netdev_ops;
882
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000883 net->ethtool_ops = &ops;
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200884 SET_NETDEV_DEVTYPE(net, &gadget_type);
885
886 return net;
887}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500888EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200889
890int gether_register_netdev(struct net_device *net)
891{
892 struct eth_dev *dev;
893 struct usb_gadget *g;
894 struct sockaddr sa;
895 int status;
896
897 if (!net->dev.parent)
898 return -EINVAL;
899 dev = netdev_priv(net);
900 g = dev->gadget;
901 status = register_netdev(net);
902 if (status < 0) {
903 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
904 return status;
905 } else {
906 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
907
908 /* two kinds of host-initiated state changes:
909 * - iff DATA transfer is active, carrier is "on"
910 * - tx queueing enabled if open *and* carrier is "on"
911 */
912 netif_carrier_off(net);
913 }
914 sa.sa_family = net->type;
915 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
916 rtnl_lock();
917 status = dev_set_mac_address(net, &sa);
918 rtnl_unlock();
919 if (status)
920 pr_warn("cannot set self ethernet address: %d\n", status);
921 else
922 INFO(dev, "MAC %pM\n", dev->dev_mac);
923
924 return status;
925}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500926EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200927
928void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
929{
930 struct eth_dev *dev;
931
932 dev = netdev_priv(net);
933 dev->gadget = g;
934 SET_NETDEV_DEV(net, &g->dev);
935}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500936EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200937
938int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
939{
940 struct eth_dev *dev;
941 u8 new_addr[ETH_ALEN];
942
943 dev = netdev_priv(net);
944 if (get_ether_addr(dev_addr, new_addr))
945 return -EINVAL;
946 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
947 return 0;
948}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500949EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200950
951int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
952{
953 struct eth_dev *dev;
954
955 dev = netdev_priv(net);
956 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
957}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500958EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200959
960int gether_set_host_addr(struct net_device *net, const char *host_addr)
961{
962 struct eth_dev *dev;
963 u8 new_addr[ETH_ALEN];
964
965 dev = netdev_priv(net);
966 if (get_ether_addr(host_addr, new_addr))
967 return -EINVAL;
968 memcpy(dev->host_mac, new_addr, ETH_ALEN);
969 return 0;
970}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500971EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200972
973int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
974{
975 struct eth_dev *dev;
976
977 dev = netdev_priv(net);
978 return get_ether_addr_str(dev->host_mac, host_addr, len);
979}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500980EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200981
982int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
983{
984 struct eth_dev *dev;
985
986 if (len < 13)
987 return -EINVAL;
988
989 dev = netdev_priv(net);
990 snprintf(host_addr, len, "%pm", dev->host_mac);
991
992 return strlen(host_addr);
993}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500994EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200995
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200996void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
997{
998 struct eth_dev *dev;
999
1000 dev = netdev_priv(net);
1001 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1002}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001003EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001004
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001005void gether_set_qmult(struct net_device *net, unsigned qmult)
1006{
1007 struct eth_dev *dev;
1008
1009 dev = netdev_priv(net);
1010 dev->qmult = qmult;
1011}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001012EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001013
1014unsigned gether_get_qmult(struct net_device *net)
1015{
1016 struct eth_dev *dev;
1017
1018 dev = netdev_priv(net);
1019 return dev->qmult;
1020}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001021EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001022
1023int gether_get_ifname(struct net_device *net, char *name, int len)
1024{
1025 rtnl_lock();
1026 strlcpy(name, netdev_name(net), len);
1027 rtnl_unlock();
1028 return strlen(name);
1029}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001030EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001031
David Brownell2b3d9422008-06-19 18:19:28 -07001032/**
1033 * gether_cleanup - remove Ethernet-over-USB device
1034 * Context: may sleep
1035 *
1036 * This is called to free all resources allocated by @gether_setup().
1037 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001038void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001039{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001040 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001041 return;
1042
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001043 unregister_netdev(dev->net);
1044 flush_work(&dev->work);
1045 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001046}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001047EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001048
David Brownell2b3d9422008-06-19 18:19:28 -07001049/**
1050 * gether_connect - notify network layer that USB link is active
1051 * @link: the USB link, set up with endpoints, descriptors matching
1052 * current device speed, and any framing wrapper(s) set up.
1053 * Context: irqs blocked
1054 *
1055 * This is called to activate endpoints and let the network layer know
1056 * the connection is active ("carrier detect"). It may cause the I/O
1057 * queues to open and start letting network packets flow, but will in
1058 * any case activate the endpoints so that they respond properly to the
1059 * USB host.
1060 *
1061 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1062 * indicate some error code (negative errno), ep->driver_data values
1063 * have been overwritten.
1064 */
1065struct net_device *gether_connect(struct gether *link)
1066{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001067 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001068 int result = 0;
1069
1070 if (!dev)
1071 return ERR_PTR(-EINVAL);
1072
1073 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001074 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001075 if (result != 0) {
1076 DBG(dev, "enable %s --> %d\n",
1077 link->in_ep->name, result);
1078 goto fail0;
1079 }
1080
1081 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001082 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001083 if (result != 0) {
1084 DBG(dev, "enable %s --> %d\n",
1085 link->out_ep->name, result);
1086 goto fail1;
1087 }
1088
1089 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001090 result = alloc_requests(dev, link, qlen(dev->gadget,
1091 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001092
1093 if (result == 0) {
1094 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001095 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001096 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001097
1098 dev->header_len = link->header_len;
1099 dev->unwrap = link->unwrap;
1100 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001101 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001102
1103 spin_lock(&dev->lock);
1104 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001105 if (netif_running(dev->net)) {
1106 if (link->open)
1107 link->open(link);
1108 } else {
1109 if (link->close)
1110 link->close(link);
1111 }
David Brownell2b3d9422008-06-19 18:19:28 -07001112 spin_unlock(&dev->lock);
1113
1114 netif_carrier_on(dev->net);
1115 if (netif_running(dev->net))
1116 eth_start(dev, GFP_ATOMIC);
1117
1118 /* on error, disable any endpoints */
1119 } else {
1120 (void) usb_ep_disable(link->out_ep);
1121fail1:
1122 (void) usb_ep_disable(link->in_ep);
1123 }
1124fail0:
1125 /* caller is responsible for cleanup on error */
1126 if (result < 0)
1127 return ERR_PTR(result);
1128 return dev->net;
1129}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001130EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001131
1132/**
1133 * gether_disconnect - notify network layer that USB link is inactive
1134 * @link: the USB link, on which gether_connect() was called
1135 * Context: irqs blocked
1136 *
1137 * This is called to deactivate endpoints and let the network layer know
1138 * the connection went inactive ("no carrier").
1139 *
1140 * On return, the state is as if gether_connect() had never been called.
1141 * The endpoints are inactive, and accordingly without active USB I/O.
1142 * Pointers to endpoint descriptors and endpoint private data are nulled.
1143 */
1144void gether_disconnect(struct gether *link)
1145{
1146 struct eth_dev *dev = link->ioport;
1147 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001148 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001149
1150 WARN_ON(!dev);
1151 if (!dev)
1152 return;
1153
1154 DBG(dev, "%s\n", __func__);
1155
1156 netif_stop_queue(dev->net);
1157 netif_carrier_off(dev->net);
1158
1159 /* disable endpoints, forcing (synchronous) completion
1160 * of all pending i/o. then free the request objects
1161 * and forget about the endpoints.
1162 */
1163 usb_ep_disable(link->in_ep);
1164 spin_lock(&dev->req_lock);
1165 while (!list_empty(&dev->tx_reqs)) {
1166 req = container_of(dev->tx_reqs.next,
1167 struct usb_request, list);
1168 list_del(&req->list);
1169
1170 spin_unlock(&dev->req_lock);
1171 usb_ep_free_request(link->in_ep, req);
1172 spin_lock(&dev->req_lock);
1173 }
1174 spin_unlock(&dev->req_lock);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001175 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001176
1177 usb_ep_disable(link->out_ep);
1178 spin_lock(&dev->req_lock);
1179 while (!list_empty(&dev->rx_reqs)) {
1180 req = container_of(dev->rx_reqs.next,
1181 struct usb_request, list);
1182 list_del(&req->list);
1183
1184 spin_unlock(&dev->req_lock);
1185 usb_ep_free_request(link->out_ep, req);
1186 spin_lock(&dev->req_lock);
1187 }
1188 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001189
1190 spin_lock(&dev->rx_frames.lock);
1191 while ((skb = __skb_dequeue(&dev->rx_frames)))
1192 dev_kfree_skb_any(skb);
1193 spin_unlock(&dev->rx_frames.lock);
1194
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001195 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001196
1197 /* finish forgetting about this USB link episode */
1198 dev->header_len = 0;
1199 dev->unwrap = NULL;
1200 dev->wrap = NULL;
1201
1202 spin_lock(&dev->lock);
1203 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001204 spin_unlock(&dev->lock);
1205}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001206EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001207
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001208static int __init gether_init(void)
1209{
1210 uether_wq = create_singlethread_workqueue("uether");
1211 if (!uether_wq) {
1212 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1213 return -ENOMEM;
1214 }
1215 return 0;
1216}
1217module_init(gether_init);
1218
1219static void __exit gether_exit(void)
1220{
1221 destroy_workqueue(uether_wq);
1222
1223}
1224module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001225MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001226MODULE_DESCRIPTION("ethernet over USB driver");
1227MODULE_LICENSE("GPL v2");