2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/errno.h>
13 #include <linux/netdevice.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/cdc.h>
16 #include <linux/usb/gadget.h>
19 #include <linux/ctype.h>
21 #include "gadget_chips.h"
24 #define USB_NET_NAME "usb_ether"
27 extern struct platform_data brd;
30 unsigned packet_received, packet_sent;
33 * Ethernet gadget driver -- with CDC and non-CDC options
34 * Builds on hardware support for a full duplex link.
36 * CDC Ethernet is the standard USB solution for sending Ethernet frames
37 * using USB. Real hardware tends to use the same framing protocol but look
38 * different for control features. This driver strongly prefers to use
39 * this USB-IF standard as its open-systems interoperability solution;
40 * most host side USB stacks (except from Microsoft) support it.
42 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
43 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
44 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
46 * There's some hardware that can't talk CDC ECM. We make that hardware
47 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
48 * link-level setup only requires activating the configuration. Only the
49 * endpoint descriptors, and product/vendor IDs, are relevant; no control
50 * operations are available. Linux supports it, but other host operating
51 * systems may not. (This is a subset of CDC Ethernet.)
53 * It turns out that if you add a few descriptors to that "CDC Subset",
54 * (Windows) host side drivers from MCCI can treat it as one submode of
55 * a proprietary scheme called "SAFE" ... without needing to know about
56 * specific product/vendor IDs. So we do that, making it easier to use
57 * those MS-Windows drivers. Those added descriptors make it resemble a
58 * CDC MDLM device, but they don't change device behavior at all. (See
59 * MCCI Engineering report 950198 "SAFE Networking Functions".)
61 * A third option is also in use. Rather than CDC Ethernet, or something
62 * simpler, Microsoft pushes their own approach: RNDIS. The published
63 * RNDIS specs are ambiguous and appear to be incomplete, and are also
64 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
66 #define ETH_ALEN 6 /* Octets in one ethernet addr */
67 #define ETH_HLEN 14 /* Total octets in header. */
68 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
69 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
70 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
71 #define ETH_FCS_LEN 4 /* Octets in the FCS */
73 #define DRIVER_DESC "Ethernet Gadget"
74 /* Based on linux 2.6.27 version */
75 #define DRIVER_VERSION "May Day 2005"
77 static const char shortname[] = "ether";
78 static const char driver_desc[] = DRIVER_DESC;
80 #define RX_EXTRA 20 /* guard against rx overflows */
82 #ifndef CONFIG_USB_ETH_RNDIS
83 #define rndis_uninit(x) do {} while (0)
84 #define rndis_deregister(c) do {} while (0)
85 #define rndis_exit() do {} while (0)
88 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
89 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
90 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
91 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
92 |USB_CDC_PACKET_TYPE_DIRECTED)
94 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
96 /*-------------------------------------------------------------------------*/
99 struct usb_gadget *gadget;
100 struct usb_request *req; /* for control responses */
101 struct usb_request *stat_req; /* for cdc & rndis status */
104 struct usb_ep *in_ep, *out_ep, *status_ep;
105 const struct usb_endpoint_descriptor
108 struct usb_request *tx_req, *rx_req;
110 struct eth_device *net;
111 struct net_device_stats stats;
112 unsigned int tx_qlen;
117 unsigned suspended:1;
118 unsigned network_started:1;
122 #define WORK_RX_MEMORY 0
124 u8 host_mac[ETH_ALEN];
128 * This version autoconfigures as much as possible at run-time.
130 * It also ASSUMES a self-powered device, without remote wakeup,
131 * although remote wakeup support would make sense.
134 /*-------------------------------------------------------------------------*/
135 static struct eth_dev l_ethdev;
136 static struct eth_device l_netdev;
137 static struct usb_gadget_driver eth_driver;
139 /*-------------------------------------------------------------------------*/
141 /* "main" config is either CDC, or its simple subset */
142 static inline int is_cdc(struct eth_dev *dev)
144 #if !defined(CONFIG_USB_ETH_SUBSET)
145 return 1; /* only cdc possible */
146 #elif !defined(CONFIG_USB_ETH_CDC)
147 return 0; /* only subset possible */
149 return dev->cdc; /* depends on what hardware we found */
153 /* "secondary" RNDIS config may sometimes be activated */
154 static inline int rndis_active(struct eth_dev *dev)
156 #ifdef CONFIG_USB_ETH_RNDIS
163 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
164 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
166 #define DEFAULT_QLEN 2 /* double buffering by default */
168 /* peak bulk transfer bits-per-second */
169 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
170 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
172 #ifdef CONFIG_USB_GADGET_DUALSPEED
173 #define DEVSPEED USB_SPEED_HIGH
175 #ifdef CONFIG_USB_ETH_QMULT
176 #define qmult CONFIG_USB_ETH_QMULT
181 /* for dual-speed hardware, use deeper queues at highspeed */
182 #define qlen(gadget) \
183 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
185 static inline int BITRATE(struct usb_gadget *g)
187 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
190 #else /* full speed (low speed doesn't do bulk) */
194 #define DEVSPEED USB_SPEED_FULL
196 #define qlen(gadget) DEFAULT_QLEN
198 static inline int BITRATE(struct usb_gadget *g)
204 /*-------------------------------------------------------------------------*/
207 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
208 * Instead: allocate your own, using normal USB-IF procedures.
212 * Thanks to NetChip Technologies for donating this product ID.
213 * It's for devices with only CDC Ethernet configurations.
215 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
216 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
219 * For hardware that can't talk CDC, we use the same vendor ID that
220 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
221 * with pxa250. We're protocol-compatible, if the host-side drivers
222 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
223 * drivers that need to hard-wire endpoint numbers have a hook.
225 * The protocol is a minimal subset of CDC Ether, which works on any bulk
226 * hardware that's not deeply broken ... even on hardware that can't talk
227 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
228 * doesn't handle control-OUT).
230 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
231 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
234 * For hardware that can talk RNDIS and either of the above protocols,
235 * use this ID ... the windows INF files will know it. Unless it's
236 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
237 * the non-RNDIS configuration.
239 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
240 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
243 * Some systems will want different product identifers published in the
244 * device descriptor, either numbers or strings or both. These string
245 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
249 * Emulating them in eth_bind:
250 * static ushort idVendor;
251 * static ushort idProduct;
254 #if defined(CONFIG_USBNET_MANUFACTURER)
255 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
257 static char *iManufacturer = "U-boot";
260 /* These probably need to be configurable. */
261 static ushort bcdDevice;
262 static char *iProduct;
263 static char *iSerialNumber;
265 static char dev_addr[18];
267 static char host_addr[18];
270 /*-------------------------------------------------------------------------*/
273 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
274 * ep0 implementation: descriptors, config management, setup().
275 * also optional class-specific notification interrupt transfer.
279 * DESCRIPTORS ... most are static, but strings and (full) configuration
280 * descriptors are built on demand. For now we do either full CDC, or
281 * our simple subset, with RNDIS as an optional second configuration.
283 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
284 * the class descriptors match a modem (they're ignored; it's really just
285 * Ethernet functionality), they don't need the NOP altsetting, and the
286 * status transfer endpoint isn't optional.
289 #define STRING_MANUFACTURER 1
290 #define STRING_PRODUCT 2
291 #define STRING_ETHADDR 3
292 #define STRING_DATA 4
293 #define STRING_CONTROL 5
294 #define STRING_RNDIS_CONTROL 6
296 #define STRING_SUBSET 8
297 #define STRING_RNDIS 9
298 #define STRING_SERIALNUMBER 10
300 /* holds our biggest descriptor (or RNDIS response) */
301 #define USB_BUFSIZ 256
304 * This device advertises one configuration, eth_config, unless RNDIS
305 * is enabled (rndis_config) on hardware supporting at least two configs.
307 * NOTE: Controllers like superh_udc should probably be able to use
308 * an RNDIS-only configuration.
310 * FIXME define some higher-powered configurations to make it easier
311 * to recharge batteries ...
314 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
315 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
317 static struct usb_device_descriptor
319 .bLength = sizeof device_desc,
320 .bDescriptorType = USB_DT_DEVICE,
322 .bcdUSB = __constant_cpu_to_le16(0x0200),
324 .bDeviceClass = USB_CLASS_COMM,
325 .bDeviceSubClass = 0,
326 .bDeviceProtocol = 0,
328 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
329 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
330 .iManufacturer = STRING_MANUFACTURER,
331 .iProduct = STRING_PRODUCT,
332 .bNumConfigurations = 1,
335 static struct usb_otg_descriptor
337 .bLength = sizeof otg_descriptor,
338 .bDescriptorType = USB_DT_OTG,
340 .bmAttributes = USB_OTG_SRP,
343 static struct usb_config_descriptor
345 .bLength = sizeof eth_config,
346 .bDescriptorType = USB_DT_CONFIG,
348 /* compute wTotalLength on the fly */
350 .bConfigurationValue = DEV_CONFIG_VALUE,
351 .iConfiguration = STRING_CDC,
352 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
356 #ifdef CONFIG_USB_ETH_RNDIS
357 static struct usb_config_descriptor
359 .bLength = sizeof rndis_config,
360 .bDescriptorType = USB_DT_CONFIG,
362 /* compute wTotalLength on the fly */
364 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
365 .iConfiguration = STRING_RNDIS,
366 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
372 * Compared to the simple CDC subset, the full CDC Ethernet model adds
373 * three class descriptors, two interface descriptors, optional status
374 * endpoint. Both have a "data" interface and two bulk endpoints.
375 * There are also differences in how control requests are handled.
377 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
378 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
379 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
380 * work around those bugs) are unlikely to ever come from MSFT, you may
381 * wish to avoid using RNDIS.
383 * MCCI offers an alternative to RNDIS if you need to connect to Windows
384 * but have hardware that can't support CDC Ethernet. We add descriptors
385 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
386 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
387 * get those drivers from MCCI, or bundled with various products.
390 #ifdef CONFIG_USB_ETH_CDC
391 static struct usb_interface_descriptor
393 .bLength = sizeof control_intf,
394 .bDescriptorType = USB_DT_INTERFACE,
396 .bInterfaceNumber = 0,
397 /* status endpoint is optional; this may be patched later */
399 .bInterfaceClass = USB_CLASS_COMM,
400 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
401 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
402 .iInterface = STRING_CONTROL,
406 #ifdef CONFIG_USB_ETH_RNDIS
407 static const struct usb_interface_descriptor
408 rndis_control_intf = {
409 .bLength = sizeof rndis_control_intf,
410 .bDescriptorType = USB_DT_INTERFACE,
412 .bInterfaceNumber = 0,
414 .bInterfaceClass = USB_CLASS_COMM,
415 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
416 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
417 .iInterface = STRING_RNDIS_CONTROL,
421 static const struct usb_cdc_header_desc header_desc = {
422 .bLength = sizeof header_desc,
423 .bDescriptorType = USB_DT_CS_INTERFACE,
424 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
426 .bcdCDC = __constant_cpu_to_le16(0x0110),
429 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
431 static const struct usb_cdc_union_desc union_desc = {
432 .bLength = sizeof union_desc,
433 .bDescriptorType = USB_DT_CS_INTERFACE,
434 .bDescriptorSubType = USB_CDC_UNION_TYPE,
436 .bMasterInterface0 = 0, /* index of control interface */
437 .bSlaveInterface0 = 1, /* index of DATA interface */
440 #endif /* CDC || RNDIS */
442 #ifdef CONFIG_USB_ETH_RNDIS
444 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
445 .bLength = sizeof call_mgmt_descriptor,
446 .bDescriptorType = USB_DT_CS_INTERFACE,
447 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
449 .bmCapabilities = 0x00,
450 .bDataInterface = 0x01,
453 static const struct usb_cdc_acm_descriptor acm_descriptor = {
454 .bLength = sizeof acm_descriptor,
455 .bDescriptorType = USB_DT_CS_INTERFACE,
456 .bDescriptorSubType = USB_CDC_ACM_TYPE,
458 .bmCapabilities = 0x00,
463 #ifndef CONFIG_USB_ETH_CDC
466 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
467 * ways: data endpoints live in the control interface, there's no data
468 * interface, and it's not used to talk to a cell phone radio.
471 static const struct usb_cdc_mdlm_desc mdlm_desc = {
472 .bLength = sizeof mdlm_desc,
473 .bDescriptorType = USB_DT_CS_INTERFACE,
474 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
476 .bcdVersion = __constant_cpu_to_le16(0x0100),
478 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
479 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
484 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
485 * can't really use its struct. All we do here is say that we're using
486 * the submode of "SAFE" which directly matches the CDC Subset.
488 static const u8 mdlm_detail_desc[] = {
491 USB_CDC_MDLM_DETAIL_TYPE,
494 0, /* network control capabilities (none) */
495 0, /* network data capabilities ("raw" encapsulation) */
500 static const struct usb_cdc_ether_desc ether_desc = {
501 .bLength = sizeof(ether_desc),
502 .bDescriptorType = USB_DT_CS_INTERFACE,
503 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
505 /* this descriptor actually adds value, surprise! */
506 .iMACAddress = STRING_ETHADDR,
507 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
508 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
509 .wNumberMCFilters = __constant_cpu_to_le16(0),
510 .bNumberPowerFilters = 0,
513 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
516 * include the status endpoint if we can, even where it's optional.
517 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
518 * packet, to simplify cancellation; and a big transfer interval, to
519 * waste less bandwidth.
521 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
522 * if they ignore the connect/disconnect notifications that real aether
523 * can provide. more advanced cdc configurations might want to support
524 * encapsulated commands (vendor-specific, using control-OUT).
526 * RNDIS requires the status endpoint, since it uses that encapsulation
527 * mechanism for its funky RPC scheme.
530 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
531 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
533 static struct usb_endpoint_descriptor
535 .bLength = USB_DT_ENDPOINT_SIZE,
536 .bDescriptorType = USB_DT_ENDPOINT,
538 .bEndpointAddress = USB_DIR_IN,
539 .bmAttributes = USB_ENDPOINT_XFER_INT,
540 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
541 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
545 #ifdef CONFIG_USB_ETH_CDC
547 /* the default data interface has no endpoints ... */
549 static const struct usb_interface_descriptor
551 .bLength = sizeof data_nop_intf,
552 .bDescriptorType = USB_DT_INTERFACE,
554 .bInterfaceNumber = 1,
555 .bAlternateSetting = 0,
557 .bInterfaceClass = USB_CLASS_CDC_DATA,
558 .bInterfaceSubClass = 0,
559 .bInterfaceProtocol = 0,
562 /* ... but the "real" data interface has two bulk endpoints */
564 static const struct usb_interface_descriptor
566 .bLength = sizeof data_intf,
567 .bDescriptorType = USB_DT_INTERFACE,
569 .bInterfaceNumber = 1,
570 .bAlternateSetting = 1,
572 .bInterfaceClass = USB_CLASS_CDC_DATA,
573 .bInterfaceSubClass = 0,
574 .bInterfaceProtocol = 0,
575 .iInterface = STRING_DATA,
580 #ifdef CONFIG_USB_ETH_RNDIS
582 /* RNDIS doesn't activate by changing to the "real" altsetting */
584 static const struct usb_interface_descriptor
586 .bLength = sizeof rndis_data_intf,
587 .bDescriptorType = USB_DT_INTERFACE,
589 .bInterfaceNumber = 1,
590 .bAlternateSetting = 0,
592 .bInterfaceClass = USB_CLASS_CDC_DATA,
593 .bInterfaceSubClass = 0,
594 .bInterfaceProtocol = 0,
595 .iInterface = STRING_DATA,
600 #ifdef CONFIG_USB_ETH_SUBSET
603 * "Simple" CDC-subset option is a simple vendor-neutral model that most
604 * full speed controllers can handle: one interface, two bulk endpoints.
606 * To assist host side drivers, we fancy it up a bit, and add descriptors
607 * so some host side drivers will understand it as a "SAFE" variant.
610 static const struct usb_interface_descriptor
612 .bLength = sizeof subset_data_intf,
613 .bDescriptorType = USB_DT_INTERFACE,
615 .bInterfaceNumber = 0,
616 .bAlternateSetting = 0,
618 .bInterfaceClass = USB_CLASS_COMM,
619 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
620 .bInterfaceProtocol = 0,
621 .iInterface = STRING_DATA,
626 static struct usb_endpoint_descriptor
628 .bLength = USB_DT_ENDPOINT_SIZE,
629 .bDescriptorType = USB_DT_ENDPOINT,
631 .bEndpointAddress = USB_DIR_IN,
632 .bmAttributes = USB_ENDPOINT_XFER_BULK,
633 .wMaxPacketSize = __constant_cpu_to_le16(64),
636 static struct usb_endpoint_descriptor
638 .bLength = USB_DT_ENDPOINT_SIZE,
639 .bDescriptorType = USB_DT_ENDPOINT,
641 .bEndpointAddress = USB_DIR_OUT,
642 .bmAttributes = USB_ENDPOINT_XFER_BULK,
643 .wMaxPacketSize = __constant_cpu_to_le16(64),
646 static const struct usb_descriptor_header *fs_eth_function[11] = {
647 (struct usb_descriptor_header *) &otg_descriptor,
648 #ifdef CONFIG_USB_ETH_CDC
649 /* "cdc" mode descriptors */
650 (struct usb_descriptor_header *) &control_intf,
651 (struct usb_descriptor_header *) &header_desc,
652 (struct usb_descriptor_header *) &union_desc,
653 (struct usb_descriptor_header *) ðer_desc,
654 /* NOTE: status endpoint may need to be removed */
655 (struct usb_descriptor_header *) &fs_status_desc,
656 /* data interface, with altsetting */
657 (struct usb_descriptor_header *) &data_nop_intf,
658 (struct usb_descriptor_header *) &data_intf,
659 (struct usb_descriptor_header *) &fs_source_desc,
660 (struct usb_descriptor_header *) &fs_sink_desc,
662 #endif /* CONFIG_USB_ETH_CDC */
665 static inline void fs_subset_descriptors(void)
667 #ifdef CONFIG_USB_ETH_SUBSET
668 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
669 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
670 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
671 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
672 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
673 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
674 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
675 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
676 fs_eth_function[8] = NULL;
678 fs_eth_function[1] = NULL;
682 #ifdef CONFIG_USB_ETH_RNDIS
683 static const struct usb_descriptor_header *fs_rndis_function[] = {
684 (struct usb_descriptor_header *) &otg_descriptor,
685 /* control interface matches ACM, not Ethernet */
686 (struct usb_descriptor_header *) &rndis_control_intf,
687 (struct usb_descriptor_header *) &header_desc,
688 (struct usb_descriptor_header *) &call_mgmt_descriptor,
689 (struct usb_descriptor_header *) &acm_descriptor,
690 (struct usb_descriptor_header *) &union_desc,
691 (struct usb_descriptor_header *) &fs_status_desc,
692 /* data interface has no altsetting */
693 (struct usb_descriptor_header *) &rndis_data_intf,
694 (struct usb_descriptor_header *) &fs_source_desc,
695 (struct usb_descriptor_header *) &fs_sink_desc,
701 * usb 2.0 devices need to expose both high speed and full speed
702 * descriptors, unless they only run at full speed.
705 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
706 static struct usb_endpoint_descriptor
708 .bLength = USB_DT_ENDPOINT_SIZE,
709 .bDescriptorType = USB_DT_ENDPOINT,
711 .bmAttributes = USB_ENDPOINT_XFER_INT,
712 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
713 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
715 #endif /* CONFIG_USB_ETH_CDC */
717 static struct usb_endpoint_descriptor
719 .bLength = USB_DT_ENDPOINT_SIZE,
720 .bDescriptorType = USB_DT_ENDPOINT,
722 .bmAttributes = USB_ENDPOINT_XFER_BULK,
723 .wMaxPacketSize = __constant_cpu_to_le16(512),
726 static struct usb_endpoint_descriptor
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
731 .bmAttributes = USB_ENDPOINT_XFER_BULK,
732 .wMaxPacketSize = __constant_cpu_to_le16(512),
735 static struct usb_qualifier_descriptor
737 .bLength = sizeof dev_qualifier,
738 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
740 .bcdUSB = __constant_cpu_to_le16(0x0200),
741 .bDeviceClass = USB_CLASS_COMM,
743 .bNumConfigurations = 1,
746 static const struct usb_descriptor_header *hs_eth_function[11] = {
747 (struct usb_descriptor_header *) &otg_descriptor,
748 #ifdef CONFIG_USB_ETH_CDC
749 /* "cdc" mode descriptors */
750 (struct usb_descriptor_header *) &control_intf,
751 (struct usb_descriptor_header *) &header_desc,
752 (struct usb_descriptor_header *) &union_desc,
753 (struct usb_descriptor_header *) ðer_desc,
754 /* NOTE: status endpoint may need to be removed */
755 (struct usb_descriptor_header *) &hs_status_desc,
756 /* data interface, with altsetting */
757 (struct usb_descriptor_header *) &data_nop_intf,
758 (struct usb_descriptor_header *) &data_intf,
759 (struct usb_descriptor_header *) &hs_source_desc,
760 (struct usb_descriptor_header *) &hs_sink_desc,
762 #endif /* CONFIG_USB_ETH_CDC */
765 static inline void hs_subset_descriptors(void)
767 #ifdef CONFIG_USB_ETH_SUBSET
768 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
769 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
770 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
771 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
772 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
773 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
774 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
775 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
776 hs_eth_function[8] = NULL;
778 hs_eth_function[1] = NULL;
782 #ifdef CONFIG_USB_ETH_RNDIS
783 static const struct usb_descriptor_header *hs_rndis_function[] = {
784 (struct usb_descriptor_header *) &otg_descriptor,
785 /* control interface matches ACM, not Ethernet */
786 (struct usb_descriptor_header *) &rndis_control_intf,
787 (struct usb_descriptor_header *) &header_desc,
788 (struct usb_descriptor_header *) &call_mgmt_descriptor,
789 (struct usb_descriptor_header *) &acm_descriptor,
790 (struct usb_descriptor_header *) &union_desc,
791 (struct usb_descriptor_header *) &hs_status_desc,
792 /* data interface has no altsetting */
793 (struct usb_descriptor_header *) &rndis_data_intf,
794 (struct usb_descriptor_header *) &hs_source_desc,
795 (struct usb_descriptor_header *) &hs_sink_desc,
801 /* maxpacket and other transfer characteristics vary by speed. */
802 static inline struct usb_endpoint_descriptor *
803 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
804 struct usb_endpoint_descriptor *fs)
806 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
811 /*-------------------------------------------------------------------------*/
813 /* descriptors that are built on-demand */
815 static char manufacturer[50];
816 static char product_desc[40] = DRIVER_DESC;
817 static char serial_number[20];
819 /* address that the host will use ... usually assigned at random */
820 static char ethaddr[2 * ETH_ALEN + 1];
822 /* static strings, in UTF-8 */
823 static struct usb_string strings[] = {
824 { STRING_MANUFACTURER, manufacturer, },
825 { STRING_PRODUCT, product_desc, },
826 { STRING_SERIALNUMBER, serial_number, },
827 { STRING_DATA, "Ethernet Data", },
828 { STRING_ETHADDR, ethaddr, },
829 #ifdef CONFIG_USB_ETH_CDC
830 { STRING_CDC, "CDC Ethernet", },
831 { STRING_CONTROL, "CDC Communications Control", },
833 #ifdef CONFIG_USB_ETH_SUBSET
834 { STRING_SUBSET, "CDC Ethernet Subset", },
836 #ifdef CONFIG_USB_ETH_RNDIS
837 { STRING_RNDIS, "RNDIS", },
838 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
840 { } /* end of list */
843 static struct usb_gadget_strings stringtab = {
844 .language = 0x0409, /* en-us */
848 /*============================================================================*/
849 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
851 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
852 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
855 /*============================================================================*/
858 * one config, two interfaces: control, data.
859 * complications: class descriptors, and an altsetting.
862 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
865 const struct usb_config_descriptor *config;
866 const struct usb_descriptor_header **function;
869 if (gadget_is_dualspeed(g)) {
870 hs = (g->speed == USB_SPEED_HIGH);
871 if (type == USB_DT_OTHER_SPEED_CONFIG)
874 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
876 if (index >= device_desc.bNumConfigurations)
879 #ifdef CONFIG_USB_ETH_RNDIS
881 * list the RNDIS config first, to make Microsoft's drivers
882 * happy. DOCSIS 1.0 needs this too.
884 if (device_desc.bNumConfigurations == 2 && index == 0) {
885 config = &rndis_config;
886 function = which_fn(rndis);
890 config = ð_config;
891 function = which_fn(eth);
894 /* for now, don't advertise srp-only devices */
898 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
901 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
905 /*-------------------------------------------------------------------------*/
907 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
908 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
911 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
914 struct usb_gadget *gadget = dev->gadget;
916 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
917 /* status endpoint used for RNDIS and (optionally) CDC */
918 if (!subset_active(dev) && dev->status_ep) {
919 dev->status = ep_desc(gadget, &hs_status_desc,
921 dev->status_ep->driver_data = dev;
923 result = usb_ep_enable(dev->status_ep, dev->status);
925 debug("enable %s --> %d\n",
926 dev->status_ep->name, result);
932 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
933 dev->in_ep->driver_data = dev;
935 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
936 dev->out_ep->driver_data = dev;
939 * With CDC, the host isn't allowed to use these two data
940 * endpoints in the default altsetting for the interface.
941 * so we don't activate them yet. Reset from SET_INTERFACE.
943 * Strictly speaking RNDIS should work the same: activation is
944 * a side effect of setting a packet filter. Deactivation is
945 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
947 if (!cdc_active(dev)) {
948 result = usb_ep_enable(dev->in_ep, dev->in);
950 debug("enable %s --> %d\n",
951 dev->in_ep->name, result);
955 result = usb_ep_enable(dev->out_ep, dev->out);
957 debug("enable %s --> %d\n",
958 dev->out_ep->name, result);
965 result = alloc_requests(dev, qlen(gadget), gfp_flags);
967 /* on error, disable any endpoints */
969 if (!subset_active(dev) && dev->status_ep)
970 (void) usb_ep_disable(dev->status_ep);
972 (void) usb_ep_disable(dev->in_ep);
973 (void) usb_ep_disable(dev->out_ep);
976 } else if (!cdc_active(dev)) {
978 * activate non-CDC configs right away
979 * this isn't strictly according to the RNDIS spec
981 eth_start(dev, GFP_ATOMIC);
984 /* caller is responsible for cleanup on error */
988 static void eth_reset_config(struct eth_dev *dev)
990 if (dev->config == 0)
993 debug("%s\n", __func__);
995 rndis_uninit(dev->rndis_config);
998 * disable endpoints, forcing (synchronous) completion of
999 * pending i/o. then free the requests.
1003 usb_ep_disable(dev->in_ep);
1005 usb_ep_free_request(dev->in_ep, dev->tx_req);
1010 usb_ep_disable(dev->out_ep);
1012 usb_ep_free_request(dev->out_ep, dev->rx_req);
1017 usb_ep_disable(dev->status_ep);
1020 dev->cdc_filter = 0;
1025 * change our operational config. must agree with the code
1026 * that returns config descriptors, and altsetting code.
1028 static int eth_set_config(struct eth_dev *dev, unsigned number,
1032 struct usb_gadget *gadget = dev->gadget;
1034 if (gadget_is_sa1100(gadget)
1036 && dev->tx_qlen != 0) {
1037 /* tx fifo is full, but we can't clear it...*/
1038 error("can't change configurations");
1041 eth_reset_config(dev);
1044 case DEV_CONFIG_VALUE:
1045 result = set_ether_config(dev, gfp_flags);
1047 #ifdef CONFIG_USB_ETH_RNDIS
1048 case DEV_RNDIS_CONFIG_VALUE:
1050 result = set_ether_config(dev, gfp_flags);
1062 eth_reset_config(dev);
1063 usb_gadget_vbus_draw(dev->gadget,
1064 gadget_is_otg(dev->gadget) ? 8 : 100);
1069 power = 2 * eth_config.bMaxPower;
1070 usb_gadget_vbus_draw(dev->gadget, power);
1072 switch (gadget->speed) {
1073 case USB_SPEED_FULL:
1074 speed = "full"; break;
1075 #ifdef CONFIG_USB_GADGET_DUALSPEED
1076 case USB_SPEED_HIGH:
1077 speed = "high"; break;
1083 dev->config = number;
1084 printf("%s speed config #%d: %d mA, %s, using %s\n",
1085 speed, number, power, driver_desc,
1090 : "CDC Ethernet Subset"));
1095 /*-------------------------------------------------------------------------*/
1097 #ifdef CONFIG_USB_ETH_CDC
1100 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1101 * only to notify the host about link status changes (which we support) or
1102 * report completion of some encapsulated command (as used in RNDIS). Since
1103 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1104 * command mechanism; and only one status request is ever queued.
1106 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1108 struct usb_cdc_notification *event = req->buf;
1109 int value = req->status;
1110 struct eth_dev *dev = ep->driver_data;
1112 /* issue the second notification if host reads the first */
1113 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1115 __le32 *data = req->buf + sizeof *event;
1117 event->bmRequestType = 0xA1;
1118 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1119 event->wValue = __constant_cpu_to_le16(0);
1120 event->wIndex = __constant_cpu_to_le16(1);
1121 event->wLength = __constant_cpu_to_le16(8);
1123 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1124 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1126 req->length = STATUS_BYTECOUNT;
1127 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1128 debug("send SPEED_CHANGE --> %d\n", value);
1131 } else if (value != -ECONNRESET) {
1132 debug("event %02x --> %d\n",
1133 event->bNotificationType, value);
1134 if (event->bNotificationType ==
1135 USB_CDC_NOTIFY_SPEED_CHANGE) {
1136 l_ethdev.network_started = 1;
1137 printf("USB network up!\n");
1140 req->context = NULL;
1143 static void issue_start_status(struct eth_dev *dev)
1145 struct usb_request *req = dev->stat_req;
1146 struct usb_cdc_notification *event;
1152 * FIXME ugly idiom, maybe we'd be better with just
1153 * a "cancel the whole queue" primitive since any
1154 * unlink-one primitive has way too many error modes.
1155 * here, we "know" toggle is already clear...
1157 * FIXME iff req->context != null just dequeue it
1159 usb_ep_disable(dev->status_ep);
1160 usb_ep_enable(dev->status_ep, dev->status);
1163 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1164 * a SPEED_CHANGE. could be useful in some configs.
1167 event->bmRequestType = 0xA1;
1168 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1169 event->wValue = __constant_cpu_to_le16(1); /* connected */
1170 event->wIndex = __constant_cpu_to_le16(1);
1173 req->length = sizeof *event;
1174 req->complete = eth_status_complete;
1177 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1179 debug("status buf queue --> %d\n", value);
1184 /*-------------------------------------------------------------------------*/
1186 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1188 if (req->status || req->actual != req->length)
1189 debug("setup complete --> %d, %d/%d\n",
1190 req->status, req->actual, req->length);
1193 #ifdef CONFIG_USB_ETH_RNDIS
1195 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1197 if (req->status || req->actual != req->length)
1198 debug("rndis response complete --> %d, %d/%d\n",
1199 req->status, req->actual, req->length);
1201 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1204 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1206 struct eth_dev *dev = ep->driver_data;
1209 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1210 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1212 error("%s: rndis parse error %d", __func__, status);
1218 * The setup() callback implements all the ep0 functionality that's not
1219 * handled lower down. CDC has a number of less-common features:
1221 * - two interfaces: control, and ethernet data
1222 * - Ethernet data interface has two altsettings: default, and active
1223 * - class-specific descriptors for the control interface
1224 * - class-specific control requests
1227 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1229 struct eth_dev *dev = get_gadget_data(gadget);
1230 struct usb_request *req = dev->req;
1231 int value = -EOPNOTSUPP;
1232 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1233 u16 wValue = le16_to_cpu(ctrl->wValue);
1234 u16 wLength = le16_to_cpu(ctrl->wLength);
1237 * descriptors just go into the pre-allocated ep0 buffer,
1238 * while config change events may enable network traffic.
1241 debug("%s\n", __func__);
1243 req->complete = eth_setup_complete;
1244 switch (ctrl->bRequest) {
1246 case USB_REQ_GET_DESCRIPTOR:
1247 if (ctrl->bRequestType != USB_DIR_IN)
1249 switch (wValue >> 8) {
1252 value = min(wLength, (u16) sizeof device_desc);
1253 memcpy(req->buf, &device_desc, value);
1255 case USB_DT_DEVICE_QUALIFIER:
1256 if (!gadget_is_dualspeed(gadget))
1258 value = min(wLength, (u16) sizeof dev_qualifier);
1259 memcpy(req->buf, &dev_qualifier, value);
1262 case USB_DT_OTHER_SPEED_CONFIG:
1263 if (!gadget_is_dualspeed(gadget))
1267 value = config_buf(gadget, req->buf,
1270 gadget_is_otg(gadget));
1272 value = min(wLength, (u16) value);
1276 value = usb_gadget_get_string(&stringtab,
1277 wValue & 0xff, req->buf);
1280 value = min(wLength, (u16) value);
1286 case USB_REQ_SET_CONFIGURATION:
1287 if (ctrl->bRequestType != 0)
1289 if (gadget->a_hnp_support)
1290 debug("HNP available\n");
1291 else if (gadget->a_alt_hnp_support)
1292 debug("HNP needs a different root port\n");
1293 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1295 case USB_REQ_GET_CONFIGURATION:
1296 if (ctrl->bRequestType != USB_DIR_IN)
1298 *(u8 *)req->buf = dev->config;
1299 value = min(wLength, (u16) 1);
1302 case USB_REQ_SET_INTERFACE:
1303 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1307 if (!cdc_active(dev) && wIndex != 0)
1311 * PXA hardware partially handles SET_INTERFACE;
1312 * we need to kluge around that interference.
1314 if (gadget_is_pxa(gadget)) {
1315 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1318 * PXA25x driver use non-CDC ethernet gadget.
1319 * But only _CDC and _RNDIS code can signalize
1320 * that network is working. So we signalize it
1323 l_ethdev.network_started = 1;
1324 debug("USB network up!\n");
1328 #ifdef CONFIG_USB_ETH_CDC
1330 case 0: /* control/master intf */
1334 usb_ep_disable(dev->status_ep);
1335 usb_ep_enable(dev->status_ep, dev->status);
1340 case 1: /* data intf */
1343 usb_ep_disable(dev->in_ep);
1344 usb_ep_disable(dev->out_ep);
1347 * CDC requires the data transfers not be done from
1348 * the default interface setting ... also, setting
1349 * the non-default interface resets filters etc.
1352 if (!cdc_active(dev))
1354 usb_ep_enable(dev->in_ep, dev->in);
1355 usb_ep_enable(dev->out_ep, dev->out);
1356 dev->cdc_filter = DEFAULT_FILTER;
1358 issue_start_status(dev);
1359 eth_start(dev, GFP_ATOMIC);
1366 * FIXME this is wrong, as is the assumption that
1367 * all non-PXA hardware talks real CDC ...
1369 debug("set_interface ignored!\n");
1370 #endif /* CONFIG_USB_ETH_CDC */
1374 case USB_REQ_GET_INTERFACE:
1375 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1379 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1382 /* for CDC, iff carrier is on, data interface is active. */
1383 if (rndis_active(dev) || wIndex != 1)
1384 *(u8 *)req->buf = 0;
1386 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1387 /* carrier always ok ...*/
1388 *(u8 *)req->buf = 1 ;
1390 value = min(wLength, (u16) 1);
1393 #ifdef CONFIG_USB_ETH_CDC
1394 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1396 * see 6.2.30: no data, wIndex = interface,
1397 * wValue = packet filter bitmap
1399 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1404 debug("packet filter %02x\n", wValue);
1405 dev->cdc_filter = wValue;
1411 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1412 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1413 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1414 * case USB_CDC_GET_ETHERNET_STATISTIC:
1417 #endif /* CONFIG_USB_ETH_CDC */
1419 #ifdef CONFIG_USB_ETH_RNDIS
1421 * RNDIS uses the CDC command encapsulation mechanism to implement
1422 * an RPC scheme, with much getting/setting of attributes by OID.
1424 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1425 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1426 || !rndis_active(dev)
1427 || wLength > USB_BUFSIZ
1429 || rndis_control_intf.bInterfaceNumber
1432 /* read the request, then process it */
1434 req->complete = rndis_command_complete;
1435 /* later, rndis_control_ack () sends a notification */
1438 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1439 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1440 == ctrl->bRequestType
1441 && rndis_active(dev)
1442 /* && wLength >= 0x0400 */
1444 && rndis_control_intf.bInterfaceNumber
1449 /* return the result */
1450 buf = rndis_get_next_response(dev->rndis_config, &n);
1452 memcpy(req->buf, buf, n);
1453 req->complete = rndis_response_complete;
1454 rndis_free_response(dev->rndis_config, buf);
1457 /* else stalls ... spec says to avoid that */
1463 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1464 ctrl->bRequestType, ctrl->bRequest,
1465 wValue, wIndex, wLength);
1468 /* respond with data transfer before status phase? */
1470 debug("respond with data transfer before status phase\n");
1471 req->length = value;
1472 req->zero = value < wLength
1473 && (value % gadget->ep0->maxpacket) == 0;
1474 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1476 debug("ep_queue --> %d\n", value);
1478 eth_setup_complete(gadget->ep0, req);
1482 /* host either stalls (value < 0) or reports success */
1486 /*-------------------------------------------------------------------------*/
1488 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1490 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1493 int retval = -ENOMEM;
1497 * Padding up to RX_EXTRA handles minor disagreements with host.
1498 * Normally we use the USB "terminate on short read" convention;
1499 * so allow up to (N*maxpacket), since that memory is normally
1500 * already allocated. Some hardware doesn't deal well with short
1501 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1502 * byte off the end (to force hardware errors on overflow).
1504 * RNDIS uses internal framing, and explicitly allows senders to
1505 * pad to end-of-packet. That's potentially nice for speed,
1506 * but means receivers can't recover synch on their own.
1509 debug("%s\n", __func__);
1513 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1514 size += dev->out_ep->maxpacket - 1;
1515 if (rndis_active(dev))
1516 size += sizeof(struct rndis_packet_msg_type);
1517 size -= size % dev->out_ep->maxpacket;
1520 * Some platforms perform better when IP packets are aligned,
1521 * but on at least one, checksumming fails otherwise. Note:
1522 * RNDIS headers involve variable numbers of LE32 values.
1525 req->buf = (u8 *) NetRxPackets[0];
1527 req->complete = rx_complete;
1529 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1532 error("rx submit --> %d", retval);
1537 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1539 struct eth_dev *dev = ep->driver_data;
1541 debug("%s: status %d\n", __func__, req->status);
1542 switch (req->status) {
1543 /* normal completion */
1545 if (rndis_active(dev)) {
1546 /* we know MaxPacketsPerTransfer == 1 here */
1547 int length = rndis_rm_hdr(req->buf, req->actual);
1550 req->length -= length;
1551 req->actual -= length;
1553 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1555 dev->stats.rx_errors++;
1556 dev->stats.rx_length_errors++;
1557 debug("rx length %d\n", req->length);
1561 dev->stats.rx_packets++;
1562 dev->stats.rx_bytes += req->length;
1565 /* software-driven interface shutdown */
1566 case -ECONNRESET: /* unlink */
1567 case -ESHUTDOWN: /* disconnect etc */
1568 /* for hardware automagic (such as pxa) */
1569 case -ECONNABORTED: /* endpoint reset */
1574 dev->stats.rx_over_errors++;
1577 dev->stats.rx_errors++;
1581 packet_received = 1;
1584 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1587 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1592 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1600 usb_ep_free_request(dev->in_ep, dev->tx_req);
1602 error("can't alloc requests");
1606 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1608 struct eth_dev *dev = ep->driver_data;
1610 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1611 switch (req->status) {
1613 dev->stats.tx_errors++;
1614 debug("tx err %d\n", req->status);
1616 case -ECONNRESET: /* unlink */
1617 case -ESHUTDOWN: /* disconnect etc */
1620 dev->stats.tx_bytes += req->length;
1622 dev->stats.tx_packets++;
1627 static inline int eth_is_promisc(struct eth_dev *dev)
1629 /* no filters for the CDC subset; always promisc */
1630 if (subset_active(dev))
1632 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1636 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1638 struct eth_dev *dev = netdev_priv(net);
1639 int length = skb->len;
1641 struct usb_request *req = NULL;
1642 unsigned long flags;
1644 /* apply outgoing CDC or RNDIS filters */
1645 if (!eth_is_promisc (dev)) {
1646 u8 *dest = skb->data;
1648 if (is_multicast_ether_addr(dest)) {
1651 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1652 * SET_ETHERNET_MULTICAST_FILTERS requests
1654 if (is_broadcast_ether_addr(dest))
1655 type = USB_CDC_PACKET_TYPE_BROADCAST;
1657 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1658 if (!(dev->cdc_filter & type)) {
1659 dev_kfree_skb_any (skb);
1663 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1666 spin_lock_irqsave(&dev->req_lock, flags);
1668 * this freelist can be empty if an interrupt triggered disconnect()
1669 * and reconfigured the gadget (shutting down this queue) after the
1670 * network stack decided to xmit but before we got the spinlock.
1672 if (list_empty(&dev->tx_reqs)) {
1673 spin_unlock_irqrestore(&dev->req_lock, flags);
1677 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1678 list_del (&req->list);
1680 /* temporarily stop TX queue when the freelist empties */
1681 if (list_empty (&dev->tx_reqs))
1682 netif_stop_queue (net);
1683 spin_unlock_irqrestore(&dev->req_lock, flags);
1685 /* no buffer copies needed, unless the network stack did it
1686 * or the hardware can't use skb buffers.
1687 * or there's not enough space for any RNDIS headers we need
1689 if (rndis_active(dev)) {
1690 struct sk_buff *skb_rndis;
1692 skb_rndis = skb_realloc_headroom (skb,
1693 sizeof (struct rndis_packet_msg_type));
1697 dev_kfree_skb_any (skb);
1699 rndis_add_hdr (skb);
1702 req->buf = skb->data;
1704 req->complete = tx_complete;
1706 /* use zlp framing on tx for strict CDC-Ether conformance,
1707 * though any robust network rx path ignores extra padding.
1708 * and some hardware doesn't like to write zlps.
1711 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1714 req->length = length;
1716 /* throttle highspeed IRQ rate back slightly */
1717 if (gadget_is_dualspeed(dev->gadget))
1718 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1719 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1722 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1725 DEBUG (dev, "tx queue err %d\n", retval);
1728 net->trans_start = jiffies;
1729 atomic_inc (&dev->tx_qlen);
1734 dev->stats.tx_dropped++;
1735 dev_kfree_skb_any (skb);
1736 spin_lock_irqsave(&dev->req_lock, flags);
1737 if (list_empty (&dev->tx_reqs))
1738 netif_start_queue (net);
1739 list_add (&req->list, &dev->tx_reqs);
1740 spin_unlock_irqrestore(&dev->req_lock, flags);
1745 /*-------------------------------------------------------------------------*/
1748 static void eth_unbind(struct usb_gadget *gadget)
1750 struct eth_dev *dev = get_gadget_data(gadget);
1752 debug("%s...\n", __func__);
1753 rndis_deregister(dev->rndis_config);
1756 /* we've already been disconnected ... no i/o is active */
1758 usb_ep_free_request(gadget->ep0, dev->req);
1761 if (dev->stat_req) {
1762 usb_ep_free_request(dev->status_ep, dev->stat_req);
1763 dev->stat_req = NULL;
1767 usb_ep_free_request(dev->in_ep, dev->tx_req);
1772 usb_ep_free_request(dev->out_ep, dev->rx_req);
1776 /* unregister_netdev (dev->net);*/
1777 /* free_netdev(dev->net);*/
1780 set_gadget_data(gadget, NULL);
1783 static void eth_disconnect(struct usb_gadget *gadget)
1785 eth_reset_config(get_gadget_data(gadget));
1786 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1789 static void eth_suspend(struct usb_gadget *gadget)
1794 static void eth_resume(struct usb_gadget *gadget)
1799 /*-------------------------------------------------------------------------*/
1801 #ifdef CONFIG_USB_ETH_RNDIS
1804 * The interrupt endpoint is used in RNDIS to notify the host when messages
1805 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1806 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1807 * REMOTE_NDIS_KEEPALIVE_MSG.
1809 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1810 * normally just one notification will be queued.
1813 static void rndis_control_ack_complete(struct usb_ep *ep,
1814 struct usb_request *req)
1816 struct eth_dev *dev = ep->driver_data;
1818 debug("%s...\n", __func__);
1819 if (req->status || req->actual != req->length)
1820 debug("rndis control ack complete --> %d, %d/%d\n",
1821 req->status, req->actual, req->length);
1823 if (!l_ethdev.network_started) {
1824 if (rndis_get_state(dev->rndis_config)
1825 == RNDIS_DATA_INITIALIZED) {
1826 l_ethdev.network_started = 1;
1827 printf("USB RNDIS network up!\n");
1831 req->context = NULL;
1833 if (req != dev->stat_req)
1834 usb_ep_free_request(ep, req);
1837 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1839 static int rndis_control_ack(struct eth_device *net)
1841 struct eth_dev *dev = &l_ethdev;
1843 struct usb_request *resp = dev->stat_req;
1845 /* in case RNDIS calls this after disconnect */
1847 debug("status ENODEV\n");
1851 /* in case queue length > 1 */
1852 if (resp->context) {
1853 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1856 resp->buf = rndis_resp_buf;
1860 * Send RNDIS RESPONSE_AVAILABLE notification;
1861 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1864 resp->complete = rndis_control_ack_complete;
1865 resp->context = dev;
1867 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1868 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1870 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1873 rndis_control_ack_complete(dev->status_ep, resp);
1881 #define rndis_control_ack NULL
1885 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1887 if (rndis_active(dev)) {
1888 rndis_set_param_medium(dev->rndis_config,
1890 BITRATE(dev->gadget)/100);
1891 rndis_signal_connect(dev->rndis_config);
1895 static int eth_stop(struct eth_dev *dev)
1897 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1899 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1902 if (rndis_active(dev)) {
1903 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1904 rndis_signal_disconnect(dev->rndis_config);
1906 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1907 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1909 while (get_timer(ts) < timeout)
1910 usb_gadget_handle_interrupts();
1913 rndis_uninit(dev->rndis_config);
1920 /*-------------------------------------------------------------------------*/
1922 static int is_eth_addr_valid(char *str)
1924 if (strlen(str) == 17) {
1929 /* see if it looks like an ethernet address */
1933 for (i = 0; i < 6; i++) {
1934 char term = (i == 5 ? '\0' : ':');
1936 ea[i] = simple_strtol(p, &q, 16);
1938 if ((q - p) != 2 || *q++ != term)
1944 /* Now check the contents. */
1945 return is_valid_ether_addr(ea);
1950 static u8 nibble(unsigned char c)
1952 if (likely(isdigit(c)))
1955 if (likely(isxdigit(c)))
1956 return 10 + c - 'A';
1960 static int get_ether_addr(const char *str, u8 *dev_addr)
1965 for (i = 0; i < 6; i++) {
1968 if ((*str == '.') || (*str == ':'))
1970 num = nibble(*str++) << 4;
1971 num |= (nibble(*str++));
1974 if (is_valid_ether_addr(dev_addr))
1980 static int eth_bind(struct usb_gadget *gadget)
1982 struct eth_dev *dev = &l_ethdev;
1983 u8 cdc = 1, zlp = 1, rndis = 1;
1984 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1985 int status = -ENOMEM;
1989 /* these flags are only ever cleared; compiler take note */
1990 #ifndef CONFIG_USB_ETH_CDC
1993 #ifndef CONFIG_USB_ETH_RNDIS
1997 * Because most host side USB stacks handle CDC Ethernet, that
1998 * standard protocol is _strongly_ preferred for interop purposes.
1999 * (By everyone except Microsoft.)
2001 if (gadget_is_pxa(gadget)) {
2002 /* pxa doesn't support altsettings */
2004 } else if (gadget_is_musbhdrc(gadget)) {
2005 /* reduce tx dma overhead by avoiding special cases */
2007 } else if (gadget_is_sh(gadget)) {
2008 /* sh doesn't support multiple interfaces or configs */
2011 } else if (gadget_is_sa1100(gadget)) {
2012 /* hardware can't write zlps */
2015 * sa1100 CAN do CDC, without status endpoint ... we use
2016 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2021 gcnum = usb_gadget_controller_number(gadget);
2023 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2026 * can't assume CDC works. don't want to default to
2027 * anything less functional on CDC-capable hardware,
2028 * so we fail in this case.
2030 error("controller '%s' not recognized",
2036 * If there's an RNDIS configuration, that's what Windows wants to
2037 * be using ... so use these product IDs here and in the "linux.inf"
2038 * needed to install MSFT drivers. Current Linux kernels will use
2039 * the second configuration if it's CDC Ethernet, and need some help
2040 * to choose the right configuration otherwise.
2043 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2044 device_desc.idVendor =
2045 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2046 device_desc.idProduct =
2047 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2049 device_desc.idVendor =
2050 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2051 device_desc.idProduct =
2052 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2054 sprintf(product_desc, "RNDIS/%s", driver_desc);
2057 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2058 * drivers aren't widely available. (That may be improved by
2059 * supporting one submode of the "SAFE" variant of MDLM.)
2062 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2063 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2064 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2067 device_desc.idVendor =
2068 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2069 device_desc.idProduct =
2070 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2074 /* support optional vendor/distro customization */
2076 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2078 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2080 strlcpy(product_desc, iProduct, sizeof product_desc);
2081 if (iSerialNumber) {
2082 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2083 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2086 /* all we really need is bulk IN/OUT */
2087 usb_ep_autoconfig_reset(gadget);
2088 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2091 error("can't autoconfigure on %s\n",
2095 in_ep->driver_data = in_ep; /* claim */
2097 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2100 out_ep->driver_data = out_ep; /* claim */
2102 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2104 * CDC Ethernet control interface doesn't require a status endpoint.
2105 * Since some hosts expect one, try to allocate one anyway.
2108 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2110 status_ep->driver_data = status_ep; /* claim */
2112 error("can't run RNDIS on %s", gadget->name);
2114 #ifdef CONFIG_USB_ETH_CDC
2116 control_intf.bNumEndpoints = 0;
2117 /* FIXME remove endpoint from descriptor list */
2123 /* one config: cdc, else minimal subset */
2125 eth_config.bNumInterfaces = 1;
2126 eth_config.iConfiguration = STRING_SUBSET;
2129 * use functions to set these up, in case we're built to work
2130 * with multiple controllers and must override CDC Ethernet.
2132 fs_subset_descriptors();
2133 hs_subset_descriptors();
2136 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2137 usb_gadget_set_selfpowered(gadget);
2139 /* For now RNDIS is always a second config */
2141 device_desc.bNumConfigurations = 2;
2143 if (gadget_is_dualspeed(gadget)) {
2145 dev_qualifier.bNumConfigurations = 2;
2147 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2149 /* assumes ep0 uses the same value for both speeds ... */
2150 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2152 /* and that all endpoints are dual-speed */
2153 hs_source_desc.bEndpointAddress =
2154 fs_source_desc.bEndpointAddress;
2155 hs_sink_desc.bEndpointAddress =
2156 fs_sink_desc.bEndpointAddress;
2157 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2159 hs_status_desc.bEndpointAddress =
2160 fs_status_desc.bEndpointAddress;
2164 if (gadget_is_otg(gadget)) {
2165 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2166 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2167 eth_config.bMaxPower = 4;
2168 #ifdef CONFIG_USB_ETH_RNDIS
2169 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2170 rndis_config.bMaxPower = 4;
2175 /* network device setup */
2176 dev->net = &l_netdev;
2182 dev->out_ep = out_ep;
2183 dev->status_ep = status_ep;
2186 * Module params for these addresses should come from ID proms.
2187 * The host side address is used with CDC and RNDIS, and commonly
2188 * ends up in a persistent config database. It's not clear if
2189 * host side code for the SAFE thing cares -- its original BLAN
2190 * thing didn't, Sharp never assigned those addresses on Zaurii.
2192 get_ether_addr(dev_addr, dev->net->enetaddr);
2194 memset(tmp, 0, sizeof(tmp));
2195 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2197 get_ether_addr(host_addr, dev->host_mac);
2199 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2200 dev->host_mac[0], dev->host_mac[1],
2201 dev->host_mac[2], dev->host_mac[3],
2202 dev->host_mac[4], dev->host_mac[5]);
2205 status = rndis_init();
2207 error("can't init RNDIS, %d", status);
2213 * use PKTSIZE (or aligned... from u-boot) and set
2214 * wMaxSegmentSize accordingly
2216 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2218 /* preallocate control message data and buffer */
2219 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2222 dev->req->buf = control_req;
2223 dev->req->complete = eth_setup_complete;
2225 /* ... and maybe likewise for status transfer */
2226 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2227 if (dev->status_ep) {
2228 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2230 if (!dev->stat_req) {
2231 usb_ep_free_request(dev->status_ep, dev->req);
2235 dev->stat_req->buf = status_req;
2236 dev->stat_req->context = NULL;
2240 /* finish hookup to lower layer ... */
2241 dev->gadget = gadget;
2242 set_gadget_data(gadget, dev);
2243 gadget->ep0->driver_data = dev;
2246 * two kinds of host-initiated state changes:
2247 * - iff DATA transfer is active, carrier is "on"
2248 * - tx queueing enabled if open *and* carrier is "on"
2251 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2252 out_ep->name, in_ep->name,
2253 status_ep ? " STATUS " : "",
2254 status_ep ? status_ep->name : ""
2256 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2257 dev->net->enetaddr[0], dev->net->enetaddr[1],
2258 dev->net->enetaddr[2], dev->net->enetaddr[3],
2259 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2262 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2263 dev->host_mac[0], dev->host_mac[1],
2264 dev->host_mac[2], dev->host_mac[3],
2265 dev->host_mac[4], dev->host_mac[5]);
2270 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2272 dev->rndis_config = rndis_register(rndis_control_ack);
2273 if (dev->rndis_config < 0) {
2276 debug("RNDIS setup failed\n");
2281 /* these set up a lot of the OIDs that RNDIS needs */
2282 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2283 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2284 &dev->stats, &dev->cdc_filter))
2286 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2289 if (rndis_set_param_medium(dev->rndis_config,
2290 NDIS_MEDIUM_802_3, 0))
2292 printf("RNDIS ready\n");
2297 error("%s failed, status = %d", __func__, status);
2302 /*-------------------------------------------------------------------------*/
2304 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2306 struct eth_dev *dev = &l_ethdev;
2307 struct usb_gadget *gadget;
2309 unsigned long timeout = USB_CONNECT_TIMEOUT;
2312 error("received NULL ptr");
2316 /* Configure default mac-addresses for the USB ethernet device */
2317 #ifdef CONFIG_USBNET_DEV_ADDR
2318 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2320 #ifdef CONFIG_USBNET_HOST_ADDR
2321 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2323 /* Check if the user overruled the MAC addresses */
2324 if (getenv("usbnet_devaddr"))
2325 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2328 if (getenv("usbnet_hostaddr"))
2329 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2332 if (!is_eth_addr_valid(dev_addr)) {
2333 error("Need valid 'usbnet_devaddr' to be set");
2336 if (!is_eth_addr_valid(host_addr)) {
2337 error("Need valid 'usbnet_hostaddr' to be set");
2341 if (usb_gadget_register_driver(ð_driver) < 0)
2344 dev->network_started = 0;
2346 packet_received = 0;
2349 gadget = dev->gadget;
2350 usb_gadget_connect(gadget);
2352 if (getenv("cdc_connect_timeout"))
2353 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2354 NULL, 10) * CONFIG_SYS_HZ;
2356 while (!l_ethdev.network_started) {
2357 /* Handle control-c and timeouts */
2358 if (ctrlc() || (get_timer(ts) > timeout)) {
2359 error("The remote end did not respond in time.");
2362 usb_gadget_handle_interrupts();
2365 packet_received = 0;
2366 rx_submit(dev, dev->rx_req, 0);
2372 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2375 void *rndis_pkt = NULL;
2376 struct eth_dev *dev = &l_ethdev;
2377 struct usb_request *req = dev->tx_req;
2379 unsigned long timeout = USB_CONNECT_TIMEOUT;
2381 debug("%s:...\n", __func__);
2383 /* new buffer is needed to include RNDIS header */
2384 if (rndis_active(dev)) {
2385 rndis_pkt = malloc(length +
2386 sizeof(struct rndis_packet_msg_type));
2388 error("No memory to alloc RNDIS packet");
2391 rndis_add_hdr(rndis_pkt, length);
2392 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2395 length += sizeof(struct rndis_packet_msg_type);
2398 req->context = NULL;
2399 req->complete = tx_complete;
2402 * use zlp framing on tx for strict CDC-Ether conformance,
2403 * though any robust network rx path ignores extra padding.
2404 * and some hardware doesn't like to write zlps.
2407 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2410 req->length = length;
2412 /* throttle highspeed IRQ rate back slightly */
2413 if (gadget_is_dualspeed(dev->gadget))
2414 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2415 ? ((dev->tx_qlen % qmult) != 0) : 0;
2421 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2424 debug("%s: packet queued\n", __func__);
2425 while (!packet_sent) {
2426 if (get_timer(ts) > timeout) {
2427 printf("timeout sending packets to usb ethernet\n");
2430 usb_gadget_handle_interrupts();
2437 dev->stats.tx_dropped++;
2441 static int usb_eth_recv(struct eth_device *netdev)
2443 struct eth_dev *dev = &l_ethdev;
2445 usb_gadget_handle_interrupts();
2447 if (packet_received) {
2448 debug("%s: packet received\n", __func__);
2450 NetReceive(NetRxPackets[0], dev->rx_req->length);
2451 packet_received = 0;
2453 rx_submit(dev, dev->rx_req, 0);
2455 error("dev->rx_req invalid");
2460 void usb_eth_halt(struct eth_device *netdev)
2462 struct eth_dev *dev = &l_ethdev;
2465 error("received NULL ptr");
2469 /* If the gadget not registered, simple return */
2474 * Some USB controllers may need additional deinitialization here
2475 * before dropping pull-up (also due to hardware issues).
2476 * For example: unhandled interrupt with status stage started may
2477 * bring the controller to fully broken state (until board reset).
2478 * There are some variants to debug and fix such cases:
2479 * 1) In the case of RNDIS connection eth_stop can perform additional
2480 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2481 * 2) 'pullup' callback in your UDC driver can be improved to perform
2482 * this deinitialization.
2486 usb_gadget_disconnect(dev->gadget);
2488 /* Clear pending interrupt */
2489 if (dev->network_started) {
2490 usb_gadget_handle_interrupts();
2491 dev->network_started = 0;
2494 usb_gadget_unregister_driver(ð_driver);
2497 static struct usb_gadget_driver eth_driver = {
2501 .unbind = eth_unbind,
2504 .disconnect = eth_disconnect,
2506 .suspend = eth_suspend,
2507 .resume = eth_resume,
2510 int usb_eth_initialize(bd_t *bi)
2512 struct eth_device *netdev = &l_netdev;
2514 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2516 netdev->init = usb_eth_init;
2517 netdev->send = usb_eth_send;
2518 netdev->recv = usb_eth_recv;
2519 netdev->halt = usb_eth_halt;
2521 #ifdef CONFIG_MCAST_TFTP
2522 #error not supported
2524 eth_register(netdev);