blob: 19ad77f275e06a1c8cb5fbf6d685b9bb86e198a5 [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.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/* #define VERBOSE_DEBUG */
24
25#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
David Brownell2b3d9422008-06-19 18:19:28 -070027#include <linux/device.h>
28#include <linux/ctype.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31
32#include "u_ether.h"
33
34
35/*
36 * This component encapsulates the Ethernet link glue needed to provide
37 * one (!) network link through the USB gadget stack, normally "usb0".
38 *
39 * The control and data models are handled by the function driver which
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050040 * connects to this code; such as CDC Ethernet (ECM or EEM),
41 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
42 * management.
David Brownell2b3d9422008-06-19 18:19:28 -070043 *
44 * Link level addressing is handled by this component using module
45 * parameters; if no such parameters are provided, random link level
46 * addresses are used. Each end of the link uses one address. The
47 * host end address is exported in various ways, and is often recorded
48 * in configuration databases.
49 *
50 * The driver which assembles each configuration using such a link is
51 * responsible for ensuring that each configuration includes at most one
52 * instance of is network link. (The network layer provides ways for
53 * this single "physical" link to be used by multiple virtual links.)
54 */
55
David Brownell8a1ce2c2008-08-18 17:43:56 -070056#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070057
58struct eth_dev {
59 /* lock is held while accessing port_usb
60 * or updating its backlink port_usb->ioport
61 */
62 spinlock_t lock;
63 struct gether *port_usb;
64
65 struct net_device *net;
66 struct usb_gadget *gadget;
67
68 spinlock_t req_lock; /* guard {rx,tx}_reqs */
69 struct list_head tx_reqs, rx_reqs;
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +053070 unsigned tx_qlen;
David Brownell2b3d9422008-06-19 18:19:28 -070071
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050072 struct sk_buff_head rx_frames;
73
David Brownell2b3d9422008-06-19 18:19:28 -070074 unsigned header_len;
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;
86 u8 host_mac[ETH_ALEN];
87};
88
89/*-------------------------------------------------------------------------*/
90
91#define RX_EXTRA 20 /* bytes guarding against rx overflows */
92
93#define DEFAULT_QLEN 2 /* double buffering by default */
94
95
96#ifdef CONFIG_USB_GADGET_DUALSPEED
97
98static unsigned qmult = 5;
99module_param(qmult, uint, S_IRUGO|S_IWUSR);
100MODULE_PARM_DESC(qmult, "queue length multiplier at high speed");
101
102#else /* full speed (low speed doesn't do bulk) */
103#define qmult 1
104#endif
105
106/* for dual-speed hardware, use deeper queues at highspeed */
107static inline int qlen(struct usb_gadget *gadget)
108{
109 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH)
110 return qmult * DEFAULT_QLEN;
111 else
112 return DEFAULT_QLEN;
113}
114
115/*-------------------------------------------------------------------------*/
116
117/* REVISIT there must be a better way than having two sets
118 * of debug calls ...
119 */
120
121#undef DBG
122#undef VDBG
123#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700124#undef INFO
125
126#define xprintk(d, level, fmt, args...) \
127 printk(level "%s: " fmt , (d)->net->name , ## args)
128
129#ifdef DEBUG
130#undef DEBUG
131#define DBG(dev, fmt, args...) \
132 xprintk(dev , KERN_DEBUG , fmt , ## args)
133#else
134#define DBG(dev, fmt, args...) \
135 do { } while (0)
136#endif /* DEBUG */
137
138#ifdef VERBOSE_DEBUG
139#define VDBG DBG
140#else
141#define VDBG(dev, fmt, args...) \
142 do { } while (0)
143#endif /* DEBUG */
144
145#define ERROR(dev, fmt, args...) \
146 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700147#define INFO(dev, fmt, args...) \
148 xprintk(dev , KERN_INFO , fmt , ## args)
149
150/*-------------------------------------------------------------------------*/
151
152/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
153
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800154static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700155{
156 struct eth_dev *dev = netdev_priv(net);
157 unsigned long flags;
158 int status = 0;
159
160 /* don't change MTU on "live" link (peer won't know) */
161 spin_lock_irqsave(&dev->lock, flags);
162 if (dev->port_usb)
163 status = -EBUSY;
164 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
165 status = -ERANGE;
166 else
167 net->mtu = new_mtu;
168 spin_unlock_irqrestore(&dev->lock, flags);
169
170 return status;
171}
172
173static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
174{
175 struct eth_dev *dev = netdev_priv(net);
176
177 strlcpy(p->driver, "g_ether", sizeof p->driver);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700178 strlcpy(p->version, UETH__VERSION, sizeof p->version);
David Brownell2b3d9422008-06-19 18:19:28 -0700179 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
180 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
181}
182
David Brownell2b3d9422008-06-19 18:19:28 -0700183/* REVISIT can also support:
184 * - WOL (by tracking suspends and issuing remote wakeup)
185 * - msglevel (implies updated messaging)
186 * - ... probably more ethtool ops
187 */
188
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700189static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700190 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700191 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700192};
193
194static void defer_kevent(struct eth_dev *dev, int flag)
195{
196 if (test_and_set_bit(flag, &dev->todo))
197 return;
198 if (!schedule_work(&dev->work))
199 ERROR(dev, "kevent %d may have been dropped\n", flag);
200 else
201 DBG(dev, "kevent %d scheduled\n", flag);
202}
203
204static void rx_complete(struct usb_ep *ep, struct usb_request *req);
205
206static int
207rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
208{
209 struct sk_buff *skb;
210 int retval = -ENOMEM;
211 size_t size = 0;
212 struct usb_ep *out;
213 unsigned long flags;
214
215 spin_lock_irqsave(&dev->lock, flags);
216 if (dev->port_usb)
217 out = dev->port_usb->out_ep;
218 else
219 out = NULL;
220 spin_unlock_irqrestore(&dev->lock, flags);
221
222 if (!out)
223 return -ENOTCONN;
224
225
226 /* Padding up to RX_EXTRA handles minor disagreements with host.
227 * Normally we use the USB "terminate on short read" convention;
228 * so allow up to (N*maxpacket), since that memory is normally
229 * already allocated. Some hardware doesn't deal well with short
230 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
231 * byte off the end (to force hardware errors on overflow).
232 *
233 * RNDIS uses internal framing, and explicitly allows senders to
234 * pad to end-of-packet. That's potentially nice for speed, but
235 * means receivers can't recover lost synch on their own (because
236 * new packets don't only start after a short RX).
237 */
238 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
239 size += dev->port_usb->header_len;
240 size += out->maxpacket - 1;
241 size -= size % out->maxpacket;
242
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200243 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800244 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200245
David Brownell2b3d9422008-06-19 18:19:28 -0700246 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
247 if (skb == NULL) {
248 DBG(dev, "no rx skb\n");
249 goto enomem;
250 }
251
252 /* Some platforms perform better when IP packets are aligned,
253 * but on at least one, checksumming fails otherwise. Note:
254 * RNDIS headers involve variable numbers of LE32 values.
255 */
256 skb_reserve(skb, NET_IP_ALIGN);
257
258 req->buf = skb->data;
259 req->length = size;
260 req->complete = rx_complete;
261 req->context = skb;
262
263 retval = usb_ep_queue(out, req, gfp_flags);
264 if (retval == -ENOMEM)
265enomem:
266 defer_kevent(dev, WORK_RX_MEMORY);
267 if (retval) {
268 DBG(dev, "rx submit --> %d\n", retval);
269 if (skb)
270 dev_kfree_skb_any(skb);
271 spin_lock_irqsave(&dev->req_lock, flags);
272 list_add(&req->list, &dev->rx_reqs);
273 spin_unlock_irqrestore(&dev->req_lock, flags);
274 }
275 return retval;
276}
277
278static void rx_complete(struct usb_ep *ep, struct usb_request *req)
279{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500280 struct sk_buff *skb = req->context, *skb2;
David Brownell2b3d9422008-06-19 18:19:28 -0700281 struct eth_dev *dev = ep->driver_data;
282 int status = req->status;
283
284 switch (status) {
285
286 /* normal completion */
287 case 0:
288 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500289
290 if (dev->unwrap) {
291 unsigned long flags;
292
293 spin_lock_irqsave(&dev->lock, flags);
294 if (dev->port_usb) {
295 status = dev->unwrap(dev->port_usb,
296 skb,
297 &dev->rx_frames);
298 } else {
299 dev_kfree_skb_any(skb);
300 status = -ENOTCONN;
301 }
302 spin_unlock_irqrestore(&dev->lock, flags);
303 } else {
304 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700305 }
David Brownell2b3d9422008-06-19 18:19:28 -0700306 skb = NULL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500307
308 skb2 = skb_dequeue(&dev->rx_frames);
309 while (skb2) {
310 if (status < 0
311 || ETH_HLEN > skb2->len
312 || skb2->len > ETH_FRAME_LEN) {
313 dev->net->stats.rx_errors++;
314 dev->net->stats.rx_length_errors++;
315 DBG(dev, "rx length %d\n", skb2->len);
316 dev_kfree_skb_any(skb2);
317 goto next_frame;
318 }
319 skb2->protocol = eth_type_trans(skb2, dev->net);
320 dev->net->stats.rx_packets++;
321 dev->net->stats.rx_bytes += skb2->len;
322
323 /* no buffer copies needed, unless hardware can't
324 * use skb buffers.
325 */
326 status = netif_rx(skb2);
327next_frame:
328 skb2 = skb_dequeue(&dev->rx_frames);
329 }
David Brownell2b3d9422008-06-19 18:19:28 -0700330 break;
331
332 /* software-driven interface shutdown */
333 case -ECONNRESET: /* unlink */
334 case -ESHUTDOWN: /* disconnect etc */
335 VDBG(dev, "rx shutdown, code %d\n", status);
336 goto quiesce;
337
338 /* for hardware automagic (such as pxa) */
339 case -ECONNABORTED: /* endpoint reset */
340 DBG(dev, "rx %s reset\n", ep->name);
341 defer_kevent(dev, WORK_RX_MEMORY);
342quiesce:
343 dev_kfree_skb_any(skb);
344 goto clean;
345
346 /* data overrun */
347 case -EOVERFLOW:
348 dev->net->stats.rx_over_errors++;
349 /* FALLTHROUGH */
350
351 default:
352 dev->net->stats.rx_errors++;
353 DBG(dev, "rx status %d\n", status);
354 break;
355 }
356
357 if (skb)
358 dev_kfree_skb_any(skb);
359 if (!netif_running(dev->net)) {
360clean:
361 spin_lock(&dev->req_lock);
362 list_add(&req->list, &dev->rx_reqs);
363 spin_unlock(&dev->req_lock);
364 req = NULL;
365 }
366 if (req)
367 rx_submit(dev, req, GFP_ATOMIC);
368}
369
370static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
371{
372 unsigned i;
373 struct usb_request *req;
374
375 if (!n)
376 return -ENOMEM;
377
378 /* queue/recycle up to N requests */
379 i = n;
380 list_for_each_entry(req, list, list) {
381 if (i-- == 0)
382 goto extra;
383 }
384 while (i--) {
385 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
386 if (!req)
387 return list_empty(list) ? -ENOMEM : 0;
388 list_add(&req->list, list);
389 }
390 return 0;
391
392extra:
393 /* free extras */
394 for (;;) {
395 struct list_head *next;
396
397 next = req->list.next;
398 list_del(&req->list);
399 usb_ep_free_request(ep, req);
400
401 if (next == list)
402 break;
403
404 req = container_of(next, struct usb_request, list);
405 }
406 return 0;
407}
408
409static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
410{
411 int status;
412
413 spin_lock(&dev->req_lock);
414 status = prealloc(&dev->tx_reqs, link->in_ep, n);
415 if (status < 0)
416 goto fail;
417 status = prealloc(&dev->rx_reqs, link->out_ep, n);
418 if (status < 0)
419 goto fail;
420 goto done;
421fail:
422 DBG(dev, "can't alloc requests\n");
423done:
424 spin_unlock(&dev->req_lock);
425 return status;
426}
427
428static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
429{
430 struct usb_request *req;
431 unsigned long flags;
432
433 /* fill unused rxq slots with some skb */
434 spin_lock_irqsave(&dev->req_lock, flags);
435 while (!list_empty(&dev->rx_reqs)) {
436 req = container_of(dev->rx_reqs.next,
437 struct usb_request, list);
438 list_del_init(&req->list);
439 spin_unlock_irqrestore(&dev->req_lock, flags);
440
441 if (rx_submit(dev, req, gfp_flags) < 0) {
442 defer_kevent(dev, WORK_RX_MEMORY);
443 return;
444 }
445
446 spin_lock_irqsave(&dev->req_lock, flags);
447 }
448 spin_unlock_irqrestore(&dev->req_lock, flags);
449}
450
451static 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
David Brownell2b3d9422008-06-19 18:19:28 -0700487 if (netif_carrier_ok(dev->net))
488 netif_wake_queue(dev->net);
489}
490
491static inline int is_promisc(u16 cdc_filter)
492{
493 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
494}
495
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000496static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
497 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700498{
499 struct eth_dev *dev = netdev_priv(net);
500 int length = skb->len;
501 int retval;
502 struct usb_request *req = NULL;
503 unsigned long flags;
504 struct usb_ep *in;
505 u16 cdc_filter;
506
507 spin_lock_irqsave(&dev->lock, flags);
508 if (dev->port_usb) {
509 in = dev->port_usb->in_ep;
510 cdc_filter = dev->port_usb->cdc_filter;
511 } else {
512 in = NULL;
513 cdc_filter = 0;
514 }
515 spin_unlock_irqrestore(&dev->lock, flags);
516
517 if (!in) {
518 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000519 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700520 }
521
522 /* apply outgoing CDC or RNDIS filters */
523 if (!is_promisc(cdc_filter)) {
524 u8 *dest = skb->data;
525
526 if (is_multicast_ether_addr(dest)) {
527 u16 type;
528
529 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
530 * SET_ETHERNET_MULTICAST_FILTERS requests
531 */
532 if (is_broadcast_ether_addr(dest))
533 type = USB_CDC_PACKET_TYPE_BROADCAST;
534 else
535 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
536 if (!(cdc_filter & type)) {
537 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000538 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700539 }
540 }
541 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
542 }
543
544 spin_lock_irqsave(&dev->req_lock, flags);
545 /*
546 * this freelist can be empty if an interrupt triggered disconnect()
547 * and reconfigured the gadget (shutting down this queue) after the
548 * network stack decided to xmit but before we got the spinlock.
549 */
550 if (list_empty(&dev->tx_reqs)) {
551 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000552 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700553 }
554
555 req = container_of(dev->tx_reqs.next, struct usb_request, list);
556 list_del(&req->list);
557
558 /* temporarily stop TX queue when the freelist empties */
559 if (list_empty(&dev->tx_reqs))
560 netif_stop_queue(net);
561 spin_unlock_irqrestore(&dev->req_lock, flags);
562
563 /* no buffer copies needed, unless the network stack did it
564 * or the hardware can't use skb buffers.
565 * or there's not enough space for extra headers we need
566 */
567 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500568 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700569
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500570 spin_lock_irqsave(&dev->lock, flags);
571 if (dev->port_usb)
572 skb = dev->wrap(dev->port_usb, skb);
573 spin_unlock_irqrestore(&dev->lock, flags);
574 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700575 goto drop;
576
David Brownell2b3d9422008-06-19 18:19:28 -0700577 length = skb->len;
578 }
579 req->buf = skb->data;
580 req->context = skb;
581 req->complete = tx_complete;
582
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200583 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
584 if (dev->port_usb->is_fixed &&
585 length == dev->port_usb->fixed_in_len &&
586 (length % in->maxpacket) == 0)
587 req->zero = 0;
588 else
589 req->zero = 1;
590
David Brownell2b3d9422008-06-19 18:19:28 -0700591 /* use zlp framing on tx for strict CDC-Ether conformance,
592 * though any robust network rx path ignores extra padding.
593 * and some hardware doesn't like to write zlps.
594 */
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200595 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
David Brownell2b3d9422008-06-19 18:19:28 -0700596 length++;
597
598 req->length = length;
599
600 /* throttle highspeed IRQ rate back slightly */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530601 if (gadget_is_dualspeed(dev->gadget) &&
602 (dev->gadget->speed == USB_SPEED_HIGH)) {
603 dev->tx_qlen++;
604 if (dev->tx_qlen == qmult) {
605 req->no_interrupt = 0;
606 dev->tx_qlen = 0;
607 } else {
608 req->no_interrupt = 1;
609 }
610 } else {
611 req->no_interrupt = 0;
612 }
David Brownell2b3d9422008-06-19 18:19:28 -0700613
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:
620 net->trans_start = jiffies;
David Brownell2b3d9422008-06-19 18:19:28 -0700621 }
622
623 if (retval) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500624 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700625drop:
626 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700627 spin_lock_irqsave(&dev->req_lock, flags);
628 if (list_empty(&dev->tx_reqs))
629 netif_start_queue(net);
630 list_add(&req->list, &dev->tx_reqs);
631 spin_unlock_irqrestore(&dev->req_lock, flags);
632 }
Patrick McHardy6ed10652009-06-23 06:03:08 +0000633 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700634}
635
636/*-------------------------------------------------------------------------*/
637
638static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
639{
640 DBG(dev, "%s\n", __func__);
641
642 /* fill the rx queue */
643 rx_fill(dev, gfp_flags);
644
645 /* and open the tx floodgates */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530646 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700647 netif_wake_queue(dev->net);
648}
649
650static int eth_open(struct net_device *net)
651{
652 struct eth_dev *dev = netdev_priv(net);
653 struct gether *link;
654
655 DBG(dev, "%s\n", __func__);
656 if (netif_carrier_ok(dev->net))
657 eth_start(dev, GFP_KERNEL);
658
659 spin_lock_irq(&dev->lock);
660 link = dev->port_usb;
661 if (link && link->open)
662 link->open(link);
663 spin_unlock_irq(&dev->lock);
664
665 return 0;
666}
667
668static int eth_stop(struct net_device *net)
669{
670 struct eth_dev *dev = netdev_priv(net);
671 unsigned long flags;
672
673 VDBG(dev, "%s\n", __func__);
674 netif_stop_queue(net);
675
676 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
677 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
678 dev->net->stats.rx_errors, dev->net->stats.tx_errors
679 );
680
681 /* ensure there are no more active requests */
682 spin_lock_irqsave(&dev->lock, flags);
683 if (dev->port_usb) {
684 struct gether *link = dev->port_usb;
685
686 if (link->close)
687 link->close(link);
688
689 /* NOTE: we have no abort-queue primitive we could use
690 * to cancel all pending I/O. Instead, we disable then
691 * reenable the endpoints ... this idiom may leave toggle
692 * wrong, but that's a self-correcting error.
693 *
694 * REVISIT: we *COULD* just let the transfers complete at
695 * their own pace; the network stack can handle old packets.
696 * For the moment we leave this here, since it works.
697 */
698 usb_ep_disable(link->in_ep);
699 usb_ep_disable(link->out_ep);
700 if (netif_carrier_ok(net)) {
701 DBG(dev, "host still using in/out endpoints\n");
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300702 usb_ep_enable(link->in_ep);
703 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700704 }
705 }
706 spin_unlock_irqrestore(&dev->lock, flags);
707
708 return 0;
709}
710
711/*-------------------------------------------------------------------------*/
712
713/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
714static char *dev_addr;
715module_param(dev_addr, charp, S_IRUGO);
716MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
717
718/* this address is invisible to ifconfig */
719static char *host_addr;
720module_param(host_addr, charp, S_IRUGO);
721MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
722
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200723static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700724{
725 if (str) {
726 unsigned i;
727
728 for (i = 0; i < 6; i++) {
729 unsigned char num;
730
731 if ((*str == '.') || (*str == ':'))
732 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300733 num = hex_to_bin(*str++) << 4;
734 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700735 dev_addr [i] = num;
736 }
737 if (is_valid_ether_addr(dev_addr))
738 return 0;
739 }
740 random_ether_addr(dev_addr);
741 return 1;
742}
743
744static struct eth_dev *the_dev;
745
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800746static const struct net_device_ops eth_netdev_ops = {
747 .ndo_open = eth_open,
748 .ndo_stop = eth_stop,
749 .ndo_start_xmit = eth_start_xmit,
750 .ndo_change_mtu = ueth_change_mtu,
751 .ndo_set_mac_address = eth_mac_addr,
752 .ndo_validate_addr = eth_validate_addr,
753};
David Brownell2b3d9422008-06-19 18:19:28 -0700754
Marcel Holtmannaa790742010-01-15 22:13:58 -0800755static struct device_type gadget_type = {
756 .name = "gadget",
757};
758
David Brownell2b3d9422008-06-19 18:19:28 -0700759/**
760 * gether_setup - initialize one ethernet-over-usb link
761 * @g: gadget to associated with these links
762 * @ethaddr: NULL, or a buffer in which the ethernet address of the
763 * host side of the link is recorded
764 * Context: may sleep
765 *
766 * This sets up the single network link that may be exported by a
767 * gadget driver using this framework. The link layer addresses are
768 * set up using module parameters.
769 *
770 * Returns negative errno, or zero on success
771 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200772int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
David Brownell2b3d9422008-06-19 18:19:28 -0700773{
Mike Lockwood394bf632011-08-12 14:35:42 -0700774 return gether_setup_name(g, ethaddr, "usb");
775}
776
777/**
778 * gether_setup_name - initialize one ethernet-over-usb link
779 * @g: gadget to associated with these links
780 * @ethaddr: NULL, or a buffer in which the ethernet address of the
781 * host side of the link is recorded
782 * @netname: name for network device (for example, "usb")
783 * Context: may sleep
784 *
785 * This sets up the single network link that may be exported by a
786 * gadget driver using this framework. The link layer addresses are
787 * set up using module parameters.
788 *
789 * Returns negative errno, or zero on success
790 */
791int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
792 const char *netname)
793{
David Brownell2b3d9422008-06-19 18:19:28 -0700794 struct eth_dev *dev;
795 struct net_device *net;
796 int status;
797
798 if (the_dev)
799 return -EBUSY;
800
801 net = alloc_etherdev(sizeof *dev);
802 if (!net)
803 return -ENOMEM;
804
805 dev = netdev_priv(net);
806 spin_lock_init(&dev->lock);
807 spin_lock_init(&dev->req_lock);
808 INIT_WORK(&dev->work, eth_work);
809 INIT_LIST_HEAD(&dev->tx_reqs);
810 INIT_LIST_HEAD(&dev->rx_reqs);
811
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500812 skb_queue_head_init(&dev->rx_frames);
813
David Brownell2b3d9422008-06-19 18:19:28 -0700814 /* network device setup */
815 dev->net = net;
Mike Lockwood394bf632011-08-12 14:35:42 -0700816 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700817
818 if (get_ether_addr(dev_addr, net->dev_addr))
819 dev_warn(&g->dev,
820 "using random %s ethernet address\n", "self");
821 if (get_ether_addr(host_addr, dev->host_mac))
822 dev_warn(&g->dev,
823 "using random %s ethernet address\n", "host");
824
825 if (ethaddr)
826 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
827
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800828 net->netdev_ops = &eth_netdev_ops;
829
David Brownell2b3d9422008-06-19 18:19:28 -0700830 SET_ETHTOOL_OPS(net, &ops);
831
832 /* two kinds of host-initiated state changes:
833 * - iff DATA transfer is active, carrier is "on"
834 * - tx queueing enabled if open *and* carrier is "on"
835 */
David Brownell2b3d9422008-06-19 18:19:28 -0700836 netif_carrier_off(net);
837
838 dev->gadget = g;
839 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800840 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700841
842 status = register_netdev(net);
843 if (status < 0) {
844 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
845 free_netdev(net);
846 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700847 INFO(dev, "MAC %pM\n", net->dev_addr);
848 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700849
850 the_dev = dev;
851 }
852
853 return status;
854}
855
856/**
857 * gether_cleanup - remove Ethernet-over-USB device
858 * Context: may sleep
859 *
860 * This is called to free all resources allocated by @gether_setup().
861 */
862void gether_cleanup(void)
863{
864 if (!the_dev)
865 return;
866
867 unregister_netdev(the_dev->net);
Tejun Heo569ff2d2010-12-24 16:14:20 +0100868 flush_work_sync(&the_dev->work);
David Brownell2b3d9422008-06-19 18:19:28 -0700869 free_netdev(the_dev->net);
870
David Brownell2b3d9422008-06-19 18:19:28 -0700871 the_dev = NULL;
872}
873
874
875/**
876 * gether_connect - notify network layer that USB link is active
877 * @link: the USB link, set up with endpoints, descriptors matching
878 * current device speed, and any framing wrapper(s) set up.
879 * Context: irqs blocked
880 *
881 * This is called to activate endpoints and let the network layer know
882 * the connection is active ("carrier detect"). It may cause the I/O
883 * queues to open and start letting network packets flow, but will in
884 * any case activate the endpoints so that they respond properly to the
885 * USB host.
886 *
887 * Verify net_device pointer returned using IS_ERR(). If it doesn't
888 * indicate some error code (negative errno), ep->driver_data values
889 * have been overwritten.
890 */
891struct net_device *gether_connect(struct gether *link)
892{
893 struct eth_dev *dev = the_dev;
894 int result = 0;
895
896 if (!dev)
897 return ERR_PTR(-EINVAL);
898
899 link->in_ep->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300900 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700901 if (result != 0) {
902 DBG(dev, "enable %s --> %d\n",
903 link->in_ep->name, result);
904 goto fail0;
905 }
906
907 link->out_ep->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300908 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700909 if (result != 0) {
910 DBG(dev, "enable %s --> %d\n",
911 link->out_ep->name, result);
912 goto fail1;
913 }
914
915 if (result == 0)
916 result = alloc_requests(dev, link, qlen(dev->gadget));
917
918 if (result == 0) {
919 dev->zlp = link->is_zlp_ok;
920 DBG(dev, "qlen %d\n", qlen(dev->gadget));
921
922 dev->header_len = link->header_len;
923 dev->unwrap = link->unwrap;
924 dev->wrap = link->wrap;
925
926 spin_lock(&dev->lock);
927 dev->port_usb = link;
928 link->ioport = dev;
David Brownell29bac7b2008-09-06 21:33:49 -0700929 if (netif_running(dev->net)) {
930 if (link->open)
931 link->open(link);
932 } else {
933 if (link->close)
934 link->close(link);
935 }
David Brownell2b3d9422008-06-19 18:19:28 -0700936 spin_unlock(&dev->lock);
937
938 netif_carrier_on(dev->net);
939 if (netif_running(dev->net))
940 eth_start(dev, GFP_ATOMIC);
941
942 /* on error, disable any endpoints */
943 } else {
944 (void) usb_ep_disable(link->out_ep);
945fail1:
946 (void) usb_ep_disable(link->in_ep);
947 }
948fail0:
949 /* caller is responsible for cleanup on error */
950 if (result < 0)
951 return ERR_PTR(result);
952 return dev->net;
953}
954
955/**
956 * gether_disconnect - notify network layer that USB link is inactive
957 * @link: the USB link, on which gether_connect() was called
958 * Context: irqs blocked
959 *
960 * This is called to deactivate endpoints and let the network layer know
961 * the connection went inactive ("no carrier").
962 *
963 * On return, the state is as if gether_connect() had never been called.
964 * The endpoints are inactive, and accordingly without active USB I/O.
965 * Pointers to endpoint descriptors and endpoint private data are nulled.
966 */
967void gether_disconnect(struct gether *link)
968{
969 struct eth_dev *dev = link->ioport;
970 struct usb_request *req;
971
David Brownell2b3d9422008-06-19 18:19:28 -0700972 if (!dev)
973 return;
974
975 DBG(dev, "%s\n", __func__);
976
977 netif_stop_queue(dev->net);
978 netif_carrier_off(dev->net);
979
980 /* disable endpoints, forcing (synchronous) completion
981 * of all pending i/o. then free the request objects
982 * and forget about the endpoints.
983 */
984 usb_ep_disable(link->in_ep);
985 spin_lock(&dev->req_lock);
986 while (!list_empty(&dev->tx_reqs)) {
987 req = container_of(dev->tx_reqs.next,
988 struct usb_request, list);
989 list_del(&req->list);
990
991 spin_unlock(&dev->req_lock);
992 usb_ep_free_request(link->in_ep, req);
993 spin_lock(&dev->req_lock);
994 }
995 spin_unlock(&dev->req_lock);
996 link->in_ep->driver_data = NULL;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300997 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700998
999 usb_ep_disable(link->out_ep);
1000 spin_lock(&dev->req_lock);
1001 while (!list_empty(&dev->rx_reqs)) {
1002 req = container_of(dev->rx_reqs.next,
1003 struct usb_request, list);
1004 list_del(&req->list);
1005
1006 spin_unlock(&dev->req_lock);
1007 usb_ep_free_request(link->out_ep, req);
1008 spin_lock(&dev->req_lock);
1009 }
1010 spin_unlock(&dev->req_lock);
1011 link->out_ep->driver_data = NULL;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +03001012 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001013
1014 /* finish forgetting about this USB link episode */
1015 dev->header_len = 0;
1016 dev->unwrap = NULL;
1017 dev->wrap = NULL;
1018
1019 spin_lock(&dev->lock);
1020 dev->port_usb = NULL;
1021 link->ioport = NULL;
1022 spin_unlock(&dev->lock);
1023}