blob: 2c277d3b934325ada4075c79f9c7814fdd366651 [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 Sridharan16a203f2014-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;
69 atomic_t tx_qlen;
70
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_lin72ffa182014-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 Sridharan16a203f2014-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;
89 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +020090 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -070091};
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 Zimmerman04617db2011-06-27 14:13:18 -070099/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200100static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700101{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700102 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
103 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700104 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 Brownell2b3d9422008-06-19 18:19:28 -0700118#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 Brownell2b3d9422008-06-19 18:19:28 -0700141#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 Hemmingerccad6372008-11-19 22:42:31 -0800148static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700149{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100150 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
151 return -ERANGE;
152 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700153
Mike Looijmansab738ff2015-11-30 12:18:23 +0100154 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700155}
156
157static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
158{
Jiri Pirko7826d432013-01-06 00:44:26 +0000159 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700160
Jiri Pirko7826d432013-01-06 00:44:26 +0000161 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 Brownell2b3d9422008-06-19 18:19:28 -0700165}
166
David Brownell2b3d9422008-06-19 18:19:28 -0700167/* 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 Hemminger0fc0b732009-09-02 01:03:33 -0700173static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700174 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700175 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700176};
177
178static 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
188static void rx_complete(struct usb_ep *ep, struct usb_request *req);
189
190static int
191rx_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_lin72ffa182014-08-14 14:48:44 +0800227 if (dev->ul_max_pkts_per_xfer)
228 size *= dev->ul_max_pkts_per_xfer;
229
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200230 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800231 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200232
xerox_lin72ffa182014-08-14 14:48:44 +0800233 DBG(dev, "%s: size: %d\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700234 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)
253enomem:
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 Brownell2b3d9422008-06-19 18:19:28 -0700259 }
260 return retval;
261}
262
263static void rx_complete(struct usb_ep *ep, struct usb_request *req)
264{
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700265 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700266 struct eth_dev *dev = ep->driver_data;
267 int status = req->status;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700268 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700269
270 switch (status) {
271
272 /* normal completion */
273 case 0:
274 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500275
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 Brownell2b3d9422008-06-19 18:19:28 -0700291 }
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700292 if (!status)
293 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700294 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);
306quiesce:
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 Sridharan16a203f2014-09-24 18:58:23 -0700316 queue = 1;
317 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700318 dev->net->stats.rx_errors++;
319 DBG(dev, "rx status %d\n", status);
320 break;
321 }
322
David Brownell2b3d9422008-06-19 18:19:28 -0700323clean:
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700324 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 Brownell2b3d9422008-06-19 18:19:28 -0700330}
331
332static 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
354extra:
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
371static 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;
383fail:
384 DBG(dev, "can't alloc requests\n");
385done:
386 spin_unlock(&dev->req_lock);
387 return status;
388}
389
390static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
391{
392 struct usb_request *req;
393 unsigned long flags;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700394 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700395
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 Sridharan16a203f2014-09-24 18:58:23 -0700399 /* break the nexus of continuous completion and re-submission*/
400 if (++req_cnt > qlen(dev->gadget))
401 break;
402
David Brownell2b3d9422008-06-19 18:19:28 -0700403 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 Sridharan16a203f2014-09-24 18:58:23 -0700409 spin_lock_irqsave(&dev->req_lock, flags);
410 list_add(&req->list, &dev->rx_reqs);
411 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700412 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 Sridharan16a203f2014-09-24 18:58:23 -0700421static 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 Brownell2b3d9422008-06-19 18:19:28 -0700451static 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
464static 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
492static inline int is_promisc(u16 cdc_filter)
493{
494 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
495}
496
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000497static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
498 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700499{
500 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100501 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700502 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 Baxter6d3865f2014-07-07 18:33:18 +0100518 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700519 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000520 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700521 }
522
523 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100524 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700525 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 McHardy6ed10652009-06-23 06:03:08 +0000539 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700540 }
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 McHardy5b548142009-06-12 06:22:29 +0000553 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700554 }
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 Niebuhr9b39e9d2009-08-14 10:04:22 -0500569 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700570
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500571 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 Baxter6d3865f2014-07-07 18:33:18 +0100575 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 Brownell2b3d9422008-06-19 18:19:28 -0700581 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100582 }
David Brownell2b3d9422008-06-19 18:19:28 -0700583 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100584
585 length = skb->len;
David Brownell2b3d9422008-06-19 18:19:28 -0700586 req->buf = skb->data;
587 req->context = skb;
588 req->complete = tx_complete;
589
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200590 /* 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 Brownell2b3d9422008-06-19 18:19:28 -0700598 /* 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 Kaliuta5c1168d2010-12-08 13:12:04 +0200602 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700603 length++;
604
605 req->length = length;
606
Paul Zimmerman04617db2011-06-27 14:13:18 -0700607 /* throttle high/super speed IRQ rate back slightly */
David Brownell2b3d9422008-06-19 18:19:28 -0700608 if (gadget_is_dualspeed(dev->gadget))
Paul Zimmerman04617db2011-06-27 14:13:18 -0700609 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH ||
610 dev->gadget->speed == USB_SPEED_SUPER)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200611 ? ((atomic_read(&dev->tx_qlen) % dev->qmult) != 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700612 : 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 Westphal860e9532016-05-03 16:33:13 +0200620 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700621 atomic_inc(&dev->tx_qlen);
622 }
623
624 if (retval) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500625 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700626drop:
627 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100628multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700629 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 McHardy6ed10652009-06-23 06:03:08 +0000635 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700636}
637
638/*-------------------------------------------------------------------------*/
639
640static 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
652static 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
670static 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 Grzeschikb1b552a2012-08-08 11:48:10 +0200687 const struct usb_endpoint_descriptor *in;
688 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700689
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 Grzeschikb1b552a2012-08-08 11:48:10 +0200702 in = link->in_ep->desc;
703 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700704 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 Grzeschikb1b552a2012-08-08 11:48:10 +0200708 link->in_ep->desc = in;
709 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300710 usb_ep_enable(link->in_ep);
711 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700712 }
713 }
714 spin_unlock_irqrestore(&dev->lock, flags);
715
716 return 0;
717}
718
719/*-------------------------------------------------------------------------*/
720
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200721static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700722{
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 Shevchenkoe6448142010-06-15 17:04:44 +0300731 num = hex_to_bin(*str++) << 4;
732 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700733 dev_addr [i] = num;
734 }
735 if (is_valid_ether_addr(dev_addr))
736 return 0;
737 }
Joe Perches006c9132012-07-12 22:33:11 -0700738 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700739 return 1;
740}
741
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200742static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
743{
744 if (len < 18)
745 return -EINVAL;
746
Andy Shevchenko27f38702015-01-15 13:40:04 +0200747 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200748 return 18;
749}
750
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800751static 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 Brownell2b3d9422008-06-19 18:19:28 -0700759
Marcel Holtmannaa790742010-01-15 22:13:58 -0800760static struct device_type gadget_type = {
761 .name = "gadget",
762};
763
David Brownell2b3d9422008-06-19 18:19:28 -0700764/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200765 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700766 * @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 Lockwood036e98b2012-05-10 10:08:02 +0200769 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700770 * 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 Carpenter574f24f2013-11-14 11:42:11 +0300776 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -0700777 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200778struct 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 Brownell2b3d9422008-06-19 18:19:28 -0700781{
782 struct eth_dev *dev;
783 struct net_device *net;
784 int status;
785
David Brownell2b3d9422008-06-19 18:19:28 -0700786 net = alloc_etherdev(sizeof *dev);
787 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100788 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700789
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 Sridharan16a203f2014-09-24 18:58:23 -0700794 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700795 INIT_LIST_HEAD(&dev->tx_reqs);
796 INIT_LIST_HEAD(&dev->rx_reqs);
797
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500798 skb_queue_head_init(&dev->rx_frames);
799
David Brownell2b3d9422008-06-19 18:19:28 -0700800 /* network device setup */
801 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200802 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200803 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700804
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 Hemminger5ec38f32009-01-07 18:05:39 -0800815 net->netdev_ops = &eth_netdev_ops;
816
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000817 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -0700818
David Brownell2b3d9422008-06-19 18:19:28 -0700819 dev->gadget = g;
820 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800821 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700822
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 Siewiord6a01432012-12-23 21:10:12 +0100827 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700828 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700829 INFO(dev, "MAC %pM\n", net->dev_addr);
830 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700831
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200832 /*
833 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700834 * - 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 Brownell2b3d9422008-06-19 18:19:28 -0700838 }
839
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100840 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700841}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500842EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700843
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200844struct 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 = &eth_netdev_ops;
873
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000874 net->ethtool_ops = &ops;
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200875 SET_NETDEV_DEVTYPE(net, &gadget_type);
876
877 return net;
878}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500879EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200880
881int 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 Balbi0700faa2014-04-01 13:19:32 -0500917EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200918
919void 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 Balbi0700faa2014-04-01 13:19:32 -0500927EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200928
929int 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 Balbi0700faa2014-04-01 13:19:32 -0500940EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200941
942int 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 Balbi0700faa2014-04-01 13:19:32 -0500949EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200950
951int 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 Balbi0700faa2014-04-01 13:19:32 -0500962EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200963
964int 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 Balbi0700faa2014-04-01 13:19:32 -0500971EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200972
973int 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 Balbi0700faa2014-04-01 13:19:32 -0500985EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200986
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200987void 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 Balbi0700faa2014-04-01 13:19:32 -0500994EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200995
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200996void 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 Balbi0700faa2014-04-01 13:19:32 -05001003EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001004
1005unsigned 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 Balbi0700faa2014-04-01 13:19:32 -05001012EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001013
1014int 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 Balbi0700faa2014-04-01 13:19:32 -05001021EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001022
David Brownell2b3d9422008-06-19 18:19:28 -07001023/**
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 Siewiord6a01432012-12-23 21:10:12 +01001029void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001030{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001031 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001032 return;
1033
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001034 unregister_netdev(dev->net);
1035 flush_work(&dev->work);
1036 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001037}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001038EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001039
David Brownell2b3d9422008-06-19 18:19:28 -07001040/**
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 */
1056struct net_device *gether_connect(struct gether *link)
1057{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001058 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001059 int result = 0;
1060
1061 if (!dev)
1062 return ERR_PTR(-EINVAL);
1063
1064 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001065 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001066 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 Brokhman72c973d2011-06-28 16:33:48 +03001073 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001074 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 Pietrasiewiczf1a18232013-05-23 09:22:03 +02001081 result = alloc_requests(dev, link, qlen(dev->gadget,
1082 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001083
1084 if (result == 0) {
1085 dev->zlp = link->is_zlp_ok;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001086 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001087
1088 dev->header_len = link->header_len;
1089 dev->unwrap = link->unwrap;
1090 dev->wrap = link->wrap;
xerox_lin72ffa182014-08-14 14:48:44 +08001091 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001092
1093 spin_lock(&dev->lock);
1094 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001095 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 Brownell2b3d9422008-06-19 18:19:28 -07001102 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);
1111fail1:
1112 (void) usb_ep_disable(link->in_ep);
1113 }
1114fail0:
1115 /* caller is responsible for cleanup on error */
1116 if (result < 0)
1117 return ERR_PTR(result);
1118 return dev->net;
1119}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001120EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001121
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 */
1134void gether_disconnect(struct gether *link)
1135{
1136 struct eth_dev *dev = link->ioport;
1137 struct usb_request *req;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001138 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001139
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 Brokhman72c973d2011-06-28 16:33:48 +03001165 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001166
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 Sridharan16a203f2014-09-24 18:58:23 -07001179
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 Brokhman72c973d2011-06-28 16:33:48 +03001185 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001186
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 Brownell2b3d9422008-06-19 18:19:28 -07001194 spin_unlock(&dev->lock);
1195}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001196EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001197
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001198static 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}
1207module_init(gether_init);
1208
1209static void __exit gether_exit(void)
1210{
1211 destroy_workqueue(uether_wq);
1212
1213}
1214module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001215MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001216MODULE_DESCRIPTION("ethernet over USB driver");
1217MODULE_LICENSE("GPL v2");