123cd1385SRemy Bohmer /*
223cd1385SRemy Bohmer * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
323cd1385SRemy Bohmer *
423cd1385SRemy Bohmer * Copyright (C) 2003-2005,2008 David Brownell
523cd1385SRemy Bohmer * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
623cd1385SRemy Bohmer * Copyright (C) 2008 Nokia Corporation
723cd1385SRemy Bohmer *
81a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
923cd1385SRemy Bohmer */
1023cd1385SRemy Bohmer
1123cd1385SRemy Bohmer #include <common.h>
1224b852a7SSimon Glass #include <console.h>
131221ce45SMasahiro Yamada #include <linux/errno.h>
14c85d70efSVitaly Kuzmichev #include <linux/netdevice.h>
1523cd1385SRemy Bohmer #include <linux/usb/ch9.h>
1623cd1385SRemy Bohmer #include <linux/usb/cdc.h>
1723cd1385SRemy Bohmer #include <linux/usb/gadget.h>
1823cd1385SRemy Bohmer #include <net.h>
198bfc288cSKishon Vijay Abraham I #include <usb.h>
207612a43dSVitaly Kuzmichev #include <malloc.h>
21cf92e05cSSimon Glass #include <memalign.h>
2223cd1385SRemy Bohmer #include <linux/ctype.h>
2323cd1385SRemy Bohmer
2423cd1385SRemy Bohmer #include "gadget_chips.h"
257612a43dSVitaly Kuzmichev #include "rndis.h"
2623cd1385SRemy Bohmer
27d4345aeeSMugunthan V N #include <dm.h>
28d4a37553SMugunthan V N #include <dm/lists.h>
29d4345aeeSMugunthan V N #include <dm/uclass-internal.h>
30d4345aeeSMugunthan V N #include <dm/device-internal.h>
31d4345aeeSMugunthan V N
328f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether"
337de73185SVitaly Kuzmichev
3423cd1385SRemy Bohmer #define atomic_read
3523cd1385SRemy Bohmer extern struct platform_data brd;
3623cd1385SRemy Bohmer
3723cd1385SRemy Bohmer
3823cd1385SRemy Bohmer unsigned packet_received, packet_sent;
3923cd1385SRemy Bohmer
4023cd1385SRemy Bohmer /*
4123cd1385SRemy Bohmer * Ethernet gadget driver -- with CDC and non-CDC options
4223cd1385SRemy Bohmer * Builds on hardware support for a full duplex link.
4323cd1385SRemy Bohmer *
4423cd1385SRemy Bohmer * CDC Ethernet is the standard USB solution for sending Ethernet frames
4523cd1385SRemy Bohmer * using USB. Real hardware tends to use the same framing protocol but look
4623cd1385SRemy Bohmer * different for control features. This driver strongly prefers to use
4723cd1385SRemy Bohmer * this USB-IF standard as its open-systems interoperability solution;
4823cd1385SRemy Bohmer * most host side USB stacks (except from Microsoft) support it.
4923cd1385SRemy Bohmer *
5023cd1385SRemy Bohmer * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
5123cd1385SRemy Bohmer * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
5223cd1385SRemy Bohmer * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
5323cd1385SRemy Bohmer *
5423cd1385SRemy Bohmer * There's some hardware that can't talk CDC ECM. We make that hardware
5523cd1385SRemy Bohmer * implement a "minimalist" vendor-agnostic CDC core: same framing, but
5623cd1385SRemy Bohmer * link-level setup only requires activating the configuration. Only the
5723cd1385SRemy Bohmer * endpoint descriptors, and product/vendor IDs, are relevant; no control
5823cd1385SRemy Bohmer * operations are available. Linux supports it, but other host operating
5923cd1385SRemy Bohmer * systems may not. (This is a subset of CDC Ethernet.)
6023cd1385SRemy Bohmer *
6123cd1385SRemy Bohmer * It turns out that if you add a few descriptors to that "CDC Subset",
6223cd1385SRemy Bohmer * (Windows) host side drivers from MCCI can treat it as one submode of
6323cd1385SRemy Bohmer * a proprietary scheme called "SAFE" ... without needing to know about
6423cd1385SRemy Bohmer * specific product/vendor IDs. So we do that, making it easier to use
6523cd1385SRemy Bohmer * those MS-Windows drivers. Those added descriptors make it resemble a
6623cd1385SRemy Bohmer * CDC MDLM device, but they don't change device behavior at all. (See
6723cd1385SRemy Bohmer * MCCI Engineering report 950198 "SAFE Networking Functions".)
6823cd1385SRemy Bohmer *
6923cd1385SRemy Bohmer * A third option is also in use. Rather than CDC Ethernet, or something
7023cd1385SRemy Bohmer * simpler, Microsoft pushes their own approach: RNDIS. The published
7123cd1385SRemy Bohmer * RNDIS specs are ambiguous and appear to be incomplete, and are also
7223cd1385SRemy Bohmer * needlessly complex. They borrow more from CDC ACM than CDC ECM.
7323cd1385SRemy Bohmer */
7423cd1385SRemy Bohmer #define ETH_ALEN 6 /* Octets in one ethernet addr */
7523cd1385SRemy Bohmer #define ETH_HLEN 14 /* Total octets in header. */
7623cd1385SRemy Bohmer #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
7723cd1385SRemy Bohmer #define ETH_DATA_LEN 1500 /* Max. octets in payload */
7823cd1385SRemy Bohmer #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
7923cd1385SRemy Bohmer
8023cd1385SRemy Bohmer #define DRIVER_DESC "Ethernet Gadget"
8123cd1385SRemy Bohmer /* Based on linux 2.6.27 version */
8223cd1385SRemy Bohmer #define DRIVER_VERSION "May Day 2005"
8323cd1385SRemy Bohmer
8423cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC;
8523cd1385SRemy Bohmer
8623cd1385SRemy Bohmer #define RX_EXTRA 20 /* guard against rx overflows */
8723cd1385SRemy Bohmer
887612a43dSVitaly Kuzmichev #ifndef CONFIG_USB_ETH_RNDIS
897612a43dSVitaly Kuzmichev #define rndis_uninit(x) do {} while (0)
907612a43dSVitaly Kuzmichev #define rndis_deregister(c) do {} while (0)
917612a43dSVitaly Kuzmichev #define rndis_exit() do {} while (0)
927612a43dSVitaly Kuzmichev #endif
937612a43dSVitaly Kuzmichev
947612a43dSVitaly Kuzmichev /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
9523cd1385SRemy Bohmer #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
9623cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
9723cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_PROMISCUOUS \
9823cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_DIRECTED)
9923cd1385SRemy Bohmer
10023cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
10123cd1385SRemy Bohmer
10223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
1038b6b66b4SVitaly Kuzmichev
1048b6b66b4SVitaly Kuzmichev struct eth_dev {
1058b6b66b4SVitaly Kuzmichev struct usb_gadget *gadget;
1068b6b66b4SVitaly Kuzmichev struct usb_request *req; /* for control responses */
1077612a43dSVitaly Kuzmichev struct usb_request *stat_req; /* for cdc & rndis status */
1088b6b66b4SVitaly Kuzmichev
1098b6b66b4SVitaly Kuzmichev u8 config;
1108b6b66b4SVitaly Kuzmichev struct usb_ep *in_ep, *out_ep, *status_ep;
1118b6b66b4SVitaly Kuzmichev const struct usb_endpoint_descriptor
1128b6b66b4SVitaly Kuzmichev *in, *out, *status;
1138b6b66b4SVitaly Kuzmichev
1148b6b66b4SVitaly Kuzmichev struct usb_request *tx_req, *rx_req;
1158b6b66b4SVitaly Kuzmichev
116d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
1178b6b66b4SVitaly Kuzmichev struct eth_device *net;
118d4a37553SMugunthan V N #else
119d4a37553SMugunthan V N struct udevice *net;
120d4a37553SMugunthan V N #endif
1218b6b66b4SVitaly Kuzmichev struct net_device_stats stats;
1228b6b66b4SVitaly Kuzmichev unsigned int tx_qlen;
1238b6b66b4SVitaly Kuzmichev
1248b6b66b4SVitaly Kuzmichev unsigned zlp:1;
1258b6b66b4SVitaly Kuzmichev unsigned cdc:1;
1267612a43dSVitaly Kuzmichev unsigned rndis:1;
1278b6b66b4SVitaly Kuzmichev unsigned suspended:1;
1288b6b66b4SVitaly Kuzmichev unsigned network_started:1;
1298b6b66b4SVitaly Kuzmichev u16 cdc_filter;
1308b6b66b4SVitaly Kuzmichev unsigned long todo;
1318b6b66b4SVitaly Kuzmichev int mtu;
1328b6b66b4SVitaly Kuzmichev #define WORK_RX_MEMORY 0
1337612a43dSVitaly Kuzmichev int rndis_config;
1348b6b66b4SVitaly Kuzmichev u8 host_mac[ETH_ALEN];
1358b6b66b4SVitaly Kuzmichev };
1368b6b66b4SVitaly Kuzmichev
1378b6b66b4SVitaly Kuzmichev /*
1388b6b66b4SVitaly Kuzmichev * This version autoconfigures as much as possible at run-time.
1398b6b66b4SVitaly Kuzmichev *
1408b6b66b4SVitaly Kuzmichev * It also ASSUMES a self-powered device, without remote wakeup,
1418b6b66b4SVitaly Kuzmichev * although remote wakeup support would make sense.
1428b6b66b4SVitaly Kuzmichev */
1438b6b66b4SVitaly Kuzmichev
1448b6b66b4SVitaly Kuzmichev /*-------------------------------------------------------------------------*/
1455cb3b9d7SMugunthan V N struct ether_priv {
1465cb3b9d7SMugunthan V N struct eth_dev ethdev;
147d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
1485cb3b9d7SMugunthan V N struct eth_device netdev;
149d4a37553SMugunthan V N #else
150d4a37553SMugunthan V N struct udevice *netdev;
151d4a37553SMugunthan V N #endif
1525cb3b9d7SMugunthan V N struct usb_gadget_driver eth_driver;
1535cb3b9d7SMugunthan V N };
1545cb3b9d7SMugunthan V N
1555cb3b9d7SMugunthan V N struct ether_priv eth_priv;
1565cb3b9d7SMugunthan V N struct ether_priv *l_priv = ð_priv;
15723cd1385SRemy Bohmer
15823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
15923cd1385SRemy Bohmer
16023cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */
is_cdc(struct eth_dev * dev)16123cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev)
16223cd1385SRemy Bohmer {
1632bb37884SLukasz Dalek #if !defined(CONFIG_USB_ETH_SUBSET)
16423cd1385SRemy Bohmer return 1; /* only cdc possible */
1652bb37884SLukasz Dalek #elif !defined(CONFIG_USB_ETH_CDC)
16623cd1385SRemy Bohmer return 0; /* only subset possible */
16723cd1385SRemy Bohmer #else
16823cd1385SRemy Bohmer return dev->cdc; /* depends on what hardware we found */
16923cd1385SRemy Bohmer #endif
17023cd1385SRemy Bohmer }
17123cd1385SRemy Bohmer
1727612a43dSVitaly Kuzmichev /* "secondary" RNDIS config may sometimes be activated */
rndis_active(struct eth_dev * dev)1737612a43dSVitaly Kuzmichev static inline int rndis_active(struct eth_dev *dev)
1747612a43dSVitaly Kuzmichev {
1757612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
1767612a43dSVitaly Kuzmichev return dev->rndis;
1777612a43dSVitaly Kuzmichev #else
1787612a43dSVitaly Kuzmichev return 0;
1797612a43dSVitaly Kuzmichev #endif
1807612a43dSVitaly Kuzmichev }
1817612a43dSVitaly Kuzmichev
1827612a43dSVitaly Kuzmichev #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
1837612a43dSVitaly Kuzmichev #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
18423cd1385SRemy Bohmer
18523cd1385SRemy Bohmer #define DEFAULT_QLEN 2 /* double buffering by default */
18623cd1385SRemy Bohmer
18723cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */
18823cd1385SRemy Bohmer #define HS_BPS (13 * 512 * 8 * 1000 * 8)
18923cd1385SRemy Bohmer #define FS_BPS (19 * 64 * 1 * 1000 * 8)
19023cd1385SRemy Bohmer
19123cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
19223cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_HIGH
19323cd1385SRemy Bohmer
1942721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT
1952721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT
1962721dbf1SVitaly Kuzmichev #else
1972721dbf1SVitaly Kuzmichev #define qmult 5
1982721dbf1SVitaly Kuzmichev #endif
1992721dbf1SVitaly Kuzmichev
20023cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */
20123cd1385SRemy Bohmer #define qlen(gadget) \
20223cd1385SRemy Bohmer (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
20323cd1385SRemy Bohmer
BITRATE(struct usb_gadget * g)20423cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
20523cd1385SRemy Bohmer {
20623cd1385SRemy Bohmer return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
20723cd1385SRemy Bohmer }
20823cd1385SRemy Bohmer
20923cd1385SRemy Bohmer #else /* full speed (low speed doesn't do bulk) */
21023cd1385SRemy Bohmer
21123cd1385SRemy Bohmer #define qmult 1
21223cd1385SRemy Bohmer
21323cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_FULL
21423cd1385SRemy Bohmer
21523cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN
21623cd1385SRemy Bohmer
BITRATE(struct usb_gadget * g)21723cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
21823cd1385SRemy Bohmer {
21923cd1385SRemy Bohmer return FS_BPS;
22023cd1385SRemy Bohmer }
22123cd1385SRemy Bohmer #endif
22223cd1385SRemy Bohmer
22323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
22423cd1385SRemy Bohmer
2256142e0aeSVitaly Kuzmichev /*
2266142e0aeSVitaly Kuzmichev * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
22723cd1385SRemy Bohmer * Instead: allocate your own, using normal USB-IF procedures.
22823cd1385SRemy Bohmer */
22923cd1385SRemy Bohmer
2306142e0aeSVitaly Kuzmichev /*
2316142e0aeSVitaly Kuzmichev * Thanks to NetChip Technologies for donating this product ID.
23223cd1385SRemy Bohmer * It's for devices with only CDC Ethernet configurations.
23323cd1385SRemy Bohmer */
23423cd1385SRemy Bohmer #define CDC_VENDOR_NUM 0x0525 /* NetChip */
23523cd1385SRemy Bohmer #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
23623cd1385SRemy Bohmer
2376142e0aeSVitaly Kuzmichev /*
2386142e0aeSVitaly Kuzmichev * For hardware that can't talk CDC, we use the same vendor ID that
23923cd1385SRemy Bohmer * ARM Linux has used for ethernet-over-usb, both with sa1100 and
24023cd1385SRemy Bohmer * with pxa250. We're protocol-compatible, if the host-side drivers
24123cd1385SRemy Bohmer * use the endpoint descriptors. bcdDevice (version) is nonzero, so
24223cd1385SRemy Bohmer * drivers that need to hard-wire endpoint numbers have a hook.
24323cd1385SRemy Bohmer *
24423cd1385SRemy Bohmer * The protocol is a minimal subset of CDC Ether, which works on any bulk
24523cd1385SRemy Bohmer * hardware that's not deeply broken ... even on hardware that can't talk
24623cd1385SRemy Bohmer * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
24723cd1385SRemy Bohmer * doesn't handle control-OUT).
24823cd1385SRemy Bohmer */
2497612a43dSVitaly Kuzmichev #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
2507612a43dSVitaly Kuzmichev #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
2517612a43dSVitaly Kuzmichev
2527612a43dSVitaly Kuzmichev /*
2537612a43dSVitaly Kuzmichev * For hardware that can talk RNDIS and either of the above protocols,
2547612a43dSVitaly Kuzmichev * use this ID ... the windows INF files will know it. Unless it's
2557612a43dSVitaly Kuzmichev * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
2567612a43dSVitaly Kuzmichev * the non-RNDIS configuration.
2577612a43dSVitaly Kuzmichev */
2587612a43dSVitaly Kuzmichev #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
2597612a43dSVitaly Kuzmichev #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
26023cd1385SRemy Bohmer
2616142e0aeSVitaly Kuzmichev /*
2626142e0aeSVitaly Kuzmichev * Some systems will want different product identifers published in the
26323cd1385SRemy Bohmer * device descriptor, either numbers or strings or both. These string
26423cd1385SRemy Bohmer * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
26523cd1385SRemy Bohmer */
26623cd1385SRemy Bohmer
2677612a43dSVitaly Kuzmichev /*
2687612a43dSVitaly Kuzmichev * Emulating them in eth_bind:
2697612a43dSVitaly Kuzmichev * static ushort idVendor;
2707612a43dSVitaly Kuzmichev * static ushort idProduct;
2717612a43dSVitaly Kuzmichev */
2727612a43dSVitaly Kuzmichev
273b466df35SMaxime Ripard #if defined(CONFIG_USB_GADGET_MANUFACTURER)
274b466df35SMaxime Ripard static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
27523cd1385SRemy Bohmer #else
276a187559eSBin Meng static char *iManufacturer = "U-Boot";
27723cd1385SRemy Bohmer #endif
2787612a43dSVitaly Kuzmichev
2797612a43dSVitaly Kuzmichev /* These probably need to be configurable. */
2807612a43dSVitaly Kuzmichev static ushort bcdDevice;
28123cd1385SRemy Bohmer static char *iProduct;
28223cd1385SRemy Bohmer static char *iSerialNumber;
2837612a43dSVitaly Kuzmichev
28423cd1385SRemy Bohmer static char dev_addr[18];
2857612a43dSVitaly Kuzmichev
28623cd1385SRemy Bohmer static char host_addr[18];
28723cd1385SRemy Bohmer
2887612a43dSVitaly Kuzmichev
28923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
29023cd1385SRemy Bohmer
2916142e0aeSVitaly Kuzmichev /*
2926142e0aeSVitaly Kuzmichev * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
29323cd1385SRemy Bohmer * ep0 implementation: descriptors, config management, setup().
29423cd1385SRemy Bohmer * also optional class-specific notification interrupt transfer.
29523cd1385SRemy Bohmer */
29623cd1385SRemy Bohmer
29723cd1385SRemy Bohmer /*
29823cd1385SRemy Bohmer * DESCRIPTORS ... most are static, but strings and (full) configuration
29923cd1385SRemy Bohmer * descriptors are built on demand. For now we do either full CDC, or
3007612a43dSVitaly Kuzmichev * our simple subset, with RNDIS as an optional second configuration.
3017612a43dSVitaly Kuzmichev *
3027612a43dSVitaly Kuzmichev * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
3037612a43dSVitaly Kuzmichev * the class descriptors match a modem (they're ignored; it's really just
3047612a43dSVitaly Kuzmichev * Ethernet functionality), they don't need the NOP altsetting, and the
3057612a43dSVitaly Kuzmichev * status transfer endpoint isn't optional.
30623cd1385SRemy Bohmer */
30723cd1385SRemy Bohmer
30823cd1385SRemy Bohmer #define STRING_MANUFACTURER 1
30923cd1385SRemy Bohmer #define STRING_PRODUCT 2
31023cd1385SRemy Bohmer #define STRING_ETHADDR 3
31123cd1385SRemy Bohmer #define STRING_DATA 4
31223cd1385SRemy Bohmer #define STRING_CONTROL 5
3137612a43dSVitaly Kuzmichev #define STRING_RNDIS_CONTROL 6
31423cd1385SRemy Bohmer #define STRING_CDC 7
31523cd1385SRemy Bohmer #define STRING_SUBSET 8
3167612a43dSVitaly Kuzmichev #define STRING_RNDIS 9
31723cd1385SRemy Bohmer #define STRING_SERIALNUMBER 10
31823cd1385SRemy Bohmer
3197612a43dSVitaly Kuzmichev /* holds our biggest descriptor (or RNDIS response) */
32023cd1385SRemy Bohmer #define USB_BUFSIZ 256
32123cd1385SRemy Bohmer
32223cd1385SRemy Bohmer /*
3237612a43dSVitaly Kuzmichev * This device advertises one configuration, eth_config, unless RNDIS
3247612a43dSVitaly Kuzmichev * is enabled (rndis_config) on hardware supporting at least two configs.
3257612a43dSVitaly Kuzmichev *
3267612a43dSVitaly Kuzmichev * NOTE: Controllers like superh_udc should probably be able to use
3277612a43dSVitaly Kuzmichev * an RNDIS-only configuration.
32823cd1385SRemy Bohmer *
32923cd1385SRemy Bohmer * FIXME define some higher-powered configurations to make it easier
33023cd1385SRemy Bohmer * to recharge batteries ...
33123cd1385SRemy Bohmer */
33223cd1385SRemy Bohmer
33323cd1385SRemy Bohmer #define DEV_CONFIG_VALUE 1 /* cdc or subset */
3347612a43dSVitaly Kuzmichev #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
33523cd1385SRemy Bohmer
33623cd1385SRemy Bohmer static struct usb_device_descriptor
33723cd1385SRemy Bohmer device_desc = {
33823cd1385SRemy Bohmer .bLength = sizeof device_desc,
33923cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE,
34023cd1385SRemy Bohmer
34123cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200),
34223cd1385SRemy Bohmer
34323cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM,
34423cd1385SRemy Bohmer .bDeviceSubClass = 0,
34523cd1385SRemy Bohmer .bDeviceProtocol = 0,
34623cd1385SRemy Bohmer
34723cd1385SRemy Bohmer .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
34823cd1385SRemy Bohmer .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
34923cd1385SRemy Bohmer .iManufacturer = STRING_MANUFACTURER,
35023cd1385SRemy Bohmer .iProduct = STRING_PRODUCT,
35123cd1385SRemy Bohmer .bNumConfigurations = 1,
35223cd1385SRemy Bohmer };
35323cd1385SRemy Bohmer
35423cd1385SRemy Bohmer static struct usb_otg_descriptor
35523cd1385SRemy Bohmer otg_descriptor = {
35623cd1385SRemy Bohmer .bLength = sizeof otg_descriptor,
35723cd1385SRemy Bohmer .bDescriptorType = USB_DT_OTG,
35823cd1385SRemy Bohmer
35923cd1385SRemy Bohmer .bmAttributes = USB_OTG_SRP,
36023cd1385SRemy Bohmer };
36123cd1385SRemy Bohmer
36223cd1385SRemy Bohmer static struct usb_config_descriptor
36323cd1385SRemy Bohmer eth_config = {
36423cd1385SRemy Bohmer .bLength = sizeof eth_config,
36523cd1385SRemy Bohmer .bDescriptorType = USB_DT_CONFIG,
36623cd1385SRemy Bohmer
36723cd1385SRemy Bohmer /* compute wTotalLength on the fly */
36823cd1385SRemy Bohmer .bNumInterfaces = 2,
36923cd1385SRemy Bohmer .bConfigurationValue = DEV_CONFIG_VALUE,
37023cd1385SRemy Bohmer .iConfiguration = STRING_CDC,
37123cd1385SRemy Bohmer .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
37223cd1385SRemy Bohmer .bMaxPower = 1,
37323cd1385SRemy Bohmer };
37423cd1385SRemy Bohmer
3757612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
3767612a43dSVitaly Kuzmichev static struct usb_config_descriptor
3777612a43dSVitaly Kuzmichev rndis_config = {
3787612a43dSVitaly Kuzmichev .bLength = sizeof rndis_config,
3797612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CONFIG,
3807612a43dSVitaly Kuzmichev
3817612a43dSVitaly Kuzmichev /* compute wTotalLength on the fly */
3827612a43dSVitaly Kuzmichev .bNumInterfaces = 2,
3837612a43dSVitaly Kuzmichev .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
3847612a43dSVitaly Kuzmichev .iConfiguration = STRING_RNDIS,
3857612a43dSVitaly Kuzmichev .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
3867612a43dSVitaly Kuzmichev .bMaxPower = 1,
3877612a43dSVitaly Kuzmichev };
3887612a43dSVitaly Kuzmichev #endif
3897612a43dSVitaly Kuzmichev
39023cd1385SRemy Bohmer /*
39123cd1385SRemy Bohmer * Compared to the simple CDC subset, the full CDC Ethernet model adds
39223cd1385SRemy Bohmer * three class descriptors, two interface descriptors, optional status
39323cd1385SRemy Bohmer * endpoint. Both have a "data" interface and two bulk endpoints.
39423cd1385SRemy Bohmer * There are also differences in how control requests are handled.
3957612a43dSVitaly Kuzmichev *
3967612a43dSVitaly Kuzmichev * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
3977612a43dSVitaly Kuzmichev * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
3987612a43dSVitaly Kuzmichev * may hang or oops. Since bugfixes (or accurate specs, letting Linux
3997612a43dSVitaly Kuzmichev * work around those bugs) are unlikely to ever come from MSFT, you may
4007612a43dSVitaly Kuzmichev * wish to avoid using RNDIS.
4017612a43dSVitaly Kuzmichev *
4027612a43dSVitaly Kuzmichev * MCCI offers an alternative to RNDIS if you need to connect to Windows
4037612a43dSVitaly Kuzmichev * but have hardware that can't support CDC Ethernet. We add descriptors
4047612a43dSVitaly Kuzmichev * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
4057612a43dSVitaly Kuzmichev * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
4067612a43dSVitaly Kuzmichev * get those drivers from MCCI, or bundled with various products.
40723cd1385SRemy Bohmer */
40823cd1385SRemy Bohmer
4092bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
41023cd1385SRemy Bohmer static struct usb_interface_descriptor
41123cd1385SRemy Bohmer control_intf = {
41223cd1385SRemy Bohmer .bLength = sizeof control_intf,
41323cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE,
41423cd1385SRemy Bohmer
41523cd1385SRemy Bohmer .bInterfaceNumber = 0,
41623cd1385SRemy Bohmer /* status endpoint is optional; this may be patched later */
41723cd1385SRemy Bohmer .bNumEndpoints = 1,
41823cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM,
41923cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
42023cd1385SRemy Bohmer .bInterfaceProtocol = USB_CDC_PROTO_NONE,
42123cd1385SRemy Bohmer .iInterface = STRING_CONTROL,
42223cd1385SRemy Bohmer };
42323cd1385SRemy Bohmer #endif
42423cd1385SRemy Bohmer
4257612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
4267612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
4277612a43dSVitaly Kuzmichev rndis_control_intf = {
4287612a43dSVitaly Kuzmichev .bLength = sizeof rndis_control_intf,
4297612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_INTERFACE,
4307612a43dSVitaly Kuzmichev
4317612a43dSVitaly Kuzmichev .bInterfaceNumber = 0,
4327612a43dSVitaly Kuzmichev .bNumEndpoints = 1,
4337612a43dSVitaly Kuzmichev .bInterfaceClass = USB_CLASS_COMM,
4347612a43dSVitaly Kuzmichev .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
4357612a43dSVitaly Kuzmichev .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
4367612a43dSVitaly Kuzmichev .iInterface = STRING_RNDIS_CONTROL,
4377612a43dSVitaly Kuzmichev };
4387612a43dSVitaly Kuzmichev #endif
4397612a43dSVitaly Kuzmichev
44023cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = {
44123cd1385SRemy Bohmer .bLength = sizeof header_desc,
44223cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE,
44323cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_HEADER_TYPE,
44423cd1385SRemy Bohmer
44523cd1385SRemy Bohmer .bcdCDC = __constant_cpu_to_le16(0x0110),
44623cd1385SRemy Bohmer };
44723cd1385SRemy Bohmer
4482bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
44923cd1385SRemy Bohmer
45023cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = {
45123cd1385SRemy Bohmer .bLength = sizeof union_desc,
45223cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE,
45323cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_UNION_TYPE,
45423cd1385SRemy Bohmer
45523cd1385SRemy Bohmer .bMasterInterface0 = 0, /* index of control interface */
45623cd1385SRemy Bohmer .bSlaveInterface0 = 1, /* index of DATA interface */
45723cd1385SRemy Bohmer };
45823cd1385SRemy Bohmer
4597612a43dSVitaly Kuzmichev #endif /* CDC || RNDIS */
4607612a43dSVitaly Kuzmichev
4617612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
4627612a43dSVitaly Kuzmichev
4637612a43dSVitaly Kuzmichev static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
4647612a43dSVitaly Kuzmichev .bLength = sizeof call_mgmt_descriptor,
4657612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CS_INTERFACE,
4667612a43dSVitaly Kuzmichev .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
4677612a43dSVitaly Kuzmichev
4687612a43dSVitaly Kuzmichev .bmCapabilities = 0x00,
4697612a43dSVitaly Kuzmichev .bDataInterface = 0x01,
4707612a43dSVitaly Kuzmichev };
4717612a43dSVitaly Kuzmichev
4727612a43dSVitaly Kuzmichev static const struct usb_cdc_acm_descriptor acm_descriptor = {
4737612a43dSVitaly Kuzmichev .bLength = sizeof acm_descriptor,
4747612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CS_INTERFACE,
4757612a43dSVitaly Kuzmichev .bDescriptorSubType = USB_CDC_ACM_TYPE,
4767612a43dSVitaly Kuzmichev
4777612a43dSVitaly Kuzmichev .bmCapabilities = 0x00,
4787612a43dSVitaly Kuzmichev };
4797612a43dSVitaly Kuzmichev
4807612a43dSVitaly Kuzmichev #endif
48123cd1385SRemy Bohmer
4822bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_CDC
48323cd1385SRemy Bohmer
4846142e0aeSVitaly Kuzmichev /*
4856142e0aeSVitaly Kuzmichev * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
48623cd1385SRemy Bohmer * ways: data endpoints live in the control interface, there's no data
48723cd1385SRemy Bohmer * interface, and it's not used to talk to a cell phone radio.
48823cd1385SRemy Bohmer */
48923cd1385SRemy Bohmer
49023cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = {
49123cd1385SRemy Bohmer .bLength = sizeof mdlm_desc,
49223cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE,
49323cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_MDLM_TYPE,
49423cd1385SRemy Bohmer
49523cd1385SRemy Bohmer .bcdVersion = __constant_cpu_to_le16(0x0100),
49623cd1385SRemy Bohmer .bGUID = {
49723cd1385SRemy Bohmer 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
49823cd1385SRemy Bohmer 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
49923cd1385SRemy Bohmer },
50023cd1385SRemy Bohmer };
50123cd1385SRemy Bohmer
5026142e0aeSVitaly Kuzmichev /*
5036142e0aeSVitaly Kuzmichev * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
50423cd1385SRemy Bohmer * can't really use its struct. All we do here is say that we're using
50523cd1385SRemy Bohmer * the submode of "SAFE" which directly matches the CDC Subset.
50623cd1385SRemy Bohmer */
50765c389d2SLokesh Vutla #ifdef CONFIG_USB_ETH_SUBSET
50823cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = {
50923cd1385SRemy Bohmer 6,
51023cd1385SRemy Bohmer USB_DT_CS_INTERFACE,
51123cd1385SRemy Bohmer USB_CDC_MDLM_DETAIL_TYPE,
51223cd1385SRemy Bohmer
51323cd1385SRemy Bohmer 0, /* "SAFE" */
51423cd1385SRemy Bohmer 0, /* network control capabilities (none) */
51523cd1385SRemy Bohmer 0, /* network data capabilities ("raw" encapsulation) */
51623cd1385SRemy Bohmer };
51765c389d2SLokesh Vutla #endif
51823cd1385SRemy Bohmer
51923cd1385SRemy Bohmer #endif
52023cd1385SRemy Bohmer
52123cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = {
52223cd1385SRemy Bohmer .bLength = sizeof(ether_desc),
52323cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE,
52423cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
52523cd1385SRemy Bohmer
52623cd1385SRemy Bohmer /* this descriptor actually adds value, surprise! */
52723cd1385SRemy Bohmer .iMACAddress = STRING_ETHADDR,
52823cd1385SRemy Bohmer .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
52923cd1385SRemy Bohmer .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
53023cd1385SRemy Bohmer .wNumberMCFilters = __constant_cpu_to_le16(0),
53123cd1385SRemy Bohmer .bNumberPowerFilters = 0,
53223cd1385SRemy Bohmer };
53323cd1385SRemy Bohmer
5342bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
53523cd1385SRemy Bohmer
5366142e0aeSVitaly Kuzmichev /*
5376142e0aeSVitaly Kuzmichev * include the status endpoint if we can, even where it's optional.
53823cd1385SRemy Bohmer * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
53923cd1385SRemy Bohmer * packet, to simplify cancellation; and a big transfer interval, to
54023cd1385SRemy Bohmer * waste less bandwidth.
54123cd1385SRemy Bohmer *
54223cd1385SRemy Bohmer * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
54323cd1385SRemy Bohmer * if they ignore the connect/disconnect notifications that real aether
54423cd1385SRemy Bohmer * can provide. more advanced cdc configurations might want to support
54523cd1385SRemy Bohmer * encapsulated commands (vendor-specific, using control-OUT).
5467612a43dSVitaly Kuzmichev *
5477612a43dSVitaly Kuzmichev * RNDIS requires the status endpoint, since it uses that encapsulation
5487612a43dSVitaly Kuzmichev * mechanism for its funky RPC scheme.
54923cd1385SRemy Bohmer */
55023cd1385SRemy Bohmer
55123cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
55223cd1385SRemy Bohmer #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
55323cd1385SRemy Bohmer
55423cd1385SRemy Bohmer static struct usb_endpoint_descriptor
55523cd1385SRemy Bohmer fs_status_desc = {
55623cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
55723cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
55823cd1385SRemy Bohmer
55923cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN,
56023cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT,
56123cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
56223cd1385SRemy Bohmer .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
56323cd1385SRemy Bohmer };
56423cd1385SRemy Bohmer #endif
56523cd1385SRemy Bohmer
5662bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
56723cd1385SRemy Bohmer
56823cd1385SRemy Bohmer /* the default data interface has no endpoints ... */
56923cd1385SRemy Bohmer
57023cd1385SRemy Bohmer static const struct usb_interface_descriptor
57123cd1385SRemy Bohmer data_nop_intf = {
57223cd1385SRemy Bohmer .bLength = sizeof data_nop_intf,
57323cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE,
57423cd1385SRemy Bohmer
57523cd1385SRemy Bohmer .bInterfaceNumber = 1,
57623cd1385SRemy Bohmer .bAlternateSetting = 0,
57723cd1385SRemy Bohmer .bNumEndpoints = 0,
57823cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA,
57923cd1385SRemy Bohmer .bInterfaceSubClass = 0,
58023cd1385SRemy Bohmer .bInterfaceProtocol = 0,
58123cd1385SRemy Bohmer };
58223cd1385SRemy Bohmer
58323cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */
58423cd1385SRemy Bohmer
58523cd1385SRemy Bohmer static const struct usb_interface_descriptor
58623cd1385SRemy Bohmer data_intf = {
58723cd1385SRemy Bohmer .bLength = sizeof data_intf,
58823cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE,
58923cd1385SRemy Bohmer
59023cd1385SRemy Bohmer .bInterfaceNumber = 1,
59123cd1385SRemy Bohmer .bAlternateSetting = 1,
59223cd1385SRemy Bohmer .bNumEndpoints = 2,
59323cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA,
59423cd1385SRemy Bohmer .bInterfaceSubClass = 0,
59523cd1385SRemy Bohmer .bInterfaceProtocol = 0,
59623cd1385SRemy Bohmer .iInterface = STRING_DATA,
59723cd1385SRemy Bohmer };
59823cd1385SRemy Bohmer
59923cd1385SRemy Bohmer #endif
60023cd1385SRemy Bohmer
6017612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
6027612a43dSVitaly Kuzmichev
6037612a43dSVitaly Kuzmichev /* RNDIS doesn't activate by changing to the "real" altsetting */
6047612a43dSVitaly Kuzmichev
6057612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
6067612a43dSVitaly Kuzmichev rndis_data_intf = {
6077612a43dSVitaly Kuzmichev .bLength = sizeof rndis_data_intf,
6087612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_INTERFACE,
6097612a43dSVitaly Kuzmichev
6107612a43dSVitaly Kuzmichev .bInterfaceNumber = 1,
6117612a43dSVitaly Kuzmichev .bAlternateSetting = 0,
6127612a43dSVitaly Kuzmichev .bNumEndpoints = 2,
6137612a43dSVitaly Kuzmichev .bInterfaceClass = USB_CLASS_CDC_DATA,
6147612a43dSVitaly Kuzmichev .bInterfaceSubClass = 0,
6157612a43dSVitaly Kuzmichev .bInterfaceProtocol = 0,
6167612a43dSVitaly Kuzmichev .iInterface = STRING_DATA,
6177612a43dSVitaly Kuzmichev };
6187612a43dSVitaly Kuzmichev
6197612a43dSVitaly Kuzmichev #endif
6207612a43dSVitaly Kuzmichev
6212bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
62223cd1385SRemy Bohmer
62323cd1385SRemy Bohmer /*
62423cd1385SRemy Bohmer * "Simple" CDC-subset option is a simple vendor-neutral model that most
62523cd1385SRemy Bohmer * full speed controllers can handle: one interface, two bulk endpoints.
62623cd1385SRemy Bohmer *
62723cd1385SRemy Bohmer * To assist host side drivers, we fancy it up a bit, and add descriptors
62823cd1385SRemy Bohmer * so some host side drivers will understand it as a "SAFE" variant.
62923cd1385SRemy Bohmer */
63023cd1385SRemy Bohmer
63123cd1385SRemy Bohmer static const struct usb_interface_descriptor
63223cd1385SRemy Bohmer subset_data_intf = {
63323cd1385SRemy Bohmer .bLength = sizeof subset_data_intf,
63423cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE,
63523cd1385SRemy Bohmer
63623cd1385SRemy Bohmer .bInterfaceNumber = 0,
63723cd1385SRemy Bohmer .bAlternateSetting = 0,
63823cd1385SRemy Bohmer .bNumEndpoints = 2,
63923cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM,
64023cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
64123cd1385SRemy Bohmer .bInterfaceProtocol = 0,
64223cd1385SRemy Bohmer .iInterface = STRING_DATA,
64323cd1385SRemy Bohmer };
64423cd1385SRemy Bohmer
64523cd1385SRemy Bohmer #endif /* SUBSET */
64623cd1385SRemy Bohmer
64723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
64823cd1385SRemy Bohmer fs_source_desc = {
64923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
65023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
65123cd1385SRemy Bohmer
65223cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN,
65323cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK,
65443880ce5STroy Kisky .wMaxPacketSize = __constant_cpu_to_le16(64),
65523cd1385SRemy Bohmer };
65623cd1385SRemy Bohmer
65723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
65823cd1385SRemy Bohmer fs_sink_desc = {
65923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
66023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
66123cd1385SRemy Bohmer
66223cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_OUT,
66323cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK,
66443880ce5STroy Kisky .wMaxPacketSize = __constant_cpu_to_le16(64),
66523cd1385SRemy Bohmer };
66623cd1385SRemy Bohmer
66723cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = {
66823cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor,
6692bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
67023cd1385SRemy Bohmer /* "cdc" mode descriptors */
67123cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf,
67223cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc,
67323cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc,
67423cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc,
67523cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */
67623cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_status_desc,
67723cd1385SRemy Bohmer /* data interface, with altsetting */
67823cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf,
67923cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf,
68023cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_source_desc,
68123cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_sink_desc,
68223cd1385SRemy Bohmer NULL,
6832bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
68423cd1385SRemy Bohmer };
68523cd1385SRemy Bohmer
fs_subset_descriptors(void)68623cd1385SRemy Bohmer static inline void fs_subset_descriptors(void)
68723cd1385SRemy Bohmer {
6882bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
68923cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
69023cd1385SRemy Bohmer fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
69123cd1385SRemy Bohmer fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
69223cd1385SRemy Bohmer fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
69323cd1385SRemy Bohmer fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
69423cd1385SRemy Bohmer fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
69523cd1385SRemy Bohmer fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
69623cd1385SRemy Bohmer fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
69723cd1385SRemy Bohmer fs_eth_function[8] = NULL;
69823cd1385SRemy Bohmer #else
69923cd1385SRemy Bohmer fs_eth_function[1] = NULL;
70023cd1385SRemy Bohmer #endif
70123cd1385SRemy Bohmer }
70223cd1385SRemy Bohmer
7037612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
7047612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *fs_rndis_function[] = {
7057612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &otg_descriptor,
7067612a43dSVitaly Kuzmichev /* control interface matches ACM, not Ethernet */
7077612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_control_intf,
7087612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &header_desc,
7097612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &call_mgmt_descriptor,
7107612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &acm_descriptor,
7117612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &union_desc,
7127612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_status_desc,
7137612a43dSVitaly Kuzmichev /* data interface has no altsetting */
7147612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_data_intf,
7157612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_source_desc,
7167612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_sink_desc,
7177612a43dSVitaly Kuzmichev NULL,
7187612a43dSVitaly Kuzmichev };
7197612a43dSVitaly Kuzmichev #endif
7207612a43dSVitaly Kuzmichev
72123cd1385SRemy Bohmer /*
72223cd1385SRemy Bohmer * usb 2.0 devices need to expose both high speed and full speed
72323cd1385SRemy Bohmer * descriptors, unless they only run at full speed.
72423cd1385SRemy Bohmer */
72523cd1385SRemy Bohmer
7262bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
72723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
72823cd1385SRemy Bohmer hs_status_desc = {
72923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
73023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
73123cd1385SRemy Bohmer
73223cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT,
73323cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
73423cd1385SRemy Bohmer .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
73523cd1385SRemy Bohmer };
7362bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
73723cd1385SRemy Bohmer
73823cd1385SRemy Bohmer static struct usb_endpoint_descriptor
73923cd1385SRemy Bohmer hs_source_desc = {
74023cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
74123cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
74223cd1385SRemy Bohmer
74323cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK,
74423cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512),
74523cd1385SRemy Bohmer };
74623cd1385SRemy Bohmer
74723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
74823cd1385SRemy Bohmer hs_sink_desc = {
74923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE,
75023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT,
75123cd1385SRemy Bohmer
75223cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK,
75323cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512),
75423cd1385SRemy Bohmer };
75523cd1385SRemy Bohmer
75623cd1385SRemy Bohmer static struct usb_qualifier_descriptor
75723cd1385SRemy Bohmer dev_qualifier = {
75823cd1385SRemy Bohmer .bLength = sizeof dev_qualifier,
75923cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
76023cd1385SRemy Bohmer
76123cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200),
76223cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM,
76323cd1385SRemy Bohmer
76423cd1385SRemy Bohmer .bNumConfigurations = 1,
76523cd1385SRemy Bohmer };
76623cd1385SRemy Bohmer
76723cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = {
76823cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor,
7692bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
77023cd1385SRemy Bohmer /* "cdc" mode descriptors */
77123cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf,
77223cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc,
77323cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc,
77423cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc,
77523cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */
77623cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_status_desc,
77723cd1385SRemy Bohmer /* data interface, with altsetting */
77823cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf,
77923cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf,
78023cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_source_desc,
78123cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_sink_desc,
78223cd1385SRemy Bohmer NULL,
7832bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
78423cd1385SRemy Bohmer };
78523cd1385SRemy Bohmer
hs_subset_descriptors(void)78623cd1385SRemy Bohmer static inline void hs_subset_descriptors(void)
78723cd1385SRemy Bohmer {
7882bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
78923cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
79023cd1385SRemy Bohmer hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
79123cd1385SRemy Bohmer hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
79223cd1385SRemy Bohmer hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
79323cd1385SRemy Bohmer hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
79423cd1385SRemy Bohmer hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
79523cd1385SRemy Bohmer hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
79623cd1385SRemy Bohmer hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
79723cd1385SRemy Bohmer hs_eth_function[8] = NULL;
79823cd1385SRemy Bohmer #else
79923cd1385SRemy Bohmer hs_eth_function[1] = NULL;
80023cd1385SRemy Bohmer #endif
80123cd1385SRemy Bohmer }
80223cd1385SRemy Bohmer
8037612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
8047612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *hs_rndis_function[] = {
8057612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &otg_descriptor,
8067612a43dSVitaly Kuzmichev /* control interface matches ACM, not Ethernet */
8077612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_control_intf,
8087612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &header_desc,
8097612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &call_mgmt_descriptor,
8107612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &acm_descriptor,
8117612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &union_desc,
8127612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_status_desc,
8137612a43dSVitaly Kuzmichev /* data interface has no altsetting */
8147612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_data_intf,
8157612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_source_desc,
8167612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_sink_desc,
8177612a43dSVitaly Kuzmichev NULL,
8187612a43dSVitaly Kuzmichev };
8197612a43dSVitaly Kuzmichev #endif
8207612a43dSVitaly Kuzmichev
8217612a43dSVitaly Kuzmichev
82223cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */
82323cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor *
ep_desc(struct usb_gadget * g,struct usb_endpoint_descriptor * hs,struct usb_endpoint_descriptor * fs)82423cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
82523cd1385SRemy Bohmer struct usb_endpoint_descriptor *fs)
82623cd1385SRemy Bohmer {
82723cd1385SRemy Bohmer if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
82823cd1385SRemy Bohmer return hs;
82923cd1385SRemy Bohmer return fs;
83023cd1385SRemy Bohmer }
83123cd1385SRemy Bohmer
83223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
83323cd1385SRemy Bohmer
83423cd1385SRemy Bohmer /* descriptors that are built on-demand */
83523cd1385SRemy Bohmer
83623cd1385SRemy Bohmer static char manufacturer[50];
83723cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC;
83823cd1385SRemy Bohmer static char serial_number[20];
83923cd1385SRemy Bohmer
84023cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */
84123cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1];
84223cd1385SRemy Bohmer
84323cd1385SRemy Bohmer /* static strings, in UTF-8 */
84423cd1385SRemy Bohmer static struct usb_string strings[] = {
84523cd1385SRemy Bohmer { STRING_MANUFACTURER, manufacturer, },
84623cd1385SRemy Bohmer { STRING_PRODUCT, product_desc, },
84723cd1385SRemy Bohmer { STRING_SERIALNUMBER, serial_number, },
84823cd1385SRemy Bohmer { STRING_DATA, "Ethernet Data", },
84923cd1385SRemy Bohmer { STRING_ETHADDR, ethaddr, },
8502bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
85123cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", },
85223cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", },
85323cd1385SRemy Bohmer #endif
8542bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
85523cd1385SRemy Bohmer { STRING_SUBSET, "CDC Ethernet Subset", },
85623cd1385SRemy Bohmer #endif
8577612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
8587612a43dSVitaly Kuzmichev { STRING_RNDIS, "RNDIS", },
8597612a43dSVitaly Kuzmichev { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
8607612a43dSVitaly Kuzmichev #endif
86123cd1385SRemy Bohmer { } /* end of list */
86223cd1385SRemy Bohmer };
86323cd1385SRemy Bohmer
86423cd1385SRemy Bohmer static struct usb_gadget_strings stringtab = {
86523cd1385SRemy Bohmer .language = 0x0409, /* en-us */
86623cd1385SRemy Bohmer .strings = strings,
86723cd1385SRemy Bohmer };
86823cd1385SRemy Bohmer
86923cd1385SRemy Bohmer /*============================================================================*/
8705290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
8715290759cSJoel Fernandes
8722bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
8735290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
874563aed25SLukasz Dalek #endif
87523cd1385SRemy Bohmer
87623cd1385SRemy Bohmer /*============================================================================*/
87723cd1385SRemy Bohmer
87823cd1385SRemy Bohmer /*
87923cd1385SRemy Bohmer * one config, two interfaces: control, data.
88023cd1385SRemy Bohmer * complications: class descriptors, and an altsetting.
88123cd1385SRemy Bohmer */
88223cd1385SRemy Bohmer static int
config_buf(struct usb_gadget * g,u8 * buf,u8 type,unsigned index,int is_otg)88323cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
88423cd1385SRemy Bohmer {
88523cd1385SRemy Bohmer int len;
88623cd1385SRemy Bohmer const struct usb_config_descriptor *config;
88723cd1385SRemy Bohmer const struct usb_descriptor_header **function;
88823cd1385SRemy Bohmer int hs = 0;
88923cd1385SRemy Bohmer
89023cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) {
89123cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH);
89223cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG)
89323cd1385SRemy Bohmer hs = !hs;
89423cd1385SRemy Bohmer }
89523cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
89623cd1385SRemy Bohmer
89723cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations)
89823cd1385SRemy Bohmer return -EINVAL;
89923cd1385SRemy Bohmer
9007612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
9017612a43dSVitaly Kuzmichev /*
9027612a43dSVitaly Kuzmichev * list the RNDIS config first, to make Microsoft's drivers
9037612a43dSVitaly Kuzmichev * happy. DOCSIS 1.0 needs this too.
9047612a43dSVitaly Kuzmichev */
9057612a43dSVitaly Kuzmichev if (device_desc.bNumConfigurations == 2 && index == 0) {
9067612a43dSVitaly Kuzmichev config = &rndis_config;
9077612a43dSVitaly Kuzmichev function = which_fn(rndis);
9087612a43dSVitaly Kuzmichev } else
9097612a43dSVitaly Kuzmichev #endif
9107612a43dSVitaly Kuzmichev {
91123cd1385SRemy Bohmer config = ð_config;
91223cd1385SRemy Bohmer function = which_fn(eth);
9137612a43dSVitaly Kuzmichev }
91423cd1385SRemy Bohmer
91523cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */
91623cd1385SRemy Bohmer if (!is_otg)
91723cd1385SRemy Bohmer function++;
91823cd1385SRemy Bohmer
91923cd1385SRemy Bohmer len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
92023cd1385SRemy Bohmer if (len < 0)
92123cd1385SRemy Bohmer return len;
92223cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
92323cd1385SRemy Bohmer return len;
92423cd1385SRemy Bohmer }
92523cd1385SRemy Bohmer
92623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
92723cd1385SRemy Bohmer
9287612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
92923cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
93023cd1385SRemy Bohmer
93123cd1385SRemy Bohmer static int
set_ether_config(struct eth_dev * dev,gfp_t gfp_flags)93223cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
93323cd1385SRemy Bohmer {
93423cd1385SRemy Bohmer int result = 0;
93523cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget;
93623cd1385SRemy Bohmer
9372bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
9387612a43dSVitaly Kuzmichev /* status endpoint used for RNDIS and (optionally) CDC */
93923cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) {
94023cd1385SRemy Bohmer dev->status = ep_desc(gadget, &hs_status_desc,
94123cd1385SRemy Bohmer &fs_status_desc);
94223cd1385SRemy Bohmer dev->status_ep->driver_data = dev;
94323cd1385SRemy Bohmer
94423cd1385SRemy Bohmer result = usb_ep_enable(dev->status_ep, dev->status);
94523cd1385SRemy Bohmer if (result != 0) {
9467de73185SVitaly Kuzmichev debug("enable %s --> %d\n",
94723cd1385SRemy Bohmer dev->status_ep->name, result);
94823cd1385SRemy Bohmer goto done;
94923cd1385SRemy Bohmer }
95023cd1385SRemy Bohmer }
95123cd1385SRemy Bohmer #endif
95223cd1385SRemy Bohmer
95323cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
95423cd1385SRemy Bohmer dev->in_ep->driver_data = dev;
95523cd1385SRemy Bohmer
95623cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
95723cd1385SRemy Bohmer dev->out_ep->driver_data = dev;
95823cd1385SRemy Bohmer
9596142e0aeSVitaly Kuzmichev /*
9606142e0aeSVitaly Kuzmichev * With CDC, the host isn't allowed to use these two data
96123cd1385SRemy Bohmer * endpoints in the default altsetting for the interface.
96223cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE.
9637612a43dSVitaly Kuzmichev *
9647612a43dSVitaly Kuzmichev * Strictly speaking RNDIS should work the same: activation is
9657612a43dSVitaly Kuzmichev * a side effect of setting a packet filter. Deactivation is
9667612a43dSVitaly Kuzmichev * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
96723cd1385SRemy Bohmer */
96823cd1385SRemy Bohmer if (!cdc_active(dev)) {
96923cd1385SRemy Bohmer result = usb_ep_enable(dev->in_ep, dev->in);
97023cd1385SRemy Bohmer if (result != 0) {
9717de73185SVitaly Kuzmichev debug("enable %s --> %d\n",
97223cd1385SRemy Bohmer dev->in_ep->name, result);
97323cd1385SRemy Bohmer goto done;
97423cd1385SRemy Bohmer }
97523cd1385SRemy Bohmer
97623cd1385SRemy Bohmer result = usb_ep_enable(dev->out_ep, dev->out);
97723cd1385SRemy Bohmer if (result != 0) {
9787de73185SVitaly Kuzmichev debug("enable %s --> %d\n",
97923cd1385SRemy Bohmer dev->out_ep->name, result);
98023cd1385SRemy Bohmer goto done;
98123cd1385SRemy Bohmer }
98223cd1385SRemy Bohmer }
98323cd1385SRemy Bohmer
98423cd1385SRemy Bohmer done:
98523cd1385SRemy Bohmer if (result == 0)
98623cd1385SRemy Bohmer result = alloc_requests(dev, qlen(gadget), gfp_flags);
98723cd1385SRemy Bohmer
98823cd1385SRemy Bohmer /* on error, disable any endpoints */
98923cd1385SRemy Bohmer if (result < 0) {
990d5292c16SVitaly Kuzmichev if (!subset_active(dev) && dev->status_ep)
99123cd1385SRemy Bohmer (void) usb_ep_disable(dev->status_ep);
99223cd1385SRemy Bohmer dev->status = NULL;
99323cd1385SRemy Bohmer (void) usb_ep_disable(dev->in_ep);
99423cd1385SRemy Bohmer (void) usb_ep_disable(dev->out_ep);
99523cd1385SRemy Bohmer dev->in = NULL;
99623cd1385SRemy Bohmer dev->out = NULL;
9977612a43dSVitaly Kuzmichev } else if (!cdc_active(dev)) {
9987612a43dSVitaly Kuzmichev /*
9997612a43dSVitaly Kuzmichev * activate non-CDC configs right away
10007612a43dSVitaly Kuzmichev * this isn't strictly according to the RNDIS spec
10017612a43dSVitaly Kuzmichev */
10027612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC);
100323cd1385SRemy Bohmer }
100423cd1385SRemy Bohmer
100523cd1385SRemy Bohmer /* caller is responsible for cleanup on error */
100623cd1385SRemy Bohmer return result;
100723cd1385SRemy Bohmer }
100823cd1385SRemy Bohmer
eth_reset_config(struct eth_dev * dev)100923cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev)
101023cd1385SRemy Bohmer {
101123cd1385SRemy Bohmer if (dev->config == 0)
101223cd1385SRemy Bohmer return;
101323cd1385SRemy Bohmer
10147de73185SVitaly Kuzmichev debug("%s\n", __func__);
10157de73185SVitaly Kuzmichev
10167612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config);
10177612a43dSVitaly Kuzmichev
10186142e0aeSVitaly Kuzmichev /*
10196142e0aeSVitaly Kuzmichev * disable endpoints, forcing (synchronous) completion of
102023cd1385SRemy Bohmer * pending i/o. then free the requests.
102123cd1385SRemy Bohmer */
102223cd1385SRemy Bohmer
102323cd1385SRemy Bohmer if (dev->in) {
102423cd1385SRemy Bohmer usb_ep_disable(dev->in_ep);
102523cd1385SRemy Bohmer if (dev->tx_req) {
102623cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req);
102723cd1385SRemy Bohmer dev->tx_req = NULL;
102823cd1385SRemy Bohmer }
102923cd1385SRemy Bohmer }
103023cd1385SRemy Bohmer if (dev->out) {
103123cd1385SRemy Bohmer usb_ep_disable(dev->out_ep);
103223cd1385SRemy Bohmer if (dev->rx_req) {
10330129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req);
103423cd1385SRemy Bohmer dev->rx_req = NULL;
103523cd1385SRemy Bohmer }
103623cd1385SRemy Bohmer }
10376142e0aeSVitaly Kuzmichev if (dev->status)
103823cd1385SRemy Bohmer usb_ep_disable(dev->status_ep);
10396142e0aeSVitaly Kuzmichev
10407612a43dSVitaly Kuzmichev dev->rndis = 0;
104123cd1385SRemy Bohmer dev->cdc_filter = 0;
104223cd1385SRemy Bohmer dev->config = 0;
104323cd1385SRemy Bohmer }
104423cd1385SRemy Bohmer
10456142e0aeSVitaly Kuzmichev /*
10466142e0aeSVitaly Kuzmichev * change our operational config. must agree with the code
104723cd1385SRemy Bohmer * that returns config descriptors, and altsetting code.
104823cd1385SRemy Bohmer */
eth_set_config(struct eth_dev * dev,unsigned number,gfp_t gfp_flags)10496142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number,
10506142e0aeSVitaly Kuzmichev gfp_t gfp_flags)
105123cd1385SRemy Bohmer {
105223cd1385SRemy Bohmer int result = 0;
105323cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget;
105423cd1385SRemy Bohmer
105523cd1385SRemy Bohmer if (gadget_is_sa1100(gadget)
105623cd1385SRemy Bohmer && dev->config
105723cd1385SRemy Bohmer && dev->tx_qlen != 0) {
105823cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/
105990aa625cSMasahiro Yamada pr_err("can't change configurations");
106023cd1385SRemy Bohmer return -ESPIPE;
106123cd1385SRemy Bohmer }
106223cd1385SRemy Bohmer eth_reset_config(dev);
106323cd1385SRemy Bohmer
106423cd1385SRemy Bohmer switch (number) {
106523cd1385SRemy Bohmer case DEV_CONFIG_VALUE:
106623cd1385SRemy Bohmer result = set_ether_config(dev, gfp_flags);
106723cd1385SRemy Bohmer break;
10687612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
10697612a43dSVitaly Kuzmichev case DEV_RNDIS_CONFIG_VALUE:
10707612a43dSVitaly Kuzmichev dev->rndis = 1;
10717612a43dSVitaly Kuzmichev result = set_ether_config(dev, gfp_flags);
10727612a43dSVitaly Kuzmichev break;
10737612a43dSVitaly Kuzmichev #endif
107423cd1385SRemy Bohmer default:
107523cd1385SRemy Bohmer result = -EINVAL;
107623cd1385SRemy Bohmer /* FALL THROUGH */
107723cd1385SRemy Bohmer case 0:
107823cd1385SRemy Bohmer break;
107923cd1385SRemy Bohmer }
108023cd1385SRemy Bohmer
108123cd1385SRemy Bohmer if (result) {
108223cd1385SRemy Bohmer if (number)
108323cd1385SRemy Bohmer eth_reset_config(dev);
108423cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget,
108523cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100);
108623cd1385SRemy Bohmer } else {
108723cd1385SRemy Bohmer char *speed;
108823cd1385SRemy Bohmer unsigned power;
108923cd1385SRemy Bohmer
109023cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower;
109123cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power);
109223cd1385SRemy Bohmer
109323cd1385SRemy Bohmer switch (gadget->speed) {
10946142e0aeSVitaly Kuzmichev case USB_SPEED_FULL:
10956142e0aeSVitaly Kuzmichev speed = "full"; break;
109623cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
10976142e0aeSVitaly Kuzmichev case USB_SPEED_HIGH:
10986142e0aeSVitaly Kuzmichev speed = "high"; break;
109923cd1385SRemy Bohmer #endif
11006142e0aeSVitaly Kuzmichev default:
11016142e0aeSVitaly Kuzmichev speed = "?"; break;
110223cd1385SRemy Bohmer }
110323cd1385SRemy Bohmer
110423cd1385SRemy Bohmer dev->config = number;
11057de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n",
110623cd1385SRemy Bohmer speed, number, power, driver_desc,
11077612a43dSVitaly Kuzmichev rndis_active(dev)
11087612a43dSVitaly Kuzmichev ? "RNDIS"
11097612a43dSVitaly Kuzmichev : (cdc_active(dev)
11107612a43dSVitaly Kuzmichev ? "CDC Ethernet"
111123cd1385SRemy Bohmer : "CDC Ethernet Subset"));
111223cd1385SRemy Bohmer }
111323cd1385SRemy Bohmer return result;
111423cd1385SRemy Bohmer }
111523cd1385SRemy Bohmer
111623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
111723cd1385SRemy Bohmer
11182bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
111923cd1385SRemy Bohmer
11206142e0aeSVitaly Kuzmichev /*
11216142e0aeSVitaly Kuzmichev * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
112223cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or
11237612a43dSVitaly Kuzmichev * report completion of some encapsulated command (as used in RNDIS). Since
112423cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that
112523cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued.
112623cd1385SRemy Bohmer */
eth_status_complete(struct usb_ep * ep,struct usb_request * req)112723cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
112823cd1385SRemy Bohmer {
112923cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf;
113023cd1385SRemy Bohmer int value = req->status;
113123cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data;
113223cd1385SRemy Bohmer
113323cd1385SRemy Bohmer /* issue the second notification if host reads the first */
113423cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
113523cd1385SRemy Bohmer && value == 0) {
113623cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event;
113723cd1385SRemy Bohmer
113823cd1385SRemy Bohmer event->bmRequestType = 0xA1;
113923cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
114023cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(0);
114123cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1);
114223cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16(8);
114323cd1385SRemy Bohmer
114423cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */
114523cd1385SRemy Bohmer data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
114623cd1385SRemy Bohmer
114723cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT;
114823cd1385SRemy Bohmer value = usb_ep_queue(ep, req, GFP_ATOMIC);
11497de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value);
115023cd1385SRemy Bohmer if (value == 0)
115123cd1385SRemy Bohmer return;
115223cd1385SRemy Bohmer } else if (value != -ECONNRESET) {
11537de73185SVitaly Kuzmichev debug("event %02x --> %d\n",
115423cd1385SRemy Bohmer event->bNotificationType, value);
115523cd1385SRemy Bohmer if (event->bNotificationType ==
11566142e0aeSVitaly Kuzmichev USB_CDC_NOTIFY_SPEED_CHANGE) {
115717b4f308SMugunthan V N dev->network_started = 1;
115823cd1385SRemy Bohmer printf("USB network up!\n");
115923cd1385SRemy Bohmer }
116023cd1385SRemy Bohmer }
116123cd1385SRemy Bohmer req->context = NULL;
116223cd1385SRemy Bohmer }
116323cd1385SRemy Bohmer
issue_start_status(struct eth_dev * dev)116423cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev)
116523cd1385SRemy Bohmer {
116623cd1385SRemy Bohmer struct usb_request *req = dev->stat_req;
116723cd1385SRemy Bohmer struct usb_cdc_notification *event;
116823cd1385SRemy Bohmer int value;
116923cd1385SRemy Bohmer
11706142e0aeSVitaly Kuzmichev /*
11716142e0aeSVitaly Kuzmichev * flush old status
117223cd1385SRemy Bohmer *
117323cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just
117423cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any
117523cd1385SRemy Bohmer * unlink-one primitive has way too many error modes.
117623cd1385SRemy Bohmer * here, we "know" toggle is already clear...
117723cd1385SRemy Bohmer *
117823cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it
117923cd1385SRemy Bohmer */
118023cd1385SRemy Bohmer usb_ep_disable(dev->status_ep);
118123cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status);
118223cd1385SRemy Bohmer
11836142e0aeSVitaly Kuzmichev /*
11846142e0aeSVitaly Kuzmichev * 3.8.1 says to issue first NETWORK_CONNECTION, then
118523cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs.
118623cd1385SRemy Bohmer */
118723cd1385SRemy Bohmer event = req->buf;
118823cd1385SRemy Bohmer event->bmRequestType = 0xA1;
118923cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
119023cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(1); /* connected */
119123cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1);
119223cd1385SRemy Bohmer event->wLength = 0;
119323cd1385SRemy Bohmer
119423cd1385SRemy Bohmer req->length = sizeof *event;
119523cd1385SRemy Bohmer req->complete = eth_status_complete;
119623cd1385SRemy Bohmer req->context = dev;
119723cd1385SRemy Bohmer
119823cd1385SRemy Bohmer value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
119923cd1385SRemy Bohmer if (value < 0)
12007de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value);
120123cd1385SRemy Bohmer }
120223cd1385SRemy Bohmer
120323cd1385SRemy Bohmer #endif
120423cd1385SRemy Bohmer
120523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
120623cd1385SRemy Bohmer
eth_setup_complete(struct usb_ep * ep,struct usb_request * req)120723cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
120823cd1385SRemy Bohmer {
120923cd1385SRemy Bohmer if (req->status || req->actual != req->length)
12107de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n",
121123cd1385SRemy Bohmer req->status, req->actual, req->length);
121223cd1385SRemy Bohmer }
121323cd1385SRemy Bohmer
12147612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
12157612a43dSVitaly Kuzmichev
rndis_response_complete(struct usb_ep * ep,struct usb_request * req)12167612a43dSVitaly Kuzmichev static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
12177612a43dSVitaly Kuzmichev {
12187612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length)
12197612a43dSVitaly Kuzmichev debug("rndis response complete --> %d, %d/%d\n",
12207612a43dSVitaly Kuzmichev req->status, req->actual, req->length);
12217612a43dSVitaly Kuzmichev
12227612a43dSVitaly Kuzmichev /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
12237612a43dSVitaly Kuzmichev }
12247612a43dSVitaly Kuzmichev
rndis_command_complete(struct usb_ep * ep,struct usb_request * req)12257612a43dSVitaly Kuzmichev static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
12267612a43dSVitaly Kuzmichev {
12277612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data;
12287612a43dSVitaly Kuzmichev int status;
12297612a43dSVitaly Kuzmichev
12307612a43dSVitaly Kuzmichev /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
12317612a43dSVitaly Kuzmichev status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
12327612a43dSVitaly Kuzmichev if (status < 0)
123390aa625cSMasahiro Yamada pr_err("%s: rndis parse error %d", __func__, status);
12347612a43dSVitaly Kuzmichev }
12357612a43dSVitaly Kuzmichev
12367612a43dSVitaly Kuzmichev #endif /* RNDIS */
12377612a43dSVitaly Kuzmichev
123823cd1385SRemy Bohmer /*
123923cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not
124023cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features:
124123cd1385SRemy Bohmer *
124223cd1385SRemy Bohmer * - two interfaces: control, and ethernet data
124323cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active
124423cd1385SRemy Bohmer * - class-specific descriptors for the control interface
124523cd1385SRemy Bohmer * - class-specific control requests
124623cd1385SRemy Bohmer */
124723cd1385SRemy Bohmer static int
eth_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)124823cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
124923cd1385SRemy Bohmer {
125023cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget);
125123cd1385SRemy Bohmer struct usb_request *req = dev->req;
125223cd1385SRemy Bohmer int value = -EOPNOTSUPP;
125323cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex);
125423cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue);
125523cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength);
125623cd1385SRemy Bohmer
12576142e0aeSVitaly Kuzmichev /*
12586142e0aeSVitaly Kuzmichev * descriptors just go into the pre-allocated ep0 buffer,
125923cd1385SRemy Bohmer * while config change events may enable network traffic.
126023cd1385SRemy Bohmer */
126123cd1385SRemy Bohmer
12627de73185SVitaly Kuzmichev debug("%s\n", __func__);
126323cd1385SRemy Bohmer
126423cd1385SRemy Bohmer req->complete = eth_setup_complete;
126523cd1385SRemy Bohmer switch (ctrl->bRequest) {
126623cd1385SRemy Bohmer
126723cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR:
126823cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN)
126923cd1385SRemy Bohmer break;
127023cd1385SRemy Bohmer switch (wValue >> 8) {
127123cd1385SRemy Bohmer
127223cd1385SRemy Bohmer case USB_DT_DEVICE:
127304afd5b5SKishon Vijay Abraham I device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
127423cd1385SRemy Bohmer value = min(wLength, (u16) sizeof device_desc);
127523cd1385SRemy Bohmer memcpy(req->buf, &device_desc, value);
127623cd1385SRemy Bohmer break;
127723cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER:
127823cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget))
127923cd1385SRemy Bohmer break;
128023cd1385SRemy Bohmer value = min(wLength, (u16) sizeof dev_qualifier);
128123cd1385SRemy Bohmer memcpy(req->buf, &dev_qualifier, value);
128223cd1385SRemy Bohmer break;
128323cd1385SRemy Bohmer
128423cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG:
128523cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget))
128623cd1385SRemy Bohmer break;
128723cd1385SRemy Bohmer /* FALLTHROUGH */
128823cd1385SRemy Bohmer case USB_DT_CONFIG:
128923cd1385SRemy Bohmer value = config_buf(gadget, req->buf,
129023cd1385SRemy Bohmer wValue >> 8,
129123cd1385SRemy Bohmer wValue & 0xff,
129223cd1385SRemy Bohmer gadget_is_otg(gadget));
129323cd1385SRemy Bohmer if (value >= 0)
129423cd1385SRemy Bohmer value = min(wLength, (u16) value);
129523cd1385SRemy Bohmer break;
129623cd1385SRemy Bohmer
129723cd1385SRemy Bohmer case USB_DT_STRING:
129823cd1385SRemy Bohmer value = usb_gadget_get_string(&stringtab,
129923cd1385SRemy Bohmer wValue & 0xff, req->buf);
130023cd1385SRemy Bohmer
130123cd1385SRemy Bohmer if (value >= 0)
130223cd1385SRemy Bohmer value = min(wLength, (u16) value);
130323cd1385SRemy Bohmer
130423cd1385SRemy Bohmer break;
130523cd1385SRemy Bohmer }
130623cd1385SRemy Bohmer break;
130723cd1385SRemy Bohmer
130823cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION:
130923cd1385SRemy Bohmer if (ctrl->bRequestType != 0)
131023cd1385SRemy Bohmer break;
131123cd1385SRemy Bohmer if (gadget->a_hnp_support)
13127de73185SVitaly Kuzmichev debug("HNP available\n");
131323cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support)
13147de73185SVitaly Kuzmichev debug("HNP needs a different root port\n");
131523cd1385SRemy Bohmer value = eth_set_config(dev, wValue, GFP_ATOMIC);
131623cd1385SRemy Bohmer break;
131723cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION:
131823cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN)
131923cd1385SRemy Bohmer break;
132023cd1385SRemy Bohmer *(u8 *)req->buf = dev->config;
132123cd1385SRemy Bohmer value = min(wLength, (u16) 1);
132223cd1385SRemy Bohmer break;
132323cd1385SRemy Bohmer
132423cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE:
132523cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE
132623cd1385SRemy Bohmer || !dev->config
132723cd1385SRemy Bohmer || wIndex > 1)
132823cd1385SRemy Bohmer break;
132923cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0)
133023cd1385SRemy Bohmer break;
133123cd1385SRemy Bohmer
13326142e0aeSVitaly Kuzmichev /*
13336142e0aeSVitaly Kuzmichev * PXA hardware partially handles SET_INTERFACE;
133423cd1385SRemy Bohmer * we need to kluge around that interference.
133523cd1385SRemy Bohmer */
133623cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) {
133723cd1385SRemy Bohmer value = eth_set_config(dev, DEV_CONFIG_VALUE,
133823cd1385SRemy Bohmer GFP_ATOMIC);
1339563aed25SLukasz Dalek /*
1340563aed25SLukasz Dalek * PXA25x driver use non-CDC ethernet gadget.
1341563aed25SLukasz Dalek * But only _CDC and _RNDIS code can signalize
1342563aed25SLukasz Dalek * that network is working. So we signalize it
1343563aed25SLukasz Dalek * here.
1344563aed25SLukasz Dalek */
134517b4f308SMugunthan V N dev->network_started = 1;
1346563aed25SLukasz Dalek debug("USB network up!\n");
134723cd1385SRemy Bohmer goto done_set_intf;
134823cd1385SRemy Bohmer }
134923cd1385SRemy Bohmer
13502bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
135123cd1385SRemy Bohmer switch (wIndex) {
135223cd1385SRemy Bohmer case 0: /* control/master intf */
135323cd1385SRemy Bohmer if (wValue != 0)
135423cd1385SRemy Bohmer break;
135523cd1385SRemy Bohmer if (dev->status) {
135623cd1385SRemy Bohmer usb_ep_disable(dev->status_ep);
135723cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status);
135823cd1385SRemy Bohmer }
13597612a43dSVitaly Kuzmichev
136023cd1385SRemy Bohmer value = 0;
136123cd1385SRemy Bohmer break;
136223cd1385SRemy Bohmer case 1: /* data intf */
136323cd1385SRemy Bohmer if (wValue > 1)
136423cd1385SRemy Bohmer break;
136523cd1385SRemy Bohmer usb_ep_disable(dev->in_ep);
136623cd1385SRemy Bohmer usb_ep_disable(dev->out_ep);
136723cd1385SRemy Bohmer
13686142e0aeSVitaly Kuzmichev /*
13696142e0aeSVitaly Kuzmichev * CDC requires the data transfers not be done from
137023cd1385SRemy Bohmer * the default interface setting ... also, setting
137123cd1385SRemy Bohmer * the non-default interface resets filters etc.
137223cd1385SRemy Bohmer */
137323cd1385SRemy Bohmer if (wValue == 1) {
137423cd1385SRemy Bohmer if (!cdc_active(dev))
137523cd1385SRemy Bohmer break;
137623cd1385SRemy Bohmer usb_ep_enable(dev->in_ep, dev->in);
137723cd1385SRemy Bohmer usb_ep_enable(dev->out_ep, dev->out);
137823cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER;
137923cd1385SRemy Bohmer if (dev->status)
138023cd1385SRemy Bohmer issue_start_status(dev);
13817612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC);
138223cd1385SRemy Bohmer }
138323cd1385SRemy Bohmer value = 0;
138423cd1385SRemy Bohmer break;
138523cd1385SRemy Bohmer }
138623cd1385SRemy Bohmer #else
13876142e0aeSVitaly Kuzmichev /*
13886142e0aeSVitaly Kuzmichev * FIXME this is wrong, as is the assumption that
138923cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ...
139023cd1385SRemy Bohmer */
13917de73185SVitaly Kuzmichev debug("set_interface ignored!\n");
13922bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
139323cd1385SRemy Bohmer
139423cd1385SRemy Bohmer done_set_intf:
139523cd1385SRemy Bohmer break;
139623cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE:
139723cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
139823cd1385SRemy Bohmer || !dev->config
139923cd1385SRemy Bohmer || wIndex > 1)
140023cd1385SRemy Bohmer break;
14017612a43dSVitaly Kuzmichev if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
140223cd1385SRemy Bohmer break;
140323cd1385SRemy Bohmer
140423cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */
14057612a43dSVitaly Kuzmichev if (rndis_active(dev) || wIndex != 1)
140623cd1385SRemy Bohmer *(u8 *)req->buf = 0;
140723cd1385SRemy Bohmer else {
140823cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
140923cd1385SRemy Bohmer /* carrier always ok ...*/
141023cd1385SRemy Bohmer *(u8 *)req->buf = 1 ;
141123cd1385SRemy Bohmer }
141223cd1385SRemy Bohmer value = min(wLength, (u16) 1);
141323cd1385SRemy Bohmer break;
141423cd1385SRemy Bohmer
14152bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
141623cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER:
14176142e0aeSVitaly Kuzmichev /*
14186142e0aeSVitaly Kuzmichev * see 6.2.30: no data, wIndex = interface,
141923cd1385SRemy Bohmer * wValue = packet filter bitmap
142023cd1385SRemy Bohmer */
142123cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
142223cd1385SRemy Bohmer || !cdc_active(dev)
142323cd1385SRemy Bohmer || wLength != 0
142423cd1385SRemy Bohmer || wIndex > 1)
142523cd1385SRemy Bohmer break;
14267de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue);
142723cd1385SRemy Bohmer dev->cdc_filter = wValue;
142823cd1385SRemy Bohmer value = 0;
142923cd1385SRemy Bohmer break;
143023cd1385SRemy Bohmer
14316142e0aeSVitaly Kuzmichev /*
14326142e0aeSVitaly Kuzmichev * and potentially:
143323cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
143423cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
143523cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
143623cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC:
143723cd1385SRemy Bohmer */
143823cd1385SRemy Bohmer
14392bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
144023cd1385SRemy Bohmer
14417612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
14427612a43dSVitaly Kuzmichev /*
14437612a43dSVitaly Kuzmichev * RNDIS uses the CDC command encapsulation mechanism to implement
14447612a43dSVitaly Kuzmichev * an RPC scheme, with much getting/setting of attributes by OID.
14457612a43dSVitaly Kuzmichev */
14467612a43dSVitaly Kuzmichev case USB_CDC_SEND_ENCAPSULATED_COMMAND:
14477612a43dSVitaly Kuzmichev if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14487612a43dSVitaly Kuzmichev || !rndis_active(dev)
14497612a43dSVitaly Kuzmichev || wLength > USB_BUFSIZ
14507612a43dSVitaly Kuzmichev || wValue
14517612a43dSVitaly Kuzmichev || rndis_control_intf.bInterfaceNumber
14527612a43dSVitaly Kuzmichev != wIndex)
14537612a43dSVitaly Kuzmichev break;
14547612a43dSVitaly Kuzmichev /* read the request, then process it */
14557612a43dSVitaly Kuzmichev value = wLength;
14567612a43dSVitaly Kuzmichev req->complete = rndis_command_complete;
14577612a43dSVitaly Kuzmichev /* later, rndis_control_ack () sends a notification */
14587612a43dSVitaly Kuzmichev break;
14597612a43dSVitaly Kuzmichev
14607612a43dSVitaly Kuzmichev case USB_CDC_GET_ENCAPSULATED_RESPONSE:
14617612a43dSVitaly Kuzmichev if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14627612a43dSVitaly Kuzmichev == ctrl->bRequestType
14637612a43dSVitaly Kuzmichev && rndis_active(dev)
14647612a43dSVitaly Kuzmichev /* && wLength >= 0x0400 */
14657612a43dSVitaly Kuzmichev && !wValue
14667612a43dSVitaly Kuzmichev && rndis_control_intf.bInterfaceNumber
14677612a43dSVitaly Kuzmichev == wIndex) {
14687612a43dSVitaly Kuzmichev u8 *buf;
14697612a43dSVitaly Kuzmichev u32 n;
14707612a43dSVitaly Kuzmichev
14717612a43dSVitaly Kuzmichev /* return the result */
14727612a43dSVitaly Kuzmichev buf = rndis_get_next_response(dev->rndis_config, &n);
14737612a43dSVitaly Kuzmichev if (buf) {
14747612a43dSVitaly Kuzmichev memcpy(req->buf, buf, n);
14757612a43dSVitaly Kuzmichev req->complete = rndis_response_complete;
14767612a43dSVitaly Kuzmichev rndis_free_response(dev->rndis_config, buf);
14777612a43dSVitaly Kuzmichev value = n;
14787612a43dSVitaly Kuzmichev }
14797612a43dSVitaly Kuzmichev /* else stalls ... spec says to avoid that */
14807612a43dSVitaly Kuzmichev }
14817612a43dSVitaly Kuzmichev break;
14827612a43dSVitaly Kuzmichev #endif /* RNDIS */
14837612a43dSVitaly Kuzmichev
148423cd1385SRemy Bohmer default:
14857de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
148623cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest,
148723cd1385SRemy Bohmer wValue, wIndex, wLength);
148823cd1385SRemy Bohmer }
148923cd1385SRemy Bohmer
149023cd1385SRemy Bohmer /* respond with data transfer before status phase? */
149123cd1385SRemy Bohmer if (value >= 0) {
14927de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n");
149323cd1385SRemy Bohmer req->length = value;
149423cd1385SRemy Bohmer req->zero = value < wLength
149523cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0;
149623cd1385SRemy Bohmer value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
149723cd1385SRemy Bohmer if (value < 0) {
14987de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value);
149923cd1385SRemy Bohmer req->status = 0;
150023cd1385SRemy Bohmer eth_setup_complete(gadget->ep0, req);
150123cd1385SRemy Bohmer }
150223cd1385SRemy Bohmer }
150323cd1385SRemy Bohmer
150423cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */
150523cd1385SRemy Bohmer return value;
150623cd1385SRemy Bohmer }
150723cd1385SRemy Bohmer
150823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
150923cd1385SRemy Bohmer
151023cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req);
151123cd1385SRemy Bohmer
rx_submit(struct eth_dev * dev,struct usb_request * req,gfp_t gfp_flags)15126142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req,
151323cd1385SRemy Bohmer gfp_t gfp_flags)
151423cd1385SRemy Bohmer {
151523cd1385SRemy Bohmer int retval = -ENOMEM;
151623cd1385SRemy Bohmer size_t size;
151723cd1385SRemy Bohmer
15186142e0aeSVitaly Kuzmichev /*
15196142e0aeSVitaly Kuzmichev * Padding up to RX_EXTRA handles minor disagreements with host.
152023cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention;
152123cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally
152223cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short
152323cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
152423cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow).
15257612a43dSVitaly Kuzmichev *
15267612a43dSVitaly Kuzmichev * RNDIS uses internal framing, and explicitly allows senders to
15277612a43dSVitaly Kuzmichev * pad to end-of-packet. That's potentially nice for speed,
15287612a43dSVitaly Kuzmichev * but means receivers can't recover synch on their own.
152923cd1385SRemy Bohmer */
153023cd1385SRemy Bohmer
15317de73185SVitaly Kuzmichev debug("%s\n", __func__);
153271fc5f91STroy Kisky if (!req)
153371fc5f91STroy Kisky return -EINVAL;
153423cd1385SRemy Bohmer
153523cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
153623cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1;
15377612a43dSVitaly Kuzmichev if (rndis_active(dev))
15387612a43dSVitaly Kuzmichev size += sizeof(struct rndis_packet_msg_type);
153923cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket;
154023cd1385SRemy Bohmer
15416142e0aeSVitaly Kuzmichev /*
15426142e0aeSVitaly Kuzmichev * Some platforms perform better when IP packets are aligned,
15437612a43dSVitaly Kuzmichev * but on at least one, checksumming fails otherwise. Note:
15447612a43dSVitaly Kuzmichev * RNDIS headers involve variable numbers of LE32 values.
154523cd1385SRemy Bohmer */
154623cd1385SRemy Bohmer
15471fd92db8SJoe Hershberger req->buf = (u8 *)net_rx_packets[0];
154823cd1385SRemy Bohmer req->length = size;
154923cd1385SRemy Bohmer req->complete = rx_complete;
155023cd1385SRemy Bohmer
155123cd1385SRemy Bohmer retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
155223cd1385SRemy Bohmer
15536142e0aeSVitaly Kuzmichev if (retval)
155490aa625cSMasahiro Yamada pr_err("rx submit --> %d", retval);
15556142e0aeSVitaly Kuzmichev
155623cd1385SRemy Bohmer return retval;
155723cd1385SRemy Bohmer }
155823cd1385SRemy Bohmer
rx_complete(struct usb_ep * ep,struct usb_request * req)155923cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req)
156023cd1385SRemy Bohmer {
156123cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data;
156223cd1385SRemy Bohmer
15637de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status);
1564c85d70efSVitaly Kuzmichev switch (req->status) {
1565c85d70efSVitaly Kuzmichev /* normal completion */
1566c85d70efSVitaly Kuzmichev case 0:
15677612a43dSVitaly Kuzmichev if (rndis_active(dev)) {
15687612a43dSVitaly Kuzmichev /* we know MaxPacketsPerTransfer == 1 here */
15697612a43dSVitaly Kuzmichev int length = rndis_rm_hdr(req->buf, req->actual);
15707612a43dSVitaly Kuzmichev if (length < 0)
15717612a43dSVitaly Kuzmichev goto length_err;
15727612a43dSVitaly Kuzmichev req->length -= length;
15737612a43dSVitaly Kuzmichev req->actual -= length;
15747612a43dSVitaly Kuzmichev }
15757612a43dSVitaly Kuzmichev if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
15767612a43dSVitaly Kuzmichev length_err:
15777612a43dSVitaly Kuzmichev dev->stats.rx_errors++;
15787612a43dSVitaly Kuzmichev dev->stats.rx_length_errors++;
15797612a43dSVitaly Kuzmichev debug("rx length %d\n", req->length);
15807612a43dSVitaly Kuzmichev break;
15817612a43dSVitaly Kuzmichev }
15827612a43dSVitaly Kuzmichev
1583c85d70efSVitaly Kuzmichev dev->stats.rx_packets++;
1584c85d70efSVitaly Kuzmichev dev->stats.rx_bytes += req->length;
1585c85d70efSVitaly Kuzmichev break;
1586c85d70efSVitaly Kuzmichev
1587c85d70efSVitaly Kuzmichev /* software-driven interface shutdown */
1588c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */
1589c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */
1590c85d70efSVitaly Kuzmichev /* for hardware automagic (such as pxa) */
1591c85d70efSVitaly Kuzmichev case -ECONNABORTED: /* endpoint reset */
1592c85d70efSVitaly Kuzmichev break;
1593c85d70efSVitaly Kuzmichev
1594c85d70efSVitaly Kuzmichev /* data overrun */
1595c85d70efSVitaly Kuzmichev case -EOVERFLOW:
1596c85d70efSVitaly Kuzmichev dev->stats.rx_over_errors++;
1597c85d70efSVitaly Kuzmichev /* FALLTHROUGH */
1598c85d70efSVitaly Kuzmichev default:
1599c85d70efSVitaly Kuzmichev dev->stats.rx_errors++;
1600c85d70efSVitaly Kuzmichev break;
1601c85d70efSVitaly Kuzmichev }
160223cd1385SRemy Bohmer
160323cd1385SRemy Bohmer packet_received = 1;
160423cd1385SRemy Bohmer }
160523cd1385SRemy Bohmer
alloc_requests(struct eth_dev * dev,unsigned n,gfp_t gfp_flags)160623cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
160723cd1385SRemy Bohmer {
160823cd1385SRemy Bohmer
160923cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
161023cd1385SRemy Bohmer
161123cd1385SRemy Bohmer if (!dev->tx_req)
1612ac5d32d1SVitaly Kuzmichev goto fail1;
161323cd1385SRemy Bohmer
161423cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
161523cd1385SRemy Bohmer
161623cd1385SRemy Bohmer if (!dev->rx_req)
1617ac5d32d1SVitaly Kuzmichev goto fail2;
161823cd1385SRemy Bohmer
161923cd1385SRemy Bohmer return 0;
162023cd1385SRemy Bohmer
1621ac5d32d1SVitaly Kuzmichev fail2:
1622ac5d32d1SVitaly Kuzmichev usb_ep_free_request(dev->in_ep, dev->tx_req);
1623ac5d32d1SVitaly Kuzmichev fail1:
162490aa625cSMasahiro Yamada pr_err("can't alloc requests");
162523cd1385SRemy Bohmer return -1;
162623cd1385SRemy Bohmer }
162723cd1385SRemy Bohmer
tx_complete(struct usb_ep * ep,struct usb_request * req)162823cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req)
162923cd1385SRemy Bohmer {
1630c85d70efSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data;
1631c85d70efSVitaly Kuzmichev
16327de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1633c85d70efSVitaly Kuzmichev switch (req->status) {
1634c85d70efSVitaly Kuzmichev default:
1635c85d70efSVitaly Kuzmichev dev->stats.tx_errors++;
1636c85d70efSVitaly Kuzmichev debug("tx err %d\n", req->status);
1637c85d70efSVitaly Kuzmichev /* FALLTHROUGH */
1638c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */
1639c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */
1640c85d70efSVitaly Kuzmichev break;
1641c85d70efSVitaly Kuzmichev case 0:
1642c85d70efSVitaly Kuzmichev dev->stats.tx_bytes += req->length;
1643c85d70efSVitaly Kuzmichev }
1644c85d70efSVitaly Kuzmichev dev->stats.tx_packets++;
1645c85d70efSVitaly Kuzmichev
164623cd1385SRemy Bohmer packet_sent = 1;
164723cd1385SRemy Bohmer }
164823cd1385SRemy Bohmer
eth_is_promisc(struct eth_dev * dev)164923cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev)
165023cd1385SRemy Bohmer {
165123cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */
165223cd1385SRemy Bohmer if (subset_active(dev))
165323cd1385SRemy Bohmer return 1;
165423cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
165523cd1385SRemy Bohmer }
165623cd1385SRemy Bohmer
165723cd1385SRemy Bohmer #if 0
165823cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
165923cd1385SRemy Bohmer {
166023cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net);
166123cd1385SRemy Bohmer int length = skb->len;
166223cd1385SRemy Bohmer int retval;
166323cd1385SRemy Bohmer struct usb_request *req = NULL;
166423cd1385SRemy Bohmer unsigned long flags;
166523cd1385SRemy Bohmer
166623cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */
166723cd1385SRemy Bohmer if (!eth_is_promisc (dev)) {
166823cd1385SRemy Bohmer u8 *dest = skb->data;
166923cd1385SRemy Bohmer
16700adb5b76SJoe Hershberger if (is_multicast_ethaddr(dest)) {
167123cd1385SRemy Bohmer u16 type;
167223cd1385SRemy Bohmer
167323cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
167423cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests
167523cd1385SRemy Bohmer */
16760adb5b76SJoe Hershberger if (is_broadcast_ethaddr(dest))
167723cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST;
167823cd1385SRemy Bohmer else
167923cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
168023cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) {
168123cd1385SRemy Bohmer dev_kfree_skb_any (skb);
168223cd1385SRemy Bohmer return 0;
168323cd1385SRemy Bohmer }
168423cd1385SRemy Bohmer }
168523cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
168623cd1385SRemy Bohmer }
168723cd1385SRemy Bohmer
168823cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags);
168923cd1385SRemy Bohmer /*
169023cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect()
169123cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the
169223cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock.
169323cd1385SRemy Bohmer */
169423cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) {
169523cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags);
169623cd1385SRemy Bohmer return 1;
169723cd1385SRemy Bohmer }
169823cd1385SRemy Bohmer
169923cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list);
170023cd1385SRemy Bohmer list_del (&req->list);
170123cd1385SRemy Bohmer
170223cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */
170323cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs))
170423cd1385SRemy Bohmer netif_stop_queue (net);
170523cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags);
170623cd1385SRemy Bohmer
170723cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it
170823cd1385SRemy Bohmer * or the hardware can't use skb buffers.
170923cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need
171023cd1385SRemy Bohmer */
171123cd1385SRemy Bohmer if (rndis_active(dev)) {
171223cd1385SRemy Bohmer struct sk_buff *skb_rndis;
171323cd1385SRemy Bohmer
171423cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb,
171523cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type));
171623cd1385SRemy Bohmer if (!skb_rndis)
171723cd1385SRemy Bohmer goto drop;
171823cd1385SRemy Bohmer
171923cd1385SRemy Bohmer dev_kfree_skb_any (skb);
172023cd1385SRemy Bohmer skb = skb_rndis;
172123cd1385SRemy Bohmer rndis_add_hdr (skb);
172223cd1385SRemy Bohmer length = skb->len;
172323cd1385SRemy Bohmer }
172423cd1385SRemy Bohmer req->buf = skb->data;
172523cd1385SRemy Bohmer req->context = skb;
172623cd1385SRemy Bohmer req->complete = tx_complete;
172723cd1385SRemy Bohmer
172823cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance,
172923cd1385SRemy Bohmer * though any robust network rx path ignores extra padding.
173023cd1385SRemy Bohmer * and some hardware doesn't like to write zlps.
173123cd1385SRemy Bohmer */
173223cd1385SRemy Bohmer req->zero = 1;
173323cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
173423cd1385SRemy Bohmer length++;
173523cd1385SRemy Bohmer
173623cd1385SRemy Bohmer req->length = length;
173723cd1385SRemy Bohmer
173823cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */
173923cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget))
174023cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
174123cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
174223cd1385SRemy Bohmer : 0;
174323cd1385SRemy Bohmer
174423cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
174523cd1385SRemy Bohmer switch (retval) {
174623cd1385SRemy Bohmer default:
174723cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval);
174823cd1385SRemy Bohmer break;
174923cd1385SRemy Bohmer case 0:
175023cd1385SRemy Bohmer net->trans_start = jiffies;
175123cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen);
175223cd1385SRemy Bohmer }
175323cd1385SRemy Bohmer
175423cd1385SRemy Bohmer if (retval) {
175523cd1385SRemy Bohmer drop:
175623cd1385SRemy Bohmer dev->stats.tx_dropped++;
175723cd1385SRemy Bohmer dev_kfree_skb_any (skb);
175823cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags);
175923cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs))
176023cd1385SRemy Bohmer netif_start_queue (net);
176123cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs);
176223cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags);
176323cd1385SRemy Bohmer }
176423cd1385SRemy Bohmer return 0;
176523cd1385SRemy Bohmer }
176623cd1385SRemy Bohmer
176723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
176823cd1385SRemy Bohmer #endif
176923cd1385SRemy Bohmer
eth_unbind(struct usb_gadget * gadget)177023cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget)
177123cd1385SRemy Bohmer {
177223cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget);
177323cd1385SRemy Bohmer
17747de73185SVitaly Kuzmichev debug("%s...\n", __func__);
17757612a43dSVitaly Kuzmichev rndis_deregister(dev->rndis_config);
17767612a43dSVitaly Kuzmichev rndis_exit();
177723cd1385SRemy Bohmer
17780129e327SVitaly Kuzmichev /* we've already been disconnected ... no i/o is active */
17790129e327SVitaly Kuzmichev if (dev->req) {
17800129e327SVitaly Kuzmichev usb_ep_free_request(gadget->ep0, dev->req);
17810129e327SVitaly Kuzmichev dev->req = NULL;
17820129e327SVitaly Kuzmichev }
178323cd1385SRemy Bohmer if (dev->stat_req) {
178423cd1385SRemy Bohmer usb_ep_free_request(dev->status_ep, dev->stat_req);
178523cd1385SRemy Bohmer dev->stat_req = NULL;
178623cd1385SRemy Bohmer }
178723cd1385SRemy Bohmer
178823cd1385SRemy Bohmer if (dev->tx_req) {
178923cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req);
179023cd1385SRemy Bohmer dev->tx_req = NULL;
179123cd1385SRemy Bohmer }
179223cd1385SRemy Bohmer
179323cd1385SRemy Bohmer if (dev->rx_req) {
17940129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req);
179523cd1385SRemy Bohmer dev->rx_req = NULL;
179623cd1385SRemy Bohmer }
179723cd1385SRemy Bohmer
179823cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/
179923cd1385SRemy Bohmer /* free_netdev(dev->net);*/
180023cd1385SRemy Bohmer
1801988ee3e3SLei Wen dev->gadget = NULL;
180223cd1385SRemy Bohmer set_gadget_data(gadget, NULL);
180323cd1385SRemy Bohmer }
180423cd1385SRemy Bohmer
eth_disconnect(struct usb_gadget * gadget)180523cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget)
180623cd1385SRemy Bohmer {
180723cd1385SRemy Bohmer eth_reset_config(get_gadget_data(gadget));
18087612a43dSVitaly Kuzmichev /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
180923cd1385SRemy Bohmer }
181023cd1385SRemy Bohmer
eth_suspend(struct usb_gadget * gadget)181123cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget)
181223cd1385SRemy Bohmer {
181323cd1385SRemy Bohmer /* Not used */
181423cd1385SRemy Bohmer }
181523cd1385SRemy Bohmer
eth_resume(struct usb_gadget * gadget)181623cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget)
181723cd1385SRemy Bohmer {
181823cd1385SRemy Bohmer /* Not used */
181923cd1385SRemy Bohmer }
182023cd1385SRemy Bohmer
182123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
182223cd1385SRemy Bohmer
18237612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
18247612a43dSVitaly Kuzmichev
18257612a43dSVitaly Kuzmichev /*
18267612a43dSVitaly Kuzmichev * The interrupt endpoint is used in RNDIS to notify the host when messages
18277612a43dSVitaly Kuzmichev * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
18287612a43dSVitaly Kuzmichev * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
18297612a43dSVitaly Kuzmichev * REMOTE_NDIS_KEEPALIVE_MSG.
18307612a43dSVitaly Kuzmichev *
18317612a43dSVitaly Kuzmichev * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
18327612a43dSVitaly Kuzmichev * normally just one notification will be queued.
18337612a43dSVitaly Kuzmichev */
18347612a43dSVitaly Kuzmichev
rndis_control_ack_complete(struct usb_ep * ep,struct usb_request * req)18357612a43dSVitaly Kuzmichev static void rndis_control_ack_complete(struct usb_ep *ep,
18367612a43dSVitaly Kuzmichev struct usb_request *req)
18377612a43dSVitaly Kuzmichev {
18387612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data;
18397612a43dSVitaly Kuzmichev
18407612a43dSVitaly Kuzmichev debug("%s...\n", __func__);
18417612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length)
18427612a43dSVitaly Kuzmichev debug("rndis control ack complete --> %d, %d/%d\n",
18437612a43dSVitaly Kuzmichev req->status, req->actual, req->length);
18447612a43dSVitaly Kuzmichev
184517b4f308SMugunthan V N if (!dev->network_started) {
18467612a43dSVitaly Kuzmichev if (rndis_get_state(dev->rndis_config)
18477612a43dSVitaly Kuzmichev == RNDIS_DATA_INITIALIZED) {
184817b4f308SMugunthan V N dev->network_started = 1;
18497612a43dSVitaly Kuzmichev printf("USB RNDIS network up!\n");
18507612a43dSVitaly Kuzmichev }
18517612a43dSVitaly Kuzmichev }
18527612a43dSVitaly Kuzmichev
18537612a43dSVitaly Kuzmichev req->context = NULL;
18547612a43dSVitaly Kuzmichev
18557612a43dSVitaly Kuzmichev if (req != dev->stat_req)
18567612a43dSVitaly Kuzmichev usb_ep_free_request(ep, req);
18577612a43dSVitaly Kuzmichev }
18587612a43dSVitaly Kuzmichev
18597612a43dSVitaly Kuzmichev static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
18607612a43dSVitaly Kuzmichev
1861d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
rndis_control_ack(struct eth_device * net)18627612a43dSVitaly Kuzmichev static int rndis_control_ack(struct eth_device *net)
1863d4a37553SMugunthan V N #else
1864d4a37553SMugunthan V N static int rndis_control_ack(struct udevice *net)
1865d4a37553SMugunthan V N #endif
18667612a43dSVitaly Kuzmichev {
1867ae70100cSMugunthan V N struct ether_priv *priv = (struct ether_priv *)net->priv;
1868ae70100cSMugunthan V N struct eth_dev *dev = &priv->ethdev;
18697612a43dSVitaly Kuzmichev int length;
18707612a43dSVitaly Kuzmichev struct usb_request *resp = dev->stat_req;
18717612a43dSVitaly Kuzmichev
18727612a43dSVitaly Kuzmichev /* in case RNDIS calls this after disconnect */
18737612a43dSVitaly Kuzmichev if (!dev->status) {
18747612a43dSVitaly Kuzmichev debug("status ENODEV\n");
18757612a43dSVitaly Kuzmichev return -ENODEV;
18767612a43dSVitaly Kuzmichev }
18777612a43dSVitaly Kuzmichev
18787612a43dSVitaly Kuzmichev /* in case queue length > 1 */
18797612a43dSVitaly Kuzmichev if (resp->context) {
18807612a43dSVitaly Kuzmichev resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
18817612a43dSVitaly Kuzmichev if (!resp)
18827612a43dSVitaly Kuzmichev return -ENOMEM;
18837612a43dSVitaly Kuzmichev resp->buf = rndis_resp_buf;
18847612a43dSVitaly Kuzmichev }
18857612a43dSVitaly Kuzmichev
18867612a43dSVitaly Kuzmichev /*
18877612a43dSVitaly Kuzmichev * Send RNDIS RESPONSE_AVAILABLE notification;
18887612a43dSVitaly Kuzmichev * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
18897612a43dSVitaly Kuzmichev */
18907612a43dSVitaly Kuzmichev resp->length = 8;
18917612a43dSVitaly Kuzmichev resp->complete = rndis_control_ack_complete;
18927612a43dSVitaly Kuzmichev resp->context = dev;
18937612a43dSVitaly Kuzmichev
18947612a43dSVitaly Kuzmichev *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
18957612a43dSVitaly Kuzmichev *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
18967612a43dSVitaly Kuzmichev
18977612a43dSVitaly Kuzmichev length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
18987612a43dSVitaly Kuzmichev if (length < 0) {
18997612a43dSVitaly Kuzmichev resp->status = 0;
19007612a43dSVitaly Kuzmichev rndis_control_ack_complete(dev->status_ep, resp);
19017612a43dSVitaly Kuzmichev }
19027612a43dSVitaly Kuzmichev
19037612a43dSVitaly Kuzmichev return 0;
19047612a43dSVitaly Kuzmichev }
19057612a43dSVitaly Kuzmichev
19067612a43dSVitaly Kuzmichev #else
19077612a43dSVitaly Kuzmichev
19087612a43dSVitaly Kuzmichev #define rndis_control_ack NULL
19097612a43dSVitaly Kuzmichev
19107612a43dSVitaly Kuzmichev #endif /* RNDIS */
19117612a43dSVitaly Kuzmichev
eth_start(struct eth_dev * dev,gfp_t gfp_flags)19127612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
19137612a43dSVitaly Kuzmichev {
19147612a43dSVitaly Kuzmichev if (rndis_active(dev)) {
19157612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config,
19167612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3,
19177612a43dSVitaly Kuzmichev BITRATE(dev->gadget)/100);
19187612a43dSVitaly Kuzmichev rndis_signal_connect(dev->rndis_config);
19197612a43dSVitaly Kuzmichev }
19207612a43dSVitaly Kuzmichev }
19217612a43dSVitaly Kuzmichev
eth_stop(struct eth_dev * dev)19227612a43dSVitaly Kuzmichev static int eth_stop(struct eth_dev *dev)
19237612a43dSVitaly Kuzmichev {
1924e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1925e4ae6660SVitaly Kuzmichev unsigned long ts;
1926e4ae6660SVitaly Kuzmichev unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1927e4ae6660SVitaly Kuzmichev #endif
1928e4ae6660SVitaly Kuzmichev
19297612a43dSVitaly Kuzmichev if (rndis_active(dev)) {
19307612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
19317612a43dSVitaly Kuzmichev rndis_signal_disconnect(dev->rndis_config);
19327612a43dSVitaly Kuzmichev
1933e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1934e4ae6660SVitaly Kuzmichev /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1935e4ae6660SVitaly Kuzmichev ts = get_timer(0);
1936e4ae6660SVitaly Kuzmichev while (get_timer(ts) < timeout)
19372d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
1938e4ae6660SVitaly Kuzmichev #endif
1939e4ae6660SVitaly Kuzmichev
19407612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config);
19417612a43dSVitaly Kuzmichev dev->rndis = 0;
19427612a43dSVitaly Kuzmichev }
19437612a43dSVitaly Kuzmichev
19447612a43dSVitaly Kuzmichev return 0;
19457612a43dSVitaly Kuzmichev }
19467612a43dSVitaly Kuzmichev
19477612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
19487612a43dSVitaly Kuzmichev
is_eth_addr_valid(char * str)194923cd1385SRemy Bohmer static int is_eth_addr_valid(char *str)
195023cd1385SRemy Bohmer {
195123cd1385SRemy Bohmer if (strlen(str) == 17) {
195223cd1385SRemy Bohmer int i;
195323cd1385SRemy Bohmer char *p, *q;
195423cd1385SRemy Bohmer uchar ea[6];
195523cd1385SRemy Bohmer
195623cd1385SRemy Bohmer /* see if it looks like an ethernet address */
195723cd1385SRemy Bohmer
195823cd1385SRemy Bohmer p = str;
195923cd1385SRemy Bohmer
196023cd1385SRemy Bohmer for (i = 0; i < 6; i++) {
196123cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':');
196223cd1385SRemy Bohmer
196323cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16);
196423cd1385SRemy Bohmer
196523cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term)
196623cd1385SRemy Bohmer break;
196723cd1385SRemy Bohmer
196823cd1385SRemy Bohmer p = q;
196923cd1385SRemy Bohmer }
197023cd1385SRemy Bohmer
197157a87a25STom Rini /* Now check the contents. */
19720adb5b76SJoe Hershberger return is_valid_ethaddr(ea);
197323cd1385SRemy Bohmer }
197423cd1385SRemy Bohmer return 0;
197523cd1385SRemy Bohmer }
197623cd1385SRemy Bohmer
nibble(unsigned char c)197723cd1385SRemy Bohmer static u8 nibble(unsigned char c)
197823cd1385SRemy Bohmer {
197923cd1385SRemy Bohmer if (likely(isdigit(c)))
198023cd1385SRemy Bohmer return c - '0';
198123cd1385SRemy Bohmer c = toupper(c);
198223cd1385SRemy Bohmer if (likely(isxdigit(c)))
198323cd1385SRemy Bohmer return 10 + c - 'A';
198423cd1385SRemy Bohmer return 0;
198523cd1385SRemy Bohmer }
198623cd1385SRemy Bohmer
get_ether_addr(const char * str,u8 * dev_addr)198723cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr)
198823cd1385SRemy Bohmer {
198923cd1385SRemy Bohmer if (str) {
199023cd1385SRemy Bohmer unsigned i;
199123cd1385SRemy Bohmer
199223cd1385SRemy Bohmer for (i = 0; i < 6; i++) {
199323cd1385SRemy Bohmer unsigned char num;
199423cd1385SRemy Bohmer
199523cd1385SRemy Bohmer if ((*str == '.') || (*str == ':'))
199623cd1385SRemy Bohmer str++;
199723cd1385SRemy Bohmer num = nibble(*str++) << 4;
199823cd1385SRemy Bohmer num |= (nibble(*str++));
199923cd1385SRemy Bohmer dev_addr[i] = num;
200023cd1385SRemy Bohmer }
20010adb5b76SJoe Hershberger if (is_valid_ethaddr(dev_addr))
200223cd1385SRemy Bohmer return 0;
200323cd1385SRemy Bohmer }
200423cd1385SRemy Bohmer return 1;
200523cd1385SRemy Bohmer }
200623cd1385SRemy Bohmer
eth_bind(struct usb_gadget * gadget)200723cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget)
200823cd1385SRemy Bohmer {
20095cb3b9d7SMugunthan V N struct eth_dev *dev = &l_priv->ethdev;
20107612a43dSVitaly Kuzmichev u8 cdc = 1, zlp = 1, rndis = 1;
201123cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
20127612a43dSVitaly Kuzmichev int status = -ENOMEM;
201323cd1385SRemy Bohmer int gcnum;
201423cd1385SRemy Bohmer u8 tmp[7];
2015d4a37553SMugunthan V N #ifdef CONFIG_DM_ETH
2016d4a37553SMugunthan V N struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2017d4a37553SMugunthan V N #endif
201823cd1385SRemy Bohmer
201923cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */
20202bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_CDC
202123cd1385SRemy Bohmer cdc = 0;
202223cd1385SRemy Bohmer #endif
20237612a43dSVitaly Kuzmichev #ifndef CONFIG_USB_ETH_RNDIS
20247612a43dSVitaly Kuzmichev rndis = 0;
20257612a43dSVitaly Kuzmichev #endif
20266142e0aeSVitaly Kuzmichev /*
20276142e0aeSVitaly Kuzmichev * Because most host side USB stacks handle CDC Ethernet, that
202823cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes.
202923cd1385SRemy Bohmer * (By everyone except Microsoft.)
203023cd1385SRemy Bohmer */
203123cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) {
203223cd1385SRemy Bohmer /* pxa doesn't support altsettings */
203323cd1385SRemy Bohmer cdc = 0;
203423cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) {
203523cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */
203623cd1385SRemy Bohmer zlp = 0;
203723cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) {
203823cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */
203923cd1385SRemy Bohmer cdc = 0;
20407612a43dSVitaly Kuzmichev rndis = 0;
204123cd1385SRemy Bohmer } else if (gadget_is_sa1100(gadget)) {
204223cd1385SRemy Bohmer /* hardware can't write zlps */
204323cd1385SRemy Bohmer zlp = 0;
20446142e0aeSVitaly Kuzmichev /*
20456142e0aeSVitaly Kuzmichev * sa1100 CAN do CDC, without status endpoint ... we use
204623cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
204723cd1385SRemy Bohmer */
204823cd1385SRemy Bohmer cdc = 0;
204923cd1385SRemy Bohmer }
205023cd1385SRemy Bohmer
205123cd1385SRemy Bohmer gcnum = usb_gadget_controller_number(gadget);
205223cd1385SRemy Bohmer if (gcnum >= 0)
205323cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
205423cd1385SRemy Bohmer else {
20556142e0aeSVitaly Kuzmichev /*
20566142e0aeSVitaly Kuzmichev * can't assume CDC works. don't want to default to
205723cd1385SRemy Bohmer * anything less functional on CDC-capable hardware,
205823cd1385SRemy Bohmer * so we fail in this case.
205923cd1385SRemy Bohmer */
206090aa625cSMasahiro Yamada pr_err("controller '%s' not recognized",
206123cd1385SRemy Bohmer gadget->name);
206223cd1385SRemy Bohmer return -ENODEV;
206323cd1385SRemy Bohmer }
206423cd1385SRemy Bohmer
20656142e0aeSVitaly Kuzmichev /*
20667612a43dSVitaly Kuzmichev * If there's an RNDIS configuration, that's what Windows wants to
20677612a43dSVitaly Kuzmichev * be using ... so use these product IDs here and in the "linux.inf"
20687612a43dSVitaly Kuzmichev * needed to install MSFT drivers. Current Linux kernels will use
20697612a43dSVitaly Kuzmichev * the second configuration if it's CDC Ethernet, and need some help
20707612a43dSVitaly Kuzmichev * to choose the right configuration otherwise.
20717612a43dSVitaly Kuzmichev */
20727612a43dSVitaly Kuzmichev if (rndis) {
2073b466df35SMaxime Ripard #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
20747612a43dSVitaly Kuzmichev device_desc.idVendor =
2075b466df35SMaxime Ripard __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
20767612a43dSVitaly Kuzmichev device_desc.idProduct =
2077b466df35SMaxime Ripard __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
20787612a43dSVitaly Kuzmichev #else
20797612a43dSVitaly Kuzmichev device_desc.idVendor =
20807612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
20817612a43dSVitaly Kuzmichev device_desc.idProduct =
20827612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
20837612a43dSVitaly Kuzmichev #endif
20847612a43dSVitaly Kuzmichev sprintf(product_desc, "RNDIS/%s", driver_desc);
20857612a43dSVitaly Kuzmichev
20867612a43dSVitaly Kuzmichev /*
20876142e0aeSVitaly Kuzmichev * CDC subset ... recognized by Linux since 2.4.10, but Windows
208823cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by
208923cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.)
209023cd1385SRemy Bohmer */
20917612a43dSVitaly Kuzmichev } else {
2092b466df35SMaxime Ripard #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2093b466df35SMaxime Ripard device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2094b466df35SMaxime Ripard device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
20957612a43dSVitaly Kuzmichev #else
209623cd1385SRemy Bohmer if (!cdc) {
209723cd1385SRemy Bohmer device_desc.idVendor =
209823cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
209923cd1385SRemy Bohmer device_desc.idProduct =
210023cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
210123cd1385SRemy Bohmer }
210223cd1385SRemy Bohmer #endif
21037612a43dSVitaly Kuzmichev }
21047612a43dSVitaly Kuzmichev /* support optional vendor/distro customization */
210523cd1385SRemy Bohmer if (bcdDevice)
210623cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice);
210723cd1385SRemy Bohmer if (iManufacturer)
21082e12abe6SVitaly Kuzmichev strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
210923cd1385SRemy Bohmer if (iProduct)
21102e12abe6SVitaly Kuzmichev strlcpy(product_desc, iProduct, sizeof product_desc);
211123cd1385SRemy Bohmer if (iSerialNumber) {
211223cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER,
21132e12abe6SVitaly Kuzmichev strlcpy(serial_number, iSerialNumber, sizeof serial_number);
211423cd1385SRemy Bohmer }
211523cd1385SRemy Bohmer
211623cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */
211723cd1385SRemy Bohmer usb_ep_autoconfig_reset(gadget);
211823cd1385SRemy Bohmer in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
211923cd1385SRemy Bohmer if (!in_ep) {
212023cd1385SRemy Bohmer autoconf_fail:
212190aa625cSMasahiro Yamada pr_err("can't autoconfigure on %s\n",
212223cd1385SRemy Bohmer gadget->name);
212323cd1385SRemy Bohmer return -ENODEV;
212423cd1385SRemy Bohmer }
212523cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */
212623cd1385SRemy Bohmer
212723cd1385SRemy Bohmer out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
212823cd1385SRemy Bohmer if (!out_ep)
212923cd1385SRemy Bohmer goto autoconf_fail;
213023cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */
213123cd1385SRemy Bohmer
21322bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
21336142e0aeSVitaly Kuzmichev /*
21346142e0aeSVitaly Kuzmichev * CDC Ethernet control interface doesn't require a status endpoint.
213523cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway.
213623cd1385SRemy Bohmer */
21377612a43dSVitaly Kuzmichev if (cdc || rndis) {
213823cd1385SRemy Bohmer status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
213923cd1385SRemy Bohmer if (status_ep) {
214023cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */
21417612a43dSVitaly Kuzmichev } else if (rndis) {
214290aa625cSMasahiro Yamada pr_err("can't run RNDIS on %s", gadget->name);
21437612a43dSVitaly Kuzmichev return -ENODEV;
21442bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
214523cd1385SRemy Bohmer } else if (cdc) {
214623cd1385SRemy Bohmer control_intf.bNumEndpoints = 0;
214723cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */
21487612a43dSVitaly Kuzmichev #endif
214923cd1385SRemy Bohmer }
215023cd1385SRemy Bohmer }
215123cd1385SRemy Bohmer #endif
215223cd1385SRemy Bohmer
215323cd1385SRemy Bohmer /* one config: cdc, else minimal subset */
215423cd1385SRemy Bohmer if (!cdc) {
215523cd1385SRemy Bohmer eth_config.bNumInterfaces = 1;
215623cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET;
215723cd1385SRemy Bohmer
21586142e0aeSVitaly Kuzmichev /*
21596142e0aeSVitaly Kuzmichev * use functions to set these up, in case we're built to work
216023cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet.
216123cd1385SRemy Bohmer */
216223cd1385SRemy Bohmer fs_subset_descriptors();
216323cd1385SRemy Bohmer hs_subset_descriptors();
216423cd1385SRemy Bohmer }
216523cd1385SRemy Bohmer
216623cd1385SRemy Bohmer usb_gadget_set_selfpowered(gadget);
216723cd1385SRemy Bohmer
21687612a43dSVitaly Kuzmichev /* For now RNDIS is always a second config */
21697612a43dSVitaly Kuzmichev if (rndis)
21707612a43dSVitaly Kuzmichev device_desc.bNumConfigurations = 2;
21717612a43dSVitaly Kuzmichev
217223cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) {
21737612a43dSVitaly Kuzmichev if (rndis)
21747612a43dSVitaly Kuzmichev dev_qualifier.bNumConfigurations = 2;
21757612a43dSVitaly Kuzmichev else if (!cdc)
217623cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
217723cd1385SRemy Bohmer
217823cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */
217923cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
218023cd1385SRemy Bohmer
218123cd1385SRemy Bohmer /* and that all endpoints are dual-speed */
218223cd1385SRemy Bohmer hs_source_desc.bEndpointAddress =
218323cd1385SRemy Bohmer fs_source_desc.bEndpointAddress;
218423cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress =
218523cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress;
21862bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
218723cd1385SRemy Bohmer if (status_ep)
218823cd1385SRemy Bohmer hs_status_desc.bEndpointAddress =
218923cd1385SRemy Bohmer fs_status_desc.bEndpointAddress;
219023cd1385SRemy Bohmer #endif
219123cd1385SRemy Bohmer }
219223cd1385SRemy Bohmer
219323cd1385SRemy Bohmer if (gadget_is_otg(gadget)) {
219423cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP,
219523cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
219623cd1385SRemy Bohmer eth_config.bMaxPower = 4;
21977612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
21987612a43dSVitaly Kuzmichev rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
21997612a43dSVitaly Kuzmichev rndis_config.bMaxPower = 4;
22007612a43dSVitaly Kuzmichev #endif
220123cd1385SRemy Bohmer }
220223cd1385SRemy Bohmer
22037612a43dSVitaly Kuzmichev
22047612a43dSVitaly Kuzmichev /* network device setup */
2205d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
22065cb3b9d7SMugunthan V N dev->net = &l_priv->netdev;
2207d4a37553SMugunthan V N #else
2208d4a37553SMugunthan V N dev->net = l_priv->netdev;
2209d4a37553SMugunthan V N #endif
221023cd1385SRemy Bohmer
221123cd1385SRemy Bohmer dev->cdc = cdc;
221223cd1385SRemy Bohmer dev->zlp = zlp;
221323cd1385SRemy Bohmer
221423cd1385SRemy Bohmer dev->in_ep = in_ep;
221523cd1385SRemy Bohmer dev->out_ep = out_ep;
221623cd1385SRemy Bohmer dev->status_ep = status_ep;
221723cd1385SRemy Bohmer
2218d4a37553SMugunthan V N memset(tmp, 0, sizeof(tmp));
22196142e0aeSVitaly Kuzmichev /*
22206142e0aeSVitaly Kuzmichev * Module params for these addresses should come from ID proms.
22217612a43dSVitaly Kuzmichev * The host side address is used with CDC and RNDIS, and commonly
222223cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if
222323cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN
222423cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii.
222523cd1385SRemy Bohmer */
2226d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
222723cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr);
222823cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2229d4a37553SMugunthan V N #else
2230d4a37553SMugunthan V N get_ether_addr(dev_addr, pdata->enetaddr);
2231d4a37553SMugunthan V N memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2232d4a37553SMugunthan V N #endif
223323cd1385SRemy Bohmer
223423cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac);
223523cd1385SRemy Bohmer
223623cd1385SRemy Bohmer sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
223723cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1],
223823cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3],
223923cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]);
224023cd1385SRemy Bohmer
22417612a43dSVitaly Kuzmichev if (rndis) {
22427612a43dSVitaly Kuzmichev status = rndis_init();
22437612a43dSVitaly Kuzmichev if (status < 0) {
224490aa625cSMasahiro Yamada pr_err("can't init RNDIS, %d", status);
22457612a43dSVitaly Kuzmichev goto fail;
22467612a43dSVitaly Kuzmichev }
224723cd1385SRemy Bohmer }
224823cd1385SRemy Bohmer
22496142e0aeSVitaly Kuzmichev /*
22506142e0aeSVitaly Kuzmichev * use PKTSIZE (or aligned... from u-boot) and set
22516142e0aeSVitaly Kuzmichev * wMaxSegmentSize accordingly
22526142e0aeSVitaly Kuzmichev */
225323cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
225423cd1385SRemy Bohmer
225523cd1385SRemy Bohmer /* preallocate control message data and buffer */
225623cd1385SRemy Bohmer dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
225723cd1385SRemy Bohmer if (!dev->req)
225823cd1385SRemy Bohmer goto fail;
225923cd1385SRemy Bohmer dev->req->buf = control_req;
226023cd1385SRemy Bohmer dev->req->complete = eth_setup_complete;
226123cd1385SRemy Bohmer
226223cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */
22632bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
226423cd1385SRemy Bohmer if (dev->status_ep) {
22656142e0aeSVitaly Kuzmichev dev->stat_req = usb_ep_alloc_request(dev->status_ep,
22666142e0aeSVitaly Kuzmichev GFP_KERNEL);
226723cd1385SRemy Bohmer if (!dev->stat_req) {
2268df559c1dSVitaly Kuzmichev usb_ep_free_request(dev->status_ep, dev->req);
226923cd1385SRemy Bohmer
227023cd1385SRemy Bohmer goto fail;
227123cd1385SRemy Bohmer }
2272df559c1dSVitaly Kuzmichev dev->stat_req->buf = status_req;
227323cd1385SRemy Bohmer dev->stat_req->context = NULL;
227423cd1385SRemy Bohmer }
227523cd1385SRemy Bohmer #endif
227623cd1385SRemy Bohmer
227723cd1385SRemy Bohmer /* finish hookup to lower layer ... */
227823cd1385SRemy Bohmer dev->gadget = gadget;
227923cd1385SRemy Bohmer set_gadget_data(gadget, dev);
228023cd1385SRemy Bohmer gadget->ep0->driver_data = dev;
228123cd1385SRemy Bohmer
22826142e0aeSVitaly Kuzmichev /*
22836142e0aeSVitaly Kuzmichev * two kinds of host-initiated state changes:
228423cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on"
228523cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on"
228623cd1385SRemy Bohmer */
22877612a43dSVitaly Kuzmichev
22887612a43dSVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
22897612a43dSVitaly Kuzmichev out_ep->name, in_ep->name,
22907612a43dSVitaly Kuzmichev status_ep ? " STATUS " : "",
22917612a43dSVitaly Kuzmichev status_ep ? status_ep->name : ""
22927612a43dSVitaly Kuzmichev );
2293d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
2294d4a37553SMugunthan V N printf("MAC %pM\n", dev->net->enetaddr);
2295d4a37553SMugunthan V N #else
2296d4a37553SMugunthan V N printf("MAC %pM\n", pdata->enetaddr);
2297d4a37553SMugunthan V N #endif
22987612a43dSVitaly Kuzmichev
22997612a43dSVitaly Kuzmichev if (cdc || rndis)
23007612a43dSVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
23017612a43dSVitaly Kuzmichev dev->host_mac[0], dev->host_mac[1],
23027612a43dSVitaly Kuzmichev dev->host_mac[2], dev->host_mac[3],
23037612a43dSVitaly Kuzmichev dev->host_mac[4], dev->host_mac[5]);
23047612a43dSVitaly Kuzmichev
23057612a43dSVitaly Kuzmichev if (rndis) {
23067612a43dSVitaly Kuzmichev u32 vendorID = 0;
23077612a43dSVitaly Kuzmichev
23087612a43dSVitaly Kuzmichev /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
23097612a43dSVitaly Kuzmichev
23107612a43dSVitaly Kuzmichev dev->rndis_config = rndis_register(rndis_control_ack);
23117612a43dSVitaly Kuzmichev if (dev->rndis_config < 0) {
23127612a43dSVitaly Kuzmichev fail0:
23137612a43dSVitaly Kuzmichev eth_unbind(gadget);
23147612a43dSVitaly Kuzmichev debug("RNDIS setup failed\n");
23157612a43dSVitaly Kuzmichev status = -ENODEV;
23167612a43dSVitaly Kuzmichev goto fail;
23177612a43dSVitaly Kuzmichev }
23187612a43dSVitaly Kuzmichev
23197612a43dSVitaly Kuzmichev /* these set up a lot of the OIDs that RNDIS needs */
23207612a43dSVitaly Kuzmichev rndis_set_host_mac(dev->rndis_config, dev->host_mac);
23217612a43dSVitaly Kuzmichev if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
23227612a43dSVitaly Kuzmichev &dev->stats, &dev->cdc_filter))
23237612a43dSVitaly Kuzmichev goto fail0;
23247612a43dSVitaly Kuzmichev if (rndis_set_param_vendor(dev->rndis_config, vendorID,
23257612a43dSVitaly Kuzmichev manufacturer))
23267612a43dSVitaly Kuzmichev goto fail0;
23277612a43dSVitaly Kuzmichev if (rndis_set_param_medium(dev->rndis_config,
23287612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3, 0))
23297612a43dSVitaly Kuzmichev goto fail0;
23307612a43dSVitaly Kuzmichev printf("RNDIS ready\n");
23317612a43dSVitaly Kuzmichev }
233223cd1385SRemy Bohmer return 0;
233323cd1385SRemy Bohmer
233423cd1385SRemy Bohmer fail:
233590aa625cSMasahiro Yamada pr_err("%s failed, status = %d", __func__, status);
233623cd1385SRemy Bohmer eth_unbind(gadget);
23377612a43dSVitaly Kuzmichev return status;
233823cd1385SRemy Bohmer }
233923cd1385SRemy Bohmer
23407612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
2341b1c323b1SJean-Jacques Hiblot static void _usb_eth_halt(struct ether_priv *priv);
2342b1c323b1SJean-Jacques Hiblot
_usb_eth_init(struct ether_priv * priv)23438269ee4fSMugunthan V N static int _usb_eth_init(struct ether_priv *priv)
234423cd1385SRemy Bohmer {
2345ae70100cSMugunthan V N struct eth_dev *dev = &priv->ethdev;
234623cd1385SRemy Bohmer struct usb_gadget *gadget;
234723cd1385SRemy Bohmer unsigned long ts;
2348b95d4446SJean-Jacques Hiblot int ret;
234923cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT;
235023cd1385SRemy Bohmer
2351b95d4446SJean-Jacques Hiblot ret = usb_gadget_initialize(0);
2352b95d4446SJean-Jacques Hiblot if (ret)
2353b95d4446SJean-Jacques Hiblot return ret;
23548bfc288cSKishon Vijay Abraham I
235558939fccSVitaly Kuzmichev /* Configure default mac-addresses for the USB ethernet device */
235658939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR
235758939fccSVitaly Kuzmichev strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
235858939fccSVitaly Kuzmichev #endif
235958939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR
236058939fccSVitaly Kuzmichev strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
236158939fccSVitaly Kuzmichev #endif
236258939fccSVitaly Kuzmichev /* Check if the user overruled the MAC addresses */
236300caae6dSSimon Glass if (env_get("usbnet_devaddr"))
236400caae6dSSimon Glass strlcpy(dev_addr, env_get("usbnet_devaddr"),
236558939fccSVitaly Kuzmichev sizeof(dev_addr));
236658939fccSVitaly Kuzmichev
236700caae6dSSimon Glass if (env_get("usbnet_hostaddr"))
236800caae6dSSimon Glass strlcpy(host_addr, env_get("usbnet_hostaddr"),
236958939fccSVitaly Kuzmichev sizeof(host_addr));
237058939fccSVitaly Kuzmichev
237158939fccSVitaly Kuzmichev if (!is_eth_addr_valid(dev_addr)) {
237290aa625cSMasahiro Yamada pr_err("Need valid 'usbnet_devaddr' to be set");
237358939fccSVitaly Kuzmichev goto fail;
237458939fccSVitaly Kuzmichev }
237558939fccSVitaly Kuzmichev if (!is_eth_addr_valid(host_addr)) {
237690aa625cSMasahiro Yamada pr_err("Need valid 'usbnet_hostaddr' to be set");
237758939fccSVitaly Kuzmichev goto fail;
237858939fccSVitaly Kuzmichev }
237958939fccSVitaly Kuzmichev
2380ae70100cSMugunthan V N priv->eth_driver.speed = DEVSPEED;
2381ae70100cSMugunthan V N priv->eth_driver.bind = eth_bind;
2382ae70100cSMugunthan V N priv->eth_driver.unbind = eth_unbind;
2383ae70100cSMugunthan V N priv->eth_driver.setup = eth_setup;
2384ae70100cSMugunthan V N priv->eth_driver.reset = eth_disconnect;
2385ae70100cSMugunthan V N priv->eth_driver.disconnect = eth_disconnect;
2386ae70100cSMugunthan V N priv->eth_driver.suspend = eth_suspend;
2387ae70100cSMugunthan V N priv->eth_driver.resume = eth_resume;
2388ae70100cSMugunthan V N if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2389988ee3e3SLei Wen goto fail;
239023cd1385SRemy Bohmer
239123cd1385SRemy Bohmer dev->network_started = 0;
239223cd1385SRemy Bohmer
239323cd1385SRemy Bohmer packet_received = 0;
239423cd1385SRemy Bohmer packet_sent = 0;
239523cd1385SRemy Bohmer
239623cd1385SRemy Bohmer gadget = dev->gadget;
239723cd1385SRemy Bohmer usb_gadget_connect(gadget);
239823cd1385SRemy Bohmer
239900caae6dSSimon Glass if (env_get("cdc_connect_timeout"))
240000caae6dSSimon Glass timeout = simple_strtoul(env_get("cdc_connect_timeout"),
240123cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ;
240223cd1385SRemy Bohmer ts = get_timer(0);
240317b4f308SMugunthan V N while (!dev->network_started) {
240423cd1385SRemy Bohmer /* Handle control-c and timeouts */
240523cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) {
240690aa625cSMasahiro Yamada pr_err("The remote end did not respond in time.");
240723cd1385SRemy Bohmer goto fail;
240823cd1385SRemy Bohmer }
24092d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
241023cd1385SRemy Bohmer }
241123cd1385SRemy Bohmer
241298fae970SVitaly Kuzmichev packet_received = 0;
241323cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0);
241423cd1385SRemy Bohmer return 0;
241523cd1385SRemy Bohmer fail:
2416b1c323b1SJean-Jacques Hiblot _usb_eth_halt(priv);
241723cd1385SRemy Bohmer return -1;
241823cd1385SRemy Bohmer }
241923cd1385SRemy Bohmer
_usb_eth_send(struct ether_priv * priv,void * packet,int length)24208269ee4fSMugunthan V N static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
242123cd1385SRemy Bohmer {
242223cd1385SRemy Bohmer int retval;
24237612a43dSVitaly Kuzmichev void *rndis_pkt = NULL;
2424ae70100cSMugunthan V N struct eth_dev *dev = &priv->ethdev;
2425ac5d32d1SVitaly Kuzmichev struct usb_request *req = dev->tx_req;
2426a170f2c7SStefano Babic unsigned long ts;
2427a170f2c7SStefano Babic unsigned long timeout = USB_CONNECT_TIMEOUT;
24287de73185SVitaly Kuzmichev
24297de73185SVitaly Kuzmichev debug("%s:...\n", __func__);
243023cd1385SRemy Bohmer
24317612a43dSVitaly Kuzmichev /* new buffer is needed to include RNDIS header */
24327612a43dSVitaly Kuzmichev if (rndis_active(dev)) {
24337612a43dSVitaly Kuzmichev rndis_pkt = malloc(length +
24347612a43dSVitaly Kuzmichev sizeof(struct rndis_packet_msg_type));
24357612a43dSVitaly Kuzmichev if (!rndis_pkt) {
243690aa625cSMasahiro Yamada pr_err("No memory to alloc RNDIS packet");
24377612a43dSVitaly Kuzmichev goto drop;
24387612a43dSVitaly Kuzmichev }
24397612a43dSVitaly Kuzmichev rndis_add_hdr(rndis_pkt, length);
24407612a43dSVitaly Kuzmichev memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
244110cbe3b6SJoe Hershberger packet, length);
24427612a43dSVitaly Kuzmichev packet = rndis_pkt;
24437612a43dSVitaly Kuzmichev length += sizeof(struct rndis_packet_msg_type);
24447612a43dSVitaly Kuzmichev }
244510cbe3b6SJoe Hershberger req->buf = packet;
244623cd1385SRemy Bohmer req->context = NULL;
244723cd1385SRemy Bohmer req->complete = tx_complete;
244823cd1385SRemy Bohmer
24496142e0aeSVitaly Kuzmichev /*
24506142e0aeSVitaly Kuzmichev * use zlp framing on tx for strict CDC-Ether conformance,
245123cd1385SRemy Bohmer * though any robust network rx path ignores extra padding.
245223cd1385SRemy Bohmer * and some hardware doesn't like to write zlps.
245323cd1385SRemy Bohmer */
245423cd1385SRemy Bohmer req->zero = 1;
245523cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
245623cd1385SRemy Bohmer length++;
245723cd1385SRemy Bohmer
245823cd1385SRemy Bohmer req->length = length;
245923cd1385SRemy Bohmer #if 0
246023cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */
246123cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget))
246223cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
246323cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0;
246423cd1385SRemy Bohmer #endif
246523cd1385SRemy Bohmer dev->tx_qlen = 1;
2466a170f2c7SStefano Babic ts = get_timer(0);
2467a170f2c7SStefano Babic packet_sent = 0;
246823cd1385SRemy Bohmer
246923cd1385SRemy Bohmer retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
247023cd1385SRemy Bohmer
247123cd1385SRemy Bohmer if (!retval)
24727de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__);
24736142e0aeSVitaly Kuzmichev while (!packet_sent) {
2474a170f2c7SStefano Babic if (get_timer(ts) > timeout) {
2475a170f2c7SStefano Babic printf("timeout sending packets to usb ethernet\n");
2476a170f2c7SStefano Babic return -1;
2477a170f2c7SStefano Babic }
24782d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
247923cd1385SRemy Bohmer }
24807612a43dSVitaly Kuzmichev if (rndis_pkt)
24817612a43dSVitaly Kuzmichev free(rndis_pkt);
248223cd1385SRemy Bohmer
248323cd1385SRemy Bohmer return 0;
24847612a43dSVitaly Kuzmichev drop:
24857612a43dSVitaly Kuzmichev dev->stats.tx_dropped++;
24867612a43dSVitaly Kuzmichev return -ENOMEM;
248723cd1385SRemy Bohmer }
248823cd1385SRemy Bohmer
_usb_eth_recv(struct ether_priv * priv)24898269ee4fSMugunthan V N static int _usb_eth_recv(struct ether_priv *priv)
249023cd1385SRemy Bohmer {
24912d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
249223cd1385SRemy Bohmer
249323cd1385SRemy Bohmer return 0;
249423cd1385SRemy Bohmer }
249523cd1385SRemy Bohmer
_usb_eth_halt(struct ether_priv * priv)2496b1c323b1SJean-Jacques Hiblot static void _usb_eth_halt(struct ether_priv *priv)
249723cd1385SRemy Bohmer {
2498ae70100cSMugunthan V N struct eth_dev *dev = &priv->ethdev;
249923cd1385SRemy Bohmer
2500988ee3e3SLei Wen /* If the gadget not registered, simple return */
2501988ee3e3SLei Wen if (!dev->gadget)
2502988ee3e3SLei Wen return;
2503988ee3e3SLei Wen
2504e4ae6660SVitaly Kuzmichev /*
2505e4ae6660SVitaly Kuzmichev * Some USB controllers may need additional deinitialization here
2506e4ae6660SVitaly Kuzmichev * before dropping pull-up (also due to hardware issues).
2507e4ae6660SVitaly Kuzmichev * For example: unhandled interrupt with status stage started may
2508e4ae6660SVitaly Kuzmichev * bring the controller to fully broken state (until board reset).
2509e4ae6660SVitaly Kuzmichev * There are some variants to debug and fix such cases:
2510e4ae6660SVitaly Kuzmichev * 1) In the case of RNDIS connection eth_stop can perform additional
2511e4ae6660SVitaly Kuzmichev * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2512e4ae6660SVitaly Kuzmichev * 2) 'pullup' callback in your UDC driver can be improved to perform
2513e4ae6660SVitaly Kuzmichev * this deinitialization.
2514e4ae6660SVitaly Kuzmichev */
25157612a43dSVitaly Kuzmichev eth_stop(dev);
25167612a43dSVitaly Kuzmichev
251723cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget);
2518b3649f3bSVitaly Kuzmichev
2519b3649f3bSVitaly Kuzmichev /* Clear pending interrupt */
2520b3649f3bSVitaly Kuzmichev if (dev->network_started) {
25212d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
2522b3649f3bSVitaly Kuzmichev dev->network_started = 0;
2523b3649f3bSVitaly Kuzmichev }
2524b3649f3bSVitaly Kuzmichev
2525ae70100cSMugunthan V N usb_gadget_unregister_driver(&priv->eth_driver);
2526b95d4446SJean-Jacques Hiblot usb_gadget_release(0);
252723cd1385SRemy Bohmer }
252823cd1385SRemy Bohmer
2529d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
usb_eth_init(struct eth_device * netdev,bd_t * bd)25308269ee4fSMugunthan V N static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
25318269ee4fSMugunthan V N {
25328269ee4fSMugunthan V N struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25338269ee4fSMugunthan V N
25348269ee4fSMugunthan V N return _usb_eth_init(priv);
25358269ee4fSMugunthan V N }
25368269ee4fSMugunthan V N
usb_eth_send(struct eth_device * netdev,void * packet,int length)25378269ee4fSMugunthan V N static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
25388269ee4fSMugunthan V N {
25398269ee4fSMugunthan V N struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25408269ee4fSMugunthan V N
25418269ee4fSMugunthan V N return _usb_eth_send(priv, packet, length);
25428269ee4fSMugunthan V N }
25438269ee4fSMugunthan V N
usb_eth_recv(struct eth_device * netdev)25448269ee4fSMugunthan V N static int usb_eth_recv(struct eth_device *netdev)
25458269ee4fSMugunthan V N {
25468269ee4fSMugunthan V N struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25478269ee4fSMugunthan V N struct eth_dev *dev = &priv->ethdev;
25488269ee4fSMugunthan V N int ret;
25498269ee4fSMugunthan V N
25508269ee4fSMugunthan V N ret = _usb_eth_recv(priv);
25518269ee4fSMugunthan V N if (ret) {
255290aa625cSMasahiro Yamada pr_err("error packet receive\n");
25538269ee4fSMugunthan V N return ret;
25548269ee4fSMugunthan V N }
25558269ee4fSMugunthan V N
25568269ee4fSMugunthan V N if (!packet_received)
25578269ee4fSMugunthan V N return 0;
25588269ee4fSMugunthan V N
25598269ee4fSMugunthan V N if (dev->rx_req) {
25608269ee4fSMugunthan V N net_process_received_packet(net_rx_packets[0],
25618269ee4fSMugunthan V N dev->rx_req->length);
25628269ee4fSMugunthan V N } else {
256390aa625cSMasahiro Yamada pr_err("dev->rx_req invalid");
25648269ee4fSMugunthan V N }
25658269ee4fSMugunthan V N packet_received = 0;
25668269ee4fSMugunthan V N rx_submit(dev, dev->rx_req, 0);
25678269ee4fSMugunthan V N
25688269ee4fSMugunthan V N return 0;
25698269ee4fSMugunthan V N }
25708269ee4fSMugunthan V N
usb_eth_halt(struct eth_device * netdev)25718269ee4fSMugunthan V N void usb_eth_halt(struct eth_device *netdev)
25728269ee4fSMugunthan V N {
25738269ee4fSMugunthan V N struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25748269ee4fSMugunthan V N
25758269ee4fSMugunthan V N _usb_eth_halt(priv);
25768269ee4fSMugunthan V N }
25778269ee4fSMugunthan V N
usb_eth_initialize(bd_t * bi)257823cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi)
257923cd1385SRemy Bohmer {
25805cb3b9d7SMugunthan V N struct eth_device *netdev = &l_priv->netdev;
258123cd1385SRemy Bohmer
25828f7aa831SVitaly Kuzmichev strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
258323cd1385SRemy Bohmer
258423cd1385SRemy Bohmer netdev->init = usb_eth_init;
258523cd1385SRemy Bohmer netdev->send = usb_eth_send;
258623cd1385SRemy Bohmer netdev->recv = usb_eth_recv;
258723cd1385SRemy Bohmer netdev->halt = usb_eth_halt;
2588ae70100cSMugunthan V N netdev->priv = l_priv;
258923cd1385SRemy Bohmer
259023cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP
259123cd1385SRemy Bohmer #error not supported
259223cd1385SRemy Bohmer #endif
259323cd1385SRemy Bohmer eth_register(netdev);
259423cd1385SRemy Bohmer return 0;
259523cd1385SRemy Bohmer }
2596d4a37553SMugunthan V N #else
usb_eth_start(struct udevice * dev)2597d4a37553SMugunthan V N static int usb_eth_start(struct udevice *dev)
2598d4a37553SMugunthan V N {
2599d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2600d4a37553SMugunthan V N
2601d4a37553SMugunthan V N return _usb_eth_init(priv);
2602d4a37553SMugunthan V N }
2603d4a37553SMugunthan V N
usb_eth_send(struct udevice * dev,void * packet,int length)2604d4a37553SMugunthan V N static int usb_eth_send(struct udevice *dev, void *packet, int length)
2605d4a37553SMugunthan V N {
2606d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2607d4a37553SMugunthan V N
2608d4a37553SMugunthan V N return _usb_eth_send(priv, packet, length);
2609d4a37553SMugunthan V N }
2610d4a37553SMugunthan V N
usb_eth_recv(struct udevice * dev,int flags,uchar ** packetp)2611d4a37553SMugunthan V N static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2612d4a37553SMugunthan V N {
2613d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2614d4a37553SMugunthan V N struct eth_dev *ethdev = &priv->ethdev;
2615d4a37553SMugunthan V N int ret;
2616d4a37553SMugunthan V N
2617d4a37553SMugunthan V N ret = _usb_eth_recv(priv);
2618d4a37553SMugunthan V N if (ret) {
261990aa625cSMasahiro Yamada pr_err("error packet receive\n");
2620d4a37553SMugunthan V N return ret;
2621d4a37553SMugunthan V N }
2622d4a37553SMugunthan V N
2623d4a37553SMugunthan V N if (packet_received) {
2624d4a37553SMugunthan V N if (ethdev->rx_req) {
2625d4a37553SMugunthan V N *packetp = (uchar *)net_rx_packets[0];
2626d4a37553SMugunthan V N return ethdev->rx_req->length;
2627d4a37553SMugunthan V N } else {
262890aa625cSMasahiro Yamada pr_err("dev->rx_req invalid");
2629d4a37553SMugunthan V N return -EFAULT;
2630d4a37553SMugunthan V N }
2631d4a37553SMugunthan V N }
2632d4a37553SMugunthan V N
2633d4a37553SMugunthan V N return -EAGAIN;
2634d4a37553SMugunthan V N }
2635d4a37553SMugunthan V N
usb_eth_free_pkt(struct udevice * dev,uchar * packet,int length)2636d4a37553SMugunthan V N static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2637d4a37553SMugunthan V N int length)
2638d4a37553SMugunthan V N {
2639d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2640d4a37553SMugunthan V N struct eth_dev *ethdev = &priv->ethdev;
2641d4a37553SMugunthan V N
2642d4a37553SMugunthan V N packet_received = 0;
2643d4a37553SMugunthan V N
2644d4a37553SMugunthan V N return rx_submit(ethdev, ethdev->rx_req, 0);
2645d4a37553SMugunthan V N }
2646d4a37553SMugunthan V N
usb_eth_stop(struct udevice * dev)2647d4a37553SMugunthan V N static void usb_eth_stop(struct udevice *dev)
2648d4a37553SMugunthan V N {
2649d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2650d4a37553SMugunthan V N
2651d4a37553SMugunthan V N _usb_eth_halt(priv);
2652d4a37553SMugunthan V N }
2653d4a37553SMugunthan V N
usb_eth_probe(struct udevice * dev)2654d4a37553SMugunthan V N static int usb_eth_probe(struct udevice *dev)
2655d4a37553SMugunthan V N {
2656d4a37553SMugunthan V N struct ether_priv *priv = dev_get_priv(dev);
2657d4a37553SMugunthan V N struct eth_pdata *pdata = dev_get_platdata(dev);
2658d4a37553SMugunthan V N
2659d4a37553SMugunthan V N priv->netdev = dev;
2660d4a37553SMugunthan V N l_priv = priv;
2661d4a37553SMugunthan V N
2662d4a37553SMugunthan V N get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2663fd1e959eSSimon Glass eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2664d4a37553SMugunthan V N
2665d4a37553SMugunthan V N return 0;
2666d4a37553SMugunthan V N }
2667d4a37553SMugunthan V N
2668d4a37553SMugunthan V N static const struct eth_ops usb_eth_ops = {
2669d4a37553SMugunthan V N .start = usb_eth_start,
2670d4a37553SMugunthan V N .send = usb_eth_send,
2671d4a37553SMugunthan V N .recv = usb_eth_recv,
2672d4a37553SMugunthan V N .free_pkt = usb_eth_free_pkt,
2673d4a37553SMugunthan V N .stop = usb_eth_stop,
2674d4a37553SMugunthan V N };
2675d4a37553SMugunthan V N
usb_ether_init(void)2676d4a37553SMugunthan V N int usb_ether_init(void)
2677d4a37553SMugunthan V N {
2678d4a37553SMugunthan V N struct udevice *dev;
2679d4a37553SMugunthan V N struct udevice *usb_dev;
2680d4a37553SMugunthan V N int ret;
2681d4a37553SMugunthan V N
2682*5395ac06SMichal Suchanek uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2683*5395ac06SMichal Suchanek if (!usb_dev) {
268490aa625cSMasahiro Yamada pr_err("No USB device found\n");
2685*5395ac06SMichal Suchanek return -ENODEV;
2686d4a37553SMugunthan V N }
2687d4a37553SMugunthan V N
2688d4a37553SMugunthan V N ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2689d4a37553SMugunthan V N if (!dev || ret) {
269090aa625cSMasahiro Yamada pr_err("usb - not able to bind usb_ether device\n");
2691d4a37553SMugunthan V N return ret;
2692d4a37553SMugunthan V N }
2693d4a37553SMugunthan V N
2694d4a37553SMugunthan V N return 0;
2695d4a37553SMugunthan V N }
2696d4a37553SMugunthan V N
2697d4a37553SMugunthan V N U_BOOT_DRIVER(eth_usb) = {
2698d4a37553SMugunthan V N .name = "usb_ether",
2699d4a37553SMugunthan V N .id = UCLASS_ETH,
2700d4a37553SMugunthan V N .probe = usb_eth_probe,
2701d4a37553SMugunthan V N .ops = &usb_eth_ops,
2702d4a37553SMugunthan V N .priv_auto_alloc_size = sizeof(struct ether_priv),
2703d4a37553SMugunthan V N .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2704d4a37553SMugunthan V N .flags = DM_FLAG_ALLOC_PRIV_DMA,
2705d4a37553SMugunthan V N };
2706d4a37553SMugunthan V N #endif /* CONFIG_DM_ETH */
2707