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 */
105 struct usb_ep *in_ep, *out_ep, *status_ep;
106 const struct usb_endpoint_descriptor
109 struct usb_request *tx_req, *rx_req;
111 #ifndef CONFIG_DM_ETH
112 struct eth_device *net;
116 struct net_device_stats stats;
117 unsigned int tx_qlen;
122 unsigned suspended:1;
123 unsigned network_started:1;
127 #define WORK_RX_MEMORY 0
129 u8 host_mac[ETH_ALEN];
133 * This version autoconfigures as much as possible at run-time.
135 * It also ASSUMES a self-powered device, without remote wakeup,
136 * although remote wakeup support would make sense.
139 /*-------------------------------------------------------------------------*/
141 struct eth_dev ethdev;
142 #ifndef CONFIG_DM_ETH
143 struct eth_device netdev;
145 struct udevice *netdev;
147 struct usb_gadget_driver eth_driver;
150 struct ether_priv eth_priv;
151 struct ether_priv *l_priv = ð_priv;
153 /*-------------------------------------------------------------------------*/
155 /* "main" config is either CDC, or its simple subset */
156 static inline int is_cdc(struct eth_dev *dev)
158 #if !defined(CONFIG_USB_ETH_SUBSET)
159 return 1; /* only cdc possible */
160 #elif !defined(CONFIG_USB_ETH_CDC)
161 return 0; /* only subset possible */
163 return dev->cdc; /* depends on what hardware we found */
167 /* "secondary" RNDIS config may sometimes be activated */
168 static inline int rndis_active(struct eth_dev *dev)
170 #ifdef CONFIG_USB_ETH_RNDIS
177 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
178 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
180 #define DEFAULT_QLEN 2 /* double buffering by default */
182 /* peak bulk transfer bits-per-second */
183 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
184 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
186 #ifdef CONFIG_USB_GADGET_DUALSPEED
187 #define DEVSPEED USB_SPEED_HIGH
189 #ifdef CONFIG_USB_ETH_QMULT
190 #define qmult CONFIG_USB_ETH_QMULT
195 /* for dual-speed hardware, use deeper queues at highspeed */
196 #define qlen(gadget) \
197 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
199 static inline int BITRATE(struct usb_gadget *g)
201 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
204 #else /* full speed (low speed doesn't do bulk) */
208 #define DEVSPEED USB_SPEED_FULL
210 #define qlen(gadget) DEFAULT_QLEN
212 static inline int BITRATE(struct usb_gadget *g)
218 /*-------------------------------------------------------------------------*/
221 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
222 * Instead: allocate your own, using normal USB-IF procedures.
226 * Thanks to NetChip Technologies for donating this product ID.
227 * It's for devices with only CDC Ethernet configurations.
229 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
230 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
233 * For hardware that can't talk CDC, we use the same vendor ID that
234 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
235 * with pxa250. We're protocol-compatible, if the host-side drivers
236 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
237 * drivers that need to hard-wire endpoint numbers have a hook.
239 * The protocol is a minimal subset of CDC Ether, which works on any bulk
240 * hardware that's not deeply broken ... even on hardware that can't talk
241 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
242 * doesn't handle control-OUT).
244 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
245 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
248 * For hardware that can talk RNDIS and either of the above protocols,
249 * use this ID ... the windows INF files will know it. Unless it's
250 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
251 * the non-RNDIS configuration.
253 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
254 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
257 * Some systems will want different product identifers published in the
258 * device descriptor, either numbers or strings or both. These string
259 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
263 * Emulating them in eth_bind:
264 * static ushort idVendor;
265 * static ushort idProduct;
268 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
269 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
271 static char *iManufacturer = "U-Boot";
274 /* These probably need to be configurable. */
275 static ushort bcdDevice;
276 static char *iProduct;
277 static char *iSerialNumber;
279 static char dev_addr[18];
281 static char host_addr[18];
284 /*-------------------------------------------------------------------------*/
287 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
288 * ep0 implementation: descriptors, config management, setup().
289 * also optional class-specific notification interrupt transfer.
293 * DESCRIPTORS ... most are static, but strings and (full) configuration
294 * descriptors are built on demand. For now we do either full CDC, or
295 * our simple subset, with RNDIS as an optional second configuration.
297 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
298 * the class descriptors match a modem (they're ignored; it's really just
299 * Ethernet functionality), they don't need the NOP altsetting, and the
300 * status transfer endpoint isn't optional.
303 #define STRING_MANUFACTURER 1
304 #define STRING_PRODUCT 2
305 #define STRING_ETHADDR 3
306 #define STRING_DATA 4
307 #define STRING_CONTROL 5
308 #define STRING_RNDIS_CONTROL 6
310 #define STRING_SUBSET 8
311 #define STRING_RNDIS 9
312 #define STRING_SERIALNUMBER 10
314 /* holds our biggest descriptor (or RNDIS response) */
315 #define USB_BUFSIZ 256
318 * This device advertises one configuration, eth_config, unless RNDIS
319 * is enabled (rndis_config) on hardware supporting at least two configs.
321 * NOTE: Controllers like superh_udc should probably be able to use
322 * an RNDIS-only configuration.
324 * FIXME define some higher-powered configurations to make it easier
325 * to recharge batteries ...
328 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
329 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
331 static struct usb_device_descriptor
333 .bLength = sizeof device_desc,
334 .bDescriptorType = USB_DT_DEVICE,
336 .bcdUSB = __constant_cpu_to_le16(0x0200),
338 .bDeviceClass = USB_CLASS_COMM,
339 .bDeviceSubClass = 0,
340 .bDeviceProtocol = 0,
342 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
343 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
344 .iManufacturer = STRING_MANUFACTURER,
345 .iProduct = STRING_PRODUCT,
346 .bNumConfigurations = 1,
349 static struct usb_otg_descriptor
351 .bLength = sizeof otg_descriptor,
352 .bDescriptorType = USB_DT_OTG,
354 .bmAttributes = USB_OTG_SRP,
357 static struct usb_config_descriptor
359 .bLength = sizeof eth_config,
360 .bDescriptorType = USB_DT_CONFIG,
362 /* compute wTotalLength on the fly */
364 .bConfigurationValue = DEV_CONFIG_VALUE,
365 .iConfiguration = STRING_CDC,
366 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
370 #ifdef CONFIG_USB_ETH_RNDIS
371 static struct usb_config_descriptor
373 .bLength = sizeof rndis_config,
374 .bDescriptorType = USB_DT_CONFIG,
376 /* compute wTotalLength on the fly */
378 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
379 .iConfiguration = STRING_RNDIS,
380 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
386 * Compared to the simple CDC subset, the full CDC Ethernet model adds
387 * three class descriptors, two interface descriptors, optional status
388 * endpoint. Both have a "data" interface and two bulk endpoints.
389 * There are also differences in how control requests are handled.
391 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
392 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
393 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
394 * work around those bugs) are unlikely to ever come from MSFT, you may
395 * wish to avoid using RNDIS.
397 * MCCI offers an alternative to RNDIS if you need to connect to Windows
398 * but have hardware that can't support CDC Ethernet. We add descriptors
399 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
400 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
401 * get those drivers from MCCI, or bundled with various products.
404 #ifdef CONFIG_USB_ETH_CDC
405 static struct usb_interface_descriptor
407 .bLength = sizeof control_intf,
408 .bDescriptorType = USB_DT_INTERFACE,
410 .bInterfaceNumber = 0,
411 /* status endpoint is optional; this may be patched later */
413 .bInterfaceClass = USB_CLASS_COMM,
414 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
415 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
416 .iInterface = STRING_CONTROL,
420 #ifdef CONFIG_USB_ETH_RNDIS
421 static const struct usb_interface_descriptor
422 rndis_control_intf = {
423 .bLength = sizeof rndis_control_intf,
424 .bDescriptorType = USB_DT_INTERFACE,
426 .bInterfaceNumber = 0,
428 .bInterfaceClass = USB_CLASS_COMM,
429 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
430 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
431 .iInterface = STRING_RNDIS_CONTROL,
435 static const struct usb_cdc_header_desc header_desc = {
436 .bLength = sizeof header_desc,
437 .bDescriptorType = USB_DT_CS_INTERFACE,
438 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
440 .bcdCDC = __constant_cpu_to_le16(0x0110),
443 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
445 static const struct usb_cdc_union_desc union_desc = {
446 .bLength = sizeof union_desc,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_UNION_TYPE,
450 .bMasterInterface0 = 0, /* index of control interface */
451 .bSlaveInterface0 = 1, /* index of DATA interface */
454 #endif /* CDC || RNDIS */
456 #ifdef CONFIG_USB_ETH_RNDIS
458 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
459 .bLength = sizeof call_mgmt_descriptor,
460 .bDescriptorType = USB_DT_CS_INTERFACE,
461 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
463 .bmCapabilities = 0x00,
464 .bDataInterface = 0x01,
467 static const struct usb_cdc_acm_descriptor acm_descriptor = {
468 .bLength = sizeof acm_descriptor,
469 .bDescriptorType = USB_DT_CS_INTERFACE,
470 .bDescriptorSubType = USB_CDC_ACM_TYPE,
472 .bmCapabilities = 0x00,
477 #ifndef CONFIG_USB_ETH_CDC
480 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
481 * ways: data endpoints live in the control interface, there's no data
482 * interface, and it's not used to talk to a cell phone radio.
485 static const struct usb_cdc_mdlm_desc mdlm_desc = {
486 .bLength = sizeof mdlm_desc,
487 .bDescriptorType = USB_DT_CS_INTERFACE,
488 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
490 .bcdVersion = __constant_cpu_to_le16(0x0100),
492 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
493 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
498 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
499 * can't really use its struct. All we do here is say that we're using
500 * the submode of "SAFE" which directly matches the CDC Subset.
502 #ifdef CONFIG_USB_ETH_SUBSET
503 static const u8 mdlm_detail_desc[] = {
506 USB_CDC_MDLM_DETAIL_TYPE,
509 0, /* network control capabilities (none) */
510 0, /* network data capabilities ("raw" encapsulation) */
516 static const struct usb_cdc_ether_desc ether_desc = {
517 .bLength = sizeof(ether_desc),
518 .bDescriptorType = USB_DT_CS_INTERFACE,
519 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
521 /* this descriptor actually adds value, surprise! */
522 .iMACAddress = STRING_ETHADDR,
523 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
524 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
525 .wNumberMCFilters = __constant_cpu_to_le16(0),
526 .bNumberPowerFilters = 0,
529 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
532 * include the status endpoint if we can, even where it's optional.
533 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
534 * packet, to simplify cancellation; and a big transfer interval, to
535 * waste less bandwidth.
537 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
538 * if they ignore the connect/disconnect notifications that real aether
539 * can provide. more advanced cdc configurations might want to support
540 * encapsulated commands (vendor-specific, using control-OUT).
542 * RNDIS requires the status endpoint, since it uses that encapsulation
543 * mechanism for its funky RPC scheme.
546 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
547 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
549 static struct usb_endpoint_descriptor
551 .bLength = USB_DT_ENDPOINT_SIZE,
552 .bDescriptorType = USB_DT_ENDPOINT,
554 .bEndpointAddress = USB_DIR_IN,
555 .bmAttributes = USB_ENDPOINT_XFER_INT,
556 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
557 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
561 #ifdef CONFIG_USB_ETH_CDC
563 /* the default data interface has no endpoints ... */
565 static const struct usb_interface_descriptor
567 .bLength = sizeof data_nop_intf,
568 .bDescriptorType = USB_DT_INTERFACE,
570 .bInterfaceNumber = 1,
571 .bAlternateSetting = 0,
573 .bInterfaceClass = USB_CLASS_CDC_DATA,
574 .bInterfaceSubClass = 0,
575 .bInterfaceProtocol = 0,
578 /* ... but the "real" data interface has two bulk endpoints */
580 static const struct usb_interface_descriptor
582 .bLength = sizeof data_intf,
583 .bDescriptorType = USB_DT_INTERFACE,
585 .bInterfaceNumber = 1,
586 .bAlternateSetting = 1,
588 .bInterfaceClass = USB_CLASS_CDC_DATA,
589 .bInterfaceSubClass = 0,
590 .bInterfaceProtocol = 0,
591 .iInterface = STRING_DATA,
596 #ifdef CONFIG_USB_ETH_RNDIS
598 /* RNDIS doesn't activate by changing to the "real" altsetting */
600 static const struct usb_interface_descriptor
602 .bLength = sizeof rndis_data_intf,
603 .bDescriptorType = USB_DT_INTERFACE,
605 .bInterfaceNumber = 1,
606 .bAlternateSetting = 0,
608 .bInterfaceClass = USB_CLASS_CDC_DATA,
609 .bInterfaceSubClass = 0,
610 .bInterfaceProtocol = 0,
611 .iInterface = STRING_DATA,
616 #ifdef CONFIG_USB_ETH_SUBSET
619 * "Simple" CDC-subset option is a simple vendor-neutral model that most
620 * full speed controllers can handle: one interface, two bulk endpoints.
622 * To assist host side drivers, we fancy it up a bit, and add descriptors
623 * so some host side drivers will understand it as a "SAFE" variant.
626 static const struct usb_interface_descriptor
628 .bLength = sizeof subset_data_intf,
629 .bDescriptorType = USB_DT_INTERFACE,
631 .bInterfaceNumber = 0,
632 .bAlternateSetting = 0,
634 .bInterfaceClass = USB_CLASS_COMM,
635 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
636 .bInterfaceProtocol = 0,
637 .iInterface = STRING_DATA,
642 static struct usb_endpoint_descriptor
644 .bLength = USB_DT_ENDPOINT_SIZE,
645 .bDescriptorType = USB_DT_ENDPOINT,
647 .bEndpointAddress = USB_DIR_IN,
648 .bmAttributes = USB_ENDPOINT_XFER_BULK,
649 .wMaxPacketSize = __constant_cpu_to_le16(64),
652 static struct usb_endpoint_descriptor
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
657 .bEndpointAddress = USB_DIR_OUT,
658 .bmAttributes = USB_ENDPOINT_XFER_BULK,
659 .wMaxPacketSize = __constant_cpu_to_le16(64),
662 static const struct usb_descriptor_header *fs_eth_function[11] = {
663 (struct usb_descriptor_header *) &otg_descriptor,
664 #ifdef CONFIG_USB_ETH_CDC
665 /* "cdc" mode descriptors */
666 (struct usb_descriptor_header *) &control_intf,
667 (struct usb_descriptor_header *) &header_desc,
668 (struct usb_descriptor_header *) &union_desc,
669 (struct usb_descriptor_header *) ðer_desc,
670 /* NOTE: status endpoint may need to be removed */
671 (struct usb_descriptor_header *) &fs_status_desc,
672 /* data interface, with altsetting */
673 (struct usb_descriptor_header *) &data_nop_intf,
674 (struct usb_descriptor_header *) &data_intf,
675 (struct usb_descriptor_header *) &fs_source_desc,
676 (struct usb_descriptor_header *) &fs_sink_desc,
678 #endif /* CONFIG_USB_ETH_CDC */
681 static inline void fs_subset_descriptors(void)
683 #ifdef CONFIG_USB_ETH_SUBSET
684 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
685 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
686 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
687 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
688 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
689 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
690 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
691 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
692 fs_eth_function[8] = NULL;
694 fs_eth_function[1] = NULL;
698 #ifdef CONFIG_USB_ETH_RNDIS
699 static const struct usb_descriptor_header *fs_rndis_function[] = {
700 (struct usb_descriptor_header *) &otg_descriptor,
701 /* control interface matches ACM, not Ethernet */
702 (struct usb_descriptor_header *) &rndis_control_intf,
703 (struct usb_descriptor_header *) &header_desc,
704 (struct usb_descriptor_header *) &call_mgmt_descriptor,
705 (struct usb_descriptor_header *) &acm_descriptor,
706 (struct usb_descriptor_header *) &union_desc,
707 (struct usb_descriptor_header *) &fs_status_desc,
708 /* data interface has no altsetting */
709 (struct usb_descriptor_header *) &rndis_data_intf,
710 (struct usb_descriptor_header *) &fs_source_desc,
711 (struct usb_descriptor_header *) &fs_sink_desc,
717 * usb 2.0 devices need to expose both high speed and full speed
718 * descriptors, unless they only run at full speed.
721 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
722 static struct usb_endpoint_descriptor
724 .bLength = USB_DT_ENDPOINT_SIZE,
725 .bDescriptorType = USB_DT_ENDPOINT,
727 .bmAttributes = USB_ENDPOINT_XFER_INT,
728 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
729 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
731 #endif /* CONFIG_USB_ETH_CDC */
733 static struct usb_endpoint_descriptor
735 .bLength = USB_DT_ENDPOINT_SIZE,
736 .bDescriptorType = USB_DT_ENDPOINT,
738 .bmAttributes = USB_ENDPOINT_XFER_BULK,
739 .wMaxPacketSize = __constant_cpu_to_le16(512),
742 static struct usb_endpoint_descriptor
744 .bLength = USB_DT_ENDPOINT_SIZE,
745 .bDescriptorType = USB_DT_ENDPOINT,
747 .bmAttributes = USB_ENDPOINT_XFER_BULK,
748 .wMaxPacketSize = __constant_cpu_to_le16(512),
751 static struct usb_qualifier_descriptor
753 .bLength = sizeof dev_qualifier,
754 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
756 .bcdUSB = __constant_cpu_to_le16(0x0200),
757 .bDeviceClass = USB_CLASS_COMM,
759 .bNumConfigurations = 1,
762 static const struct usb_descriptor_header *hs_eth_function[11] = {
763 (struct usb_descriptor_header *) &otg_descriptor,
764 #ifdef CONFIG_USB_ETH_CDC
765 /* "cdc" mode descriptors */
766 (struct usb_descriptor_header *) &control_intf,
767 (struct usb_descriptor_header *) &header_desc,
768 (struct usb_descriptor_header *) &union_desc,
769 (struct usb_descriptor_header *) ðer_desc,
770 /* NOTE: status endpoint may need to be removed */
771 (struct usb_descriptor_header *) &hs_status_desc,
772 /* data interface, with altsetting */
773 (struct usb_descriptor_header *) &data_nop_intf,
774 (struct usb_descriptor_header *) &data_intf,
775 (struct usb_descriptor_header *) &hs_source_desc,
776 (struct usb_descriptor_header *) &hs_sink_desc,
778 #endif /* CONFIG_USB_ETH_CDC */
781 static inline void hs_subset_descriptors(void)
783 #ifdef CONFIG_USB_ETH_SUBSET
784 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
785 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
786 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
787 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
788 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
789 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
790 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
791 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
792 hs_eth_function[8] = NULL;
794 hs_eth_function[1] = NULL;
798 #ifdef CONFIG_USB_ETH_RNDIS
799 static const struct usb_descriptor_header *hs_rndis_function[] = {
800 (struct usb_descriptor_header *) &otg_descriptor,
801 /* control interface matches ACM, not Ethernet */
802 (struct usb_descriptor_header *) &rndis_control_intf,
803 (struct usb_descriptor_header *) &header_desc,
804 (struct usb_descriptor_header *) &call_mgmt_descriptor,
805 (struct usb_descriptor_header *) &acm_descriptor,
806 (struct usb_descriptor_header *) &union_desc,
807 (struct usb_descriptor_header *) &hs_status_desc,
808 /* data interface has no altsetting */
809 (struct usb_descriptor_header *) &rndis_data_intf,
810 (struct usb_descriptor_header *) &hs_source_desc,
811 (struct usb_descriptor_header *) &hs_sink_desc,
817 /* maxpacket and other transfer characteristics vary by speed. */
818 static inline struct usb_endpoint_descriptor *
819 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
820 struct usb_endpoint_descriptor *fs)
822 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
827 /*-------------------------------------------------------------------------*/
829 /* descriptors that are built on-demand */
831 static char manufacturer[50];
832 static char product_desc[40] = DRIVER_DESC;
833 static char serial_number[20];
835 /* address that the host will use ... usually assigned at random */
836 static char ethaddr[2 * ETH_ALEN + 1];
838 /* static strings, in UTF-8 */
839 static struct usb_string strings[] = {
840 { STRING_MANUFACTURER, manufacturer, },
841 { STRING_PRODUCT, product_desc, },
842 { STRING_SERIALNUMBER, serial_number, },
843 { STRING_DATA, "Ethernet Data", },
844 { STRING_ETHADDR, ethaddr, },
845 #ifdef CONFIG_USB_ETH_CDC
846 { STRING_CDC, "CDC Ethernet", },
847 { STRING_CONTROL, "CDC Communications Control", },
849 #ifdef CONFIG_USB_ETH_SUBSET
850 { STRING_SUBSET, "CDC Ethernet Subset", },
852 #ifdef CONFIG_USB_ETH_RNDIS
853 { STRING_RNDIS, "RNDIS", },
854 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
856 { } /* end of list */
859 static struct usb_gadget_strings stringtab = {
860 .language = 0x0409, /* en-us */
864 /*============================================================================*/
865 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
867 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
868 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
871 /*============================================================================*/
874 * one config, two interfaces: control, data.
875 * complications: class descriptors, and an altsetting.
878 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
881 const struct usb_config_descriptor *config;
882 const struct usb_descriptor_header **function;
885 if (gadget_is_dualspeed(g)) {
886 hs = (g->speed == USB_SPEED_HIGH);
887 if (type == USB_DT_OTHER_SPEED_CONFIG)
890 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
892 if (index >= device_desc.bNumConfigurations)
895 #ifdef CONFIG_USB_ETH_RNDIS
897 * list the RNDIS config first, to make Microsoft's drivers
898 * happy. DOCSIS 1.0 needs this too.
900 if (device_desc.bNumConfigurations == 2 && index == 0) {
901 config = &rndis_config;
902 function = which_fn(rndis);
906 config = ð_config;
907 function = which_fn(eth);
910 /* for now, don't advertise srp-only devices */
914 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
917 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
921 /*-------------------------------------------------------------------------*/
923 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
924 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
927 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
930 struct usb_gadget *gadget = dev->gadget;
932 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
933 /* status endpoint used for RNDIS and (optionally) CDC */
934 if (!subset_active(dev) && dev->status_ep) {
935 dev->status = ep_desc(gadget, &hs_status_desc,
937 dev->status_ep->driver_data = dev;
939 result = usb_ep_enable(dev->status_ep, dev->status);
941 debug("enable %s --> %d\n",
942 dev->status_ep->name, result);
948 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
949 dev->in_ep->driver_data = dev;
951 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
952 dev->out_ep->driver_data = dev;
955 * With CDC, the host isn't allowed to use these two data
956 * endpoints in the default altsetting for the interface.
957 * so we don't activate them yet. Reset from SET_INTERFACE.
959 * Strictly speaking RNDIS should work the same: activation is
960 * a side effect of setting a packet filter. Deactivation is
961 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
963 if (!cdc_active(dev)) {
964 result = usb_ep_enable(dev->in_ep, dev->in);
966 debug("enable %s --> %d\n",
967 dev->in_ep->name, result);
971 result = usb_ep_enable(dev->out_ep, dev->out);
973 debug("enable %s --> %d\n",
974 dev->out_ep->name, result);
981 result = alloc_requests(dev, qlen(gadget), gfp_flags);
983 /* on error, disable any endpoints */
985 if (!subset_active(dev) && dev->status_ep)
986 (void) usb_ep_disable(dev->status_ep);
988 (void) usb_ep_disable(dev->in_ep);
989 (void) usb_ep_disable(dev->out_ep);
992 } else if (!cdc_active(dev)) {
994 * activate non-CDC configs right away
995 * this isn't strictly according to the RNDIS spec
997 eth_start(dev, GFP_ATOMIC);
1000 /* caller is responsible for cleanup on error */
1004 static void eth_reset_config(struct eth_dev *dev)
1006 if (dev->config == 0)
1009 debug("%s\n", __func__);
1011 rndis_uninit(dev->rndis_config);
1014 * disable endpoints, forcing (synchronous) completion of
1015 * pending i/o. then free the requests.
1019 usb_ep_disable(dev->in_ep);
1021 usb_ep_free_request(dev->in_ep, dev->tx_req);
1026 usb_ep_disable(dev->out_ep);
1028 usb_ep_free_request(dev->out_ep, dev->rx_req);
1033 usb_ep_disable(dev->status_ep);
1036 dev->cdc_filter = 0;
1041 * change our operational config. must agree with the code
1042 * that returns config descriptors, and altsetting code.
1044 static int eth_set_config(struct eth_dev *dev, unsigned number,
1048 struct usb_gadget *gadget = dev->gadget;
1050 if (gadget_is_sa1100(gadget)
1052 && dev->tx_qlen != 0) {
1053 /* tx fifo is full, but we can't clear it...*/
1054 pr_err("can't change configurations");
1057 eth_reset_config(dev);
1060 case DEV_CONFIG_VALUE:
1061 result = set_ether_config(dev, gfp_flags);
1063 #ifdef CONFIG_USB_ETH_RNDIS
1064 case DEV_RNDIS_CONFIG_VALUE:
1066 result = set_ether_config(dev, gfp_flags);
1078 eth_reset_config(dev);
1079 usb_gadget_vbus_draw(dev->gadget,
1080 gadget_is_otg(dev->gadget) ? 8 : 100);
1085 power = 2 * eth_config.bMaxPower;
1086 usb_gadget_vbus_draw(dev->gadget, power);
1088 switch (gadget->speed) {
1089 case USB_SPEED_FULL:
1090 speed = "full"; break;
1091 #ifdef CONFIG_USB_GADGET_DUALSPEED
1092 case USB_SPEED_HIGH:
1093 speed = "high"; break;
1099 dev->config = number;
1100 printf("%s speed config #%d: %d mA, %s, using %s\n",
1101 speed, number, power, driver_desc,
1106 : "CDC Ethernet Subset"));
1111 /*-------------------------------------------------------------------------*/
1113 #ifdef CONFIG_USB_ETH_CDC
1116 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1117 * only to notify the host about link status changes (which we support) or
1118 * report completion of some encapsulated command (as used in RNDIS). Since
1119 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1120 * command mechanism; and only one status request is ever queued.
1122 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1124 struct usb_cdc_notification *event = req->buf;
1125 int value = req->status;
1126 struct eth_dev *dev = ep->driver_data;
1128 /* issue the second notification if host reads the first */
1129 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1131 __le32 *data = req->buf + sizeof *event;
1133 event->bmRequestType = 0xA1;
1134 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1135 event->wValue = __constant_cpu_to_le16(0);
1136 event->wIndex = __constant_cpu_to_le16(1);
1137 event->wLength = __constant_cpu_to_le16(8);
1139 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1140 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1142 req->length = STATUS_BYTECOUNT;
1143 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1144 debug("send SPEED_CHANGE --> %d\n", value);
1147 } else if (value != -ECONNRESET) {
1148 debug("event %02x --> %d\n",
1149 event->bNotificationType, value);
1150 if (event->bNotificationType ==
1151 USB_CDC_NOTIFY_SPEED_CHANGE) {
1152 dev->network_started = 1;
1153 printf("USB network up!\n");
1156 req->context = NULL;
1159 static void issue_start_status(struct eth_dev *dev)
1161 struct usb_request *req = dev->stat_req;
1162 struct usb_cdc_notification *event;
1168 * FIXME ugly idiom, maybe we'd be better with just
1169 * a "cancel the whole queue" primitive since any
1170 * unlink-one primitive has way too many error modes.
1171 * here, we "know" toggle is already clear...
1173 * FIXME iff req->context != null just dequeue it
1175 usb_ep_disable(dev->status_ep);
1176 usb_ep_enable(dev->status_ep, dev->status);
1179 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1180 * a SPEED_CHANGE. could be useful in some configs.
1183 event->bmRequestType = 0xA1;
1184 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1185 event->wValue = __constant_cpu_to_le16(1); /* connected */
1186 event->wIndex = __constant_cpu_to_le16(1);
1189 req->length = sizeof *event;
1190 req->complete = eth_status_complete;
1193 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1195 debug("status buf queue --> %d\n", value);
1200 /*-------------------------------------------------------------------------*/
1202 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1204 if (req->status || req->actual != req->length)
1205 debug("setup complete --> %d, %d/%d\n",
1206 req->status, req->actual, req->length);
1209 #ifdef CONFIG_USB_ETH_RNDIS
1211 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1213 if (req->status || req->actual != req->length)
1214 debug("rndis response complete --> %d, %d/%d\n",
1215 req->status, req->actual, req->length);
1217 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1220 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1222 struct eth_dev *dev = ep->driver_data;
1225 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1226 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1228 pr_err("%s: rndis parse error %d", __func__, status);
1234 * The setup() callback implements all the ep0 functionality that's not
1235 * handled lower down. CDC has a number of less-common features:
1237 * - two interfaces: control, and ethernet data
1238 * - Ethernet data interface has two altsettings: default, and active
1239 * - class-specific descriptors for the control interface
1240 * - class-specific control requests
1243 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1245 struct eth_dev *dev = get_gadget_data(gadget);
1246 struct usb_request *req = dev->req;
1247 int value = -EOPNOTSUPP;
1248 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1249 u16 wValue = le16_to_cpu(ctrl->wValue);
1250 u16 wLength = le16_to_cpu(ctrl->wLength);
1253 * descriptors just go into the pre-allocated ep0 buffer,
1254 * while config change events may enable network traffic.
1257 debug("%s\n", __func__);
1259 req->complete = eth_setup_complete;
1260 switch (ctrl->bRequest) {
1262 case USB_REQ_GET_DESCRIPTOR:
1263 if (ctrl->bRequestType != USB_DIR_IN)
1265 switch (wValue >> 8) {
1268 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1269 value = min(wLength, (u16) sizeof device_desc);
1270 memcpy(req->buf, &device_desc, value);
1272 case USB_DT_DEVICE_QUALIFIER:
1273 if (!gadget_is_dualspeed(gadget))
1275 value = min(wLength, (u16) sizeof dev_qualifier);
1276 memcpy(req->buf, &dev_qualifier, value);
1279 case USB_DT_OTHER_SPEED_CONFIG:
1280 if (!gadget_is_dualspeed(gadget))
1284 value = config_buf(gadget, req->buf,
1287 gadget_is_otg(gadget));
1289 value = min(wLength, (u16) value);
1293 value = usb_gadget_get_string(&stringtab,
1294 wValue & 0xff, req->buf);
1297 value = min(wLength, (u16) value);
1303 case USB_REQ_SET_CONFIGURATION:
1304 if (ctrl->bRequestType != 0)
1306 if (gadget->a_hnp_support)
1307 debug("HNP available\n");
1308 else if (gadget->a_alt_hnp_support)
1309 debug("HNP needs a different root port\n");
1310 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1312 case USB_REQ_GET_CONFIGURATION:
1313 if (ctrl->bRequestType != USB_DIR_IN)
1315 *(u8 *)req->buf = dev->config;
1316 value = min(wLength, (u16) 1);
1319 case USB_REQ_SET_INTERFACE:
1320 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1324 if (!cdc_active(dev) && wIndex != 0)
1328 * PXA hardware partially handles SET_INTERFACE;
1329 * we need to kluge around that interference.
1331 if (gadget_is_pxa(gadget)) {
1332 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1335 * PXA25x driver use non-CDC ethernet gadget.
1336 * But only _CDC and _RNDIS code can signalize
1337 * that network is working. So we signalize it
1340 dev->network_started = 1;
1341 debug("USB network up!\n");
1345 #ifdef CONFIG_USB_ETH_CDC
1347 case 0: /* control/master intf */
1351 usb_ep_disable(dev->status_ep);
1352 usb_ep_enable(dev->status_ep, dev->status);
1357 case 1: /* data intf */
1360 usb_ep_disable(dev->in_ep);
1361 usb_ep_disable(dev->out_ep);
1364 * CDC requires the data transfers not be done from
1365 * the default interface setting ... also, setting
1366 * the non-default interface resets filters etc.
1369 if (!cdc_active(dev))
1371 usb_ep_enable(dev->in_ep, dev->in);
1372 usb_ep_enable(dev->out_ep, dev->out);
1373 dev->cdc_filter = DEFAULT_FILTER;
1375 issue_start_status(dev);
1376 eth_start(dev, GFP_ATOMIC);
1383 * FIXME this is wrong, as is the assumption that
1384 * all non-PXA hardware talks real CDC ...
1386 debug("set_interface ignored!\n");
1387 #endif /* CONFIG_USB_ETH_CDC */
1391 case USB_REQ_GET_INTERFACE:
1392 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1396 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1399 /* for CDC, iff carrier is on, data interface is active. */
1400 if (rndis_active(dev) || wIndex != 1)
1401 *(u8 *)req->buf = 0;
1403 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1404 /* carrier always ok ...*/
1405 *(u8 *)req->buf = 1 ;
1407 value = min(wLength, (u16) 1);
1410 #ifdef CONFIG_USB_ETH_CDC
1411 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1413 * see 6.2.30: no data, wIndex = interface,
1414 * wValue = packet filter bitmap
1416 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1421 debug("packet filter %02x\n", wValue);
1422 dev->cdc_filter = wValue;
1428 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1429 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1430 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1431 * case USB_CDC_GET_ETHERNET_STATISTIC:
1434 #endif /* CONFIG_USB_ETH_CDC */
1436 #ifdef CONFIG_USB_ETH_RNDIS
1438 * RNDIS uses the CDC command encapsulation mechanism to implement
1439 * an RPC scheme, with much getting/setting of attributes by OID.
1441 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1442 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1443 || !rndis_active(dev)
1444 || wLength > USB_BUFSIZ
1446 || rndis_control_intf.bInterfaceNumber
1449 /* read the request, then process it */
1451 req->complete = rndis_command_complete;
1452 /* later, rndis_control_ack () sends a notification */
1455 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1456 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1457 == ctrl->bRequestType
1458 && rndis_active(dev)
1459 /* && wLength >= 0x0400 */
1461 && rndis_control_intf.bInterfaceNumber
1466 /* return the result */
1467 buf = rndis_get_next_response(dev->rndis_config, &n);
1469 memcpy(req->buf, buf, n);
1470 req->complete = rndis_response_complete;
1471 rndis_free_response(dev->rndis_config, buf);
1474 /* else stalls ... spec says to avoid that */
1480 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1481 ctrl->bRequestType, ctrl->bRequest,
1482 wValue, wIndex, wLength);
1485 /* respond with data transfer before status phase? */
1487 debug("respond with data transfer before status phase\n");
1488 req->length = value;
1489 req->zero = value < wLength
1490 && (value % gadget->ep0->maxpacket) == 0;
1491 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1493 debug("ep_queue --> %d\n", value);
1495 eth_setup_complete(gadget->ep0, req);
1499 /* host either stalls (value < 0) or reports success */
1503 /*-------------------------------------------------------------------------*/
1505 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1507 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1510 int retval = -ENOMEM;
1514 * Padding up to RX_EXTRA handles minor disagreements with host.
1515 * Normally we use the USB "terminate on short read" convention;
1516 * so allow up to (N*maxpacket), since that memory is normally
1517 * already allocated. Some hardware doesn't deal well with short
1518 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1519 * byte off the end (to force hardware errors on overflow).
1521 * RNDIS uses internal framing, and explicitly allows senders to
1522 * pad to end-of-packet. That's potentially nice for speed,
1523 * but means receivers can't recover synch on their own.
1526 debug("%s\n", __func__);
1530 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1531 size += dev->out_ep->maxpacket - 1;
1532 if (rndis_active(dev))
1533 size += sizeof(struct rndis_packet_msg_type);
1534 size -= size % dev->out_ep->maxpacket;
1537 * Some platforms perform better when IP packets are aligned,
1538 * but on at least one, checksumming fails otherwise. Note:
1539 * RNDIS headers involve variable numbers of LE32 values.
1542 req->buf = (u8 *)net_rx_packets[0];
1544 req->complete = rx_complete;
1546 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1549 pr_err("rx submit --> %d", retval);
1554 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1556 struct eth_dev *dev = ep->driver_data;
1558 debug("%s: status %d\n", __func__, req->status);
1559 switch (req->status) {
1560 /* normal completion */
1562 if (rndis_active(dev)) {
1563 /* we know MaxPacketsPerTransfer == 1 here */
1564 int length = rndis_rm_hdr(req->buf, req->actual);
1567 req->length -= length;
1568 req->actual -= length;
1570 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
1572 dev->stats.rx_errors++;
1573 dev->stats.rx_length_errors++;
1574 debug("rx length %d\n", req->length);
1578 dev->stats.rx_packets++;
1579 dev->stats.rx_bytes += req->length;
1582 /* software-driven interface shutdown */
1583 case -ECONNRESET: /* unlink */
1584 case -ESHUTDOWN: /* disconnect etc */
1585 /* for hardware automagic (such as pxa) */
1586 case -ECONNABORTED: /* endpoint reset */
1591 dev->stats.rx_over_errors++;
1594 dev->stats.rx_errors++;
1598 packet_received = 1;
1601 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1604 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1609 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1617 usb_ep_free_request(dev->in_ep, dev->tx_req);
1619 pr_err("can't alloc requests");
1623 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1625 struct eth_dev *dev = ep->driver_data;
1627 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1628 switch (req->status) {
1630 dev->stats.tx_errors++;
1631 debug("tx err %d\n", req->status);
1633 case -ECONNRESET: /* unlink */
1634 case -ESHUTDOWN: /* disconnect etc */
1637 dev->stats.tx_bytes += req->length;
1639 dev->stats.tx_packets++;
1644 static inline int eth_is_promisc(struct eth_dev *dev)
1646 /* no filters for the CDC subset; always promisc */
1647 if (subset_active(dev))
1649 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1653 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1655 struct eth_dev *dev = netdev_priv(net);
1656 int length = skb->len;
1658 struct usb_request *req = NULL;
1659 unsigned long flags;
1661 /* apply outgoing CDC or RNDIS filters */
1662 if (!eth_is_promisc (dev)) {
1663 u8 *dest = skb->data;
1665 if (is_multicast_ethaddr(dest)) {
1668 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1669 * SET_ETHERNET_MULTICAST_FILTERS requests
1671 if (is_broadcast_ethaddr(dest))
1672 type = USB_CDC_PACKET_TYPE_BROADCAST;
1674 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1675 if (!(dev->cdc_filter & type)) {
1676 dev_kfree_skb_any (skb);
1680 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1683 spin_lock_irqsave(&dev->req_lock, flags);
1685 * this freelist can be empty if an interrupt triggered disconnect()
1686 * and reconfigured the gadget (shutting down this queue) after the
1687 * network stack decided to xmit but before we got the spinlock.
1689 if (list_empty(&dev->tx_reqs)) {
1690 spin_unlock_irqrestore(&dev->req_lock, flags);
1694 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1695 list_del (&req->list);
1697 /* temporarily stop TX queue when the freelist empties */
1698 if (list_empty (&dev->tx_reqs))
1699 netif_stop_queue (net);
1700 spin_unlock_irqrestore(&dev->req_lock, flags);
1702 /* no buffer copies needed, unless the network stack did it
1703 * or the hardware can't use skb buffers.
1704 * or there's not enough space for any RNDIS headers we need
1706 if (rndis_active(dev)) {
1707 struct sk_buff *skb_rndis;
1709 skb_rndis = skb_realloc_headroom (skb,
1710 sizeof (struct rndis_packet_msg_type));
1714 dev_kfree_skb_any (skb);
1716 rndis_add_hdr (skb);
1719 req->buf = skb->data;
1721 req->complete = tx_complete;
1723 /* use zlp framing on tx for strict CDC-Ether conformance,
1724 * though any robust network rx path ignores extra padding.
1725 * and some hardware doesn't like to write zlps.
1728 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1731 req->length = length;
1733 /* throttle highspeed IRQ rate back slightly */
1734 if (gadget_is_dualspeed(dev->gadget))
1735 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1736 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1739 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1742 DEBUG (dev, "tx queue err %d\n", retval);
1745 net->trans_start = jiffies;
1746 atomic_inc (&dev->tx_qlen);
1751 dev->stats.tx_dropped++;
1752 dev_kfree_skb_any (skb);
1753 spin_lock_irqsave(&dev->req_lock, flags);
1754 if (list_empty (&dev->tx_reqs))
1755 netif_start_queue (net);
1756 list_add (&req->list, &dev->tx_reqs);
1757 spin_unlock_irqrestore(&dev->req_lock, flags);
1762 /*-------------------------------------------------------------------------*/
1765 static void eth_unbind(struct usb_gadget *gadget)
1767 struct eth_dev *dev = get_gadget_data(gadget);
1769 debug("%s...\n", __func__);
1770 rndis_deregister(dev->rndis_config);
1773 /* we've already been disconnected ... no i/o is active */
1775 usb_ep_free_request(gadget->ep0, dev->req);
1778 if (dev->stat_req) {
1779 usb_ep_free_request(dev->status_ep, dev->stat_req);
1780 dev->stat_req = NULL;
1784 usb_ep_free_request(dev->in_ep, dev->tx_req);
1789 usb_ep_free_request(dev->out_ep, dev->rx_req);
1793 /* unregister_netdev (dev->net);*/
1794 /* free_netdev(dev->net);*/
1797 set_gadget_data(gadget, NULL);
1800 static void eth_disconnect(struct usb_gadget *gadget)
1802 eth_reset_config(get_gadget_data(gadget));
1803 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1806 static void eth_suspend(struct usb_gadget *gadget)
1811 static void eth_resume(struct usb_gadget *gadget)
1816 /*-------------------------------------------------------------------------*/
1818 #ifdef CONFIG_USB_ETH_RNDIS
1821 * The interrupt endpoint is used in RNDIS to notify the host when messages
1822 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1823 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1824 * REMOTE_NDIS_KEEPALIVE_MSG.
1826 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1827 * normally just one notification will be queued.
1830 static void rndis_control_ack_complete(struct usb_ep *ep,
1831 struct usb_request *req)
1833 struct eth_dev *dev = ep->driver_data;
1835 debug("%s...\n", __func__);
1836 if (req->status || req->actual != req->length)
1837 debug("rndis control ack complete --> %d, %d/%d\n",
1838 req->status, req->actual, req->length);
1840 if (!dev->network_started) {
1841 if (rndis_get_state(dev->rndis_config)
1842 == RNDIS_DATA_INITIALIZED) {
1843 dev->network_started = 1;
1844 printf("USB RNDIS network up!\n");
1848 req->context = NULL;
1850 if (req != dev->stat_req)
1851 usb_ep_free_request(ep, req);
1854 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1856 #ifndef CONFIG_DM_ETH
1857 static int rndis_control_ack(struct eth_device *net)
1859 static int rndis_control_ack(struct udevice *net)
1862 struct ether_priv *priv = (struct ether_priv *)net->priv;
1863 struct eth_dev *dev = &priv->ethdev;
1865 struct usb_request *resp = dev->stat_req;
1867 /* in case RNDIS calls this after disconnect */
1869 debug("status ENODEV\n");
1873 /* in case queue length > 1 */
1874 if (resp->context) {
1875 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1878 resp->buf = rndis_resp_buf;
1882 * Send RNDIS RESPONSE_AVAILABLE notification;
1883 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1886 resp->complete = rndis_control_ack_complete;
1887 resp->context = dev;
1889 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1890 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1892 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1895 rndis_control_ack_complete(dev->status_ep, resp);
1903 #define rndis_control_ack NULL
1907 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1909 if (rndis_active(dev)) {
1910 rndis_set_param_medium(dev->rndis_config,
1912 BITRATE(dev->gadget)/100);
1913 rndis_signal_connect(dev->rndis_config);
1917 static int eth_stop(struct eth_dev *dev)
1919 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1921 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1924 if (rndis_active(dev)) {
1925 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1926 rndis_signal_disconnect(dev->rndis_config);
1928 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1929 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1931 while (get_timer(ts) < timeout)
1932 usb_gadget_handle_interrupts(0);
1935 rndis_uninit(dev->rndis_config);
1942 /*-------------------------------------------------------------------------*/
1944 static int is_eth_addr_valid(char *str)
1946 if (strlen(str) == 17) {
1951 /* see if it looks like an ethernet address */
1955 for (i = 0; i < 6; i++) {
1956 char term = (i == 5 ? '\0' : ':');
1958 ea[i] = simple_strtol(p, &q, 16);
1960 if ((q - p) != 2 || *q++ != term)
1966 /* Now check the contents. */
1967 return is_valid_ethaddr(ea);
1972 static u8 nibble(unsigned char c)
1974 if (likely(isdigit(c)))
1977 if (likely(isxdigit(c)))
1978 return 10 + c - 'A';
1982 static int get_ether_addr(const char *str, u8 *dev_addr)
1987 for (i = 0; i < 6; i++) {
1990 if ((*str == '.') || (*str == ':'))
1992 num = nibble(*str++) << 4;
1993 num |= (nibble(*str++));
1996 if (is_valid_ethaddr(dev_addr))
2002 static int eth_bind(struct usb_gadget *gadget)
2004 struct eth_dev *dev = &l_priv->ethdev;
2005 u8 cdc = 1, zlp = 1, rndis = 1;
2006 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2007 int status = -ENOMEM;
2010 #ifdef CONFIG_DM_ETH
2011 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2014 /* these flags are only ever cleared; compiler take note */
2015 #ifndef CONFIG_USB_ETH_CDC
2018 #ifndef CONFIG_USB_ETH_RNDIS
2022 * Because most host side USB stacks handle CDC Ethernet, that
2023 * standard protocol is _strongly_ preferred for interop purposes.
2024 * (By everyone except Microsoft.)
2026 if (gadget_is_pxa(gadget)) {
2027 /* pxa doesn't support altsettings */
2029 } else if (gadget_is_musbhdrc(gadget)) {
2030 /* reduce tx dma overhead by avoiding special cases */
2032 } else if (gadget_is_sh(gadget)) {
2033 /* sh doesn't support multiple interfaces or configs */
2036 } else if (gadget_is_sa1100(gadget)) {
2037 /* hardware can't write zlps */
2040 * sa1100 CAN do CDC, without status endpoint ... we use
2041 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2046 gcnum = usb_gadget_controller_number(gadget);
2048 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2051 * can't assume CDC works. don't want to default to
2052 * anything less functional on CDC-capable hardware,
2053 * so we fail in this case.
2055 pr_err("controller '%s' not recognized",
2061 * If there's an RNDIS configuration, that's what Windows wants to
2062 * be using ... so use these product IDs here and in the "linux.inf"
2063 * needed to install MSFT drivers. Current Linux kernels will use
2064 * the second configuration if it's CDC Ethernet, and need some help
2065 * to choose the right configuration otherwise.
2068 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2069 device_desc.idVendor =
2070 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2071 device_desc.idProduct =
2072 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2074 device_desc.idVendor =
2075 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2076 device_desc.idProduct =
2077 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2079 sprintf(product_desc, "RNDIS/%s", driver_desc);
2082 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2083 * drivers aren't widely available. (That may be improved by
2084 * supporting one submode of the "SAFE" variant of MDLM.)
2087 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2088 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2089 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2092 device_desc.idVendor =
2093 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2094 device_desc.idProduct =
2095 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2099 /* support optional vendor/distro customization */
2101 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2103 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2105 strlcpy(product_desc, iProduct, sizeof product_desc);
2106 if (iSerialNumber) {
2107 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2108 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2111 /* all we really need is bulk IN/OUT */
2112 usb_ep_autoconfig_reset(gadget);
2113 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2116 pr_err("can't autoconfigure on %s\n",
2120 in_ep->driver_data = in_ep; /* claim */
2122 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2125 out_ep->driver_data = out_ep; /* claim */
2127 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2129 * CDC Ethernet control interface doesn't require a status endpoint.
2130 * Since some hosts expect one, try to allocate one anyway.
2133 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2135 status_ep->driver_data = status_ep; /* claim */
2137 pr_err("can't run RNDIS on %s", gadget->name);
2139 #ifdef CONFIG_USB_ETH_CDC
2141 control_intf.bNumEndpoints = 0;
2142 /* FIXME remove endpoint from descriptor list */
2148 /* one config: cdc, else minimal subset */
2150 eth_config.bNumInterfaces = 1;
2151 eth_config.iConfiguration = STRING_SUBSET;
2154 * use functions to set these up, in case we're built to work
2155 * with multiple controllers and must override CDC Ethernet.
2157 fs_subset_descriptors();
2158 hs_subset_descriptors();
2161 usb_gadget_set_selfpowered(gadget);
2163 /* For now RNDIS is always a second config */
2165 device_desc.bNumConfigurations = 2;
2167 if (gadget_is_dualspeed(gadget)) {
2169 dev_qualifier.bNumConfigurations = 2;
2171 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2173 /* assumes ep0 uses the same value for both speeds ... */
2174 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2176 /* and that all endpoints are dual-speed */
2177 hs_source_desc.bEndpointAddress =
2178 fs_source_desc.bEndpointAddress;
2179 hs_sink_desc.bEndpointAddress =
2180 fs_sink_desc.bEndpointAddress;
2181 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2183 hs_status_desc.bEndpointAddress =
2184 fs_status_desc.bEndpointAddress;
2188 if (gadget_is_otg(gadget)) {
2189 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2190 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2191 eth_config.bMaxPower = 4;
2192 #ifdef CONFIG_USB_ETH_RNDIS
2193 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2194 rndis_config.bMaxPower = 4;
2199 /* network device setup */
2200 #ifndef CONFIG_DM_ETH
2201 dev->net = &l_priv->netdev;
2203 dev->net = l_priv->netdev;
2210 dev->out_ep = out_ep;
2211 dev->status_ep = status_ep;
2213 memset(tmp, 0, sizeof(tmp));
2215 * Module params for these addresses should come from ID proms.
2216 * The host side address is used with CDC and RNDIS, and commonly
2217 * ends up in a persistent config database. It's not clear if
2218 * host side code for the SAFE thing cares -- its original BLAN
2219 * thing didn't, Sharp never assigned those addresses on Zaurii.
2221 #ifndef CONFIG_DM_ETH
2222 get_ether_addr(dev_addr, dev->net->enetaddr);
2223 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2225 get_ether_addr(dev_addr, pdata->enetaddr);
2226 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2229 get_ether_addr(host_addr, dev->host_mac);
2231 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2232 dev->host_mac[0], dev->host_mac[1],
2233 dev->host_mac[2], dev->host_mac[3],
2234 dev->host_mac[4], dev->host_mac[5]);
2237 status = rndis_init();
2239 pr_err("can't init RNDIS, %d", status);
2245 * use PKTSIZE (or aligned... from u-boot) and set
2246 * wMaxSegmentSize accordingly
2248 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2250 /* preallocate control message data and buffer */
2251 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2254 dev->req->buf = control_req;
2255 dev->req->complete = eth_setup_complete;
2257 /* ... and maybe likewise for status transfer */
2258 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2259 if (dev->status_ep) {
2260 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2262 if (!dev->stat_req) {
2263 usb_ep_free_request(dev->status_ep, dev->req);
2267 dev->stat_req->buf = status_req;
2268 dev->stat_req->context = NULL;
2272 /* finish hookup to lower layer ... */
2273 dev->gadget = gadget;
2274 set_gadget_data(gadget, dev);
2275 gadget->ep0->driver_data = dev;
2278 * two kinds of host-initiated state changes:
2279 * - iff DATA transfer is active, carrier is "on"
2280 * - tx queueing enabled if open *and* carrier is "on"
2283 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2284 out_ep->name, in_ep->name,
2285 status_ep ? " STATUS " : "",
2286 status_ep ? status_ep->name : ""
2288 #ifndef CONFIG_DM_ETH
2289 printf("MAC %pM\n", dev->net->enetaddr);
2291 printf("MAC %pM\n", pdata->enetaddr);
2295 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2296 dev->host_mac[0], dev->host_mac[1],
2297 dev->host_mac[2], dev->host_mac[3],
2298 dev->host_mac[4], dev->host_mac[5]);
2303 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2305 dev->rndis_config = rndis_register(rndis_control_ack);
2306 if (dev->rndis_config < 0) {
2309 debug("RNDIS setup failed\n");
2314 /* these set up a lot of the OIDs that RNDIS needs */
2315 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2316 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2317 &dev->stats, &dev->cdc_filter))
2319 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2322 if (rndis_set_param_medium(dev->rndis_config,
2323 NDIS_MEDIUM_802_3, 0))
2325 printf("RNDIS ready\n");
2330 pr_err("%s failed, status = %d", __func__, status);
2335 /*-------------------------------------------------------------------------*/
2336 static void _usb_eth_halt(struct ether_priv *priv);
2338 static int _usb_eth_init(struct ether_priv *priv)
2340 struct eth_dev *dev = &priv->ethdev;
2341 struct usb_gadget *gadget;
2344 unsigned long timeout = USB_CONNECT_TIMEOUT;
2346 ret = usb_gadget_initialize(0);
2350 /* Configure default mac-addresses for the USB ethernet device */
2351 #ifdef CONFIG_USBNET_DEV_ADDR
2352 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2354 #ifdef CONFIG_USBNET_HOST_ADDR
2355 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2357 /* Check if the user overruled the MAC addresses */
2358 if (env_get("usbnet_devaddr"))
2359 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2362 if (env_get("usbnet_hostaddr"))
2363 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2366 if (!is_eth_addr_valid(dev_addr)) {
2367 pr_err("Need valid 'usbnet_devaddr' to be set");
2370 if (!is_eth_addr_valid(host_addr)) {
2371 pr_err("Need valid 'usbnet_hostaddr' to be set");
2375 priv->eth_driver.speed = DEVSPEED;
2376 priv->eth_driver.bind = eth_bind;
2377 priv->eth_driver.unbind = eth_unbind;
2378 priv->eth_driver.setup = eth_setup;
2379 priv->eth_driver.reset = eth_disconnect;
2380 priv->eth_driver.disconnect = eth_disconnect;
2381 priv->eth_driver.suspend = eth_suspend;
2382 priv->eth_driver.resume = eth_resume;
2383 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2386 dev->network_started = 0;
2388 packet_received = 0;
2391 gadget = dev->gadget;
2392 usb_gadget_connect(gadget);
2394 if (env_get("cdc_connect_timeout"))
2395 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
2396 NULL, 10) * CONFIG_SYS_HZ;
2398 while (!dev->network_started) {
2399 /* Handle control-c and timeouts */
2400 if (ctrlc() || (get_timer(ts) > timeout)) {
2401 pr_err("The remote end did not respond in time.");
2404 usb_gadget_handle_interrupts(0);
2407 packet_received = 0;
2408 rx_submit(dev, dev->rx_req, 0);
2411 _usb_eth_halt(priv);
2415 static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
2418 void *rndis_pkt = NULL;
2419 struct eth_dev *dev = &priv->ethdev;
2420 struct usb_request *req = dev->tx_req;
2422 unsigned long timeout = USB_CONNECT_TIMEOUT;
2424 debug("%s:...\n", __func__);
2426 /* new buffer is needed to include RNDIS header */
2427 if (rndis_active(dev)) {
2428 rndis_pkt = malloc(length +
2429 sizeof(struct rndis_packet_msg_type));
2431 pr_err("No memory to alloc RNDIS packet");
2434 rndis_add_hdr(rndis_pkt, length);
2435 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2438 length += sizeof(struct rndis_packet_msg_type);
2441 req->context = NULL;
2442 req->complete = tx_complete;
2445 * use zlp framing on tx for strict CDC-Ether conformance,
2446 * though any robust network rx path ignores extra padding.
2447 * and some hardware doesn't like to write zlps.
2450 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2453 req->length = length;
2455 /* throttle highspeed IRQ rate back slightly */
2456 if (gadget_is_dualspeed(dev->gadget))
2457 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2458 ? ((dev->tx_qlen % qmult) != 0) : 0;
2464 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2467 debug("%s: packet queued\n", __func__);
2468 while (!packet_sent) {
2469 if (get_timer(ts) > timeout) {
2470 printf("timeout sending packets to usb ethernet\n");
2473 usb_gadget_handle_interrupts(0);
2480 dev->stats.tx_dropped++;
2484 static int _usb_eth_recv(struct ether_priv *priv)
2486 usb_gadget_handle_interrupts(0);
2491 static void _usb_eth_halt(struct ether_priv *priv)
2493 struct eth_dev *dev = &priv->ethdev;
2495 /* If the gadget not registered, simple return */
2500 * Some USB controllers may need additional deinitialization here
2501 * before dropping pull-up (also due to hardware issues).
2502 * For example: unhandled interrupt with status stage started may
2503 * bring the controller to fully broken state (until board reset).
2504 * There are some variants to debug and fix such cases:
2505 * 1) In the case of RNDIS connection eth_stop can perform additional
2506 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2507 * 2) 'pullup' callback in your UDC driver can be improved to perform
2508 * this deinitialization.
2512 usb_gadget_disconnect(dev->gadget);
2514 /* Clear pending interrupt */
2515 if (dev->network_started) {
2516 usb_gadget_handle_interrupts(0);
2517 dev->network_started = 0;
2520 usb_gadget_unregister_driver(&priv->eth_driver);
2521 usb_gadget_release(0);
2524 #ifndef CONFIG_DM_ETH
2525 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2527 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2529 return _usb_eth_init(priv);
2532 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2534 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2536 return _usb_eth_send(priv, packet, length);
2539 static int usb_eth_recv(struct eth_device *netdev)
2541 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2542 struct eth_dev *dev = &priv->ethdev;
2545 ret = _usb_eth_recv(priv);
2547 pr_err("error packet receive\n");
2551 if (!packet_received)
2555 net_process_received_packet(net_rx_packets[0],
2556 dev->rx_req->length);
2558 pr_err("dev->rx_req invalid");
2560 packet_received = 0;
2561 rx_submit(dev, dev->rx_req, 0);
2566 void usb_eth_halt(struct eth_device *netdev)
2568 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2570 _usb_eth_halt(priv);
2573 int usb_eth_initialize(bd_t *bi)
2575 struct eth_device *netdev = &l_priv->netdev;
2577 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2579 netdev->init = usb_eth_init;
2580 netdev->send = usb_eth_send;
2581 netdev->recv = usb_eth_recv;
2582 netdev->halt = usb_eth_halt;
2583 netdev->priv = l_priv;
2585 eth_register(netdev);
2589 static int usb_eth_start(struct udevice *dev)
2591 struct ether_priv *priv = dev_get_priv(dev);
2593 return _usb_eth_init(priv);
2596 static int usb_eth_send(struct udevice *dev, void *packet, int length)
2598 struct ether_priv *priv = dev_get_priv(dev);
2600 return _usb_eth_send(priv, packet, length);
2603 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2605 struct ether_priv *priv = dev_get_priv(dev);
2606 struct eth_dev *ethdev = &priv->ethdev;
2609 ret = _usb_eth_recv(priv);
2611 pr_err("error packet receive\n");
2615 if (packet_received) {
2616 if (ethdev->rx_req) {
2617 *packetp = (uchar *)net_rx_packets[0];
2618 return ethdev->rx_req->length;
2620 pr_err("dev->rx_req invalid");
2628 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2631 struct ether_priv *priv = dev_get_priv(dev);
2632 struct eth_dev *ethdev = &priv->ethdev;
2634 packet_received = 0;
2636 return rx_submit(ethdev, ethdev->rx_req, 0);
2639 static void usb_eth_stop(struct udevice *dev)
2641 struct ether_priv *priv = dev_get_priv(dev);
2643 _usb_eth_halt(priv);
2646 static int usb_eth_probe(struct udevice *dev)
2648 struct ether_priv *priv = dev_get_priv(dev);
2649 struct eth_pdata *pdata = dev_get_platdata(dev);
2654 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2655 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2660 static const struct eth_ops usb_eth_ops = {
2661 .start = usb_eth_start,
2662 .send = usb_eth_send,
2663 .recv = usb_eth_recv,
2664 .free_pkt = usb_eth_free_pkt,
2665 .stop = usb_eth_stop,
2668 int usb_ether_init(void)
2670 struct udevice *dev;
2671 struct udevice *usb_dev;
2674 ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2675 if (!usb_dev || ret) {
2676 pr_err("No USB device found\n");
2680 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2682 pr_err("usb - not able to bind usb_ether device\n");
2689 U_BOOT_DRIVER(eth_usb) = {
2690 .name = "usb_ether",
2692 .probe = usb_eth_probe,
2693 .ops = &usb_eth_ops,
2694 .priv_auto_alloc_size = sizeof(struct ether_priv),
2695 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2696 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2698 #endif /* CONFIG_DM_ETH */