2 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
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.
14 /* #define VERBOSE_DEBUG */
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/gfp.h>
19 #include <linux/device.h>
20 #include <linux/ctype.h>
21 #include <linux/etherdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/if_vlan.h>
29 * This component encapsulates the Ethernet link glue needed to provide
30 * one (!) network link through the USB gadget stack, normally "usb0".
32 * The control and data models are handled by the function driver which
33 * connects to this code; such as CDC Ethernet (ECM or EEM),
34 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
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.
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.)
49 #define UETH__VERSION "29-May-2008"
51 /* Experiments show that both Linux and Windows hosts allow up to 16k
52 * frame sizes. Set the max size to 15k+52 to prevent allocating 32k
53 * blocks and still have efficient handling. */
54 #define GETHER_MAX_ETH_FRAME_LEN 15412
56 static struct workqueue_struct *uether_wq;
59 /* lock is held while accessing port_usb
62 struct gether *port_usb;
64 struct net_device *net;
65 struct usb_gadget *gadget;
67 spinlock_t req_lock; /* guard {rx,tx}_reqs */
68 struct list_head tx_reqs, rx_reqs;
70 /* Minimum number of TX USB request queued to UDC */
71 #define TX_REQ_THRESHOLD 5
73 int tx_skb_hold_count;
76 struct sk_buff_head rx_frames;
81 unsigned ul_max_pkts_per_xfer;
82 unsigned dl_max_pkts_per_xfer;
83 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
84 int (*unwrap)(struct gether *,
86 struct sk_buff_head *list);
88 struct work_struct work;
89 struct work_struct rx_work;
92 #define WORK_RX_MEMORY 0
95 u8 host_mac[ETH_ALEN];
99 /*-------------------------------------------------------------------------*/
101 #define RX_EXTRA 20 /* bytes guarding against rx overflows */
103 #define DEFAULT_QLEN 2 /* double buffering by default */
105 /* for dual-speed hardware, use deeper queues at high/super speed */
106 static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
108 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
109 gadget->speed == USB_SPEED_SUPER))
110 return qmult * DEFAULT_QLEN;
115 /*-------------------------------------------------------------------------*/
117 /* REVISIT there must be a better way than having two sets
126 #define xprintk(d, level, fmt, args...) \
127 printk(level "%s: " fmt , (d)->net->name , ## args)
131 #define DBG(dev, fmt, args...) \
132 xprintk(dev , KERN_DEBUG , fmt , ## args)
134 #define DBG(dev, fmt, args...) \
141 #define VDBG(dev, fmt, args...) \
145 #define ERROR(dev, fmt, args...) \
146 xprintk(dev , KERN_ERR , fmt , ## args)
147 #define INFO(dev, fmt, args...) \
148 xprintk(dev , KERN_INFO , fmt , ## args)
150 /*-------------------------------------------------------------------------*/
152 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
154 static int ueth_change_mtu(struct net_device *net, int new_mtu)
156 struct eth_dev *dev = netdev_priv(net);
160 /* don't change MTU on "live" link (peer won't know) */
161 spin_lock_irqsave(&dev->lock, flags);
164 else if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
168 spin_unlock_irqrestore(&dev->lock, flags);
173 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
175 struct eth_dev *dev = netdev_priv(net);
177 strlcpy(p->driver, "g_ether", sizeof(p->driver));
178 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
179 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));
183 /* REVISIT can also support:
184 * - WOL (by tracking suspends and issuing remote wakeup)
185 * - msglevel (implies updated messaging)
186 * - ... probably more ethtool ops
189 static const struct ethtool_ops ops = {
190 .get_drvinfo = eth_get_drvinfo,
191 .get_link = ethtool_op_get_link,
194 static void defer_kevent(struct eth_dev *dev, int flag)
196 if (test_and_set_bit(flag, &dev->todo))
198 if (!schedule_work(&dev->work))
199 ERROR(dev, "kevent %d may have been dropped\n", flag);
201 DBG(dev, "kevent %d scheduled\n", flag);
204 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
207 rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
210 int retval = -ENOMEM;
215 spin_lock_irqsave(&dev->lock, flags);
217 out = dev->port_usb->out_ep;
220 spin_unlock_irqrestore(&dev->lock, flags);
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).
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).
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;
243 if (dev->ul_max_pkts_per_xfer)
244 size *= dev->ul_max_pkts_per_xfer;
246 if (dev->port_usb->is_fixed)
247 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
249 DBG(dev, "%s: size: %zd\n", __func__, size);
250 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
252 DBG(dev, "no rx skb\n");
256 /* Some platforms perform better when IP packets are aligned,
257 * but on at least one, checksumming fails otherwise. Note:
258 * RNDIS headers involve variable numbers of LE32 values.
260 skb_reserve(skb, NET_IP_ALIGN);
262 req->buf = skb->data;
264 req->complete = rx_complete;
267 retval = usb_ep_queue(out, req, gfp_flags);
268 if (retval == -ENOMEM)
270 defer_kevent(dev, WORK_RX_MEMORY);
272 DBG(dev, "rx submit --> %d\n", retval);
274 dev_kfree_skb_any(skb);
279 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
281 struct sk_buff *skb = req->context;
282 struct eth_dev *dev = ep->driver_data;
283 int status = req->status;
288 /* normal completion */
290 skb_put(skb, req->actual);
295 spin_lock_irqsave(&dev->lock, flags);
297 status = dev->unwrap(dev->port_usb,
300 if (status == -EINVAL)
301 dev->net->stats.rx_errors++;
302 else if (status == -EOVERFLOW)
303 dev->net->stats.rx_over_errors++;
305 dev_kfree_skb_any(skb);
308 spin_unlock_irqrestore(&dev->lock, flags);
310 skb_queue_tail(&dev->rx_frames, skb);
316 /* software-driven interface shutdown */
317 case -ECONNRESET: /* unlink */
318 case -ESHUTDOWN: /* disconnect etc */
319 VDBG(dev, "rx shutdown, code %d\n", status);
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);
327 dev_kfree_skb_any(skb);
332 dev->net->stats.rx_over_errors++;
337 dev_kfree_skb_any(skb);
338 dev->net->stats.rx_errors++;
339 DBG(dev, "rx status %d\n", status);
344 spin_lock(&dev->req_lock);
345 list_add(&req->list, &dev->rx_reqs);
346 spin_unlock(&dev->req_lock);
349 queue_work(uether_wq, &dev->rx_work);
352 static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
355 struct usb_request *req;
360 /* queue/recycle up to N requests */
362 list_for_each_entry(req, list, list) {
367 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
369 return list_empty(list) ? -ENOMEM : 0;
370 list_add(&req->list, list);
377 struct list_head *next;
379 next = req->list.next;
380 list_del(&req->list);
381 usb_ep_free_request(ep, req);
386 req = container_of(next, struct usb_request, list);
391 static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
395 spin_lock(&dev->req_lock);
396 status = prealloc(&dev->tx_reqs, link->in_ep, n);
399 status = prealloc(&dev->rx_reqs, link->out_ep, n);
404 DBG(dev, "can't alloc requests\n");
406 spin_unlock(&dev->req_lock);
410 static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
412 struct usb_request *req;
416 /* fill unused rxq slots with some skb */
417 spin_lock_irqsave(&dev->req_lock, flags);
418 while (!list_empty(&dev->rx_reqs)) {
419 /* break the nexus of continuous completion and re-submission*/
420 if (++req_cnt > qlen(dev->gadget, dev->qmult))
423 req = container_of(dev->rx_reqs.next,
424 struct usb_request, list);
425 list_del_init(&req->list);
426 spin_unlock_irqrestore(&dev->req_lock, flags);
428 if (rx_submit(dev, req, gfp_flags) < 0) {
429 spin_lock_irqsave(&dev->req_lock, flags);
430 list_add(&req->list, &dev->rx_reqs);
431 spin_unlock_irqrestore(&dev->req_lock, flags);
432 defer_kevent(dev, WORK_RX_MEMORY);
436 spin_lock_irqsave(&dev->req_lock, flags);
438 spin_unlock_irqrestore(&dev->req_lock, flags);
441 static void process_rx_w(struct work_struct *work)
443 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
450 while ((skb = skb_dequeue(&dev->rx_frames))) {
452 || ETH_HLEN > skb->len
453 || skb->len > ETH_FRAME_LEN) {
454 dev->net->stats.rx_errors++;
455 dev->net->stats.rx_length_errors++;
456 DBG(dev, "rx length %d\n", skb->len);
457 dev_kfree_skb_any(skb);
460 skb->protocol = eth_type_trans(skb, dev->net);
461 dev->net->stats.rx_packets++;
462 dev->net->stats.rx_bytes += skb->len;
464 status = netif_rx_ni(skb);
467 if (netif_running(dev->net))
468 rx_fill(dev, GFP_KERNEL);
471 static void eth_work(struct work_struct *work)
473 struct eth_dev *dev = container_of(work, struct eth_dev, work);
475 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
476 if (netif_running(dev->net))
477 rx_fill(dev, GFP_KERNEL);
481 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
484 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
486 struct sk_buff *skb = req->context;
487 struct eth_dev *dev = ep->driver_data;
488 struct net_device *net = dev->net;
489 struct usb_request *new_req;
494 switch (req->status) {
496 dev->net->stats.tx_errors++;
497 VDBG(dev, "tx err %d\n", req->status);
499 case -ECONNRESET: /* unlink */
500 case -ESHUTDOWN: /* disconnect etc */
504 dev->net->stats.tx_bytes += req->length-1;
506 dev->net->stats.tx_bytes += req->length;
508 dev->net->stats.tx_packets++;
510 spin_lock(&dev->req_lock);
511 list_add_tail(&req->list, &dev->tx_reqs);
513 if (dev->port_usb->multi_pkt_xfer) {
514 dev->no_tx_req_used--;
516 in = dev->port_usb->in_ep;
518 if (!list_empty(&dev->tx_reqs)) {
519 new_req = container_of(dev->tx_reqs.next,
520 struct usb_request, list);
521 list_del(&new_req->list);
522 spin_unlock(&dev->req_lock);
523 if (new_req->length > 0) {
524 length = new_req->length;
526 /* NCM requires no zlp if transfer is
528 if (dev->port_usb->is_fixed &&
529 length == dev->port_usb->fixed_in_len &&
530 (length % in->maxpacket) == 0)
535 /* use zlp framing on tx for strict CDC-Ether
536 * conformance, though any robust network rx
537 * path ignores extra padding. and some hardware
538 * doesn't like to write zlps.
540 if (new_req->zero && !dev->zlp &&
541 (length % in->maxpacket) == 0) {
546 new_req->length = length;
547 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
550 DBG(dev, "tx queue err %d\n", retval);
553 spin_lock(&dev->req_lock);
554 dev->no_tx_req_used++;
555 spin_unlock(&dev->req_lock);
556 net->trans_start = jiffies;
559 spin_lock(&dev->req_lock);
560 list_add(&new_req->list, &dev->tx_reqs);
561 spin_unlock(&dev->req_lock);
564 spin_unlock(&dev->req_lock);
567 spin_unlock(&dev->req_lock);
568 dev_kfree_skb_any(skb);
571 if (netif_carrier_ok(dev->net))
572 netif_wake_queue(dev->net);
575 static inline int is_promisc(u16 cdc_filter)
577 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
580 static void alloc_tx_buffer(struct eth_dev *dev)
582 struct list_head *act;
583 struct usb_request *req;
585 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
587 + sizeof(struct ethhdr)
588 /* size of rndis_packet_msg_type */
592 list_for_each(act, &dev->tx_reqs) {
593 req = container_of(act, struct usb_request, list);
595 req->buf = kmalloc(dev->tx_req_bufsize,
600 static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
601 struct net_device *net)
603 struct eth_dev *dev = netdev_priv(net);
606 struct usb_request *req = NULL;
611 spin_lock_irqsave(&dev->lock, flags);
613 in = dev->port_usb->in_ep;
614 cdc_filter = dev->port_usb->cdc_filter;
619 spin_unlock_irqrestore(&dev->lock, flags);
622 dev_kfree_skb_any(skb);
626 /* Allocate memory for tx_reqs to support multi packet transfer */
627 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
628 alloc_tx_buffer(dev);
630 /* apply outgoing CDC or RNDIS filters */
631 if (skb && !is_promisc(cdc_filter)) {
632 u8 *dest = skb->data;
634 if (is_multicast_ether_addr(dest)) {
637 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
638 * SET_ETHERNET_MULTICAST_FILTERS requests
640 if (is_broadcast_ether_addr(dest))
641 type = USB_CDC_PACKET_TYPE_BROADCAST;
643 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
644 if (!(cdc_filter & type)) {
645 dev_kfree_skb_any(skb);
649 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
652 spin_lock_irqsave(&dev->req_lock, flags);
654 * this freelist can be empty if an interrupt triggered disconnect()
655 * and reconfigured the gadget (shutting down this queue) after the
656 * network stack decided to xmit but before we got the spinlock.
658 if (list_empty(&dev->tx_reqs)) {
659 spin_unlock_irqrestore(&dev->req_lock, flags);
660 return NETDEV_TX_BUSY;
663 req = container_of(dev->tx_reqs.next, struct usb_request, list);
664 list_del(&req->list);
666 /* temporarily stop TX queue when the freelist empties */
667 if (list_empty(&dev->tx_reqs))
668 netif_stop_queue(net);
669 spin_unlock_irqrestore(&dev->req_lock, flags);
671 /* no buffer copies needed, unless the network stack did it
672 * or the hardware can't use skb buffers.
673 * or there's not enough space for extra headers we need
678 spin_lock_irqsave(&dev->lock, flags);
680 skb = dev->wrap(dev->port_usb, skb);
681 spin_unlock_irqrestore(&dev->lock, flags);
683 /* Multi frame CDC protocols may store the frame for
684 * later which is not a dropped frame.
686 if (dev->port_usb->supports_multi_frame)
692 spin_lock_irqsave(&dev->req_lock, flags);
693 dev->tx_skb_hold_count++;
694 spin_unlock_irqrestore(&dev->req_lock, flags);
696 if (dev->port_usb->multi_pkt_xfer) {
697 memcpy(req->buf + req->length, skb->data, skb->len);
698 req->length = req->length + skb->len;
699 length = req->length;
700 dev_kfree_skb_any(skb);
702 spin_lock_irqsave(&dev->req_lock, flags);
703 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
704 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
705 list_add(&req->list, &dev->tx_reqs);
706 spin_unlock_irqrestore(&dev->req_lock, flags);
711 dev->no_tx_req_used++;
712 spin_unlock_irqrestore(&dev->req_lock, flags);
714 spin_lock_irqsave(&dev->lock, flags);
715 dev->tx_skb_hold_count = 0;
716 spin_unlock_irqrestore(&dev->lock, flags);
719 req->buf = skb->data;
723 req->complete = tx_complete;
725 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
726 if (dev->port_usb->is_fixed &&
727 length == dev->port_usb->fixed_in_len &&
728 (length % in->maxpacket) == 0)
733 /* use zlp framing on tx for strict CDC-Ether conformance,
734 * though any robust network rx path ignores extra padding.
735 * and some hardware doesn't like to write zlps.
737 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
742 req->length = length;
744 /* throttle highspeed IRQ rate back slightly */
745 if (gadget_is_dualspeed(dev->gadget) &&
746 (dev->gadget->speed == USB_SPEED_HIGH)) {
748 if (dev->tx_qlen == (dev->qmult/2)) {
749 req->no_interrupt = 0;
752 req->no_interrupt = 1;
755 req->no_interrupt = 0;
758 retval = usb_ep_queue(in, req, GFP_ATOMIC);
761 DBG(dev, "tx queue err %d\n", retval);
764 net->trans_start = jiffies;
768 if (!dev->port_usb->multi_pkt_xfer)
769 dev_kfree_skb_any(skb);
771 dev->net->stats.tx_dropped++;
773 spin_lock_irqsave(&dev->req_lock, flags);
774 if (list_empty(&dev->tx_reqs))
775 netif_start_queue(net);
776 list_add(&req->list, &dev->tx_reqs);
777 spin_unlock_irqrestore(&dev->req_lock, flags);
783 /*-------------------------------------------------------------------------*/
785 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
787 DBG(dev, "%s\n", __func__);
789 /* fill the rx queue */
790 rx_fill(dev, gfp_flags);
792 /* and open the tx floodgates */
794 netif_wake_queue(dev->net);
797 static int eth_open(struct net_device *net)
799 struct eth_dev *dev = netdev_priv(net);
802 DBG(dev, "%s\n", __func__);
803 if (netif_carrier_ok(dev->net))
804 eth_start(dev, GFP_KERNEL);
806 spin_lock_irq(&dev->lock);
807 link = dev->port_usb;
808 if (link && link->open)
810 spin_unlock_irq(&dev->lock);
815 static int eth_stop(struct net_device *net)
817 struct eth_dev *dev = netdev_priv(net);
820 VDBG(dev, "%s\n", __func__);
821 netif_stop_queue(net);
823 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
824 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
825 dev->net->stats.rx_errors, dev->net->stats.tx_errors
828 /* ensure there are no more active requests */
829 spin_lock_irqsave(&dev->lock, flags);
831 struct gether *link = dev->port_usb;
832 const struct usb_endpoint_descriptor *in;
833 const struct usb_endpoint_descriptor *out;
838 /* NOTE: we have no abort-queue primitive we could use
839 * to cancel all pending I/O. Instead, we disable then
840 * reenable the endpoints ... this idiom may leave toggle
841 * wrong, but that's a self-correcting error.
843 * REVISIT: we *COULD* just let the transfers complete at
844 * their own pace; the network stack can handle old packets.
845 * For the moment we leave this here, since it works.
847 in = link->in_ep->desc;
848 out = link->out_ep->desc;
849 usb_ep_disable(link->in_ep);
850 usb_ep_disable(link->out_ep);
851 if (netif_carrier_ok(net)) {
852 DBG(dev, "host still using in/out endpoints\n");
853 link->in_ep->desc = in;
854 link->out_ep->desc = out;
855 usb_ep_enable(link->in_ep);
856 usb_ep_enable(link->out_ep);
859 spin_unlock_irqrestore(&dev->lock, flags);
864 /*-------------------------------------------------------------------------*/
866 static u8 host_ethaddr[ETH_ALEN];
868 static int get_ether_addr(const char *str, u8 *dev_addr)
873 for (i = 0; i < 6; i++) {
876 if ((*str == '.') || (*str == ':'))
878 num = hex_to_bin(*str++) << 4;
879 num |= hex_to_bin(*str++);
882 if (is_valid_ether_addr(dev_addr))
885 eth_random_addr(dev_addr);
889 static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
894 snprintf(str, len, "%pM", dev_addr);
898 static int get_host_ether_addr(u8 *str, u8 *dev_addr)
900 memcpy(dev_addr, str, ETH_ALEN);
901 if (is_valid_ether_addr(dev_addr))
904 random_ether_addr(dev_addr);
905 memcpy(str, dev_addr, ETH_ALEN);
909 static const struct net_device_ops eth_netdev_ops = {
910 .ndo_open = eth_open,
911 .ndo_stop = eth_stop,
912 .ndo_start_xmit = eth_start_xmit,
913 .ndo_change_mtu = ueth_change_mtu,
914 .ndo_set_mac_address = eth_mac_addr,
915 .ndo_validate_addr = eth_validate_addr,
918 static struct device_type gadget_type = {
923 * gether_setup_name - initialize one ethernet-over-usb link
924 * @g: gadget to associated with these links
925 * @ethaddr: NULL, or a buffer in which the ethernet address of the
926 * host side of the link is recorded
927 * @netname: name for network device (for example, "usb")
930 * This sets up the single network link that may be exported by a
931 * gadget driver using this framework. The link layer addresses are
932 * set up using module parameters.
934 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
936 struct eth_dev *gether_setup_name(struct usb_gadget *g,
937 const char *dev_addr, const char *host_addr,
938 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
941 struct net_device *net;
944 net = alloc_etherdev(sizeof *dev);
946 return ERR_PTR(-ENOMEM);
948 dev = netdev_priv(net);
949 spin_lock_init(&dev->lock);
950 spin_lock_init(&dev->req_lock);
951 INIT_WORK(&dev->work, eth_work);
952 INIT_WORK(&dev->rx_work, process_rx_w);
953 INIT_LIST_HEAD(&dev->tx_reqs);
954 INIT_LIST_HEAD(&dev->rx_reqs);
956 skb_queue_head_init(&dev->rx_frames);
958 /* network device setup */
961 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
963 if (get_ether_addr(dev_addr, net->dev_addr))
965 "using random %s ethernet address\n", "self");
967 if (get_host_ether_addr(host_ethaddr, dev->host_mac))
968 dev_warn(&g->dev, "using random %s ethernet address\n", "host");
970 dev_warn(&g->dev, "using previous %s ethernet address\n", "host");
973 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
975 net->netdev_ops = ð_netdev_ops;
977 net->ethtool_ops = &ops;
980 SET_NETDEV_DEV(net, &g->dev);
981 SET_NETDEV_DEVTYPE(net, &gadget_type);
983 status = register_netdev(net);
985 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
987 dev = ERR_PTR(status);
989 INFO(dev, "MAC %pM\n", net->dev_addr);
990 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
993 * two kinds of host-initiated state changes:
994 * - iff DATA transfer is active, carrier is "on"
995 * - tx queueing enabled if open *and* carrier is "on"
997 netif_carrier_off(net);
1002 EXPORT_SYMBOL_GPL(gether_setup_name);
1004 struct net_device *gether_setup_name_default(const char *netname)
1006 struct net_device *net;
1007 struct eth_dev *dev;
1009 net = alloc_etherdev(sizeof(*dev));
1011 return ERR_PTR(-ENOMEM);
1013 dev = netdev_priv(net);
1014 spin_lock_init(&dev->lock);
1015 spin_lock_init(&dev->req_lock);
1016 INIT_WORK(&dev->work, eth_work);
1017 INIT_LIST_HEAD(&dev->tx_reqs);
1018 INIT_LIST_HEAD(&dev->rx_reqs);
1020 skb_queue_head_init(&dev->rx_frames);
1022 /* network device setup */
1024 dev->qmult = QMULT_DEFAULT;
1025 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1027 eth_random_addr(dev->dev_mac);
1028 pr_warn("using random %s ethernet address\n", "self");
1029 eth_random_addr(dev->host_mac);
1030 pr_warn("using random %s ethernet address\n", "host");
1032 net->netdev_ops = ð_netdev_ops;
1034 net->ethtool_ops = &ops;
1035 SET_NETDEV_DEVTYPE(net, &gadget_type);
1039 EXPORT_SYMBOL_GPL(gether_setup_name_default);
1041 int gether_register_netdev(struct net_device *net)
1043 struct eth_dev *dev;
1044 struct usb_gadget *g;
1048 if (!net->dev.parent)
1050 dev = netdev_priv(net);
1052 status = register_netdev(net);
1054 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1057 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1059 /* two kinds of host-initiated state changes:
1060 * - iff DATA transfer is active, carrier is "on"
1061 * - tx queueing enabled if open *and* carrier is "on"
1063 netif_carrier_off(net);
1065 sa.sa_family = net->type;
1066 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1068 status = dev_set_mac_address(net, &sa);
1071 pr_warn("cannot set self ethernet address: %d\n", status);
1073 INFO(dev, "MAC %pM\n", dev->dev_mac);
1077 EXPORT_SYMBOL_GPL(gether_register_netdev);
1079 void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1081 struct eth_dev *dev;
1083 dev = netdev_priv(net);
1085 SET_NETDEV_DEV(net, &g->dev);
1087 EXPORT_SYMBOL_GPL(gether_set_gadget);
1089 int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1091 struct eth_dev *dev;
1092 u8 new_addr[ETH_ALEN];
1094 dev = netdev_priv(net);
1095 if (get_ether_addr(dev_addr, new_addr))
1097 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1100 EXPORT_SYMBOL_GPL(gether_set_dev_addr);
1102 int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1104 struct eth_dev *dev;
1106 dev = netdev_priv(net);
1107 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1109 EXPORT_SYMBOL_GPL(gether_get_dev_addr);
1111 int gether_set_host_addr(struct net_device *net, const char *host_addr)
1113 struct eth_dev *dev;
1114 u8 new_addr[ETH_ALEN];
1116 dev = netdev_priv(net);
1117 if (get_ether_addr(host_addr, new_addr))
1119 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1122 EXPORT_SYMBOL_GPL(gether_set_host_addr);
1124 int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1126 struct eth_dev *dev;
1128 dev = netdev_priv(net);
1129 return get_ether_addr_str(dev->host_mac, host_addr, len);
1131 EXPORT_SYMBOL_GPL(gether_get_host_addr);
1133 int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1135 struct eth_dev *dev;
1140 dev = netdev_priv(net);
1141 snprintf(host_addr, len, "%pm", dev->host_mac);
1143 return strlen(host_addr);
1145 EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
1147 void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1149 struct eth_dev *dev;
1151 dev = netdev_priv(net);
1152 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1154 EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
1156 void gether_set_qmult(struct net_device *net, unsigned qmult)
1158 struct eth_dev *dev;
1160 dev = netdev_priv(net);
1163 EXPORT_SYMBOL_GPL(gether_set_qmult);
1165 unsigned gether_get_qmult(struct net_device *net)
1167 struct eth_dev *dev;
1169 dev = netdev_priv(net);
1172 EXPORT_SYMBOL_GPL(gether_get_qmult);
1174 int gether_get_ifname(struct net_device *net, char *name, int len)
1177 strlcpy(name, netdev_name(net), len);
1179 return strlen(name);
1181 EXPORT_SYMBOL_GPL(gether_get_ifname);
1184 * gether_cleanup - remove Ethernet-over-USB device
1185 * Context: may sleep
1187 * This is called to free all resources allocated by @gether_setup().
1189 void gether_cleanup(struct eth_dev *dev)
1194 unregister_netdev(dev->net);
1195 flush_work(&dev->work);
1196 free_netdev(dev->net);
1198 EXPORT_SYMBOL_GPL(gether_cleanup);
1201 * gether_connect - notify network layer that USB link is active
1202 * @link: the USB link, set up with endpoints, descriptors matching
1203 * current device speed, and any framing wrapper(s) set up.
1204 * Context: irqs blocked
1206 * This is called to activate endpoints and let the network layer know
1207 * the connection is active ("carrier detect"). It may cause the I/O
1208 * queues to open and start letting network packets flow, but will in
1209 * any case activate the endpoints so that they respond properly to the
1212 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1213 * indicate some error code (negative errno), ep->driver_data values
1214 * have been overwritten.
1216 struct net_device *gether_connect(struct gether *link)
1218 struct eth_dev *dev = link->ioport;
1222 return ERR_PTR(-EINVAL);
1224 link->in_ep->driver_data = dev;
1225 result = usb_ep_enable(link->in_ep);
1227 DBG(dev, "enable %s --> %d\n",
1228 link->in_ep->name, result);
1232 link->out_ep->driver_data = dev;
1233 result = usb_ep_enable(link->out_ep);
1235 DBG(dev, "enable %s --> %d\n",
1236 link->out_ep->name, result);
1241 result = alloc_requests(dev, link, qlen(dev->gadget,
1245 dev->zlp = link->is_zlp_ok;
1246 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
1248 dev->header_len = link->header_len;
1249 dev->unwrap = link->unwrap;
1250 dev->wrap = link->wrap;
1251 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
1252 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
1254 spin_lock(&dev->lock);
1255 dev->tx_skb_hold_count = 0;
1256 dev->no_tx_req_used = 0;
1257 dev->tx_req_bufsize = 0;
1258 dev->port_usb = link;
1259 if (netif_running(dev->net)) {
1266 spin_unlock(&dev->lock);
1268 netif_carrier_on(dev->net);
1269 if (netif_running(dev->net))
1270 eth_start(dev, GFP_ATOMIC);
1272 /* on error, disable any endpoints */
1274 (void) usb_ep_disable(link->out_ep);
1276 (void) usb_ep_disable(link->in_ep);
1279 /* caller is responsible for cleanup on error */
1281 return ERR_PTR(result);
1284 EXPORT_SYMBOL_GPL(gether_connect);
1287 * gether_disconnect - notify network layer that USB link is inactive
1288 * @link: the USB link, on which gether_connect() was called
1289 * Context: irqs blocked
1291 * This is called to deactivate endpoints and let the network layer know
1292 * the connection went inactive ("no carrier").
1294 * On return, the state is as if gether_connect() had never been called.
1295 * The endpoints are inactive, and accordingly without active USB I/O.
1296 * Pointers to endpoint descriptors and endpoint private data are nulled.
1298 void gether_disconnect(struct gether *link)
1300 struct eth_dev *dev = link->ioport;
1301 struct usb_request *req;
1302 struct sk_buff *skb;
1308 DBG(dev, "%s\n", __func__);
1310 netif_stop_queue(dev->net);
1311 netif_carrier_off(dev->net);
1313 /* disable endpoints, forcing (synchronous) completion
1314 * of all pending i/o. then free the request objects
1315 * and forget about the endpoints.
1317 usb_ep_disable(link->in_ep);
1318 spin_lock(&dev->req_lock);
1319 while (!list_empty(&dev->tx_reqs)) {
1320 req = container_of(dev->tx_reqs.next,
1321 struct usb_request, list);
1322 list_del(&req->list);
1324 spin_unlock(&dev->req_lock);
1325 if (link->multi_pkt_xfer)
1327 usb_ep_free_request(link->in_ep, req);
1328 spin_lock(&dev->req_lock);
1330 spin_unlock(&dev->req_lock);
1331 link->in_ep->desc = NULL;
1333 usb_ep_disable(link->out_ep);
1334 spin_lock(&dev->req_lock);
1335 while (!list_empty(&dev->rx_reqs)) {
1336 req = container_of(dev->rx_reqs.next,
1337 struct usb_request, list);
1338 list_del(&req->list);
1340 spin_unlock(&dev->req_lock);
1341 usb_ep_free_request(link->out_ep, req);
1342 spin_lock(&dev->req_lock);
1344 spin_unlock(&dev->req_lock);
1346 spin_lock(&dev->rx_frames.lock);
1347 while ((skb = __skb_dequeue(&dev->rx_frames)))
1348 dev_kfree_skb_any(skb);
1349 spin_unlock(&dev->rx_frames.lock);
1351 link->out_ep->desc = NULL;
1353 /* finish forgetting about this USB link episode */
1354 dev->header_len = 0;
1358 spin_lock(&dev->lock);
1359 dev->port_usb = NULL;
1360 spin_unlock(&dev->lock);
1362 EXPORT_SYMBOL_GPL(gether_disconnect);
1364 static int __init gether_init(void)
1366 uether_wq = create_singlethread_workqueue("uether");
1368 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1373 module_init(gether_init);
1375 static void __exit gether_exit(void)
1377 destroy_workqueue(uether_wq);
1380 module_exit(gether_exit);
1381 MODULE_AUTHOR("David Brownell");
1382 MODULE_DESCRIPTION("ethernet over USB driver");
1383 MODULE_LICENSE("GPL v2");