1 // SPDX-License-Identifier: GPL-2.0+
3 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
10 /* #define VERBOSE_DEBUG */
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/gfp.h>
15 #include <linux/device.h>
16 #include <linux/ctype.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/if_vlan.h>
25 * This component encapsulates the Ethernet link glue needed to provide
26 * one (!) network link through the USB gadget stack, normally "usb0".
28 * The control and data models are handled by the function driver which
29 * connects to this code; such as CDC Ethernet (ECM or EEM),
30 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
33 * Link level addressing is handled by this component using module
34 * parameters; if no such parameters are provided, random link level
35 * addresses are used. Each end of the link uses one address. The
36 * host end address is exported in various ways, and is often recorded
37 * in configuration databases.
39 * The driver which assembles each configuration using such a link is
40 * responsible for ensuring that each configuration includes at most one
41 * instance of is network link. (The network layer provides ways for
42 * this single "physical" link to be used by multiple virtual links.)
45 #define UETH__VERSION "29-May-2008"
47 /* Experiments show that both Linux and Windows hosts allow up to 16k
48 * frame sizes. Set the max MTU size to 15k+52 to prevent allocating 32k
49 * blocks and still have efficient handling. */
50 #define GETHER_MAX_MTU_SIZE 15412
51 #define GETHER_MAX_ETH_FRAME_LEN (GETHER_MAX_MTU_SIZE + ETH_HLEN)
54 /* lock is held while accessing port_usb
57 struct gether
*port_usb
;
59 struct net_device
*net
;
60 struct usb_gadget
*gadget
;
62 spinlock_t req_lock
; /* guard {rx,tx}_reqs */
63 struct list_head tx_reqs
, rx_reqs
;
66 struct sk_buff_head rx_frames
;
71 struct sk_buff
*(*wrap
)(struct gether
*, struct sk_buff
*skb
);
72 int (*unwrap
)(struct gether
*,
74 struct sk_buff_head
*list
);
76 struct work_struct work
;
79 #define WORK_RX_MEMORY 0
83 u8 host_mac
[ETH_ALEN
];
87 /*-------------------------------------------------------------------------*/
89 #define RX_EXTRA 20 /* bytes guarding against rx overflows */
91 #define DEFAULT_QLEN 2 /* double buffering by default */
93 /* for dual-speed hardware, use deeper queues at high/super speed */
94 static inline int qlen(struct usb_gadget
*gadget
, unsigned qmult
)
96 if (gadget_is_dualspeed(gadget
) && (gadget
->speed
== USB_SPEED_HIGH
||
97 gadget
->speed
>= USB_SPEED_SUPER
))
98 return qmult
* DEFAULT_QLEN
;
103 /*-------------------------------------------------------------------------*/
105 /* REVISIT there must be a better way than having two sets
114 #define xprintk(d, level, fmt, args...) \
115 printk(level "%s: " fmt , (d)->net->name , ## args)
119 #define DBG(dev, fmt, args...) \
120 xprintk(dev , KERN_DEBUG , fmt , ## args)
122 #define DBG(dev, fmt, args...) \
129 #define VDBG(dev, fmt, args...) \
133 #define ERROR(dev, fmt, args...) \
134 xprintk(dev , KERN_ERR , fmt , ## args)
135 #define INFO(dev, fmt, args...) \
136 xprintk(dev , KERN_INFO , fmt , ## args)
138 /*-------------------------------------------------------------------------*/
140 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
142 static void eth_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*p
)
144 struct eth_dev
*dev
= netdev_priv(net
);
146 strlcpy(p
->driver
, "g_ether", sizeof(p
->driver
));
147 strlcpy(p
->version
, UETH__VERSION
, sizeof(p
->version
));
148 strlcpy(p
->fw_version
, dev
->gadget
->name
, sizeof(p
->fw_version
));
149 strlcpy(p
->bus_info
, dev_name(&dev
->gadget
->dev
), sizeof(p
->bus_info
));
152 /* REVISIT can also support:
153 * - WOL (by tracking suspends and issuing remote wakeup)
154 * - msglevel (implies updated messaging)
155 * - ... probably more ethtool ops
158 static const struct ethtool_ops ops
= {
159 .get_drvinfo
= eth_get_drvinfo
,
160 .get_link
= ethtool_op_get_link
,
163 static void defer_kevent(struct eth_dev
*dev
, int flag
)
165 if (test_and_set_bit(flag
, &dev
->todo
))
167 if (!schedule_work(&dev
->work
))
168 ERROR(dev
, "kevent %d may have been dropped\n", flag
);
170 DBG(dev
, "kevent %d scheduled\n", flag
);
173 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
);
176 rx_submit(struct eth_dev
*dev
, struct usb_request
*req
, gfp_t gfp_flags
)
178 struct usb_gadget
*g
= dev
->gadget
;
180 int retval
= -ENOMEM
;
185 spin_lock_irqsave(&dev
->lock
, flags
);
187 out
= dev
->port_usb
->out_ep
;
193 spin_unlock_irqrestore(&dev
->lock
, flags
);
197 /* Padding up to RX_EXTRA handles minor disagreements with host.
198 * Normally we use the USB "terminate on short read" convention;
199 * so allow up to (N*maxpacket), since that memory is normally
200 * already allocated. Some hardware doesn't deal well with short
201 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
202 * byte off the end (to force hardware errors on overflow).
204 * RNDIS uses internal framing, and explicitly allows senders to
205 * pad to end-of-packet. That's potentially nice for speed, but
206 * means receivers can't recover lost synch on their own (because
207 * new packets don't only start after a short RX).
209 size
+= sizeof(struct ethhdr
) + dev
->net
->mtu
+ RX_EXTRA
;
210 size
+= dev
->port_usb
->header_len
;
212 if (g
->quirk_ep_out_aligned_size
) {
213 size
+= out
->maxpacket
- 1;
214 size
-= size
% out
->maxpacket
;
217 if (dev
->port_usb
->is_fixed
)
218 size
= max_t(size_t, size
, dev
->port_usb
->fixed_out_len
);
219 spin_unlock_irqrestore(&dev
->lock
, flags
);
221 skb
= __netdev_alloc_skb(dev
->net
, size
+ NET_IP_ALIGN
, gfp_flags
);
223 DBG(dev
, "no rx skb\n");
227 /* Some platforms perform better when IP packets are aligned,
228 * but on at least one, checksumming fails otherwise. Note:
229 * RNDIS headers involve variable numbers of LE32 values.
231 if (likely(!dev
->no_skb_reserve
))
232 skb_reserve(skb
, NET_IP_ALIGN
);
234 req
->buf
= skb
->data
;
236 req
->complete
= rx_complete
;
239 retval
= usb_ep_queue(out
, req
, gfp_flags
);
240 if (retval
== -ENOMEM
)
242 defer_kevent(dev
, WORK_RX_MEMORY
);
244 DBG(dev
, "rx submit --> %d\n", retval
);
246 dev_kfree_skb_any(skb
);
247 spin_lock_irqsave(&dev
->req_lock
, flags
);
248 list_add(&req
->list
, &dev
->rx_reqs
);
249 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
254 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
256 struct sk_buff
*skb
= req
->context
, *skb2
;
257 struct eth_dev
*dev
= ep
->driver_data
;
258 int status
= req
->status
;
262 /* normal completion */
264 skb_put(skb
, req
->actual
);
269 spin_lock_irqsave(&dev
->lock
, flags
);
271 status
= dev
->unwrap(dev
->port_usb
,
275 dev_kfree_skb_any(skb
);
278 spin_unlock_irqrestore(&dev
->lock
, flags
);
280 skb_queue_tail(&dev
->rx_frames
, skb
);
284 skb2
= skb_dequeue(&dev
->rx_frames
);
287 || ETH_HLEN
> skb2
->len
288 || skb2
->len
> GETHER_MAX_ETH_FRAME_LEN
) {
289 dev
->net
->stats
.rx_errors
++;
290 dev
->net
->stats
.rx_length_errors
++;
291 DBG(dev
, "rx length %d\n", skb2
->len
);
292 dev_kfree_skb_any(skb2
);
295 skb2
->protocol
= eth_type_trans(skb2
, dev
->net
);
296 dev
->net
->stats
.rx_packets
++;
297 dev
->net
->stats
.rx_bytes
+= skb2
->len
;
299 /* no buffer copies needed, unless hardware can't
302 status
= netif_rx(skb2
);
304 skb2
= skb_dequeue(&dev
->rx_frames
);
308 /* software-driven interface shutdown */
309 case -ECONNRESET
: /* unlink */
310 case -ESHUTDOWN
: /* disconnect etc */
311 VDBG(dev
, "rx shutdown, code %d\n", status
);
314 /* for hardware automagic (such as pxa) */
315 case -ECONNABORTED
: /* endpoint reset */
316 DBG(dev
, "rx %s reset\n", ep
->name
);
317 defer_kevent(dev
, WORK_RX_MEMORY
);
319 dev_kfree_skb_any(skb
);
324 dev
->net
->stats
.rx_over_errors
++;
328 dev
->net
->stats
.rx_errors
++;
329 DBG(dev
, "rx status %d\n", status
);
334 dev_kfree_skb_any(skb
);
335 if (!netif_running(dev
->net
)) {
337 spin_lock(&dev
->req_lock
);
338 list_add(&req
->list
, &dev
->rx_reqs
);
339 spin_unlock(&dev
->req_lock
);
343 rx_submit(dev
, req
, GFP_ATOMIC
);
346 static int prealloc(struct list_head
*list
, struct usb_ep
*ep
, unsigned n
)
349 struct usb_request
*req
;
354 /* queue/recycle up to N requests */
356 list_for_each_entry(req
, list
, list
) {
361 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
363 return list_empty(list
) ? -ENOMEM
: 0;
364 list_add(&req
->list
, list
);
371 struct list_head
*next
;
373 next
= req
->list
.next
;
374 list_del(&req
->list
);
375 usb_ep_free_request(ep
, req
);
380 req
= container_of(next
, struct usb_request
, list
);
385 static int alloc_requests(struct eth_dev
*dev
, struct gether
*link
, unsigned n
)
389 spin_lock(&dev
->req_lock
);
390 status
= prealloc(&dev
->tx_reqs
, link
->in_ep
, n
);
393 status
= prealloc(&dev
->rx_reqs
, link
->out_ep
, n
);
398 DBG(dev
, "can't alloc requests\n");
400 spin_unlock(&dev
->req_lock
);
404 static void rx_fill(struct eth_dev
*dev
, gfp_t gfp_flags
)
406 struct usb_request
*req
;
409 /* fill unused rxq slots with some skb */
410 spin_lock_irqsave(&dev
->req_lock
, flags
);
411 while (!list_empty(&dev
->rx_reqs
)) {
412 req
= list_first_entry(&dev
->rx_reqs
, struct usb_request
, list
);
413 list_del_init(&req
->list
);
414 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
416 if (rx_submit(dev
, req
, gfp_flags
) < 0) {
417 defer_kevent(dev
, WORK_RX_MEMORY
);
421 spin_lock_irqsave(&dev
->req_lock
, flags
);
423 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
426 static void eth_work(struct work_struct
*work
)
428 struct eth_dev
*dev
= container_of(work
, struct eth_dev
, work
);
430 if (test_and_clear_bit(WORK_RX_MEMORY
, &dev
->todo
)) {
431 if (netif_running(dev
->net
))
432 rx_fill(dev
, GFP_KERNEL
);
436 DBG(dev
, "work done, flags = 0x%lx\n", dev
->todo
);
439 static void tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
441 struct sk_buff
*skb
= req
->context
;
442 struct eth_dev
*dev
= ep
->driver_data
;
444 switch (req
->status
) {
446 dev
->net
->stats
.tx_errors
++;
447 VDBG(dev
, "tx err %d\n", req
->status
);
449 case -ECONNRESET
: /* unlink */
450 case -ESHUTDOWN
: /* disconnect etc */
451 dev_kfree_skb_any(skb
);
454 dev
->net
->stats
.tx_bytes
+= skb
->len
;
455 dev_consume_skb_any(skb
);
457 dev
->net
->stats
.tx_packets
++;
459 spin_lock(&dev
->req_lock
);
460 list_add(&req
->list
, &dev
->tx_reqs
);
461 spin_unlock(&dev
->req_lock
);
463 atomic_dec(&dev
->tx_qlen
);
464 if (netif_carrier_ok(dev
->net
))
465 netif_wake_queue(dev
->net
);
468 static inline int is_promisc(u16 cdc_filter
)
470 return cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
473 static netdev_tx_t
eth_start_xmit(struct sk_buff
*skb
,
474 struct net_device
*net
)
476 struct eth_dev
*dev
= netdev_priv(net
);
479 struct usb_request
*req
= NULL
;
484 spin_lock_irqsave(&dev
->lock
, flags
);
486 in
= dev
->port_usb
->in_ep
;
487 cdc_filter
= dev
->port_usb
->cdc_filter
;
492 spin_unlock_irqrestore(&dev
->lock
, flags
);
495 dev_kfree_skb_any(skb
);
499 /* apply outgoing CDC or RNDIS filters */
500 if (skb
&& !is_promisc(cdc_filter
)) {
501 u8
*dest
= skb
->data
;
503 if (is_multicast_ether_addr(dest
)) {
506 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
507 * SET_ETHERNET_MULTICAST_FILTERS requests
509 if (is_broadcast_ether_addr(dest
))
510 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
512 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
513 if (!(cdc_filter
& type
)) {
514 dev_kfree_skb_any(skb
);
518 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
521 spin_lock_irqsave(&dev
->req_lock
, flags
);
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.
527 if (list_empty(&dev
->tx_reqs
)) {
528 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
529 return NETDEV_TX_BUSY
;
532 req
= list_first_entry(&dev
->tx_reqs
, struct usb_request
, list
);
533 list_del(&req
->list
);
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
);
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
547 spin_lock_irqsave(&dev
->lock
, flags
);
549 skb
= dev
->wrap(dev
->port_usb
, skb
);
550 spin_unlock_irqrestore(&dev
->lock
, flags
);
552 /* Multi frame CDC protocols may store the frame for
553 * later which is not a dropped frame.
556 dev
->port_usb
->supports_multi_frame
)
563 req
->buf
= skb
->data
;
565 req
->complete
= tx_complete
;
567 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
569 dev
->port_usb
->is_fixed
&&
570 length
== dev
->port_usb
->fixed_in_len
&&
571 (length
% in
->maxpacket
) == 0)
576 /* use zlp framing on tx for strict CDC-Ether conformance,
577 * though any robust network rx path ignores extra padding.
578 * and some hardware doesn't like to write zlps.
580 if (req
->zero
&& !dev
->zlp
&& (length
% in
->maxpacket
) == 0)
583 req
->length
= length
;
585 retval
= usb_ep_queue(in
, req
, GFP_ATOMIC
);
588 DBG(dev
, "tx queue err %d\n", retval
);
591 netif_trans_update(net
);
592 atomic_inc(&dev
->tx_qlen
);
596 dev_kfree_skb_any(skb
);
598 dev
->net
->stats
.tx_dropped
++;
600 spin_lock_irqsave(&dev
->req_lock
, flags
);
601 if (list_empty(&dev
->tx_reqs
))
602 netif_start_queue(net
);
603 list_add(&req
->list
, &dev
->tx_reqs
);
604 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
609 /*-------------------------------------------------------------------------*/
611 static void eth_start(struct eth_dev
*dev
, gfp_t gfp_flags
)
613 DBG(dev
, "%s\n", __func__
);
615 /* fill the rx queue */
616 rx_fill(dev
, gfp_flags
);
618 /* and open the tx floodgates */
619 atomic_set(&dev
->tx_qlen
, 0);
620 netif_wake_queue(dev
->net
);
623 static int eth_open(struct net_device
*net
)
625 struct eth_dev
*dev
= netdev_priv(net
);
628 DBG(dev
, "%s\n", __func__
);
629 if (netif_carrier_ok(dev
->net
))
630 eth_start(dev
, GFP_KERNEL
);
632 spin_lock_irq(&dev
->lock
);
633 link
= dev
->port_usb
;
634 if (link
&& link
->open
)
636 spin_unlock_irq(&dev
->lock
);
641 static int eth_stop(struct net_device
*net
)
643 struct eth_dev
*dev
= netdev_priv(net
);
646 VDBG(dev
, "%s\n", __func__
);
647 netif_stop_queue(net
);
649 DBG(dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
650 dev
->net
->stats
.rx_packets
, dev
->net
->stats
.tx_packets
,
651 dev
->net
->stats
.rx_errors
, dev
->net
->stats
.tx_errors
654 /* ensure there are no more active requests */
655 spin_lock_irqsave(&dev
->lock
, flags
);
657 struct gether
*link
= dev
->port_usb
;
658 const struct usb_endpoint_descriptor
*in
;
659 const struct usb_endpoint_descriptor
*out
;
664 /* NOTE: we have no abort-queue primitive we could use
665 * to cancel all pending I/O. Instead, we disable then
666 * reenable the endpoints ... this idiom may leave toggle
667 * wrong, but that's a self-correcting error.
669 * REVISIT: we *COULD* just let the transfers complete at
670 * their own pace; the network stack can handle old packets.
671 * For the moment we leave this here, since it works.
673 in
= link
->in_ep
->desc
;
674 out
= link
->out_ep
->desc
;
675 usb_ep_disable(link
->in_ep
);
676 usb_ep_disable(link
->out_ep
);
677 if (netif_carrier_ok(net
)) {
678 DBG(dev
, "host still using in/out endpoints\n");
679 link
->in_ep
->desc
= in
;
680 link
->out_ep
->desc
= out
;
681 usb_ep_enable(link
->in_ep
);
682 usb_ep_enable(link
->out_ep
);
685 spin_unlock_irqrestore(&dev
->lock
, flags
);
690 /*-------------------------------------------------------------------------*/
692 static int get_ether_addr(const char *str
, u8
*dev_addr
)
697 for (i
= 0; i
< 6; i
++) {
700 if ((*str
== '.') || (*str
== ':'))
702 num
= hex_to_bin(*str
++) << 4;
703 num
|= hex_to_bin(*str
++);
706 if (is_valid_ether_addr(dev_addr
))
709 eth_random_addr(dev_addr
);
713 static int get_ether_addr_str(u8 dev_addr
[ETH_ALEN
], char *str
, int len
)
718 snprintf(str
, len
, "%pM", dev_addr
);
722 static const struct net_device_ops eth_netdev_ops
= {
723 .ndo_open
= eth_open
,
724 .ndo_stop
= eth_stop
,
725 .ndo_start_xmit
= eth_start_xmit
,
726 .ndo_set_mac_address
= eth_mac_addr
,
727 .ndo_validate_addr
= eth_validate_addr
,
730 static struct device_type gadget_type
= {
735 * gether_setup_name - initialize one ethernet-over-usb link
736 * @g: gadget to associated with these links
737 * @ethaddr: NULL, or a buffer in which the ethernet address of the
738 * host side of the link is recorded
739 * @netname: name for network device (for example, "usb")
742 * This sets up the single network link that may be exported by a
743 * gadget driver using this framework. The link layer addresses are
744 * set up using module parameters.
746 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
748 struct eth_dev
*gether_setup_name(struct usb_gadget
*g
,
749 const char *dev_addr
, const char *host_addr
,
750 u8 ethaddr
[ETH_ALEN
], unsigned qmult
, const char *netname
)
753 struct net_device
*net
;
756 net
= alloc_etherdev(sizeof *dev
);
758 return ERR_PTR(-ENOMEM
);
760 dev
= netdev_priv(net
);
761 spin_lock_init(&dev
->lock
);
762 spin_lock_init(&dev
->req_lock
);
763 INIT_WORK(&dev
->work
, eth_work
);
764 INIT_LIST_HEAD(&dev
->tx_reqs
);
765 INIT_LIST_HEAD(&dev
->rx_reqs
);
767 skb_queue_head_init(&dev
->rx_frames
);
769 /* network device setup */
772 snprintf(net
->name
, sizeof(net
->name
), "%s%%d", netname
);
774 if (get_ether_addr(dev_addr
, net
->dev_addr
))
776 "using random %s ethernet address\n", "self");
777 if (get_ether_addr(host_addr
, dev
->host_mac
))
779 "using random %s ethernet address\n", "host");
782 memcpy(ethaddr
, dev
->host_mac
, ETH_ALEN
);
784 net
->netdev_ops
= ð_netdev_ops
;
786 net
->ethtool_ops
= &ops
;
788 /* MTU range: 14 - 15412 */
789 net
->min_mtu
= ETH_HLEN
;
790 net
->max_mtu
= GETHER_MAX_MTU_SIZE
;
793 SET_NETDEV_DEV(net
, &g
->dev
);
794 SET_NETDEV_DEVTYPE(net
, &gadget_type
);
796 status
= register_netdev(net
);
798 dev_dbg(&g
->dev
, "register_netdev failed, %d\n", status
);
800 dev
= ERR_PTR(status
);
802 INFO(dev
, "MAC %pM\n", net
->dev_addr
);
803 INFO(dev
, "HOST MAC %pM\n", dev
->host_mac
);
806 * two kinds of host-initiated state changes:
807 * - iff DATA transfer is active, carrier is "on"
808 * - tx queueing enabled if open *and* carrier is "on"
810 netif_carrier_off(net
);
815 EXPORT_SYMBOL_GPL(gether_setup_name
);
817 struct net_device
*gether_setup_name_default(const char *netname
)
819 struct net_device
*net
;
822 net
= alloc_etherdev(sizeof(*dev
));
824 return ERR_PTR(-ENOMEM
);
826 dev
= netdev_priv(net
);
827 spin_lock_init(&dev
->lock
);
828 spin_lock_init(&dev
->req_lock
);
829 INIT_WORK(&dev
->work
, eth_work
);
830 INIT_LIST_HEAD(&dev
->tx_reqs
);
831 INIT_LIST_HEAD(&dev
->rx_reqs
);
833 skb_queue_head_init(&dev
->rx_frames
);
835 /* network device setup */
837 dev
->qmult
= QMULT_DEFAULT
;
838 snprintf(net
->name
, sizeof(net
->name
), "%s%%d", netname
);
840 eth_random_addr(dev
->dev_mac
);
841 pr_warn("using random %s ethernet address\n", "self");
842 eth_random_addr(dev
->host_mac
);
843 pr_warn("using random %s ethernet address\n", "host");
845 net
->netdev_ops
= ð_netdev_ops
;
847 net
->ethtool_ops
= &ops
;
848 SET_NETDEV_DEVTYPE(net
, &gadget_type
);
850 /* MTU range: 14 - 15412 */
851 net
->min_mtu
= ETH_HLEN
;
852 net
->max_mtu
= GETHER_MAX_MTU_SIZE
;
856 EXPORT_SYMBOL_GPL(gether_setup_name_default
);
858 int gether_register_netdev(struct net_device
*net
)
861 struct usb_gadget
*g
;
865 if (!net
->dev
.parent
)
867 dev
= netdev_priv(net
);
869 status
= register_netdev(net
);
871 dev_dbg(&g
->dev
, "register_netdev failed, %d\n", status
);
874 INFO(dev
, "HOST MAC %pM\n", dev
->host_mac
);
876 /* two kinds of host-initiated state changes:
877 * - iff DATA transfer is active, carrier is "on"
878 * - tx queueing enabled if open *and* carrier is "on"
880 netif_carrier_off(net
);
882 sa
.sa_family
= net
->type
;
883 memcpy(sa
.sa_data
, dev
->dev_mac
, ETH_ALEN
);
885 status
= dev_set_mac_address(net
, &sa
, NULL
);
888 pr_warn("cannot set self ethernet address: %d\n", status
);
890 INFO(dev
, "MAC %pM\n", dev
->dev_mac
);
894 EXPORT_SYMBOL_GPL(gether_register_netdev
);
896 void gether_set_gadget(struct net_device
*net
, struct usb_gadget
*g
)
900 dev
= netdev_priv(net
);
902 SET_NETDEV_DEV(net
, &g
->dev
);
904 EXPORT_SYMBOL_GPL(gether_set_gadget
);
906 int gether_set_dev_addr(struct net_device
*net
, const char *dev_addr
)
909 u8 new_addr
[ETH_ALEN
];
911 dev
= netdev_priv(net
);
912 if (get_ether_addr(dev_addr
, new_addr
))
914 memcpy(dev
->dev_mac
, new_addr
, ETH_ALEN
);
917 EXPORT_SYMBOL_GPL(gether_set_dev_addr
);
919 int gether_get_dev_addr(struct net_device
*net
, char *dev_addr
, int len
)
924 dev
= netdev_priv(net
);
925 ret
= get_ether_addr_str(dev
->dev_mac
, dev_addr
, len
);
927 dev_addr
[ret
++] = '\n';
928 dev_addr
[ret
] = '\0';
933 EXPORT_SYMBOL_GPL(gether_get_dev_addr
);
935 int gether_set_host_addr(struct net_device
*net
, const char *host_addr
)
938 u8 new_addr
[ETH_ALEN
];
940 dev
= netdev_priv(net
);
941 if (get_ether_addr(host_addr
, new_addr
))
943 memcpy(dev
->host_mac
, new_addr
, ETH_ALEN
);
946 EXPORT_SYMBOL_GPL(gether_set_host_addr
);
948 int gether_get_host_addr(struct net_device
*net
, char *host_addr
, int len
)
953 dev
= netdev_priv(net
);
954 ret
= get_ether_addr_str(dev
->host_mac
, host_addr
, len
);
956 host_addr
[ret
++] = '\n';
957 host_addr
[ret
] = '\0';
962 EXPORT_SYMBOL_GPL(gether_get_host_addr
);
964 int gether_get_host_addr_cdc(struct net_device
*net
, char *host_addr
, int len
)
971 dev
= netdev_priv(net
);
972 snprintf(host_addr
, len
, "%pm", dev
->host_mac
);
974 return strlen(host_addr
);
976 EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc
);
978 void gether_get_host_addr_u8(struct net_device
*net
, u8 host_mac
[ETH_ALEN
])
982 dev
= netdev_priv(net
);
983 memcpy(host_mac
, dev
->host_mac
, ETH_ALEN
);
985 EXPORT_SYMBOL_GPL(gether_get_host_addr_u8
);
987 void gether_set_qmult(struct net_device
*net
, unsigned qmult
)
991 dev
= netdev_priv(net
);
994 EXPORT_SYMBOL_GPL(gether_set_qmult
);
996 unsigned gether_get_qmult(struct net_device
*net
)
1000 dev
= netdev_priv(net
);
1003 EXPORT_SYMBOL_GPL(gether_get_qmult
);
1005 int gether_get_ifname(struct net_device
*net
, char *name
, int len
)
1010 ret
= scnprintf(name
, len
, "%s\n", netdev_name(net
));
1014 EXPORT_SYMBOL_GPL(gether_get_ifname
);
1017 * gether_cleanup - remove Ethernet-over-USB device
1018 * Context: may sleep
1020 * This is called to free all resources allocated by @gether_setup().
1022 void gether_cleanup(struct eth_dev
*dev
)
1027 unregister_netdev(dev
->net
);
1028 flush_work(&dev
->work
);
1029 free_netdev(dev
->net
);
1031 EXPORT_SYMBOL_GPL(gether_cleanup
);
1034 * gether_connect - notify network layer that USB link is active
1035 * @link: the USB link, set up with endpoints, descriptors matching
1036 * current device speed, and any framing wrapper(s) set up.
1037 * Context: irqs blocked
1039 * This is called to activate endpoints and let the network layer know
1040 * the connection is active ("carrier detect"). It may cause the I/O
1041 * queues to open and start letting network packets flow, but will in
1042 * any case activate the endpoints so that they respond properly to the
1045 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1046 * indicate some error code (negative errno), ep->driver_data values
1047 * have been overwritten.
1049 struct net_device
*gether_connect(struct gether
*link
)
1051 struct eth_dev
*dev
= link
->ioport
;
1055 return ERR_PTR(-EINVAL
);
1057 link
->in_ep
->driver_data
= dev
;
1058 result
= usb_ep_enable(link
->in_ep
);
1060 DBG(dev
, "enable %s --> %d\n",
1061 link
->in_ep
->name
, result
);
1065 link
->out_ep
->driver_data
= dev
;
1066 result
= usb_ep_enable(link
->out_ep
);
1068 DBG(dev
, "enable %s --> %d\n",
1069 link
->out_ep
->name
, result
);
1074 result
= alloc_requests(dev
, link
, qlen(dev
->gadget
,
1078 dev
->zlp
= link
->is_zlp_ok
;
1079 dev
->no_skb_reserve
= gadget_avoids_skb_reserve(dev
->gadget
);
1080 DBG(dev
, "qlen %d\n", qlen(dev
->gadget
, dev
->qmult
));
1082 dev
->header_len
= link
->header_len
;
1083 dev
->unwrap
= link
->unwrap
;
1084 dev
->wrap
= link
->wrap
;
1086 spin_lock(&dev
->lock
);
1087 dev
->port_usb
= link
;
1088 if (netif_running(dev
->net
)) {
1095 spin_unlock(&dev
->lock
);
1097 netif_carrier_on(dev
->net
);
1098 if (netif_running(dev
->net
))
1099 eth_start(dev
, GFP_ATOMIC
);
1101 /* on error, disable any endpoints */
1103 (void) usb_ep_disable(link
->out_ep
);
1105 (void) usb_ep_disable(link
->in_ep
);
1108 /* caller is responsible for cleanup on error */
1110 return ERR_PTR(result
);
1113 EXPORT_SYMBOL_GPL(gether_connect
);
1116 * gether_disconnect - notify network layer that USB link is inactive
1117 * @link: the USB link, on which gether_connect() was called
1118 * Context: irqs blocked
1120 * This is called to deactivate endpoints and let the network layer know
1121 * the connection went inactive ("no carrier").
1123 * On return, the state is as if gether_connect() had never been called.
1124 * The endpoints are inactive, and accordingly without active USB I/O.
1125 * Pointers to endpoint descriptors and endpoint private data are nulled.
1127 void gether_disconnect(struct gether
*link
)
1129 struct eth_dev
*dev
= link
->ioport
;
1130 struct usb_request
*req
;
1136 DBG(dev
, "%s\n", __func__
);
1138 netif_stop_queue(dev
->net
);
1139 netif_carrier_off(dev
->net
);
1141 /* disable endpoints, forcing (synchronous) completion
1142 * of all pending i/o. then free the request objects
1143 * and forget about the endpoints.
1145 usb_ep_disable(link
->in_ep
);
1146 spin_lock(&dev
->req_lock
);
1147 while (!list_empty(&dev
->tx_reqs
)) {
1148 req
= list_first_entry(&dev
->tx_reqs
, struct usb_request
, list
);
1149 list_del(&req
->list
);
1151 spin_unlock(&dev
->req_lock
);
1152 usb_ep_free_request(link
->in_ep
, req
);
1153 spin_lock(&dev
->req_lock
);
1155 spin_unlock(&dev
->req_lock
);
1156 link
->in_ep
->desc
= NULL
;
1158 usb_ep_disable(link
->out_ep
);
1159 spin_lock(&dev
->req_lock
);
1160 while (!list_empty(&dev
->rx_reqs
)) {
1161 req
= list_first_entry(&dev
->rx_reqs
, struct usb_request
, list
);
1162 list_del(&req
->list
);
1164 spin_unlock(&dev
->req_lock
);
1165 usb_ep_free_request(link
->out_ep
, req
);
1166 spin_lock(&dev
->req_lock
);
1168 spin_unlock(&dev
->req_lock
);
1169 link
->out_ep
->desc
= NULL
;
1171 /* finish forgetting about this USB link episode */
1172 dev
->header_len
= 0;
1176 spin_lock(&dev
->lock
);
1177 dev
->port_usb
= NULL
;
1178 spin_unlock(&dev
->lock
);
1180 EXPORT_SYMBOL_GPL(gether_disconnect
);
1182 MODULE_LICENSE("GPL");
1183 MODULE_AUTHOR("David Brownell");