blob: 5f9dacfe6be5f68f1061e4f7619c6811e0fc1ef8 [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
51struct eth_dev {
52 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070053 */
54 spinlock_t lock;
55 struct gether *port_usb;
56
57 struct net_device *net;
58 struct usb_gadget *gadget;
59
60 spinlock_t req_lock; /* guard {rx,tx}_reqs */
61 struct list_head tx_reqs, rx_reqs;
62 atomic_t tx_qlen;
63
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050064 struct sk_buff_head rx_frames;
65
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020066 unsigned qmult;
67
David Brownell2b3d9422008-06-19 18:19:28 -070068 unsigned header_len;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050069 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
70 int (*unwrap)(struct gether *,
71 struct sk_buff *skb,
72 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070073
74 struct work_struct work;
75
76 unsigned long todo;
77#define WORK_RX_MEMORY 0
78
79 bool zlp;
80 u8 host_mac[ETH_ALEN];
81};
82
83/*-------------------------------------------------------------------------*/
84
85#define RX_EXTRA 20 /* bytes guarding against rx overflows */
86
87#define DEFAULT_QLEN 2 /* double buffering by default */
88
Paul Zimmerman04617db2011-06-27 14:13:18 -070089/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020090static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -070091{
Paul Zimmerman04617db2011-06-27 14:13:18 -070092 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
93 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -070094 return qmult * DEFAULT_QLEN;
95 else
96 return DEFAULT_QLEN;
97}
98
99/*-------------------------------------------------------------------------*/
100
101/* REVISIT there must be a better way than having two sets
102 * of debug calls ...
103 */
104
105#undef DBG
106#undef VDBG
107#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700108#undef INFO
109
110#define xprintk(d, level, fmt, args...) \
111 printk(level "%s: " fmt , (d)->net->name , ## args)
112
113#ifdef DEBUG
114#undef DEBUG
115#define DBG(dev, fmt, args...) \
116 xprintk(dev , KERN_DEBUG , fmt , ## args)
117#else
118#define DBG(dev, fmt, args...) \
119 do { } while (0)
120#endif /* DEBUG */
121
122#ifdef VERBOSE_DEBUG
123#define VDBG DBG
124#else
125#define VDBG(dev, fmt, args...) \
126 do { } while (0)
127#endif /* DEBUG */
128
129#define ERROR(dev, fmt, args...) \
130 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700131#define INFO(dev, fmt, args...) \
132 xprintk(dev , KERN_INFO , fmt , ## args)
133
134/*-------------------------------------------------------------------------*/
135
136/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
137
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800138static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700139{
140 struct eth_dev *dev = netdev_priv(net);
141 unsigned long flags;
142 int status = 0;
143
144 /* don't change MTU on "live" link (peer won't know) */
145 spin_lock_irqsave(&dev->lock, flags);
146 if (dev->port_usb)
147 status = -EBUSY;
148 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
149 status = -ERANGE;
150 else
151 net->mtu = new_mtu;
152 spin_unlock_irqrestore(&dev->lock, flags);
153
154 return status;
155}
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
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200227 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800228 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200229
David Brownell2b3d9422008-06-19 18:19:28 -0700230 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
231 if (skb == NULL) {
232 DBG(dev, "no rx skb\n");
233 goto enomem;
234 }
235
236 /* Some platforms perform better when IP packets are aligned,
237 * but on at least one, checksumming fails otherwise. Note:
238 * RNDIS headers involve variable numbers of LE32 values.
239 */
240 skb_reserve(skb, NET_IP_ALIGN);
241
242 req->buf = skb->data;
243 req->length = size;
244 req->complete = rx_complete;
245 req->context = skb;
246
247 retval = usb_ep_queue(out, req, gfp_flags);
248 if (retval == -ENOMEM)
249enomem:
250 defer_kevent(dev, WORK_RX_MEMORY);
251 if (retval) {
252 DBG(dev, "rx submit --> %d\n", retval);
253 if (skb)
254 dev_kfree_skb_any(skb);
255 spin_lock_irqsave(&dev->req_lock, flags);
256 list_add(&req->list, &dev->rx_reqs);
257 spin_unlock_irqrestore(&dev->req_lock, flags);
258 }
259 return retval;
260}
261
262static void rx_complete(struct usb_ep *ep, struct usb_request *req)
263{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500264 struct sk_buff *skb = req->context, *skb2;
David Brownell2b3d9422008-06-19 18:19:28 -0700265 struct eth_dev *dev = ep->driver_data;
266 int status = req->status;
267
268 switch (status) {
269
270 /* normal completion */
271 case 0:
272 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500273
274 if (dev->unwrap) {
275 unsigned long flags;
276
277 spin_lock_irqsave(&dev->lock, flags);
278 if (dev->port_usb) {
279 status = dev->unwrap(dev->port_usb,
280 skb,
281 &dev->rx_frames);
282 } else {
283 dev_kfree_skb_any(skb);
284 status = -ENOTCONN;
285 }
286 spin_unlock_irqrestore(&dev->lock, flags);
287 } else {
288 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700289 }
David Brownell2b3d9422008-06-19 18:19:28 -0700290 skb = NULL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500291
292 skb2 = skb_dequeue(&dev->rx_frames);
293 while (skb2) {
294 if (status < 0
295 || ETH_HLEN > skb2->len
Ian Coolidge4fe5f072012-11-07 14:39:18 +0000296 || skb2->len > VLAN_ETH_FRAME_LEN) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500297 dev->net->stats.rx_errors++;
298 dev->net->stats.rx_length_errors++;
299 DBG(dev, "rx length %d\n", skb2->len);
300 dev_kfree_skb_any(skb2);
301 goto next_frame;
302 }
303 skb2->protocol = eth_type_trans(skb2, dev->net);
304 dev->net->stats.rx_packets++;
305 dev->net->stats.rx_bytes += skb2->len;
306
307 /* no buffer copies needed, unless hardware can't
308 * use skb buffers.
309 */
310 status = netif_rx(skb2);
311next_frame:
312 skb2 = skb_dequeue(&dev->rx_frames);
313 }
David Brownell2b3d9422008-06-19 18:19:28 -0700314 break;
315
316 /* software-driven interface shutdown */
317 case -ECONNRESET: /* unlink */
318 case -ESHUTDOWN: /* disconnect etc */
319 VDBG(dev, "rx shutdown, code %d\n", status);
320 goto quiesce;
321
322 /* for hardware automagic (such as pxa) */
323 case -ECONNABORTED: /* endpoint reset */
324 DBG(dev, "rx %s reset\n", ep->name);
325 defer_kevent(dev, WORK_RX_MEMORY);
326quiesce:
327 dev_kfree_skb_any(skb);
328 goto clean;
329
330 /* data overrun */
331 case -EOVERFLOW:
332 dev->net->stats.rx_over_errors++;
333 /* FALLTHROUGH */
334
335 default:
336 dev->net->stats.rx_errors++;
337 DBG(dev, "rx status %d\n", status);
338 break;
339 }
340
341 if (skb)
342 dev_kfree_skb_any(skb);
343 if (!netif_running(dev->net)) {
344clean:
345 spin_lock(&dev->req_lock);
346 list_add(&req->list, &dev->rx_reqs);
347 spin_unlock(&dev->req_lock);
348 req = NULL;
349 }
350 if (req)
351 rx_submit(dev, req, GFP_ATOMIC);
352}
353
354static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
355{
356 unsigned i;
357 struct usb_request *req;
358
359 if (!n)
360 return -ENOMEM;
361
362 /* queue/recycle up to N requests */
363 i = n;
364 list_for_each_entry(req, list, list) {
365 if (i-- == 0)
366 goto extra;
367 }
368 while (i--) {
369 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
370 if (!req)
371 return list_empty(list) ? -ENOMEM : 0;
372 list_add(&req->list, list);
373 }
374 return 0;
375
376extra:
377 /* free extras */
378 for (;;) {
379 struct list_head *next;
380
381 next = req->list.next;
382 list_del(&req->list);
383 usb_ep_free_request(ep, req);
384
385 if (next == list)
386 break;
387
388 req = container_of(next, struct usb_request, list);
389 }
390 return 0;
391}
392
393static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
394{
395 int status;
396
397 spin_lock(&dev->req_lock);
398 status = prealloc(&dev->tx_reqs, link->in_ep, n);
399 if (status < 0)
400 goto fail;
401 status = prealloc(&dev->rx_reqs, link->out_ep, n);
402 if (status < 0)
403 goto fail;
404 goto done;
405fail:
406 DBG(dev, "can't alloc requests\n");
407done:
408 spin_unlock(&dev->req_lock);
409 return status;
410}
411
412static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
413{
414 struct usb_request *req;
415 unsigned long flags;
416
417 /* fill unused rxq slots with some skb */
418 spin_lock_irqsave(&dev->req_lock, flags);
419 while (!list_empty(&dev->rx_reqs)) {
420 req = container_of(dev->rx_reqs.next,
421 struct usb_request, list);
422 list_del_init(&req->list);
423 spin_unlock_irqrestore(&dev->req_lock, flags);
424
425 if (rx_submit(dev, req, gfp_flags) < 0) {
426 defer_kevent(dev, WORK_RX_MEMORY);
427 return;
428 }
429
430 spin_lock_irqsave(&dev->req_lock, flags);
431 }
432 spin_unlock_irqrestore(&dev->req_lock, flags);
433}
434
435static void eth_work(struct work_struct *work)
436{
437 struct eth_dev *dev = container_of(work, struct eth_dev, work);
438
439 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
440 if (netif_running(dev->net))
441 rx_fill(dev, GFP_KERNEL);
442 }
443
444 if (dev->todo)
445 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
446}
447
448static void tx_complete(struct usb_ep *ep, struct usb_request *req)
449{
450 struct sk_buff *skb = req->context;
451 struct eth_dev *dev = ep->driver_data;
452
453 switch (req->status) {
454 default:
455 dev->net->stats.tx_errors++;
456 VDBG(dev, "tx err %d\n", req->status);
457 /* FALLTHROUGH */
458 case -ECONNRESET: /* unlink */
459 case -ESHUTDOWN: /* disconnect etc */
460 break;
461 case 0:
462 dev->net->stats.tx_bytes += skb->len;
463 }
464 dev->net->stats.tx_packets++;
465
466 spin_lock(&dev->req_lock);
467 list_add(&req->list, &dev->tx_reqs);
468 spin_unlock(&dev->req_lock);
469 dev_kfree_skb_any(skb);
470
471 atomic_dec(&dev->tx_qlen);
472 if (netif_carrier_ok(dev->net))
473 netif_wake_queue(dev->net);
474}
475
476static inline int is_promisc(u16 cdc_filter)
477{
478 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
479}
480
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000481static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
482 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700483{
484 struct eth_dev *dev = netdev_priv(net);
485 int length = skb->len;
486 int retval;
487 struct usb_request *req = NULL;
488 unsigned long flags;
489 struct usb_ep *in;
490 u16 cdc_filter;
491
492 spin_lock_irqsave(&dev->lock, flags);
493 if (dev->port_usb) {
494 in = dev->port_usb->in_ep;
495 cdc_filter = dev->port_usb->cdc_filter;
496 } else {
497 in = NULL;
498 cdc_filter = 0;
499 }
500 spin_unlock_irqrestore(&dev->lock, flags);
501
502 if (!in) {
503 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000504 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700505 }
506
507 /* apply outgoing CDC or RNDIS filters */
508 if (!is_promisc(cdc_filter)) {
509 u8 *dest = skb->data;
510
511 if (is_multicast_ether_addr(dest)) {
512 u16 type;
513
514 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
515 * SET_ETHERNET_MULTICAST_FILTERS requests
516 */
517 if (is_broadcast_ether_addr(dest))
518 type = USB_CDC_PACKET_TYPE_BROADCAST;
519 else
520 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
521 if (!(cdc_filter & type)) {
522 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000523 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700524 }
525 }
526 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
527 }
528
529 spin_lock_irqsave(&dev->req_lock, flags);
530 /*
531 * this freelist can be empty if an interrupt triggered disconnect()
532 * and reconfigured the gadget (shutting down this queue) after the
533 * network stack decided to xmit but before we got the spinlock.
534 */
535 if (list_empty(&dev->tx_reqs)) {
536 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000537 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700538 }
539
540 req = container_of(dev->tx_reqs.next, struct usb_request, list);
541 list_del(&req->list);
542
543 /* temporarily stop TX queue when the freelist empties */
544 if (list_empty(&dev->tx_reqs))
545 netif_stop_queue(net);
546 spin_unlock_irqrestore(&dev->req_lock, flags);
547
548 /* no buffer copies needed, unless the network stack did it
549 * or the hardware can't use skb buffers.
550 * or there's not enough space for extra headers we need
551 */
552 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500553 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700554
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500555 spin_lock_irqsave(&dev->lock, flags);
556 if (dev->port_usb)
557 skb = dev->wrap(dev->port_usb, skb);
558 spin_unlock_irqrestore(&dev->lock, flags);
559 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700560 goto drop;
561
David Brownell2b3d9422008-06-19 18:19:28 -0700562 length = skb->len;
563 }
564 req->buf = skb->data;
565 req->context = skb;
566 req->complete = tx_complete;
567
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200568 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
569 if (dev->port_usb->is_fixed &&
570 length == dev->port_usb->fixed_in_len &&
571 (length % in->maxpacket) == 0)
572 req->zero = 0;
573 else
574 req->zero = 1;
575
David Brownell2b3d9422008-06-19 18:19:28 -0700576 /* use zlp framing on tx for strict CDC-Ether conformance,
577 * though any robust network rx path ignores extra padding.
578 * and some hardware doesn't like to write zlps.
579 */
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200580 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700581 length++;
582
583 req->length = length;
584
Paul Zimmerman04617db2011-06-27 14:13:18 -0700585 /* throttle high/super speed IRQ rate back slightly */
David Brownell2b3d9422008-06-19 18:19:28 -0700586 if (gadget_is_dualspeed(dev->gadget))
Paul Zimmerman04617db2011-06-27 14:13:18 -0700587 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH ||
588 dev->gadget->speed == USB_SPEED_SUPER)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200589 ? ((atomic_read(&dev->tx_qlen) % dev->qmult) != 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700590 : 0;
591
592 retval = usb_ep_queue(in, req, GFP_ATOMIC);
593 switch (retval) {
594 default:
595 DBG(dev, "tx queue err %d\n", retval);
596 break;
597 case 0:
598 net->trans_start = jiffies;
599 atomic_inc(&dev->tx_qlen);
600 }
601
602 if (retval) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500603 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700604drop:
605 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700606 spin_lock_irqsave(&dev->req_lock, flags);
607 if (list_empty(&dev->tx_reqs))
608 netif_start_queue(net);
609 list_add(&req->list, &dev->tx_reqs);
610 spin_unlock_irqrestore(&dev->req_lock, flags);
611 }
Patrick McHardy6ed10652009-06-23 06:03:08 +0000612 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700613}
614
615/*-------------------------------------------------------------------------*/
616
617static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
618{
619 DBG(dev, "%s\n", __func__);
620
621 /* fill the rx queue */
622 rx_fill(dev, gfp_flags);
623
624 /* and open the tx floodgates */
625 atomic_set(&dev->tx_qlen, 0);
626 netif_wake_queue(dev->net);
627}
628
629static int eth_open(struct net_device *net)
630{
631 struct eth_dev *dev = netdev_priv(net);
632 struct gether *link;
633
634 DBG(dev, "%s\n", __func__);
635 if (netif_carrier_ok(dev->net))
636 eth_start(dev, GFP_KERNEL);
637
638 spin_lock_irq(&dev->lock);
639 link = dev->port_usb;
640 if (link && link->open)
641 link->open(link);
642 spin_unlock_irq(&dev->lock);
643
644 return 0;
645}
646
647static int eth_stop(struct net_device *net)
648{
649 struct eth_dev *dev = netdev_priv(net);
650 unsigned long flags;
651
652 VDBG(dev, "%s\n", __func__);
653 netif_stop_queue(net);
654
655 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
656 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
657 dev->net->stats.rx_errors, dev->net->stats.tx_errors
658 );
659
660 /* ensure there are no more active requests */
661 spin_lock_irqsave(&dev->lock, flags);
662 if (dev->port_usb) {
663 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200664 const struct usb_endpoint_descriptor *in;
665 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700666
667 if (link->close)
668 link->close(link);
669
670 /* NOTE: we have no abort-queue primitive we could use
671 * to cancel all pending I/O. Instead, we disable then
672 * reenable the endpoints ... this idiom may leave toggle
673 * wrong, but that's a self-correcting error.
674 *
675 * REVISIT: we *COULD* just let the transfers complete at
676 * their own pace; the network stack can handle old packets.
677 * For the moment we leave this here, since it works.
678 */
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200679 in = link->in_ep->desc;
680 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700681 usb_ep_disable(link->in_ep);
682 usb_ep_disable(link->out_ep);
683 if (netif_carrier_ok(net)) {
684 DBG(dev, "host still using in/out endpoints\n");
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200685 link->in_ep->desc = in;
686 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300687 usb_ep_enable(link->in_ep);
688 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700689 }
690 }
691 spin_unlock_irqrestore(&dev->lock, flags);
692
693 return 0;
694}
695
696/*-------------------------------------------------------------------------*/
697
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200698static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700699{
700 if (str) {
701 unsigned i;
702
703 for (i = 0; i < 6; i++) {
704 unsigned char num;
705
706 if ((*str == '.') || (*str == ':'))
707 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300708 num = hex_to_bin(*str++) << 4;
709 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700710 dev_addr [i] = num;
711 }
712 if (is_valid_ether_addr(dev_addr))
713 return 0;
714 }
Joe Perches006c9132012-07-12 22:33:11 -0700715 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700716 return 1;
717}
718
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800719static const struct net_device_ops eth_netdev_ops = {
720 .ndo_open = eth_open,
721 .ndo_stop = eth_stop,
722 .ndo_start_xmit = eth_start_xmit,
723 .ndo_change_mtu = ueth_change_mtu,
724 .ndo_set_mac_address = eth_mac_addr,
725 .ndo_validate_addr = eth_validate_addr,
726};
David Brownell2b3d9422008-06-19 18:19:28 -0700727
Marcel Holtmannaa790742010-01-15 22:13:58 -0800728static struct device_type gadget_type = {
729 .name = "gadget",
730};
731
David Brownell2b3d9422008-06-19 18:19:28 -0700732/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200733 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700734 * @g: gadget to associated with these links
735 * @ethaddr: NULL, or a buffer in which the ethernet address of the
736 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +0200737 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700738 * Context: may sleep
739 *
740 * This sets up the single network link that may be exported by a
741 * gadget driver using this framework. The link layer addresses are
742 * set up using module parameters.
743 *
744 * Returns negative errno, or zero on success
745 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200746struct eth_dev *gether_setup_name(struct usb_gadget *g,
747 const char *dev_addr, const char *host_addr,
748 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -0700749{
750 struct eth_dev *dev;
751 struct net_device *net;
752 int status;
753
David Brownell2b3d9422008-06-19 18:19:28 -0700754 net = alloc_etherdev(sizeof *dev);
755 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100756 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700757
758 dev = netdev_priv(net);
759 spin_lock_init(&dev->lock);
760 spin_lock_init(&dev->req_lock);
761 INIT_WORK(&dev->work, eth_work);
762 INIT_LIST_HEAD(&dev->tx_reqs);
763 INIT_LIST_HEAD(&dev->rx_reqs);
764
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500765 skb_queue_head_init(&dev->rx_frames);
766
David Brownell2b3d9422008-06-19 18:19:28 -0700767 /* network device setup */
768 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200769 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200770 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700771
772 if (get_ether_addr(dev_addr, net->dev_addr))
773 dev_warn(&g->dev,
774 "using random %s ethernet address\n", "self");
775 if (get_ether_addr(host_addr, dev->host_mac))
776 dev_warn(&g->dev,
777 "using random %s ethernet address\n", "host");
778
779 if (ethaddr)
780 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
781
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800782 net->netdev_ops = &eth_netdev_ops;
783
David Brownell2b3d9422008-06-19 18:19:28 -0700784 SET_ETHTOOL_OPS(net, &ops);
785
David Brownell2b3d9422008-06-19 18:19:28 -0700786 dev->gadget = g;
787 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800788 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700789
790 status = register_netdev(net);
791 if (status < 0) {
792 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
793 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100794 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700795 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700796 INFO(dev, "MAC %pM\n", net->dev_addr);
797 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700798
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700799 /* two kinds of host-initiated state changes:
800 * - iff DATA transfer is active, carrier is "on"
801 * - tx queueing enabled if open *and* carrier is "on"
802 */
803 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700804 }
805
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100806 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700807}
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200808EXPORT_SYMBOL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700809
810/**
811 * gether_cleanup - remove Ethernet-over-USB device
812 * Context: may sleep
813 *
814 * This is called to free all resources allocated by @gether_setup().
815 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100816void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -0700817{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100818 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -0700819 return;
820
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100821 unregister_netdev(dev->net);
822 flush_work(&dev->work);
823 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -0700824}
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200825EXPORT_SYMBOL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -0700826
David Brownell2b3d9422008-06-19 18:19:28 -0700827/**
828 * gether_connect - notify network layer that USB link is active
829 * @link: the USB link, set up with endpoints, descriptors matching
830 * current device speed, and any framing wrapper(s) set up.
831 * Context: irqs blocked
832 *
833 * This is called to activate endpoints and let the network layer know
834 * the connection is active ("carrier detect"). It may cause the I/O
835 * queues to open and start letting network packets flow, but will in
836 * any case activate the endpoints so that they respond properly to the
837 * USB host.
838 *
839 * Verify net_device pointer returned using IS_ERR(). If it doesn't
840 * indicate some error code (negative errno), ep->driver_data values
841 * have been overwritten.
842 */
843struct net_device *gether_connect(struct gether *link)
844{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100845 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -0700846 int result = 0;
847
848 if (!dev)
849 return ERR_PTR(-EINVAL);
850
851 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300852 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700853 if (result != 0) {
854 DBG(dev, "enable %s --> %d\n",
855 link->in_ep->name, result);
856 goto fail0;
857 }
858
859 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300860 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700861 if (result != 0) {
862 DBG(dev, "enable %s --> %d\n",
863 link->out_ep->name, result);
864 goto fail1;
865 }
866
867 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200868 result = alloc_requests(dev, link, qlen(dev->gadget,
869 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -0700870
871 if (result == 0) {
872 dev->zlp = link->is_zlp_ok;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200873 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -0700874
875 dev->header_len = link->header_len;
876 dev->unwrap = link->unwrap;
877 dev->wrap = link->wrap;
878
879 spin_lock(&dev->lock);
880 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -0700881 if (netif_running(dev->net)) {
882 if (link->open)
883 link->open(link);
884 } else {
885 if (link->close)
886 link->close(link);
887 }
David Brownell2b3d9422008-06-19 18:19:28 -0700888 spin_unlock(&dev->lock);
889
890 netif_carrier_on(dev->net);
891 if (netif_running(dev->net))
892 eth_start(dev, GFP_ATOMIC);
893
894 /* on error, disable any endpoints */
895 } else {
896 (void) usb_ep_disable(link->out_ep);
897fail1:
898 (void) usb_ep_disable(link->in_ep);
899 }
900fail0:
901 /* caller is responsible for cleanup on error */
902 if (result < 0)
903 return ERR_PTR(result);
904 return dev->net;
905}
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200906EXPORT_SYMBOL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -0700907
908/**
909 * gether_disconnect - notify network layer that USB link is inactive
910 * @link: the USB link, on which gether_connect() was called
911 * Context: irqs blocked
912 *
913 * This is called to deactivate endpoints and let the network layer know
914 * the connection went inactive ("no carrier").
915 *
916 * On return, the state is as if gether_connect() had never been called.
917 * The endpoints are inactive, and accordingly without active USB I/O.
918 * Pointers to endpoint descriptors and endpoint private data are nulled.
919 */
920void gether_disconnect(struct gether *link)
921{
922 struct eth_dev *dev = link->ioport;
923 struct usb_request *req;
924
925 WARN_ON(!dev);
926 if (!dev)
927 return;
928
929 DBG(dev, "%s\n", __func__);
930
931 netif_stop_queue(dev->net);
932 netif_carrier_off(dev->net);
933
934 /* disable endpoints, forcing (synchronous) completion
935 * of all pending i/o. then free the request objects
936 * and forget about the endpoints.
937 */
938 usb_ep_disable(link->in_ep);
939 spin_lock(&dev->req_lock);
940 while (!list_empty(&dev->tx_reqs)) {
941 req = container_of(dev->tx_reqs.next,
942 struct usb_request, list);
943 list_del(&req->list);
944
945 spin_unlock(&dev->req_lock);
946 usb_ep_free_request(link->in_ep, req);
947 spin_lock(&dev->req_lock);
948 }
949 spin_unlock(&dev->req_lock);
950 link->in_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300951 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700952
953 usb_ep_disable(link->out_ep);
954 spin_lock(&dev->req_lock);
955 while (!list_empty(&dev->rx_reqs)) {
956 req = container_of(dev->rx_reqs.next,
957 struct usb_request, list);
958 list_del(&req->list);
959
960 spin_unlock(&dev->req_lock);
961 usb_ep_free_request(link->out_ep, req);
962 spin_lock(&dev->req_lock);
963 }
964 spin_unlock(&dev->req_lock);
965 link->out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300966 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700967
968 /* finish forgetting about this USB link episode */
969 dev->header_len = 0;
970 dev->unwrap = NULL;
971 dev->wrap = NULL;
972
973 spin_lock(&dev->lock);
974 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700975 spin_unlock(&dev->lock);
976}
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200977EXPORT_SYMBOL(gether_disconnect);
978
979MODULE_LICENSE("GPL");
980MODULE_AUTHOR("David Brownell");