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>
21 #include <linux/ctype.h>
23 #include "gadget_chips.h"
26 #define USB_NET_NAME "usb_ether"
29 extern struct platform_data brd
;
32 unsigned packet_received
, packet_sent
;
35 * Ethernet gadget driver -- with CDC and non-CDC options
36 * Builds on hardware support for a full duplex link.
38 * CDC Ethernet is the standard USB solution for sending Ethernet frames
39 * using USB. Real hardware tends to use the same framing protocol but look
40 * different for control features. This driver strongly prefers to use
41 * this USB-IF standard as its open-systems interoperability solution;
42 * most host side USB stacks (except from Microsoft) support it.
44 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
45 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
46 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
48 * There's some hardware that can't talk CDC ECM. We make that hardware
49 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
50 * link-level setup only requires activating the configuration. Only the
51 * endpoint descriptors, and product/vendor IDs, are relevant; no control
52 * operations are available. Linux supports it, but other host operating
53 * systems may not. (This is a subset of CDC Ethernet.)
55 * It turns out that if you add a few descriptors to that "CDC Subset",
56 * (Windows) host side drivers from MCCI can treat it as one submode of
57 * a proprietary scheme called "SAFE" ... without needing to know about
58 * specific product/vendor IDs. So we do that, making it easier to use
59 * those MS-Windows drivers. Those added descriptors make it resemble a
60 * CDC MDLM device, but they don't change device behavior at all. (See
61 * MCCI Engineering report 950198 "SAFE Networking Functions".)
63 * A third option is also in use. Rather than CDC Ethernet, or something
64 * simpler, Microsoft pushes their own approach: RNDIS. The published
65 * RNDIS specs are ambiguous and appear to be incomplete, and are also
66 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
68 #define ETH_ALEN 6 /* Octets in one ethernet addr */
69 #define ETH_HLEN 14 /* Total octets in header. */
70 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
71 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
72 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
74 #define DRIVER_DESC "Ethernet Gadget"
75 /* Based on linux 2.6.27 version */
76 #define DRIVER_VERSION "May Day 2005"
78 static const char shortname
[] = "ether";
79 static const char driver_desc
[] = DRIVER_DESC
;
81 #define RX_EXTRA 20 /* guard against rx overflows */
83 #ifndef CONFIG_USB_ETH_RNDIS
84 #define rndis_uninit(x) do {} while (0)
85 #define rndis_deregister(c) do {} while (0)
86 #define rndis_exit() do {} while (0)
89 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
90 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
95 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
97 /*-------------------------------------------------------------------------*/
100 struct usb_gadget
*gadget
;
101 struct usb_request
*req
; /* for control responses */
102 struct usb_request
*stat_req
; /* for cdc & rndis status */
105 struct usb_ep
*in_ep
, *out_ep
, *status_ep
;
106 const struct usb_endpoint_descriptor
109 struct usb_request
*tx_req
, *rx_req
;
111 struct eth_device
*net
;
112 struct net_device_stats stats
;
113 unsigned int tx_qlen
;
118 unsigned suspended
:1;
119 unsigned network_started
:1;
123 #define WORK_RX_MEMORY 0
125 u8 host_mac
[ETH_ALEN
];
129 * This version autoconfigures as much as possible at run-time.
131 * It also ASSUMES a self-powered device, without remote wakeup,
132 * although remote wakeup support would make sense.
135 /*-------------------------------------------------------------------------*/
136 static struct eth_dev l_ethdev
;
137 static struct eth_device l_netdev
;
138 static struct usb_gadget_driver eth_driver
;
140 /*-------------------------------------------------------------------------*/
142 /* "main" config is either CDC, or its simple subset */
143 static inline int is_cdc(struct eth_dev
*dev
)
145 #if !defined(CONFIG_USB_ETH_SUBSET)
146 return 1; /* only cdc possible */
147 #elif !defined(CONFIG_USB_ETH_CDC)
148 return 0; /* only subset possible */
150 return dev
->cdc
; /* depends on what hardware we found */
154 /* "secondary" RNDIS config may sometimes be activated */
155 static inline int rndis_active(struct eth_dev
*dev
)
157 #ifdef CONFIG_USB_ETH_RNDIS
164 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
165 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
167 #define DEFAULT_QLEN 2 /* double buffering by default */
169 /* peak bulk transfer bits-per-second */
170 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
171 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
173 #ifdef CONFIG_USB_GADGET_DUALSPEED
174 #define DEVSPEED USB_SPEED_HIGH
176 #ifdef CONFIG_USB_ETH_QMULT
177 #define qmult CONFIG_USB_ETH_QMULT
182 /* for dual-speed hardware, use deeper queues at highspeed */
183 #define qlen(gadget) \
184 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
186 static inline int BITRATE(struct usb_gadget
*g
)
188 return (g
->speed
== USB_SPEED_HIGH
) ? HS_BPS
: FS_BPS
;
191 #else /* full speed (low speed doesn't do bulk) */
195 #define DEVSPEED USB_SPEED_FULL
197 #define qlen(gadget) DEFAULT_QLEN
199 static inline int BITRATE(struct usb_gadget
*g
)
205 /*-------------------------------------------------------------------------*/
208 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
209 * Instead: allocate your own, using normal USB-IF procedures.
213 * Thanks to NetChip Technologies for donating this product ID.
214 * It's for devices with only CDC Ethernet configurations.
216 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
217 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
220 * For hardware that can't talk CDC, we use the same vendor ID that
221 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
222 * with pxa250. We're protocol-compatible, if the host-side drivers
223 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
224 * drivers that need to hard-wire endpoint numbers have a hook.
226 * The protocol is a minimal subset of CDC Ether, which works on any bulk
227 * hardware that's not deeply broken ... even on hardware that can't talk
228 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
229 * doesn't handle control-OUT).
231 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
232 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
235 * For hardware that can talk RNDIS and either of the above protocols,
236 * use this ID ... the windows INF files will know it. Unless it's
237 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
238 * the non-RNDIS configuration.
240 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
241 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
244 * Some systems will want different product identifers published in the
245 * device descriptor, either numbers or strings or both. These string
246 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
250 * Emulating them in eth_bind:
251 * static ushort idVendor;
252 * static ushort idProduct;
255 #if defined(CONFIG_USBNET_MANUFACTURER)
256 static char *iManufacturer
= CONFIG_USBNET_MANUFACTURER
;
258 static char *iManufacturer
= "U-boot";
261 /* These probably need to be configurable. */
262 static ushort bcdDevice
;
263 static char *iProduct
;
264 static char *iSerialNumber
;
266 static char dev_addr
[18];
268 static char host_addr
[18];
271 /*-------------------------------------------------------------------------*/
274 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
275 * ep0 implementation: descriptors, config management, setup().
276 * also optional class-specific notification interrupt transfer.
280 * DESCRIPTORS ... most are static, but strings and (full) configuration
281 * descriptors are built on demand. For now we do either full CDC, or
282 * our simple subset, with RNDIS as an optional second configuration.
284 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
285 * the class descriptors match a modem (they're ignored; it's really just
286 * Ethernet functionality), they don't need the NOP altsetting, and the
287 * status transfer endpoint isn't optional.
290 #define STRING_MANUFACTURER 1
291 #define STRING_PRODUCT 2
292 #define STRING_ETHADDR 3
293 #define STRING_DATA 4
294 #define STRING_CONTROL 5
295 #define STRING_RNDIS_CONTROL 6
297 #define STRING_SUBSET 8
298 #define STRING_RNDIS 9
299 #define STRING_SERIALNUMBER 10
301 /* holds our biggest descriptor (or RNDIS response) */
302 #define USB_BUFSIZ 256
305 * This device advertises one configuration, eth_config, unless RNDIS
306 * is enabled (rndis_config) on hardware supporting at least two configs.
308 * NOTE: Controllers like superh_udc should probably be able to use
309 * an RNDIS-only configuration.
311 * FIXME define some higher-powered configurations to make it easier
312 * to recharge batteries ...
315 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
316 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
318 static struct usb_device_descriptor
320 .bLength
= sizeof device_desc
,
321 .bDescriptorType
= USB_DT_DEVICE
,
323 .bcdUSB
= __constant_cpu_to_le16(0x0200),
325 .bDeviceClass
= USB_CLASS_COMM
,
326 .bDeviceSubClass
= 0,
327 .bDeviceProtocol
= 0,
329 .idVendor
= __constant_cpu_to_le16(CDC_VENDOR_NUM
),
330 .idProduct
= __constant_cpu_to_le16(CDC_PRODUCT_NUM
),
331 .iManufacturer
= STRING_MANUFACTURER
,
332 .iProduct
= STRING_PRODUCT
,
333 .bNumConfigurations
= 1,
336 static struct usb_otg_descriptor
338 .bLength
= sizeof otg_descriptor
,
339 .bDescriptorType
= USB_DT_OTG
,
341 .bmAttributes
= USB_OTG_SRP
,
344 static struct usb_config_descriptor
346 .bLength
= sizeof eth_config
,
347 .bDescriptorType
= USB_DT_CONFIG
,
349 /* compute wTotalLength on the fly */
351 .bConfigurationValue
= DEV_CONFIG_VALUE
,
352 .iConfiguration
= STRING_CDC
,
353 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
357 #ifdef CONFIG_USB_ETH_RNDIS
358 static struct usb_config_descriptor
360 .bLength
= sizeof rndis_config
,
361 .bDescriptorType
= USB_DT_CONFIG
,
363 /* compute wTotalLength on the fly */
365 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
366 .iConfiguration
= STRING_RNDIS
,
367 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
373 * Compared to the simple CDC subset, the full CDC Ethernet model adds
374 * three class descriptors, two interface descriptors, optional status
375 * endpoint. Both have a "data" interface and two bulk endpoints.
376 * There are also differences in how control requests are handled.
378 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
379 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
380 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
381 * work around those bugs) are unlikely to ever come from MSFT, you may
382 * wish to avoid using RNDIS.
384 * MCCI offers an alternative to RNDIS if you need to connect to Windows
385 * but have hardware that can't support CDC Ethernet. We add descriptors
386 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
387 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
388 * get those drivers from MCCI, or bundled with various products.
391 #ifdef CONFIG_USB_ETH_CDC
392 static struct usb_interface_descriptor
394 .bLength
= sizeof control_intf
,
395 .bDescriptorType
= USB_DT_INTERFACE
,
397 .bInterfaceNumber
= 0,
398 /* status endpoint is optional; this may be patched later */
400 .bInterfaceClass
= USB_CLASS_COMM
,
401 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
402 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
403 .iInterface
= STRING_CONTROL
,
407 #ifdef CONFIG_USB_ETH_RNDIS
408 static const struct usb_interface_descriptor
409 rndis_control_intf
= {
410 .bLength
= sizeof rndis_control_intf
,
411 .bDescriptorType
= USB_DT_INTERFACE
,
413 .bInterfaceNumber
= 0,
415 .bInterfaceClass
= USB_CLASS_COMM
,
416 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
417 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
418 .iInterface
= STRING_RNDIS_CONTROL
,
422 static const struct usb_cdc_header_desc header_desc
= {
423 .bLength
= sizeof header_desc
,
424 .bDescriptorType
= USB_DT_CS_INTERFACE
,
425 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
427 .bcdCDC
= __constant_cpu_to_le16(0x0110),
430 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
432 static const struct usb_cdc_union_desc union_desc
= {
433 .bLength
= sizeof union_desc
,
434 .bDescriptorType
= USB_DT_CS_INTERFACE
,
435 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
437 .bMasterInterface0
= 0, /* index of control interface */
438 .bSlaveInterface0
= 1, /* index of DATA interface */
441 #endif /* CDC || RNDIS */
443 #ifdef CONFIG_USB_ETH_RNDIS
445 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor
= {
446 .bLength
= sizeof call_mgmt_descriptor
,
447 .bDescriptorType
= USB_DT_CS_INTERFACE
,
448 .bDescriptorSubType
= USB_CDC_CALL_MANAGEMENT_TYPE
,
450 .bmCapabilities
= 0x00,
451 .bDataInterface
= 0x01,
454 static const struct usb_cdc_acm_descriptor acm_descriptor
= {
455 .bLength
= sizeof acm_descriptor
,
456 .bDescriptorType
= USB_DT_CS_INTERFACE
,
457 .bDescriptorSubType
= USB_CDC_ACM_TYPE
,
459 .bmCapabilities
= 0x00,
464 #ifndef CONFIG_USB_ETH_CDC
467 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
468 * ways: data endpoints live in the control interface, there's no data
469 * interface, and it's not used to talk to a cell phone radio.
472 static const struct usb_cdc_mdlm_desc mdlm_desc
= {
473 .bLength
= sizeof mdlm_desc
,
474 .bDescriptorType
= USB_DT_CS_INTERFACE
,
475 .bDescriptorSubType
= USB_CDC_MDLM_TYPE
,
477 .bcdVersion
= __constant_cpu_to_le16(0x0100),
479 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
480 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
485 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
486 * can't really use its struct. All we do here is say that we're using
487 * the submode of "SAFE" which directly matches the CDC Subset.
489 static const u8 mdlm_detail_desc
[] = {
492 USB_CDC_MDLM_DETAIL_TYPE
,
495 0, /* network control capabilities (none) */
496 0, /* network data capabilities ("raw" encapsulation) */
501 static const struct usb_cdc_ether_desc ether_desc
= {
502 .bLength
= sizeof(ether_desc
),
503 .bDescriptorType
= USB_DT_CS_INTERFACE
,
504 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
506 /* this descriptor actually adds value, surprise! */
507 .iMACAddress
= STRING_ETHADDR
,
508 .bmEthernetStatistics
= __constant_cpu_to_le32(0), /* no statistics */
509 .wMaxSegmentSize
= __constant_cpu_to_le16(ETH_FRAME_LEN
),
510 .wNumberMCFilters
= __constant_cpu_to_le16(0),
511 .bNumberPowerFilters
= 0,
514 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
517 * include the status endpoint if we can, even where it's optional.
518 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
519 * packet, to simplify cancellation; and a big transfer interval, to
520 * waste less bandwidth.
522 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
523 * if they ignore the connect/disconnect notifications that real aether
524 * can provide. more advanced cdc configurations might want to support
525 * encapsulated commands (vendor-specific, using control-OUT).
527 * RNDIS requires the status endpoint, since it uses that encapsulation
528 * mechanism for its funky RPC scheme.
531 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
532 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
534 static struct usb_endpoint_descriptor
536 .bLength
= USB_DT_ENDPOINT_SIZE
,
537 .bDescriptorType
= USB_DT_ENDPOINT
,
539 .bEndpointAddress
= USB_DIR_IN
,
540 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
541 .wMaxPacketSize
= __constant_cpu_to_le16(STATUS_BYTECOUNT
),
542 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
546 #ifdef CONFIG_USB_ETH_CDC
548 /* the default data interface has no endpoints ... */
550 static const struct usb_interface_descriptor
552 .bLength
= sizeof data_nop_intf
,
553 .bDescriptorType
= USB_DT_INTERFACE
,
555 .bInterfaceNumber
= 1,
556 .bAlternateSetting
= 0,
558 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
559 .bInterfaceSubClass
= 0,
560 .bInterfaceProtocol
= 0,
563 /* ... but the "real" data interface has two bulk endpoints */
565 static const struct usb_interface_descriptor
567 .bLength
= sizeof data_intf
,
568 .bDescriptorType
= USB_DT_INTERFACE
,
570 .bInterfaceNumber
= 1,
571 .bAlternateSetting
= 1,
573 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
574 .bInterfaceSubClass
= 0,
575 .bInterfaceProtocol
= 0,
576 .iInterface
= STRING_DATA
,
581 #ifdef CONFIG_USB_ETH_RNDIS
583 /* RNDIS doesn't activate by changing to the "real" altsetting */
585 static const struct usb_interface_descriptor
587 .bLength
= sizeof rndis_data_intf
,
588 .bDescriptorType
= USB_DT_INTERFACE
,
590 .bInterfaceNumber
= 1,
591 .bAlternateSetting
= 0,
593 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
594 .bInterfaceSubClass
= 0,
595 .bInterfaceProtocol
= 0,
596 .iInterface
= STRING_DATA
,
601 #ifdef CONFIG_USB_ETH_SUBSET
604 * "Simple" CDC-subset option is a simple vendor-neutral model that most
605 * full speed controllers can handle: one interface, two bulk endpoints.
607 * To assist host side drivers, we fancy it up a bit, and add descriptors
608 * so some host side drivers will understand it as a "SAFE" variant.
611 static const struct usb_interface_descriptor
613 .bLength
= sizeof subset_data_intf
,
614 .bDescriptorType
= USB_DT_INTERFACE
,
616 .bInterfaceNumber
= 0,
617 .bAlternateSetting
= 0,
619 .bInterfaceClass
= USB_CLASS_COMM
,
620 .bInterfaceSubClass
= USB_CDC_SUBCLASS_MDLM
,
621 .bInterfaceProtocol
= 0,
622 .iInterface
= STRING_DATA
,
627 static struct usb_endpoint_descriptor
629 .bLength
= USB_DT_ENDPOINT_SIZE
,
630 .bDescriptorType
= USB_DT_ENDPOINT
,
632 .bEndpointAddress
= USB_DIR_IN
,
633 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
634 .wMaxPacketSize
= __constant_cpu_to_le16(64),
637 static struct usb_endpoint_descriptor
639 .bLength
= USB_DT_ENDPOINT_SIZE
,
640 .bDescriptorType
= USB_DT_ENDPOINT
,
642 .bEndpointAddress
= USB_DIR_OUT
,
643 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
644 .wMaxPacketSize
= __constant_cpu_to_le16(64),
647 static const struct usb_descriptor_header
*fs_eth_function
[11] = {
648 (struct usb_descriptor_header
*) &otg_descriptor
,
649 #ifdef CONFIG_USB_ETH_CDC
650 /* "cdc" mode descriptors */
651 (struct usb_descriptor_header
*) &control_intf
,
652 (struct usb_descriptor_header
*) &header_desc
,
653 (struct usb_descriptor_header
*) &union_desc
,
654 (struct usb_descriptor_header
*) ðer_desc
,
655 /* NOTE: status endpoint may need to be removed */
656 (struct usb_descriptor_header
*) &fs_status_desc
,
657 /* data interface, with altsetting */
658 (struct usb_descriptor_header
*) &data_nop_intf
,
659 (struct usb_descriptor_header
*) &data_intf
,
660 (struct usb_descriptor_header
*) &fs_source_desc
,
661 (struct usb_descriptor_header
*) &fs_sink_desc
,
663 #endif /* CONFIG_USB_ETH_CDC */
666 static inline void fs_subset_descriptors(void)
668 #ifdef CONFIG_USB_ETH_SUBSET
669 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
670 fs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
671 fs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
672 fs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
673 fs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
674 fs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
675 fs_eth_function
[6] = (struct usb_descriptor_header
*) &fs_source_desc
;
676 fs_eth_function
[7] = (struct usb_descriptor_header
*) &fs_sink_desc
;
677 fs_eth_function
[8] = NULL
;
679 fs_eth_function
[1] = NULL
;
683 #ifdef CONFIG_USB_ETH_RNDIS
684 static const struct usb_descriptor_header
*fs_rndis_function
[] = {
685 (struct usb_descriptor_header
*) &otg_descriptor
,
686 /* control interface matches ACM, not Ethernet */
687 (struct usb_descriptor_header
*) &rndis_control_intf
,
688 (struct usb_descriptor_header
*) &header_desc
,
689 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
690 (struct usb_descriptor_header
*) &acm_descriptor
,
691 (struct usb_descriptor_header
*) &union_desc
,
692 (struct usb_descriptor_header
*) &fs_status_desc
,
693 /* data interface has no altsetting */
694 (struct usb_descriptor_header
*) &rndis_data_intf
,
695 (struct usb_descriptor_header
*) &fs_source_desc
,
696 (struct usb_descriptor_header
*) &fs_sink_desc
,
702 * usb 2.0 devices need to expose both high speed and full speed
703 * descriptors, unless they only run at full speed.
706 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
707 static struct usb_endpoint_descriptor
709 .bLength
= USB_DT_ENDPOINT_SIZE
,
710 .bDescriptorType
= USB_DT_ENDPOINT
,
712 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
713 .wMaxPacketSize
= __constant_cpu_to_le16(STATUS_BYTECOUNT
),
714 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
716 #endif /* CONFIG_USB_ETH_CDC */
718 static struct usb_endpoint_descriptor
720 .bLength
= USB_DT_ENDPOINT_SIZE
,
721 .bDescriptorType
= USB_DT_ENDPOINT
,
723 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
724 .wMaxPacketSize
= __constant_cpu_to_le16(512),
727 static struct usb_endpoint_descriptor
729 .bLength
= USB_DT_ENDPOINT_SIZE
,
730 .bDescriptorType
= USB_DT_ENDPOINT
,
732 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
733 .wMaxPacketSize
= __constant_cpu_to_le16(512),
736 static struct usb_qualifier_descriptor
738 .bLength
= sizeof dev_qualifier
,
739 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
741 .bcdUSB
= __constant_cpu_to_le16(0x0200),
742 .bDeviceClass
= USB_CLASS_COMM
,
744 .bNumConfigurations
= 1,
747 static const struct usb_descriptor_header
*hs_eth_function
[11] = {
748 (struct usb_descriptor_header
*) &otg_descriptor
,
749 #ifdef CONFIG_USB_ETH_CDC
750 /* "cdc" mode descriptors */
751 (struct usb_descriptor_header
*) &control_intf
,
752 (struct usb_descriptor_header
*) &header_desc
,
753 (struct usb_descriptor_header
*) &union_desc
,
754 (struct usb_descriptor_header
*) ðer_desc
,
755 /* NOTE: status endpoint may need to be removed */
756 (struct usb_descriptor_header
*) &hs_status_desc
,
757 /* data interface, with altsetting */
758 (struct usb_descriptor_header
*) &data_nop_intf
,
759 (struct usb_descriptor_header
*) &data_intf
,
760 (struct usb_descriptor_header
*) &hs_source_desc
,
761 (struct usb_descriptor_header
*) &hs_sink_desc
,
763 #endif /* CONFIG_USB_ETH_CDC */
766 static inline void hs_subset_descriptors(void)
768 #ifdef CONFIG_USB_ETH_SUBSET
769 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
770 hs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
771 hs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
772 hs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
773 hs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
774 hs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
775 hs_eth_function
[6] = (struct usb_descriptor_header
*) &hs_source_desc
;
776 hs_eth_function
[7] = (struct usb_descriptor_header
*) &hs_sink_desc
;
777 hs_eth_function
[8] = NULL
;
779 hs_eth_function
[1] = NULL
;
783 #ifdef CONFIG_USB_ETH_RNDIS
784 static const struct usb_descriptor_header
*hs_rndis_function
[] = {
785 (struct usb_descriptor_header
*) &otg_descriptor
,
786 /* control interface matches ACM, not Ethernet */
787 (struct usb_descriptor_header
*) &rndis_control_intf
,
788 (struct usb_descriptor_header
*) &header_desc
,
789 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
790 (struct usb_descriptor_header
*) &acm_descriptor
,
791 (struct usb_descriptor_header
*) &union_desc
,
792 (struct usb_descriptor_header
*) &hs_status_desc
,
793 /* data interface has no altsetting */
794 (struct usb_descriptor_header
*) &rndis_data_intf
,
795 (struct usb_descriptor_header
*) &hs_source_desc
,
796 (struct usb_descriptor_header
*) &hs_sink_desc
,
802 /* maxpacket and other transfer characteristics vary by speed. */
803 static inline struct usb_endpoint_descriptor
*
804 ep_desc(struct usb_gadget
*g
, struct usb_endpoint_descriptor
*hs
,
805 struct usb_endpoint_descriptor
*fs
)
807 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
812 /*-------------------------------------------------------------------------*/
814 /* descriptors that are built on-demand */
816 static char manufacturer
[50];
817 static char product_desc
[40] = DRIVER_DESC
;
818 static char serial_number
[20];
820 /* address that the host will use ... usually assigned at random */
821 static char ethaddr
[2 * ETH_ALEN
+ 1];
823 /* static strings, in UTF-8 */
824 static struct usb_string strings
[] = {
825 { STRING_MANUFACTURER
, manufacturer
, },
826 { STRING_PRODUCT
, product_desc
, },
827 { STRING_SERIALNUMBER
, serial_number
, },
828 { STRING_DATA
, "Ethernet Data", },
829 { STRING_ETHADDR
, ethaddr
, },
830 #ifdef CONFIG_USB_ETH_CDC
831 { STRING_CDC
, "CDC Ethernet", },
832 { STRING_CONTROL
, "CDC Communications Control", },
834 #ifdef CONFIG_USB_ETH_SUBSET
835 { STRING_SUBSET
, "CDC Ethernet Subset", },
837 #ifdef CONFIG_USB_ETH_RNDIS
838 { STRING_RNDIS
, "RNDIS", },
839 { STRING_RNDIS_CONTROL
, "RNDIS Communications Control", },
841 { } /* end of list */
844 static struct usb_gadget_strings stringtab
= {
845 .language
= 0x0409, /* en-us */
849 /*============================================================================*/
850 DEFINE_CACHE_ALIGN_BUFFER(u8
, control_req
, USB_BUFSIZ
);
852 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
853 DEFINE_CACHE_ALIGN_BUFFER(u8
, status_req
, STATUS_BYTECOUNT
);
856 /*============================================================================*/
859 * one config, two interfaces: control, data.
860 * complications: class descriptors, and an altsetting.
863 config_buf(struct usb_gadget
*g
, u8
*buf
, u8 type
, unsigned index
, int is_otg
)
866 const struct usb_config_descriptor
*config
;
867 const struct usb_descriptor_header
**function
;
870 if (gadget_is_dualspeed(g
)) {
871 hs
= (g
->speed
== USB_SPEED_HIGH
);
872 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
875 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
877 if (index
>= device_desc
.bNumConfigurations
)
880 #ifdef CONFIG_USB_ETH_RNDIS
882 * list the RNDIS config first, to make Microsoft's drivers
883 * happy. DOCSIS 1.0 needs this too.
885 if (device_desc
.bNumConfigurations
== 2 && index
== 0) {
886 config
= &rndis_config
;
887 function
= which_fn(rndis
);
891 config
= ð_config
;
892 function
= which_fn(eth
);
895 /* for now, don't advertise srp-only devices */
899 len
= usb_gadget_config_buf(config
, buf
, USB_BUFSIZ
, function
);
902 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
906 /*-------------------------------------------------------------------------*/
908 static void eth_start(struct eth_dev
*dev
, gfp_t gfp_flags
);
909 static int alloc_requests(struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
);
912 set_ether_config(struct eth_dev
*dev
, gfp_t gfp_flags
)
915 struct usb_gadget
*gadget
= dev
->gadget
;
917 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
918 /* status endpoint used for RNDIS and (optionally) CDC */
919 if (!subset_active(dev
) && dev
->status_ep
) {
920 dev
->status
= ep_desc(gadget
, &hs_status_desc
,
922 dev
->status_ep
->driver_data
= dev
;
924 result
= usb_ep_enable(dev
->status_ep
, dev
->status
);
926 debug("enable %s --> %d\n",
927 dev
->status_ep
->name
, result
);
933 dev
->in
= ep_desc(gadget
, &hs_source_desc
, &fs_source_desc
);
934 dev
->in_ep
->driver_data
= dev
;
936 dev
->out
= ep_desc(gadget
, &hs_sink_desc
, &fs_sink_desc
);
937 dev
->out_ep
->driver_data
= dev
;
940 * With CDC, the host isn't allowed to use these two data
941 * endpoints in the default altsetting for the interface.
942 * so we don't activate them yet. Reset from SET_INTERFACE.
944 * Strictly speaking RNDIS should work the same: activation is
945 * a side effect of setting a packet filter. Deactivation is
946 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
948 if (!cdc_active(dev
)) {
949 result
= usb_ep_enable(dev
->in_ep
, dev
->in
);
951 debug("enable %s --> %d\n",
952 dev
->in_ep
->name
, result
);
956 result
= usb_ep_enable(dev
->out_ep
, dev
->out
);
958 debug("enable %s --> %d\n",
959 dev
->out_ep
->name
, result
);
966 result
= alloc_requests(dev
, qlen(gadget
), gfp_flags
);
968 /* on error, disable any endpoints */
970 if (!subset_active(dev
) && dev
->status_ep
)
971 (void) usb_ep_disable(dev
->status_ep
);
973 (void) usb_ep_disable(dev
->in_ep
);
974 (void) usb_ep_disable(dev
->out_ep
);
977 } else if (!cdc_active(dev
)) {
979 * activate non-CDC configs right away
980 * this isn't strictly according to the RNDIS spec
982 eth_start(dev
, GFP_ATOMIC
);
985 /* caller is responsible for cleanup on error */
989 static void eth_reset_config(struct eth_dev
*dev
)
991 if (dev
->config
== 0)
994 debug("%s\n", __func__
);
996 rndis_uninit(dev
->rndis_config
);
999 * disable endpoints, forcing (synchronous) completion of
1000 * pending i/o. then free the requests.
1004 usb_ep_disable(dev
->in_ep
);
1006 usb_ep_free_request(dev
->in_ep
, dev
->tx_req
);
1011 usb_ep_disable(dev
->out_ep
);
1013 usb_ep_free_request(dev
->out_ep
, dev
->rx_req
);
1018 usb_ep_disable(dev
->status_ep
);
1021 dev
->cdc_filter
= 0;
1026 * change our operational config. must agree with the code
1027 * that returns config descriptors, and altsetting code.
1029 static int eth_set_config(struct eth_dev
*dev
, unsigned number
,
1033 struct usb_gadget
*gadget
= dev
->gadget
;
1035 if (gadget_is_sa1100(gadget
)
1037 && dev
->tx_qlen
!= 0) {
1038 /* tx fifo is full, but we can't clear it...*/
1039 error("can't change configurations");
1042 eth_reset_config(dev
);
1045 case DEV_CONFIG_VALUE
:
1046 result
= set_ether_config(dev
, gfp_flags
);
1048 #ifdef CONFIG_USB_ETH_RNDIS
1049 case DEV_RNDIS_CONFIG_VALUE
:
1051 result
= set_ether_config(dev
, gfp_flags
);
1063 eth_reset_config(dev
);
1064 usb_gadget_vbus_draw(dev
->gadget
,
1065 gadget_is_otg(dev
->gadget
) ? 8 : 100);
1070 power
= 2 * eth_config
.bMaxPower
;
1071 usb_gadget_vbus_draw(dev
->gadget
, power
);
1073 switch (gadget
->speed
) {
1074 case USB_SPEED_FULL
:
1075 speed
= "full"; break;
1076 #ifdef CONFIG_USB_GADGET_DUALSPEED
1077 case USB_SPEED_HIGH
:
1078 speed
= "high"; break;
1084 dev
->config
= number
;
1085 printf("%s speed config #%d: %d mA, %s, using %s\n",
1086 speed
, number
, power
, driver_desc
,
1091 : "CDC Ethernet Subset"));
1096 /*-------------------------------------------------------------------------*/
1098 #ifdef CONFIG_USB_ETH_CDC
1101 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1102 * only to notify the host about link status changes (which we support) or
1103 * report completion of some encapsulated command (as used in RNDIS). Since
1104 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1105 * command mechanism; and only one status request is ever queued.
1107 static void eth_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1109 struct usb_cdc_notification
*event
= req
->buf
;
1110 int value
= req
->status
;
1111 struct eth_dev
*dev
= ep
->driver_data
;
1113 /* issue the second notification if host reads the first */
1114 if (event
->bNotificationType
== USB_CDC_NOTIFY_NETWORK_CONNECTION
1116 __le32
*data
= req
->buf
+ sizeof *event
;
1118 event
->bmRequestType
= 0xA1;
1119 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
1120 event
->wValue
= __constant_cpu_to_le16(0);
1121 event
->wIndex
= __constant_cpu_to_le16(1);
1122 event
->wLength
= __constant_cpu_to_le16(8);
1124 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1125 data
[0] = data
[1] = cpu_to_le32(BITRATE(dev
->gadget
));
1127 req
->length
= STATUS_BYTECOUNT
;
1128 value
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
1129 debug("send SPEED_CHANGE --> %d\n", value
);
1132 } else if (value
!= -ECONNRESET
) {
1133 debug("event %02x --> %d\n",
1134 event
->bNotificationType
, value
);
1135 if (event
->bNotificationType
==
1136 USB_CDC_NOTIFY_SPEED_CHANGE
) {
1137 l_ethdev
.network_started
= 1;
1138 printf("USB network up!\n");
1141 req
->context
= NULL
;
1144 static void issue_start_status(struct eth_dev
*dev
)
1146 struct usb_request
*req
= dev
->stat_req
;
1147 struct usb_cdc_notification
*event
;
1153 * FIXME ugly idiom, maybe we'd be better with just
1154 * a "cancel the whole queue" primitive since any
1155 * unlink-one primitive has way too many error modes.
1156 * here, we "know" toggle is already clear...
1158 * FIXME iff req->context != null just dequeue it
1160 usb_ep_disable(dev
->status_ep
);
1161 usb_ep_enable(dev
->status_ep
, dev
->status
);
1164 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1165 * a SPEED_CHANGE. could be useful in some configs.
1168 event
->bmRequestType
= 0xA1;
1169 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
1170 event
->wValue
= __constant_cpu_to_le16(1); /* connected */
1171 event
->wIndex
= __constant_cpu_to_le16(1);
1174 req
->length
= sizeof *event
;
1175 req
->complete
= eth_status_complete
;
1178 value
= usb_ep_queue(dev
->status_ep
, req
, GFP_ATOMIC
);
1180 debug("status buf queue --> %d\n", value
);
1185 /*-------------------------------------------------------------------------*/
1187 static void eth_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1189 if (req
->status
|| req
->actual
!= req
->length
)
1190 debug("setup complete --> %d, %d/%d\n",
1191 req
->status
, req
->actual
, req
->length
);
1194 #ifdef CONFIG_USB_ETH_RNDIS
1196 static void rndis_response_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1198 if (req
->status
|| req
->actual
!= req
->length
)
1199 debug("rndis response complete --> %d, %d/%d\n",
1200 req
->status
, req
->actual
, req
->length
);
1202 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1205 static void rndis_command_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1207 struct eth_dev
*dev
= ep
->driver_data
;
1210 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1211 status
= rndis_msg_parser(dev
->rndis_config
, (u8
*) req
->buf
);
1213 error("%s: rndis parse error %d", __func__
, status
);
1219 * The setup() callback implements all the ep0 functionality that's not
1220 * handled lower down. CDC has a number of less-common features:
1222 * - two interfaces: control, and ethernet data
1223 * - Ethernet data interface has two altsettings: default, and active
1224 * - class-specific descriptors for the control interface
1225 * - class-specific control requests
1228 eth_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1230 struct eth_dev
*dev
= get_gadget_data(gadget
);
1231 struct usb_request
*req
= dev
->req
;
1232 int value
= -EOPNOTSUPP
;
1233 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
1234 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
1235 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
1238 * descriptors just go into the pre-allocated ep0 buffer,
1239 * while config change events may enable network traffic.
1242 debug("%s\n", __func__
);
1244 req
->complete
= eth_setup_complete
;
1245 switch (ctrl
->bRequest
) {
1247 case USB_REQ_GET_DESCRIPTOR
:
1248 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1250 switch (wValue
>> 8) {
1253 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1254 value
= min(wLength
, (u16
) sizeof device_desc
);
1255 memcpy(req
->buf
, &device_desc
, value
);
1257 case USB_DT_DEVICE_QUALIFIER
:
1258 if (!gadget_is_dualspeed(gadget
))
1260 value
= min(wLength
, (u16
) sizeof dev_qualifier
);
1261 memcpy(req
->buf
, &dev_qualifier
, value
);
1264 case USB_DT_OTHER_SPEED_CONFIG
:
1265 if (!gadget_is_dualspeed(gadget
))
1269 value
= config_buf(gadget
, req
->buf
,
1272 gadget_is_otg(gadget
));
1274 value
= min(wLength
, (u16
) value
);
1278 value
= usb_gadget_get_string(&stringtab
,
1279 wValue
& 0xff, req
->buf
);
1282 value
= min(wLength
, (u16
) value
);
1288 case USB_REQ_SET_CONFIGURATION
:
1289 if (ctrl
->bRequestType
!= 0)
1291 if (gadget
->a_hnp_support
)
1292 debug("HNP available\n");
1293 else if (gadget
->a_alt_hnp_support
)
1294 debug("HNP needs a different root port\n");
1295 value
= eth_set_config(dev
, wValue
, GFP_ATOMIC
);
1297 case USB_REQ_GET_CONFIGURATION
:
1298 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1300 *(u8
*)req
->buf
= dev
->config
;
1301 value
= min(wLength
, (u16
) 1);
1304 case USB_REQ_SET_INTERFACE
:
1305 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
1309 if (!cdc_active(dev
) && wIndex
!= 0)
1313 * PXA hardware partially handles SET_INTERFACE;
1314 * we need to kluge around that interference.
1316 if (gadget_is_pxa(gadget
)) {
1317 value
= eth_set_config(dev
, DEV_CONFIG_VALUE
,
1320 * PXA25x driver use non-CDC ethernet gadget.
1321 * But only _CDC and _RNDIS code can signalize
1322 * that network is working. So we signalize it
1325 l_ethdev
.network_started
= 1;
1326 debug("USB network up!\n");
1330 #ifdef CONFIG_USB_ETH_CDC
1332 case 0: /* control/master intf */
1336 usb_ep_disable(dev
->status_ep
);
1337 usb_ep_enable(dev
->status_ep
, dev
->status
);
1342 case 1: /* data intf */
1345 usb_ep_disable(dev
->in_ep
);
1346 usb_ep_disable(dev
->out_ep
);
1349 * CDC requires the data transfers not be done from
1350 * the default interface setting ... also, setting
1351 * the non-default interface resets filters etc.
1354 if (!cdc_active(dev
))
1356 usb_ep_enable(dev
->in_ep
, dev
->in
);
1357 usb_ep_enable(dev
->out_ep
, dev
->out
);
1358 dev
->cdc_filter
= DEFAULT_FILTER
;
1360 issue_start_status(dev
);
1361 eth_start(dev
, GFP_ATOMIC
);
1368 * FIXME this is wrong, as is the assumption that
1369 * all non-PXA hardware talks real CDC ...
1371 debug("set_interface ignored!\n");
1372 #endif /* CONFIG_USB_ETH_CDC */
1376 case USB_REQ_GET_INTERFACE
:
1377 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1381 if (!(cdc_active(dev
) || rndis_active(dev
)) && wIndex
!= 0)
1384 /* for CDC, iff carrier is on, data interface is active. */
1385 if (rndis_active(dev
) || wIndex
!= 1)
1386 *(u8
*)req
->buf
= 0;
1388 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1389 /* carrier always ok ...*/
1390 *(u8
*)req
->buf
= 1 ;
1392 value
= min(wLength
, (u16
) 1);
1395 #ifdef CONFIG_USB_ETH_CDC
1396 case USB_CDC_SET_ETHERNET_PACKET_FILTER
:
1398 * see 6.2.30: no data, wIndex = interface,
1399 * wValue = packet filter bitmap
1401 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1406 debug("packet filter %02x\n", wValue
);
1407 dev
->cdc_filter
= wValue
;
1413 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1414 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1415 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1416 * case USB_CDC_GET_ETHERNET_STATISTIC:
1419 #endif /* CONFIG_USB_ETH_CDC */
1421 #ifdef CONFIG_USB_ETH_RNDIS
1423 * RNDIS uses the CDC command encapsulation mechanism to implement
1424 * an RPC scheme, with much getting/setting of attributes by OID.
1426 case USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1427 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1428 || !rndis_active(dev
)
1429 || wLength
> USB_BUFSIZ
1431 || rndis_control_intf
.bInterfaceNumber
1434 /* read the request, then process it */
1436 req
->complete
= rndis_command_complete
;
1437 /* later, rndis_control_ack () sends a notification */
1440 case USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1441 if ((USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1442 == ctrl
->bRequestType
1443 && rndis_active(dev
)
1444 /* && wLength >= 0x0400 */
1446 && rndis_control_intf
.bInterfaceNumber
1451 /* return the result */
1452 buf
= rndis_get_next_response(dev
->rndis_config
, &n
);
1454 memcpy(req
->buf
, buf
, n
);
1455 req
->complete
= rndis_response_complete
;
1456 rndis_free_response(dev
->rndis_config
, buf
);
1459 /* else stalls ... spec says to avoid that */
1465 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1466 ctrl
->bRequestType
, ctrl
->bRequest
,
1467 wValue
, wIndex
, wLength
);
1470 /* respond with data transfer before status phase? */
1472 debug("respond with data transfer before status phase\n");
1473 req
->length
= value
;
1474 req
->zero
= value
< wLength
1475 && (value
% gadget
->ep0
->maxpacket
) == 0;
1476 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1478 debug("ep_queue --> %d\n", value
);
1480 eth_setup_complete(gadget
->ep0
, req
);
1484 /* host either stalls (value < 0) or reports success */
1488 /*-------------------------------------------------------------------------*/
1490 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
);
1492 static int rx_submit(struct eth_dev
*dev
, struct usb_request
*req
,
1495 int retval
= -ENOMEM
;
1499 * Padding up to RX_EXTRA handles minor disagreements with host.
1500 * Normally we use the USB "terminate on short read" convention;
1501 * so allow up to (N*maxpacket), since that memory is normally
1502 * already allocated. Some hardware doesn't deal well with short
1503 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1504 * byte off the end (to force hardware errors on overflow).
1506 * RNDIS uses internal framing, and explicitly allows senders to
1507 * pad to end-of-packet. That's potentially nice for speed,
1508 * but means receivers can't recover synch on their own.
1511 debug("%s\n", __func__
);
1515 size
= (ETHER_HDR_SIZE
+ dev
->mtu
+ RX_EXTRA
);
1516 size
+= dev
->out_ep
->maxpacket
- 1;
1517 if (rndis_active(dev
))
1518 size
+= sizeof(struct rndis_packet_msg_type
);
1519 size
-= size
% dev
->out_ep
->maxpacket
;
1522 * Some platforms perform better when IP packets are aligned,
1523 * but on at least one, checksumming fails otherwise. Note:
1524 * RNDIS headers involve variable numbers of LE32 values.
1527 req
->buf
= (u8
*)net_rx_packets
[0];
1529 req
->complete
= rx_complete
;
1531 retval
= usb_ep_queue(dev
->out_ep
, req
, gfp_flags
);
1534 error("rx submit --> %d", retval
);
1539 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1541 struct eth_dev
*dev
= ep
->driver_data
;
1543 debug("%s: status %d\n", __func__
, req
->status
);
1544 switch (req
->status
) {
1545 /* normal completion */
1547 if (rndis_active(dev
)) {
1548 /* we know MaxPacketsPerTransfer == 1 here */
1549 int length
= rndis_rm_hdr(req
->buf
, req
->actual
);
1552 req
->length
-= length
;
1553 req
->actual
-= length
;
1555 if (req
->actual
< ETH_HLEN
|| ETH_FRAME_LEN
< req
->actual
) {
1557 dev
->stats
.rx_errors
++;
1558 dev
->stats
.rx_length_errors
++;
1559 debug("rx length %d\n", req
->length
);
1563 dev
->stats
.rx_packets
++;
1564 dev
->stats
.rx_bytes
+= req
->length
;
1567 /* software-driven interface shutdown */
1568 case -ECONNRESET
: /* unlink */
1569 case -ESHUTDOWN
: /* disconnect etc */
1570 /* for hardware automagic (such as pxa) */
1571 case -ECONNABORTED
: /* endpoint reset */
1576 dev
->stats
.rx_over_errors
++;
1579 dev
->stats
.rx_errors
++;
1583 packet_received
= 1;
1586 static int alloc_requests(struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
)
1589 dev
->tx_req
= usb_ep_alloc_request(dev
->in_ep
, 0);
1594 dev
->rx_req
= usb_ep_alloc_request(dev
->out_ep
, 0);
1602 usb_ep_free_request(dev
->in_ep
, dev
->tx_req
);
1604 error("can't alloc requests");
1608 static void tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1610 struct eth_dev
*dev
= ep
->driver_data
;
1612 debug("%s: status %s\n", __func__
, (req
->status
) ? "failed" : "ok");
1613 switch (req
->status
) {
1615 dev
->stats
.tx_errors
++;
1616 debug("tx err %d\n", req
->status
);
1618 case -ECONNRESET
: /* unlink */
1619 case -ESHUTDOWN
: /* disconnect etc */
1622 dev
->stats
.tx_bytes
+= req
->length
;
1624 dev
->stats
.tx_packets
++;
1629 static inline int eth_is_promisc(struct eth_dev
*dev
)
1631 /* no filters for the CDC subset; always promisc */
1632 if (subset_active(dev
))
1634 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1638 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1640 struct eth_dev
*dev
= netdev_priv(net
);
1641 int length
= skb
->len
;
1643 struct usb_request
*req
= NULL
;
1644 unsigned long flags
;
1646 /* apply outgoing CDC or RNDIS filters */
1647 if (!eth_is_promisc (dev
)) {
1648 u8
*dest
= skb
->data
;
1650 if (is_multicast_ethaddr(dest
)) {
1653 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1654 * SET_ETHERNET_MULTICAST_FILTERS requests
1656 if (is_broadcast_ethaddr(dest
))
1657 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1659 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1660 if (!(dev
->cdc_filter
& type
)) {
1661 dev_kfree_skb_any (skb
);
1665 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1668 spin_lock_irqsave(&dev
->req_lock
, flags
);
1670 * this freelist can be empty if an interrupt triggered disconnect()
1671 * and reconfigured the gadget (shutting down this queue) after the
1672 * network stack decided to xmit but before we got the spinlock.
1674 if (list_empty(&dev
->tx_reqs
)) {
1675 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1679 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1680 list_del (&req
->list
);
1682 /* temporarily stop TX queue when the freelist empties */
1683 if (list_empty (&dev
->tx_reqs
))
1684 netif_stop_queue (net
);
1685 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1687 /* no buffer copies needed, unless the network stack did it
1688 * or the hardware can't use skb buffers.
1689 * or there's not enough space for any RNDIS headers we need
1691 if (rndis_active(dev
)) {
1692 struct sk_buff
*skb_rndis
;
1694 skb_rndis
= skb_realloc_headroom (skb
,
1695 sizeof (struct rndis_packet_msg_type
));
1699 dev_kfree_skb_any (skb
);
1701 rndis_add_hdr (skb
);
1704 req
->buf
= skb
->data
;
1706 req
->complete
= tx_complete
;
1708 /* use zlp framing on tx for strict CDC-Ether conformance,
1709 * though any robust network rx path ignores extra padding.
1710 * and some hardware doesn't like to write zlps.
1713 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
1716 req
->length
= length
;
1718 /* throttle highspeed IRQ rate back slightly */
1719 if (gadget_is_dualspeed(dev
->gadget
))
1720 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
1721 ? ((atomic_read(&dev
->tx_qlen
) % qmult
) != 0)
1724 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
1727 DEBUG (dev
, "tx queue err %d\n", retval
);
1730 net
->trans_start
= jiffies
;
1731 atomic_inc (&dev
->tx_qlen
);
1736 dev
->stats
.tx_dropped
++;
1737 dev_kfree_skb_any (skb
);
1738 spin_lock_irqsave(&dev
->req_lock
, flags
);
1739 if (list_empty (&dev
->tx_reqs
))
1740 netif_start_queue (net
);
1741 list_add (&req
->list
, &dev
->tx_reqs
);
1742 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1747 /*-------------------------------------------------------------------------*/
1750 static void eth_unbind(struct usb_gadget
*gadget
)
1752 struct eth_dev
*dev
= get_gadget_data(gadget
);
1754 debug("%s...\n", __func__
);
1755 rndis_deregister(dev
->rndis_config
);
1758 /* we've already been disconnected ... no i/o is active */
1760 usb_ep_free_request(gadget
->ep0
, dev
->req
);
1763 if (dev
->stat_req
) {
1764 usb_ep_free_request(dev
->status_ep
, dev
->stat_req
);
1765 dev
->stat_req
= NULL
;
1769 usb_ep_free_request(dev
->in_ep
, dev
->tx_req
);
1774 usb_ep_free_request(dev
->out_ep
, dev
->rx_req
);
1778 /* unregister_netdev (dev->net);*/
1779 /* free_netdev(dev->net);*/
1782 set_gadget_data(gadget
, NULL
);
1785 static void eth_disconnect(struct usb_gadget
*gadget
)
1787 eth_reset_config(get_gadget_data(gadget
));
1788 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1791 static void eth_suspend(struct usb_gadget
*gadget
)
1796 static void eth_resume(struct usb_gadget
*gadget
)
1801 /*-------------------------------------------------------------------------*/
1803 #ifdef CONFIG_USB_ETH_RNDIS
1806 * The interrupt endpoint is used in RNDIS to notify the host when messages
1807 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1808 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1809 * REMOTE_NDIS_KEEPALIVE_MSG.
1811 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1812 * normally just one notification will be queued.
1815 static void rndis_control_ack_complete(struct usb_ep
*ep
,
1816 struct usb_request
*req
)
1818 struct eth_dev
*dev
= ep
->driver_data
;
1820 debug("%s...\n", __func__
);
1821 if (req
->status
|| req
->actual
!= req
->length
)
1822 debug("rndis control ack complete --> %d, %d/%d\n",
1823 req
->status
, req
->actual
, req
->length
);
1825 if (!l_ethdev
.network_started
) {
1826 if (rndis_get_state(dev
->rndis_config
)
1827 == RNDIS_DATA_INITIALIZED
) {
1828 l_ethdev
.network_started
= 1;
1829 printf("USB RNDIS network up!\n");
1833 req
->context
= NULL
;
1835 if (req
!= dev
->stat_req
)
1836 usb_ep_free_request(ep
, req
);
1839 static char rndis_resp_buf
[8] __attribute__((aligned(sizeof(__le32
))));
1841 static int rndis_control_ack(struct eth_device
*net
)
1843 struct eth_dev
*dev
= &l_ethdev
;
1845 struct usb_request
*resp
= dev
->stat_req
;
1847 /* in case RNDIS calls this after disconnect */
1849 debug("status ENODEV\n");
1853 /* in case queue length > 1 */
1854 if (resp
->context
) {
1855 resp
= usb_ep_alloc_request(dev
->status_ep
, GFP_ATOMIC
);
1858 resp
->buf
= rndis_resp_buf
;
1862 * Send RNDIS RESPONSE_AVAILABLE notification;
1863 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1866 resp
->complete
= rndis_control_ack_complete
;
1867 resp
->context
= dev
;
1869 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32(1);
1870 *((__le32
*) (resp
->buf
+ 4)) = __constant_cpu_to_le32(0);
1872 length
= usb_ep_queue(dev
->status_ep
, resp
, GFP_ATOMIC
);
1875 rndis_control_ack_complete(dev
->status_ep
, resp
);
1883 #define rndis_control_ack NULL
1887 static void eth_start(struct eth_dev
*dev
, gfp_t gfp_flags
)
1889 if (rndis_active(dev
)) {
1890 rndis_set_param_medium(dev
->rndis_config
,
1892 BITRATE(dev
->gadget
)/100);
1893 rndis_signal_connect(dev
->rndis_config
);
1897 static int eth_stop(struct eth_dev
*dev
)
1899 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1901 unsigned long timeout
= CONFIG_SYS_HZ
; /* 1 sec to stop RNDIS */
1904 if (rndis_active(dev
)) {
1905 rndis_set_param_medium(dev
->rndis_config
, NDIS_MEDIUM_802_3
, 0);
1906 rndis_signal_disconnect(dev
->rndis_config
);
1908 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1909 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1911 while (get_timer(ts
) < timeout
)
1912 usb_gadget_handle_interrupts(0);
1915 rndis_uninit(dev
->rndis_config
);
1922 /*-------------------------------------------------------------------------*/
1924 static int is_eth_addr_valid(char *str
)
1926 if (strlen(str
) == 17) {
1931 /* see if it looks like an ethernet address */
1935 for (i
= 0; i
< 6; i
++) {
1936 char term
= (i
== 5 ? '\0' : ':');
1938 ea
[i
] = simple_strtol(p
, &q
, 16);
1940 if ((q
- p
) != 2 || *q
++ != term
)
1946 /* Now check the contents. */
1947 return is_valid_ethaddr(ea
);
1952 static u8
nibble(unsigned char c
)
1954 if (likely(isdigit(c
)))
1957 if (likely(isxdigit(c
)))
1958 return 10 + c
- 'A';
1962 static int get_ether_addr(const char *str
, u8
*dev_addr
)
1967 for (i
= 0; i
< 6; i
++) {
1970 if ((*str
== '.') || (*str
== ':'))
1972 num
= nibble(*str
++) << 4;
1973 num
|= (nibble(*str
++));
1976 if (is_valid_ethaddr(dev_addr
))
1982 static int eth_bind(struct usb_gadget
*gadget
)
1984 struct eth_dev
*dev
= &l_ethdev
;
1985 u8 cdc
= 1, zlp
= 1, rndis
= 1;
1986 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
1987 int status
= -ENOMEM
;
1991 /* these flags are only ever cleared; compiler take note */
1992 #ifndef CONFIG_USB_ETH_CDC
1995 #ifndef CONFIG_USB_ETH_RNDIS
1999 * Because most host side USB stacks handle CDC Ethernet, that
2000 * standard protocol is _strongly_ preferred for interop purposes.
2001 * (By everyone except Microsoft.)
2003 if (gadget_is_pxa(gadget
)) {
2004 /* pxa doesn't support altsettings */
2006 } else if (gadget_is_musbhdrc(gadget
)) {
2007 /* reduce tx dma overhead by avoiding special cases */
2009 } else if (gadget_is_sh(gadget
)) {
2010 /* sh doesn't support multiple interfaces or configs */
2013 } else if (gadget_is_sa1100(gadget
)) {
2014 /* hardware can't write zlps */
2017 * sa1100 CAN do CDC, without status endpoint ... we use
2018 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2023 gcnum
= usb_gadget_controller_number(gadget
);
2025 device_desc
.bcdDevice
= cpu_to_le16(0x0300 + gcnum
);
2028 * can't assume CDC works. don't want to default to
2029 * anything less functional on CDC-capable hardware,
2030 * so we fail in this case.
2032 error("controller '%s' not recognized",
2038 * If there's an RNDIS configuration, that's what Windows wants to
2039 * be using ... so use these product IDs here and in the "linux.inf"
2040 * needed to install MSFT drivers. Current Linux kernels will use
2041 * the second configuration if it's CDC Ethernet, and need some help
2042 * to choose the right configuration otherwise.
2045 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2046 device_desc
.idVendor
=
2047 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID
);
2048 device_desc
.idProduct
=
2049 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID
);
2051 device_desc
.idVendor
=
2052 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2053 device_desc
.idProduct
=
2054 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2056 sprintf(product_desc
, "RNDIS/%s", driver_desc
);
2059 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2060 * drivers aren't widely available. (That may be improved by
2061 * supporting one submode of the "SAFE" variant of MDLM.)
2064 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2065 device_desc
.idVendor
= cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID
);
2066 device_desc
.idProduct
= cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID
);
2069 device_desc
.idVendor
=
2070 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2071 device_desc
.idProduct
=
2072 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2076 /* support optional vendor/distro customization */
2078 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2080 strlcpy(manufacturer
, iManufacturer
, sizeof manufacturer
);
2082 strlcpy(product_desc
, iProduct
, sizeof product_desc
);
2083 if (iSerialNumber
) {
2084 device_desc
.iSerialNumber
= STRING_SERIALNUMBER
,
2085 strlcpy(serial_number
, iSerialNumber
, sizeof serial_number
);
2088 /* all we really need is bulk IN/OUT */
2089 usb_ep_autoconfig_reset(gadget
);
2090 in_ep
= usb_ep_autoconfig(gadget
, &fs_source_desc
);
2093 error("can't autoconfigure on %s\n",
2097 in_ep
->driver_data
= in_ep
; /* claim */
2099 out_ep
= usb_ep_autoconfig(gadget
, &fs_sink_desc
);
2102 out_ep
->driver_data
= out_ep
; /* claim */
2104 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2106 * CDC Ethernet control interface doesn't require a status endpoint.
2107 * Since some hosts expect one, try to allocate one anyway.
2110 status_ep
= usb_ep_autoconfig(gadget
, &fs_status_desc
);
2112 status_ep
->driver_data
= status_ep
; /* claim */
2114 error("can't run RNDIS on %s", gadget
->name
);
2116 #ifdef CONFIG_USB_ETH_CDC
2118 control_intf
.bNumEndpoints
= 0;
2119 /* FIXME remove endpoint from descriptor list */
2125 /* one config: cdc, else minimal subset */
2127 eth_config
.bNumInterfaces
= 1;
2128 eth_config
.iConfiguration
= STRING_SUBSET
;
2131 * use functions to set these up, in case we're built to work
2132 * with multiple controllers and must override CDC Ethernet.
2134 fs_subset_descriptors();
2135 hs_subset_descriptors();
2138 usb_gadget_set_selfpowered(gadget
);
2140 /* For now RNDIS is always a second config */
2142 device_desc
.bNumConfigurations
= 2;
2144 if (gadget_is_dualspeed(gadget
)) {
2146 dev_qualifier
.bNumConfigurations
= 2;
2148 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2150 /* assumes ep0 uses the same value for both speeds ... */
2151 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2153 /* and that all endpoints are dual-speed */
2154 hs_source_desc
.bEndpointAddress
=
2155 fs_source_desc
.bEndpointAddress
;
2156 hs_sink_desc
.bEndpointAddress
=
2157 fs_sink_desc
.bEndpointAddress
;
2158 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2160 hs_status_desc
.bEndpointAddress
=
2161 fs_status_desc
.bEndpointAddress
;
2165 if (gadget_is_otg(gadget
)) {
2166 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2167 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2168 eth_config
.bMaxPower
= 4;
2169 #ifdef CONFIG_USB_ETH_RNDIS
2170 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2171 rndis_config
.bMaxPower
= 4;
2176 /* network device setup */
2177 dev
->net
= &l_netdev
;
2183 dev
->out_ep
= out_ep
;
2184 dev
->status_ep
= status_ep
;
2187 * Module params for these addresses should come from ID proms.
2188 * The host side address is used with CDC and RNDIS, and commonly
2189 * ends up in a persistent config database. It's not clear if
2190 * host side code for the SAFE thing cares -- its original BLAN
2191 * thing didn't, Sharp never assigned those addresses on Zaurii.
2193 get_ether_addr(dev_addr
, dev
->net
->enetaddr
);
2195 memset(tmp
, 0, sizeof(tmp
));
2196 memcpy(tmp
, dev
->net
->enetaddr
, sizeof(dev
->net
->enetaddr
));
2198 get_ether_addr(host_addr
, dev
->host_mac
);
2200 sprintf(ethaddr
, "%02X%02X%02X%02X%02X%02X",
2201 dev
->host_mac
[0], dev
->host_mac
[1],
2202 dev
->host_mac
[2], dev
->host_mac
[3],
2203 dev
->host_mac
[4], dev
->host_mac
[5]);
2206 status
= rndis_init();
2208 error("can't init RNDIS, %d", status
);
2214 * use PKTSIZE (or aligned... from u-boot) and set
2215 * wMaxSegmentSize accordingly
2217 dev
->mtu
= PKTSIZE_ALIGN
; /* RNDIS does not like this, only 1514, TODO*/
2219 /* preallocate control message data and buffer */
2220 dev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
2223 dev
->req
->buf
= control_req
;
2224 dev
->req
->complete
= eth_setup_complete
;
2226 /* ... and maybe likewise for status transfer */
2227 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2228 if (dev
->status_ep
) {
2229 dev
->stat_req
= usb_ep_alloc_request(dev
->status_ep
,
2231 if (!dev
->stat_req
) {
2232 usb_ep_free_request(dev
->status_ep
, dev
->req
);
2236 dev
->stat_req
->buf
= status_req
;
2237 dev
->stat_req
->context
= NULL
;
2241 /* finish hookup to lower layer ... */
2242 dev
->gadget
= gadget
;
2243 set_gadget_data(gadget
, dev
);
2244 gadget
->ep0
->driver_data
= dev
;
2247 * two kinds of host-initiated state changes:
2248 * - iff DATA transfer is active, carrier is "on"
2249 * - tx queueing enabled if open *and* carrier is "on"
2252 printf("using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2253 out_ep
->name
, in_ep
->name
,
2254 status_ep
? " STATUS " : "",
2255 status_ep
? status_ep
->name
: ""
2257 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2258 dev
->net
->enetaddr
[0], dev
->net
->enetaddr
[1],
2259 dev
->net
->enetaddr
[2], dev
->net
->enetaddr
[3],
2260 dev
->net
->enetaddr
[4], dev
->net
->enetaddr
[5]);
2263 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2264 dev
->host_mac
[0], dev
->host_mac
[1],
2265 dev
->host_mac
[2], dev
->host_mac
[3],
2266 dev
->host_mac
[4], dev
->host_mac
[5]);
2271 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2273 dev
->rndis_config
= rndis_register(rndis_control_ack
);
2274 if (dev
->rndis_config
< 0) {
2277 debug("RNDIS setup failed\n");
2282 /* these set up a lot of the OIDs that RNDIS needs */
2283 rndis_set_host_mac(dev
->rndis_config
, dev
->host_mac
);
2284 if (rndis_set_param_dev(dev
->rndis_config
, dev
->net
, dev
->mtu
,
2285 &dev
->stats
, &dev
->cdc_filter
))
2287 if (rndis_set_param_vendor(dev
->rndis_config
, vendorID
,
2290 if (rndis_set_param_medium(dev
->rndis_config
,
2291 NDIS_MEDIUM_802_3
, 0))
2293 printf("RNDIS ready\n");
2298 error("%s failed, status = %d", __func__
, status
);
2303 /*-------------------------------------------------------------------------*/
2305 static int usb_eth_init(struct eth_device
*netdev
, bd_t
*bd
)
2307 struct eth_dev
*dev
= &l_ethdev
;
2308 struct usb_gadget
*gadget
;
2310 unsigned long timeout
= USB_CONNECT_TIMEOUT
;
2313 error("received NULL ptr");
2317 board_usb_init(0, USB_INIT_DEVICE
);
2319 /* Configure default mac-addresses for the USB ethernet device */
2320 #ifdef CONFIG_USBNET_DEV_ADDR
2321 strlcpy(dev_addr
, CONFIG_USBNET_DEV_ADDR
, sizeof(dev_addr
));
2323 #ifdef CONFIG_USBNET_HOST_ADDR
2324 strlcpy(host_addr
, CONFIG_USBNET_HOST_ADDR
, sizeof(host_addr
));
2326 /* Check if the user overruled the MAC addresses */
2327 if (getenv("usbnet_devaddr"))
2328 strlcpy(dev_addr
, getenv("usbnet_devaddr"),
2331 if (getenv("usbnet_hostaddr"))
2332 strlcpy(host_addr
, getenv("usbnet_hostaddr"),
2335 if (!is_eth_addr_valid(dev_addr
)) {
2336 error("Need valid 'usbnet_devaddr' to be set");
2339 if (!is_eth_addr_valid(host_addr
)) {
2340 error("Need valid 'usbnet_hostaddr' to be set");
2344 if (usb_gadget_register_driver(ð_driver
) < 0)
2347 dev
->network_started
= 0;
2349 packet_received
= 0;
2352 gadget
= dev
->gadget
;
2353 usb_gadget_connect(gadget
);
2355 if (getenv("cdc_connect_timeout"))
2356 timeout
= simple_strtoul(getenv("cdc_connect_timeout"),
2357 NULL
, 10) * CONFIG_SYS_HZ
;
2359 while (!l_ethdev
.network_started
) {
2360 /* Handle control-c and timeouts */
2361 if (ctrlc() || (get_timer(ts
) > timeout
)) {
2362 error("The remote end did not respond in time.");
2365 usb_gadget_handle_interrupts(0);
2368 packet_received
= 0;
2369 rx_submit(dev
, dev
->rx_req
, 0);
2375 static int usb_eth_send(struct eth_device
*netdev
, void *packet
, int length
)
2378 void *rndis_pkt
= NULL
;
2379 struct eth_dev
*dev
= &l_ethdev
;
2380 struct usb_request
*req
= dev
->tx_req
;
2382 unsigned long timeout
= USB_CONNECT_TIMEOUT
;
2384 debug("%s:...\n", __func__
);
2386 /* new buffer is needed to include RNDIS header */
2387 if (rndis_active(dev
)) {
2388 rndis_pkt
= malloc(length
+
2389 sizeof(struct rndis_packet_msg_type
));
2391 error("No memory to alloc RNDIS packet");
2394 rndis_add_hdr(rndis_pkt
, length
);
2395 memcpy(rndis_pkt
+ sizeof(struct rndis_packet_msg_type
),
2398 length
+= sizeof(struct rndis_packet_msg_type
);
2401 req
->context
= NULL
;
2402 req
->complete
= tx_complete
;
2405 * use zlp framing on tx for strict CDC-Ether conformance,
2406 * though any robust network rx path ignores extra padding.
2407 * and some hardware doesn't like to write zlps.
2410 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
2413 req
->length
= length
;
2415 /* throttle highspeed IRQ rate back slightly */
2416 if (gadget_is_dualspeed(dev
->gadget
))
2417 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
2418 ? ((dev
->tx_qlen
% qmult
) != 0) : 0;
2424 retval
= usb_ep_queue(dev
->in_ep
, req
, GFP_ATOMIC
);
2427 debug("%s: packet queued\n", __func__
);
2428 while (!packet_sent
) {
2429 if (get_timer(ts
) > timeout
) {
2430 printf("timeout sending packets to usb ethernet\n");
2433 usb_gadget_handle_interrupts(0);
2440 dev
->stats
.tx_dropped
++;
2444 static int usb_eth_recv(struct eth_device
*netdev
)
2446 struct eth_dev
*dev
= &l_ethdev
;
2448 usb_gadget_handle_interrupts(0);
2450 if (packet_received
) {
2451 debug("%s: packet received\n", __func__
);
2453 net_process_received_packet(net_rx_packets
[0],
2454 dev
->rx_req
->length
);
2455 packet_received
= 0;
2457 rx_submit(dev
, dev
->rx_req
, 0);
2459 error("dev->rx_req invalid");
2464 void usb_eth_halt(struct eth_device
*netdev
)
2466 struct eth_dev
*dev
= &l_ethdev
;
2469 error("received NULL ptr");
2473 /* If the gadget not registered, simple return */
2478 * Some USB controllers may need additional deinitialization here
2479 * before dropping pull-up (also due to hardware issues).
2480 * For example: unhandled interrupt with status stage started may
2481 * bring the controller to fully broken state (until board reset).
2482 * There are some variants to debug and fix such cases:
2483 * 1) In the case of RNDIS connection eth_stop can perform additional
2484 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2485 * 2) 'pullup' callback in your UDC driver can be improved to perform
2486 * this deinitialization.
2490 usb_gadget_disconnect(dev
->gadget
);
2492 /* Clear pending interrupt */
2493 if (dev
->network_started
) {
2494 usb_gadget_handle_interrupts(0);
2495 dev
->network_started
= 0;
2498 usb_gadget_unregister_driver(ð_driver
);
2499 board_usb_cleanup(0, USB_INIT_DEVICE
);
2502 static struct usb_gadget_driver eth_driver
= {
2506 .unbind
= eth_unbind
,
2509 .reset
= eth_disconnect
,
2510 .disconnect
= eth_disconnect
,
2512 .suspend
= eth_suspend
,
2513 .resume
= eth_resume
,
2516 int usb_eth_initialize(bd_t
*bi
)
2518 struct eth_device
*netdev
= &l_netdev
;
2520 strlcpy(netdev
->name
, USB_NET_NAME
, sizeof(netdev
->name
));
2522 netdev
->init
= usb_eth_init
;
2523 netdev
->send
= usb_eth_send
;
2524 netdev
->recv
= usb_eth_recv
;
2525 netdev
->halt
= usb_eth_halt
;
2527 #ifdef CONFIG_MCAST_TFTP
2528 #error not supported
2530 eth_register(netdev
);