blob: 6516ce76fa35e12e7e09e6fc1c318b1190b847ec [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
David Brownell2b3d9422008-06-19 18:19:28 -070056struct eth_dev {
57 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070058 */
59 spinlock_t lock;
60 struct gether *port_usb;
61
62 struct net_device *net;
63 struct usb_gadget *gadget;
64
65 spinlock_t req_lock; /* guard {rx,tx}_reqs */
66 struct list_head tx_reqs, rx_reqs;
67 atomic_t tx_qlen;
68
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050069 struct sk_buff_head rx_frames;
70
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020071 unsigned qmult;
72
David Brownell2b3d9422008-06-19 18:19:28 -070073 unsigned header_len;
xerox_lin87bebf82014-08-14 14:48:44 +080074 unsigned ul_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050075 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
76 int (*unwrap)(struct gether *,
77 struct sk_buff *skb,
78 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070079
80 struct work_struct work;
81
82 unsigned long todo;
83#define WORK_RX_MEMORY 0
84
85 bool zlp;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +090086 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -070087 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +020088 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -070089};
90
91/*-------------------------------------------------------------------------*/
92
93#define RX_EXTRA 20 /* bytes guarding against rx overflows */
94
95#define DEFAULT_QLEN 2 /* double buffering by default */
96
Paul Zimmerman04617db2011-06-27 14:13:18 -070097/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020098static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -070099{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700100 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
101 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700102 return qmult * DEFAULT_QLEN;
103 else
104 return DEFAULT_QLEN;
105}
106
107/*-------------------------------------------------------------------------*/
108
109/* REVISIT there must be a better way than having two sets
110 * of debug calls ...
111 */
112
113#undef DBG
114#undef VDBG
115#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700116#undef INFO
117
118#define xprintk(d, level, fmt, args...) \
119 printk(level "%s: " fmt , (d)->net->name , ## args)
120
121#ifdef DEBUG
122#undef DEBUG
123#define DBG(dev, fmt, args...) \
124 xprintk(dev , KERN_DEBUG , fmt , ## args)
125#else
126#define DBG(dev, fmt, args...) \
127 do { } while (0)
128#endif /* DEBUG */
129
130#ifdef VERBOSE_DEBUG
131#define VDBG DBG
132#else
133#define VDBG(dev, fmt, args...) \
134 do { } while (0)
135#endif /* DEBUG */
136
137#define ERROR(dev, fmt, args...) \
138 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700139#define INFO(dev, fmt, args...) \
140 xprintk(dev , KERN_INFO , fmt , ## args)
141
142/*-------------------------------------------------------------------------*/
143
144/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
145
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800146static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700147{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100148 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
149 return -ERANGE;
150 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700151
Mike Looijmansab738ff2015-11-30 12:18:23 +0100152 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700153}
154
155static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
156{
Jiri Pirko7826d432013-01-06 00:44:26 +0000157 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700158
Jiri Pirko7826d432013-01-06 00:44:26 +0000159 strlcpy(p->driver, "g_ether", sizeof(p->driver));
160 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
161 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
162 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700163}
164
David Brownell2b3d9422008-06-19 18:19:28 -0700165/* REVISIT can also support:
166 * - WOL (by tracking suspends and issuing remote wakeup)
167 * - msglevel (implies updated messaging)
168 * - ... probably more ethtool ops
169 */
170
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700171static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700172 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700173 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700174};
175
176static void defer_kevent(struct eth_dev *dev, int flag)
177{
178 if (test_and_set_bit(flag, &dev->todo))
179 return;
180 if (!schedule_work(&dev->work))
181 ERROR(dev, "kevent %d may have been dropped\n", flag);
182 else
183 DBG(dev, "kevent %d scheduled\n", flag);
184}
185
186static void rx_complete(struct usb_ep *ep, struct usb_request *req);
187
188static int
189rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
190{
191 struct sk_buff *skb;
192 int retval = -ENOMEM;
193 size_t size = 0;
194 struct usb_ep *out;
195 unsigned long flags;
196
197 spin_lock_irqsave(&dev->lock, flags);
198 if (dev->port_usb)
199 out = dev->port_usb->out_ep;
200 else
201 out = NULL;
202 spin_unlock_irqrestore(&dev->lock, flags);
203
204 if (!out)
205 return -ENOTCONN;
206
207
208 /* Padding up to RX_EXTRA handles minor disagreements with host.
209 * Normally we use the USB "terminate on short read" convention;
210 * so allow up to (N*maxpacket), since that memory is normally
211 * already allocated. Some hardware doesn't deal well with short
212 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
213 * byte off the end (to force hardware errors on overflow).
214 *
215 * RNDIS uses internal framing, and explicitly allows senders to
216 * pad to end-of-packet. That's potentially nice for speed, but
217 * means receivers can't recover lost synch on their own (because
218 * new packets don't only start after a short RX).
219 */
220 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
221 size += dev->port_usb->header_len;
222 size += out->maxpacket - 1;
223 size -= size % out->maxpacket;
224
xerox_lin87bebf82014-08-14 14:48:44 +0800225 if (dev->ul_max_pkts_per_xfer)
226 size *= dev->ul_max_pkts_per_xfer;
227
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200228 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800229 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200230
xerox_lin87bebf82014-08-14 14:48:44 +0800231 DBG(dev, "%s: size: %d\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700232 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
233 if (skb == NULL) {
234 DBG(dev, "no rx skb\n");
235 goto enomem;
236 }
237
238 /* Some platforms perform better when IP packets are aligned,
239 * but on at least one, checksumming fails otherwise. Note:
240 * RNDIS headers involve variable numbers of LE32 values.
241 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900242 if (likely(!dev->no_skb_reserve))
243 skb_reserve(skb, NET_IP_ALIGN);
David Brownell2b3d9422008-06-19 18:19:28 -0700244
245 req->buf = skb->data;
246 req->length = size;
247 req->complete = rx_complete;
248 req->context = skb;
249
250 retval = usb_ep_queue(out, req, gfp_flags);
251 if (retval == -ENOMEM)
252enomem:
253 defer_kevent(dev, WORK_RX_MEMORY);
254 if (retval) {
255 DBG(dev, "rx submit --> %d\n", retval);
256 if (skb)
257 dev_kfree_skb_any(skb);
Felipe Balbi9189a332014-04-21 10:15:12 -0500258 spin_lock_irqsave(&dev->req_lock, flags);
259 list_add(&req->list, &dev->rx_reqs);
260 spin_unlock_irqrestore(&dev->req_lock, flags);
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{
Felipe Balbi9189a332014-04-21 10:15:12 -0500267 struct sk_buff *skb = req->context, *skb2;
David Brownell2b3d9422008-06-19 18:19:28 -0700268 struct eth_dev *dev = ep->driver_data;
269 int status = req->status;
270
271 switch (status) {
272
273 /* normal completion */
274 case 0:
275 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500276
277 if (dev->unwrap) {
278 unsigned long flags;
279
280 spin_lock_irqsave(&dev->lock, flags);
281 if (dev->port_usb) {
282 status = dev->unwrap(dev->port_usb,
283 skb,
284 &dev->rx_frames);
285 } else {
286 dev_kfree_skb_any(skb);
287 status = -ENOTCONN;
288 }
289 spin_unlock_irqrestore(&dev->lock, flags);
290 } else {
291 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700292 }
Felipe Balbi9189a332014-04-21 10:15:12 -0500293 skb = NULL;
294
295 skb2 = skb_dequeue(&dev->rx_frames);
296 while (skb2) {
297 if (status < 0
298 || ETH_HLEN > skb2->len
Mike Looijmansbba787a2015-08-05 08:54:55 +0200299 || skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
Felipe Balbi9189a332014-04-21 10:15:12 -0500300 dev->net->stats.rx_errors++;
301 dev->net->stats.rx_length_errors++;
302 DBG(dev, "rx length %d\n", skb2->len);
303 dev_kfree_skb_any(skb2);
304 goto next_frame;
305 }
306 skb2->protocol = eth_type_trans(skb2, dev->net);
307 dev->net->stats.rx_packets++;
308 dev->net->stats.rx_bytes += skb2->len;
309
310 /* no buffer copies needed, unless hardware can't
311 * use skb buffers.
312 */
313 status = netif_rx(skb2);
314next_frame:
315 skb2 = skb_dequeue(&dev->rx_frames);
316 }
David Brownell2b3d9422008-06-19 18:19:28 -0700317 break;
318
319 /* software-driven interface shutdown */
320 case -ECONNRESET: /* unlink */
321 case -ESHUTDOWN: /* disconnect etc */
322 VDBG(dev, "rx shutdown, code %d\n", status);
323 goto quiesce;
324
325 /* for hardware automagic (such as pxa) */
326 case -ECONNABORTED: /* endpoint reset */
327 DBG(dev, "rx %s reset\n", ep->name);
328 defer_kevent(dev, WORK_RX_MEMORY);
329quiesce:
330 dev_kfree_skb_any(skb);
331 goto clean;
332
333 /* data overrun */
334 case -EOVERFLOW:
335 dev->net->stats.rx_over_errors++;
336 /* FALLTHROUGH */
337
338 default:
339 dev->net->stats.rx_errors++;
340 DBG(dev, "rx status %d\n", status);
341 break;
342 }
343
Felipe Balbi9189a332014-04-21 10:15:12 -0500344 if (skb)
345 dev_kfree_skb_any(skb);
346 if (!netif_running(dev->net)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700347clean:
348 spin_lock(&dev->req_lock);
349 list_add(&req->list, &dev->rx_reqs);
350 spin_unlock(&dev->req_lock);
Felipe Balbi9189a332014-04-21 10:15:12 -0500351 req = NULL;
352 }
353 if (req)
354 rx_submit(dev, req, GFP_ATOMIC);
David Brownell2b3d9422008-06-19 18:19:28 -0700355}
356
357static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
358{
359 unsigned i;
360 struct usb_request *req;
361
362 if (!n)
363 return -ENOMEM;
364
365 /* queue/recycle up to N requests */
366 i = n;
367 list_for_each_entry(req, list, list) {
368 if (i-- == 0)
369 goto extra;
370 }
371 while (i--) {
372 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
373 if (!req)
374 return list_empty(list) ? -ENOMEM : 0;
375 list_add(&req->list, list);
376 }
377 return 0;
378
379extra:
380 /* free extras */
381 for (;;) {
382 struct list_head *next;
383
384 next = req->list.next;
385 list_del(&req->list);
386 usb_ep_free_request(ep, req);
387
388 if (next == list)
389 break;
390
391 req = container_of(next, struct usb_request, list);
392 }
393 return 0;
394}
395
396static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
397{
398 int status;
399
400 spin_lock(&dev->req_lock);
401 status = prealloc(&dev->tx_reqs, link->in_ep, n);
402 if (status < 0)
403 goto fail;
404 status = prealloc(&dev->rx_reqs, link->out_ep, n);
405 if (status < 0)
406 goto fail;
407 goto done;
408fail:
409 DBG(dev, "can't alloc requests\n");
410done:
411 spin_unlock(&dev->req_lock);
412 return status;
413}
414
415static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
416{
417 struct usb_request *req;
418 unsigned long flags;
419
420 /* fill unused rxq slots with some skb */
421 spin_lock_irqsave(&dev->req_lock, flags);
422 while (!list_empty(&dev->rx_reqs)) {
423 req = container_of(dev->rx_reqs.next,
424 struct usb_request, list);
425 list_del_init(&req->list);
426 spin_unlock_irqrestore(&dev->req_lock, flags);
427
428 if (rx_submit(dev, req, gfp_flags) < 0) {
429 defer_kevent(dev, WORK_RX_MEMORY);
430 return;
431 }
432
433 spin_lock_irqsave(&dev->req_lock, flags);
434 }
435 spin_unlock_irqrestore(&dev->req_lock, flags);
436}
437
438static void eth_work(struct work_struct *work)
439{
440 struct eth_dev *dev = container_of(work, struct eth_dev, work);
441
442 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
443 if (netif_running(dev->net))
444 rx_fill(dev, GFP_KERNEL);
445 }
446
447 if (dev->todo)
448 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
449}
450
451static void tx_complete(struct usb_ep *ep, struct usb_request *req)
452{
453 struct sk_buff *skb = req->context;
454 struct eth_dev *dev = ep->driver_data;
455
456 switch (req->status) {
457 default:
458 dev->net->stats.tx_errors++;
459 VDBG(dev, "tx err %d\n", req->status);
460 /* FALLTHROUGH */
461 case -ECONNRESET: /* unlink */
462 case -ESHUTDOWN: /* disconnect etc */
463 break;
464 case 0:
465 dev->net->stats.tx_bytes += skb->len;
466 }
467 dev->net->stats.tx_packets++;
468
469 spin_lock(&dev->req_lock);
470 list_add(&req->list, &dev->tx_reqs);
471 spin_unlock(&dev->req_lock);
472 dev_kfree_skb_any(skb);
473
474 atomic_dec(&dev->tx_qlen);
475 if (netif_carrier_ok(dev->net))
476 netif_wake_queue(dev->net);
477}
478
479static inline int is_promisc(u16 cdc_filter)
480{
481 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
482}
483
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000484static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
485 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700486{
487 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100488 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700489 int retval;
490 struct usb_request *req = NULL;
491 unsigned long flags;
492 struct usb_ep *in;
493 u16 cdc_filter;
494
495 spin_lock_irqsave(&dev->lock, flags);
496 if (dev->port_usb) {
497 in = dev->port_usb->in_ep;
498 cdc_filter = dev->port_usb->cdc_filter;
499 } else {
500 in = NULL;
501 cdc_filter = 0;
502 }
503 spin_unlock_irqrestore(&dev->lock, flags);
504
Jim Baxter6d3865f2014-07-07 18:33:18 +0100505 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700506 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000507 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700508 }
509
510 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100511 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700512 u8 *dest = skb->data;
513
514 if (is_multicast_ether_addr(dest)) {
515 u16 type;
516
517 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
518 * SET_ETHERNET_MULTICAST_FILTERS requests
519 */
520 if (is_broadcast_ether_addr(dest))
521 type = USB_CDC_PACKET_TYPE_BROADCAST;
522 else
523 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
524 if (!(cdc_filter & type)) {
525 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000526 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700527 }
528 }
529 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
530 }
531
532 spin_lock_irqsave(&dev->req_lock, flags);
533 /*
534 * this freelist can be empty if an interrupt triggered disconnect()
535 * and reconfigured the gadget (shutting down this queue) after the
536 * network stack decided to xmit but before we got the spinlock.
537 */
538 if (list_empty(&dev->tx_reqs)) {
539 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000540 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700541 }
542
543 req = container_of(dev->tx_reqs.next, struct usb_request, list);
544 list_del(&req->list);
545
546 /* temporarily stop TX queue when the freelist empties */
547 if (list_empty(&dev->tx_reqs))
548 netif_stop_queue(net);
549 spin_unlock_irqrestore(&dev->req_lock, flags);
550
551 /* no buffer copies needed, unless the network stack did it
552 * or the hardware can't use skb buffers.
553 * or there's not enough space for extra headers we need
554 */
555 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500556 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700557
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500558 spin_lock_irqsave(&dev->lock, flags);
559 if (dev->port_usb)
560 skb = dev->wrap(dev->port_usb, skb);
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200561 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100562 if (!skb) {
563 /* Multi frame CDC protocols may store the frame for
564 * later which is not a dropped frame.
565 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800566 if (dev->port_usb &&
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200567 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100568 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700569 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100570 }
David Brownell2b3d9422008-06-19 18:19:28 -0700571 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100572
573 length = skb->len;
David Brownell2b3d9422008-06-19 18:19:28 -0700574 req->buf = skb->data;
575 req->context = skb;
576 req->complete = tx_complete;
577
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200578 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200579 if (dev->port_usb &&
580 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200581 length == dev->port_usb->fixed_in_len &&
582 (length % in->maxpacket) == 0)
583 req->zero = 0;
584 else
585 req->zero = 1;
586
David Brownell2b3d9422008-06-19 18:19:28 -0700587 /* use zlp framing on tx for strict CDC-Ether conformance,
588 * though any robust network rx path ignores extra padding.
589 * and some hardware doesn't like to write zlps.
590 */
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200591 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700592 length++;
593
594 req->length = length;
595
Paul Zimmerman04617db2011-06-27 14:13:18 -0700596 /* throttle high/super speed IRQ rate back slightly */
David Brownell2b3d9422008-06-19 18:19:28 -0700597 if (gadget_is_dualspeed(dev->gadget))
Paul Zimmerman04617db2011-06-27 14:13:18 -0700598 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH ||
599 dev->gadget->speed == USB_SPEED_SUPER)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200600 ? ((atomic_read(&dev->tx_qlen) % dev->qmult) != 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700601 : 0;
602
603 retval = usb_ep_queue(in, req, GFP_ATOMIC);
604 switch (retval) {
605 default:
606 DBG(dev, "tx queue err %d\n", retval);
607 break;
608 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200609 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700610 atomic_inc(&dev->tx_qlen);
611 }
612
613 if (retval) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500614 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700615drop:
616 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100617multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700618 spin_lock_irqsave(&dev->req_lock, flags);
619 if (list_empty(&dev->tx_reqs))
620 netif_start_queue(net);
621 list_add(&req->list, &dev->tx_reqs);
622 spin_unlock_irqrestore(&dev->req_lock, flags);
623 }
Patrick McHardy6ed10652009-06-23 06:03:08 +0000624 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700625}
626
627/*-------------------------------------------------------------------------*/
628
629static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
630{
631 DBG(dev, "%s\n", __func__);
632
633 /* fill the rx queue */
634 rx_fill(dev, gfp_flags);
635
636 /* and open the tx floodgates */
637 atomic_set(&dev->tx_qlen, 0);
638 netif_wake_queue(dev->net);
639}
640
641static int eth_open(struct net_device *net)
642{
643 struct eth_dev *dev = netdev_priv(net);
644 struct gether *link;
645
646 DBG(dev, "%s\n", __func__);
647 if (netif_carrier_ok(dev->net))
648 eth_start(dev, GFP_KERNEL);
649
650 spin_lock_irq(&dev->lock);
651 link = dev->port_usb;
652 if (link && link->open)
653 link->open(link);
654 spin_unlock_irq(&dev->lock);
655
656 return 0;
657}
658
659static int eth_stop(struct net_device *net)
660{
661 struct eth_dev *dev = netdev_priv(net);
662 unsigned long flags;
663
664 VDBG(dev, "%s\n", __func__);
665 netif_stop_queue(net);
666
667 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
668 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
669 dev->net->stats.rx_errors, dev->net->stats.tx_errors
670 );
671
672 /* ensure there are no more active requests */
673 spin_lock_irqsave(&dev->lock, flags);
674 if (dev->port_usb) {
675 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200676 const struct usb_endpoint_descriptor *in;
677 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700678
679 if (link->close)
680 link->close(link);
681
682 /* NOTE: we have no abort-queue primitive we could use
683 * to cancel all pending I/O. Instead, we disable then
684 * reenable the endpoints ... this idiom may leave toggle
685 * wrong, but that's a self-correcting error.
686 *
687 * REVISIT: we *COULD* just let the transfers complete at
688 * their own pace; the network stack can handle old packets.
689 * For the moment we leave this here, since it works.
690 */
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200691 in = link->in_ep->desc;
692 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700693 usb_ep_disable(link->in_ep);
694 usb_ep_disable(link->out_ep);
695 if (netif_carrier_ok(net)) {
696 DBG(dev, "host still using in/out endpoints\n");
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200697 link->in_ep->desc = in;
698 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300699 usb_ep_enable(link->in_ep);
700 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700701 }
702 }
703 spin_unlock_irqrestore(&dev->lock, flags);
704
705 return 0;
706}
707
708/*-------------------------------------------------------------------------*/
709
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200710static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700711{
712 if (str) {
713 unsigned i;
714
715 for (i = 0; i < 6; i++) {
716 unsigned char num;
717
718 if ((*str == '.') || (*str == ':'))
719 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300720 num = hex_to_bin(*str++) << 4;
721 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700722 dev_addr [i] = num;
723 }
724 if (is_valid_ether_addr(dev_addr))
725 return 0;
726 }
Joe Perches006c9132012-07-12 22:33:11 -0700727 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700728 return 1;
729}
730
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200731static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
732{
733 if (len < 18)
734 return -EINVAL;
735
Andy Shevchenko27f38702015-01-15 13:40:04 +0200736 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200737 return 18;
738}
739
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800740static const struct net_device_ops eth_netdev_ops = {
741 .ndo_open = eth_open,
742 .ndo_stop = eth_stop,
743 .ndo_start_xmit = eth_start_xmit,
744 .ndo_change_mtu = ueth_change_mtu,
745 .ndo_set_mac_address = eth_mac_addr,
746 .ndo_validate_addr = eth_validate_addr,
747};
David Brownell2b3d9422008-06-19 18:19:28 -0700748
Marcel Holtmannaa790742010-01-15 22:13:58 -0800749static struct device_type gadget_type = {
750 .name = "gadget",
751};
752
David Brownell2b3d9422008-06-19 18:19:28 -0700753/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200754 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700755 * @g: gadget to associated with these links
756 * @ethaddr: NULL, or a buffer in which the ethernet address of the
757 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +0200758 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700759 * Context: may sleep
760 *
761 * This sets up the single network link that may be exported by a
762 * gadget driver using this framework. The link layer addresses are
763 * set up using module parameters.
764 *
Dan Carpenter574f24f2013-11-14 11:42:11 +0300765 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -0700766 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200767struct eth_dev *gether_setup_name(struct usb_gadget *g,
768 const char *dev_addr, const char *host_addr,
769 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -0700770{
771 struct eth_dev *dev;
772 struct net_device *net;
773 int status;
774
David Brownell2b3d9422008-06-19 18:19:28 -0700775 net = alloc_etherdev(sizeof *dev);
776 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100777 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700778
779 dev = netdev_priv(net);
780 spin_lock_init(&dev->lock);
781 spin_lock_init(&dev->req_lock);
782 INIT_WORK(&dev->work, eth_work);
783 INIT_LIST_HEAD(&dev->tx_reqs);
784 INIT_LIST_HEAD(&dev->rx_reqs);
785
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500786 skb_queue_head_init(&dev->rx_frames);
787
David Brownell2b3d9422008-06-19 18:19:28 -0700788 /* network device setup */
789 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200790 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200791 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700792
793 if (get_ether_addr(dev_addr, net->dev_addr))
794 dev_warn(&g->dev,
795 "using random %s ethernet address\n", "self");
796 if (get_ether_addr(host_addr, dev->host_mac))
797 dev_warn(&g->dev,
798 "using random %s ethernet address\n", "host");
799
800 if (ethaddr)
801 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
802
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800803 net->netdev_ops = &eth_netdev_ops;
804
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000805 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -0700806
David Brownell2b3d9422008-06-19 18:19:28 -0700807 dev->gadget = g;
808 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800809 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700810
811 status = register_netdev(net);
812 if (status < 0) {
813 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
814 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100815 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700816 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700817 INFO(dev, "MAC %pM\n", net->dev_addr);
818 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700819
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200820 /*
821 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700822 * - iff DATA transfer is active, carrier is "on"
823 * - tx queueing enabled if open *and* carrier is "on"
824 */
825 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700826 }
827
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100828 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700829}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500830EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700831
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200832struct net_device *gether_setup_name_default(const char *netname)
833{
834 struct net_device *net;
835 struct eth_dev *dev;
836
837 net = alloc_etherdev(sizeof(*dev));
838 if (!net)
839 return ERR_PTR(-ENOMEM);
840
841 dev = netdev_priv(net);
842 spin_lock_init(&dev->lock);
843 spin_lock_init(&dev->req_lock);
844 INIT_WORK(&dev->work, eth_work);
845 INIT_LIST_HEAD(&dev->tx_reqs);
846 INIT_LIST_HEAD(&dev->rx_reqs);
847
848 skb_queue_head_init(&dev->rx_frames);
849
850 /* network device setup */
851 dev->net = net;
852 dev->qmult = QMULT_DEFAULT;
853 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
854
855 eth_random_addr(dev->dev_mac);
856 pr_warn("using random %s ethernet address\n", "self");
857 eth_random_addr(dev->host_mac);
858 pr_warn("using random %s ethernet address\n", "host");
859
860 net->netdev_ops = &eth_netdev_ops;
861
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000862 net->ethtool_ops = &ops;
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200863 SET_NETDEV_DEVTYPE(net, &gadget_type);
864
865 return net;
866}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500867EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200868
869int gether_register_netdev(struct net_device *net)
870{
871 struct eth_dev *dev;
872 struct usb_gadget *g;
873 struct sockaddr sa;
874 int status;
875
876 if (!net->dev.parent)
877 return -EINVAL;
878 dev = netdev_priv(net);
879 g = dev->gadget;
880 status = register_netdev(net);
881 if (status < 0) {
882 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
883 return status;
884 } else {
885 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
886
887 /* two kinds of host-initiated state changes:
888 * - iff DATA transfer is active, carrier is "on"
889 * - tx queueing enabled if open *and* carrier is "on"
890 */
891 netif_carrier_off(net);
892 }
893 sa.sa_family = net->type;
894 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
895 rtnl_lock();
896 status = dev_set_mac_address(net, &sa);
897 rtnl_unlock();
898 if (status)
899 pr_warn("cannot set self ethernet address: %d\n", status);
900 else
901 INFO(dev, "MAC %pM\n", dev->dev_mac);
902
903 return status;
904}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500905EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200906
907void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
908{
909 struct eth_dev *dev;
910
911 dev = netdev_priv(net);
912 dev->gadget = g;
913 SET_NETDEV_DEV(net, &g->dev);
914}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500915EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200916
917int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
918{
919 struct eth_dev *dev;
920 u8 new_addr[ETH_ALEN];
921
922 dev = netdev_priv(net);
923 if (get_ether_addr(dev_addr, new_addr))
924 return -EINVAL;
925 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
926 return 0;
927}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500928EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200929
930int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
931{
932 struct eth_dev *dev;
933
934 dev = netdev_priv(net);
935 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
936}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500937EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200938
939int gether_set_host_addr(struct net_device *net, const char *host_addr)
940{
941 struct eth_dev *dev;
942 u8 new_addr[ETH_ALEN];
943
944 dev = netdev_priv(net);
945 if (get_ether_addr(host_addr, new_addr))
946 return -EINVAL;
947 memcpy(dev->host_mac, new_addr, ETH_ALEN);
948 return 0;
949}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500950EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200951
952int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
953{
954 struct eth_dev *dev;
955
956 dev = netdev_priv(net);
957 return get_ether_addr_str(dev->host_mac, host_addr, len);
958}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500959EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200960
961int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
962{
963 struct eth_dev *dev;
964
965 if (len < 13)
966 return -EINVAL;
967
968 dev = netdev_priv(net);
969 snprintf(host_addr, len, "%pm", dev->host_mac);
970
971 return strlen(host_addr);
972}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500973EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200974
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200975void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
976{
977 struct eth_dev *dev;
978
979 dev = netdev_priv(net);
980 memcpy(host_mac, dev->host_mac, ETH_ALEN);
981}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500982EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200983
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200984void gether_set_qmult(struct net_device *net, unsigned qmult)
985{
986 struct eth_dev *dev;
987
988 dev = netdev_priv(net);
989 dev->qmult = qmult;
990}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500991EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200992
993unsigned gether_get_qmult(struct net_device *net)
994{
995 struct eth_dev *dev;
996
997 dev = netdev_priv(net);
998 return dev->qmult;
999}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001000EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001001
1002int gether_get_ifname(struct net_device *net, char *name, int len)
1003{
1004 rtnl_lock();
1005 strlcpy(name, netdev_name(net), len);
1006 rtnl_unlock();
1007 return strlen(name);
1008}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001009EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001010
David Brownell2b3d9422008-06-19 18:19:28 -07001011/**
1012 * gether_cleanup - remove Ethernet-over-USB device
1013 * Context: may sleep
1014 *
1015 * This is called to free all resources allocated by @gether_setup().
1016 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001017void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001018{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001019 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001020 return;
1021
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001022 unregister_netdev(dev->net);
1023 flush_work(&dev->work);
1024 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001025}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001026EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001027
David Brownell2b3d9422008-06-19 18:19:28 -07001028/**
1029 * gether_connect - notify network layer that USB link is active
1030 * @link: the USB link, set up with endpoints, descriptors matching
1031 * current device speed, and any framing wrapper(s) set up.
1032 * Context: irqs blocked
1033 *
1034 * This is called to activate endpoints and let the network layer know
1035 * the connection is active ("carrier detect"). It may cause the I/O
1036 * queues to open and start letting network packets flow, but will in
1037 * any case activate the endpoints so that they respond properly to the
1038 * USB host.
1039 *
1040 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1041 * indicate some error code (negative errno), ep->driver_data values
1042 * have been overwritten.
1043 */
1044struct net_device *gether_connect(struct gether *link)
1045{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001046 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001047 int result = 0;
1048
1049 if (!dev)
1050 return ERR_PTR(-EINVAL);
1051
1052 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001053 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001054 if (result != 0) {
1055 DBG(dev, "enable %s --> %d\n",
1056 link->in_ep->name, result);
1057 goto fail0;
1058 }
1059
1060 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001061 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001062 if (result != 0) {
1063 DBG(dev, "enable %s --> %d\n",
1064 link->out_ep->name, result);
1065 goto fail1;
1066 }
1067
1068 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001069 result = alloc_requests(dev, link, qlen(dev->gadget,
1070 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001071
1072 if (result == 0) {
1073 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001074 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001075 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001076
1077 dev->header_len = link->header_len;
1078 dev->unwrap = link->unwrap;
1079 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001080 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001081
1082 spin_lock(&dev->lock);
1083 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001084 if (netif_running(dev->net)) {
1085 if (link->open)
1086 link->open(link);
1087 } else {
1088 if (link->close)
1089 link->close(link);
1090 }
David Brownell2b3d9422008-06-19 18:19:28 -07001091 spin_unlock(&dev->lock);
1092
1093 netif_carrier_on(dev->net);
1094 if (netif_running(dev->net))
1095 eth_start(dev, GFP_ATOMIC);
1096
1097 /* on error, disable any endpoints */
1098 } else {
1099 (void) usb_ep_disable(link->out_ep);
1100fail1:
1101 (void) usb_ep_disable(link->in_ep);
1102 }
1103fail0:
1104 /* caller is responsible for cleanup on error */
1105 if (result < 0)
1106 return ERR_PTR(result);
1107 return dev->net;
1108}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001109EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001110
1111/**
1112 * gether_disconnect - notify network layer that USB link is inactive
1113 * @link: the USB link, on which gether_connect() was called
1114 * Context: irqs blocked
1115 *
1116 * This is called to deactivate endpoints and let the network layer know
1117 * the connection went inactive ("no carrier").
1118 *
1119 * On return, the state is as if gether_connect() had never been called.
1120 * The endpoints are inactive, and accordingly without active USB I/O.
1121 * Pointers to endpoint descriptors and endpoint private data are nulled.
1122 */
1123void gether_disconnect(struct gether *link)
1124{
1125 struct eth_dev *dev = link->ioport;
1126 struct usb_request *req;
1127
1128 WARN_ON(!dev);
1129 if (!dev)
1130 return;
1131
1132 DBG(dev, "%s\n", __func__);
1133
1134 netif_stop_queue(dev->net);
1135 netif_carrier_off(dev->net);
1136
1137 /* disable endpoints, forcing (synchronous) completion
1138 * of all pending i/o. then free the request objects
1139 * and forget about the endpoints.
1140 */
1141 usb_ep_disable(link->in_ep);
1142 spin_lock(&dev->req_lock);
1143 while (!list_empty(&dev->tx_reqs)) {
1144 req = container_of(dev->tx_reqs.next,
1145 struct usb_request, list);
1146 list_del(&req->list);
1147
1148 spin_unlock(&dev->req_lock);
1149 usb_ep_free_request(link->in_ep, req);
1150 spin_lock(&dev->req_lock);
1151 }
1152 spin_unlock(&dev->req_lock);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001153 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001154
1155 usb_ep_disable(link->out_ep);
1156 spin_lock(&dev->req_lock);
1157 while (!list_empty(&dev->rx_reqs)) {
1158 req = container_of(dev->rx_reqs.next,
1159 struct usb_request, list);
1160 list_del(&req->list);
1161
1162 spin_unlock(&dev->req_lock);
1163 usb_ep_free_request(link->out_ep, req);
1164 spin_lock(&dev->req_lock);
1165 }
1166 spin_unlock(&dev->req_lock);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001167 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001168
1169 /* finish forgetting about this USB link episode */
1170 dev->header_len = 0;
1171 dev->unwrap = NULL;
1172 dev->wrap = NULL;
1173
1174 spin_lock(&dev->lock);
1175 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001176 spin_unlock(&dev->lock);
1177}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001178EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001179
1180MODULE_LICENSE("GPL");
1181MODULE_AUTHOR("David Brownell");