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;
29 #define spin_unlock(x)
32 unsigned packet_received, packet_sent;
34 #define GFP_ATOMIC ((gfp_t) 0)
35 #define GFP_KERNEL ((gfp_t) 0)
38 * Ethernet gadget driver -- with CDC and non-CDC options
39 * Builds on hardware support for a full duplex link.
41 * CDC Ethernet is the standard USB solution for sending Ethernet frames
42 * using USB. Real hardware tends to use the same framing protocol but look
43 * different for control features. This driver strongly prefers to use
44 * this USB-IF standard as its open-systems interoperability solution;
45 * most host side USB stacks (except from Microsoft) support it.
47 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
48 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
49 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
51 * There's some hardware that can't talk CDC ECM. We make that hardware
52 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
53 * link-level setup only requires activating the configuration. Only the
54 * endpoint descriptors, and product/vendor IDs, are relevant; no control
55 * operations are available. Linux supports it, but other host operating
56 * systems may not. (This is a subset of CDC Ethernet.)
58 * It turns out that if you add a few descriptors to that "CDC Subset",
59 * (Windows) host side drivers from MCCI can treat it as one submode of
60 * a proprietary scheme called "SAFE" ... without needing to know about
61 * specific product/vendor IDs. So we do that, making it easier to use
62 * those MS-Windows drivers. Those added descriptors make it resemble a
63 * CDC MDLM device, but they don't change device behavior at all. (See
64 * MCCI Engineering report 950198 "SAFE Networking Functions".)
66 * A third option is also in use. Rather than CDC Ethernet, or something
67 * simpler, Microsoft pushes their own approach: RNDIS. The published
68 * RNDIS specs are ambiguous and appear to be incomplete, and are also
69 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
71 #define ETH_ALEN 6 /* Octets in one ethernet addr */
72 #define ETH_HLEN 14 /* Total octets in header. */
73 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
74 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
75 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
76 #define ETH_FCS_LEN 4 /* Octets in the FCS */
78 #define DRIVER_DESC "Ethernet Gadget"
79 /* Based on linux 2.6.27 version */
80 #define DRIVER_VERSION "May Day 2005"
82 static const char shortname[] = "ether";
83 static const char driver_desc[] = DRIVER_DESC;
85 #define RX_EXTRA 20 /* guard against rx overflows */
87 #ifndef CONFIG_USB_ETH_RNDIS
88 #define rndis_uninit(x) do {} while (0)
89 #define rndis_deregister(c) do {} while (0)
90 #define rndis_exit() do {} while (0)
93 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
94 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
95 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
96 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
97 |USB_CDC_PACKET_TYPE_DIRECTED)
99 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
101 /*-------------------------------------------------------------------------*/
104 struct usb_gadget *gadget;
105 struct usb_request *req; /* for control responses */
106 struct usb_request *stat_req; /* for cdc & rndis status */
109 struct usb_ep *in_ep, *out_ep, *status_ep;
110 const struct usb_endpoint_descriptor
113 struct usb_request *tx_req, *rx_req;
115 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 /*-------------------------------------------------------------------------*/
140 static struct eth_dev l_ethdev;
141 static struct eth_device l_netdev;
142 static struct usb_gadget_driver eth_driver;
144 /*-------------------------------------------------------------------------*/
146 /* "main" config is either CDC, or its simple subset */
147 static inline int is_cdc(struct eth_dev *dev)
149 #if !defined(CONFIG_USB_ETH_SUBSET)
150 return 1; /* only cdc possible */
151 #elif !defined(CONFIG_USB_ETH_CDC)
152 return 0; /* only subset possible */
154 return dev->cdc; /* depends on what hardware we found */
158 /* "secondary" RNDIS config may sometimes be activated */
159 static inline int rndis_active(struct eth_dev *dev)
161 #ifdef CONFIG_USB_ETH_RNDIS
168 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
169 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
171 #define DEFAULT_QLEN 2 /* double buffering by default */
173 /* peak bulk transfer bits-per-second */
174 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
175 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
177 #ifdef CONFIG_USB_GADGET_DUALSPEED
178 #define DEVSPEED USB_SPEED_HIGH
180 #ifdef CONFIG_USB_ETH_QMULT
181 #define qmult CONFIG_USB_ETH_QMULT
186 /* for dual-speed hardware, use deeper queues at highspeed */
187 #define qlen(gadget) \
188 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
190 static inline int BITRATE(struct usb_gadget *g)
192 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
195 #else /* full speed (low speed doesn't do bulk) */
199 #define DEVSPEED USB_SPEED_FULL
201 #define qlen(gadget) DEFAULT_QLEN
203 static inline int BITRATE(struct usb_gadget *g)
209 /*-------------------------------------------------------------------------*/
212 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
213 * Instead: allocate your own, using normal USB-IF procedures.
217 * Thanks to NetChip Technologies for donating this product ID.
218 * It's for devices with only CDC Ethernet configurations.
220 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
221 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
224 * For hardware that can't talk CDC, we use the same vendor ID that
225 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
226 * with pxa250. We're protocol-compatible, if the host-side drivers
227 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
228 * drivers that need to hard-wire endpoint numbers have a hook.
230 * The protocol is a minimal subset of CDC Ether, which works on any bulk
231 * hardware that's not deeply broken ... even on hardware that can't talk
232 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
233 * doesn't handle control-OUT).
235 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
236 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
239 * For hardware that can talk RNDIS and either of the above protocols,
240 * use this ID ... the windows INF files will know it. Unless it's
241 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
242 * the non-RNDIS configuration.
244 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
245 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
248 * Some systems will want different product identifers published in the
249 * device descriptor, either numbers or strings or both. These string
250 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
254 * Emulating them in eth_bind:
255 * static ushort idVendor;
256 * static ushort idProduct;
259 #if defined(CONFIG_USBNET_MANUFACTURER)
260 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
262 static char *iManufacturer = "U-boot";
265 /* These probably need to be configurable. */
266 static ushort bcdDevice;
267 static char *iProduct;
268 static char *iSerialNumber;
270 static char dev_addr[18];
272 static char host_addr[18];
275 /*-------------------------------------------------------------------------*/
278 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
279 * ep0 implementation: descriptors, config management, setup().
280 * also optional class-specific notification interrupt transfer.
284 * DESCRIPTORS ... most are static, but strings and (full) configuration
285 * descriptors are built on demand. For now we do either full CDC, or
286 * our simple subset, with RNDIS as an optional second configuration.
288 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
289 * the class descriptors match a modem (they're ignored; it's really just
290 * Ethernet functionality), they don't need the NOP altsetting, and the
291 * status transfer endpoint isn't optional.
294 #define STRING_MANUFACTURER 1
295 #define STRING_PRODUCT 2
296 #define STRING_ETHADDR 3
297 #define STRING_DATA 4
298 #define STRING_CONTROL 5
299 #define STRING_RNDIS_CONTROL 6
301 #define STRING_SUBSET 8
302 #define STRING_RNDIS 9
303 #define STRING_SERIALNUMBER 10
305 /* holds our biggest descriptor (or RNDIS response) */
306 #define USB_BUFSIZ 256
309 * This device advertises one configuration, eth_config, unless RNDIS
310 * is enabled (rndis_config) on hardware supporting at least two configs.
312 * NOTE: Controllers like superh_udc should probably be able to use
313 * an RNDIS-only configuration.
315 * FIXME define some higher-powered configurations to make it easier
316 * to recharge batteries ...
319 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
320 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
322 static struct usb_device_descriptor
324 .bLength = sizeof device_desc,
325 .bDescriptorType = USB_DT_DEVICE,
327 .bcdUSB = __constant_cpu_to_le16(0x0200),
329 .bDeviceClass = USB_CLASS_COMM,
330 .bDeviceSubClass = 0,
331 .bDeviceProtocol = 0,
333 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
334 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
335 .iManufacturer = STRING_MANUFACTURER,
336 .iProduct = STRING_PRODUCT,
337 .bNumConfigurations = 1,
340 static struct usb_otg_descriptor
342 .bLength = sizeof otg_descriptor,
343 .bDescriptorType = USB_DT_OTG,
345 .bmAttributes = USB_OTG_SRP,
348 static struct usb_config_descriptor
350 .bLength = sizeof eth_config,
351 .bDescriptorType = USB_DT_CONFIG,
353 /* compute wTotalLength on the fly */
355 .bConfigurationValue = DEV_CONFIG_VALUE,
356 .iConfiguration = STRING_CDC,
357 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
361 #ifdef CONFIG_USB_ETH_RNDIS
362 static struct usb_config_descriptor
364 .bLength = sizeof rndis_config,
365 .bDescriptorType = USB_DT_CONFIG,
367 /* compute wTotalLength on the fly */
369 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
370 .iConfiguration = STRING_RNDIS,
371 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
377 * Compared to the simple CDC subset, the full CDC Ethernet model adds
378 * three class descriptors, two interface descriptors, optional status
379 * endpoint. Both have a "data" interface and two bulk endpoints.
380 * There are also differences in how control requests are handled.
382 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
383 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
384 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
385 * work around those bugs) are unlikely to ever come from MSFT, you may
386 * wish to avoid using RNDIS.
388 * MCCI offers an alternative to RNDIS if you need to connect to Windows
389 * but have hardware that can't support CDC Ethernet. We add descriptors
390 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
391 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
392 * get those drivers from MCCI, or bundled with various products.
395 #ifdef CONFIG_USB_ETH_CDC
396 static struct usb_interface_descriptor
398 .bLength = sizeof control_intf,
399 .bDescriptorType = USB_DT_INTERFACE,
401 .bInterfaceNumber = 0,
402 /* status endpoint is optional; this may be patched later */
404 .bInterfaceClass = USB_CLASS_COMM,
405 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
406 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
407 .iInterface = STRING_CONTROL,
411 #ifdef CONFIG_USB_ETH_RNDIS
412 static const struct usb_interface_descriptor
413 rndis_control_intf = {
414 .bLength = sizeof rndis_control_intf,
415 .bDescriptorType = USB_DT_INTERFACE,
417 .bInterfaceNumber = 0,
419 .bInterfaceClass = USB_CLASS_COMM,
420 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
421 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
422 .iInterface = STRING_RNDIS_CONTROL,
426 static const struct usb_cdc_header_desc header_desc = {
427 .bLength = sizeof header_desc,
428 .bDescriptorType = USB_DT_CS_INTERFACE,
429 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
431 .bcdCDC = __constant_cpu_to_le16(0x0110),
434 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
436 static const struct usb_cdc_union_desc union_desc = {
437 .bLength = sizeof union_desc,
438 .bDescriptorType = USB_DT_CS_INTERFACE,
439 .bDescriptorSubType = USB_CDC_UNION_TYPE,
441 .bMasterInterface0 = 0, /* index of control interface */
442 .bSlaveInterface0 = 1, /* index of DATA interface */
445 #endif /* CDC || RNDIS */
447 #ifdef CONFIG_USB_ETH_RNDIS
449 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
450 .bLength = sizeof call_mgmt_descriptor,
451 .bDescriptorType = USB_DT_CS_INTERFACE,
452 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
454 .bmCapabilities = 0x00,
455 .bDataInterface = 0x01,
458 static const struct usb_cdc_acm_descriptor acm_descriptor = {
459 .bLength = sizeof acm_descriptor,
460 .bDescriptorType = USB_DT_CS_INTERFACE,
461 .bDescriptorSubType = USB_CDC_ACM_TYPE,
463 .bmCapabilities = 0x00,
468 #ifndef CONFIG_USB_ETH_CDC
471 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
472 * ways: data endpoints live in the control interface, there's no data
473 * interface, and it's not used to talk to a cell phone radio.
476 static const struct usb_cdc_mdlm_desc mdlm_desc = {
477 .bLength = sizeof mdlm_desc,
478 .bDescriptorType = USB_DT_CS_INTERFACE,
479 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
481 .bcdVersion = __constant_cpu_to_le16(0x0100),
483 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
484 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
489 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
490 * can't really use its struct. All we do here is say that we're using
491 * the submode of "SAFE" which directly matches the CDC Subset.
493 static const u8 mdlm_detail_desc[] = {
496 USB_CDC_MDLM_DETAIL_TYPE,
499 0, /* network control capabilities (none) */
500 0, /* network data capabilities ("raw" encapsulation) */
505 static const struct usb_cdc_ether_desc ether_desc = {
506 .bLength = sizeof(ether_desc),
507 .bDescriptorType = USB_DT_CS_INTERFACE,
508 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
510 /* this descriptor actually adds value, surprise! */
511 .iMACAddress = STRING_ETHADDR,
512 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
513 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
514 .wNumberMCFilters = __constant_cpu_to_le16(0),
515 .bNumberPowerFilters = 0,
518 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
521 * include the status endpoint if we can, even where it's optional.
522 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
523 * packet, to simplify cancellation; and a big transfer interval, to
524 * waste less bandwidth.
526 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
527 * if they ignore the connect/disconnect notifications that real aether
528 * can provide. more advanced cdc configurations might want to support
529 * encapsulated commands (vendor-specific, using control-OUT).
531 * RNDIS requires the status endpoint, since it uses that encapsulation
532 * mechanism for its funky RPC scheme.
535 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
536 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
538 static struct usb_endpoint_descriptor
540 .bLength = USB_DT_ENDPOINT_SIZE,
541 .bDescriptorType = USB_DT_ENDPOINT,
543 .bEndpointAddress = USB_DIR_IN,
544 .bmAttributes = USB_ENDPOINT_XFER_INT,
545 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
546 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
550 #ifdef CONFIG_USB_ETH_CDC
552 /* the default data interface has no endpoints ... */
554 static const struct usb_interface_descriptor
556 .bLength = sizeof data_nop_intf,
557 .bDescriptorType = USB_DT_INTERFACE,
559 .bInterfaceNumber = 1,
560 .bAlternateSetting = 0,
562 .bInterfaceClass = USB_CLASS_CDC_DATA,
563 .bInterfaceSubClass = 0,
564 .bInterfaceProtocol = 0,
567 /* ... but the "real" data interface has two bulk endpoints */
569 static const struct usb_interface_descriptor
571 .bLength = sizeof data_intf,
572 .bDescriptorType = USB_DT_INTERFACE,
574 .bInterfaceNumber = 1,
575 .bAlternateSetting = 1,
577 .bInterfaceClass = USB_CLASS_CDC_DATA,
578 .bInterfaceSubClass = 0,
579 .bInterfaceProtocol = 0,
580 .iInterface = STRING_DATA,
585 #ifdef CONFIG_USB_ETH_RNDIS
587 /* RNDIS doesn't activate by changing to the "real" altsetting */
589 static const struct usb_interface_descriptor
591 .bLength = sizeof rndis_data_intf,
592 .bDescriptorType = USB_DT_INTERFACE,
594 .bInterfaceNumber = 1,
595 .bAlternateSetting = 0,
597 .bInterfaceClass = USB_CLASS_CDC_DATA,
598 .bInterfaceSubClass = 0,
599 .bInterfaceProtocol = 0,
600 .iInterface = STRING_DATA,
605 #ifdef CONFIG_USB_ETH_SUBSET
608 * "Simple" CDC-subset option is a simple vendor-neutral model that most
609 * full speed controllers can handle: one interface, two bulk endpoints.
611 * To assist host side drivers, we fancy it up a bit, and add descriptors
612 * so some host side drivers will understand it as a "SAFE" variant.
615 static const struct usb_interface_descriptor
617 .bLength = sizeof subset_data_intf,
618 .bDescriptorType = USB_DT_INTERFACE,
620 .bInterfaceNumber = 0,
621 .bAlternateSetting = 0,
623 .bInterfaceClass = USB_CLASS_COMM,
624 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
625 .bInterfaceProtocol = 0,
626 .iInterface = STRING_DATA,
631 static struct usb_endpoint_descriptor
633 .bLength = USB_DT_ENDPOINT_SIZE,
634 .bDescriptorType = USB_DT_ENDPOINT,
636 .bEndpointAddress = USB_DIR_IN,
637 .bmAttributes = USB_ENDPOINT_XFER_BULK,
638 .wMaxPacketSize = __constant_cpu_to_le16(64),
641 static struct usb_endpoint_descriptor
643 .bLength = USB_DT_ENDPOINT_SIZE,
644 .bDescriptorType = USB_DT_ENDPOINT,
646 .bEndpointAddress = USB_DIR_OUT,
647 .bmAttributes = USB_ENDPOINT_XFER_BULK,
648 .wMaxPacketSize = __constant_cpu_to_le16(64),
651 static const struct usb_descriptor_header *fs_eth_function[11] = {
652 (struct usb_descriptor_header *) &otg_descriptor,
653 #ifdef CONFIG_USB_ETH_CDC
654 /* "cdc" mode descriptors */
655 (struct usb_descriptor_header *) &control_intf,
656 (struct usb_descriptor_header *) &header_desc,
657 (struct usb_descriptor_header *) &union_desc,
658 (struct usb_descriptor_header *) ðer_desc,
659 /* NOTE: status endpoint may need to be removed */
660 (struct usb_descriptor_header *) &fs_status_desc,
661 /* data interface, with altsetting */
662 (struct usb_descriptor_header *) &data_nop_intf,
663 (struct usb_descriptor_header *) &data_intf,
664 (struct usb_descriptor_header *) &fs_source_desc,
665 (struct usb_descriptor_header *) &fs_sink_desc,
667 #endif /* CONFIG_USB_ETH_CDC */
670 static inline void fs_subset_descriptors(void)
672 #ifdef CONFIG_USB_ETH_SUBSET
673 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
674 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
675 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
676 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
677 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
678 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
679 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
680 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
681 fs_eth_function[8] = NULL;
683 fs_eth_function[1] = NULL;
687 #ifdef CONFIG_USB_ETH_RNDIS
688 static const struct usb_descriptor_header *fs_rndis_function[] = {
689 (struct usb_descriptor_header *) &otg_descriptor,
690 /* control interface matches ACM, not Ethernet */
691 (struct usb_descriptor_header *) &rndis_control_intf,
692 (struct usb_descriptor_header *) &header_desc,
693 (struct usb_descriptor_header *) &call_mgmt_descriptor,
694 (struct usb_descriptor_header *) &acm_descriptor,
695 (struct usb_descriptor_header *) &union_desc,
696 (struct usb_descriptor_header *) &fs_status_desc,
697 /* data interface has no altsetting */
698 (struct usb_descriptor_header *) &rndis_data_intf,
699 (struct usb_descriptor_header *) &fs_source_desc,
700 (struct usb_descriptor_header *) &fs_sink_desc,
706 * usb 2.0 devices need to expose both high speed and full speed
707 * descriptors, unless they only run at full speed.
710 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
711 static struct usb_endpoint_descriptor
713 .bLength = USB_DT_ENDPOINT_SIZE,
714 .bDescriptorType = USB_DT_ENDPOINT,
716 .bmAttributes = USB_ENDPOINT_XFER_INT,
717 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
718 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
720 #endif /* CONFIG_USB_ETH_CDC */
722 static struct usb_endpoint_descriptor
724 .bLength = USB_DT_ENDPOINT_SIZE,
725 .bDescriptorType = USB_DT_ENDPOINT,
727 .bmAttributes = USB_ENDPOINT_XFER_BULK,
728 .wMaxPacketSize = __constant_cpu_to_le16(512),
731 static struct usb_endpoint_descriptor
733 .bLength = USB_DT_ENDPOINT_SIZE,
734 .bDescriptorType = USB_DT_ENDPOINT,
736 .bmAttributes = USB_ENDPOINT_XFER_BULK,
737 .wMaxPacketSize = __constant_cpu_to_le16(512),
740 static struct usb_qualifier_descriptor
742 .bLength = sizeof dev_qualifier,
743 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
745 .bcdUSB = __constant_cpu_to_le16(0x0200),
746 .bDeviceClass = USB_CLASS_COMM,
748 .bNumConfigurations = 1,
751 static const struct usb_descriptor_header *hs_eth_function[11] = {
752 (struct usb_descriptor_header *) &otg_descriptor,
753 #ifdef CONFIG_USB_ETH_CDC
754 /* "cdc" mode descriptors */
755 (struct usb_descriptor_header *) &control_intf,
756 (struct usb_descriptor_header *) &header_desc,
757 (struct usb_descriptor_header *) &union_desc,
758 (struct usb_descriptor_header *) ðer_desc,
759 /* NOTE: status endpoint may need to be removed */
760 (struct usb_descriptor_header *) &hs_status_desc,
761 /* data interface, with altsetting */
762 (struct usb_descriptor_header *) &data_nop_intf,
763 (struct usb_descriptor_header *) &data_intf,
764 (struct usb_descriptor_header *) &hs_source_desc,
765 (struct usb_descriptor_header *) &hs_sink_desc,
767 #endif /* CONFIG_USB_ETH_CDC */
770 static inline void hs_subset_descriptors(void)
772 #ifdef CONFIG_USB_ETH_SUBSET
773 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
774 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
775 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
776 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
777 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
778 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
779 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
780 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
781 hs_eth_function[8] = NULL;
783 hs_eth_function[1] = NULL;
787 #ifdef CONFIG_USB_ETH_RNDIS
788 static const struct usb_descriptor_header *hs_rndis_function[] = {
789 (struct usb_descriptor_header *) &otg_descriptor,
790 /* control interface matches ACM, not Ethernet */
791 (struct usb_descriptor_header *) &rndis_control_intf,
792 (struct usb_descriptor_header *) &header_desc,
793 (struct usb_descriptor_header *) &call_mgmt_descriptor,
794 (struct usb_descriptor_header *) &acm_descriptor,
795 (struct usb_descriptor_header *) &union_desc,
796 (struct usb_descriptor_header *) &hs_status_desc,
797 /* data interface has no altsetting */
798 (struct usb_descriptor_header *) &rndis_data_intf,
799 (struct usb_descriptor_header *) &hs_source_desc,
800 (struct usb_descriptor_header *) &hs_sink_desc,
806 /* maxpacket and other transfer characteristics vary by speed. */
807 static inline struct usb_endpoint_descriptor *
808 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
809 struct usb_endpoint_descriptor *fs)
811 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
816 /*-------------------------------------------------------------------------*/
818 /* descriptors that are built on-demand */
820 static char manufacturer[50];
821 static char product_desc[40] = DRIVER_DESC;
822 static char serial_number[20];
824 /* address that the host will use ... usually assigned at random */
825 static char ethaddr[2 * ETH_ALEN + 1];
827 /* static strings, in UTF-8 */
828 static struct usb_string strings[] = {
829 { STRING_MANUFACTURER, manufacturer, },
830 { STRING_PRODUCT, product_desc, },
831 { STRING_SERIALNUMBER, serial_number, },
832 { STRING_DATA, "Ethernet Data", },
833 { STRING_ETHADDR, ethaddr, },
834 #ifdef CONFIG_USB_ETH_CDC
835 { STRING_CDC, "CDC Ethernet", },
836 { STRING_CONTROL, "CDC Communications Control", },
838 #ifdef CONFIG_USB_ETH_SUBSET
839 { STRING_SUBSET, "CDC Ethernet Subset", },
841 #ifdef CONFIG_USB_ETH_RNDIS
842 { STRING_RNDIS, "RNDIS", },
843 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
845 { } /* end of list */
848 static struct usb_gadget_strings stringtab = {
849 .language = 0x0409, /* en-us */
853 /*============================================================================*/
854 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
856 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
857 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
862 * strlcpy - Copy a %NUL terminated string into a sized buffer
863 * @dest: Where to copy the string to
864 * @src: Where to copy the string from
865 * @size: size of destination buffer
867 * Compatible with *BSD: the result is always a valid
868 * NUL-terminated string that fits in the buffer (unless,
869 * of course, the buffer size is zero). It does not pad
870 * out the result like strncpy() does.
872 size_t strlcpy(char *dest, const char *src, size_t size)
874 size_t ret = strlen(src);
877 size_t len = (ret >= size) ? size - 1 : ret;
878 memcpy(dest, src, len);
884 /*============================================================================*/
887 * one config, two interfaces: control, data.
888 * complications: class descriptors, and an altsetting.
891 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
894 const struct usb_config_descriptor *config;
895 const struct usb_descriptor_header **function;
898 if (gadget_is_dualspeed(g)) {
899 hs = (g->speed == USB_SPEED_HIGH);
900 if (type == USB_DT_OTHER_SPEED_CONFIG)
903 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
905 if (index >= device_desc.bNumConfigurations)
908 #ifdef CONFIG_USB_ETH_RNDIS
910 * list the RNDIS config first, to make Microsoft's drivers
911 * happy. DOCSIS 1.0 needs this too.
913 if (device_desc.bNumConfigurations == 2 && index == 0) {
914 config = &rndis_config;
915 function = which_fn(rndis);
919 config = ð_config;
920 function = which_fn(eth);
923 /* for now, don't advertise srp-only devices */
927 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
930 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
934 /*-------------------------------------------------------------------------*/
936 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
937 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
940 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
943 struct usb_gadget *gadget = dev->gadget;
945 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
946 /* status endpoint used for RNDIS and (optionally) CDC */
947 if (!subset_active(dev) && dev->status_ep) {
948 dev->status = ep_desc(gadget, &hs_status_desc,
950 dev->status_ep->driver_data = dev;
952 result = usb_ep_enable(dev->status_ep, dev->status);
954 debug("enable %s --> %d\n",
955 dev->status_ep->name, result);
961 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
962 dev->in_ep->driver_data = dev;
964 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
965 dev->out_ep->driver_data = dev;
968 * With CDC, the host isn't allowed to use these two data
969 * endpoints in the default altsetting for the interface.
970 * so we don't activate them yet. Reset from SET_INTERFACE.
972 * Strictly speaking RNDIS should work the same: activation is
973 * a side effect of setting a packet filter. Deactivation is
974 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
976 if (!cdc_active(dev)) {
977 result = usb_ep_enable(dev->in_ep, dev->in);
979 debug("enable %s --> %d\n",
980 dev->in_ep->name, result);
984 result = usb_ep_enable(dev->out_ep, dev->out);
986 debug("enable %s --> %d\n",
987 dev->out_ep->name, result);
994 result = alloc_requests(dev, qlen(gadget), gfp_flags);
996 /* on error, disable any endpoints */
998 if (!subset_active(dev) && dev->status_ep)
999 (void) usb_ep_disable(dev->status_ep);
1001 (void) usb_ep_disable(dev->in_ep);
1002 (void) usb_ep_disable(dev->out_ep);
1005 } else if (!cdc_active(dev)) {
1007 * activate non-CDC configs right away
1008 * this isn't strictly according to the RNDIS spec
1010 eth_start(dev, GFP_ATOMIC);
1013 /* caller is responsible for cleanup on error */
1017 static void eth_reset_config(struct eth_dev *dev)
1019 if (dev->config == 0)
1022 debug("%s\n", __func__);
1024 rndis_uninit(dev->rndis_config);
1027 * disable endpoints, forcing (synchronous) completion of
1028 * pending i/o. then free the requests.
1032 usb_ep_disable(dev->in_ep);
1034 usb_ep_free_request(dev->in_ep, dev->tx_req);
1039 usb_ep_disable(dev->out_ep);
1041 usb_ep_free_request(dev->out_ep, dev->rx_req);
1046 usb_ep_disable(dev->status_ep);
1049 dev->cdc_filter = 0;
1054 * change our operational config. must agree with the code
1055 * that returns config descriptors, and altsetting code.
1057 static int eth_set_config(struct eth_dev *dev, unsigned number,
1061 struct usb_gadget *gadget = dev->gadget;
1063 if (gadget_is_sa1100(gadget)
1065 && dev->tx_qlen != 0) {
1066 /* tx fifo is full, but we can't clear it...*/
1067 error("can't change configurations");
1070 eth_reset_config(dev);
1073 case DEV_CONFIG_VALUE:
1074 result = set_ether_config(dev, gfp_flags);
1076 #ifdef CONFIG_USB_ETH_RNDIS
1077 case DEV_RNDIS_CONFIG_VALUE:
1079 result = set_ether_config(dev, gfp_flags);
1091 eth_reset_config(dev);
1092 usb_gadget_vbus_draw(dev->gadget,
1093 gadget_is_otg(dev->gadget) ? 8 : 100);
1098 power = 2 * eth_config.bMaxPower;
1099 usb_gadget_vbus_draw(dev->gadget, power);
1101 switch (gadget->speed) {
1102 case USB_SPEED_FULL:
1103 speed = "full"; break;
1104 #ifdef CONFIG_USB_GADGET_DUALSPEED
1105 case USB_SPEED_HIGH:
1106 speed = "high"; break;
1112 dev->config = number;
1113 printf("%s speed config #%d: %d mA, %s, using %s\n",
1114 speed, number, power, driver_desc,
1119 : "CDC Ethernet Subset"));
1124 /*-------------------------------------------------------------------------*/
1126 #ifdef CONFIG_USB_ETH_CDC
1129 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1130 * only to notify the host about link status changes (which we support) or
1131 * report completion of some encapsulated command (as used in RNDIS). Since
1132 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1133 * command mechanism; and only one status request is ever queued.
1135 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1137 struct usb_cdc_notification *event = req->buf;
1138 int value = req->status;
1139 struct eth_dev *dev = ep->driver_data;
1141 /* issue the second notification if host reads the first */
1142 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1144 __le32 *data = req->buf + sizeof *event;
1146 event->bmRequestType = 0xA1;
1147 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1148 event->wValue = __constant_cpu_to_le16(0);
1149 event->wIndex = __constant_cpu_to_le16(1);
1150 event->wLength = __constant_cpu_to_le16(8);
1152 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1153 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1155 req->length = STATUS_BYTECOUNT;
1156 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1157 debug("send SPEED_CHANGE --> %d\n", value);
1160 } else if (value != -ECONNRESET) {
1161 debug("event %02x --> %d\n",
1162 event->bNotificationType, value);
1163 if (event->bNotificationType ==
1164 USB_CDC_NOTIFY_SPEED_CHANGE) {
1165 l_ethdev.network_started = 1;
1166 printf("USB network up!\n");
1169 req->context = NULL;
1172 static void issue_start_status(struct eth_dev *dev)
1174 struct usb_request *req = dev->stat_req;
1175 struct usb_cdc_notification *event;
1181 * FIXME ugly idiom, maybe we'd be better with just
1182 * a "cancel the whole queue" primitive since any
1183 * unlink-one primitive has way too many error modes.
1184 * here, we "know" toggle is already clear...
1186 * FIXME iff req->context != null just dequeue it
1188 usb_ep_disable(dev->status_ep);
1189 usb_ep_enable(dev->status_ep, dev->status);
1192 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1193 * a SPEED_CHANGE. could be useful in some configs.
1196 event->bmRequestType = 0xA1;
1197 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1198 event->wValue = __constant_cpu_to_le16(1); /* connected */
1199 event->wIndex = __constant_cpu_to_le16(1);
1202 req->length = sizeof *event;
1203 req->complete = eth_status_complete;
1206 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1208 debug("status buf queue --> %d\n", value);
1213 /*-------------------------------------------------------------------------*/
1215 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1217 if (req->status || req->actual != req->length)
1218 debug("setup complete --> %d, %d/%d\n",
1219 req->status, req->actual, req->length);
1222 #ifdef CONFIG_USB_ETH_RNDIS
1224 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1226 if (req->status || req->actual != req->length)
1227 debug("rndis response complete --> %d, %d/%d\n",
1228 req->status, req->actual, req->length);
1230 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1233 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1235 struct eth_dev *dev = ep->driver_data;
1238 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1239 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1241 error("%s: rndis parse error %d", __func__, status);
1247 * The setup() callback implements all the ep0 functionality that's not
1248 * handled lower down. CDC has a number of less-common features:
1250 * - two interfaces: control, and ethernet data
1251 * - Ethernet data interface has two altsettings: default, and active
1252 * - class-specific descriptors for the control interface
1253 * - class-specific control requests
1256 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1258 struct eth_dev *dev = get_gadget_data(gadget);
1259 struct usb_request *req = dev->req;
1260 int value = -EOPNOTSUPP;
1261 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1262 u16 wValue = le16_to_cpu(ctrl->wValue);
1263 u16 wLength = le16_to_cpu(ctrl->wLength);
1266 * descriptors just go into the pre-allocated ep0 buffer,
1267 * while config change events may enable network traffic.
1270 debug("%s\n", __func__);
1272 req->complete = eth_setup_complete;
1273 switch (ctrl->bRequest) {
1275 case USB_REQ_GET_DESCRIPTOR:
1276 if (ctrl->bRequestType != USB_DIR_IN)
1278 switch (wValue >> 8) {
1281 value = min(wLength, (u16) sizeof device_desc);
1282 memcpy(req->buf, &device_desc, value);
1284 case USB_DT_DEVICE_QUALIFIER:
1285 if (!gadget_is_dualspeed(gadget))
1287 value = min(wLength, (u16) sizeof dev_qualifier);
1288 memcpy(req->buf, &dev_qualifier, value);
1291 case USB_DT_OTHER_SPEED_CONFIG:
1292 if (!gadget_is_dualspeed(gadget))
1296 value = config_buf(gadget, req->buf,
1299 gadget_is_otg(gadget));
1301 value = min(wLength, (u16) value);
1305 value = usb_gadget_get_string(&stringtab,
1306 wValue & 0xff, req->buf);
1309 value = min(wLength, (u16) value);
1315 case USB_REQ_SET_CONFIGURATION:
1316 if (ctrl->bRequestType != 0)
1318 if (gadget->a_hnp_support)
1319 debug("HNP available\n");
1320 else if (gadget->a_alt_hnp_support)
1321 debug("HNP needs a different root port\n");
1322 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1324 case USB_REQ_GET_CONFIGURATION:
1325 if (ctrl->bRequestType != USB_DIR_IN)
1327 *(u8 *)req->buf = dev->config;
1328 value = min(wLength, (u16) 1);
1331 case USB_REQ_SET_INTERFACE:
1332 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1336 if (!cdc_active(dev) && wIndex != 0)
1340 * PXA hardware partially handles SET_INTERFACE;
1341 * we need to kluge around that interference.
1343 if (gadget_is_pxa(gadget)) {
1344 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1347 * PXA25x driver use non-CDC ethernet gadget.
1348 * But only _CDC and _RNDIS code can signalize
1349 * that network is working. So we signalize it
1352 l_ethdev.network_started = 1;
1353 debug("USB network up!\n");
1357 #ifdef CONFIG_USB_ETH_CDC
1359 case 0: /* control/master intf */
1363 usb_ep_disable(dev->status_ep);
1364 usb_ep_enable(dev->status_ep, dev->status);
1369 case 1: /* data intf */
1372 usb_ep_disable(dev->in_ep);
1373 usb_ep_disable(dev->out_ep);
1376 * CDC requires the data transfers not be done from
1377 * the default interface setting ... also, setting
1378 * the non-default interface resets filters etc.
1381 if (!cdc_active(dev))
1383 usb_ep_enable(dev->in_ep, dev->in);
1384 usb_ep_enable(dev->out_ep, dev->out);
1385 dev->cdc_filter = DEFAULT_FILTER;
1387 issue_start_status(dev);
1388 eth_start(dev, GFP_ATOMIC);
1395 * FIXME this is wrong, as is the assumption that
1396 * all non-PXA hardware talks real CDC ...
1398 debug("set_interface ignored!\n");
1399 #endif /* CONFIG_USB_ETH_CDC */
1403 case USB_REQ_GET_INTERFACE:
1404 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1408 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1411 /* for CDC, iff carrier is on, data interface is active. */
1412 if (rndis_active(dev) || wIndex != 1)
1413 *(u8 *)req->buf = 0;
1415 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1416 /* carrier always ok ...*/
1417 *(u8 *)req->buf = 1 ;
1419 value = min(wLength, (u16) 1);
1422 #ifdef CONFIG_USB_ETH_CDC
1423 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1425 * see 6.2.30: no data, wIndex = interface,
1426 * wValue = packet filter bitmap
1428 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1433 debug("packet filter %02x\n", wValue);
1434 dev->cdc_filter = wValue;
1440 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1441 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1442 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1443 * case USB_CDC_GET_ETHERNET_STATISTIC:
1446 #endif /* CONFIG_USB_ETH_CDC */
1448 #ifdef CONFIG_USB_ETH_RNDIS
1450 * RNDIS uses the CDC command encapsulation mechanism to implement
1451 * an RPC scheme, with much getting/setting of attributes by OID.
1453 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1454 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1455 || !rndis_active(dev)
1456 || wLength > USB_BUFSIZ
1458 || rndis_control_intf.bInterfaceNumber
1461 /* read the request, then process it */
1463 req->complete = rndis_command_complete;
1464 /* later, rndis_control_ack () sends a notification */
1467 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1468 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1469 == ctrl->bRequestType
1470 && rndis_active(dev)
1471 /* && wLength >= 0x0400 */
1473 && rndis_control_intf.bInterfaceNumber
1478 /* return the result */
1479 buf = rndis_get_next_response(dev->rndis_config, &n);
1481 memcpy(req->buf, buf, n);
1482 req->complete = rndis_response_complete;
1483 rndis_free_response(dev->rndis_config, buf);
1486 /* else stalls ... spec says to avoid that */
1492 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1493 ctrl->bRequestType, ctrl->bRequest,
1494 wValue, wIndex, wLength);
1497 /* respond with data transfer before status phase? */
1499 debug("respond with data transfer before status phase\n");
1500 req->length = value;
1501 req->zero = value < wLength
1502 && (value % gadget->ep0->maxpacket) == 0;
1503 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1505 debug("ep_queue --> %d\n", value);
1507 eth_setup_complete(gadget->ep0, req);
1511 /* host either stalls (value < 0) or reports success */
1515 /*-------------------------------------------------------------------------*/
1517 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1519 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1522 int retval = -ENOMEM;
1526 * Padding up to RX_EXTRA handles minor disagreements with host.
1527 * Normally we use the USB "terminate on short read" convention;
1528 * so allow up to (N*maxpacket), since that memory is normally
1529 * already allocated. Some hardware doesn't deal well with short
1530 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1531 * byte off the end (to force hardware errors on overflow).
1533 * RNDIS uses internal framing, and explicitly allows senders to
1534 * pad to end-of-packet. That's potentially nice for speed,
1535 * but means receivers can't recover synch on their own.
1538 debug("%s\n", __func__);
1540 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1541 size += dev->out_ep->maxpacket - 1;
1542 if (rndis_active(dev))
1543 size += sizeof(struct rndis_packet_msg_type);
1544 size -= size % dev->out_ep->maxpacket;
1547 * Some platforms perform better when IP packets are aligned,
1548 * but on at least one, checksumming fails otherwise. Note:
1549 * RNDIS headers involve variable numbers of LE32 values.
1552 req->buf = (u8 *) NetRxPackets[0];
1554 req->complete = rx_complete;
1556 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1559 error("rx submit --> %d", retval);
1564 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1566 struct eth_dev *dev = ep->driver_data;
1568 debug("%s: status %d\n", __func__, req->status);
1569 switch (req->status) {
1570 /* normal completion */
1572 if (rndis_active(dev)) {
1573 /* we know MaxPacketsPerTransfer == 1 here */
1574 int length = rndis_rm_hdr(req->buf, req->actual);
1577 req->length -= length;
1578 req->actual -= length;
1580 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1582 dev->stats.rx_errors++;
1583 dev->stats.rx_length_errors++;
1584 debug("rx length %d\n", req->length);
1588 dev->stats.rx_packets++;
1589 dev->stats.rx_bytes += req->length;
1592 /* software-driven interface shutdown */
1593 case -ECONNRESET: /* unlink */
1594 case -ESHUTDOWN: /* disconnect etc */
1595 /* for hardware automagic (such as pxa) */
1596 case -ECONNABORTED: /* endpoint reset */
1601 dev->stats.rx_over_errors++;
1604 dev->stats.rx_errors++;
1608 packet_received = 1;
1611 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1614 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1619 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1627 usb_ep_free_request(dev->in_ep, dev->tx_req);
1629 error("can't alloc requests");
1633 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1635 struct eth_dev *dev = ep->driver_data;
1637 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1638 switch (req->status) {
1640 dev->stats.tx_errors++;
1641 debug("tx err %d\n", req->status);
1643 case -ECONNRESET: /* unlink */
1644 case -ESHUTDOWN: /* disconnect etc */
1647 dev->stats.tx_bytes += req->length;
1649 dev->stats.tx_packets++;
1654 static inline int eth_is_promisc(struct eth_dev *dev)
1656 /* no filters for the CDC subset; always promisc */
1657 if (subset_active(dev))
1659 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1663 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1665 struct eth_dev *dev = netdev_priv(net);
1666 int length = skb->len;
1668 struct usb_request *req = NULL;
1669 unsigned long flags;
1671 /* apply outgoing CDC or RNDIS filters */
1672 if (!eth_is_promisc (dev)) {
1673 u8 *dest = skb->data;
1675 if (is_multicast_ether_addr(dest)) {
1678 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1679 * SET_ETHERNET_MULTICAST_FILTERS requests
1681 if (is_broadcast_ether_addr(dest))
1682 type = USB_CDC_PACKET_TYPE_BROADCAST;
1684 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1685 if (!(dev->cdc_filter & type)) {
1686 dev_kfree_skb_any (skb);
1690 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1693 spin_lock_irqsave(&dev->req_lock, flags);
1695 * this freelist can be empty if an interrupt triggered disconnect()
1696 * and reconfigured the gadget (shutting down this queue) after the
1697 * network stack decided to xmit but before we got the spinlock.
1699 if (list_empty(&dev->tx_reqs)) {
1700 spin_unlock_irqrestore(&dev->req_lock, flags);
1704 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1705 list_del (&req->list);
1707 /* temporarily stop TX queue when the freelist empties */
1708 if (list_empty (&dev->tx_reqs))
1709 netif_stop_queue (net);
1710 spin_unlock_irqrestore(&dev->req_lock, flags);
1712 /* no buffer copies needed, unless the network stack did it
1713 * or the hardware can't use skb buffers.
1714 * or there's not enough space for any RNDIS headers we need
1716 if (rndis_active(dev)) {
1717 struct sk_buff *skb_rndis;
1719 skb_rndis = skb_realloc_headroom (skb,
1720 sizeof (struct rndis_packet_msg_type));
1724 dev_kfree_skb_any (skb);
1726 rndis_add_hdr (skb);
1729 req->buf = skb->data;
1731 req->complete = tx_complete;
1733 /* use zlp framing on tx for strict CDC-Ether conformance,
1734 * though any robust network rx path ignores extra padding.
1735 * and some hardware doesn't like to write zlps.
1738 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1741 req->length = length;
1743 /* throttle highspeed IRQ rate back slightly */
1744 if (gadget_is_dualspeed(dev->gadget))
1745 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1746 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1749 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1752 DEBUG (dev, "tx queue err %d\n", retval);
1755 net->trans_start = jiffies;
1756 atomic_inc (&dev->tx_qlen);
1761 dev->stats.tx_dropped++;
1762 dev_kfree_skb_any (skb);
1763 spin_lock_irqsave(&dev->req_lock, flags);
1764 if (list_empty (&dev->tx_reqs))
1765 netif_start_queue (net);
1766 list_add (&req->list, &dev->tx_reqs);
1767 spin_unlock_irqrestore(&dev->req_lock, flags);
1772 /*-------------------------------------------------------------------------*/
1775 static void eth_unbind(struct usb_gadget *gadget)
1777 struct eth_dev *dev = get_gadget_data(gadget);
1779 debug("%s...\n", __func__);
1780 rndis_deregister(dev->rndis_config);
1783 /* we've already been disconnected ... no i/o is active */
1785 usb_ep_free_request(gadget->ep0, dev->req);
1788 if (dev->stat_req) {
1789 usb_ep_free_request(dev->status_ep, dev->stat_req);
1790 dev->stat_req = NULL;
1794 usb_ep_free_request(dev->in_ep, dev->tx_req);
1799 usb_ep_free_request(dev->out_ep, dev->rx_req);
1803 /* unregister_netdev (dev->net);*/
1804 /* free_netdev(dev->net);*/
1807 set_gadget_data(gadget, NULL);
1810 static void eth_disconnect(struct usb_gadget *gadget)
1812 eth_reset_config(get_gadget_data(gadget));
1813 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1816 static void eth_suspend(struct usb_gadget *gadget)
1821 static void eth_resume(struct usb_gadget *gadget)
1826 /*-------------------------------------------------------------------------*/
1828 #ifdef CONFIG_USB_ETH_RNDIS
1831 * The interrupt endpoint is used in RNDIS to notify the host when messages
1832 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1833 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1834 * REMOTE_NDIS_KEEPALIVE_MSG.
1836 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1837 * normally just one notification will be queued.
1840 static void rndis_control_ack_complete(struct usb_ep *ep,
1841 struct usb_request *req)
1843 struct eth_dev *dev = ep->driver_data;
1845 debug("%s...\n", __func__);
1846 if (req->status || req->actual != req->length)
1847 debug("rndis control ack complete --> %d, %d/%d\n",
1848 req->status, req->actual, req->length);
1850 if (!l_ethdev.network_started) {
1851 if (rndis_get_state(dev->rndis_config)
1852 == RNDIS_DATA_INITIALIZED) {
1853 l_ethdev.network_started = 1;
1854 printf("USB RNDIS network up!\n");
1858 req->context = NULL;
1860 if (req != dev->stat_req)
1861 usb_ep_free_request(ep, req);
1864 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1866 static int rndis_control_ack(struct eth_device *net)
1868 struct eth_dev *dev = &l_ethdev;
1870 struct usb_request *resp = dev->stat_req;
1872 /* in case RNDIS calls this after disconnect */
1874 debug("status ENODEV\n");
1878 /* in case queue length > 1 */
1879 if (resp->context) {
1880 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1883 resp->buf = rndis_resp_buf;
1887 * Send RNDIS RESPONSE_AVAILABLE notification;
1888 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1891 resp->complete = rndis_control_ack_complete;
1892 resp->context = dev;
1894 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1895 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1897 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1900 rndis_control_ack_complete(dev->status_ep, resp);
1908 #define rndis_control_ack NULL
1912 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1914 if (rndis_active(dev)) {
1915 rndis_set_param_medium(dev->rndis_config,
1917 BITRATE(dev->gadget)/100);
1918 rndis_signal_connect(dev->rndis_config);
1922 static int eth_stop(struct eth_dev *dev)
1924 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1926 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1929 if (rndis_active(dev)) {
1930 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1931 rndis_signal_disconnect(dev->rndis_config);
1933 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1934 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1936 while (get_timer(ts) < timeout)
1937 usb_gadget_handle_interrupts();
1940 rndis_uninit(dev->rndis_config);
1947 /*-------------------------------------------------------------------------*/
1949 static int is_eth_addr_valid(char *str)
1951 if (strlen(str) == 17) {
1956 /* see if it looks like an ethernet address */
1960 for (i = 0; i < 6; i++) {
1961 char term = (i == 5 ? '\0' : ':');
1963 ea[i] = simple_strtol(p, &q, 16);
1965 if ((q - p) != 2 || *q++ != term)
1971 /* Now check the contents. */
1972 return is_valid_ether_addr(ea);
1977 static u8 nibble(unsigned char c)
1979 if (likely(isdigit(c)))
1982 if (likely(isxdigit(c)))
1983 return 10 + c - 'A';
1987 static int get_ether_addr(const char *str, u8 *dev_addr)
1992 for (i = 0; i < 6; i++) {
1995 if ((*str == '.') || (*str == ':'))
1997 num = nibble(*str++) << 4;
1998 num |= (nibble(*str++));
2001 if (is_valid_ether_addr(dev_addr))
2007 static int eth_bind(struct usb_gadget *gadget)
2009 struct eth_dev *dev = &l_ethdev;
2010 u8 cdc = 1, zlp = 1, rndis = 1;
2011 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2012 int status = -ENOMEM;
2016 /* these flags are only ever cleared; compiler take note */
2017 #ifndef CONFIG_USB_ETH_CDC
2020 #ifndef CONFIG_USB_ETH_RNDIS
2024 * Because most host side USB stacks handle CDC Ethernet, that
2025 * standard protocol is _strongly_ preferred for interop purposes.
2026 * (By everyone except Microsoft.)
2028 if (gadget_is_pxa(gadget)) {
2029 /* pxa doesn't support altsettings */
2031 } else if (gadget_is_musbhdrc(gadget)) {
2032 /* reduce tx dma overhead by avoiding special cases */
2034 } else if (gadget_is_sh(gadget)) {
2035 /* sh doesn't support multiple interfaces or configs */
2038 } else if (gadget_is_sa1100(gadget)) {
2039 /* hardware can't write zlps */
2042 * sa1100 CAN do CDC, without status endpoint ... we use
2043 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2048 gcnum = usb_gadget_controller_number(gadget);
2050 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2053 * can't assume CDC works. don't want to default to
2054 * anything less functional on CDC-capable hardware,
2055 * so we fail in this case.
2057 error("controller '%s' not recognized",
2063 * If there's an RNDIS configuration, that's what Windows wants to
2064 * be using ... so use these product IDs here and in the "linux.inf"
2065 * needed to install MSFT drivers. Current Linux kernels will use
2066 * the second configuration if it's CDC Ethernet, and need some help
2067 * to choose the right configuration otherwise.
2070 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2071 device_desc.idVendor =
2072 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2073 device_desc.idProduct =
2074 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2076 device_desc.idVendor =
2077 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2078 device_desc.idProduct =
2079 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2081 sprintf(product_desc, "RNDIS/%s", driver_desc);
2084 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2085 * drivers aren't widely available. (That may be improved by
2086 * supporting one submode of the "SAFE" variant of MDLM.)
2089 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2090 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2091 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2094 device_desc.idVendor =
2095 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2096 device_desc.idProduct =
2097 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2101 /* support optional vendor/distro customization */
2103 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2105 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2107 strlcpy(product_desc, iProduct, sizeof product_desc);
2108 if (iSerialNumber) {
2109 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2110 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2113 /* all we really need is bulk IN/OUT */
2114 usb_ep_autoconfig_reset(gadget);
2115 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2118 error("can't autoconfigure on %s\n",
2122 in_ep->driver_data = in_ep; /* claim */
2124 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2127 out_ep->driver_data = out_ep; /* claim */
2129 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2131 * CDC Ethernet control interface doesn't require a status endpoint.
2132 * Since some hosts expect one, try to allocate one anyway.
2135 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2137 status_ep->driver_data = status_ep; /* claim */
2139 error("can't run RNDIS on %s", gadget->name);
2141 #ifdef CONFIG_USB_ETH_CDC
2143 control_intf.bNumEndpoints = 0;
2144 /* FIXME remove endpoint from descriptor list */
2150 /* one config: cdc, else minimal subset */
2152 eth_config.bNumInterfaces = 1;
2153 eth_config.iConfiguration = STRING_SUBSET;
2156 * use functions to set these up, in case we're built to work
2157 * with multiple controllers and must override CDC Ethernet.
2159 fs_subset_descriptors();
2160 hs_subset_descriptors();
2163 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2164 usb_gadget_set_selfpowered(gadget);
2166 /* For now RNDIS is always a second config */
2168 device_desc.bNumConfigurations = 2;
2170 if (gadget_is_dualspeed(gadget)) {
2172 dev_qualifier.bNumConfigurations = 2;
2174 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2176 /* assumes ep0 uses the same value for both speeds ... */
2177 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2179 /* and that all endpoints are dual-speed */
2180 hs_source_desc.bEndpointAddress =
2181 fs_source_desc.bEndpointAddress;
2182 hs_sink_desc.bEndpointAddress =
2183 fs_sink_desc.bEndpointAddress;
2184 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2186 hs_status_desc.bEndpointAddress =
2187 fs_status_desc.bEndpointAddress;
2191 if (gadget_is_otg(gadget)) {
2192 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2193 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2194 eth_config.bMaxPower = 4;
2195 #ifdef CONFIG_USB_ETH_RNDIS
2196 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2197 rndis_config.bMaxPower = 4;
2202 /* network device setup */
2203 dev->net = &l_netdev;
2209 dev->out_ep = out_ep;
2210 dev->status_ep = status_ep;
2213 * Module params for these addresses should come from ID proms.
2214 * The host side address is used with CDC and RNDIS, and commonly
2215 * ends up in a persistent config database. It's not clear if
2216 * host side code for the SAFE thing cares -- its original BLAN
2217 * thing didn't, Sharp never assigned those addresses on Zaurii.
2219 get_ether_addr(dev_addr, dev->net->enetaddr);
2221 memset(tmp, 0, sizeof(tmp));
2222 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2224 get_ether_addr(host_addr, dev->host_mac);
2226 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2227 dev->host_mac[0], dev->host_mac[1],
2228 dev->host_mac[2], dev->host_mac[3],
2229 dev->host_mac[4], dev->host_mac[5]);
2232 status = rndis_init();
2234 error("can't init RNDIS, %d", status);
2240 * use PKTSIZE (or aligned... from u-boot) and set
2241 * wMaxSegmentSize accordingly
2243 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2245 /* preallocate control message data and buffer */
2246 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2249 dev->req->buf = control_req;
2250 dev->req->complete = eth_setup_complete;
2252 /* ... and maybe likewise for status transfer */
2253 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2254 if (dev->status_ep) {
2255 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2257 if (!dev->stat_req) {
2258 usb_ep_free_request(dev->status_ep, dev->req);
2262 dev->stat_req->buf = status_req;
2263 dev->stat_req->context = NULL;
2267 /* finish hookup to lower layer ... */
2268 dev->gadget = gadget;
2269 set_gadget_data(gadget, dev);
2270 gadget->ep0->driver_data = dev;
2273 * two kinds of host-initiated state changes:
2274 * - iff DATA transfer is active, carrier is "on"
2275 * - tx queueing enabled if open *and* carrier is "on"
2278 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2279 out_ep->name, in_ep->name,
2280 status_ep ? " STATUS " : "",
2281 status_ep ? status_ep->name : ""
2283 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2284 dev->net->enetaddr[0], dev->net->enetaddr[1],
2285 dev->net->enetaddr[2], dev->net->enetaddr[3],
2286 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2289 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2290 dev->host_mac[0], dev->host_mac[1],
2291 dev->host_mac[2], dev->host_mac[3],
2292 dev->host_mac[4], dev->host_mac[5]);
2297 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2299 dev->rndis_config = rndis_register(rndis_control_ack);
2300 if (dev->rndis_config < 0) {
2303 debug("RNDIS setup failed\n");
2308 /* these set up a lot of the OIDs that RNDIS needs */
2309 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2310 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2311 &dev->stats, &dev->cdc_filter))
2313 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2316 if (rndis_set_param_medium(dev->rndis_config,
2317 NDIS_MEDIUM_802_3, 0))
2319 printf("RNDIS ready\n");
2324 error("%s failed, status = %d", __func__, status);
2329 /*-------------------------------------------------------------------------*/
2331 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2333 struct eth_dev *dev = &l_ethdev;
2334 struct usb_gadget *gadget;
2336 unsigned long timeout = USB_CONNECT_TIMEOUT;
2339 error("received NULL ptr");
2343 /* Configure default mac-addresses for the USB ethernet device */
2344 #ifdef CONFIG_USBNET_DEV_ADDR
2345 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2347 #ifdef CONFIG_USBNET_HOST_ADDR
2348 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2350 /* Check if the user overruled the MAC addresses */
2351 if (getenv("usbnet_devaddr"))
2352 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2355 if (getenv("usbnet_hostaddr"))
2356 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2359 if (!is_eth_addr_valid(dev_addr)) {
2360 error("Need valid 'usbnet_devaddr' to be set");
2363 if (!is_eth_addr_valid(host_addr)) {
2364 error("Need valid 'usbnet_hostaddr' to be set");
2368 if (usb_gadget_register_driver(ð_driver) < 0)
2371 dev->network_started = 0;
2373 packet_received = 0;
2376 gadget = dev->gadget;
2377 usb_gadget_connect(gadget);
2379 if (getenv("cdc_connect_timeout"))
2380 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2381 NULL, 10) * CONFIG_SYS_HZ;
2383 while (!l_ethdev.network_started) {
2384 /* Handle control-c and timeouts */
2385 if (ctrlc() || (get_timer(ts) > timeout)) {
2386 error("The remote end did not respond in time.");
2389 usb_gadget_handle_interrupts();
2392 packet_received = 0;
2393 rx_submit(dev, dev->rx_req, 0);
2399 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2402 void *rndis_pkt = NULL;
2403 struct eth_dev *dev = &l_ethdev;
2404 struct usb_request *req = dev->tx_req;
2406 unsigned long timeout = USB_CONNECT_TIMEOUT;
2408 debug("%s:...\n", __func__);
2410 /* new buffer is needed to include RNDIS header */
2411 if (rndis_active(dev)) {
2412 rndis_pkt = malloc(length +
2413 sizeof(struct rndis_packet_msg_type));
2415 error("No memory to alloc RNDIS packet");
2418 rndis_add_hdr(rndis_pkt, length);
2419 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2422 length += sizeof(struct rndis_packet_msg_type);
2425 req->context = NULL;
2426 req->complete = tx_complete;
2429 * use zlp framing on tx for strict CDC-Ether conformance,
2430 * though any robust network rx path ignores extra padding.
2431 * and some hardware doesn't like to write zlps.
2434 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2437 req->length = length;
2439 /* throttle highspeed IRQ rate back slightly */
2440 if (gadget_is_dualspeed(dev->gadget))
2441 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2442 ? ((dev->tx_qlen % qmult) != 0) : 0;
2448 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2451 debug("%s: packet queued\n", __func__);
2452 while (!packet_sent) {
2453 if (get_timer(ts) > timeout) {
2454 printf("timeout sending packets to usb ethernet\n");
2457 usb_gadget_handle_interrupts();
2464 dev->stats.tx_dropped++;
2468 static int usb_eth_recv(struct eth_device *netdev)
2470 struct eth_dev *dev = &l_ethdev;
2472 usb_gadget_handle_interrupts();
2474 if (packet_received) {
2475 debug("%s: packet received\n", __func__);
2477 NetReceive(NetRxPackets[0], dev->rx_req->length);
2478 packet_received = 0;
2480 rx_submit(dev, dev->rx_req, 0);
2482 error("dev->rx_req invalid");
2487 void usb_eth_halt(struct eth_device *netdev)
2489 struct eth_dev *dev = &l_ethdev;
2492 error("received NULL ptr");
2496 /* If the gadget not registered, simple return */
2501 * Some USB controllers may need additional deinitialization here
2502 * before dropping pull-up (also due to hardware issues).
2503 * For example: unhandled interrupt with status stage started may
2504 * bring the controller to fully broken state (until board reset).
2505 * There are some variants to debug and fix such cases:
2506 * 1) In the case of RNDIS connection eth_stop can perform additional
2507 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2508 * 2) 'pullup' callback in your UDC driver can be improved to perform
2509 * this deinitialization.
2513 usb_gadget_disconnect(dev->gadget);
2515 /* Clear pending interrupt */
2516 if (dev->network_started) {
2517 usb_gadget_handle_interrupts();
2518 dev->network_started = 0;
2521 usb_gadget_unregister_driver(ð_driver);
2524 static struct usb_gadget_driver eth_driver = {
2528 .unbind = eth_unbind,
2531 .disconnect = eth_disconnect,
2533 .suspend = eth_suspend,
2534 .resume = eth_resume,
2537 int usb_eth_initialize(bd_t *bi)
2539 struct eth_device *netdev = &l_netdev;
2541 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2543 netdev->init = usb_eth_init;
2544 netdev->send = usb_eth_send;
2545 netdev->recv = usb_eth_recv;
2546 netdev->halt = usb_eth_halt;
2548 #ifdef CONFIG_MCAST_TFTP
2549 #error not supported
2551 eth_register(netdev);