1 /*
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>
26 #include <linux/utsname.h>
27 #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
40 * connects to this code; such as CDC Ethernet, "CDC Subset", or RNDIS.
41 * That includes all descriptor and endpoint management.
42 *
43 * Link level addressing is handled by this component using module
44 * parameters; if no such parameters are provided, random link level
45 * addresses are used. Each end of the link uses one address. The
46 * host end address is exported in various ways, and is often recorded
47 * in configuration databases.
48 *
49 * The driver which assembles each configuration using such a link is
50 * responsible for ensuring that each configuration includes at most one
51 * instance of is network link. (The network layer provides ways for
52 * this single "physical" link to be used by multiple virtual links.)
53 */
54
55 #define UETH__VERSION "29-May-2008"
56
57 struct eth_dev {
58 /* lock is held while accessing port_usb
59 * or updating its backlink port_usb->ioport
60 */
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
71 unsigned header_len;
72 struct sk_buff *(*wrap)(struct sk_buff *skb);
73 int (*unwrap)(struct sk_buff *skb);
74
75 struct work_struct work;
76
77 unsigned long todo;
78 #define WORK_RX_MEMORY 0
79
80 bool zlp;
81 u8 host_mac[ETH_ALEN];
82 };
83
84 /*-------------------------------------------------------------------------*/
85
86 #define RX_EXTRA 20 /* bytes guarding against rx overflows */
87
88 #define DEFAULT_QLEN 2 /* double buffering by default */
89
90
91 #ifdef CONFIG_USB_GADGET_DUALSPEED
92
93 static unsigned qmult = 5;
94 module_param(qmult, uint, S_IRUGO|S_IWUSR);
95 MODULE_PARM_DESC(qmult, "queue length multiplier at high speed");
96
97 #else /* full speed (low speed doesn't do bulk) */
98 #define qmult 1
99 #endif
100
101 /* for dual-speed hardware, use deeper queues at highspeed */
qlen(struct usb_gadget * gadget)102 static inline int qlen(struct usb_gadget *gadget)
103 {
104 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH)
105 return qmult * DEFAULT_QLEN;
106 else
107 return DEFAULT_QLEN;
108 }
109
110 /*-------------------------------------------------------------------------*/
111
112 /* REVISIT there must be a better way than having two sets
113 * of debug calls ...
114 */
115
116 #undef DBG
117 #undef VDBG
118 #undef ERROR
119 #undef INFO
120
121 #define xprintk(d, level, fmt, args...) \
122 printk(level "%s: " fmt , (d)->net->name , ## args)
123
124 #ifdef DEBUG
125 #undef DEBUG
126 #define DBG(dev, fmt, args...) \
127 xprintk(dev , KERN_DEBUG , fmt , ## args)
128 #else
129 #define DBG(dev, fmt, args...) \
130 do { } while (0)
131 #endif /* DEBUG */
132
133 #ifdef VERBOSE_DEBUG
134 #define VDBG DBG
135 #else
136 #define VDBG(dev, fmt, args...) \
137 do { } while (0)
138 #endif /* DEBUG */
139
140 #define ERROR(dev, fmt, args...) \
141 xprintk(dev , KERN_ERR , fmt , ## args)
142 #define INFO(dev, fmt, args...) \
143 xprintk(dev , KERN_INFO , fmt , ## args)
144
145 /*-------------------------------------------------------------------------*/
146
147 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
148
ueth_change_mtu(struct net_device * net,int new_mtu)149 static int ueth_change_mtu(struct net_device *net, int new_mtu)
150 {
151 struct eth_dev *dev = netdev_priv(net);
152 unsigned long flags;
153 int status = 0;
154
155 /* don't change MTU on "live" link (peer won't know) */
156 spin_lock_irqsave(&dev->lock, flags);
157 if (dev->port_usb)
158 status = -EBUSY;
159 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
160 status = -ERANGE;
161 else
162 net->mtu = new_mtu;
163 spin_unlock_irqrestore(&dev->lock, flags);
164
165 return status;
166 }
167
eth_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * p)168 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
169 {
170 struct eth_dev *dev = netdev_priv(net);
171
172 strlcpy(p->driver, "g_ether", sizeof p->driver);
173 strlcpy(p->version, UETH__VERSION, sizeof p->version);
174 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
175 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
176 }
177
eth_get_link(struct net_device * net)178 static u32 eth_get_link(struct net_device *net)
179 {
180 struct eth_dev *dev = netdev_priv(net);
181 return dev->gadget->speed != USB_SPEED_UNKNOWN;
182 }
183
184 /* REVISIT can also support:
185 * - WOL (by tracking suspends and issuing remote wakeup)
186 * - msglevel (implies updated messaging)
187 * - ... probably more ethtool ops
188 */
189
190 static struct ethtool_ops ops = {
191 .get_drvinfo = eth_get_drvinfo,
192 .get_link = eth_get_link
193 };
194
defer_kevent(struct eth_dev * dev,int flag)195 static void defer_kevent(struct eth_dev *dev, int flag)
196 {
197 if (test_and_set_bit(flag, &dev->todo))
198 return;
199 if (!schedule_work(&dev->work))
200 ERROR(dev, "kevent %d may have been dropped\n", flag);
201 else
202 DBG(dev, "kevent %d scheduled\n", flag);
203 }
204
205 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
206
207 static int
rx_submit(struct eth_dev * dev,struct usb_request * req,gfp_t gfp_flags)208 rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
209 {
210 struct sk_buff *skb;
211 int retval = -ENOMEM;
212 size_t size = 0;
213 struct usb_ep *out;
214 unsigned long flags;
215
216 spin_lock_irqsave(&dev->lock, flags);
217 if (dev->port_usb)
218 out = dev->port_usb->out_ep;
219 else
220 out = NULL;
221 spin_unlock_irqrestore(&dev->lock, flags);
222
223 if (!out)
224 return -ENOTCONN;
225
226
227 /* Padding up to RX_EXTRA handles minor disagreements with host.
228 * Normally we use the USB "terminate on short read" convention;
229 * so allow up to (N*maxpacket), since that memory is normally
230 * already allocated. Some hardware doesn't deal well with short
231 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
232 * byte off the end (to force hardware errors on overflow).
233 *
234 * RNDIS uses internal framing, and explicitly allows senders to
235 * pad to end-of-packet. That's potentially nice for speed, but
236 * means receivers can't recover lost synch on their own (because
237 * new packets don't only start after a short RX).
238 */
239 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
240 size += dev->port_usb->header_len;
241 size += out->maxpacket - 1;
242 size -= size % out->maxpacket;
243
244 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
245 if (skb == NULL) {
246 DBG(dev, "no rx skb\n");
247 goto enomem;
248 }
249
250 /* Some platforms perform better when IP packets are aligned,
251 * but on at least one, checksumming fails otherwise. Note:
252 * RNDIS headers involve variable numbers of LE32 values.
253 */
254 skb_reserve(skb, NET_IP_ALIGN);
255
256 req->buf = skb->data;
257 req->length = size;
258 req->complete = rx_complete;
259 req->context = skb;
260
261 retval = usb_ep_queue(out, req, gfp_flags);
262 if (retval == -ENOMEM)
263 enomem:
264 defer_kevent(dev, WORK_RX_MEMORY);
265 if (retval) {
266 DBG(dev, "rx submit --> %d\n", retval);
267 if (skb)
268 dev_kfree_skb_any(skb);
269 spin_lock_irqsave(&dev->req_lock, flags);
270 list_add(&req->list, &dev->rx_reqs);
271 spin_unlock_irqrestore(&dev->req_lock, flags);
272 }
273 return retval;
274 }
275
rx_complete(struct usb_ep * ep,struct usb_request * req)276 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
277 {
278 struct sk_buff *skb = req->context;
279 struct eth_dev *dev = ep->driver_data;
280 int status = req->status;
281
282 switch (status) {
283
284 /* normal completion */
285 case 0:
286 skb_put(skb, req->actual);
287 if (dev->unwrap)
288 status = dev->unwrap(skb);
289 if (status < 0
290 || ETH_HLEN > skb->len
291 || skb->len > ETH_FRAME_LEN) {
292 dev->net->stats.rx_errors++;
293 dev->net->stats.rx_length_errors++;
294 DBG(dev, "rx length %d\n", skb->len);
295 break;
296 }
297
298 skb->protocol = eth_type_trans(skb, dev->net);
299 dev->net->stats.rx_packets++;
300 dev->net->stats.rx_bytes += skb->len;
301
302 /* no buffer copies needed, unless hardware can't
303 * use skb buffers.
304 */
305 status = netif_rx(skb);
306 skb = NULL;
307 break;
308
309 /* software-driven interface shutdown */
310 case -ECONNRESET: /* unlink */
311 case -ESHUTDOWN: /* disconnect etc */
312 VDBG(dev, "rx shutdown, code %d\n", status);
313 goto quiesce;
314
315 /* for hardware automagic (such as pxa) */
316 case -ECONNABORTED: /* endpoint reset */
317 DBG(dev, "rx %s reset\n", ep->name);
318 defer_kevent(dev, WORK_RX_MEMORY);
319 quiesce:
320 dev_kfree_skb_any(skb);
321 goto clean;
322
323 /* data overrun */
324 case -EOVERFLOW:
325 dev->net->stats.rx_over_errors++;
326 /* FALLTHROUGH */
327
328 default:
329 dev->net->stats.rx_errors++;
330 DBG(dev, "rx status %d\n", status);
331 break;
332 }
333
334 if (skb)
335 dev_kfree_skb_any(skb);
336 if (!netif_running(dev->net)) {
337 clean:
338 spin_lock(&dev->req_lock);
339 list_add(&req->list, &dev->rx_reqs);
340 spin_unlock(&dev->req_lock);
341 req = NULL;
342 }
343 if (req)
344 rx_submit(dev, req, GFP_ATOMIC);
345 }
346
prealloc(struct list_head * list,struct usb_ep * ep,unsigned n)347 static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
348 {
349 unsigned i;
350 struct usb_request *req;
351
352 if (!n)
353 return -ENOMEM;
354
355 /* queue/recycle up to N requests */
356 i = n;
357 list_for_each_entry(req, list, list) {
358 if (i-- == 0)
359 goto extra;
360 }
361 while (i--) {
362 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
363 if (!req)
364 return list_empty(list) ? -ENOMEM : 0;
365 list_add(&req->list, list);
366 }
367 return 0;
368
369 extra:
370 /* free extras */
371 for (;;) {
372 struct list_head *next;
373
374 next = req->list.next;
375 list_del(&req->list);
376 usb_ep_free_request(ep, req);
377
378 if (next == list)
379 break;
380
381 req = container_of(next, struct usb_request, list);
382 }
383 return 0;
384 }
385
alloc_requests(struct eth_dev * dev,struct gether * link,unsigned n)386 static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
387 {
388 int status;
389
390 spin_lock(&dev->req_lock);
391 status = prealloc(&dev->tx_reqs, link->in_ep, n);
392 if (status < 0)
393 goto fail;
394 status = prealloc(&dev->rx_reqs, link->out_ep, n);
395 if (status < 0)
396 goto fail;
397 goto done;
398 fail:
399 DBG(dev, "can't alloc requests\n");
400 done:
401 spin_unlock(&dev->req_lock);
402 return status;
403 }
404
rx_fill(struct eth_dev * dev,gfp_t gfp_flags)405 static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
406 {
407 struct usb_request *req;
408 unsigned long flags;
409
410 /* fill unused rxq slots with some skb */
411 spin_lock_irqsave(&dev->req_lock, flags);
412 while (!list_empty(&dev->rx_reqs)) {
413 req = container_of(dev->rx_reqs.next,
414 struct usb_request, list);
415 list_del_init(&req->list);
416 spin_unlock_irqrestore(&dev->req_lock, flags);
417
418 if (rx_submit(dev, req, gfp_flags) < 0) {
419 defer_kevent(dev, WORK_RX_MEMORY);
420 return;
421 }
422
423 spin_lock_irqsave(&dev->req_lock, flags);
424 }
425 spin_unlock_irqrestore(&dev->req_lock, flags);
426 }
427
eth_work(struct work_struct * work)428 static void eth_work(struct work_struct *work)
429 {
430 struct eth_dev *dev = container_of(work, struct eth_dev, work);
431
432 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
433 if (netif_running(dev->net))
434 rx_fill(dev, GFP_KERNEL);
435 }
436
437 if (dev->todo)
438 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
439 }
440
tx_complete(struct usb_ep * ep,struct usb_request * req)441 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
442 {
443 struct sk_buff *skb = req->context;
444 struct eth_dev *dev = ep->driver_data;
445
446 switch (req->status) {
447 default:
448 dev->net->stats.tx_errors++;
449 VDBG(dev, "tx err %d\n", req->status);
450 /* FALLTHROUGH */
451 case -ECONNRESET: /* unlink */
452 case -ESHUTDOWN: /* disconnect etc */
453 break;
454 case 0:
455 dev->net->stats.tx_bytes += skb->len;
456 }
457 dev->net->stats.tx_packets++;
458
459 spin_lock(&dev->req_lock);
460 list_add(&req->list, &dev->tx_reqs);
461 spin_unlock(&dev->req_lock);
462 dev_kfree_skb_any(skb);
463
464 atomic_dec(&dev->tx_qlen);
465 if (netif_carrier_ok(dev->net))
466 netif_wake_queue(dev->net);
467 }
468
is_promisc(u16 cdc_filter)469 static inline int is_promisc(u16 cdc_filter)
470 {
471 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
472 }
473
eth_start_xmit(struct sk_buff * skb,struct net_device * net)474 static int eth_start_xmit(struct sk_buff *skb, struct net_device *net)
475 {
476 struct eth_dev *dev = netdev_priv(net);
477 int length = skb->len;
478 int retval;
479 struct usb_request *req = NULL;
480 unsigned long flags;
481 struct usb_ep *in;
482 u16 cdc_filter;
483
484 spin_lock_irqsave(&dev->lock, flags);
485 if (dev->port_usb) {
486 in = dev->port_usb->in_ep;
487 cdc_filter = dev->port_usb->cdc_filter;
488 } else {
489 in = NULL;
490 cdc_filter = 0;
491 }
492 spin_unlock_irqrestore(&dev->lock, flags);
493
494 if (!in) {
495 dev_kfree_skb_any(skb);
496 return 0;
497 }
498
499 /* apply outgoing CDC or RNDIS filters */
500 if (!is_promisc(cdc_filter)) {
501 u8 *dest = skb->data;
502
503 if (is_multicast_ether_addr(dest)) {
504 u16 type;
505
506 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
507 * SET_ETHERNET_MULTICAST_FILTERS requests
508 */
509 if (is_broadcast_ether_addr(dest))
510 type = USB_CDC_PACKET_TYPE_BROADCAST;
511 else
512 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
513 if (!(cdc_filter & type)) {
514 dev_kfree_skb_any(skb);
515 return 0;
516 }
517 }
518 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
519 }
520
521 spin_lock_irqsave(&dev->req_lock, flags);
522 /*
523 * this freelist can be empty if an interrupt triggered disconnect()
524 * and reconfigured the gadget (shutting down this queue) after the
525 * network stack decided to xmit but before we got the spinlock.
526 */
527 if (list_empty(&dev->tx_reqs)) {
528 spin_unlock_irqrestore(&dev->req_lock, flags);
529 return 1;
530 }
531
532 req = container_of(dev->tx_reqs.next, struct usb_request, list);
533 list_del(&req->list);
534
535 /* temporarily stop TX queue when the freelist empties */
536 if (list_empty(&dev->tx_reqs))
537 netif_stop_queue(net);
538 spin_unlock_irqrestore(&dev->req_lock, flags);
539
540 /* no buffer copies needed, unless the network stack did it
541 * or the hardware can't use skb buffers.
542 * or there's not enough space for extra headers we need
543 */
544 if (dev->wrap) {
545 struct sk_buff *skb_new;
546
547 skb_new = dev->wrap(skb);
548 if (!skb_new)
549 goto drop;
550
551 dev_kfree_skb_any(skb);
552 skb = skb_new;
553 length = skb->len;
554 }
555 req->buf = skb->data;
556 req->context = skb;
557 req->complete = tx_complete;
558
559 /* use zlp framing on tx for strict CDC-Ether conformance,
560 * though any robust network rx path ignores extra padding.
561 * and some hardware doesn't like to write zlps.
562 */
563 req->zero = 1;
564 if (!dev->zlp && (length % in->maxpacket) == 0)
565 length++;
566
567 req->length = length;
568
569 /* throttle highspeed IRQ rate back slightly */
570 if (gadget_is_dualspeed(dev->gadget))
571 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
572 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
573 : 0;
574
575 retval = usb_ep_queue(in, req, GFP_ATOMIC);
576 switch (retval) {
577 default:
578 DBG(dev, "tx queue err %d\n", retval);
579 break;
580 case 0:
581 net->trans_start = jiffies;
582 atomic_inc(&dev->tx_qlen);
583 }
584
585 if (retval) {
586 drop:
587 dev->net->stats.tx_dropped++;
588 dev_kfree_skb_any(skb);
589 spin_lock_irqsave(&dev->req_lock, flags);
590 if (list_empty(&dev->tx_reqs))
591 netif_start_queue(net);
592 list_add(&req->list, &dev->tx_reqs);
593 spin_unlock_irqrestore(&dev->req_lock, flags);
594 }
595 return 0;
596 }
597
598 /*-------------------------------------------------------------------------*/
599
eth_start(struct eth_dev * dev,gfp_t gfp_flags)600 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
601 {
602 DBG(dev, "%s\n", __func__);
603
604 /* fill the rx queue */
605 rx_fill(dev, gfp_flags);
606
607 /* and open the tx floodgates */
608 atomic_set(&dev->tx_qlen, 0);
609 netif_wake_queue(dev->net);
610 }
611
eth_open(struct net_device * net)612 static int eth_open(struct net_device *net)
613 {
614 struct eth_dev *dev = netdev_priv(net);
615 struct gether *link;
616
617 DBG(dev, "%s\n", __func__);
618 if (netif_carrier_ok(dev->net))
619 eth_start(dev, GFP_KERNEL);
620
621 spin_lock_irq(&dev->lock);
622 link = dev->port_usb;
623 if (link && link->open)
624 link->open(link);
625 spin_unlock_irq(&dev->lock);
626
627 return 0;
628 }
629
eth_stop(struct net_device * net)630 static int eth_stop(struct net_device *net)
631 {
632 struct eth_dev *dev = netdev_priv(net);
633 unsigned long flags;
634
635 VDBG(dev, "%s\n", __func__);
636 netif_stop_queue(net);
637
638 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
639 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
640 dev->net->stats.rx_errors, dev->net->stats.tx_errors
641 );
642
643 /* ensure there are no more active requests */
644 spin_lock_irqsave(&dev->lock, flags);
645 if (dev->port_usb) {
646 struct gether *link = dev->port_usb;
647
648 if (link->close)
649 link->close(link);
650
651 /* NOTE: we have no abort-queue primitive we could use
652 * to cancel all pending I/O. Instead, we disable then
653 * reenable the endpoints ... this idiom may leave toggle
654 * wrong, but that's a self-correcting error.
655 *
656 * REVISIT: we *COULD* just let the transfers complete at
657 * their own pace; the network stack can handle old packets.
658 * For the moment we leave this here, since it works.
659 */
660 usb_ep_disable(link->in_ep);
661 usb_ep_disable(link->out_ep);
662 if (netif_carrier_ok(net)) {
663 DBG(dev, "host still using in/out endpoints\n");
664 usb_ep_enable(link->in_ep, link->in);
665 usb_ep_enable(link->out_ep, link->out);
666 }
667 }
668 spin_unlock_irqrestore(&dev->lock, flags);
669
670 return 0;
671 }
672
673 /*-------------------------------------------------------------------------*/
674
675 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
676 static char *dev_addr;
677 module_param(dev_addr, charp, S_IRUGO);
678 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
679
680 /* this address is invisible to ifconfig */
681 static char *host_addr;
682 module_param(host_addr, charp, S_IRUGO);
683 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
684
685
nibble(unsigned char c)686 static u8 __init nibble(unsigned char c)
687 {
688 if (isdigit(c))
689 return c - '0';
690 c = toupper(c);
691 if (isxdigit(c))
692 return 10 + c - 'A';
693 return 0;
694 }
695
get_ether_addr(const char * str,u8 * dev_addr)696 static int __init get_ether_addr(const char *str, u8 *dev_addr)
697 {
698 if (str) {
699 unsigned i;
700
701 for (i = 0; i < 6; i++) {
702 unsigned char num;
703
704 if ((*str == '.') || (*str == ':'))
705 str++;
706 num = nibble(*str++) << 4;
707 num |= (nibble(*str++));
708 dev_addr [i] = num;
709 }
710 if (is_valid_ether_addr(dev_addr))
711 return 0;
712 }
713 random_ether_addr(dev_addr);
714 return 1;
715 }
716
717 static struct eth_dev *the_dev;
718
719 static 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 };
727
728 /**
729 * gether_setup - initialize one ethernet-over-usb link
730 * @g: gadget to associated with these links
731 * @ethaddr: NULL, or a buffer in which the ethernet address of the
732 * host side of the link is recorded
733 * Context: may sleep
734 *
735 * This sets up the single network link that may be exported by a
736 * gadget driver using this framework. The link layer addresses are
737 * set up using module parameters.
738 *
739 * Returns negative errno, or zero on success
740 */
gether_setup(struct usb_gadget * g,u8 ethaddr[ETH_ALEN])741 int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
742 {
743 struct eth_dev *dev;
744 struct net_device *net;
745 int status;
746
747 if (the_dev)
748 return -EBUSY;
749
750 net = alloc_etherdev(sizeof *dev);
751 if (!net)
752 return -ENOMEM;
753
754 dev = netdev_priv(net);
755 spin_lock_init(&dev->lock);
756 spin_lock_init(&dev->req_lock);
757 INIT_WORK(&dev->work, eth_work);
758 INIT_LIST_HEAD(&dev->tx_reqs);
759 INIT_LIST_HEAD(&dev->rx_reqs);
760
761 /* network device setup */
762 dev->net = net;
763 strcpy(net->name, "usb%d");
764
765 if (get_ether_addr(dev_addr, net->dev_addr))
766 dev_warn(&g->dev,
767 "using random %s ethernet address\n", "self");
768 if (get_ether_addr(host_addr, dev->host_mac))
769 dev_warn(&g->dev,
770 "using random %s ethernet address\n", "host");
771
772 if (ethaddr)
773 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
774
775 net->netdev_ops = ð_netdev_ops;
776
777 SET_ETHTOOL_OPS(net, &ops);
778
779 /* two kinds of host-initiated state changes:
780 * - iff DATA transfer is active, carrier is "on"
781 * - tx queueing enabled if open *and* carrier is "on"
782 */
783 netif_stop_queue(net);
784 netif_carrier_off(net);
785
786 dev->gadget = g;
787 SET_NETDEV_DEV(net, &g->dev);
788
789 status = register_netdev(net);
790 if (status < 0) {
791 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
792 free_netdev(net);
793 } else {
794 INFO(dev, "MAC %pM\n", net->dev_addr);
795 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
796
797 the_dev = dev;
798 }
799
800 return status;
801 }
802
803 /**
804 * gether_cleanup - remove Ethernet-over-USB device
805 * Context: may sleep
806 *
807 * This is called to free all resources allocated by @gether_setup().
808 */
gether_cleanup(void)809 void gether_cleanup(void)
810 {
811 if (!the_dev)
812 return;
813
814 unregister_netdev(the_dev->net);
815 free_netdev(the_dev->net);
816
817 /* assuming we used keventd, it must quiesce too */
818 flush_scheduled_work();
819
820 the_dev = NULL;
821 }
822
823
824 /**
825 * gether_connect - notify network layer that USB link is active
826 * @link: the USB link, set up with endpoints, descriptors matching
827 * current device speed, and any framing wrapper(s) set up.
828 * Context: irqs blocked
829 *
830 * This is called to activate endpoints and let the network layer know
831 * the connection is active ("carrier detect"). It may cause the I/O
832 * queues to open and start letting network packets flow, but will in
833 * any case activate the endpoints so that they respond properly to the
834 * USB host.
835 *
836 * Verify net_device pointer returned using IS_ERR(). If it doesn't
837 * indicate some error code (negative errno), ep->driver_data values
838 * have been overwritten.
839 */
gether_connect(struct gether * link)840 struct net_device *gether_connect(struct gether *link)
841 {
842 struct eth_dev *dev = the_dev;
843 int result = 0;
844
845 if (!dev)
846 return ERR_PTR(-EINVAL);
847
848 link->in_ep->driver_data = dev;
849 result = usb_ep_enable(link->in_ep, link->in);
850 if (result != 0) {
851 DBG(dev, "enable %s --> %d\n",
852 link->in_ep->name, result);
853 goto fail0;
854 }
855
856 link->out_ep->driver_data = dev;
857 result = usb_ep_enable(link->out_ep, link->out);
858 if (result != 0) {
859 DBG(dev, "enable %s --> %d\n",
860 link->out_ep->name, result);
861 goto fail1;
862 }
863
864 if (result == 0)
865 result = alloc_requests(dev, link, qlen(dev->gadget));
866
867 if (result == 0) {
868 dev->zlp = link->is_zlp_ok;
869 DBG(dev, "qlen %d\n", qlen(dev->gadget));
870
871 dev->header_len = link->header_len;
872 dev->unwrap = link->unwrap;
873 dev->wrap = link->wrap;
874
875 spin_lock(&dev->lock);
876 dev->port_usb = link;
877 link->ioport = dev;
878 if (netif_running(dev->net)) {
879 if (link->open)
880 link->open(link);
881 } else {
882 if (link->close)
883 link->close(link);
884 }
885 spin_unlock(&dev->lock);
886
887 netif_carrier_on(dev->net);
888 if (netif_running(dev->net))
889 eth_start(dev, GFP_ATOMIC);
890
891 /* on error, disable any endpoints */
892 } else {
893 (void) usb_ep_disable(link->out_ep);
894 fail1:
895 (void) usb_ep_disable(link->in_ep);
896 }
897 fail0:
898 /* caller is responsible for cleanup on error */
899 if (result < 0)
900 return ERR_PTR(result);
901 return dev->net;
902 }
903
904 /**
905 * gether_disconnect - notify network layer that USB link is inactive
906 * @link: the USB link, on which gether_connect() was called
907 * Context: irqs blocked
908 *
909 * This is called to deactivate endpoints and let the network layer know
910 * the connection went inactive ("no carrier").
911 *
912 * On return, the state is as if gether_connect() had never been called.
913 * The endpoints are inactive, and accordingly without active USB I/O.
914 * Pointers to endpoint descriptors and endpoint private data are nulled.
915 */
gether_disconnect(struct gether * link)916 void gether_disconnect(struct gether *link)
917 {
918 struct eth_dev *dev = link->ioport;
919 struct usb_request *req;
920
921 WARN_ON(!dev);
922 if (!dev)
923 return;
924
925 DBG(dev, "%s\n", __func__);
926
927 netif_stop_queue(dev->net);
928 netif_carrier_off(dev->net);
929
930 /* disable endpoints, forcing (synchronous) completion
931 * of all pending i/o. then free the request objects
932 * and forget about the endpoints.
933 */
934 usb_ep_disable(link->in_ep);
935 spin_lock(&dev->req_lock);
936 while (!list_empty(&dev->tx_reqs)) {
937 req = container_of(dev->tx_reqs.next,
938 struct usb_request, list);
939 list_del(&req->list);
940
941 spin_unlock(&dev->req_lock);
942 usb_ep_free_request(link->in_ep, req);
943 spin_lock(&dev->req_lock);
944 }
945 spin_unlock(&dev->req_lock);
946 link->in_ep->driver_data = NULL;
947 link->in = NULL;
948
949 usb_ep_disable(link->out_ep);
950 spin_lock(&dev->req_lock);
951 while (!list_empty(&dev->rx_reqs)) {
952 req = container_of(dev->rx_reqs.next,
953 struct usb_request, list);
954 list_del(&req->list);
955
956 spin_unlock(&dev->req_lock);
957 usb_ep_free_request(link->out_ep, req);
958 spin_lock(&dev->req_lock);
959 }
960 spin_unlock(&dev->req_lock);
961 link->out_ep->driver_data = NULL;
962 link->out = NULL;
963
964 /* finish forgetting about this USB link episode */
965 dev->header_len = 0;
966 dev->unwrap = NULL;
967 dev->wrap = NULL;
968
969 spin_lock(&dev->lock);
970 dev->port_usb = NULL;
971 link->ioport = NULL;
972 spin_unlock(&dev->lock);
973 }
974