1 // SPDX-License-Identifier: GPL-2.0+
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
12 #include <environment.h>
13 #include <linux/errno.h>
14 #include <linux/netdevice.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/cdc.h>
17 #include <linux/usb/gadget.h>
22 #include <linux/ctype.h>
24 #include "gadget_chips.h"
29 #include <dm/uclass-internal.h>
30 #include <dm/device-internal.h>
32 #define USB_NET_NAME "usb_ether"
35 extern struct platform_data brd;
38 unsigned packet_received, packet_sent;
41 * Ethernet gadget driver -- with CDC and non-CDC options
42 * Builds on hardware support for a full duplex link.
44 * CDC Ethernet is the standard USB solution for sending Ethernet frames
45 * using USB. Real hardware tends to use the same framing protocol but look
46 * different for control features. This driver strongly prefers to use
47 * this USB-IF standard as its open-systems interoperability solution;
48 * most host side USB stacks (except from Microsoft) support it.
50 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
51 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
52 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
54 * There's some hardware that can't talk CDC ECM. We make that hardware
55 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
56 * link-level setup only requires activating the configuration. Only the
57 * endpoint descriptors, and product/vendor IDs, are relevant; no control
58 * operations are available. Linux supports it, but other host operating
59 * systems may not. (This is a subset of CDC Ethernet.)
61 * It turns out that if you add a few descriptors to that "CDC Subset",
62 * (Windows) host side drivers from MCCI can treat it as one submode of
63 * a proprietary scheme called "SAFE" ... without needing to know about
64 * specific product/vendor IDs. So we do that, making it easier to use
65 * those MS-Windows drivers. Those added descriptors make it resemble a
66 * CDC MDLM device, but they don't change device behavior at all. (See
67 * MCCI Engineering report 950198 "SAFE Networking Functions".)
69 * A third option is also in use. Rather than CDC Ethernet, or something
70 * simpler, Microsoft pushes their own approach: RNDIS. The published
71 * RNDIS specs are ambiguous and appear to be incomplete, and are also
72 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
75 #define DRIVER_DESC "Ethernet Gadget"
76 /* Based on linux 2.6.27 version */
77 #define DRIVER_VERSION "May Day 2005"
79 static const char driver_desc[] = DRIVER_DESC;
81 #define RX_EXTRA 20 /* guard against rx overflows */
83 #ifndef CONFIG_USB_ETH_RNDIS
84 #define rndis_uninit(x) do {} while (0)
85 #define rndis_deregister(c) do {} while (0)
86 #define rndis_exit() do {} while (0)
89 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
90 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
95 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
97 /*-------------------------------------------------------------------------*/
100 struct usb_gadget *gadget;
101 struct usb_request *req; /* for control responses */
102 struct usb_request *stat_req; /* for cdc & rndis status */
103 #if CONFIG_IS_ENABLED(DM_USB)
104 struct udevice *usb_udev;
108 struct usb_ep *in_ep, *out_ep, *status_ep;
109 const struct usb_endpoint_descriptor
112 struct usb_request *tx_req, *rx_req;
114 #ifndef CONFIG_DM_ETH
115 struct eth_device *net;
119 struct net_device_stats stats;
120 unsigned int tx_qlen;
125 unsigned suspended:1;
126 unsigned network_started:1;
130 #define WORK_RX_MEMORY 0
132 u8 host_mac[ETH_ALEN];
136 * This version autoconfigures as much as possible at run-time.
138 * It also ASSUMES a self-powered device, without remote wakeup,
139 * although remote wakeup support would make sense.
142 /*-------------------------------------------------------------------------*/
144 struct eth_dev ethdev;
145 #ifndef CONFIG_DM_ETH
146 struct eth_device netdev;
148 struct udevice *netdev;
150 struct usb_gadget_driver eth_driver;
153 struct ether_priv eth_priv;
154 struct ether_priv *l_priv = ð_priv;
156 /*-------------------------------------------------------------------------*/
158 /* "main" config is either CDC, or its simple subset */
159 static inline int is_cdc(struct eth_dev *dev)
161 #if !defined(CONFIG_USB_ETH_SUBSET)
162 return 1; /* only cdc possible */
163 #elif !defined(CONFIG_USB_ETH_CDC)
164 return 0; /* only subset possible */
166 return dev->cdc; /* depends on what hardware we found */
170 /* "secondary" RNDIS config may sometimes be activated */
171 static inline int rndis_active(struct eth_dev *dev)
173 #ifdef CONFIG_USB_ETH_RNDIS
180 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
181 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
183 #define DEFAULT_QLEN 2 /* double buffering by default */
185 /* peak bulk transfer bits-per-second */
186 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
187 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
189 #ifdef CONFIG_USB_GADGET_DUALSPEED
190 #define DEVSPEED USB_SPEED_HIGH
192 #ifdef CONFIG_USB_ETH_QMULT
193 #define qmult CONFIG_USB_ETH_QMULT
198 /* for dual-speed hardware, use deeper queues at highspeed */
199 #define qlen(gadget) \
200 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
202 static inline int BITRATE(struct usb_gadget *g)
204 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
207 #else /* full speed (low speed doesn't do bulk) */
211 #define DEVSPEED USB_SPEED_FULL
213 #define qlen(gadget) DEFAULT_QLEN
215 static inline int BITRATE(struct usb_gadget *g)
221 /*-------------------------------------------------------------------------*/
224 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
225 * Instead: allocate your own, using normal USB-IF procedures.
229 * Thanks to NetChip Technologies for donating this product ID.
230 * It's for devices with only CDC Ethernet configurations.
232 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
233 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
236 * For hardware that can't talk CDC, we use the same vendor ID that
237 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
238 * with pxa250. We're protocol-compatible, if the host-side drivers
239 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
240 * drivers that need to hard-wire endpoint numbers have a hook.
242 * The protocol is a minimal subset of CDC Ether, which works on any bulk
243 * hardware that's not deeply broken ... even on hardware that can't talk
244 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
245 * doesn't handle control-OUT).
247 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
248 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
251 * For hardware that can talk RNDIS and either of the above protocols,
252 * use this ID ... the windows INF files will know it. Unless it's
253 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
254 * the non-RNDIS configuration.
256 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
257 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
260 * Some systems will want different product identifers published in the
261 * device descriptor, either numbers or strings or both. These string
262 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
266 * Emulating them in eth_bind:
267 * static ushort idVendor;
268 * static ushort idProduct;
271 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
272 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
274 static char *iManufacturer = "U-Boot";
277 /* These probably need to be configurable. */
278 static ushort bcdDevice;
279 static char *iProduct;
280 static char *iSerialNumber;
282 static char dev_addr[18];
284 static char host_addr[18];
287 /*-------------------------------------------------------------------------*/
290 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
291 * ep0 implementation: descriptors, config management, setup().
292 * also optional class-specific notification interrupt transfer.
296 * DESCRIPTORS ... most are static, but strings and (full) configuration
297 * descriptors are built on demand. For now we do either full CDC, or
298 * our simple subset, with RNDIS as an optional second configuration.
300 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
301 * the class descriptors match a modem (they're ignored; it's really just
302 * Ethernet functionality), they don't need the NOP altsetting, and the
303 * status transfer endpoint isn't optional.
306 #define STRING_MANUFACTURER 1
307 #define STRING_PRODUCT 2
308 #define STRING_ETHADDR 3
309 #define STRING_DATA 4
310 #define STRING_CONTROL 5
311 #define STRING_RNDIS_CONTROL 6
313 #define STRING_SUBSET 8
314 #define STRING_RNDIS 9
315 #define STRING_SERIALNUMBER 10
317 /* holds our biggest descriptor (or RNDIS response) */
318 #define USB_BUFSIZ 256
321 * This device advertises one configuration, eth_config, unless RNDIS
322 * is enabled (rndis_config) on hardware supporting at least two configs.
324 * NOTE: Controllers like superh_udc should probably be able to use
325 * an RNDIS-only configuration.
327 * FIXME define some higher-powered configurations to make it easier
328 * to recharge batteries ...
331 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
332 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
334 static struct usb_device_descriptor
336 .bLength = sizeof device_desc,
337 .bDescriptorType = USB_DT_DEVICE,
339 .bcdUSB = __constant_cpu_to_le16(0x0200),
341 .bDeviceClass = USB_CLASS_COMM,
342 .bDeviceSubClass = 0,
343 .bDeviceProtocol = 0,
345 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
346 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
347 .iManufacturer = STRING_MANUFACTURER,
348 .iProduct = STRING_PRODUCT,
349 .bNumConfigurations = 1,
352 static struct usb_otg_descriptor
354 .bLength = sizeof otg_descriptor,
355 .bDescriptorType = USB_DT_OTG,
357 .bmAttributes = USB_OTG_SRP,
360 static struct usb_config_descriptor
362 .bLength = sizeof eth_config,
363 .bDescriptorType = USB_DT_CONFIG,
365 /* compute wTotalLength on the fly */
367 .bConfigurationValue = DEV_CONFIG_VALUE,
368 .iConfiguration = STRING_CDC,
369 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
373 #ifdef CONFIG_USB_ETH_RNDIS
374 static struct usb_config_descriptor
376 .bLength = sizeof rndis_config,
377 .bDescriptorType = USB_DT_CONFIG,
379 /* compute wTotalLength on the fly */
381 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
382 .iConfiguration = STRING_RNDIS,
383 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
389 * Compared to the simple CDC subset, the full CDC Ethernet model adds
390 * three class descriptors, two interface descriptors, optional status
391 * endpoint. Both have a "data" interface and two bulk endpoints.
392 * There are also differences in how control requests are handled.
394 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
395 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
396 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
397 * work around those bugs) are unlikely to ever come from MSFT, you may
398 * wish to avoid using RNDIS.
400 * MCCI offers an alternative to RNDIS if you need to connect to Windows
401 * but have hardware that can't support CDC Ethernet. We add descriptors
402 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
403 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
404 * get those drivers from MCCI, or bundled with various products.
407 #ifdef CONFIG_USB_ETH_CDC
408 static struct usb_interface_descriptor
410 .bLength = sizeof control_intf,
411 .bDescriptorType = USB_DT_INTERFACE,
413 .bInterfaceNumber = 0,
414 /* status endpoint is optional; this may be patched later */
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
418 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
419 .iInterface = STRING_CONTROL,
423 #ifdef CONFIG_USB_ETH_RNDIS
424 static const struct usb_interface_descriptor
425 rndis_control_intf = {
426 .bLength = sizeof rndis_control_intf,
427 .bDescriptorType = USB_DT_INTERFACE,
429 .bInterfaceNumber = 0,
431 .bInterfaceClass = USB_CLASS_COMM,
432 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
433 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
434 .iInterface = STRING_RNDIS_CONTROL,
438 static const struct usb_cdc_header_desc header_desc = {
439 .bLength = sizeof header_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
443 .bcdCDC = __constant_cpu_to_le16(0x0110),
446 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
448 static const struct usb_cdc_union_desc union_desc = {
449 .bLength = sizeof union_desc,
450 .bDescriptorType = USB_DT_CS_INTERFACE,
451 .bDescriptorSubType = USB_CDC_UNION_TYPE,
453 .bMasterInterface0 = 0, /* index of control interface */
454 .bSlaveInterface0 = 1, /* index of DATA interface */
457 #endif /* CDC || RNDIS */
459 #ifdef CONFIG_USB_ETH_RNDIS
461 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
462 .bLength = sizeof call_mgmt_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
466 .bmCapabilities = 0x00,
467 .bDataInterface = 0x01,
470 static const struct usb_cdc_acm_descriptor acm_descriptor = {
471 .bLength = sizeof acm_descriptor,
472 .bDescriptorType = USB_DT_CS_INTERFACE,
473 .bDescriptorSubType = USB_CDC_ACM_TYPE,
475 .bmCapabilities = 0x00,
480 #ifndef CONFIG_USB_ETH_CDC
483 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
484 * ways: data endpoints live in the control interface, there's no data
485 * interface, and it's not used to talk to a cell phone radio.
488 static const struct usb_cdc_mdlm_desc mdlm_desc = {
489 .bLength = sizeof mdlm_desc,
490 .bDescriptorType = USB_DT_CS_INTERFACE,
491 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
493 .bcdVersion = __constant_cpu_to_le16(0x0100),
495 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
496 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
501 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
502 * can't really use its struct. All we do here is say that we're using
503 * the submode of "SAFE" which directly matches the CDC Subset.
505 #ifdef CONFIG_USB_ETH_SUBSET
506 static const u8 mdlm_detail_desc[] = {
509 USB_CDC_MDLM_DETAIL_TYPE,
512 0, /* network control capabilities (none) */
513 0, /* network data capabilities ("raw" encapsulation) */
519 static const struct usb_cdc_ether_desc ether_desc = {
520 .bLength = sizeof(ether_desc),
521 .bDescriptorType = USB_DT_CS_INTERFACE,
522 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
524 /* this descriptor actually adds value, surprise! */
525 .iMACAddress = STRING_ETHADDR,
526 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
527 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
528 .wNumberMCFilters = __constant_cpu_to_le16(0),
529 .bNumberPowerFilters = 0,
532 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
535 * include the status endpoint if we can, even where it's optional.
536 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
537 * packet, to simplify cancellation; and a big transfer interval, to
538 * waste less bandwidth.
540 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
541 * if they ignore the connect/disconnect notifications that real aether
542 * can provide. more advanced cdc configurations might want to support
543 * encapsulated commands (vendor-specific, using control-OUT).
545 * RNDIS requires the status endpoint, since it uses that encapsulation
546 * mechanism for its funky RPC scheme.
549 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
550 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
552 static struct usb_endpoint_descriptor
554 .bLength = USB_DT_ENDPOINT_SIZE,
555 .bDescriptorType = USB_DT_ENDPOINT,
557 .bEndpointAddress = USB_DIR_IN,
558 .bmAttributes = USB_ENDPOINT_XFER_INT,
559 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
560 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
564 #ifdef CONFIG_USB_ETH_CDC
566 /* the default data interface has no endpoints ... */
568 static const struct usb_interface_descriptor
570 .bLength = sizeof data_nop_intf,
571 .bDescriptorType = USB_DT_INTERFACE,
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
576 .bInterfaceClass = USB_CLASS_CDC_DATA,
577 .bInterfaceSubClass = 0,
578 .bInterfaceProtocol = 0,
581 /* ... but the "real" data interface has two bulk endpoints */
583 static const struct usb_interface_descriptor
585 .bLength = sizeof data_intf,
586 .bDescriptorType = USB_DT_INTERFACE,
588 .bInterfaceNumber = 1,
589 .bAlternateSetting = 1,
591 .bInterfaceClass = USB_CLASS_CDC_DATA,
592 .bInterfaceSubClass = 0,
593 .bInterfaceProtocol = 0,
594 .iInterface = STRING_DATA,
599 #ifdef CONFIG_USB_ETH_RNDIS
601 /* RNDIS doesn't activate by changing to the "real" altsetting */
603 static const struct usb_interface_descriptor
605 .bLength = sizeof rndis_data_intf,
606 .bDescriptorType = USB_DT_INTERFACE,
608 .bInterfaceNumber = 1,
609 .bAlternateSetting = 0,
611 .bInterfaceClass = USB_CLASS_CDC_DATA,
612 .bInterfaceSubClass = 0,
613 .bInterfaceProtocol = 0,
614 .iInterface = STRING_DATA,
619 #ifdef CONFIG_USB_ETH_SUBSET
622 * "Simple" CDC-subset option is a simple vendor-neutral model that most
623 * full speed controllers can handle: one interface, two bulk endpoints.
625 * To assist host side drivers, we fancy it up a bit, and add descriptors
626 * so some host side drivers will understand it as a "SAFE" variant.
629 static const struct usb_interface_descriptor
631 .bLength = sizeof subset_data_intf,
632 .bDescriptorType = USB_DT_INTERFACE,
634 .bInterfaceNumber = 0,
635 .bAlternateSetting = 0,
637 .bInterfaceClass = USB_CLASS_COMM,
638 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
639 .bInterfaceProtocol = 0,
640 .iInterface = STRING_DATA,
645 static struct usb_endpoint_descriptor
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652 .wMaxPacketSize = __constant_cpu_to_le16(64),
655 static struct usb_endpoint_descriptor
657 .bLength = USB_DT_ENDPOINT_SIZE,
658 .bDescriptorType = USB_DT_ENDPOINT,
660 .bEndpointAddress = USB_DIR_OUT,
661 .bmAttributes = USB_ENDPOINT_XFER_BULK,
662 .wMaxPacketSize = __constant_cpu_to_le16(64),
665 static const struct usb_descriptor_header *fs_eth_function[11] = {
666 (struct usb_descriptor_header *) &otg_descriptor,
667 #ifdef CONFIG_USB_ETH_CDC
668 /* "cdc" mode descriptors */
669 (struct usb_descriptor_header *) &control_intf,
670 (struct usb_descriptor_header *) &header_desc,
671 (struct usb_descriptor_header *) &union_desc,
672 (struct usb_descriptor_header *) ðer_desc,
673 /* NOTE: status endpoint may need to be removed */
674 (struct usb_descriptor_header *) &fs_status_desc,
675 /* data interface, with altsetting */
676 (struct usb_descriptor_header *) &data_nop_intf,
677 (struct usb_descriptor_header *) &data_intf,
678 (struct usb_descriptor_header *) &fs_source_desc,
679 (struct usb_descriptor_header *) &fs_sink_desc,
681 #endif /* CONFIG_USB_ETH_CDC */
684 static inline void fs_subset_descriptors(void)
686 #ifdef CONFIG_USB_ETH_SUBSET
687 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
688 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
689 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
690 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
691 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
692 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
693 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
694 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
695 fs_eth_function[8] = NULL;
697 fs_eth_function[1] = NULL;
701 #ifdef CONFIG_USB_ETH_RNDIS
702 static const struct usb_descriptor_header *fs_rndis_function[] = {
703 (struct usb_descriptor_header *) &otg_descriptor,
704 /* control interface matches ACM, not Ethernet */
705 (struct usb_descriptor_header *) &rndis_control_intf,
706 (struct usb_descriptor_header *) &header_desc,
707 (struct usb_descriptor_header *) &call_mgmt_descriptor,
708 (struct usb_descriptor_header *) &acm_descriptor,
709 (struct usb_descriptor_header *) &union_desc,
710 (struct usb_descriptor_header *) &fs_status_desc,
711 /* data interface has no altsetting */
712 (struct usb_descriptor_header *) &rndis_data_intf,
713 (struct usb_descriptor_header *) &fs_source_desc,
714 (struct usb_descriptor_header *) &fs_sink_desc,
720 * usb 2.0 devices need to expose both high speed and full speed
721 * descriptors, unless they only run at full speed.
724 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
725 static struct usb_endpoint_descriptor
727 .bLength = USB_DT_ENDPOINT_SIZE,
728 .bDescriptorType = USB_DT_ENDPOINT,
730 .bmAttributes = USB_ENDPOINT_XFER_INT,
731 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
732 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
734 #endif /* CONFIG_USB_ETH_CDC */
736 static struct usb_endpoint_descriptor
738 .bLength = USB_DT_ENDPOINT_SIZE,
739 .bDescriptorType = USB_DT_ENDPOINT,
741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
742 .wMaxPacketSize = __constant_cpu_to_le16(512),
745 static struct usb_endpoint_descriptor
747 .bLength = USB_DT_ENDPOINT_SIZE,
748 .bDescriptorType = USB_DT_ENDPOINT,
750 .bmAttributes = USB_ENDPOINT_XFER_BULK,
751 .wMaxPacketSize = __constant_cpu_to_le16(512),
754 static struct usb_qualifier_descriptor
756 .bLength = sizeof dev_qualifier,
757 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
759 .bcdUSB = __constant_cpu_to_le16(0x0200),
760 .bDeviceClass = USB_CLASS_COMM,
762 .bNumConfigurations = 1,
765 static const struct usb_descriptor_header *hs_eth_function[11] = {
766 (struct usb_descriptor_header *) &otg_descriptor,
767 #ifdef CONFIG_USB_ETH_CDC
768 /* "cdc" mode descriptors */
769 (struct usb_descriptor_header *) &control_intf,
770 (struct usb_descriptor_header *) &header_desc,
771 (struct usb_descriptor_header *) &union_desc,
772 (struct usb_descriptor_header *) ðer_desc,
773 /* NOTE: status endpoint may need to be removed */
774 (struct usb_descriptor_header *) &hs_status_desc,
775 /* data interface, with altsetting */
776 (struct usb_descriptor_header *) &data_nop_intf,
777 (struct usb_descriptor_header *) &data_intf,
778 (struct usb_descriptor_header *) &hs_source_desc,
779 (struct usb_descriptor_header *) &hs_sink_desc,
781 #endif /* CONFIG_USB_ETH_CDC */
784 static inline void hs_subset_descriptors(void)
786 #ifdef CONFIG_USB_ETH_SUBSET
787 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
788 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
789 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
790 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
791 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
792 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
793 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
794 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
795 hs_eth_function[8] = NULL;
797 hs_eth_function[1] = NULL;
801 #ifdef CONFIG_USB_ETH_RNDIS
802 static const struct usb_descriptor_header *hs_rndis_function[] = {
803 (struct usb_descriptor_header *) &otg_descriptor,
804 /* control interface matches ACM, not Ethernet */
805 (struct usb_descriptor_header *) &rndis_control_intf,
806 (struct usb_descriptor_header *) &header_desc,
807 (struct usb_descriptor_header *) &call_mgmt_descriptor,
808 (struct usb_descriptor_header *) &acm_descriptor,
809 (struct usb_descriptor_header *) &union_desc,
810 (struct usb_descriptor_header *) &hs_status_desc,
811 /* data interface has no altsetting */
812 (struct usb_descriptor_header *) &rndis_data_intf,
813 (struct usb_descriptor_header *) &hs_source_desc,
814 (struct usb_descriptor_header *) &hs_sink_desc,
820 /* maxpacket and other transfer characteristics vary by speed. */
821 static inline struct usb_endpoint_descriptor *
822 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
823 struct usb_endpoint_descriptor *fs)
825 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
830 /*-------------------------------------------------------------------------*/
832 /* descriptors that are built on-demand */
834 static char manufacturer[50];
835 static char product_desc[40] = DRIVER_DESC;
836 static char serial_number[20];
838 /* address that the host will use ... usually assigned at random */
839 static char ethaddr[2 * ETH_ALEN + 1];
841 /* static strings, in UTF-8 */
842 static struct usb_string strings[] = {
843 { STRING_MANUFACTURER, manufacturer, },
844 { STRING_PRODUCT, product_desc, },
845 { STRING_SERIALNUMBER, serial_number, },
846 { STRING_DATA, "Ethernet Data", },
847 { STRING_ETHADDR, ethaddr, },
848 #ifdef CONFIG_USB_ETH_CDC
849 { STRING_CDC, "CDC Ethernet", },
850 { STRING_CONTROL, "CDC Communications Control", },
852 #ifdef CONFIG_USB_ETH_SUBSET
853 { STRING_SUBSET, "CDC Ethernet Subset", },
855 #ifdef CONFIG_USB_ETH_RNDIS
856 { STRING_RNDIS, "RNDIS", },
857 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
859 { } /* end of list */
862 static struct usb_gadget_strings stringtab = {
863 .language = 0x0409, /* en-us */
867 /*============================================================================*/
868 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
870 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
871 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
874 /*============================================================================*/
877 * one config, two interfaces: control, data.
878 * complications: class descriptors, and an altsetting.
881 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
884 const struct usb_config_descriptor *config;
885 const struct usb_descriptor_header **function;
888 if (gadget_is_dualspeed(g)) {
889 hs = (g->speed == USB_SPEED_HIGH);
890 if (type == USB_DT_OTHER_SPEED_CONFIG)
893 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
895 if (index >= device_desc.bNumConfigurations)
898 #ifdef CONFIG_USB_ETH_RNDIS
900 * list the RNDIS config first, to make Microsoft's drivers
901 * happy. DOCSIS 1.0 needs this too.
903 if (device_desc.bNumConfigurations == 2 && index == 0) {
904 config = &rndis_config;
905 function = which_fn(rndis);
909 config = ð_config;
910 function = which_fn(eth);
913 /* for now, don't advertise srp-only devices */
917 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
920 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
924 /*-------------------------------------------------------------------------*/
926 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
927 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
930 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
933 struct usb_gadget *gadget = dev->gadget;
935 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
936 /* status endpoint used for RNDIS and (optionally) CDC */
937 if (!subset_active(dev) && dev->status_ep) {
938 dev->status = ep_desc(gadget, &hs_status_desc,
940 dev->status_ep->driver_data = dev;
942 result = usb_ep_enable(dev->status_ep, dev->status);
944 debug("enable %s --> %d\n",
945 dev->status_ep->name, result);
951 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
952 dev->in_ep->driver_data = dev;
954 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
955 dev->out_ep->driver_data = dev;
958 * With CDC, the host isn't allowed to use these two data
959 * endpoints in the default altsetting for the interface.
960 * so we don't activate them yet. Reset from SET_INTERFACE.
962 * Strictly speaking RNDIS should work the same: activation is
963 * a side effect of setting a packet filter. Deactivation is
964 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
966 if (!cdc_active(dev)) {
967 result = usb_ep_enable(dev->in_ep, dev->in);
969 debug("enable %s --> %d\n",
970 dev->in_ep->name, result);
974 result = usb_ep_enable(dev->out_ep, dev->out);
976 debug("enable %s --> %d\n",
977 dev->out_ep->name, result);
984 result = alloc_requests(dev, qlen(gadget), gfp_flags);
986 /* on error, disable any endpoints */
988 if (!subset_active(dev) && dev->status_ep)
989 (void) usb_ep_disable(dev->status_ep);
991 (void) usb_ep_disable(dev->in_ep);
992 (void) usb_ep_disable(dev->out_ep);
995 } else if (!cdc_active(dev)) {
997 * activate non-CDC configs right away
998 * this isn't strictly according to the RNDIS spec
1000 eth_start(dev, GFP_ATOMIC);
1003 /* caller is responsible for cleanup on error */
1007 static void eth_reset_config(struct eth_dev *dev)
1009 if (dev->config == 0)
1012 debug("%s\n", __func__);
1014 rndis_uninit(dev->rndis_config);
1017 * disable endpoints, forcing (synchronous) completion of
1018 * pending i/o. then free the requests.
1022 usb_ep_disable(dev->in_ep);
1024 usb_ep_free_request(dev->in_ep, dev->tx_req);
1029 usb_ep_disable(dev->out_ep);
1031 usb_ep_free_request(dev->out_ep, dev->rx_req);
1036 usb_ep_disable(dev->status_ep);
1039 dev->cdc_filter = 0;
1044 * change our operational config. must agree with the code
1045 * that returns config descriptors, and altsetting code.
1047 static int eth_set_config(struct eth_dev *dev, unsigned number,
1051 struct usb_gadget *gadget = dev->gadget;
1053 if (gadget_is_sa1100(gadget)
1055 && dev->tx_qlen != 0) {
1056 /* tx fifo is full, but we can't clear it...*/
1057 pr_err("can't change configurations");
1060 eth_reset_config(dev);
1063 case DEV_CONFIG_VALUE:
1064 result = set_ether_config(dev, gfp_flags);
1066 #ifdef CONFIG_USB_ETH_RNDIS
1067 case DEV_RNDIS_CONFIG_VALUE:
1069 result = set_ether_config(dev, gfp_flags);
1081 eth_reset_config(dev);
1082 usb_gadget_vbus_draw(dev->gadget,
1083 gadget_is_otg(dev->gadget) ? 8 : 100);
1088 power = 2 * eth_config.bMaxPower;
1089 usb_gadget_vbus_draw(dev->gadget, power);
1091 switch (gadget->speed) {
1092 case USB_SPEED_FULL:
1093 speed = "full"; break;
1094 #ifdef CONFIG_USB_GADGET_DUALSPEED
1095 case USB_SPEED_HIGH:
1096 speed = "high"; break;
1102 dev->config = number;
1103 printf("%s speed config #%d: %d mA, %s, using %s\n",
1104 speed, number, power, driver_desc,
1109 : "CDC Ethernet Subset"));
1114 /*-------------------------------------------------------------------------*/
1116 #ifdef CONFIG_USB_ETH_CDC
1119 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1120 * only to notify the host about link status changes (which we support) or
1121 * report completion of some encapsulated command (as used in RNDIS). Since
1122 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1123 * command mechanism; and only one status request is ever queued.
1125 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1127 struct usb_cdc_notification *event = req->buf;
1128 int value = req->status;
1129 struct eth_dev *dev = ep->driver_data;
1131 /* issue the second notification if host reads the first */
1132 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1134 __le32 *data = req->buf + sizeof *event;
1136 event->bmRequestType = 0xA1;
1137 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1138 event->wValue = __constant_cpu_to_le16(0);
1139 event->wIndex = __constant_cpu_to_le16(1);
1140 event->wLength = __constant_cpu_to_le16(8);
1142 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1143 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1145 req->length = STATUS_BYTECOUNT;
1146 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1147 debug("send SPEED_CHANGE --> %d\n", value);
1150 } else if (value != -ECONNRESET) {
1151 debug("event %02x --> %d\n",
1152 event->bNotificationType, value);
1153 if (event->bNotificationType ==
1154 USB_CDC_NOTIFY_SPEED_CHANGE) {
1155 dev->network_started = 1;
1156 printf("USB network up!\n");
1159 req->context = NULL;
1162 static void issue_start_status(struct eth_dev *dev)
1164 struct usb_request *req = dev->stat_req;
1165 struct usb_cdc_notification *event;
1171 * FIXME ugly idiom, maybe we'd be better with just
1172 * a "cancel the whole queue" primitive since any
1173 * unlink-one primitive has way too many error modes.
1174 * here, we "know" toggle is already clear...
1176 * FIXME iff req->context != null just dequeue it
1178 usb_ep_disable(dev->status_ep);
1179 usb_ep_enable(dev->status_ep, dev->status);
1182 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1183 * a SPEED_CHANGE. could be useful in some configs.
1186 event->bmRequestType = 0xA1;
1187 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1188 event->wValue = __constant_cpu_to_le16(1); /* connected */
1189 event->wIndex = __constant_cpu_to_le16(1);
1192 req->length = sizeof *event;
1193 req->complete = eth_status_complete;
1196 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1198 debug("status buf queue --> %d\n", value);
1203 /*-------------------------------------------------------------------------*/
1205 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1207 if (req->status || req->actual != req->length)
1208 debug("setup complete --> %d, %d/%d\n",
1209 req->status, req->actual, req->length);
1212 #ifdef CONFIG_USB_ETH_RNDIS
1214 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1216 if (req->status || req->actual != req->length)
1217 debug("rndis response complete --> %d, %d/%d\n",
1218 req->status, req->actual, req->length);
1220 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1223 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1225 struct eth_dev *dev = ep->driver_data;
1228 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1229 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1231 pr_err("%s: rndis parse error %d", __func__, status);
1237 * The setup() callback implements all the ep0 functionality that's not
1238 * handled lower down. CDC has a number of less-common features:
1240 * - two interfaces: control, and ethernet data
1241 * - Ethernet data interface has two altsettings: default, and active
1242 * - class-specific descriptors for the control interface
1243 * - class-specific control requests
1246 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1248 struct eth_dev *dev = get_gadget_data(gadget);
1249 struct usb_request *req = dev->req;
1250 int value = -EOPNOTSUPP;
1251 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1252 u16 wValue = le16_to_cpu(ctrl->wValue);
1253 u16 wLength = le16_to_cpu(ctrl->wLength);
1256 * descriptors just go into the pre-allocated ep0 buffer,
1257 * while config change events may enable network traffic.
1260 debug("%s\n", __func__);
1262 req->complete = eth_setup_complete;
1263 switch (ctrl->bRequest) {
1265 case USB_REQ_GET_DESCRIPTOR:
1266 if (ctrl->bRequestType != USB_DIR_IN)
1268 switch (wValue >> 8) {
1271 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1272 value = min(wLength, (u16) sizeof device_desc);
1273 memcpy(req->buf, &device_desc, value);
1275 case USB_DT_DEVICE_QUALIFIER:
1276 if (!gadget_is_dualspeed(gadget))
1278 value = min(wLength, (u16) sizeof dev_qualifier);
1279 memcpy(req->buf, &dev_qualifier, value);
1282 case USB_DT_OTHER_SPEED_CONFIG:
1283 if (!gadget_is_dualspeed(gadget))
1287 value = config_buf(gadget, req->buf,
1290 gadget_is_otg(gadget));
1292 value = min(wLength, (u16) value);
1296 value = usb_gadget_get_string(&stringtab,
1297 wValue & 0xff, req->buf);
1300 value = min(wLength, (u16) value);
1306 case USB_REQ_SET_CONFIGURATION:
1307 if (ctrl->bRequestType != 0)
1309 if (gadget->a_hnp_support)
1310 debug("HNP available\n");
1311 else if (gadget->a_alt_hnp_support)
1312 debug("HNP needs a different root port\n");
1313 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1315 case USB_REQ_GET_CONFIGURATION:
1316 if (ctrl->bRequestType != USB_DIR_IN)
1318 *(u8 *)req->buf = dev->config;
1319 value = min(wLength, (u16) 1);
1322 case USB_REQ_SET_INTERFACE:
1323 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1327 if (!cdc_active(dev) && wIndex != 0)
1331 * PXA hardware partially handles SET_INTERFACE;
1332 * we need to kluge around that interference.
1334 if (gadget_is_pxa(gadget)) {
1335 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1338 * PXA25x driver use non-CDC ethernet gadget.
1339 * But only _CDC and _RNDIS code can signalize
1340 * that network is working. So we signalize it
1343 dev->network_started = 1;
1344 debug("USB network up!\n");
1348 #ifdef CONFIG_USB_ETH_CDC
1350 case 0: /* control/master intf */
1354 usb_ep_disable(dev->status_ep);
1355 usb_ep_enable(dev->status_ep, dev->status);
1360 case 1: /* data intf */
1363 usb_ep_disable(dev->in_ep);
1364 usb_ep_disable(dev->out_ep);
1367 * CDC requires the data transfers not be done from
1368 * the default interface setting ... also, setting
1369 * the non-default interface resets filters etc.
1372 if (!cdc_active(dev))
1374 usb_ep_enable(dev->in_ep, dev->in);
1375 usb_ep_enable(dev->out_ep, dev->out);
1376 dev->cdc_filter = DEFAULT_FILTER;
1378 issue_start_status(dev);
1379 eth_start(dev, GFP_ATOMIC);
1386 * FIXME this is wrong, as is the assumption that
1387 * all non-PXA hardware talks real CDC ...
1389 debug("set_interface ignored!\n");
1390 #endif /* CONFIG_USB_ETH_CDC */
1394 case USB_REQ_GET_INTERFACE:
1395 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1399 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1402 /* for CDC, iff carrier is on, data interface is active. */
1403 if (rndis_active(dev) || wIndex != 1)
1404 *(u8 *)req->buf = 0;
1406 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1407 /* carrier always ok ...*/
1408 *(u8 *)req->buf = 1 ;
1410 value = min(wLength, (u16) 1);
1413 #ifdef CONFIG_USB_ETH_CDC
1414 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1416 * see 6.2.30: no data, wIndex = interface,
1417 * wValue = packet filter bitmap
1419 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1424 debug("packet filter %02x\n", wValue);
1425 dev->cdc_filter = wValue;
1431 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1432 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1433 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1434 * case USB_CDC_GET_ETHERNET_STATISTIC:
1437 #endif /* CONFIG_USB_ETH_CDC */
1439 #ifdef CONFIG_USB_ETH_RNDIS
1441 * RNDIS uses the CDC command encapsulation mechanism to implement
1442 * an RPC scheme, with much getting/setting of attributes by OID.
1444 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1445 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1446 || !rndis_active(dev)
1447 || wLength > USB_BUFSIZ
1449 || rndis_control_intf.bInterfaceNumber
1452 /* read the request, then process it */
1454 req->complete = rndis_command_complete;
1455 /* later, rndis_control_ack () sends a notification */
1458 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1459 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1460 == ctrl->bRequestType
1461 && rndis_active(dev)
1462 /* && wLength >= 0x0400 */
1464 && rndis_control_intf.bInterfaceNumber
1469 /* return the result */
1470 buf = rndis_get_next_response(dev->rndis_config, &n);
1472 memcpy(req->buf, buf, n);
1473 req->complete = rndis_response_complete;
1474 rndis_free_response(dev->rndis_config, buf);
1477 /* else stalls ... spec says to avoid that */
1483 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1484 ctrl->bRequestType, ctrl->bRequest,
1485 wValue, wIndex, wLength);
1488 /* respond with data transfer before status phase? */
1490 debug("respond with data transfer before status phase\n");
1491 req->length = value;
1492 req->zero = value < wLength
1493 && (value % gadget->ep0->maxpacket) == 0;
1494 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1496 debug("ep_queue --> %d\n", value);
1498 eth_setup_complete(gadget->ep0, req);
1502 /* host either stalls (value < 0) or reports success */
1506 /*-------------------------------------------------------------------------*/
1508 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1510 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1513 int retval = -ENOMEM;
1517 * Padding up to RX_EXTRA handles minor disagreements with host.
1518 * Normally we use the USB "terminate on short read" convention;
1519 * so allow up to (N*maxpacket), since that memory is normally
1520 * already allocated. Some hardware doesn't deal well with short
1521 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1522 * byte off the end (to force hardware errors on overflow).
1524 * RNDIS uses internal framing, and explicitly allows senders to
1525 * pad to end-of-packet. That's potentially nice for speed,
1526 * but means receivers can't recover synch on their own.
1529 debug("%s\n", __func__);
1533 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1534 size += dev->out_ep->maxpacket - 1;
1535 if (rndis_active(dev))
1536 size += sizeof(struct rndis_packet_msg_type);
1537 size -= size % dev->out_ep->maxpacket;
1540 * Some platforms perform better when IP packets are aligned,
1541 * but on at least one, checksumming fails otherwise. Note:
1542 * RNDIS headers involve variable numbers of LE32 values.
1545 req->buf = (u8 *)net_rx_packets[0];
1547 req->complete = rx_complete;
1549 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1552 pr_err("rx submit --> %d", retval);
1557 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1559 struct eth_dev *dev = ep->driver_data;
1561 debug("%s: status %d\n", __func__, req->status);
1562 switch (req->status) {
1563 /* normal completion */
1565 if (rndis_active(dev)) {
1566 /* we know MaxPacketsPerTransfer == 1 here */
1567 int length = rndis_rm_hdr(req->buf, req->actual);
1570 req->length -= length;
1571 req->actual -= length;
1573 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
1575 dev->stats.rx_errors++;
1576 dev->stats.rx_length_errors++;
1577 debug("rx length %d\n", req->length);
1581 dev->stats.rx_packets++;
1582 dev->stats.rx_bytes += req->length;
1585 /* software-driven interface shutdown */
1586 case -ECONNRESET: /* unlink */
1587 case -ESHUTDOWN: /* disconnect etc */
1588 /* for hardware automagic (such as pxa) */
1589 case -ECONNABORTED: /* endpoint reset */
1594 dev->stats.rx_over_errors++;
1597 dev->stats.rx_errors++;
1601 packet_received = 1;
1604 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1607 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1612 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1620 usb_ep_free_request(dev->in_ep, dev->tx_req);
1622 pr_err("can't alloc requests");
1626 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1628 struct eth_dev *dev = ep->driver_data;
1630 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1631 switch (req->status) {
1633 dev->stats.tx_errors++;
1634 debug("tx err %d\n", req->status);
1636 case -ECONNRESET: /* unlink */
1637 case -ESHUTDOWN: /* disconnect etc */
1640 dev->stats.tx_bytes += req->length;
1642 dev->stats.tx_packets++;
1647 static inline int eth_is_promisc(struct eth_dev *dev)
1649 /* no filters for the CDC subset; always promisc */
1650 if (subset_active(dev))
1652 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1656 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1658 struct eth_dev *dev = netdev_priv(net);
1659 int length = skb->len;
1661 struct usb_request *req = NULL;
1662 unsigned long flags;
1664 /* apply outgoing CDC or RNDIS filters */
1665 if (!eth_is_promisc (dev)) {
1666 u8 *dest = skb->data;
1668 if (is_multicast_ethaddr(dest)) {
1671 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1672 * SET_ETHERNET_MULTICAST_FILTERS requests
1674 if (is_broadcast_ethaddr(dest))
1675 type = USB_CDC_PACKET_TYPE_BROADCAST;
1677 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1678 if (!(dev->cdc_filter & type)) {
1679 dev_kfree_skb_any (skb);
1683 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1686 spin_lock_irqsave(&dev->req_lock, flags);
1688 * this freelist can be empty if an interrupt triggered disconnect()
1689 * and reconfigured the gadget (shutting down this queue) after the
1690 * network stack decided to xmit but before we got the spinlock.
1692 if (list_empty(&dev->tx_reqs)) {
1693 spin_unlock_irqrestore(&dev->req_lock, flags);
1697 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1698 list_del (&req->list);
1700 /* temporarily stop TX queue when the freelist empties */
1701 if (list_empty (&dev->tx_reqs))
1702 netif_stop_queue (net);
1703 spin_unlock_irqrestore(&dev->req_lock, flags);
1705 /* no buffer copies needed, unless the network stack did it
1706 * or the hardware can't use skb buffers.
1707 * or there's not enough space for any RNDIS headers we need
1709 if (rndis_active(dev)) {
1710 struct sk_buff *skb_rndis;
1712 skb_rndis = skb_realloc_headroom (skb,
1713 sizeof (struct rndis_packet_msg_type));
1717 dev_kfree_skb_any (skb);
1719 rndis_add_hdr (skb);
1722 req->buf = skb->data;
1724 req->complete = tx_complete;
1726 /* use zlp framing on tx for strict CDC-Ether conformance,
1727 * though any robust network rx path ignores extra padding.
1728 * and some hardware doesn't like to write zlps.
1731 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1734 req->length = length;
1736 /* throttle highspeed IRQ rate back slightly */
1737 if (gadget_is_dualspeed(dev->gadget))
1738 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1739 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1742 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1745 DEBUG (dev, "tx queue err %d\n", retval);
1748 net->trans_start = jiffies;
1749 atomic_inc (&dev->tx_qlen);
1754 dev->stats.tx_dropped++;
1755 dev_kfree_skb_any (skb);
1756 spin_lock_irqsave(&dev->req_lock, flags);
1757 if (list_empty (&dev->tx_reqs))
1758 netif_start_queue (net);
1759 list_add (&req->list, &dev->tx_reqs);
1760 spin_unlock_irqrestore(&dev->req_lock, flags);
1765 /*-------------------------------------------------------------------------*/
1768 static void eth_unbind(struct usb_gadget *gadget)
1770 struct eth_dev *dev = get_gadget_data(gadget);
1772 debug("%s...\n", __func__);
1773 rndis_deregister(dev->rndis_config);
1776 /* we've already been disconnected ... no i/o is active */
1778 usb_ep_free_request(gadget->ep0, dev->req);
1781 if (dev->stat_req) {
1782 usb_ep_free_request(dev->status_ep, dev->stat_req);
1783 dev->stat_req = NULL;
1787 usb_ep_free_request(dev->in_ep, dev->tx_req);
1792 usb_ep_free_request(dev->out_ep, dev->rx_req);
1796 /* unregister_netdev (dev->net);*/
1797 /* free_netdev(dev->net);*/
1800 set_gadget_data(gadget, NULL);
1803 static void eth_disconnect(struct usb_gadget *gadget)
1805 eth_reset_config(get_gadget_data(gadget));
1806 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1809 static void eth_suspend(struct usb_gadget *gadget)
1814 static void eth_resume(struct usb_gadget *gadget)
1819 /*-------------------------------------------------------------------------*/
1821 #ifdef CONFIG_USB_ETH_RNDIS
1824 * The interrupt endpoint is used in RNDIS to notify the host when messages
1825 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1826 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1827 * REMOTE_NDIS_KEEPALIVE_MSG.
1829 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1830 * normally just one notification will be queued.
1833 static void rndis_control_ack_complete(struct usb_ep *ep,
1834 struct usb_request *req)
1836 struct eth_dev *dev = ep->driver_data;
1838 debug("%s...\n", __func__);
1839 if (req->status || req->actual != req->length)
1840 debug("rndis control ack complete --> %d, %d/%d\n",
1841 req->status, req->actual, req->length);
1843 if (!dev->network_started) {
1844 if (rndis_get_state(dev->rndis_config)
1845 == RNDIS_DATA_INITIALIZED) {
1846 dev->network_started = 1;
1847 printf("USB RNDIS network up!\n");
1851 req->context = NULL;
1853 if (req != dev->stat_req)
1854 usb_ep_free_request(ep, req);
1857 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1859 #ifndef CONFIG_DM_ETH
1860 static int rndis_control_ack(struct eth_device *net)
1862 static int rndis_control_ack(struct udevice *net)
1865 struct ether_priv *priv = (struct ether_priv *)net->priv;
1866 struct eth_dev *dev = &priv->ethdev;
1868 struct usb_request *resp = dev->stat_req;
1870 /* in case RNDIS calls this after disconnect */
1872 debug("status ENODEV\n");
1876 /* in case queue length > 1 */
1877 if (resp->context) {
1878 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1881 resp->buf = rndis_resp_buf;
1885 * Send RNDIS RESPONSE_AVAILABLE notification;
1886 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1889 resp->complete = rndis_control_ack_complete;
1890 resp->context = dev;
1892 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1893 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1895 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1898 rndis_control_ack_complete(dev->status_ep, resp);
1906 #define rndis_control_ack NULL
1910 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1912 if (rndis_active(dev)) {
1913 rndis_set_param_medium(dev->rndis_config,
1915 BITRATE(dev->gadget)/100);
1916 rndis_signal_connect(dev->rndis_config);
1920 static int eth_stop(struct eth_dev *dev)
1922 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1924 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1927 if (rndis_active(dev)) {
1928 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1929 rndis_signal_disconnect(dev->rndis_config);
1931 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1932 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1934 while (get_timer(ts) < timeout)
1935 usb_gadget_handle_interrupts(0);
1938 rndis_uninit(dev->rndis_config);
1945 /*-------------------------------------------------------------------------*/
1947 static int is_eth_addr_valid(char *str)
1949 if (strlen(str) == 17) {
1954 /* see if it looks like an ethernet address */
1958 for (i = 0; i < 6; i++) {
1959 char term = (i == 5 ? '\0' : ':');
1961 ea[i] = simple_strtol(p, &q, 16);
1963 if ((q - p) != 2 || *q++ != term)
1969 /* Now check the contents. */
1970 return is_valid_ethaddr(ea);
1975 static u8 nibble(unsigned char c)
1977 if (likely(isdigit(c)))
1980 if (likely(isxdigit(c)))
1981 return 10 + c - 'A';
1985 static int get_ether_addr(const char *str, u8 *dev_addr)
1990 for (i = 0; i < 6; i++) {
1993 if ((*str == '.') || (*str == ':'))
1995 num = nibble(*str++) << 4;
1996 num |= (nibble(*str++));
1999 if (is_valid_ethaddr(dev_addr))
2005 static int eth_bind(struct usb_gadget *gadget)
2007 struct eth_dev *dev = &l_priv->ethdev;
2008 u8 cdc = 1, zlp = 1, rndis = 1;
2009 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2010 int status = -ENOMEM;
2013 #ifdef CONFIG_DM_ETH
2014 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2017 /* these flags are only ever cleared; compiler take note */
2018 #ifndef CONFIG_USB_ETH_CDC
2021 #ifndef CONFIG_USB_ETH_RNDIS
2025 * Because most host side USB stacks handle CDC Ethernet, that
2026 * standard protocol is _strongly_ preferred for interop purposes.
2027 * (By everyone except Microsoft.)
2029 if (gadget_is_pxa(gadget)) {
2030 /* pxa doesn't support altsettings */
2032 } else if (gadget_is_musbhdrc(gadget)) {
2033 /* reduce tx dma overhead by avoiding special cases */
2035 } else if (gadget_is_sh(gadget)) {
2036 /* sh doesn't support multiple interfaces or configs */
2039 } else if (gadget_is_sa1100(gadget)) {
2040 /* hardware can't write zlps */
2043 * sa1100 CAN do CDC, without status endpoint ... we use
2044 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2049 gcnum = usb_gadget_controller_number(gadget);
2051 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2054 * can't assume CDC works. don't want to default to
2055 * anything less functional on CDC-capable hardware,
2056 * so we fail in this case.
2058 pr_err("controller '%s' not recognized",
2064 * If there's an RNDIS configuration, that's what Windows wants to
2065 * be using ... so use these product IDs here and in the "linux.inf"
2066 * needed to install MSFT drivers. Current Linux kernels will use
2067 * the second configuration if it's CDC Ethernet, and need some help
2068 * to choose the right configuration otherwise.
2071 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2072 device_desc.idVendor =
2073 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2074 device_desc.idProduct =
2075 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2082 sprintf(product_desc, "RNDIS/%s", driver_desc);
2085 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2086 * drivers aren't widely available. (That may be improved by
2087 * supporting one submode of the "SAFE" variant of MDLM.)
2090 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2091 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2092 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2095 device_desc.idVendor =
2096 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2097 device_desc.idProduct =
2098 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2102 /* support optional vendor/distro customization */
2104 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2106 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2108 strlcpy(product_desc, iProduct, sizeof product_desc);
2109 if (iSerialNumber) {
2110 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2111 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2114 /* all we really need is bulk IN/OUT */
2115 usb_ep_autoconfig_reset(gadget);
2116 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2119 pr_err("can't autoconfigure on %s\n",
2123 in_ep->driver_data = in_ep; /* claim */
2125 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2128 out_ep->driver_data = out_ep; /* claim */
2130 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2132 * CDC Ethernet control interface doesn't require a status endpoint.
2133 * Since some hosts expect one, try to allocate one anyway.
2136 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2138 status_ep->driver_data = status_ep; /* claim */
2140 pr_err("can't run RNDIS on %s", gadget->name);
2142 #ifdef CONFIG_USB_ETH_CDC
2144 control_intf.bNumEndpoints = 0;
2145 /* FIXME remove endpoint from descriptor list */
2151 /* one config: cdc, else minimal subset */
2153 eth_config.bNumInterfaces = 1;
2154 eth_config.iConfiguration = STRING_SUBSET;
2157 * use functions to set these up, in case we're built to work
2158 * with multiple controllers and must override CDC Ethernet.
2160 fs_subset_descriptors();
2161 hs_subset_descriptors();
2164 usb_gadget_set_selfpowered(gadget);
2166 /* For now RNDIS is always a second config */
2168 device_desc.bNumConfigurations = 2;
2170 if (gadget_is_dualspeed(gadget)) {
2172 dev_qualifier.bNumConfigurations = 2;
2174 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2176 /* assumes ep0 uses the same value for both speeds ... */
2177 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2179 /* and that all endpoints are dual-speed */
2180 hs_source_desc.bEndpointAddress =
2181 fs_source_desc.bEndpointAddress;
2182 hs_sink_desc.bEndpointAddress =
2183 fs_sink_desc.bEndpointAddress;
2184 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2186 hs_status_desc.bEndpointAddress =
2187 fs_status_desc.bEndpointAddress;
2191 if (gadget_is_otg(gadget)) {
2192 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2193 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2194 eth_config.bMaxPower = 4;
2195 #ifdef CONFIG_USB_ETH_RNDIS
2196 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2197 rndis_config.bMaxPower = 4;
2202 /* network device setup */
2203 #ifndef CONFIG_DM_ETH
2204 dev->net = &l_priv->netdev;
2206 dev->net = l_priv->netdev;
2213 dev->out_ep = out_ep;
2214 dev->status_ep = status_ep;
2216 memset(tmp, 0, sizeof(tmp));
2218 * Module params for these addresses should come from ID proms.
2219 * The host side address is used with CDC and RNDIS, and commonly
2220 * ends up in a persistent config database. It's not clear if
2221 * host side code for the SAFE thing cares -- its original BLAN
2222 * thing didn't, Sharp never assigned those addresses on Zaurii.
2224 #ifndef CONFIG_DM_ETH
2225 get_ether_addr(dev_addr, dev->net->enetaddr);
2226 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2228 get_ether_addr(dev_addr, pdata->enetaddr);
2229 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2232 get_ether_addr(host_addr, dev->host_mac);
2234 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2235 dev->host_mac[0], dev->host_mac[1],
2236 dev->host_mac[2], dev->host_mac[3],
2237 dev->host_mac[4], dev->host_mac[5]);
2240 status = rndis_init();
2242 pr_err("can't init RNDIS, %d", status);
2248 * use PKTSIZE (or aligned... from u-boot) and set
2249 * wMaxSegmentSize accordingly
2251 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2253 /* preallocate control message data and buffer */
2254 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2257 dev->req->buf = control_req;
2258 dev->req->complete = eth_setup_complete;
2260 /* ... and maybe likewise for status transfer */
2261 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2262 if (dev->status_ep) {
2263 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2265 if (!dev->stat_req) {
2266 usb_ep_free_request(dev->status_ep, dev->req);
2270 dev->stat_req->buf = status_req;
2271 dev->stat_req->context = NULL;
2275 /* finish hookup to lower layer ... */
2276 dev->gadget = gadget;
2277 set_gadget_data(gadget, dev);
2278 gadget->ep0->driver_data = dev;
2281 * two kinds of host-initiated state changes:
2282 * - iff DATA transfer is active, carrier is "on"
2283 * - tx queueing enabled if open *and* carrier is "on"
2286 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2287 out_ep->name, in_ep->name,
2288 status_ep ? " STATUS " : "",
2289 status_ep ? status_ep->name : ""
2291 #ifndef CONFIG_DM_ETH
2292 printf("MAC %pM\n", dev->net->enetaddr);
2294 printf("MAC %pM\n", pdata->enetaddr);
2298 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2299 dev->host_mac[0], dev->host_mac[1],
2300 dev->host_mac[2], dev->host_mac[3],
2301 dev->host_mac[4], dev->host_mac[5]);
2306 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2308 dev->rndis_config = rndis_register(rndis_control_ack);
2309 if (dev->rndis_config < 0) {
2312 debug("RNDIS setup failed\n");
2317 /* these set up a lot of the OIDs that RNDIS needs */
2318 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2319 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2320 &dev->stats, &dev->cdc_filter))
2322 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2325 if (rndis_set_param_medium(dev->rndis_config,
2326 NDIS_MEDIUM_802_3, 0))
2328 printf("RNDIS ready\n");
2333 pr_err("%s failed, status = %d", __func__, status);
2338 /*-------------------------------------------------------------------------*/
2340 #if CONFIG_IS_ENABLED(DM_USB)
2341 int dm_usb_init(struct eth_dev *e_dev)
2343 struct udevice *dev = NULL;
2346 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2348 pr_err("No USB device found\n");
2352 e_dev->usb_udev = dev;
2358 static int _usb_eth_init(struct ether_priv *priv)
2360 struct eth_dev *dev = &priv->ethdev;
2361 struct usb_gadget *gadget;
2363 unsigned long timeout = USB_CONNECT_TIMEOUT;
2365 #if CONFIG_IS_ENABLED(DM_USB)
2366 if (dm_usb_init(dev)) {
2367 pr_err("USB ether not found\n");
2371 board_usb_init(0, USB_INIT_DEVICE);
2374 /* Configure default mac-addresses for the USB ethernet device */
2375 #ifdef CONFIG_USBNET_DEV_ADDR
2376 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2378 #ifdef CONFIG_USBNET_HOST_ADDR
2379 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2381 /* Check if the user overruled the MAC addresses */
2382 if (env_get("usbnet_devaddr"))
2383 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2386 if (env_get("usbnet_hostaddr"))
2387 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2390 if (!is_eth_addr_valid(dev_addr)) {
2391 pr_err("Need valid 'usbnet_devaddr' to be set");
2394 if (!is_eth_addr_valid(host_addr)) {
2395 pr_err("Need valid 'usbnet_hostaddr' to be set");
2399 priv->eth_driver.speed = DEVSPEED;
2400 priv->eth_driver.bind = eth_bind;
2401 priv->eth_driver.unbind = eth_unbind;
2402 priv->eth_driver.setup = eth_setup;
2403 priv->eth_driver.reset = eth_disconnect;
2404 priv->eth_driver.disconnect = eth_disconnect;
2405 priv->eth_driver.suspend = eth_suspend;
2406 priv->eth_driver.resume = eth_resume;
2407 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2410 dev->network_started = 0;
2412 packet_received = 0;
2415 gadget = dev->gadget;
2416 usb_gadget_connect(gadget);
2418 if (env_get("cdc_connect_timeout"))
2419 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
2420 NULL, 10) * CONFIG_SYS_HZ;
2422 while (!dev->network_started) {
2423 /* Handle control-c and timeouts */
2424 if (ctrlc() || (get_timer(ts) > timeout)) {
2425 pr_err("The remote end did not respond in time.");
2428 usb_gadget_handle_interrupts(0);
2431 packet_received = 0;
2432 rx_submit(dev, dev->rx_req, 0);
2438 static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
2441 void *rndis_pkt = NULL;
2442 struct eth_dev *dev = &priv->ethdev;
2443 struct usb_request *req = dev->tx_req;
2445 unsigned long timeout = USB_CONNECT_TIMEOUT;
2447 debug("%s:...\n", __func__);
2449 /* new buffer is needed to include RNDIS header */
2450 if (rndis_active(dev)) {
2451 rndis_pkt = malloc(length +
2452 sizeof(struct rndis_packet_msg_type));
2454 pr_err("No memory to alloc RNDIS packet");
2457 rndis_add_hdr(rndis_pkt, length);
2458 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2461 length += sizeof(struct rndis_packet_msg_type);
2464 req->context = NULL;
2465 req->complete = tx_complete;
2468 * use zlp framing on tx for strict CDC-Ether conformance,
2469 * though any robust network rx path ignores extra padding.
2470 * and some hardware doesn't like to write zlps.
2473 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2476 req->length = length;
2478 /* throttle highspeed IRQ rate back slightly */
2479 if (gadget_is_dualspeed(dev->gadget))
2480 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2481 ? ((dev->tx_qlen % qmult) != 0) : 0;
2487 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2490 debug("%s: packet queued\n", __func__);
2491 while (!packet_sent) {
2492 if (get_timer(ts) > timeout) {
2493 printf("timeout sending packets to usb ethernet\n");
2496 usb_gadget_handle_interrupts(0);
2503 dev->stats.tx_dropped++;
2507 static int _usb_eth_recv(struct ether_priv *priv)
2509 usb_gadget_handle_interrupts(0);
2514 void _usb_eth_halt(struct ether_priv *priv)
2516 struct eth_dev *dev = &priv->ethdev;
2518 /* If the gadget not registered, simple return */
2523 * Some USB controllers may need additional deinitialization here
2524 * before dropping pull-up (also due to hardware issues).
2525 * For example: unhandled interrupt with status stage started may
2526 * bring the controller to fully broken state (until board reset).
2527 * There are some variants to debug and fix such cases:
2528 * 1) In the case of RNDIS connection eth_stop can perform additional
2529 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2530 * 2) 'pullup' callback in your UDC driver can be improved to perform
2531 * this deinitialization.
2535 usb_gadget_disconnect(dev->gadget);
2537 /* Clear pending interrupt */
2538 if (dev->network_started) {
2539 usb_gadget_handle_interrupts(0);
2540 dev->network_started = 0;
2543 usb_gadget_unregister_driver(&priv->eth_driver);
2544 #if !CONFIG_IS_ENABLED(DM_USB)
2545 board_usb_cleanup(0, USB_INIT_DEVICE);
2549 #ifndef CONFIG_DM_ETH
2550 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2552 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2554 return _usb_eth_init(priv);
2557 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2559 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2561 return _usb_eth_send(priv, packet, length);
2564 static int usb_eth_recv(struct eth_device *netdev)
2566 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2567 struct eth_dev *dev = &priv->ethdev;
2570 ret = _usb_eth_recv(priv);
2572 pr_err("error packet receive\n");
2576 if (!packet_received)
2580 net_process_received_packet(net_rx_packets[0],
2581 dev->rx_req->length);
2583 pr_err("dev->rx_req invalid");
2585 packet_received = 0;
2586 rx_submit(dev, dev->rx_req, 0);
2591 void usb_eth_halt(struct eth_device *netdev)
2593 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2595 _usb_eth_halt(priv);
2598 int usb_eth_initialize(bd_t *bi)
2600 struct eth_device *netdev = &l_priv->netdev;
2602 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2604 netdev->init = usb_eth_init;
2605 netdev->send = usb_eth_send;
2606 netdev->recv = usb_eth_recv;
2607 netdev->halt = usb_eth_halt;
2608 netdev->priv = l_priv;
2610 #ifdef CONFIG_MCAST_TFTP
2611 #error not supported
2613 eth_register(netdev);
2617 static int usb_eth_start(struct udevice *dev)
2619 struct ether_priv *priv = dev_get_priv(dev);
2621 return _usb_eth_init(priv);
2624 static int usb_eth_send(struct udevice *dev, void *packet, int length)
2626 struct ether_priv *priv = dev_get_priv(dev);
2628 return _usb_eth_send(priv, packet, length);
2631 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2633 struct ether_priv *priv = dev_get_priv(dev);
2634 struct eth_dev *ethdev = &priv->ethdev;
2637 ret = _usb_eth_recv(priv);
2639 pr_err("error packet receive\n");
2643 if (packet_received) {
2644 if (ethdev->rx_req) {
2645 *packetp = (uchar *)net_rx_packets[0];
2646 return ethdev->rx_req->length;
2648 pr_err("dev->rx_req invalid");
2656 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2659 struct ether_priv *priv = dev_get_priv(dev);
2660 struct eth_dev *ethdev = &priv->ethdev;
2662 packet_received = 0;
2664 return rx_submit(ethdev, ethdev->rx_req, 0);
2667 static void usb_eth_stop(struct udevice *dev)
2669 struct ether_priv *priv = dev_get_priv(dev);
2671 _usb_eth_halt(priv);
2674 static int usb_eth_probe(struct udevice *dev)
2676 struct ether_priv *priv = dev_get_priv(dev);
2677 struct eth_pdata *pdata = dev_get_platdata(dev);
2682 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2683 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2688 static const struct eth_ops usb_eth_ops = {
2689 .start = usb_eth_start,
2690 .send = usb_eth_send,
2691 .recv = usb_eth_recv,
2692 .free_pkt = usb_eth_free_pkt,
2693 .stop = usb_eth_stop,
2696 int usb_ether_init(void)
2698 struct udevice *dev;
2699 struct udevice *usb_dev;
2702 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2703 if (!usb_dev || ret) {
2704 pr_err("No USB device found\n");
2708 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2710 pr_err("usb - not able to bind usb_ether device\n");
2717 U_BOOT_DRIVER(eth_usb) = {
2718 .name = "usb_ether",
2720 .probe = usb_eth_probe,
2721 .ops = &usb_eth_ops,
2722 .priv_auto_alloc_size = sizeof(struct ether_priv),
2723 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2724 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2726 #endif /* CONFIG_DM_ETH */