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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <asm/errno.h>
25 #include <linux/netdevice.h>
26 #include <linux/usb/ch9.h>
27 #include <usbdescriptors.h>
28 #include <linux/usb/cdc.h>
29 #include <linux/usb/gadget.h>
32 #include <linux/ctype.h>
34 #include "gadget_chips.h"
37 #define USB_NET_NAME "usb_ether"
40 extern struct platform_data brd;
42 #define spin_unlock(x)
45 unsigned packet_received, packet_sent;
47 #define DEV_CONFIG_CDC 1
48 #define GFP_ATOMIC ((gfp_t) 0)
49 #define GFP_KERNEL ((gfp_t) 0)
52 * Ethernet gadget driver -- with CDC and non-CDC options
53 * Builds on hardware support for a full duplex link.
55 * CDC Ethernet is the standard USB solution for sending Ethernet frames
56 * using USB. Real hardware tends to use the same framing protocol but look
57 * different for control features. This driver strongly prefers to use
58 * this USB-IF standard as its open-systems interoperability solution;
59 * most host side USB stacks (except from Microsoft) support it.
61 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
62 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
63 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
65 * There's some hardware that can't talk CDC ECM. We make that hardware
66 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
67 * link-level setup only requires activating the configuration. Only the
68 * endpoint descriptors, and product/vendor IDs, are relevant; no control
69 * operations are available. Linux supports it, but other host operating
70 * systems may not. (This is a subset of CDC Ethernet.)
72 * It turns out that if you add a few descriptors to that "CDC Subset",
73 * (Windows) host side drivers from MCCI can treat it as one submode of
74 * a proprietary scheme called "SAFE" ... without needing to know about
75 * specific product/vendor IDs. So we do that, making it easier to use
76 * those MS-Windows drivers. Those added descriptors make it resemble a
77 * CDC MDLM device, but they don't change device behavior at all. (See
78 * MCCI Engineering report 950198 "SAFE Networking Functions".)
80 * A third option is also in use. Rather than CDC Ethernet, or something
81 * simpler, Microsoft pushes their own approach: RNDIS. The published
82 * RNDIS specs are ambiguous and appear to be incomplete, and are also
83 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
85 #define ETH_ALEN 6 /* Octets in one ethernet addr */
86 #define ETH_HLEN 14 /* Total octets in header. */
87 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
88 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
89 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
90 #define ETH_FCS_LEN 4 /* Octets in the FCS */
92 #define DRIVER_DESC "Ethernet Gadget"
93 /* Based on linux 2.6.27 version */
94 #define DRIVER_VERSION "May Day 2005"
96 static const char shortname[] = "ether";
97 static const char driver_desc[] = DRIVER_DESC;
99 #define RX_EXTRA 20 /* guard against rx overflows */
101 #ifndef CONFIG_USB_ETH_RNDIS
102 #define rndis_uninit(x) do {} while (0)
103 #define rndis_deregister(c) do {} while (0)
104 #define rndis_exit() do {} while (0)
107 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
108 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
109 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
110 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
111 |USB_CDC_PACKET_TYPE_DIRECTED)
113 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
115 /*-------------------------------------------------------------------------*/
118 struct usb_gadget *gadget;
119 struct usb_request *req; /* for control responses */
120 struct usb_request *stat_req; /* for cdc & rndis status */
123 struct usb_ep *in_ep, *out_ep, *status_ep;
124 const struct usb_endpoint_descriptor
127 struct usb_request *tx_req, *rx_req;
129 struct eth_device *net;
130 struct net_device_stats stats;
131 unsigned int tx_qlen;
136 unsigned suspended:1;
137 unsigned network_started:1;
141 #define WORK_RX_MEMORY 0
143 u8 host_mac[ETH_ALEN];
147 * This version autoconfigures as much as possible at run-time.
149 * It also ASSUMES a self-powered device, without remote wakeup,
150 * although remote wakeup support would make sense.
153 /*-------------------------------------------------------------------------*/
154 static struct eth_dev l_ethdev;
155 static struct eth_device l_netdev;
156 static struct usb_gadget_driver eth_driver;
158 /*-------------------------------------------------------------------------*/
160 /* "main" config is either CDC, or its simple subset */
161 static inline int is_cdc(struct eth_dev *dev)
163 #if !defined(DEV_CONFIG_SUBSET)
164 return 1; /* only cdc possible */
165 #elif !defined(DEV_CONFIG_CDC)
166 return 0; /* only subset possible */
168 return dev->cdc; /* depends on what hardware we found */
172 /* "secondary" RNDIS config may sometimes be activated */
173 static inline int rndis_active(struct eth_dev *dev)
175 #ifdef CONFIG_USB_ETH_RNDIS
182 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
183 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
185 #define DEFAULT_QLEN 2 /* double buffering by default */
187 /* peak bulk transfer bits-per-second */
188 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
189 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
191 #ifdef CONFIG_USB_GADGET_DUALSPEED
192 #define DEVSPEED USB_SPEED_HIGH
194 #ifdef CONFIG_USB_ETH_QMULT
195 #define qmult CONFIG_USB_ETH_QMULT
200 /* for dual-speed hardware, use deeper queues at highspeed */
201 #define qlen(gadget) \
202 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
204 static inline int BITRATE(struct usb_gadget *g)
206 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
209 #else /* full speed (low speed doesn't do bulk) */
213 #define DEVSPEED USB_SPEED_FULL
215 #define qlen(gadget) DEFAULT_QLEN
217 static inline int BITRATE(struct usb_gadget *g)
223 /*-------------------------------------------------------------------------*/
226 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
227 * Instead: allocate your own, using normal USB-IF procedures.
231 * Thanks to NetChip Technologies for donating this product ID.
232 * It's for devices with only CDC Ethernet configurations.
234 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
235 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
238 * For hardware that can't talk CDC, we use the same vendor ID that
239 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
240 * with pxa250. We're protocol-compatible, if the host-side drivers
241 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
242 * drivers that need to hard-wire endpoint numbers have a hook.
244 * The protocol is a minimal subset of CDC Ether, which works on any bulk
245 * hardware that's not deeply broken ... even on hardware that can't talk
246 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
247 * doesn't handle control-OUT).
249 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
250 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
253 * For hardware that can talk RNDIS and either of the above protocols,
254 * use this ID ... the windows INF files will know it. Unless it's
255 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
256 * the non-RNDIS configuration.
258 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
259 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
262 * Some systems will want different product identifers published in the
263 * device descriptor, either numbers or strings or both. These string
264 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
268 * Emulating them in eth_bind:
269 * static ushort idVendor;
270 * static ushort idProduct;
273 #if defined(CONFIG_USBNET_MANUFACTURER)
274 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
276 static char *iManufacturer = "U-boot";
279 /* These probably need to be configurable. */
280 static ushort bcdDevice;
281 static char *iProduct;
282 static char *iSerialNumber;
284 static char dev_addr[18];
286 static char host_addr[18];
289 /*-------------------------------------------------------------------------*/
292 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
293 * ep0 implementation: descriptors, config management, setup().
294 * also optional class-specific notification interrupt transfer.
298 * DESCRIPTORS ... most are static, but strings and (full) configuration
299 * descriptors are built on demand. For now we do either full CDC, or
300 * our simple subset, with RNDIS as an optional second configuration.
302 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
303 * the class descriptors match a modem (they're ignored; it's really just
304 * Ethernet functionality), they don't need the NOP altsetting, and the
305 * status transfer endpoint isn't optional.
308 #define STRING_MANUFACTURER 1
309 #define STRING_PRODUCT 2
310 #define STRING_ETHADDR 3
311 #define STRING_DATA 4
312 #define STRING_CONTROL 5
313 #define STRING_RNDIS_CONTROL 6
315 #define STRING_SUBSET 8
316 #define STRING_RNDIS 9
317 #define STRING_SERIALNUMBER 10
319 /* holds our biggest descriptor (or RNDIS response) */
320 #define USB_BUFSIZ 256
323 * This device advertises one configuration, eth_config, unless RNDIS
324 * is enabled (rndis_config) on hardware supporting at least two configs.
326 * NOTE: Controllers like superh_udc should probably be able to use
327 * an RNDIS-only configuration.
329 * FIXME define some higher-powered configurations to make it easier
330 * to recharge batteries ...
333 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
334 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
336 static struct usb_device_descriptor
338 .bLength = sizeof device_desc,
339 .bDescriptorType = USB_DT_DEVICE,
341 .bcdUSB = __constant_cpu_to_le16(0x0200),
343 .bDeviceClass = USB_CLASS_COMM,
344 .bDeviceSubClass = 0,
345 .bDeviceProtocol = 0,
347 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
348 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
349 .iManufacturer = STRING_MANUFACTURER,
350 .iProduct = STRING_PRODUCT,
351 .bNumConfigurations = 1,
354 static struct usb_otg_descriptor
356 .bLength = sizeof otg_descriptor,
357 .bDescriptorType = USB_DT_OTG,
359 .bmAttributes = USB_OTG_SRP,
362 static struct usb_config_descriptor
364 .bLength = sizeof eth_config,
365 .bDescriptorType = USB_DT_CONFIG,
367 /* compute wTotalLength on the fly */
369 .bConfigurationValue = DEV_CONFIG_VALUE,
370 .iConfiguration = STRING_CDC,
371 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
375 #ifdef CONFIG_USB_ETH_RNDIS
376 static struct usb_config_descriptor
378 .bLength = sizeof rndis_config,
379 .bDescriptorType = USB_DT_CONFIG,
381 /* compute wTotalLength on the fly */
383 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
384 .iConfiguration = STRING_RNDIS,
385 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
391 * Compared to the simple CDC subset, the full CDC Ethernet model adds
392 * three class descriptors, two interface descriptors, optional status
393 * endpoint. Both have a "data" interface and two bulk endpoints.
394 * There are also differences in how control requests are handled.
396 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
397 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
398 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
399 * work around those bugs) are unlikely to ever come from MSFT, you may
400 * wish to avoid using RNDIS.
402 * MCCI offers an alternative to RNDIS if you need to connect to Windows
403 * but have hardware that can't support CDC Ethernet. We add descriptors
404 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
405 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
406 * get those drivers from MCCI, or bundled with various products.
409 #ifdef DEV_CONFIG_CDC
410 static struct usb_interface_descriptor
412 .bLength = sizeof control_intf,
413 .bDescriptorType = USB_DT_INTERFACE,
415 .bInterfaceNumber = 0,
416 /* status endpoint is optional; this may be patched later */
418 .bInterfaceClass = USB_CLASS_COMM,
419 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
420 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
421 .iInterface = STRING_CONTROL,
425 #ifdef CONFIG_USB_ETH_RNDIS
426 static const struct usb_interface_descriptor
427 rndis_control_intf = {
428 .bLength = sizeof rndis_control_intf,
429 .bDescriptorType = USB_DT_INTERFACE,
431 .bInterfaceNumber = 0,
433 .bInterfaceClass = USB_CLASS_COMM,
434 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
435 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
436 .iInterface = STRING_RNDIS_CONTROL,
440 static const struct usb_cdc_header_desc header_desc = {
441 .bLength = sizeof header_desc,
442 .bDescriptorType = USB_DT_CS_INTERFACE,
443 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
445 .bcdCDC = __constant_cpu_to_le16(0x0110),
448 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
450 static const struct usb_cdc_union_desc union_desc = {
451 .bLength = sizeof union_desc,
452 .bDescriptorType = USB_DT_CS_INTERFACE,
453 .bDescriptorSubType = USB_CDC_UNION_TYPE,
455 .bMasterInterface0 = 0, /* index of control interface */
456 .bSlaveInterface0 = 1, /* index of DATA interface */
459 #endif /* CDC || RNDIS */
461 #ifdef CONFIG_USB_ETH_RNDIS
463 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
464 .bLength = sizeof call_mgmt_descriptor,
465 .bDescriptorType = USB_DT_CS_INTERFACE,
466 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
468 .bmCapabilities = 0x00,
469 .bDataInterface = 0x01,
472 static const struct usb_cdc_acm_descriptor acm_descriptor = {
473 .bLength = sizeof acm_descriptor,
474 .bDescriptorType = USB_DT_CS_INTERFACE,
475 .bDescriptorSubType = USB_CDC_ACM_TYPE,
477 .bmCapabilities = 0x00,
482 #ifndef DEV_CONFIG_CDC
485 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
486 * ways: data endpoints live in the control interface, there's no data
487 * interface, and it's not used to talk to a cell phone radio.
490 static const struct usb_cdc_mdlm_desc mdlm_desc = {
491 .bLength = sizeof mdlm_desc,
492 .bDescriptorType = USB_DT_CS_INTERFACE,
493 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
495 .bcdVersion = __constant_cpu_to_le16(0x0100),
497 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
498 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
503 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
504 * can't really use its struct. All we do here is say that we're using
505 * the submode of "SAFE" which directly matches the CDC Subset.
507 static const u8 mdlm_detail_desc[] = {
510 USB_CDC_MDLM_DETAIL_TYPE,
513 0, /* network control capabilities (none) */
514 0, /* network data capabilities ("raw" encapsulation) */
519 static const struct usb_cdc_ether_desc ether_desc = {
520 .bLength = sizeof(ether_desc),
521 .bDescriptorType = USB_DT_CS_INTERFACE,
522 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
524 /* this descriptor actually adds value, surprise! */
525 .iMACAddress = STRING_ETHADDR,
526 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
527 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
528 .wNumberMCFilters = __constant_cpu_to_le16(0),
529 .bNumberPowerFilters = 0,
532 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
535 * include the status endpoint if we can, even where it's optional.
536 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
537 * packet, to simplify cancellation; and a big transfer interval, to
538 * waste less bandwidth.
540 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
541 * if they ignore the connect/disconnect notifications that real aether
542 * can provide. more advanced cdc configurations might want to support
543 * encapsulated commands (vendor-specific, using control-OUT).
545 * RNDIS requires the status endpoint, since it uses that encapsulation
546 * mechanism for its funky RPC scheme.
549 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
550 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
552 static struct usb_endpoint_descriptor
554 .bLength = USB_DT_ENDPOINT_SIZE,
555 .bDescriptorType = USB_DT_ENDPOINT,
557 .bEndpointAddress = USB_DIR_IN,
558 .bmAttributes = USB_ENDPOINT_XFER_INT,
559 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
560 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
564 #ifdef DEV_CONFIG_CDC
566 /* the default data interface has no endpoints ... */
568 static const struct usb_interface_descriptor
570 .bLength = sizeof data_nop_intf,
571 .bDescriptorType = USB_DT_INTERFACE,
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
576 .bInterfaceClass = USB_CLASS_CDC_DATA,
577 .bInterfaceSubClass = 0,
578 .bInterfaceProtocol = 0,
581 /* ... but the "real" data interface has two bulk endpoints */
583 static const struct usb_interface_descriptor
585 .bLength = sizeof data_intf,
586 .bDescriptorType = USB_DT_INTERFACE,
588 .bInterfaceNumber = 1,
589 .bAlternateSetting = 1,
591 .bInterfaceClass = USB_CLASS_CDC_DATA,
592 .bInterfaceSubClass = 0,
593 .bInterfaceProtocol = 0,
594 .iInterface = STRING_DATA,
599 #ifdef CONFIG_USB_ETH_RNDIS
601 /* RNDIS doesn't activate by changing to the "real" altsetting */
603 static const struct usb_interface_descriptor
605 .bLength = sizeof rndis_data_intf,
606 .bDescriptorType = USB_DT_INTERFACE,
608 .bInterfaceNumber = 1,
609 .bAlternateSetting = 0,
611 .bInterfaceClass = USB_CLASS_CDC_DATA,
612 .bInterfaceSubClass = 0,
613 .bInterfaceProtocol = 0,
614 .iInterface = STRING_DATA,
619 #ifdef DEV_CONFIG_SUBSET
622 * "Simple" CDC-subset option is a simple vendor-neutral model that most
623 * full speed controllers can handle: one interface, two bulk endpoints.
625 * To assist host side drivers, we fancy it up a bit, and add descriptors
626 * so some host side drivers will understand it as a "SAFE" variant.
629 static const struct usb_interface_descriptor
631 .bLength = sizeof subset_data_intf,
632 .bDescriptorType = USB_DT_INTERFACE,
634 .bInterfaceNumber = 0,
635 .bAlternateSetting = 0,
637 .bInterfaceClass = USB_CLASS_COMM,
638 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
639 .bInterfaceProtocol = 0,
640 .iInterface = STRING_DATA,
645 static struct usb_endpoint_descriptor
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
654 static struct usb_endpoint_descriptor
656 .bLength = USB_DT_ENDPOINT_SIZE,
657 .bDescriptorType = USB_DT_ENDPOINT,
659 .bEndpointAddress = USB_DIR_OUT,
660 .bmAttributes = USB_ENDPOINT_XFER_BULK,
663 static const struct usb_descriptor_header *fs_eth_function[11] = {
664 (struct usb_descriptor_header *) &otg_descriptor,
665 #ifdef DEV_CONFIG_CDC
666 /* "cdc" mode descriptors */
667 (struct usb_descriptor_header *) &control_intf,
668 (struct usb_descriptor_header *) &header_desc,
669 (struct usb_descriptor_header *) &union_desc,
670 (struct usb_descriptor_header *) ðer_desc,
671 /* NOTE: status endpoint may need to be removed */
672 (struct usb_descriptor_header *) &fs_status_desc,
673 /* data interface, with altsetting */
674 (struct usb_descriptor_header *) &data_nop_intf,
675 (struct usb_descriptor_header *) &data_intf,
676 (struct usb_descriptor_header *) &fs_source_desc,
677 (struct usb_descriptor_header *) &fs_sink_desc,
679 #endif /* DEV_CONFIG_CDC */
682 static inline void fs_subset_descriptors(void)
684 #ifdef DEV_CONFIG_SUBSET
685 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
686 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
687 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
688 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
689 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
690 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
691 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
692 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
693 fs_eth_function[8] = NULL;
695 fs_eth_function[1] = NULL;
699 #ifdef CONFIG_USB_ETH_RNDIS
700 static const struct usb_descriptor_header *fs_rndis_function[] = {
701 (struct usb_descriptor_header *) &otg_descriptor,
702 /* control interface matches ACM, not Ethernet */
703 (struct usb_descriptor_header *) &rndis_control_intf,
704 (struct usb_descriptor_header *) &header_desc,
705 (struct usb_descriptor_header *) &call_mgmt_descriptor,
706 (struct usb_descriptor_header *) &acm_descriptor,
707 (struct usb_descriptor_header *) &union_desc,
708 (struct usb_descriptor_header *) &fs_status_desc,
709 /* data interface has no altsetting */
710 (struct usb_descriptor_header *) &rndis_data_intf,
711 (struct usb_descriptor_header *) &fs_source_desc,
712 (struct usb_descriptor_header *) &fs_sink_desc,
718 * usb 2.0 devices need to expose both high speed and full speed
719 * descriptors, unless they only run at full speed.
722 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
723 static struct usb_endpoint_descriptor
725 .bLength = USB_DT_ENDPOINT_SIZE,
726 .bDescriptorType = USB_DT_ENDPOINT,
728 .bmAttributes = USB_ENDPOINT_XFER_INT,
729 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
730 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
732 #endif /* DEV_CONFIG_CDC */
734 static struct usb_endpoint_descriptor
736 .bLength = USB_DT_ENDPOINT_SIZE,
737 .bDescriptorType = USB_DT_ENDPOINT,
739 .bmAttributes = USB_ENDPOINT_XFER_BULK,
740 .wMaxPacketSize = __constant_cpu_to_le16(512),
743 static struct usb_endpoint_descriptor
745 .bLength = USB_DT_ENDPOINT_SIZE,
746 .bDescriptorType = USB_DT_ENDPOINT,
748 .bmAttributes = USB_ENDPOINT_XFER_BULK,
749 .wMaxPacketSize = __constant_cpu_to_le16(512),
752 static struct usb_qualifier_descriptor
754 .bLength = sizeof dev_qualifier,
755 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
757 .bcdUSB = __constant_cpu_to_le16(0x0200),
758 .bDeviceClass = USB_CLASS_COMM,
760 .bNumConfigurations = 1,
763 static const struct usb_descriptor_header *hs_eth_function[11] = {
764 (struct usb_descriptor_header *) &otg_descriptor,
765 #ifdef DEV_CONFIG_CDC
766 /* "cdc" mode descriptors */
767 (struct usb_descriptor_header *) &control_intf,
768 (struct usb_descriptor_header *) &header_desc,
769 (struct usb_descriptor_header *) &union_desc,
770 (struct usb_descriptor_header *) ðer_desc,
771 /* NOTE: status endpoint may need to be removed */
772 (struct usb_descriptor_header *) &hs_status_desc,
773 /* data interface, with altsetting */
774 (struct usb_descriptor_header *) &data_nop_intf,
775 (struct usb_descriptor_header *) &data_intf,
776 (struct usb_descriptor_header *) &hs_source_desc,
777 (struct usb_descriptor_header *) &hs_sink_desc,
779 #endif /* DEV_CONFIG_CDC */
782 static inline void hs_subset_descriptors(void)
784 #ifdef DEV_CONFIG_SUBSET
785 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
786 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
787 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
788 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
789 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
790 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
791 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
792 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
793 hs_eth_function[8] = NULL;
795 hs_eth_function[1] = NULL;
799 #ifdef CONFIG_USB_ETH_RNDIS
800 static const struct usb_descriptor_header *hs_rndis_function[] = {
801 (struct usb_descriptor_header *) &otg_descriptor,
802 /* control interface matches ACM, not Ethernet */
803 (struct usb_descriptor_header *) &rndis_control_intf,
804 (struct usb_descriptor_header *) &header_desc,
805 (struct usb_descriptor_header *) &call_mgmt_descriptor,
806 (struct usb_descriptor_header *) &acm_descriptor,
807 (struct usb_descriptor_header *) &union_desc,
808 (struct usb_descriptor_header *) &hs_status_desc,
809 /* data interface has no altsetting */
810 (struct usb_descriptor_header *) &rndis_data_intf,
811 (struct usb_descriptor_header *) &hs_source_desc,
812 (struct usb_descriptor_header *) &hs_sink_desc,
818 /* maxpacket and other transfer characteristics vary by speed. */
819 static inline struct usb_endpoint_descriptor *
820 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
821 struct usb_endpoint_descriptor *fs)
823 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
828 /*-------------------------------------------------------------------------*/
830 /* descriptors that are built on-demand */
832 static char manufacturer[50];
833 static char product_desc[40] = DRIVER_DESC;
834 static char serial_number[20];
836 /* address that the host will use ... usually assigned at random */
837 static char ethaddr[2 * ETH_ALEN + 1];
839 /* static strings, in UTF-8 */
840 static struct usb_string strings[] = {
841 { STRING_MANUFACTURER, manufacturer, },
842 { STRING_PRODUCT, product_desc, },
843 { STRING_SERIALNUMBER, serial_number, },
844 { STRING_DATA, "Ethernet Data", },
845 { STRING_ETHADDR, ethaddr, },
846 #ifdef DEV_CONFIG_CDC
847 { STRING_CDC, "CDC Ethernet", },
848 { STRING_CONTROL, "CDC Communications Control", },
850 #ifdef DEV_CONFIG_SUBSET
851 { STRING_SUBSET, "CDC Ethernet Subset", },
853 #ifdef CONFIG_USB_ETH_RNDIS
854 { STRING_RNDIS, "RNDIS", },
855 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
857 { } /* end of list */
860 static struct usb_gadget_strings stringtab = {
861 .language = 0x0409, /* en-us */
865 /*============================================================================*/
866 static u8 control_req[USB_BUFSIZ];
867 static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
871 * strlcpy - Copy a %NUL terminated string into a sized buffer
872 * @dest: Where to copy the string to
873 * @src: Where to copy the string from
874 * @size: size of destination buffer
876 * Compatible with *BSD: the result is always a valid
877 * NUL-terminated string that fits in the buffer (unless,
878 * of course, the buffer size is zero). It does not pad
879 * out the result like strncpy() does.
881 size_t strlcpy(char *dest, const char *src, size_t size)
883 size_t ret = strlen(src);
886 size_t len = (ret >= size) ? size - 1 : ret;
887 memcpy(dest, src, len);
893 /*============================================================================*/
896 * one config, two interfaces: control, data.
897 * complications: class descriptors, and an altsetting.
900 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
903 const struct usb_config_descriptor *config;
904 const struct usb_descriptor_header **function;
907 if (gadget_is_dualspeed(g)) {
908 hs = (g->speed == USB_SPEED_HIGH);
909 if (type == USB_DT_OTHER_SPEED_CONFIG)
912 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
914 if (index >= device_desc.bNumConfigurations)
917 #ifdef CONFIG_USB_ETH_RNDIS
919 * list the RNDIS config first, to make Microsoft's drivers
920 * happy. DOCSIS 1.0 needs this too.
922 if (device_desc.bNumConfigurations == 2 && index == 0) {
923 config = &rndis_config;
924 function = which_fn(rndis);
928 config = ð_config;
929 function = which_fn(eth);
932 /* for now, don't advertise srp-only devices */
936 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
939 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
943 /*-------------------------------------------------------------------------*/
945 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
946 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
949 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
952 struct usb_gadget *gadget = dev->gadget;
954 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
955 /* status endpoint used for RNDIS and (optionally) CDC */
956 if (!subset_active(dev) && dev->status_ep) {
957 dev->status = ep_desc(gadget, &hs_status_desc,
959 dev->status_ep->driver_data = dev;
961 result = usb_ep_enable(dev->status_ep, dev->status);
963 debug("enable %s --> %d\n",
964 dev->status_ep->name, result);
970 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
971 dev->in_ep->driver_data = dev;
973 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
974 dev->out_ep->driver_data = dev;
977 * With CDC, the host isn't allowed to use these two data
978 * endpoints in the default altsetting for the interface.
979 * so we don't activate them yet. Reset from SET_INTERFACE.
981 * Strictly speaking RNDIS should work the same: activation is
982 * a side effect of setting a packet filter. Deactivation is
983 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
985 if (!cdc_active(dev)) {
986 result = usb_ep_enable(dev->in_ep, dev->in);
988 debug("enable %s --> %d\n",
989 dev->in_ep->name, result);
993 result = usb_ep_enable(dev->out_ep, dev->out);
995 debug("enable %s --> %d\n",
996 dev->out_ep->name, result);
1003 result = alloc_requests(dev, qlen(gadget), gfp_flags);
1005 /* on error, disable any endpoints */
1007 if (!subset_active(dev) && dev->status_ep)
1008 (void) usb_ep_disable(dev->status_ep);
1010 (void) usb_ep_disable(dev->in_ep);
1011 (void) usb_ep_disable(dev->out_ep);
1014 } else if (!cdc_active(dev)) {
1016 * activate non-CDC configs right away
1017 * this isn't strictly according to the RNDIS spec
1019 eth_start(dev, GFP_ATOMIC);
1022 /* caller is responsible for cleanup on error */
1026 static void eth_reset_config(struct eth_dev *dev)
1028 if (dev->config == 0)
1031 debug("%s\n", __func__);
1033 rndis_uninit(dev->rndis_config);
1036 * disable endpoints, forcing (synchronous) completion of
1037 * pending i/o. then free the requests.
1041 usb_ep_disable(dev->in_ep);
1043 usb_ep_free_request(dev->in_ep, dev->tx_req);
1048 usb_ep_disable(dev->out_ep);
1050 usb_ep_free_request(dev->out_ep, dev->rx_req);
1055 usb_ep_disable(dev->status_ep);
1058 dev->cdc_filter = 0;
1063 * change our operational config. must agree with the code
1064 * that returns config descriptors, and altsetting code.
1066 static int eth_set_config(struct eth_dev *dev, unsigned number,
1070 struct usb_gadget *gadget = dev->gadget;
1072 if (gadget_is_sa1100(gadget)
1074 && dev->tx_qlen != 0) {
1075 /* tx fifo is full, but we can't clear it...*/
1076 error("can't change configurations");
1079 eth_reset_config(dev);
1082 case DEV_CONFIG_VALUE:
1083 result = set_ether_config(dev, gfp_flags);
1085 #ifdef CONFIG_USB_ETH_RNDIS
1086 case DEV_RNDIS_CONFIG_VALUE:
1088 result = set_ether_config(dev, gfp_flags);
1100 eth_reset_config(dev);
1101 usb_gadget_vbus_draw(dev->gadget,
1102 gadget_is_otg(dev->gadget) ? 8 : 100);
1107 power = 2 * eth_config.bMaxPower;
1108 usb_gadget_vbus_draw(dev->gadget, power);
1110 switch (gadget->speed) {
1111 case USB_SPEED_FULL:
1112 speed = "full"; break;
1113 #ifdef CONFIG_USB_GADGET_DUALSPEED
1114 case USB_SPEED_HIGH:
1115 speed = "high"; break;
1121 dev->config = number;
1122 printf("%s speed config #%d: %d mA, %s, using %s\n",
1123 speed, number, power, driver_desc,
1128 : "CDC Ethernet Subset"));
1133 /*-------------------------------------------------------------------------*/
1135 #ifdef DEV_CONFIG_CDC
1138 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1139 * only to notify the host about link status changes (which we support) or
1140 * report completion of some encapsulated command (as used in RNDIS). Since
1141 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1142 * command mechanism; and only one status request is ever queued.
1144 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1146 struct usb_cdc_notification *event = req->buf;
1147 int value = req->status;
1148 struct eth_dev *dev = ep->driver_data;
1150 /* issue the second notification if host reads the first */
1151 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1153 __le32 *data = req->buf + sizeof *event;
1155 event->bmRequestType = 0xA1;
1156 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1157 event->wValue = __constant_cpu_to_le16(0);
1158 event->wIndex = __constant_cpu_to_le16(1);
1159 event->wLength = __constant_cpu_to_le16(8);
1161 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1162 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1164 req->length = STATUS_BYTECOUNT;
1165 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1166 debug("send SPEED_CHANGE --> %d\n", value);
1169 } else if (value != -ECONNRESET) {
1170 debug("event %02x --> %d\n",
1171 event->bNotificationType, value);
1172 if (event->bNotificationType ==
1173 USB_CDC_NOTIFY_SPEED_CHANGE) {
1174 l_ethdev.network_started = 1;
1175 printf("USB network up!\n");
1178 req->context = NULL;
1181 static void issue_start_status(struct eth_dev *dev)
1183 struct usb_request *req = dev->stat_req;
1184 struct usb_cdc_notification *event;
1190 * FIXME ugly idiom, maybe we'd be better with just
1191 * a "cancel the whole queue" primitive since any
1192 * unlink-one primitive has way too many error modes.
1193 * here, we "know" toggle is already clear...
1195 * FIXME iff req->context != null just dequeue it
1197 usb_ep_disable(dev->status_ep);
1198 usb_ep_enable(dev->status_ep, dev->status);
1201 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1202 * a SPEED_CHANGE. could be useful in some configs.
1205 event->bmRequestType = 0xA1;
1206 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1207 event->wValue = __constant_cpu_to_le16(1); /* connected */
1208 event->wIndex = __constant_cpu_to_le16(1);
1211 req->length = sizeof *event;
1212 req->complete = eth_status_complete;
1215 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1217 debug("status buf queue --> %d\n", value);
1222 /*-------------------------------------------------------------------------*/
1224 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1226 if (req->status || req->actual != req->length)
1227 debug("setup complete --> %d, %d/%d\n",
1228 req->status, req->actual, req->length);
1231 #ifdef CONFIG_USB_ETH_RNDIS
1233 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1235 if (req->status || req->actual != req->length)
1236 debug("rndis response complete --> %d, %d/%d\n",
1237 req->status, req->actual, req->length);
1239 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1242 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1244 struct eth_dev *dev = ep->driver_data;
1247 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1248 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1250 error("%s: rndis parse error %d", __func__, status);
1256 * The setup() callback implements all the ep0 functionality that's not
1257 * handled lower down. CDC has a number of less-common features:
1259 * - two interfaces: control, and ethernet data
1260 * - Ethernet data interface has two altsettings: default, and active
1261 * - class-specific descriptors for the control interface
1262 * - class-specific control requests
1265 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1267 struct eth_dev *dev = get_gadget_data(gadget);
1268 struct usb_request *req = dev->req;
1269 int value = -EOPNOTSUPP;
1270 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1271 u16 wValue = le16_to_cpu(ctrl->wValue);
1272 u16 wLength = le16_to_cpu(ctrl->wLength);
1275 * descriptors just go into the pre-allocated ep0 buffer,
1276 * while config change events may enable network traffic.
1279 debug("%s\n", __func__);
1281 req->complete = eth_setup_complete;
1282 switch (ctrl->bRequest) {
1284 case USB_REQ_GET_DESCRIPTOR:
1285 if (ctrl->bRequestType != USB_DIR_IN)
1287 switch (wValue >> 8) {
1290 value = min(wLength, (u16) sizeof device_desc);
1291 memcpy(req->buf, &device_desc, value);
1293 case USB_DT_DEVICE_QUALIFIER:
1294 if (!gadget_is_dualspeed(gadget))
1296 value = min(wLength, (u16) sizeof dev_qualifier);
1297 memcpy(req->buf, &dev_qualifier, value);
1300 case USB_DT_OTHER_SPEED_CONFIG:
1301 if (!gadget_is_dualspeed(gadget))
1305 value = config_buf(gadget, req->buf,
1308 gadget_is_otg(gadget));
1310 value = min(wLength, (u16) value);
1314 value = usb_gadget_get_string(&stringtab,
1315 wValue & 0xff, req->buf);
1318 value = min(wLength, (u16) value);
1324 case USB_REQ_SET_CONFIGURATION:
1325 if (ctrl->bRequestType != 0)
1327 if (gadget->a_hnp_support)
1328 debug("HNP available\n");
1329 else if (gadget->a_alt_hnp_support)
1330 debug("HNP needs a different root port\n");
1331 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1333 case USB_REQ_GET_CONFIGURATION:
1334 if (ctrl->bRequestType != USB_DIR_IN)
1336 *(u8 *)req->buf = dev->config;
1337 value = min(wLength, (u16) 1);
1340 case USB_REQ_SET_INTERFACE:
1341 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1345 if (!cdc_active(dev) && wIndex != 0)
1349 * PXA hardware partially handles SET_INTERFACE;
1350 * we need to kluge around that interference.
1352 if (gadget_is_pxa(gadget)) {
1353 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1358 #ifdef DEV_CONFIG_CDC
1360 case 0: /* control/master intf */
1364 usb_ep_disable(dev->status_ep);
1365 usb_ep_enable(dev->status_ep, dev->status);
1370 case 1: /* data intf */
1373 usb_ep_disable(dev->in_ep);
1374 usb_ep_disable(dev->out_ep);
1377 * CDC requires the data transfers not be done from
1378 * the default interface setting ... also, setting
1379 * the non-default interface resets filters etc.
1382 if (!cdc_active(dev))
1384 usb_ep_enable(dev->in_ep, dev->in);
1385 usb_ep_enable(dev->out_ep, dev->out);
1386 dev->cdc_filter = DEFAULT_FILTER;
1388 issue_start_status(dev);
1389 eth_start(dev, GFP_ATOMIC);
1396 * FIXME this is wrong, as is the assumption that
1397 * all non-PXA hardware talks real CDC ...
1399 debug("set_interface ignored!\n");
1400 #endif /* DEV_CONFIG_CDC */
1404 case USB_REQ_GET_INTERFACE:
1405 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1409 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1412 /* for CDC, iff carrier is on, data interface is active. */
1413 if (rndis_active(dev) || wIndex != 1)
1414 *(u8 *)req->buf = 0;
1416 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1417 /* carrier always ok ...*/
1418 *(u8 *)req->buf = 1 ;
1420 value = min(wLength, (u16) 1);
1423 #ifdef DEV_CONFIG_CDC
1424 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1426 * see 6.2.30: no data, wIndex = interface,
1427 * wValue = packet filter bitmap
1429 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1434 debug("packet filter %02x\n", wValue);
1435 dev->cdc_filter = wValue;
1441 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1442 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1443 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1444 * case USB_CDC_GET_ETHERNET_STATISTIC:
1447 #endif /* DEV_CONFIG_CDC */
1449 #ifdef CONFIG_USB_ETH_RNDIS
1451 * RNDIS uses the CDC command encapsulation mechanism to implement
1452 * an RPC scheme, with much getting/setting of attributes by OID.
1454 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1455 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1456 || !rndis_active(dev)
1457 || wLength > USB_BUFSIZ
1459 || rndis_control_intf.bInterfaceNumber
1462 /* read the request, then process it */
1464 req->complete = rndis_command_complete;
1465 /* later, rndis_control_ack () sends a notification */
1468 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1469 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1470 == ctrl->bRequestType
1471 && rndis_active(dev)
1472 /* && wLength >= 0x0400 */
1474 && rndis_control_intf.bInterfaceNumber
1479 /* return the result */
1480 buf = rndis_get_next_response(dev->rndis_config, &n);
1482 memcpy(req->buf, buf, n);
1483 req->complete = rndis_response_complete;
1484 rndis_free_response(dev->rndis_config, buf);
1487 /* else stalls ... spec says to avoid that */
1493 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1494 ctrl->bRequestType, ctrl->bRequest,
1495 wValue, wIndex, wLength);
1498 /* respond with data transfer before status phase? */
1500 debug("respond with data transfer before status phase\n");
1501 req->length = value;
1502 req->zero = value < wLength
1503 && (value % gadget->ep0->maxpacket) == 0;
1504 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1506 debug("ep_queue --> %d\n", value);
1508 eth_setup_complete(gadget->ep0, req);
1512 /* host either stalls (value < 0) or reports success */
1516 /*-------------------------------------------------------------------------*/
1518 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1520 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1523 int retval = -ENOMEM;
1527 * Padding up to RX_EXTRA handles minor disagreements with host.
1528 * Normally we use the USB "terminate on short read" convention;
1529 * so allow up to (N*maxpacket), since that memory is normally
1530 * already allocated. Some hardware doesn't deal well with short
1531 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1532 * byte off the end (to force hardware errors on overflow).
1534 * RNDIS uses internal framing, and explicitly allows senders to
1535 * pad to end-of-packet. That's potentially nice for speed,
1536 * but means receivers can't recover synch on their own.
1539 debug("%s\n", __func__);
1541 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1542 size += dev->out_ep->maxpacket - 1;
1543 if (rndis_active(dev))
1544 size += sizeof(struct rndis_packet_msg_type);
1545 size -= size % dev->out_ep->maxpacket;
1548 * Some platforms perform better when IP packets are aligned,
1549 * but on at least one, checksumming fails otherwise. Note:
1550 * RNDIS headers involve variable numbers of LE32 values.
1553 req->buf = (u8 *) NetRxPackets[0];
1555 req->complete = rx_complete;
1557 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1560 error("rx submit --> %d", retval);
1565 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1567 struct eth_dev *dev = ep->driver_data;
1569 debug("%s: status %d\n", __func__, req->status);
1570 switch (req->status) {
1571 /* normal completion */
1573 if (rndis_active(dev)) {
1574 /* we know MaxPacketsPerTransfer == 1 here */
1575 int length = rndis_rm_hdr(req->buf, req->actual);
1578 req->length -= length;
1579 req->actual -= length;
1581 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1583 dev->stats.rx_errors++;
1584 dev->stats.rx_length_errors++;
1585 debug("rx length %d\n", req->length);
1589 dev->stats.rx_packets++;
1590 dev->stats.rx_bytes += req->length;
1593 /* software-driven interface shutdown */
1594 case -ECONNRESET: /* unlink */
1595 case -ESHUTDOWN: /* disconnect etc */
1596 /* for hardware automagic (such as pxa) */
1597 case -ECONNABORTED: /* endpoint reset */
1602 dev->stats.rx_over_errors++;
1605 dev->stats.rx_errors++;
1609 packet_received = 1;
1612 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1615 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1620 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1628 usb_ep_free_request(dev->in_ep, dev->tx_req);
1630 error("can't alloc requests");
1634 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1636 struct eth_dev *dev = ep->driver_data;
1638 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1639 switch (req->status) {
1641 dev->stats.tx_errors++;
1642 debug("tx err %d\n", req->status);
1644 case -ECONNRESET: /* unlink */
1645 case -ESHUTDOWN: /* disconnect etc */
1648 dev->stats.tx_bytes += req->length;
1650 dev->stats.tx_packets++;
1655 static inline int eth_is_promisc(struct eth_dev *dev)
1657 /* no filters for the CDC subset; always promisc */
1658 if (subset_active(dev))
1660 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1664 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1666 struct eth_dev *dev = netdev_priv(net);
1667 int length = skb->len;
1669 struct usb_request *req = NULL;
1670 unsigned long flags;
1672 /* apply outgoing CDC or RNDIS filters */
1673 if (!eth_is_promisc (dev)) {
1674 u8 *dest = skb->data;
1676 if (is_multicast_ether_addr(dest)) {
1679 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1680 * SET_ETHERNET_MULTICAST_FILTERS requests
1682 if (is_broadcast_ether_addr(dest))
1683 type = USB_CDC_PACKET_TYPE_BROADCAST;
1685 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1686 if (!(dev->cdc_filter & type)) {
1687 dev_kfree_skb_any (skb);
1691 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1694 spin_lock_irqsave(&dev->req_lock, flags);
1696 * this freelist can be empty if an interrupt triggered disconnect()
1697 * and reconfigured the gadget (shutting down this queue) after the
1698 * network stack decided to xmit but before we got the spinlock.
1700 if (list_empty(&dev->tx_reqs)) {
1701 spin_unlock_irqrestore(&dev->req_lock, flags);
1705 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1706 list_del (&req->list);
1708 /* temporarily stop TX queue when the freelist empties */
1709 if (list_empty (&dev->tx_reqs))
1710 netif_stop_queue (net);
1711 spin_unlock_irqrestore(&dev->req_lock, flags);
1713 /* no buffer copies needed, unless the network stack did it
1714 * or the hardware can't use skb buffers.
1715 * or there's not enough space for any RNDIS headers we need
1717 if (rndis_active(dev)) {
1718 struct sk_buff *skb_rndis;
1720 skb_rndis = skb_realloc_headroom (skb,
1721 sizeof (struct rndis_packet_msg_type));
1725 dev_kfree_skb_any (skb);
1727 rndis_add_hdr (skb);
1730 req->buf = skb->data;
1732 req->complete = tx_complete;
1734 /* use zlp framing on tx for strict CDC-Ether conformance,
1735 * though any robust network rx path ignores extra padding.
1736 * and some hardware doesn't like to write zlps.
1739 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1742 req->length = length;
1744 /* throttle highspeed IRQ rate back slightly */
1745 if (gadget_is_dualspeed(dev->gadget))
1746 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1747 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1750 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1753 DEBUG (dev, "tx queue err %d\n", retval);
1756 net->trans_start = jiffies;
1757 atomic_inc (&dev->tx_qlen);
1762 dev->stats.tx_dropped++;
1763 dev_kfree_skb_any (skb);
1764 spin_lock_irqsave(&dev->req_lock, flags);
1765 if (list_empty (&dev->tx_reqs))
1766 netif_start_queue (net);
1767 list_add (&req->list, &dev->tx_reqs);
1768 spin_unlock_irqrestore(&dev->req_lock, flags);
1773 /*-------------------------------------------------------------------------*/
1776 static void eth_unbind(struct usb_gadget *gadget)
1778 struct eth_dev *dev = get_gadget_data(gadget);
1780 debug("%s...\n", __func__);
1781 rndis_deregister(dev->rndis_config);
1784 /* we've already been disconnected ... no i/o is active */
1786 usb_ep_free_request(gadget->ep0, dev->req);
1789 if (dev->stat_req) {
1790 usb_ep_free_request(dev->status_ep, dev->stat_req);
1791 dev->stat_req = NULL;
1795 usb_ep_free_request(dev->in_ep, dev->tx_req);
1800 usb_ep_free_request(dev->out_ep, dev->rx_req);
1804 /* unregister_netdev (dev->net);*/
1805 /* free_netdev(dev->net);*/
1808 set_gadget_data(gadget, NULL);
1811 static void eth_disconnect(struct usb_gadget *gadget)
1813 eth_reset_config(get_gadget_data(gadget));
1814 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1817 static void eth_suspend(struct usb_gadget *gadget)
1822 static void eth_resume(struct usb_gadget *gadget)
1827 /*-------------------------------------------------------------------------*/
1829 #ifdef CONFIG_USB_ETH_RNDIS
1832 * The interrupt endpoint is used in RNDIS to notify the host when messages
1833 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1834 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1835 * REMOTE_NDIS_KEEPALIVE_MSG.
1837 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1838 * normally just one notification will be queued.
1841 static void rndis_control_ack_complete(struct usb_ep *ep,
1842 struct usb_request *req)
1844 struct eth_dev *dev = ep->driver_data;
1846 debug("%s...\n", __func__);
1847 if (req->status || req->actual != req->length)
1848 debug("rndis control ack complete --> %d, %d/%d\n",
1849 req->status, req->actual, req->length);
1851 if (!l_ethdev.network_started) {
1852 if (rndis_get_state(dev->rndis_config)
1853 == RNDIS_DATA_INITIALIZED) {
1854 l_ethdev.network_started = 1;
1855 printf("USB RNDIS network up!\n");
1859 req->context = NULL;
1861 if (req != dev->stat_req)
1862 usb_ep_free_request(ep, req);
1865 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1867 static int rndis_control_ack(struct eth_device *net)
1869 struct eth_dev *dev = &l_ethdev;
1871 struct usb_request *resp = dev->stat_req;
1873 /* in case RNDIS calls this after disconnect */
1875 debug("status ENODEV\n");
1879 /* in case queue length > 1 */
1880 if (resp->context) {
1881 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1884 resp->buf = rndis_resp_buf;
1888 * Send RNDIS RESPONSE_AVAILABLE notification;
1889 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1892 resp->complete = rndis_control_ack_complete;
1893 resp->context = dev;
1895 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1896 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1898 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1901 rndis_control_ack_complete(dev->status_ep, resp);
1909 #define rndis_control_ack NULL
1913 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1915 if (rndis_active(dev)) {
1916 rndis_set_param_medium(dev->rndis_config,
1918 BITRATE(dev->gadget)/100);
1919 rndis_signal_connect(dev->rndis_config);
1923 static int eth_stop(struct eth_dev *dev)
1925 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1927 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1930 if (rndis_active(dev)) {
1931 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1932 rndis_signal_disconnect(dev->rndis_config);
1934 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1935 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1937 while (get_timer(ts) < timeout)
1938 usb_gadget_handle_interrupts();
1941 rndis_uninit(dev->rndis_config);
1948 /*-------------------------------------------------------------------------*/
1950 static int is_eth_addr_valid(char *str)
1952 if (strlen(str) == 17) {
1957 /* see if it looks like an ethernet address */
1961 for (i = 0; i < 6; i++) {
1962 char term = (i == 5 ? '\0' : ':');
1964 ea[i] = simple_strtol(p, &q, 16);
1966 if ((q - p) != 2 || *q++ != term)
1972 if (i == 6) /* it looks ok */
1978 static u8 nibble(unsigned char c)
1980 if (likely(isdigit(c)))
1983 if (likely(isxdigit(c)))
1984 return 10 + c - 'A';
1988 static int get_ether_addr(const char *str, u8 *dev_addr)
1993 for (i = 0; i < 6; i++) {
1996 if ((*str == '.') || (*str == ':'))
1998 num = nibble(*str++) << 4;
1999 num |= (nibble(*str++));
2002 if (is_valid_ether_addr(dev_addr))
2008 static int eth_bind(struct usb_gadget *gadget)
2010 struct eth_dev *dev = &l_ethdev;
2011 u8 cdc = 1, zlp = 1, rndis = 1;
2012 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2013 int status = -ENOMEM;
2017 /* these flags are only ever cleared; compiler take note */
2018 #ifndef DEV_CONFIG_CDC
2021 #ifndef CONFIG_USB_ETH_RNDIS
2025 * Because most host side USB stacks handle CDC Ethernet, that
2026 * standard protocol is _strongly_ preferred for interop purposes.
2027 * (By everyone except Microsoft.)
2029 if (gadget_is_pxa(gadget)) {
2030 /* pxa doesn't support altsettings */
2032 } else if (gadget_is_musbhdrc(gadget)) {
2033 /* reduce tx dma overhead by avoiding special cases */
2035 } else if (gadget_is_sh(gadget)) {
2036 /* sh doesn't support multiple interfaces or configs */
2039 } else if (gadget_is_sa1100(gadget)) {
2040 /* hardware can't write zlps */
2043 * sa1100 CAN do CDC, without status endpoint ... we use
2044 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2049 gcnum = usb_gadget_controller_number(gadget);
2051 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2054 * can't assume CDC works. don't want to default to
2055 * anything less functional on CDC-capable hardware,
2056 * so we fail in this case.
2058 error("controller '%s' not recognized",
2064 * If there's an RNDIS configuration, that's what Windows wants to
2065 * be using ... so use these product IDs here and in the "linux.inf"
2066 * needed to install MSFT drivers. Current Linux kernels will use
2067 * the second configuration if it's CDC Ethernet, and need some help
2068 * to choose the right configuration otherwise.
2071 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2072 device_desc.idVendor =
2073 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2074 device_desc.idProduct =
2075 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2082 sprintf(product_desc, "RNDIS/%s", driver_desc);
2085 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2086 * drivers aren't widely available. (That may be improved by
2087 * supporting one submode of the "SAFE" variant of MDLM.)
2090 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2091 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2092 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2095 device_desc.idVendor =
2096 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2097 device_desc.idProduct =
2098 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2102 /* support optional vendor/distro customization */
2104 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2106 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2108 strlcpy(product_desc, iProduct, sizeof product_desc);
2109 if (iSerialNumber) {
2110 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2111 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2114 /* all we really need is bulk IN/OUT */
2115 usb_ep_autoconfig_reset(gadget);
2116 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2119 error("can't autoconfigure on %s\n",
2123 in_ep->driver_data = in_ep; /* claim */
2125 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2128 out_ep->driver_data = out_ep; /* claim */
2130 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2132 * CDC Ethernet control interface doesn't require a status endpoint.
2133 * Since some hosts expect one, try to allocate one anyway.
2136 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2138 status_ep->driver_data = status_ep; /* claim */
2140 error("can't run RNDIS on %s", gadget->name);
2142 #ifdef DEV_CONFIG_CDC
2144 control_intf.bNumEndpoints = 0;
2145 /* FIXME remove endpoint from descriptor list */
2151 /* one config: cdc, else minimal subset */
2153 eth_config.bNumInterfaces = 1;
2154 eth_config.iConfiguration = STRING_SUBSET;
2157 * use functions to set these up, in case we're built to work
2158 * with multiple controllers and must override CDC Ethernet.
2160 fs_subset_descriptors();
2161 hs_subset_descriptors();
2164 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2165 usb_gadget_set_selfpowered(gadget);
2167 /* For now RNDIS is always a second config */
2169 device_desc.bNumConfigurations = 2;
2171 if (gadget_is_dualspeed(gadget)) {
2173 dev_qualifier.bNumConfigurations = 2;
2175 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2177 /* assumes ep0 uses the same value for both speeds ... */
2178 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2180 /* and that all endpoints are dual-speed */
2181 hs_source_desc.bEndpointAddress =
2182 fs_source_desc.bEndpointAddress;
2183 hs_sink_desc.bEndpointAddress =
2184 fs_sink_desc.bEndpointAddress;
2185 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2187 hs_status_desc.bEndpointAddress =
2188 fs_status_desc.bEndpointAddress;
2192 if (gadget_is_otg(gadget)) {
2193 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2194 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2195 eth_config.bMaxPower = 4;
2196 #ifdef CONFIG_USB_ETH_RNDIS
2197 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2198 rndis_config.bMaxPower = 4;
2203 /* network device setup */
2204 dev->net = &l_netdev;
2210 dev->out_ep = out_ep;
2211 dev->status_ep = status_ep;
2214 * Module params for these addresses should come from ID proms.
2215 * The host side address is used with CDC and RNDIS, and commonly
2216 * ends up in a persistent config database. It's not clear if
2217 * host side code for the SAFE thing cares -- its original BLAN
2218 * thing didn't, Sharp never assigned those addresses on Zaurii.
2220 get_ether_addr(dev_addr, dev->net->enetaddr);
2222 memset(tmp, 0, sizeof(tmp));
2223 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2225 get_ether_addr(host_addr, dev->host_mac);
2227 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2228 dev->host_mac[0], dev->host_mac[1],
2229 dev->host_mac[2], dev->host_mac[3],
2230 dev->host_mac[4], dev->host_mac[5]);
2233 status = rndis_init();
2235 error("can't init RNDIS, %d", status);
2241 * use PKTSIZE (or aligned... from u-boot) and set
2242 * wMaxSegmentSize accordingly
2244 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2246 /* preallocate control message data and buffer */
2247 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2250 dev->req->buf = control_req;
2251 dev->req->complete = eth_setup_complete;
2253 /* ... and maybe likewise for status transfer */
2254 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2255 if (dev->status_ep) {
2256 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2258 if (!dev->stat_req) {
2259 usb_ep_free_request(dev->status_ep, dev->req);
2263 dev->stat_req->buf = status_req;
2264 dev->stat_req->context = NULL;
2268 /* finish hookup to lower layer ... */
2269 dev->gadget = gadget;
2270 set_gadget_data(gadget, dev);
2271 gadget->ep0->driver_data = dev;
2274 * two kinds of host-initiated state changes:
2275 * - iff DATA transfer is active, carrier is "on"
2276 * - tx queueing enabled if open *and* carrier is "on"
2279 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2280 out_ep->name, in_ep->name,
2281 status_ep ? " STATUS " : "",
2282 status_ep ? status_ep->name : ""
2284 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2285 dev->net->enetaddr[0], dev->net->enetaddr[1],
2286 dev->net->enetaddr[2], dev->net->enetaddr[3],
2287 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2290 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2291 dev->host_mac[0], dev->host_mac[1],
2292 dev->host_mac[2], dev->host_mac[3],
2293 dev->host_mac[4], dev->host_mac[5]);
2298 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2300 dev->rndis_config = rndis_register(rndis_control_ack);
2301 if (dev->rndis_config < 0) {
2304 debug("RNDIS setup failed\n");
2309 /* these set up a lot of the OIDs that RNDIS needs */
2310 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2311 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2312 &dev->stats, &dev->cdc_filter))
2314 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2317 if (rndis_set_param_medium(dev->rndis_config,
2318 NDIS_MEDIUM_802_3, 0))
2320 printf("RNDIS ready\n");
2325 error("%s failed, status = %d", __func__, status);
2330 /*-------------------------------------------------------------------------*/
2332 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2334 struct eth_dev *dev = &l_ethdev;
2335 struct usb_gadget *gadget;
2337 unsigned long timeout = USB_CONNECT_TIMEOUT;
2340 error("received NULL ptr");
2344 /* Configure default mac-addresses for the USB ethernet device */
2345 #ifdef CONFIG_USBNET_DEV_ADDR
2346 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2348 #ifdef CONFIG_USBNET_HOST_ADDR
2349 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2351 /* Check if the user overruled the MAC addresses */
2352 if (getenv("usbnet_devaddr"))
2353 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2356 if (getenv("usbnet_hostaddr"))
2357 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2360 if (!is_eth_addr_valid(dev_addr)) {
2361 error("Need valid 'usbnet_devaddr' to be set");
2364 if (!is_eth_addr_valid(host_addr)) {
2365 error("Need valid 'usbnet_hostaddr' to be set");
2369 if (usb_gadget_register_driver(ð_driver) < 0)
2372 dev->network_started = 0;
2374 packet_received = 0;
2377 gadget = dev->gadget;
2378 usb_gadget_connect(gadget);
2380 if (getenv("cdc_connect_timeout"))
2381 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2382 NULL, 10) * CONFIG_SYS_HZ;
2384 while (!l_ethdev.network_started) {
2385 /* Handle control-c and timeouts */
2386 if (ctrlc() || (get_timer(ts) > timeout)) {
2387 error("The remote end did not respond in time.");
2390 usb_gadget_handle_interrupts();
2393 packet_received = 0;
2394 rx_submit(dev, dev->rx_req, 0);
2400 static int usb_eth_send(struct eth_device *netdev,
2401 volatile void *packet, int length)
2404 void *rndis_pkt = NULL;
2405 struct eth_dev *dev = &l_ethdev;
2406 struct usb_request *req = dev->tx_req;
2408 unsigned long timeout = USB_CONNECT_TIMEOUT;
2410 debug("%s:...\n", __func__);
2412 /* new buffer is needed to include RNDIS header */
2413 if (rndis_active(dev)) {
2414 rndis_pkt = malloc(length +
2415 sizeof(struct rndis_packet_msg_type));
2417 error("No memory to alloc RNDIS packet");
2420 rndis_add_hdr(rndis_pkt, length);
2421 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2422 (void *)packet, length);
2424 length += sizeof(struct rndis_packet_msg_type);
2426 req->buf = (void *)packet;
2427 req->context = NULL;
2428 req->complete = tx_complete;
2431 * use zlp framing on tx for strict CDC-Ether conformance,
2432 * though any robust network rx path ignores extra padding.
2433 * and some hardware doesn't like to write zlps.
2436 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2439 req->length = length;
2441 /* throttle highspeed IRQ rate back slightly */
2442 if (gadget_is_dualspeed(dev->gadget))
2443 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2444 ? ((dev->tx_qlen % qmult) != 0) : 0;
2450 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2453 debug("%s: packet queued\n", __func__);
2454 while (!packet_sent) {
2455 if (get_timer(ts) > timeout) {
2456 printf("timeout sending packets to usb ethernet\n");
2459 usb_gadget_handle_interrupts();
2466 dev->stats.tx_dropped++;
2470 static int usb_eth_recv(struct eth_device *netdev)
2472 struct eth_dev *dev = &l_ethdev;
2474 usb_gadget_handle_interrupts();
2476 if (packet_received) {
2477 debug("%s: packet received\n", __func__);
2479 NetReceive(NetRxPackets[0], dev->rx_req->length);
2480 packet_received = 0;
2482 rx_submit(dev, dev->rx_req, 0);
2484 error("dev->rx_req invalid");
2489 void usb_eth_halt(struct eth_device *netdev)
2491 struct eth_dev *dev = &l_ethdev;
2494 error("received NULL ptr");
2498 /* If the gadget not registered, simple return */
2503 * Some USB controllers may need additional deinitialization here
2504 * before dropping pull-up (also due to hardware issues).
2505 * For example: unhandled interrupt with status stage started may
2506 * bring the controller to fully broken state (until board reset).
2507 * There are some variants to debug and fix such cases:
2508 * 1) In the case of RNDIS connection eth_stop can perform additional
2509 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2510 * 2) 'pullup' callback in your UDC driver can be improved to perform
2511 * this deinitialization.
2515 usb_gadget_disconnect(dev->gadget);
2517 /* Clear pending interrupt */
2518 if (dev->network_started) {
2519 usb_gadget_handle_interrupts();
2520 dev->network_started = 0;
2523 usb_gadget_unregister_driver(ð_driver);
2526 static struct usb_gadget_driver eth_driver = {
2530 .unbind = eth_unbind,
2533 .disconnect = eth_disconnect,
2535 .suspend = eth_suspend,
2536 .resume = eth_resume,
2539 int usb_eth_initialize(bd_t *bi)
2541 struct eth_device *netdev = &l_netdev;
2543 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2545 netdev->init = usb_eth_init;
2546 netdev->send = usb_eth_send;
2547 netdev->recv = usb_eth_recv;
2548 netdev->halt = usb_eth_halt;
2550 #ifdef CONFIG_MCAST_TFTP
2551 #error not supported
2553 eth_register(netdev);