xref: /openbmc/u-boot/drivers/usb/gadget/ether.c (revision 1b0769f2ed17ffc1cf9b32ad057bc8b160cbcbae)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
223cd1385SRemy Bohmer /*
323cd1385SRemy Bohmer  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
423cd1385SRemy Bohmer  *
523cd1385SRemy Bohmer  * Copyright (C) 2003-2005,2008 David Brownell
623cd1385SRemy Bohmer  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
723cd1385SRemy Bohmer  * Copyright (C) 2008 Nokia Corporation
823cd1385SRemy Bohmer  */
923cd1385SRemy Bohmer 
1023cd1385SRemy Bohmer #include <common.h>
1124b852a7SSimon Glass #include <console.h>
129925f1dbSAlex Kiernan #include <environment.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 
7523cd1385SRemy Bohmer #define DRIVER_DESC		"Ethernet Gadget"
7623cd1385SRemy Bohmer /* Based on linux 2.6.27 version */
7723cd1385SRemy Bohmer #define DRIVER_VERSION		"May Day 2005"
7823cd1385SRemy Bohmer 
7923cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC;
8023cd1385SRemy Bohmer 
8123cd1385SRemy Bohmer #define RX_EXTRA	20		/* guard against rx overflows */
8223cd1385SRemy Bohmer 
837612a43dSVitaly Kuzmichev #ifndef	CONFIG_USB_ETH_RNDIS
847612a43dSVitaly Kuzmichev #define rndis_uninit(x)		do {} while (0)
857612a43dSVitaly Kuzmichev #define rndis_deregister(c)	do {} while (0)
867612a43dSVitaly Kuzmichev #define rndis_exit()		do {} while (0)
877612a43dSVitaly Kuzmichev #endif
887612a43dSVitaly Kuzmichev 
897612a43dSVitaly Kuzmichev /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
9023cd1385SRemy Bohmer #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
9123cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
9223cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
9323cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_DIRECTED)
9423cd1385SRemy Bohmer 
9523cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
9623cd1385SRemy Bohmer 
9723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
988b6b66b4SVitaly Kuzmichev 
998b6b66b4SVitaly Kuzmichev struct eth_dev {
1008b6b66b4SVitaly Kuzmichev 	struct usb_gadget	*gadget;
1018b6b66b4SVitaly Kuzmichev 	struct usb_request	*req;		/* for control responses */
1027612a43dSVitaly Kuzmichev 	struct usb_request	*stat_req;	/* for cdc & rndis status */
1038b6b66b4SVitaly Kuzmichev 
1048b6b66b4SVitaly Kuzmichev 	u8			config;
1058b6b66b4SVitaly Kuzmichev 	struct usb_ep		*in_ep, *out_ep, *status_ep;
1068b6b66b4SVitaly Kuzmichev 	const struct usb_endpoint_descriptor
1078b6b66b4SVitaly Kuzmichev 				*in, *out, *status;
1088b6b66b4SVitaly Kuzmichev 
1098b6b66b4SVitaly Kuzmichev 	struct usb_request	*tx_req, *rx_req;
1108b6b66b4SVitaly Kuzmichev 
111d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
1128b6b66b4SVitaly Kuzmichev 	struct eth_device	*net;
113d4a37553SMugunthan V N #else
114d4a37553SMugunthan V N 	struct udevice		*net;
115d4a37553SMugunthan V N #endif
1168b6b66b4SVitaly Kuzmichev 	struct net_device_stats	stats;
1178b6b66b4SVitaly Kuzmichev 	unsigned int		tx_qlen;
1188b6b66b4SVitaly Kuzmichev 
1198b6b66b4SVitaly Kuzmichev 	unsigned		zlp:1;
1208b6b66b4SVitaly Kuzmichev 	unsigned		cdc:1;
1217612a43dSVitaly Kuzmichev 	unsigned		rndis:1;
1228b6b66b4SVitaly Kuzmichev 	unsigned		suspended:1;
1238b6b66b4SVitaly Kuzmichev 	unsigned		network_started:1;
1248b6b66b4SVitaly Kuzmichev 	u16			cdc_filter;
1258b6b66b4SVitaly Kuzmichev 	unsigned long		todo;
1268b6b66b4SVitaly Kuzmichev 	int			mtu;
1278b6b66b4SVitaly Kuzmichev #define	WORK_RX_MEMORY		0
1287612a43dSVitaly Kuzmichev 	int			rndis_config;
1298b6b66b4SVitaly Kuzmichev 	u8			host_mac[ETH_ALEN];
1308b6b66b4SVitaly Kuzmichev };
1318b6b66b4SVitaly Kuzmichev 
1328b6b66b4SVitaly Kuzmichev /*
1338b6b66b4SVitaly Kuzmichev  * This version autoconfigures as much as possible at run-time.
1348b6b66b4SVitaly Kuzmichev  *
1358b6b66b4SVitaly Kuzmichev  * It also ASSUMES a self-powered device, without remote wakeup,
1368b6b66b4SVitaly Kuzmichev  * although remote wakeup support would make sense.
1378b6b66b4SVitaly Kuzmichev  */
1388b6b66b4SVitaly Kuzmichev 
1398b6b66b4SVitaly Kuzmichev /*-------------------------------------------------------------------------*/
1405cb3b9d7SMugunthan V N struct ether_priv {
1415cb3b9d7SMugunthan V N 	struct eth_dev ethdev;
142d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
1435cb3b9d7SMugunthan V N 	struct eth_device netdev;
144d4a37553SMugunthan V N #else
145d4a37553SMugunthan V N 	struct udevice *netdev;
146d4a37553SMugunthan V N #endif
1475cb3b9d7SMugunthan V N 	struct usb_gadget_driver eth_driver;
1485cb3b9d7SMugunthan V N };
1495cb3b9d7SMugunthan V N 
1505cb3b9d7SMugunthan V N struct ether_priv eth_priv;
1515cb3b9d7SMugunthan V N struct ether_priv *l_priv = &eth_priv;
15223cd1385SRemy Bohmer 
15323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
15423cd1385SRemy Bohmer 
15523cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */
is_cdc(struct eth_dev * dev)15623cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev)
15723cd1385SRemy Bohmer {
1582bb37884SLukasz Dalek #if	!defined(CONFIG_USB_ETH_SUBSET)
15923cd1385SRemy Bohmer 	return 1;		/* only cdc possible */
1602bb37884SLukasz Dalek #elif	!defined(CONFIG_USB_ETH_CDC)
16123cd1385SRemy Bohmer 	return 0;		/* only subset possible */
16223cd1385SRemy Bohmer #else
16323cd1385SRemy Bohmer 	return dev->cdc;	/* depends on what hardware we found */
16423cd1385SRemy Bohmer #endif
16523cd1385SRemy Bohmer }
16623cd1385SRemy Bohmer 
1677612a43dSVitaly Kuzmichev /* "secondary" RNDIS config may sometimes be activated */
rndis_active(struct eth_dev * dev)1687612a43dSVitaly Kuzmichev static inline int rndis_active(struct eth_dev *dev)
1697612a43dSVitaly Kuzmichev {
1707612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
1717612a43dSVitaly Kuzmichev 	return dev->rndis;
1727612a43dSVitaly Kuzmichev #else
1737612a43dSVitaly Kuzmichev 	return 0;
1747612a43dSVitaly Kuzmichev #endif
1757612a43dSVitaly Kuzmichev }
1767612a43dSVitaly Kuzmichev 
1777612a43dSVitaly Kuzmichev #define	subset_active(dev)	(!is_cdc(dev) && !rndis_active(dev))
1787612a43dSVitaly Kuzmichev #define	cdc_active(dev)		(is_cdc(dev) && !rndis_active(dev))
17923cd1385SRemy Bohmer 
18023cd1385SRemy Bohmer #define DEFAULT_QLEN	2	/* double buffering by default */
18123cd1385SRemy Bohmer 
18223cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */
18323cd1385SRemy Bohmer #define	HS_BPS		(13 * 512 * 8 * 1000 * 8)
18423cd1385SRemy Bohmer #define	FS_BPS		(19 *  64 * 1 * 1000 * 8)
18523cd1385SRemy Bohmer 
18623cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
18723cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_HIGH
18823cd1385SRemy Bohmer 
1892721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT
1902721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT
1912721dbf1SVitaly Kuzmichev #else
1922721dbf1SVitaly Kuzmichev #define qmult 5
1932721dbf1SVitaly Kuzmichev #endif
1942721dbf1SVitaly Kuzmichev 
19523cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */
19623cd1385SRemy Bohmer #define qlen(gadget) \
19723cd1385SRemy Bohmer 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
19823cd1385SRemy Bohmer 
BITRATE(struct usb_gadget * g)19923cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
20023cd1385SRemy Bohmer {
20123cd1385SRemy Bohmer 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
20223cd1385SRemy Bohmer }
20323cd1385SRemy Bohmer 
20423cd1385SRemy Bohmer #else	/* full speed (low speed doesn't do bulk) */
20523cd1385SRemy Bohmer 
20623cd1385SRemy Bohmer #define qmult		1
20723cd1385SRemy Bohmer 
20823cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_FULL
20923cd1385SRemy Bohmer 
21023cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN
21123cd1385SRemy Bohmer 
BITRATE(struct usb_gadget * g)21223cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
21323cd1385SRemy Bohmer {
21423cd1385SRemy Bohmer 	return FS_BPS;
21523cd1385SRemy Bohmer }
21623cd1385SRemy Bohmer #endif
21723cd1385SRemy Bohmer 
21823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
21923cd1385SRemy Bohmer 
2206142e0aeSVitaly Kuzmichev /*
2216142e0aeSVitaly Kuzmichev  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
22223cd1385SRemy Bohmer  * Instead:  allocate your own, using normal USB-IF procedures.
22323cd1385SRemy Bohmer  */
22423cd1385SRemy Bohmer 
2256142e0aeSVitaly Kuzmichev /*
2266142e0aeSVitaly Kuzmichev  * Thanks to NetChip Technologies for donating this product ID.
22723cd1385SRemy Bohmer  * It's for devices with only CDC Ethernet configurations.
22823cd1385SRemy Bohmer  */
22923cd1385SRemy Bohmer #define CDC_VENDOR_NUM		0x0525	/* NetChip */
23023cd1385SRemy Bohmer #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
23123cd1385SRemy Bohmer 
2326142e0aeSVitaly Kuzmichev /*
2336142e0aeSVitaly Kuzmichev  * For hardware that can't talk CDC, we use the same vendor ID that
23423cd1385SRemy Bohmer  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
23523cd1385SRemy Bohmer  * with pxa250.  We're protocol-compatible, if the host-side drivers
23623cd1385SRemy Bohmer  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
23723cd1385SRemy Bohmer  * drivers that need to hard-wire endpoint numbers have a hook.
23823cd1385SRemy Bohmer  *
23923cd1385SRemy Bohmer  * The protocol is a minimal subset of CDC Ether, which works on any bulk
24023cd1385SRemy Bohmer  * hardware that's not deeply broken ... even on hardware that can't talk
24123cd1385SRemy Bohmer  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
24223cd1385SRemy Bohmer  * doesn't handle control-OUT).
24323cd1385SRemy Bohmer  */
2447612a43dSVitaly Kuzmichev #define	SIMPLE_VENDOR_NUM	0x049f	/* Compaq Computer Corp. */
2457612a43dSVitaly Kuzmichev #define	SIMPLE_PRODUCT_NUM	0x505a	/* Linux-USB "CDC Subset" Device */
2467612a43dSVitaly Kuzmichev 
2477612a43dSVitaly Kuzmichev /*
2487612a43dSVitaly Kuzmichev  * For hardware that can talk RNDIS and either of the above protocols,
2497612a43dSVitaly Kuzmichev  * use this ID ... the windows INF files will know it.  Unless it's
2507612a43dSVitaly Kuzmichev  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
2517612a43dSVitaly Kuzmichev  * the non-RNDIS configuration.
2527612a43dSVitaly Kuzmichev  */
2537612a43dSVitaly Kuzmichev #define RNDIS_VENDOR_NUM	0x0525	/* NetChip */
2547612a43dSVitaly Kuzmichev #define RNDIS_PRODUCT_NUM	0xa4a2	/* Ethernet/RNDIS Gadget */
25523cd1385SRemy Bohmer 
2566142e0aeSVitaly Kuzmichev /*
2576142e0aeSVitaly Kuzmichev  * Some systems will want different product identifers published in the
25823cd1385SRemy Bohmer  * device descriptor, either numbers or strings or both.  These string
25923cd1385SRemy Bohmer  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
26023cd1385SRemy Bohmer  */
26123cd1385SRemy Bohmer 
2627612a43dSVitaly Kuzmichev /*
2637612a43dSVitaly Kuzmichev  * Emulating them in eth_bind:
2647612a43dSVitaly Kuzmichev  * static ushort idVendor;
2657612a43dSVitaly Kuzmichev  * static ushort idProduct;
2667612a43dSVitaly Kuzmichev  */
2677612a43dSVitaly Kuzmichev 
26810ac57fdSMaxime Ripard #if defined(CONFIG_USB_GADGET_MANUFACTURER)
26910ac57fdSMaxime Ripard static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
27023cd1385SRemy Bohmer #else
271a187559eSBin Meng static char *iManufacturer = "U-Boot";
27223cd1385SRemy Bohmer #endif
2737612a43dSVitaly Kuzmichev 
2747612a43dSVitaly Kuzmichev /* These probably need to be configurable. */
2757612a43dSVitaly Kuzmichev static ushort bcdDevice;
27623cd1385SRemy Bohmer static char *iProduct;
27723cd1385SRemy Bohmer static char *iSerialNumber;
2787612a43dSVitaly Kuzmichev 
27923cd1385SRemy Bohmer static char dev_addr[18];
2807612a43dSVitaly Kuzmichev 
28123cd1385SRemy Bohmer static char host_addr[18];
28223cd1385SRemy Bohmer 
2837612a43dSVitaly Kuzmichev 
28423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
28523cd1385SRemy Bohmer 
2866142e0aeSVitaly Kuzmichev /*
2876142e0aeSVitaly Kuzmichev  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
28823cd1385SRemy Bohmer  * ep0 implementation:  descriptors, config management, setup().
28923cd1385SRemy Bohmer  * also optional class-specific notification interrupt transfer.
29023cd1385SRemy Bohmer  */
29123cd1385SRemy Bohmer 
29223cd1385SRemy Bohmer /*
29323cd1385SRemy Bohmer  * DESCRIPTORS ... most are static, but strings and (full) configuration
29423cd1385SRemy Bohmer  * descriptors are built on demand.  For now we do either full CDC, or
2957612a43dSVitaly Kuzmichev  * our simple subset, with RNDIS as an optional second configuration.
2967612a43dSVitaly Kuzmichev  *
2977612a43dSVitaly Kuzmichev  * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet.  But
2987612a43dSVitaly Kuzmichev  * the class descriptors match a modem (they're ignored; it's really just
2997612a43dSVitaly Kuzmichev  * Ethernet functionality), they don't need the NOP altsetting, and the
3007612a43dSVitaly Kuzmichev  * status transfer endpoint isn't optional.
30123cd1385SRemy Bohmer  */
30223cd1385SRemy Bohmer 
30323cd1385SRemy Bohmer #define STRING_MANUFACTURER		1
30423cd1385SRemy Bohmer #define STRING_PRODUCT			2
30523cd1385SRemy Bohmer #define STRING_ETHADDR			3
30623cd1385SRemy Bohmer #define STRING_DATA			4
30723cd1385SRemy Bohmer #define STRING_CONTROL			5
3087612a43dSVitaly Kuzmichev #define STRING_RNDIS_CONTROL		6
30923cd1385SRemy Bohmer #define STRING_CDC			7
31023cd1385SRemy Bohmer #define STRING_SUBSET			8
3117612a43dSVitaly Kuzmichev #define STRING_RNDIS			9
31223cd1385SRemy Bohmer #define STRING_SERIALNUMBER		10
31323cd1385SRemy Bohmer 
3147612a43dSVitaly Kuzmichev /* holds our biggest descriptor (or RNDIS response) */
31523cd1385SRemy Bohmer #define USB_BUFSIZ	256
31623cd1385SRemy Bohmer 
31723cd1385SRemy Bohmer /*
3187612a43dSVitaly Kuzmichev  * This device advertises one configuration, eth_config, unless RNDIS
3197612a43dSVitaly Kuzmichev  * is enabled (rndis_config) on hardware supporting at least two configs.
3207612a43dSVitaly Kuzmichev  *
3217612a43dSVitaly Kuzmichev  * NOTE:  Controllers like superh_udc should probably be able to use
3227612a43dSVitaly Kuzmichev  * an RNDIS-only configuration.
32323cd1385SRemy Bohmer  *
32423cd1385SRemy Bohmer  * FIXME define some higher-powered configurations to make it easier
32523cd1385SRemy Bohmer  * to recharge batteries ...
32623cd1385SRemy Bohmer  */
32723cd1385SRemy Bohmer 
32823cd1385SRemy Bohmer #define DEV_CONFIG_VALUE	1	/* cdc or subset */
3297612a43dSVitaly Kuzmichev #define DEV_RNDIS_CONFIG_VALUE	2	/* rndis; optional */
33023cd1385SRemy Bohmer 
33123cd1385SRemy Bohmer static struct usb_device_descriptor
33223cd1385SRemy Bohmer device_desc = {
33323cd1385SRemy Bohmer 	.bLength =		sizeof device_desc,
33423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE,
33523cd1385SRemy Bohmer 
33623cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
33723cd1385SRemy Bohmer 
33823cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
33923cd1385SRemy Bohmer 	.bDeviceSubClass =	0,
34023cd1385SRemy Bohmer 	.bDeviceProtocol =	0,
34123cd1385SRemy Bohmer 
34223cd1385SRemy Bohmer 	.idVendor =		__constant_cpu_to_le16(CDC_VENDOR_NUM),
34323cd1385SRemy Bohmer 	.idProduct =		__constant_cpu_to_le16(CDC_PRODUCT_NUM),
34423cd1385SRemy Bohmer 	.iManufacturer =	STRING_MANUFACTURER,
34523cd1385SRemy Bohmer 	.iProduct =		STRING_PRODUCT,
34623cd1385SRemy Bohmer 	.bNumConfigurations =	1,
34723cd1385SRemy Bohmer };
34823cd1385SRemy Bohmer 
34923cd1385SRemy Bohmer static struct usb_otg_descriptor
35023cd1385SRemy Bohmer otg_descriptor = {
35123cd1385SRemy Bohmer 	.bLength =		sizeof otg_descriptor,
35223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_OTG,
35323cd1385SRemy Bohmer 
35423cd1385SRemy Bohmer 	.bmAttributes =		USB_OTG_SRP,
35523cd1385SRemy Bohmer };
35623cd1385SRemy Bohmer 
35723cd1385SRemy Bohmer static struct usb_config_descriptor
35823cd1385SRemy Bohmer eth_config = {
35923cd1385SRemy Bohmer 	.bLength =		sizeof eth_config,
36023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CONFIG,
36123cd1385SRemy Bohmer 
36223cd1385SRemy Bohmer 	/* compute wTotalLength on the fly */
36323cd1385SRemy Bohmer 	.bNumInterfaces =	2,
36423cd1385SRemy Bohmer 	.bConfigurationValue =	DEV_CONFIG_VALUE,
36523cd1385SRemy Bohmer 	.iConfiguration =	STRING_CDC,
36623cd1385SRemy Bohmer 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
36723cd1385SRemy Bohmer 	.bMaxPower =		1,
36823cd1385SRemy Bohmer };
36923cd1385SRemy Bohmer 
3707612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
3717612a43dSVitaly Kuzmichev static struct usb_config_descriptor
3727612a43dSVitaly Kuzmichev rndis_config = {
3737612a43dSVitaly Kuzmichev 	.bLength =              sizeof rndis_config,
3747612a43dSVitaly Kuzmichev 	.bDescriptorType =      USB_DT_CONFIG,
3757612a43dSVitaly Kuzmichev 
3767612a43dSVitaly Kuzmichev 	/* compute wTotalLength on the fly */
3777612a43dSVitaly Kuzmichev 	.bNumInterfaces =       2,
3787612a43dSVitaly Kuzmichev 	.bConfigurationValue =  DEV_RNDIS_CONFIG_VALUE,
3797612a43dSVitaly Kuzmichev 	.iConfiguration =       STRING_RNDIS,
3807612a43dSVitaly Kuzmichev 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
3817612a43dSVitaly Kuzmichev 	.bMaxPower =            1,
3827612a43dSVitaly Kuzmichev };
3837612a43dSVitaly Kuzmichev #endif
3847612a43dSVitaly Kuzmichev 
38523cd1385SRemy Bohmer /*
38623cd1385SRemy Bohmer  * Compared to the simple CDC subset, the full CDC Ethernet model adds
38723cd1385SRemy Bohmer  * three class descriptors, two interface descriptors, optional status
38823cd1385SRemy Bohmer  * endpoint.  Both have a "data" interface and two bulk endpoints.
38923cd1385SRemy Bohmer  * There are also differences in how control requests are handled.
3907612a43dSVitaly Kuzmichev  *
3917612a43dSVitaly Kuzmichev  * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
3927612a43dSVitaly Kuzmichev  * CDC-ACM (modem) spec.  Unfortunately MSFT's RNDIS driver is buggy; it
3937612a43dSVitaly Kuzmichev  * may hang or oops.  Since bugfixes (or accurate specs, letting Linux
3947612a43dSVitaly Kuzmichev  * work around those bugs) are unlikely to ever come from MSFT, you may
3957612a43dSVitaly Kuzmichev  * wish to avoid using RNDIS.
3967612a43dSVitaly Kuzmichev  *
3977612a43dSVitaly Kuzmichev  * MCCI offers an alternative to RNDIS if you need to connect to Windows
3987612a43dSVitaly Kuzmichev  * but have hardware that can't support CDC Ethernet.   We add descriptors
3997612a43dSVitaly Kuzmichev  * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
4007612a43dSVitaly Kuzmichev  * "SAFE".  That borrows from both CDC Ethernet and CDC MDLM.  You can
4017612a43dSVitaly Kuzmichev  * get those drivers from MCCI, or bundled with various products.
40223cd1385SRemy Bohmer  */
40323cd1385SRemy Bohmer 
4042bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
40523cd1385SRemy Bohmer static struct usb_interface_descriptor
40623cd1385SRemy Bohmer control_intf = {
40723cd1385SRemy Bohmer 	.bLength =		sizeof control_intf,
40823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
40923cd1385SRemy Bohmer 
41023cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
41123cd1385SRemy Bohmer 	/* status endpoint is optional; this may be patched later */
41223cd1385SRemy Bohmer 	.bNumEndpoints =	1,
41323cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_COMM,
41423cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
41523cd1385SRemy Bohmer 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
41623cd1385SRemy Bohmer 	.iInterface =		STRING_CONTROL,
41723cd1385SRemy Bohmer };
41823cd1385SRemy Bohmer #endif
41923cd1385SRemy Bohmer 
4207612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
4217612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
4227612a43dSVitaly Kuzmichev rndis_control_intf = {
4237612a43dSVitaly Kuzmichev 	.bLength =              sizeof rndis_control_intf,
4247612a43dSVitaly Kuzmichev 	.bDescriptorType =      USB_DT_INTERFACE,
4257612a43dSVitaly Kuzmichev 
4267612a43dSVitaly Kuzmichev 	.bInterfaceNumber =     0,
4277612a43dSVitaly Kuzmichev 	.bNumEndpoints =        1,
4287612a43dSVitaly Kuzmichev 	.bInterfaceClass =      USB_CLASS_COMM,
4297612a43dSVitaly Kuzmichev 	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
4307612a43dSVitaly Kuzmichev 	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
4317612a43dSVitaly Kuzmichev 	.iInterface =           STRING_RNDIS_CONTROL,
4327612a43dSVitaly Kuzmichev };
4337612a43dSVitaly Kuzmichev #endif
4347612a43dSVitaly Kuzmichev 
43523cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = {
43623cd1385SRemy Bohmer 	.bLength =		sizeof header_desc,
43723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
43823cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
43923cd1385SRemy Bohmer 
44023cd1385SRemy Bohmer 	.bcdCDC =		__constant_cpu_to_le16(0x0110),
44123cd1385SRemy Bohmer };
44223cd1385SRemy Bohmer 
4432bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
44423cd1385SRemy Bohmer 
44523cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = {
44623cd1385SRemy Bohmer 	.bLength =		sizeof union_desc,
44723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
44823cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
44923cd1385SRemy Bohmer 
45023cd1385SRemy Bohmer 	.bMasterInterface0 =	0,	/* index of control interface */
45123cd1385SRemy Bohmer 	.bSlaveInterface0 =	1,	/* index of DATA interface */
45223cd1385SRemy Bohmer };
45323cd1385SRemy Bohmer 
4547612a43dSVitaly Kuzmichev #endif	/* CDC || RNDIS */
4557612a43dSVitaly Kuzmichev 
4567612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
4577612a43dSVitaly Kuzmichev 
4587612a43dSVitaly Kuzmichev static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
4597612a43dSVitaly Kuzmichev 	.bLength =		sizeof call_mgmt_descriptor,
4607612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_CS_INTERFACE,
4617612a43dSVitaly Kuzmichev 	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
4627612a43dSVitaly Kuzmichev 
4637612a43dSVitaly Kuzmichev 	.bmCapabilities =	0x00,
4647612a43dSVitaly Kuzmichev 	.bDataInterface =	0x01,
4657612a43dSVitaly Kuzmichev };
4667612a43dSVitaly Kuzmichev 
4677612a43dSVitaly Kuzmichev static const struct usb_cdc_acm_descriptor acm_descriptor = {
4687612a43dSVitaly Kuzmichev 	.bLength =		sizeof acm_descriptor,
4697612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_CS_INTERFACE,
4707612a43dSVitaly Kuzmichev 	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
4717612a43dSVitaly Kuzmichev 
4727612a43dSVitaly Kuzmichev 	.bmCapabilities =	0x00,
4737612a43dSVitaly Kuzmichev };
4747612a43dSVitaly Kuzmichev 
4757612a43dSVitaly Kuzmichev #endif
47623cd1385SRemy Bohmer 
4772bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_CDC
47823cd1385SRemy Bohmer 
4796142e0aeSVitaly Kuzmichev /*
4806142e0aeSVitaly Kuzmichev  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
48123cd1385SRemy Bohmer  * ways:  data endpoints live in the control interface, there's no data
48223cd1385SRemy Bohmer  * interface, and it's not used to talk to a cell phone radio.
48323cd1385SRemy Bohmer  */
48423cd1385SRemy Bohmer 
48523cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = {
48623cd1385SRemy Bohmer 	.bLength =		sizeof mdlm_desc,
48723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
48823cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_MDLM_TYPE,
48923cd1385SRemy Bohmer 
49023cd1385SRemy Bohmer 	.bcdVersion =		__constant_cpu_to_le16(0x0100),
49123cd1385SRemy Bohmer 	.bGUID = {
49223cd1385SRemy Bohmer 		0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
49323cd1385SRemy Bohmer 		0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
49423cd1385SRemy Bohmer 	},
49523cd1385SRemy Bohmer };
49623cd1385SRemy Bohmer 
4976142e0aeSVitaly Kuzmichev /*
4986142e0aeSVitaly Kuzmichev  * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
49923cd1385SRemy Bohmer  * can't really use its struct.  All we do here is say that we're using
50023cd1385SRemy Bohmer  * the submode of "SAFE" which directly matches the CDC Subset.
50123cd1385SRemy Bohmer  */
50265c389d2SLokesh Vutla #ifdef CONFIG_USB_ETH_SUBSET
50323cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = {
50423cd1385SRemy Bohmer 	6,
50523cd1385SRemy Bohmer 	USB_DT_CS_INTERFACE,
50623cd1385SRemy Bohmer 	USB_CDC_MDLM_DETAIL_TYPE,
50723cd1385SRemy Bohmer 
50823cd1385SRemy Bohmer 	0,	/* "SAFE" */
50923cd1385SRemy Bohmer 	0,	/* network control capabilities (none) */
51023cd1385SRemy Bohmer 	0,	/* network data capabilities ("raw" encapsulation) */
51123cd1385SRemy Bohmer };
51265c389d2SLokesh Vutla #endif
51323cd1385SRemy Bohmer 
51423cd1385SRemy Bohmer #endif
51523cd1385SRemy Bohmer 
51623cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = {
51723cd1385SRemy Bohmer 	.bLength =		sizeof(ether_desc),
51823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
51923cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
52023cd1385SRemy Bohmer 
52123cd1385SRemy Bohmer 	/* this descriptor actually adds value, surprise! */
52223cd1385SRemy Bohmer 	.iMACAddress =		STRING_ETHADDR,
52323cd1385SRemy Bohmer 	.bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
524dda52510SBin Meng 	.wMaxSegmentSize =	__constant_cpu_to_le16(PKTSIZE_ALIGN),
52523cd1385SRemy Bohmer 	.wNumberMCFilters =	__constant_cpu_to_le16(0),
52623cd1385SRemy Bohmer 	.bNumberPowerFilters =	0,
52723cd1385SRemy Bohmer };
52823cd1385SRemy Bohmer 
5292bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
53023cd1385SRemy Bohmer 
5316142e0aeSVitaly Kuzmichev /*
5326142e0aeSVitaly Kuzmichev  * include the status endpoint if we can, even where it's optional.
53323cd1385SRemy Bohmer  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
53423cd1385SRemy Bohmer  * packet, to simplify cancellation; and a big transfer interval, to
53523cd1385SRemy Bohmer  * waste less bandwidth.
53623cd1385SRemy Bohmer  *
53723cd1385SRemy Bohmer  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
53823cd1385SRemy Bohmer  * if they ignore the connect/disconnect notifications that real aether
53923cd1385SRemy Bohmer  * can provide.  more advanced cdc configurations might want to support
54023cd1385SRemy Bohmer  * encapsulated commands (vendor-specific, using control-OUT).
5417612a43dSVitaly Kuzmichev  *
5427612a43dSVitaly Kuzmichev  * RNDIS requires the status endpoint, since it uses that encapsulation
5437612a43dSVitaly Kuzmichev  * mechanism for its funky RPC scheme.
54423cd1385SRemy Bohmer  */
54523cd1385SRemy Bohmer 
54623cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
54723cd1385SRemy Bohmer #define STATUS_BYTECOUNT		16	/* 8 byte header + data */
54823cd1385SRemy Bohmer 
54923cd1385SRemy Bohmer static struct usb_endpoint_descriptor
55023cd1385SRemy Bohmer fs_status_desc = {
55123cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
55223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
55323cd1385SRemy Bohmer 
55423cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
55523cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
55623cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
55723cd1385SRemy Bohmer 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
55823cd1385SRemy Bohmer };
55923cd1385SRemy Bohmer #endif
56023cd1385SRemy Bohmer 
5612bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
56223cd1385SRemy Bohmer 
56323cd1385SRemy Bohmer /* the default data interface has no endpoints ... */
56423cd1385SRemy Bohmer 
56523cd1385SRemy Bohmer static const struct usb_interface_descriptor
56623cd1385SRemy Bohmer data_nop_intf = {
56723cd1385SRemy Bohmer 	.bLength =		sizeof data_nop_intf,
56823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
56923cd1385SRemy Bohmer 
57023cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
57123cd1385SRemy Bohmer 	.bAlternateSetting =	0,
57223cd1385SRemy Bohmer 	.bNumEndpoints =	0,
57323cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
57423cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
57523cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
57623cd1385SRemy Bohmer };
57723cd1385SRemy Bohmer 
57823cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */
57923cd1385SRemy Bohmer 
58023cd1385SRemy Bohmer static const struct usb_interface_descriptor
58123cd1385SRemy Bohmer data_intf = {
58223cd1385SRemy Bohmer 	.bLength =		sizeof data_intf,
58323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
58423cd1385SRemy Bohmer 
58523cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
58623cd1385SRemy Bohmer 	.bAlternateSetting =	1,
58723cd1385SRemy Bohmer 	.bNumEndpoints =	2,
58823cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
58923cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
59023cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
59123cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
59223cd1385SRemy Bohmer };
59323cd1385SRemy Bohmer 
59423cd1385SRemy Bohmer #endif
59523cd1385SRemy Bohmer 
5967612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
5977612a43dSVitaly Kuzmichev 
5987612a43dSVitaly Kuzmichev /* RNDIS doesn't activate by changing to the "real" altsetting */
5997612a43dSVitaly Kuzmichev 
6007612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
6017612a43dSVitaly Kuzmichev rndis_data_intf = {
6027612a43dSVitaly Kuzmichev 	.bLength =		sizeof rndis_data_intf,
6037612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_INTERFACE,
6047612a43dSVitaly Kuzmichev 
6057612a43dSVitaly Kuzmichev 	.bInterfaceNumber =	1,
6067612a43dSVitaly Kuzmichev 	.bAlternateSetting =	0,
6077612a43dSVitaly Kuzmichev 	.bNumEndpoints =	2,
6087612a43dSVitaly Kuzmichev 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
6097612a43dSVitaly Kuzmichev 	.bInterfaceSubClass =	0,
6107612a43dSVitaly Kuzmichev 	.bInterfaceProtocol =	0,
6117612a43dSVitaly Kuzmichev 	.iInterface =		STRING_DATA,
6127612a43dSVitaly Kuzmichev };
6137612a43dSVitaly Kuzmichev 
6147612a43dSVitaly Kuzmichev #endif
6157612a43dSVitaly Kuzmichev 
6162bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
61723cd1385SRemy Bohmer 
61823cd1385SRemy Bohmer /*
61923cd1385SRemy Bohmer  * "Simple" CDC-subset option is a simple vendor-neutral model that most
62023cd1385SRemy Bohmer  * full speed controllers can handle:  one interface, two bulk endpoints.
62123cd1385SRemy Bohmer  *
62223cd1385SRemy Bohmer  * To assist host side drivers, we fancy it up a bit, and add descriptors
62323cd1385SRemy Bohmer  * so some host side drivers will understand it as a "SAFE" variant.
62423cd1385SRemy Bohmer  */
62523cd1385SRemy Bohmer 
62623cd1385SRemy Bohmer static const struct usb_interface_descriptor
62723cd1385SRemy Bohmer subset_data_intf = {
62823cd1385SRemy Bohmer 	.bLength =		sizeof subset_data_intf,
62923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
63023cd1385SRemy Bohmer 
63123cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
63223cd1385SRemy Bohmer 	.bAlternateSetting =	0,
63323cd1385SRemy Bohmer 	.bNumEndpoints =	2,
63423cd1385SRemy Bohmer 	.bInterfaceClass =      USB_CLASS_COMM,
63523cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_MDLM,
63623cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
63723cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
63823cd1385SRemy Bohmer };
63923cd1385SRemy Bohmer 
64023cd1385SRemy Bohmer #endif	/* SUBSET */
64123cd1385SRemy Bohmer 
64223cd1385SRemy Bohmer static struct usb_endpoint_descriptor
64323cd1385SRemy Bohmer fs_source_desc = {
64423cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
64523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
64623cd1385SRemy Bohmer 
64723cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
64823cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
64943880ce5STroy Kisky 	.wMaxPacketSize =	__constant_cpu_to_le16(64),
65023cd1385SRemy Bohmer };
65123cd1385SRemy Bohmer 
65223cd1385SRemy Bohmer static struct usb_endpoint_descriptor
65323cd1385SRemy Bohmer fs_sink_desc = {
65423cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
65523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
65623cd1385SRemy Bohmer 
65723cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_OUT,
65823cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
65943880ce5STroy Kisky 	.wMaxPacketSize =	__constant_cpu_to_le16(64),
66023cd1385SRemy Bohmer };
66123cd1385SRemy Bohmer 
66223cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = {
66323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
6642bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
66523cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
66623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
66723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
66823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
66923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
67023cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
67123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_status_desc,
67223cd1385SRemy Bohmer 	/* data interface, with altsetting */
67323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
67423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
67523cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_source_desc,
67623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_sink_desc,
67723cd1385SRemy Bohmer 	NULL,
6782bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
67923cd1385SRemy Bohmer };
68023cd1385SRemy Bohmer 
fs_subset_descriptors(void)68123cd1385SRemy Bohmer static inline void fs_subset_descriptors(void)
68223cd1385SRemy Bohmer {
6832bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
68423cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
68523cd1385SRemy Bohmer 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
68623cd1385SRemy Bohmer 	fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
68723cd1385SRemy Bohmer 	fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
68823cd1385SRemy Bohmer 	fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
68923cd1385SRemy Bohmer 	fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
69023cd1385SRemy Bohmer 	fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
69123cd1385SRemy Bohmer 	fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
69223cd1385SRemy Bohmer 	fs_eth_function[8] = NULL;
69323cd1385SRemy Bohmer #else
69423cd1385SRemy Bohmer 	fs_eth_function[1] = NULL;
69523cd1385SRemy Bohmer #endif
69623cd1385SRemy Bohmer }
69723cd1385SRemy Bohmer 
6987612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
6997612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *fs_rndis_function[] = {
7007612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &otg_descriptor,
7017612a43dSVitaly Kuzmichev 	/* control interface matches ACM, not Ethernet */
7027612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_control_intf,
7037612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &header_desc,
7047612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
7057612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &acm_descriptor,
7067612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &union_desc,
7077612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_status_desc,
7087612a43dSVitaly Kuzmichev 	/* data interface has no altsetting */
7097612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_data_intf,
7107612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_source_desc,
7117612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_sink_desc,
7127612a43dSVitaly Kuzmichev 	NULL,
7137612a43dSVitaly Kuzmichev };
7147612a43dSVitaly Kuzmichev #endif
7157612a43dSVitaly Kuzmichev 
71623cd1385SRemy Bohmer /*
71723cd1385SRemy Bohmer  * usb 2.0 devices need to expose both high speed and full speed
71823cd1385SRemy Bohmer  * descriptors, unless they only run at full speed.
71923cd1385SRemy Bohmer  */
72023cd1385SRemy Bohmer 
7212bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
72223cd1385SRemy Bohmer static struct usb_endpoint_descriptor
72323cd1385SRemy Bohmer hs_status_desc = {
72423cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
72523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
72623cd1385SRemy Bohmer 
72723cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
72823cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
72923cd1385SRemy Bohmer 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
73023cd1385SRemy Bohmer };
7312bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
73223cd1385SRemy Bohmer 
73323cd1385SRemy Bohmer static struct usb_endpoint_descriptor
73423cd1385SRemy Bohmer hs_source_desc = {
73523cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
73623cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
73723cd1385SRemy Bohmer 
73823cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
73923cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
74023cd1385SRemy Bohmer };
74123cd1385SRemy Bohmer 
74223cd1385SRemy Bohmer static struct usb_endpoint_descriptor
74323cd1385SRemy Bohmer hs_sink_desc = {
74423cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
74523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
74623cd1385SRemy Bohmer 
74723cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
74823cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
74923cd1385SRemy Bohmer };
75023cd1385SRemy Bohmer 
75123cd1385SRemy Bohmer static struct usb_qualifier_descriptor
75223cd1385SRemy Bohmer dev_qualifier = {
75323cd1385SRemy Bohmer 	.bLength =		sizeof dev_qualifier,
75423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
75523cd1385SRemy Bohmer 
75623cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
75723cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
75823cd1385SRemy Bohmer 
75923cd1385SRemy Bohmer 	.bNumConfigurations =	1,
76023cd1385SRemy Bohmer };
76123cd1385SRemy Bohmer 
76223cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = {
76323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
7642bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
76523cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
76623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
76723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
76823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
76923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
77023cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
77123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_status_desc,
77223cd1385SRemy Bohmer 	/* data interface, with altsetting */
77323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
77423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
77523cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_source_desc,
77623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_sink_desc,
77723cd1385SRemy Bohmer 	NULL,
7782bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
77923cd1385SRemy Bohmer };
78023cd1385SRemy Bohmer 
hs_subset_descriptors(void)78123cd1385SRemy Bohmer static inline void hs_subset_descriptors(void)
78223cd1385SRemy Bohmer {
7832bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
78423cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
78523cd1385SRemy Bohmer 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
78623cd1385SRemy Bohmer 	hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
78723cd1385SRemy Bohmer 	hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
78823cd1385SRemy Bohmer 	hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
78923cd1385SRemy Bohmer 	hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
79023cd1385SRemy Bohmer 	hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
79123cd1385SRemy Bohmer 	hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
79223cd1385SRemy Bohmer 	hs_eth_function[8] = NULL;
79323cd1385SRemy Bohmer #else
79423cd1385SRemy Bohmer 	hs_eth_function[1] = NULL;
79523cd1385SRemy Bohmer #endif
79623cd1385SRemy Bohmer }
79723cd1385SRemy Bohmer 
7987612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
7997612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *hs_rndis_function[] = {
8007612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &otg_descriptor,
8017612a43dSVitaly Kuzmichev 	/* control interface matches ACM, not Ethernet */
8027612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_control_intf,
8037612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &header_desc,
8047612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
8057612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &acm_descriptor,
8067612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &union_desc,
8077612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_status_desc,
8087612a43dSVitaly Kuzmichev 	/* data interface has no altsetting */
8097612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_data_intf,
8107612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_source_desc,
8117612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_sink_desc,
8127612a43dSVitaly Kuzmichev 	NULL,
8137612a43dSVitaly Kuzmichev };
8147612a43dSVitaly Kuzmichev #endif
8157612a43dSVitaly Kuzmichev 
8167612a43dSVitaly Kuzmichev 
81723cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */
81823cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor *
ep_desc(struct usb_gadget * g,struct usb_endpoint_descriptor * hs,struct usb_endpoint_descriptor * fs)81923cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
82023cd1385SRemy Bohmer 		struct usb_endpoint_descriptor *fs)
82123cd1385SRemy Bohmer {
82223cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
82323cd1385SRemy Bohmer 		return hs;
82423cd1385SRemy Bohmer 	return fs;
82523cd1385SRemy Bohmer }
82623cd1385SRemy Bohmer 
82723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
82823cd1385SRemy Bohmer 
82923cd1385SRemy Bohmer /* descriptors that are built on-demand */
83023cd1385SRemy Bohmer 
83123cd1385SRemy Bohmer static char manufacturer[50];
83223cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC;
83323cd1385SRemy Bohmer static char serial_number[20];
83423cd1385SRemy Bohmer 
83523cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */
83623cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1];
83723cd1385SRemy Bohmer 
83823cd1385SRemy Bohmer /* static strings, in UTF-8 */
83923cd1385SRemy Bohmer static struct usb_string		strings[] = {
84023cd1385SRemy Bohmer 	{ STRING_MANUFACTURER,	manufacturer, },
84123cd1385SRemy Bohmer 	{ STRING_PRODUCT,	product_desc, },
84223cd1385SRemy Bohmer 	{ STRING_SERIALNUMBER,	serial_number, },
84323cd1385SRemy Bohmer 	{ STRING_DATA,		"Ethernet Data", },
84423cd1385SRemy Bohmer 	{ STRING_ETHADDR,	ethaddr, },
8452bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
84623cd1385SRemy Bohmer 	{ STRING_CDC,		"CDC Ethernet", },
84723cd1385SRemy Bohmer 	{ STRING_CONTROL,	"CDC Communications Control", },
84823cd1385SRemy Bohmer #endif
8492bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_SUBSET
85023cd1385SRemy Bohmer 	{ STRING_SUBSET,	"CDC Ethernet Subset", },
85123cd1385SRemy Bohmer #endif
8527612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
8537612a43dSVitaly Kuzmichev 	{ STRING_RNDIS,		"RNDIS", },
8547612a43dSVitaly Kuzmichev 	{ STRING_RNDIS_CONTROL,	"RNDIS Communications Control", },
8557612a43dSVitaly Kuzmichev #endif
85623cd1385SRemy Bohmer 	{  }		/* end of list */
85723cd1385SRemy Bohmer };
85823cd1385SRemy Bohmer 
85923cd1385SRemy Bohmer static struct usb_gadget_strings	stringtab = {
86023cd1385SRemy Bohmer 	.language	= 0x0409,	/* en-us */
86123cd1385SRemy Bohmer 	.strings	= strings,
86223cd1385SRemy Bohmer };
86323cd1385SRemy Bohmer 
86423cd1385SRemy Bohmer /*============================================================================*/
8655290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
8665290759cSJoel Fernandes 
8672bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
8685290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
869563aed25SLukasz Dalek #endif
87023cd1385SRemy Bohmer 
87123cd1385SRemy Bohmer /*============================================================================*/
87223cd1385SRemy Bohmer 
87323cd1385SRemy Bohmer /*
87423cd1385SRemy Bohmer  * one config, two interfaces:  control, data.
87523cd1385SRemy Bohmer  * complications: class descriptors, and an altsetting.
87623cd1385SRemy Bohmer  */
87723cd1385SRemy Bohmer static int
config_buf(struct usb_gadget * g,u8 * buf,u8 type,unsigned index,int is_otg)87823cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
87923cd1385SRemy Bohmer {
88023cd1385SRemy Bohmer 	int					len;
88123cd1385SRemy Bohmer 	const struct usb_config_descriptor	*config;
88223cd1385SRemy Bohmer 	const struct usb_descriptor_header	**function;
88323cd1385SRemy Bohmer 	int					hs = 0;
88423cd1385SRemy Bohmer 
88523cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g)) {
88623cd1385SRemy Bohmer 		hs = (g->speed == USB_SPEED_HIGH);
88723cd1385SRemy Bohmer 		if (type == USB_DT_OTHER_SPEED_CONFIG)
88823cd1385SRemy Bohmer 			hs = !hs;
88923cd1385SRemy Bohmer 	}
89023cd1385SRemy Bohmer #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function)
89123cd1385SRemy Bohmer 
89223cd1385SRemy Bohmer 	if (index >= device_desc.bNumConfigurations)
89323cd1385SRemy Bohmer 		return -EINVAL;
89423cd1385SRemy Bohmer 
8957612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
8967612a43dSVitaly Kuzmichev 	/*
8977612a43dSVitaly Kuzmichev 	 * list the RNDIS config first, to make Microsoft's drivers
8987612a43dSVitaly Kuzmichev 	 * happy. DOCSIS 1.0 needs this too.
8997612a43dSVitaly Kuzmichev 	 */
9007612a43dSVitaly Kuzmichev 	if (device_desc.bNumConfigurations == 2 && index == 0) {
9017612a43dSVitaly Kuzmichev 		config = &rndis_config;
9027612a43dSVitaly Kuzmichev 		function = which_fn(rndis);
9037612a43dSVitaly Kuzmichev 	} else
9047612a43dSVitaly Kuzmichev #endif
9057612a43dSVitaly Kuzmichev 	{
90623cd1385SRemy Bohmer 		config = &eth_config;
90723cd1385SRemy Bohmer 		function = which_fn(eth);
9087612a43dSVitaly Kuzmichev 	}
90923cd1385SRemy Bohmer 
91023cd1385SRemy Bohmer 	/* for now, don't advertise srp-only devices */
91123cd1385SRemy Bohmer 	if (!is_otg)
91223cd1385SRemy Bohmer 		function++;
91323cd1385SRemy Bohmer 
91423cd1385SRemy Bohmer 	len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
91523cd1385SRemy Bohmer 	if (len < 0)
91623cd1385SRemy Bohmer 		return len;
91723cd1385SRemy Bohmer 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
91823cd1385SRemy Bohmer 	return len;
91923cd1385SRemy Bohmer }
92023cd1385SRemy Bohmer 
92123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
92223cd1385SRemy Bohmer 
9237612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
92423cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
92523cd1385SRemy Bohmer 
92623cd1385SRemy Bohmer static int
set_ether_config(struct eth_dev * dev,gfp_t gfp_flags)92723cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
92823cd1385SRemy Bohmer {
92923cd1385SRemy Bohmer 	int					result = 0;
93023cd1385SRemy Bohmer 	struct usb_gadget			*gadget = dev->gadget;
93123cd1385SRemy Bohmer 
9322bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
9337612a43dSVitaly Kuzmichev 	/* status endpoint used for RNDIS and (optionally) CDC */
93423cd1385SRemy Bohmer 	if (!subset_active(dev) && dev->status_ep) {
93523cd1385SRemy Bohmer 		dev->status = ep_desc(gadget, &hs_status_desc,
93623cd1385SRemy Bohmer 						&fs_status_desc);
93723cd1385SRemy Bohmer 		dev->status_ep->driver_data = dev;
93823cd1385SRemy Bohmer 
93923cd1385SRemy Bohmer 		result = usb_ep_enable(dev->status_ep, dev->status);
94023cd1385SRemy Bohmer 		if (result != 0) {
9417de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
94223cd1385SRemy Bohmer 				dev->status_ep->name, result);
94323cd1385SRemy Bohmer 			goto done;
94423cd1385SRemy Bohmer 		}
94523cd1385SRemy Bohmer 	}
94623cd1385SRemy Bohmer #endif
94723cd1385SRemy Bohmer 
94823cd1385SRemy Bohmer 	dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
94923cd1385SRemy Bohmer 	dev->in_ep->driver_data = dev;
95023cd1385SRemy Bohmer 
95123cd1385SRemy Bohmer 	dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
95223cd1385SRemy Bohmer 	dev->out_ep->driver_data = dev;
95323cd1385SRemy Bohmer 
9546142e0aeSVitaly Kuzmichev 	/*
9556142e0aeSVitaly Kuzmichev 	 * With CDC,  the host isn't allowed to use these two data
95623cd1385SRemy Bohmer 	 * endpoints in the default altsetting for the interface.
95723cd1385SRemy Bohmer 	 * so we don't activate them yet.  Reset from SET_INTERFACE.
9587612a43dSVitaly Kuzmichev 	 *
9597612a43dSVitaly Kuzmichev 	 * Strictly speaking RNDIS should work the same: activation is
9607612a43dSVitaly Kuzmichev 	 * a side effect of setting a packet filter.  Deactivation is
9617612a43dSVitaly Kuzmichev 	 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
96223cd1385SRemy Bohmer 	 */
96323cd1385SRemy Bohmer 	if (!cdc_active(dev)) {
96423cd1385SRemy Bohmer 		result = usb_ep_enable(dev->in_ep, dev->in);
96523cd1385SRemy Bohmer 		if (result != 0) {
9667de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
96723cd1385SRemy Bohmer 				dev->in_ep->name, result);
96823cd1385SRemy Bohmer 			goto done;
96923cd1385SRemy Bohmer 		}
97023cd1385SRemy Bohmer 
97123cd1385SRemy Bohmer 		result = usb_ep_enable(dev->out_ep, dev->out);
97223cd1385SRemy Bohmer 		if (result != 0) {
9737de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
97423cd1385SRemy Bohmer 				dev->out_ep->name, result);
97523cd1385SRemy Bohmer 			goto done;
97623cd1385SRemy Bohmer 		}
97723cd1385SRemy Bohmer 	}
97823cd1385SRemy Bohmer 
97923cd1385SRemy Bohmer done:
98023cd1385SRemy Bohmer 	if (result == 0)
98123cd1385SRemy Bohmer 		result = alloc_requests(dev, qlen(gadget), gfp_flags);
98223cd1385SRemy Bohmer 
98323cd1385SRemy Bohmer 	/* on error, disable any endpoints  */
98423cd1385SRemy Bohmer 	if (result < 0) {
985d5292c16SVitaly Kuzmichev 		if (!subset_active(dev) && dev->status_ep)
98623cd1385SRemy Bohmer 			(void) usb_ep_disable(dev->status_ep);
98723cd1385SRemy Bohmer 		dev->status = NULL;
98823cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->in_ep);
98923cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->out_ep);
99023cd1385SRemy Bohmer 		dev->in = NULL;
99123cd1385SRemy Bohmer 		dev->out = NULL;
9927612a43dSVitaly Kuzmichev 	} else if (!cdc_active(dev)) {
9937612a43dSVitaly Kuzmichev 		/*
9947612a43dSVitaly Kuzmichev 		 * activate non-CDC configs right away
9957612a43dSVitaly Kuzmichev 		 * this isn't strictly according to the RNDIS spec
9967612a43dSVitaly Kuzmichev 		 */
9977612a43dSVitaly Kuzmichev 		eth_start(dev, GFP_ATOMIC);
99823cd1385SRemy Bohmer 	}
99923cd1385SRemy Bohmer 
100023cd1385SRemy Bohmer 	/* caller is responsible for cleanup on error */
100123cd1385SRemy Bohmer 	return result;
100223cd1385SRemy Bohmer }
100323cd1385SRemy Bohmer 
eth_reset_config(struct eth_dev * dev)100423cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev)
100523cd1385SRemy Bohmer {
100623cd1385SRemy Bohmer 	if (dev->config == 0)
100723cd1385SRemy Bohmer 		return;
100823cd1385SRemy Bohmer 
10097de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
10107de73185SVitaly Kuzmichev 
10117612a43dSVitaly Kuzmichev 	rndis_uninit(dev->rndis_config);
10127612a43dSVitaly Kuzmichev 
10136142e0aeSVitaly Kuzmichev 	/*
10146142e0aeSVitaly Kuzmichev 	 * disable endpoints, forcing (synchronous) completion of
101523cd1385SRemy Bohmer 	 * pending i/o.  then free the requests.
101623cd1385SRemy Bohmer 	 */
101723cd1385SRemy Bohmer 
101823cd1385SRemy Bohmer 	if (dev->in) {
101923cd1385SRemy Bohmer 		usb_ep_disable(dev->in_ep);
102023cd1385SRemy Bohmer 		if (dev->tx_req) {
102123cd1385SRemy Bohmer 			usb_ep_free_request(dev->in_ep, dev->tx_req);
102223cd1385SRemy Bohmer 			dev->tx_req = NULL;
102323cd1385SRemy Bohmer 		}
102423cd1385SRemy Bohmer 	}
102523cd1385SRemy Bohmer 	if (dev->out) {
102623cd1385SRemy Bohmer 		usb_ep_disable(dev->out_ep);
102723cd1385SRemy Bohmer 		if (dev->rx_req) {
10280129e327SVitaly Kuzmichev 			usb_ep_free_request(dev->out_ep, dev->rx_req);
102923cd1385SRemy Bohmer 			dev->rx_req = NULL;
103023cd1385SRemy Bohmer 		}
103123cd1385SRemy Bohmer 	}
10326142e0aeSVitaly Kuzmichev 	if (dev->status)
103323cd1385SRemy Bohmer 		usb_ep_disable(dev->status_ep);
10346142e0aeSVitaly Kuzmichev 
10357612a43dSVitaly Kuzmichev 	dev->rndis = 0;
103623cd1385SRemy Bohmer 	dev->cdc_filter = 0;
103723cd1385SRemy Bohmer 	dev->config = 0;
103823cd1385SRemy Bohmer }
103923cd1385SRemy Bohmer 
10406142e0aeSVitaly Kuzmichev /*
10416142e0aeSVitaly Kuzmichev  * change our operational config.  must agree with the code
104223cd1385SRemy Bohmer  * that returns config descriptors, and altsetting code.
104323cd1385SRemy Bohmer  */
eth_set_config(struct eth_dev * dev,unsigned number,gfp_t gfp_flags)10446142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number,
10456142e0aeSVitaly Kuzmichev 				gfp_t gfp_flags)
104623cd1385SRemy Bohmer {
104723cd1385SRemy Bohmer 	int			result = 0;
104823cd1385SRemy Bohmer 	struct usb_gadget	*gadget = dev->gadget;
104923cd1385SRemy Bohmer 
105023cd1385SRemy Bohmer 	if (gadget_is_sa1100(gadget)
105123cd1385SRemy Bohmer 			&& dev->config
105223cd1385SRemy Bohmer 			&& dev->tx_qlen != 0) {
105323cd1385SRemy Bohmer 		/* tx fifo is full, but we can't clear it...*/
10549b643e31SMasahiro Yamada 		pr_err("can't change configurations");
105523cd1385SRemy Bohmer 		return -ESPIPE;
105623cd1385SRemy Bohmer 	}
105723cd1385SRemy Bohmer 	eth_reset_config(dev);
105823cd1385SRemy Bohmer 
105923cd1385SRemy Bohmer 	switch (number) {
106023cd1385SRemy Bohmer 	case DEV_CONFIG_VALUE:
106123cd1385SRemy Bohmer 		result = set_ether_config(dev, gfp_flags);
106223cd1385SRemy Bohmer 		break;
10637612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
10647612a43dSVitaly Kuzmichev 	case DEV_RNDIS_CONFIG_VALUE:
10657612a43dSVitaly Kuzmichev 		dev->rndis = 1;
10667612a43dSVitaly Kuzmichev 		result = set_ether_config(dev, gfp_flags);
10677612a43dSVitaly Kuzmichev 		break;
10687612a43dSVitaly Kuzmichev #endif
106923cd1385SRemy Bohmer 	default:
107023cd1385SRemy Bohmer 		result = -EINVAL;
107123cd1385SRemy Bohmer 		/* FALL THROUGH */
107223cd1385SRemy Bohmer 	case 0:
107323cd1385SRemy Bohmer 		break;
107423cd1385SRemy Bohmer 	}
107523cd1385SRemy Bohmer 
107623cd1385SRemy Bohmer 	if (result) {
107723cd1385SRemy Bohmer 		if (number)
107823cd1385SRemy Bohmer 			eth_reset_config(dev);
107923cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget,
108023cd1385SRemy Bohmer 				gadget_is_otg(dev->gadget) ? 8 : 100);
108123cd1385SRemy Bohmer 	} else {
108223cd1385SRemy Bohmer 		char *speed;
108323cd1385SRemy Bohmer 		unsigned power;
108423cd1385SRemy Bohmer 
108523cd1385SRemy Bohmer 		power = 2 * eth_config.bMaxPower;
108623cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget, power);
108723cd1385SRemy Bohmer 
108823cd1385SRemy Bohmer 		switch (gadget->speed) {
10896142e0aeSVitaly Kuzmichev 		case USB_SPEED_FULL:
10906142e0aeSVitaly Kuzmichev 			speed = "full"; break;
109123cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
10926142e0aeSVitaly Kuzmichev 		case USB_SPEED_HIGH:
10936142e0aeSVitaly Kuzmichev 			speed = "high"; break;
109423cd1385SRemy Bohmer #endif
10956142e0aeSVitaly Kuzmichev 		default:
10966142e0aeSVitaly Kuzmichev 			speed = "?"; break;
109723cd1385SRemy Bohmer 		}
109823cd1385SRemy Bohmer 
109923cd1385SRemy Bohmer 		dev->config = number;
11007de73185SVitaly Kuzmichev 		printf("%s speed config #%d: %d mA, %s, using %s\n",
110123cd1385SRemy Bohmer 				speed, number, power, driver_desc,
11027612a43dSVitaly Kuzmichev 				rndis_active(dev)
11037612a43dSVitaly Kuzmichev 					? "RNDIS"
11047612a43dSVitaly Kuzmichev 					: (cdc_active(dev)
11057612a43dSVitaly Kuzmichev 						? "CDC Ethernet"
110623cd1385SRemy Bohmer 						: "CDC Ethernet Subset"));
110723cd1385SRemy Bohmer 	}
110823cd1385SRemy Bohmer 	return result;
110923cd1385SRemy Bohmer }
111023cd1385SRemy Bohmer 
111123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
111223cd1385SRemy Bohmer 
11132bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
111423cd1385SRemy Bohmer 
11156142e0aeSVitaly Kuzmichev /*
11166142e0aeSVitaly Kuzmichev  * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
111723cd1385SRemy Bohmer  * only to notify the host about link status changes (which we support) or
11187612a43dSVitaly Kuzmichev  * report completion of some encapsulated command (as used in RNDIS).  Since
111923cd1385SRemy Bohmer  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
112023cd1385SRemy Bohmer  * command mechanism; and only one status request is ever queued.
112123cd1385SRemy Bohmer  */
eth_status_complete(struct usb_ep * ep,struct usb_request * req)112223cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
112323cd1385SRemy Bohmer {
112423cd1385SRemy Bohmer 	struct usb_cdc_notification	*event = req->buf;
112523cd1385SRemy Bohmer 	int				value = req->status;
112623cd1385SRemy Bohmer 	struct eth_dev			*dev = ep->driver_data;
112723cd1385SRemy Bohmer 
112823cd1385SRemy Bohmer 	/* issue the second notification if host reads the first */
112923cd1385SRemy Bohmer 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
113023cd1385SRemy Bohmer 			&& value == 0) {
113123cd1385SRemy Bohmer 		__le32	*data = req->buf + sizeof *event;
113223cd1385SRemy Bohmer 
113323cd1385SRemy Bohmer 		event->bmRequestType = 0xA1;
113423cd1385SRemy Bohmer 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
113523cd1385SRemy Bohmer 		event->wValue = __constant_cpu_to_le16(0);
113623cd1385SRemy Bohmer 		event->wIndex = __constant_cpu_to_le16(1);
113723cd1385SRemy Bohmer 		event->wLength = __constant_cpu_to_le16(8);
113823cd1385SRemy Bohmer 
113923cd1385SRemy Bohmer 		/* SPEED_CHANGE data is up/down speeds in bits/sec */
114023cd1385SRemy Bohmer 		data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
114123cd1385SRemy Bohmer 
114223cd1385SRemy Bohmer 		req->length = STATUS_BYTECOUNT;
114323cd1385SRemy Bohmer 		value = usb_ep_queue(ep, req, GFP_ATOMIC);
11447de73185SVitaly Kuzmichev 		debug("send SPEED_CHANGE --> %d\n", value);
114523cd1385SRemy Bohmer 		if (value == 0)
114623cd1385SRemy Bohmer 			return;
114723cd1385SRemy Bohmer 	} else if (value != -ECONNRESET) {
11487de73185SVitaly Kuzmichev 		debug("event %02x --> %d\n",
114923cd1385SRemy Bohmer 			event->bNotificationType, value);
115023cd1385SRemy Bohmer 		if (event->bNotificationType ==
11516142e0aeSVitaly Kuzmichev 				USB_CDC_NOTIFY_SPEED_CHANGE) {
115217b4f308SMugunthan V N 			dev->network_started = 1;
115323cd1385SRemy Bohmer 			printf("USB network up!\n");
115423cd1385SRemy Bohmer 		}
115523cd1385SRemy Bohmer 	}
115623cd1385SRemy Bohmer 	req->context = NULL;
115723cd1385SRemy Bohmer }
115823cd1385SRemy Bohmer 
issue_start_status(struct eth_dev * dev)115923cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev)
116023cd1385SRemy Bohmer {
116123cd1385SRemy Bohmer 	struct usb_request		*req = dev->stat_req;
116223cd1385SRemy Bohmer 	struct usb_cdc_notification	*event;
116323cd1385SRemy Bohmer 	int				value;
116423cd1385SRemy Bohmer 
11656142e0aeSVitaly Kuzmichev 	/*
11666142e0aeSVitaly Kuzmichev 	 * flush old status
116723cd1385SRemy Bohmer 	 *
116823cd1385SRemy Bohmer 	 * FIXME ugly idiom, maybe we'd be better with just
116923cd1385SRemy Bohmer 	 * a "cancel the whole queue" primitive since any
117023cd1385SRemy Bohmer 	 * unlink-one primitive has way too many error modes.
117123cd1385SRemy Bohmer 	 * here, we "know" toggle is already clear...
117223cd1385SRemy Bohmer 	 *
117323cd1385SRemy Bohmer 	 * FIXME iff req->context != null just dequeue it
117423cd1385SRemy Bohmer 	 */
117523cd1385SRemy Bohmer 	usb_ep_disable(dev->status_ep);
117623cd1385SRemy Bohmer 	usb_ep_enable(dev->status_ep, dev->status);
117723cd1385SRemy Bohmer 
11786142e0aeSVitaly Kuzmichev 	/*
11796142e0aeSVitaly Kuzmichev 	 * 3.8.1 says to issue first NETWORK_CONNECTION, then
118023cd1385SRemy Bohmer 	 * a SPEED_CHANGE.  could be useful in some configs.
118123cd1385SRemy Bohmer 	 */
118223cd1385SRemy Bohmer 	event = req->buf;
118323cd1385SRemy Bohmer 	event->bmRequestType = 0xA1;
118423cd1385SRemy Bohmer 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
118523cd1385SRemy Bohmer 	event->wValue = __constant_cpu_to_le16(1);	/* connected */
118623cd1385SRemy Bohmer 	event->wIndex = __constant_cpu_to_le16(1);
118723cd1385SRemy Bohmer 	event->wLength = 0;
118823cd1385SRemy Bohmer 
118923cd1385SRemy Bohmer 	req->length = sizeof *event;
119023cd1385SRemy Bohmer 	req->complete = eth_status_complete;
119123cd1385SRemy Bohmer 	req->context = dev;
119223cd1385SRemy Bohmer 
119323cd1385SRemy Bohmer 	value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
119423cd1385SRemy Bohmer 	if (value < 0)
11957de73185SVitaly Kuzmichev 		debug("status buf queue --> %d\n", value);
119623cd1385SRemy Bohmer }
119723cd1385SRemy Bohmer 
119823cd1385SRemy Bohmer #endif
119923cd1385SRemy Bohmer 
120023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
120123cd1385SRemy Bohmer 
eth_setup_complete(struct usb_ep * ep,struct usb_request * req)120223cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
120323cd1385SRemy Bohmer {
120423cd1385SRemy Bohmer 	if (req->status || req->actual != req->length)
12057de73185SVitaly Kuzmichev 		debug("setup complete --> %d, %d/%d\n",
120623cd1385SRemy Bohmer 				req->status, req->actual, req->length);
120723cd1385SRemy Bohmer }
120823cd1385SRemy Bohmer 
12097612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
12107612a43dSVitaly Kuzmichev 
rndis_response_complete(struct usb_ep * ep,struct usb_request * req)12117612a43dSVitaly Kuzmichev static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
12127612a43dSVitaly Kuzmichev {
12137612a43dSVitaly Kuzmichev 	if (req->status || req->actual != req->length)
12147612a43dSVitaly Kuzmichev 		debug("rndis response complete --> %d, %d/%d\n",
12157612a43dSVitaly Kuzmichev 			req->status, req->actual, req->length);
12167612a43dSVitaly Kuzmichev 
12177612a43dSVitaly Kuzmichev 	/* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
12187612a43dSVitaly Kuzmichev }
12197612a43dSVitaly Kuzmichev 
rndis_command_complete(struct usb_ep * ep,struct usb_request * req)12207612a43dSVitaly Kuzmichev static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
12217612a43dSVitaly Kuzmichev {
12227612a43dSVitaly Kuzmichev 	struct eth_dev          *dev = ep->driver_data;
12237612a43dSVitaly Kuzmichev 	int			status;
12247612a43dSVitaly Kuzmichev 
12257612a43dSVitaly Kuzmichev 	/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
12267612a43dSVitaly Kuzmichev 	status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
12277612a43dSVitaly Kuzmichev 	if (status < 0)
12289b643e31SMasahiro Yamada 		pr_err("%s: rndis parse error %d", __func__, status);
12297612a43dSVitaly Kuzmichev }
12307612a43dSVitaly Kuzmichev 
12317612a43dSVitaly Kuzmichev #endif	/* RNDIS */
12327612a43dSVitaly Kuzmichev 
123323cd1385SRemy Bohmer /*
123423cd1385SRemy Bohmer  * The setup() callback implements all the ep0 functionality that's not
123523cd1385SRemy Bohmer  * handled lower down.  CDC has a number of less-common features:
123623cd1385SRemy Bohmer  *
123723cd1385SRemy Bohmer  *  - two interfaces:  control, and ethernet data
123823cd1385SRemy Bohmer  *  - Ethernet data interface has two altsettings:  default, and active
123923cd1385SRemy Bohmer  *  - class-specific descriptors for the control interface
124023cd1385SRemy Bohmer  *  - class-specific control requests
124123cd1385SRemy Bohmer  */
124223cd1385SRemy Bohmer static int
eth_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)124323cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
124423cd1385SRemy Bohmer {
124523cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data(gadget);
124623cd1385SRemy Bohmer 	struct usb_request	*req = dev->req;
124723cd1385SRemy Bohmer 	int			value = -EOPNOTSUPP;
124823cd1385SRemy Bohmer 	u16			wIndex = le16_to_cpu(ctrl->wIndex);
124923cd1385SRemy Bohmer 	u16			wValue = le16_to_cpu(ctrl->wValue);
125023cd1385SRemy Bohmer 	u16			wLength = le16_to_cpu(ctrl->wLength);
125123cd1385SRemy Bohmer 
12526142e0aeSVitaly Kuzmichev 	/*
12536142e0aeSVitaly Kuzmichev 	 * descriptors just go into the pre-allocated ep0 buffer,
125423cd1385SRemy Bohmer 	 * while config change events may enable network traffic.
125523cd1385SRemy Bohmer 	 */
125623cd1385SRemy Bohmer 
12577de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
125823cd1385SRemy Bohmer 
125923cd1385SRemy Bohmer 	req->complete = eth_setup_complete;
126023cd1385SRemy Bohmer 	switch (ctrl->bRequest) {
126123cd1385SRemy Bohmer 
126223cd1385SRemy Bohmer 	case USB_REQ_GET_DESCRIPTOR:
126323cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
126423cd1385SRemy Bohmer 			break;
126523cd1385SRemy Bohmer 		switch (wValue >> 8) {
126623cd1385SRemy Bohmer 
126723cd1385SRemy Bohmer 		case USB_DT_DEVICE:
126804afd5b5SKishon Vijay Abraham I 			device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
126923cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof device_desc);
127023cd1385SRemy Bohmer 			memcpy(req->buf, &device_desc, value);
127123cd1385SRemy Bohmer 			break;
127223cd1385SRemy Bohmer 		case USB_DT_DEVICE_QUALIFIER:
127323cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
127423cd1385SRemy Bohmer 				break;
127523cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof dev_qualifier);
127623cd1385SRemy Bohmer 			memcpy(req->buf, &dev_qualifier, value);
127723cd1385SRemy Bohmer 			break;
127823cd1385SRemy Bohmer 
127923cd1385SRemy Bohmer 		case USB_DT_OTHER_SPEED_CONFIG:
128023cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
128123cd1385SRemy Bohmer 				break;
128223cd1385SRemy Bohmer 			/* FALLTHROUGH */
128323cd1385SRemy Bohmer 		case USB_DT_CONFIG:
128423cd1385SRemy Bohmer 			value = config_buf(gadget, req->buf,
128523cd1385SRemy Bohmer 					wValue >> 8,
128623cd1385SRemy Bohmer 					wValue & 0xff,
128723cd1385SRemy Bohmer 					gadget_is_otg(gadget));
128823cd1385SRemy Bohmer 			if (value >= 0)
128923cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
129023cd1385SRemy Bohmer 			break;
129123cd1385SRemy Bohmer 
129223cd1385SRemy Bohmer 		case USB_DT_STRING:
129323cd1385SRemy Bohmer 			value = usb_gadget_get_string(&stringtab,
129423cd1385SRemy Bohmer 					wValue & 0xff, req->buf);
129523cd1385SRemy Bohmer 
129623cd1385SRemy Bohmer 			if (value >= 0)
129723cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
129823cd1385SRemy Bohmer 
129923cd1385SRemy Bohmer 			break;
130023cd1385SRemy Bohmer 		}
130123cd1385SRemy Bohmer 		break;
130223cd1385SRemy Bohmer 
130323cd1385SRemy Bohmer 	case USB_REQ_SET_CONFIGURATION:
130423cd1385SRemy Bohmer 		if (ctrl->bRequestType != 0)
130523cd1385SRemy Bohmer 			break;
130623cd1385SRemy Bohmer 		if (gadget->a_hnp_support)
13077de73185SVitaly Kuzmichev 			debug("HNP available\n");
130823cd1385SRemy Bohmer 		else if (gadget->a_alt_hnp_support)
13097de73185SVitaly Kuzmichev 			debug("HNP needs a different root port\n");
131023cd1385SRemy Bohmer 		value = eth_set_config(dev, wValue, GFP_ATOMIC);
131123cd1385SRemy Bohmer 		break;
131223cd1385SRemy Bohmer 	case USB_REQ_GET_CONFIGURATION:
131323cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
131423cd1385SRemy Bohmer 			break;
131523cd1385SRemy Bohmer 		*(u8 *)req->buf = dev->config;
131623cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
131723cd1385SRemy Bohmer 		break;
131823cd1385SRemy Bohmer 
131923cd1385SRemy Bohmer 	case USB_REQ_SET_INTERFACE:
132023cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_RECIP_INTERFACE
132123cd1385SRemy Bohmer 				|| !dev->config
132223cd1385SRemy Bohmer 				|| wIndex > 1)
132323cd1385SRemy Bohmer 			break;
132423cd1385SRemy Bohmer 		if (!cdc_active(dev) && wIndex != 0)
132523cd1385SRemy Bohmer 			break;
132623cd1385SRemy Bohmer 
13276142e0aeSVitaly Kuzmichev 		/*
13286142e0aeSVitaly Kuzmichev 		 * PXA hardware partially handles SET_INTERFACE;
132923cd1385SRemy Bohmer 		 * we need to kluge around that interference.
133023cd1385SRemy Bohmer 		 */
133123cd1385SRemy Bohmer 		if (gadget_is_pxa(gadget)) {
133223cd1385SRemy Bohmer 			value = eth_set_config(dev, DEV_CONFIG_VALUE,
133323cd1385SRemy Bohmer 						GFP_ATOMIC);
1334563aed25SLukasz Dalek 			/*
1335563aed25SLukasz Dalek 			 * PXA25x driver use non-CDC ethernet gadget.
1336563aed25SLukasz Dalek 			 * But only _CDC and _RNDIS code can signalize
1337563aed25SLukasz Dalek 			 * that network is working. So we signalize it
1338563aed25SLukasz Dalek 			 * here.
1339563aed25SLukasz Dalek 			 */
134017b4f308SMugunthan V N 			dev->network_started = 1;
1341563aed25SLukasz Dalek 			debug("USB network up!\n");
134223cd1385SRemy Bohmer 			goto done_set_intf;
134323cd1385SRemy Bohmer 		}
134423cd1385SRemy Bohmer 
13452bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
134623cd1385SRemy Bohmer 		switch (wIndex) {
134723cd1385SRemy Bohmer 		case 0:		/* control/master intf */
134823cd1385SRemy Bohmer 			if (wValue != 0)
134923cd1385SRemy Bohmer 				break;
135023cd1385SRemy Bohmer 			if (dev->status) {
135123cd1385SRemy Bohmer 				usb_ep_disable(dev->status_ep);
135223cd1385SRemy Bohmer 				usb_ep_enable(dev->status_ep, dev->status);
135323cd1385SRemy Bohmer 			}
13547612a43dSVitaly Kuzmichev 
135523cd1385SRemy Bohmer 			value = 0;
135623cd1385SRemy Bohmer 			break;
135723cd1385SRemy Bohmer 		case 1:		/* data intf */
135823cd1385SRemy Bohmer 			if (wValue > 1)
135923cd1385SRemy Bohmer 				break;
136023cd1385SRemy Bohmer 			usb_ep_disable(dev->in_ep);
136123cd1385SRemy Bohmer 			usb_ep_disable(dev->out_ep);
136223cd1385SRemy Bohmer 
13636142e0aeSVitaly Kuzmichev 			/*
13646142e0aeSVitaly Kuzmichev 			 * CDC requires the data transfers not be done from
136523cd1385SRemy Bohmer 			 * the default interface setting ... also, setting
136623cd1385SRemy Bohmer 			 * the non-default interface resets filters etc.
136723cd1385SRemy Bohmer 			 */
136823cd1385SRemy Bohmer 			if (wValue == 1) {
136923cd1385SRemy Bohmer 				if (!cdc_active(dev))
137023cd1385SRemy Bohmer 					break;
137123cd1385SRemy Bohmer 				usb_ep_enable(dev->in_ep, dev->in);
137223cd1385SRemy Bohmer 				usb_ep_enable(dev->out_ep, dev->out);
137323cd1385SRemy Bohmer 				dev->cdc_filter = DEFAULT_FILTER;
137423cd1385SRemy Bohmer 				if (dev->status)
137523cd1385SRemy Bohmer 					issue_start_status(dev);
13767612a43dSVitaly Kuzmichev 				eth_start(dev, GFP_ATOMIC);
137723cd1385SRemy Bohmer 			}
137823cd1385SRemy Bohmer 			value = 0;
137923cd1385SRemy Bohmer 			break;
138023cd1385SRemy Bohmer 		}
138123cd1385SRemy Bohmer #else
13826142e0aeSVitaly Kuzmichev 		/*
13836142e0aeSVitaly Kuzmichev 		 * FIXME this is wrong, as is the assumption that
138423cd1385SRemy Bohmer 		 * all non-PXA hardware talks real CDC ...
138523cd1385SRemy Bohmer 		 */
13867de73185SVitaly Kuzmichev 		debug("set_interface ignored!\n");
13872bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
138823cd1385SRemy Bohmer 
138923cd1385SRemy Bohmer done_set_intf:
139023cd1385SRemy Bohmer 		break;
139123cd1385SRemy Bohmer 	case USB_REQ_GET_INTERFACE:
139223cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
139323cd1385SRemy Bohmer 				|| !dev->config
139423cd1385SRemy Bohmer 				|| wIndex > 1)
139523cd1385SRemy Bohmer 			break;
13967612a43dSVitaly Kuzmichev 		if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
139723cd1385SRemy Bohmer 			break;
139823cd1385SRemy Bohmer 
139923cd1385SRemy Bohmer 		/* for CDC, iff carrier is on, data interface is active. */
14007612a43dSVitaly Kuzmichev 		if (rndis_active(dev) || wIndex != 1)
140123cd1385SRemy Bohmer 			*(u8 *)req->buf = 0;
140223cd1385SRemy Bohmer 		else {
140323cd1385SRemy Bohmer 			/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
140423cd1385SRemy Bohmer 			/* carrier always ok ...*/
140523cd1385SRemy Bohmer 			*(u8 *)req->buf = 1 ;
140623cd1385SRemy Bohmer 		}
140723cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
140823cd1385SRemy Bohmer 		break;
140923cd1385SRemy Bohmer 
14102bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
141123cd1385SRemy Bohmer 	case USB_CDC_SET_ETHERNET_PACKET_FILTER:
14126142e0aeSVitaly Kuzmichev 		/*
14136142e0aeSVitaly Kuzmichev 		 * see 6.2.30: no data, wIndex = interface,
141423cd1385SRemy Bohmer 		 * wValue = packet filter bitmap
141523cd1385SRemy Bohmer 		 */
141623cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
141723cd1385SRemy Bohmer 				|| !cdc_active(dev)
141823cd1385SRemy Bohmer 				|| wLength != 0
141923cd1385SRemy Bohmer 				|| wIndex > 1)
142023cd1385SRemy Bohmer 			break;
14217de73185SVitaly Kuzmichev 		debug("packet filter %02x\n", wValue);
142223cd1385SRemy Bohmer 		dev->cdc_filter = wValue;
142323cd1385SRemy Bohmer 		value = 0;
142423cd1385SRemy Bohmer 		break;
142523cd1385SRemy Bohmer 
14266142e0aeSVitaly Kuzmichev 	/*
14276142e0aeSVitaly Kuzmichev 	 * and potentially:
142823cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
142923cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
143023cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
143123cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_STATISTIC:
143223cd1385SRemy Bohmer 	 */
143323cd1385SRemy Bohmer 
14342bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
143523cd1385SRemy Bohmer 
14367612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
14377612a43dSVitaly Kuzmichev 	/*
14387612a43dSVitaly Kuzmichev 	 * RNDIS uses the CDC command encapsulation mechanism to implement
14397612a43dSVitaly Kuzmichev 	 * an RPC scheme, with much getting/setting of attributes by OID.
14407612a43dSVitaly Kuzmichev 	 */
14417612a43dSVitaly Kuzmichev 	case USB_CDC_SEND_ENCAPSULATED_COMMAND:
14427612a43dSVitaly Kuzmichev 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14437612a43dSVitaly Kuzmichev 				|| !rndis_active(dev)
14447612a43dSVitaly Kuzmichev 				|| wLength > USB_BUFSIZ
14457612a43dSVitaly Kuzmichev 				|| wValue
14467612a43dSVitaly Kuzmichev 				|| rndis_control_intf.bInterfaceNumber
14477612a43dSVitaly Kuzmichev 					!= wIndex)
14487612a43dSVitaly Kuzmichev 			break;
14497612a43dSVitaly Kuzmichev 		/* read the request, then process it */
14507612a43dSVitaly Kuzmichev 		value = wLength;
14517612a43dSVitaly Kuzmichev 		req->complete = rndis_command_complete;
14527612a43dSVitaly Kuzmichev 		/* later, rndis_control_ack () sends a notification */
14537612a43dSVitaly Kuzmichev 		break;
14547612a43dSVitaly Kuzmichev 
14557612a43dSVitaly Kuzmichev 	case USB_CDC_GET_ENCAPSULATED_RESPONSE:
14567612a43dSVitaly Kuzmichev 		if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14577612a43dSVitaly Kuzmichev 					== ctrl->bRequestType
14587612a43dSVitaly Kuzmichev 				&& rndis_active(dev)
14597612a43dSVitaly Kuzmichev 				/* && wLength >= 0x0400 */
14607612a43dSVitaly Kuzmichev 				&& !wValue
14617612a43dSVitaly Kuzmichev 				&& rndis_control_intf.bInterfaceNumber
14627612a43dSVitaly Kuzmichev 					== wIndex) {
14637612a43dSVitaly Kuzmichev 			u8 *buf;
14647612a43dSVitaly Kuzmichev 			u32 n;
14657612a43dSVitaly Kuzmichev 
14667612a43dSVitaly Kuzmichev 			/* return the result */
14677612a43dSVitaly Kuzmichev 			buf = rndis_get_next_response(dev->rndis_config, &n);
14687612a43dSVitaly Kuzmichev 			if (buf) {
14697612a43dSVitaly Kuzmichev 				memcpy(req->buf, buf, n);
14707612a43dSVitaly Kuzmichev 				req->complete = rndis_response_complete;
14717612a43dSVitaly Kuzmichev 				rndis_free_response(dev->rndis_config, buf);
14727612a43dSVitaly Kuzmichev 				value = n;
14737612a43dSVitaly Kuzmichev 			}
14747612a43dSVitaly Kuzmichev 			/* else stalls ... spec says to avoid that */
14757612a43dSVitaly Kuzmichev 		}
14767612a43dSVitaly Kuzmichev 		break;
14777612a43dSVitaly Kuzmichev #endif	/* RNDIS */
14787612a43dSVitaly Kuzmichev 
147923cd1385SRemy Bohmer 	default:
14807de73185SVitaly Kuzmichev 		debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
148123cd1385SRemy Bohmer 			ctrl->bRequestType, ctrl->bRequest,
148223cd1385SRemy Bohmer 			wValue, wIndex, wLength);
148323cd1385SRemy Bohmer 	}
148423cd1385SRemy Bohmer 
148523cd1385SRemy Bohmer 	/* respond with data transfer before status phase? */
148623cd1385SRemy Bohmer 	if (value >= 0) {
14877de73185SVitaly Kuzmichev 		debug("respond with data transfer before status phase\n");
148823cd1385SRemy Bohmer 		req->length = value;
148923cd1385SRemy Bohmer 		req->zero = value < wLength
149023cd1385SRemy Bohmer 				&& (value % gadget->ep0->maxpacket) == 0;
149123cd1385SRemy Bohmer 		value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
149223cd1385SRemy Bohmer 		if (value < 0) {
14937de73185SVitaly Kuzmichev 			debug("ep_queue --> %d\n", value);
149423cd1385SRemy Bohmer 			req->status = 0;
149523cd1385SRemy Bohmer 			eth_setup_complete(gadget->ep0, req);
149623cd1385SRemy Bohmer 		}
149723cd1385SRemy Bohmer 	}
149823cd1385SRemy Bohmer 
149923cd1385SRemy Bohmer 	/* host either stalls (value < 0) or reports success */
150023cd1385SRemy Bohmer 	return value;
150123cd1385SRemy Bohmer }
150223cd1385SRemy Bohmer 
150323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
150423cd1385SRemy Bohmer 
150523cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req);
150623cd1385SRemy Bohmer 
rx_submit(struct eth_dev * dev,struct usb_request * req,gfp_t gfp_flags)15076142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req,
150823cd1385SRemy Bohmer 				gfp_t gfp_flags)
150923cd1385SRemy Bohmer {
151023cd1385SRemy Bohmer 	int			retval = -ENOMEM;
151123cd1385SRemy Bohmer 	size_t			size;
151223cd1385SRemy Bohmer 
15136142e0aeSVitaly Kuzmichev 	/*
15146142e0aeSVitaly Kuzmichev 	 * Padding up to RX_EXTRA handles minor disagreements with host.
151523cd1385SRemy Bohmer 	 * Normally we use the USB "terminate on short read" convention;
151623cd1385SRemy Bohmer 	 * so allow up to (N*maxpacket), since that memory is normally
151723cd1385SRemy Bohmer 	 * already allocated.  Some hardware doesn't deal well with short
151823cd1385SRemy Bohmer 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
151923cd1385SRemy Bohmer 	 * byte off the end (to force hardware errors on overflow).
15207612a43dSVitaly Kuzmichev 	 *
15217612a43dSVitaly Kuzmichev 	 * RNDIS uses internal framing, and explicitly allows senders to
15227612a43dSVitaly Kuzmichev 	 * pad to end-of-packet.  That's potentially nice for speed,
15237612a43dSVitaly Kuzmichev 	 * but means receivers can't recover synch on their own.
152423cd1385SRemy Bohmer 	 */
152523cd1385SRemy Bohmer 
15267de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
152771fc5f91STroy Kisky 	if (!req)
152871fc5f91STroy Kisky 		return -EINVAL;
152923cd1385SRemy Bohmer 
153023cd1385SRemy Bohmer 	size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
153123cd1385SRemy Bohmer 	size += dev->out_ep->maxpacket - 1;
15327612a43dSVitaly Kuzmichev 	if (rndis_active(dev))
15337612a43dSVitaly Kuzmichev 		size += sizeof(struct rndis_packet_msg_type);
153423cd1385SRemy Bohmer 	size -= size % dev->out_ep->maxpacket;
153523cd1385SRemy Bohmer 
15366142e0aeSVitaly Kuzmichev 	/*
15376142e0aeSVitaly Kuzmichev 	 * Some platforms perform better when IP packets are aligned,
15387612a43dSVitaly Kuzmichev 	 * but on at least one, checksumming fails otherwise.  Note:
15397612a43dSVitaly Kuzmichev 	 * RNDIS headers involve variable numbers of LE32 values.
154023cd1385SRemy Bohmer 	 */
154123cd1385SRemy Bohmer 
15421fd92db8SJoe Hershberger 	req->buf = (u8 *)net_rx_packets[0];
154323cd1385SRemy Bohmer 	req->length = size;
154423cd1385SRemy Bohmer 	req->complete = rx_complete;
154523cd1385SRemy Bohmer 
154623cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
154723cd1385SRemy Bohmer 
15486142e0aeSVitaly Kuzmichev 	if (retval)
15499b643e31SMasahiro Yamada 		pr_err("rx submit --> %d", retval);
15506142e0aeSVitaly Kuzmichev 
155123cd1385SRemy Bohmer 	return retval;
155223cd1385SRemy Bohmer }
155323cd1385SRemy Bohmer 
rx_complete(struct usb_ep * ep,struct usb_request * req)155423cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req)
155523cd1385SRemy Bohmer {
155623cd1385SRemy Bohmer 	struct eth_dev	*dev = ep->driver_data;
155723cd1385SRemy Bohmer 
15587de73185SVitaly Kuzmichev 	debug("%s: status %d\n", __func__, req->status);
1559c85d70efSVitaly Kuzmichev 	switch (req->status) {
1560c85d70efSVitaly Kuzmichev 	/* normal completion */
1561c85d70efSVitaly Kuzmichev 	case 0:
15627612a43dSVitaly Kuzmichev 		if (rndis_active(dev)) {
15637612a43dSVitaly Kuzmichev 			/* we know MaxPacketsPerTransfer == 1 here */
15647612a43dSVitaly Kuzmichev 			int length = rndis_rm_hdr(req->buf, req->actual);
15657612a43dSVitaly Kuzmichev 			if (length < 0)
15667612a43dSVitaly Kuzmichev 				goto length_err;
15677612a43dSVitaly Kuzmichev 			req->length -= length;
15687612a43dSVitaly Kuzmichev 			req->actual -= length;
15697612a43dSVitaly Kuzmichev 		}
1570dda52510SBin Meng 		if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
15717612a43dSVitaly Kuzmichev length_err:
15727612a43dSVitaly Kuzmichev 			dev->stats.rx_errors++;
15737612a43dSVitaly Kuzmichev 			dev->stats.rx_length_errors++;
15747612a43dSVitaly Kuzmichev 			debug("rx length %d\n", req->length);
15757612a43dSVitaly Kuzmichev 			break;
15767612a43dSVitaly Kuzmichev 		}
15777612a43dSVitaly Kuzmichev 
1578c85d70efSVitaly Kuzmichev 		dev->stats.rx_packets++;
1579c85d70efSVitaly Kuzmichev 		dev->stats.rx_bytes += req->length;
1580c85d70efSVitaly Kuzmichev 		break;
1581c85d70efSVitaly Kuzmichev 
1582c85d70efSVitaly Kuzmichev 	/* software-driven interface shutdown */
1583c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1584c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1585c85d70efSVitaly Kuzmichev 	/* for hardware automagic (such as pxa) */
1586c85d70efSVitaly Kuzmichev 	case -ECONNABORTED:		/* endpoint reset */
1587c85d70efSVitaly Kuzmichev 		break;
1588c85d70efSVitaly Kuzmichev 
1589c85d70efSVitaly Kuzmichev 	/* data overrun */
1590c85d70efSVitaly Kuzmichev 	case -EOVERFLOW:
1591c85d70efSVitaly Kuzmichev 		dev->stats.rx_over_errors++;
1592c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1593c85d70efSVitaly Kuzmichev 	default:
1594c85d70efSVitaly Kuzmichev 		dev->stats.rx_errors++;
1595c85d70efSVitaly Kuzmichev 		break;
1596c85d70efSVitaly Kuzmichev 	}
159723cd1385SRemy Bohmer 
159823cd1385SRemy Bohmer 	packet_received = 1;
159923cd1385SRemy Bohmer }
160023cd1385SRemy Bohmer 
alloc_requests(struct eth_dev * dev,unsigned n,gfp_t gfp_flags)160123cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
160223cd1385SRemy Bohmer {
160323cd1385SRemy Bohmer 
160423cd1385SRemy Bohmer 	dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
160523cd1385SRemy Bohmer 
160623cd1385SRemy Bohmer 	if (!dev->tx_req)
1607ac5d32d1SVitaly Kuzmichev 		goto fail1;
160823cd1385SRemy Bohmer 
160923cd1385SRemy Bohmer 	dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
161023cd1385SRemy Bohmer 
161123cd1385SRemy Bohmer 	if (!dev->rx_req)
1612ac5d32d1SVitaly Kuzmichev 		goto fail2;
161323cd1385SRemy Bohmer 
161423cd1385SRemy Bohmer 	return 0;
161523cd1385SRemy Bohmer 
1616ac5d32d1SVitaly Kuzmichev fail2:
1617ac5d32d1SVitaly Kuzmichev 	usb_ep_free_request(dev->in_ep, dev->tx_req);
1618ac5d32d1SVitaly Kuzmichev fail1:
16199b643e31SMasahiro Yamada 	pr_err("can't alloc requests");
162023cd1385SRemy Bohmer 	return -1;
162123cd1385SRemy Bohmer }
162223cd1385SRemy Bohmer 
tx_complete(struct usb_ep * ep,struct usb_request * req)162323cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req)
162423cd1385SRemy Bohmer {
1625c85d70efSVitaly Kuzmichev 	struct eth_dev	*dev = ep->driver_data;
1626c85d70efSVitaly Kuzmichev 
16277de73185SVitaly Kuzmichev 	debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1628c85d70efSVitaly Kuzmichev 	switch (req->status) {
1629c85d70efSVitaly Kuzmichev 	default:
1630c85d70efSVitaly Kuzmichev 		dev->stats.tx_errors++;
1631c85d70efSVitaly Kuzmichev 		debug("tx err %d\n", req->status);
1632c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1633c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1634c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1635c85d70efSVitaly Kuzmichev 		break;
1636c85d70efSVitaly Kuzmichev 	case 0:
1637c85d70efSVitaly Kuzmichev 		dev->stats.tx_bytes += req->length;
1638c85d70efSVitaly Kuzmichev 	}
1639c85d70efSVitaly Kuzmichev 	dev->stats.tx_packets++;
1640c85d70efSVitaly Kuzmichev 
164123cd1385SRemy Bohmer 	packet_sent = 1;
164223cd1385SRemy Bohmer }
164323cd1385SRemy Bohmer 
eth_is_promisc(struct eth_dev * dev)164423cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev)
164523cd1385SRemy Bohmer {
164623cd1385SRemy Bohmer 	/* no filters for the CDC subset; always promisc */
164723cd1385SRemy Bohmer 	if (subset_active(dev))
164823cd1385SRemy Bohmer 		return 1;
164923cd1385SRemy Bohmer 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
165023cd1385SRemy Bohmer }
165123cd1385SRemy Bohmer 
165223cd1385SRemy Bohmer #if 0
165323cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
165423cd1385SRemy Bohmer {
165523cd1385SRemy Bohmer 	struct eth_dev		*dev = netdev_priv(net);
165623cd1385SRemy Bohmer 	int			length = skb->len;
165723cd1385SRemy Bohmer 	int			retval;
165823cd1385SRemy Bohmer 	struct usb_request	*req = NULL;
165923cd1385SRemy Bohmer 	unsigned long		flags;
166023cd1385SRemy Bohmer 
166123cd1385SRemy Bohmer 	/* apply outgoing CDC or RNDIS filters */
166223cd1385SRemy Bohmer 	if (!eth_is_promisc (dev)) {
166323cd1385SRemy Bohmer 		u8		*dest = skb->data;
166423cd1385SRemy Bohmer 
16650adb5b76SJoe Hershberger 		if (is_multicast_ethaddr(dest)) {
166623cd1385SRemy Bohmer 			u16	type;
166723cd1385SRemy Bohmer 
166823cd1385SRemy Bohmer 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
166923cd1385SRemy Bohmer 			 * SET_ETHERNET_MULTICAST_FILTERS requests
167023cd1385SRemy Bohmer 			 */
16710adb5b76SJoe Hershberger 			if (is_broadcast_ethaddr(dest))
167223cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_BROADCAST;
167323cd1385SRemy Bohmer 			else
167423cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
167523cd1385SRemy Bohmer 			if (!(dev->cdc_filter & type)) {
167623cd1385SRemy Bohmer 				dev_kfree_skb_any (skb);
167723cd1385SRemy Bohmer 				return 0;
167823cd1385SRemy Bohmer 			}
167923cd1385SRemy Bohmer 		}
168023cd1385SRemy Bohmer 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
168123cd1385SRemy Bohmer 	}
168223cd1385SRemy Bohmer 
168323cd1385SRemy Bohmer 	spin_lock_irqsave(&dev->req_lock, flags);
168423cd1385SRemy Bohmer 	/*
168523cd1385SRemy Bohmer 	 * this freelist can be empty if an interrupt triggered disconnect()
168623cd1385SRemy Bohmer 	 * and reconfigured the gadget (shutting down this queue) after the
168723cd1385SRemy Bohmer 	 * network stack decided to xmit but before we got the spinlock.
168823cd1385SRemy Bohmer 	 */
168923cd1385SRemy Bohmer 	if (list_empty(&dev->tx_reqs)) {
169023cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
169123cd1385SRemy Bohmer 		return 1;
169223cd1385SRemy Bohmer 	}
169323cd1385SRemy Bohmer 
169423cd1385SRemy Bohmer 	req = container_of (dev->tx_reqs.next, struct usb_request, list);
169523cd1385SRemy Bohmer 	list_del (&req->list);
169623cd1385SRemy Bohmer 
169723cd1385SRemy Bohmer 	/* temporarily stop TX queue when the freelist empties */
169823cd1385SRemy Bohmer 	if (list_empty (&dev->tx_reqs))
169923cd1385SRemy Bohmer 		netif_stop_queue (net);
170023cd1385SRemy Bohmer 	spin_unlock_irqrestore(&dev->req_lock, flags);
170123cd1385SRemy Bohmer 
170223cd1385SRemy Bohmer 	/* no buffer copies needed, unless the network stack did it
170323cd1385SRemy Bohmer 	 * or the hardware can't use skb buffers.
170423cd1385SRemy Bohmer 	 * or there's not enough space for any RNDIS headers we need
170523cd1385SRemy Bohmer 	 */
170623cd1385SRemy Bohmer 	if (rndis_active(dev)) {
170723cd1385SRemy Bohmer 		struct sk_buff	*skb_rndis;
170823cd1385SRemy Bohmer 
170923cd1385SRemy Bohmer 		skb_rndis = skb_realloc_headroom (skb,
171023cd1385SRemy Bohmer 				sizeof (struct rndis_packet_msg_type));
171123cd1385SRemy Bohmer 		if (!skb_rndis)
171223cd1385SRemy Bohmer 			goto drop;
171323cd1385SRemy Bohmer 
171423cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
171523cd1385SRemy Bohmer 		skb = skb_rndis;
171623cd1385SRemy Bohmer 		rndis_add_hdr (skb);
171723cd1385SRemy Bohmer 		length = skb->len;
171823cd1385SRemy Bohmer 	}
171923cd1385SRemy Bohmer 	req->buf = skb->data;
172023cd1385SRemy Bohmer 	req->context = skb;
172123cd1385SRemy Bohmer 	req->complete = tx_complete;
172223cd1385SRemy Bohmer 
172323cd1385SRemy Bohmer 	/* use zlp framing on tx for strict CDC-Ether conformance,
172423cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
172523cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
172623cd1385SRemy Bohmer 	 */
172723cd1385SRemy Bohmer 	req->zero = 1;
172823cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
172923cd1385SRemy Bohmer 		length++;
173023cd1385SRemy Bohmer 
173123cd1385SRemy Bohmer 	req->length = length;
173223cd1385SRemy Bohmer 
173323cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
173423cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
173523cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
173623cd1385SRemy Bohmer 			? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
173723cd1385SRemy Bohmer 			: 0;
173823cd1385SRemy Bohmer 
173923cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
174023cd1385SRemy Bohmer 	switch (retval) {
174123cd1385SRemy Bohmer 	default:
174223cd1385SRemy Bohmer 		DEBUG (dev, "tx queue err %d\n", retval);
174323cd1385SRemy Bohmer 		break;
174423cd1385SRemy Bohmer 	case 0:
174523cd1385SRemy Bohmer 		net->trans_start = jiffies;
174623cd1385SRemy Bohmer 		atomic_inc (&dev->tx_qlen);
174723cd1385SRemy Bohmer 	}
174823cd1385SRemy Bohmer 
174923cd1385SRemy Bohmer 	if (retval) {
175023cd1385SRemy Bohmer drop:
175123cd1385SRemy Bohmer 		dev->stats.tx_dropped++;
175223cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
175323cd1385SRemy Bohmer 		spin_lock_irqsave(&dev->req_lock, flags);
175423cd1385SRemy Bohmer 		if (list_empty (&dev->tx_reqs))
175523cd1385SRemy Bohmer 			netif_start_queue (net);
175623cd1385SRemy Bohmer 		list_add (&req->list, &dev->tx_reqs);
175723cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
175823cd1385SRemy Bohmer 	}
175923cd1385SRemy Bohmer 	return 0;
176023cd1385SRemy Bohmer }
176123cd1385SRemy Bohmer 
176223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
176323cd1385SRemy Bohmer #endif
176423cd1385SRemy Bohmer 
eth_unbind(struct usb_gadget * gadget)176523cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget)
176623cd1385SRemy Bohmer {
176723cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data(gadget);
176823cd1385SRemy Bohmer 
17697de73185SVitaly Kuzmichev 	debug("%s...\n", __func__);
17707612a43dSVitaly Kuzmichev 	rndis_deregister(dev->rndis_config);
17717612a43dSVitaly Kuzmichev 	rndis_exit();
177223cd1385SRemy Bohmer 
17730129e327SVitaly Kuzmichev 	/* we've already been disconnected ... no i/o is active */
17740129e327SVitaly Kuzmichev 	if (dev->req) {
17750129e327SVitaly Kuzmichev 		usb_ep_free_request(gadget->ep0, dev->req);
17760129e327SVitaly Kuzmichev 		dev->req = NULL;
17770129e327SVitaly Kuzmichev 	}
177823cd1385SRemy Bohmer 	if (dev->stat_req) {
177923cd1385SRemy Bohmer 		usb_ep_free_request(dev->status_ep, dev->stat_req);
178023cd1385SRemy Bohmer 		dev->stat_req = NULL;
178123cd1385SRemy Bohmer 	}
178223cd1385SRemy Bohmer 
178323cd1385SRemy Bohmer 	if (dev->tx_req) {
178423cd1385SRemy Bohmer 		usb_ep_free_request(dev->in_ep, dev->tx_req);
178523cd1385SRemy Bohmer 		dev->tx_req = NULL;
178623cd1385SRemy Bohmer 	}
178723cd1385SRemy Bohmer 
178823cd1385SRemy Bohmer 	if (dev->rx_req) {
17890129e327SVitaly Kuzmichev 		usb_ep_free_request(dev->out_ep, dev->rx_req);
179023cd1385SRemy Bohmer 		dev->rx_req = NULL;
179123cd1385SRemy Bohmer 	}
179223cd1385SRemy Bohmer 
179323cd1385SRemy Bohmer /*	unregister_netdev (dev->net);*/
179423cd1385SRemy Bohmer /*	free_netdev(dev->net);*/
179523cd1385SRemy Bohmer 
1796988ee3e3SLei Wen 	dev->gadget = NULL;
179723cd1385SRemy Bohmer 	set_gadget_data(gadget, NULL);
179823cd1385SRemy Bohmer }
179923cd1385SRemy Bohmer 
eth_disconnect(struct usb_gadget * gadget)180023cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget)
180123cd1385SRemy Bohmer {
180223cd1385SRemy Bohmer 	eth_reset_config(get_gadget_data(gadget));
18037612a43dSVitaly Kuzmichev 	/* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
180423cd1385SRemy Bohmer }
180523cd1385SRemy Bohmer 
eth_suspend(struct usb_gadget * gadget)180623cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget)
180723cd1385SRemy Bohmer {
180823cd1385SRemy Bohmer 	/* Not used */
180923cd1385SRemy Bohmer }
181023cd1385SRemy Bohmer 
eth_resume(struct usb_gadget * gadget)181123cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget)
181223cd1385SRemy Bohmer {
181323cd1385SRemy Bohmer 	/* Not used */
181423cd1385SRemy Bohmer }
181523cd1385SRemy Bohmer 
181623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
181723cd1385SRemy Bohmer 
18187612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
18197612a43dSVitaly Kuzmichev 
18207612a43dSVitaly Kuzmichev /*
18217612a43dSVitaly Kuzmichev  * The interrupt endpoint is used in RNDIS to notify the host when messages
18227612a43dSVitaly Kuzmichev  * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
18237612a43dSVitaly Kuzmichev  * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
18247612a43dSVitaly Kuzmichev  * REMOTE_NDIS_KEEPALIVE_MSG.
18257612a43dSVitaly Kuzmichev  *
18267612a43dSVitaly Kuzmichev  * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
18277612a43dSVitaly Kuzmichev  * normally just one notification will be queued.
18287612a43dSVitaly Kuzmichev  */
18297612a43dSVitaly Kuzmichev 
rndis_control_ack_complete(struct usb_ep * ep,struct usb_request * req)18307612a43dSVitaly Kuzmichev static void rndis_control_ack_complete(struct usb_ep *ep,
18317612a43dSVitaly Kuzmichev 					struct usb_request *req)
18327612a43dSVitaly Kuzmichev {
18337612a43dSVitaly Kuzmichev 	struct eth_dev          *dev = ep->driver_data;
18347612a43dSVitaly Kuzmichev 
18357612a43dSVitaly Kuzmichev 	debug("%s...\n", __func__);
18367612a43dSVitaly Kuzmichev 	if (req->status || req->actual != req->length)
18377612a43dSVitaly Kuzmichev 		debug("rndis control ack complete --> %d, %d/%d\n",
18387612a43dSVitaly Kuzmichev 			req->status, req->actual, req->length);
18397612a43dSVitaly Kuzmichev 
184017b4f308SMugunthan V N 	if (!dev->network_started) {
18417612a43dSVitaly Kuzmichev 		if (rndis_get_state(dev->rndis_config)
18427612a43dSVitaly Kuzmichev 				== RNDIS_DATA_INITIALIZED) {
184317b4f308SMugunthan V N 			dev->network_started = 1;
18447612a43dSVitaly Kuzmichev 			printf("USB RNDIS network up!\n");
18457612a43dSVitaly Kuzmichev 		}
18467612a43dSVitaly Kuzmichev 	}
18477612a43dSVitaly Kuzmichev 
18487612a43dSVitaly Kuzmichev 	req->context = NULL;
18497612a43dSVitaly Kuzmichev 
18507612a43dSVitaly Kuzmichev 	if (req != dev->stat_req)
18517612a43dSVitaly Kuzmichev 		usb_ep_free_request(ep, req);
18527612a43dSVitaly Kuzmichev }
18537612a43dSVitaly Kuzmichev 
18547612a43dSVitaly Kuzmichev static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
18557612a43dSVitaly Kuzmichev 
1856d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
rndis_control_ack(struct eth_device * net)18577612a43dSVitaly Kuzmichev static int rndis_control_ack(struct eth_device *net)
1858d4a37553SMugunthan V N #else
1859d4a37553SMugunthan V N static int rndis_control_ack(struct udevice *net)
1860d4a37553SMugunthan V N #endif
18617612a43dSVitaly Kuzmichev {
1862ae70100cSMugunthan V N 	struct ether_priv	*priv = (struct ether_priv *)net->priv;
1863ae70100cSMugunthan V N 	struct eth_dev		*dev = &priv->ethdev;
18647612a43dSVitaly Kuzmichev 	int                     length;
18657612a43dSVitaly Kuzmichev 	struct usb_request      *resp = dev->stat_req;
18667612a43dSVitaly Kuzmichev 
18677612a43dSVitaly Kuzmichev 	/* in case RNDIS calls this after disconnect */
18687612a43dSVitaly Kuzmichev 	if (!dev->status) {
18697612a43dSVitaly Kuzmichev 		debug("status ENODEV\n");
18707612a43dSVitaly Kuzmichev 		return -ENODEV;
18717612a43dSVitaly Kuzmichev 	}
18727612a43dSVitaly Kuzmichev 
18737612a43dSVitaly Kuzmichev 	/* in case queue length > 1 */
18747612a43dSVitaly Kuzmichev 	if (resp->context) {
18757612a43dSVitaly Kuzmichev 		resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
18767612a43dSVitaly Kuzmichev 		if (!resp)
18777612a43dSVitaly Kuzmichev 			return -ENOMEM;
18787612a43dSVitaly Kuzmichev 		resp->buf = rndis_resp_buf;
18797612a43dSVitaly Kuzmichev 	}
18807612a43dSVitaly Kuzmichev 
18817612a43dSVitaly Kuzmichev 	/*
18827612a43dSVitaly Kuzmichev 	 * Send RNDIS RESPONSE_AVAILABLE notification;
18837612a43dSVitaly Kuzmichev 	 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
18847612a43dSVitaly Kuzmichev 	 */
18857612a43dSVitaly Kuzmichev 	resp->length = 8;
18867612a43dSVitaly Kuzmichev 	resp->complete = rndis_control_ack_complete;
18877612a43dSVitaly Kuzmichev 	resp->context = dev;
18887612a43dSVitaly Kuzmichev 
18897612a43dSVitaly Kuzmichev 	*((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
18907612a43dSVitaly Kuzmichev 	*((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
18917612a43dSVitaly Kuzmichev 
18927612a43dSVitaly Kuzmichev 	length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
18937612a43dSVitaly Kuzmichev 	if (length < 0) {
18947612a43dSVitaly Kuzmichev 		resp->status = 0;
18957612a43dSVitaly Kuzmichev 		rndis_control_ack_complete(dev->status_ep, resp);
18967612a43dSVitaly Kuzmichev 	}
18977612a43dSVitaly Kuzmichev 
18987612a43dSVitaly Kuzmichev 	return 0;
18997612a43dSVitaly Kuzmichev }
19007612a43dSVitaly Kuzmichev 
19017612a43dSVitaly Kuzmichev #else
19027612a43dSVitaly Kuzmichev 
19037612a43dSVitaly Kuzmichev #define	rndis_control_ack	NULL
19047612a43dSVitaly Kuzmichev 
19057612a43dSVitaly Kuzmichev #endif	/* RNDIS */
19067612a43dSVitaly Kuzmichev 
eth_start(struct eth_dev * dev,gfp_t gfp_flags)19077612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
19087612a43dSVitaly Kuzmichev {
19097612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
19107612a43dSVitaly Kuzmichev 		rndis_set_param_medium(dev->rndis_config,
19117612a43dSVitaly Kuzmichev 					NDIS_MEDIUM_802_3,
19127612a43dSVitaly Kuzmichev 					BITRATE(dev->gadget)/100);
19137612a43dSVitaly Kuzmichev 		rndis_signal_connect(dev->rndis_config);
19147612a43dSVitaly Kuzmichev 	}
19157612a43dSVitaly Kuzmichev }
19167612a43dSVitaly Kuzmichev 
eth_stop(struct eth_dev * dev)19177612a43dSVitaly Kuzmichev static int eth_stop(struct eth_dev *dev)
19187612a43dSVitaly Kuzmichev {
1919e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1920e4ae6660SVitaly Kuzmichev 	unsigned long ts;
1921e4ae6660SVitaly Kuzmichev 	unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1922e4ae6660SVitaly Kuzmichev #endif
1923e4ae6660SVitaly Kuzmichev 
19247612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
19257612a43dSVitaly Kuzmichev 		rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
19267612a43dSVitaly Kuzmichev 		rndis_signal_disconnect(dev->rndis_config);
19277612a43dSVitaly Kuzmichev 
1928e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1929e4ae6660SVitaly Kuzmichev 		/* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1930e4ae6660SVitaly Kuzmichev 		ts = get_timer(0);
1931e4ae6660SVitaly Kuzmichev 		while (get_timer(ts) < timeout)
19322d48aa69SKishon Vijay Abraham I 			usb_gadget_handle_interrupts(0);
1933e4ae6660SVitaly Kuzmichev #endif
1934e4ae6660SVitaly Kuzmichev 
19357612a43dSVitaly Kuzmichev 		rndis_uninit(dev->rndis_config);
19367612a43dSVitaly Kuzmichev 		dev->rndis = 0;
19377612a43dSVitaly Kuzmichev 	}
19387612a43dSVitaly Kuzmichev 
19397612a43dSVitaly Kuzmichev 	return 0;
19407612a43dSVitaly Kuzmichev }
19417612a43dSVitaly Kuzmichev 
19427612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
19437612a43dSVitaly Kuzmichev 
is_eth_addr_valid(char * str)194423cd1385SRemy Bohmer static int is_eth_addr_valid(char *str)
194523cd1385SRemy Bohmer {
194623cd1385SRemy Bohmer 	if (strlen(str) == 17) {
194723cd1385SRemy Bohmer 		int i;
194823cd1385SRemy Bohmer 		char *p, *q;
194923cd1385SRemy Bohmer 		uchar ea[6];
195023cd1385SRemy Bohmer 
195123cd1385SRemy Bohmer 		/* see if it looks like an ethernet address */
195223cd1385SRemy Bohmer 
195323cd1385SRemy Bohmer 		p = str;
195423cd1385SRemy Bohmer 
195523cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
195623cd1385SRemy Bohmer 			char term = (i == 5 ? '\0' : ':');
195723cd1385SRemy Bohmer 
195823cd1385SRemy Bohmer 			ea[i] = simple_strtol(p, &q, 16);
195923cd1385SRemy Bohmer 
196023cd1385SRemy Bohmer 			if ((q - p) != 2 || *q++ != term)
196123cd1385SRemy Bohmer 				break;
196223cd1385SRemy Bohmer 
196323cd1385SRemy Bohmer 			p = q;
196423cd1385SRemy Bohmer 		}
196523cd1385SRemy Bohmer 
196657a87a25STom Rini 		/* Now check the contents. */
19670adb5b76SJoe Hershberger 		return is_valid_ethaddr(ea);
196823cd1385SRemy Bohmer 	}
196923cd1385SRemy Bohmer 	return 0;
197023cd1385SRemy Bohmer }
197123cd1385SRemy Bohmer 
nibble(unsigned char c)197223cd1385SRemy Bohmer static u8 nibble(unsigned char c)
197323cd1385SRemy Bohmer {
197423cd1385SRemy Bohmer 	if (likely(isdigit(c)))
197523cd1385SRemy Bohmer 		return c - '0';
197623cd1385SRemy Bohmer 	c = toupper(c);
197723cd1385SRemy Bohmer 	if (likely(isxdigit(c)))
197823cd1385SRemy Bohmer 		return 10 + c - 'A';
197923cd1385SRemy Bohmer 	return 0;
198023cd1385SRemy Bohmer }
198123cd1385SRemy Bohmer 
get_ether_addr(const char * str,u8 * dev_addr)198223cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr)
198323cd1385SRemy Bohmer {
198423cd1385SRemy Bohmer 	if (str) {
198523cd1385SRemy Bohmer 		unsigned	i;
198623cd1385SRemy Bohmer 
198723cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
198823cd1385SRemy Bohmer 			unsigned char num;
198923cd1385SRemy Bohmer 
199023cd1385SRemy Bohmer 			if ((*str == '.') || (*str == ':'))
199123cd1385SRemy Bohmer 				str++;
199223cd1385SRemy Bohmer 			num = nibble(*str++) << 4;
199323cd1385SRemy Bohmer 			num |= (nibble(*str++));
199423cd1385SRemy Bohmer 			dev_addr[i] = num;
199523cd1385SRemy Bohmer 		}
19960adb5b76SJoe Hershberger 		if (is_valid_ethaddr(dev_addr))
199723cd1385SRemy Bohmer 			return 0;
199823cd1385SRemy Bohmer 	}
199923cd1385SRemy Bohmer 	return 1;
200023cd1385SRemy Bohmer }
200123cd1385SRemy Bohmer 
eth_bind(struct usb_gadget * gadget)200223cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget)
200323cd1385SRemy Bohmer {
20045cb3b9d7SMugunthan V N 	struct eth_dev		*dev = &l_priv->ethdev;
20057612a43dSVitaly Kuzmichev 	u8			cdc = 1, zlp = 1, rndis = 1;
200623cd1385SRemy Bohmer 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
20077612a43dSVitaly Kuzmichev 	int			status = -ENOMEM;
200823cd1385SRemy Bohmer 	int			gcnum;
200923cd1385SRemy Bohmer 	u8			tmp[7];
2010d4a37553SMugunthan V N #ifdef CONFIG_DM_ETH
2011d4a37553SMugunthan V N 	struct eth_pdata	*pdata = dev_get_platdata(l_priv->netdev);
2012d4a37553SMugunthan V N #endif
201323cd1385SRemy Bohmer 
201423cd1385SRemy Bohmer 	/* these flags are only ever cleared; compiler take note */
20152bb37884SLukasz Dalek #ifndef	CONFIG_USB_ETH_CDC
201623cd1385SRemy Bohmer 	cdc = 0;
201723cd1385SRemy Bohmer #endif
20187612a43dSVitaly Kuzmichev #ifndef	CONFIG_USB_ETH_RNDIS
20197612a43dSVitaly Kuzmichev 	rndis = 0;
20207612a43dSVitaly Kuzmichev #endif
20216142e0aeSVitaly Kuzmichev 	/*
20226142e0aeSVitaly Kuzmichev 	 * Because most host side USB stacks handle CDC Ethernet, that
202323cd1385SRemy Bohmer 	 * standard protocol is _strongly_ preferred for interop purposes.
202423cd1385SRemy Bohmer 	 * (By everyone except Microsoft.)
202523cd1385SRemy Bohmer 	 */
202623cd1385SRemy Bohmer 	if (gadget_is_pxa(gadget)) {
202723cd1385SRemy Bohmer 		/* pxa doesn't support altsettings */
202823cd1385SRemy Bohmer 		cdc = 0;
202923cd1385SRemy Bohmer 	} else if (gadget_is_musbhdrc(gadget)) {
203023cd1385SRemy Bohmer 		/* reduce tx dma overhead by avoiding special cases */
203123cd1385SRemy Bohmer 		zlp = 0;
203223cd1385SRemy Bohmer 	} else if (gadget_is_sh(gadget)) {
203323cd1385SRemy Bohmer 		/* sh doesn't support multiple interfaces or configs */
203423cd1385SRemy Bohmer 		cdc = 0;
20357612a43dSVitaly Kuzmichev 		rndis = 0;
203623cd1385SRemy Bohmer 	} else if (gadget_is_sa1100(gadget)) {
203723cd1385SRemy Bohmer 		/* hardware can't write zlps */
203823cd1385SRemy Bohmer 		zlp = 0;
20396142e0aeSVitaly Kuzmichev 		/*
20406142e0aeSVitaly Kuzmichev 		 * sa1100 CAN do CDC, without status endpoint ... we use
204123cd1385SRemy Bohmer 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
204223cd1385SRemy Bohmer 		 */
204323cd1385SRemy Bohmer 		cdc = 0;
204423cd1385SRemy Bohmer 	}
204523cd1385SRemy Bohmer 
204623cd1385SRemy Bohmer 	gcnum = usb_gadget_controller_number(gadget);
204723cd1385SRemy Bohmer 	if (gcnum >= 0)
204823cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
204923cd1385SRemy Bohmer 	else {
20506142e0aeSVitaly Kuzmichev 		/*
20516142e0aeSVitaly Kuzmichev 		 * can't assume CDC works.  don't want to default to
205223cd1385SRemy Bohmer 		 * anything less functional on CDC-capable hardware,
205323cd1385SRemy Bohmer 		 * so we fail in this case.
205423cd1385SRemy Bohmer 		 */
20559b643e31SMasahiro Yamada 		pr_err("controller '%s' not recognized",
205623cd1385SRemy Bohmer 			gadget->name);
205723cd1385SRemy Bohmer 		return -ENODEV;
205823cd1385SRemy Bohmer 	}
205923cd1385SRemy Bohmer 
20606142e0aeSVitaly Kuzmichev 	/*
20617612a43dSVitaly Kuzmichev 	 * If there's an RNDIS configuration, that's what Windows wants to
20627612a43dSVitaly Kuzmichev 	 * be using ... so use these product IDs here and in the "linux.inf"
20637612a43dSVitaly Kuzmichev 	 * needed to install MSFT drivers.  Current Linux kernels will use
20647612a43dSVitaly Kuzmichev 	 * the second configuration if it's CDC Ethernet, and need some help
20657612a43dSVitaly Kuzmichev 	 * to choose the right configuration otherwise.
20667612a43dSVitaly Kuzmichev 	 */
20677612a43dSVitaly Kuzmichev 	if (rndis) {
206810ac57fdSMaxime Ripard #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
20697612a43dSVitaly Kuzmichev 		device_desc.idVendor =
207010ac57fdSMaxime Ripard 			__constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
20717612a43dSVitaly Kuzmichev 		device_desc.idProduct =
207210ac57fdSMaxime Ripard 			__constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
20737612a43dSVitaly Kuzmichev #else
20747612a43dSVitaly Kuzmichev 		device_desc.idVendor =
20757612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(RNDIS_VENDOR_NUM);
20767612a43dSVitaly Kuzmichev 		device_desc.idProduct =
20777612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
20787612a43dSVitaly Kuzmichev #endif
20797612a43dSVitaly Kuzmichev 		sprintf(product_desc, "RNDIS/%s", driver_desc);
20807612a43dSVitaly Kuzmichev 
20817612a43dSVitaly Kuzmichev 	/*
20826142e0aeSVitaly Kuzmichev 	 * CDC subset ... recognized by Linux since 2.4.10, but Windows
208323cd1385SRemy Bohmer 	 * drivers aren't widely available.  (That may be improved by
208423cd1385SRemy Bohmer 	 * supporting one submode of the "SAFE" variant of MDLM.)
208523cd1385SRemy Bohmer 	 */
20867612a43dSVitaly Kuzmichev 	} else {
208710ac57fdSMaxime Ripard #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
208810ac57fdSMaxime Ripard 		device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
208910ac57fdSMaxime Ripard 		device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
20907612a43dSVitaly Kuzmichev #else
209123cd1385SRemy Bohmer 		if (!cdc) {
209223cd1385SRemy Bohmer 			device_desc.idVendor =
209323cd1385SRemy Bohmer 				__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
209423cd1385SRemy Bohmer 			device_desc.idProduct =
209523cd1385SRemy Bohmer 				__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
209623cd1385SRemy Bohmer 		}
209723cd1385SRemy Bohmer #endif
20987612a43dSVitaly Kuzmichev 	}
20997612a43dSVitaly Kuzmichev 	/* support optional vendor/distro customization */
210023cd1385SRemy Bohmer 	if (bcdDevice)
210123cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(bcdDevice);
210223cd1385SRemy Bohmer 	if (iManufacturer)
21032e12abe6SVitaly Kuzmichev 		strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
210423cd1385SRemy Bohmer 	if (iProduct)
21052e12abe6SVitaly Kuzmichev 		strlcpy(product_desc, iProduct, sizeof product_desc);
210623cd1385SRemy Bohmer 	if (iSerialNumber) {
210723cd1385SRemy Bohmer 		device_desc.iSerialNumber = STRING_SERIALNUMBER,
21082e12abe6SVitaly Kuzmichev 		strlcpy(serial_number, iSerialNumber, sizeof serial_number);
210923cd1385SRemy Bohmer 	}
211023cd1385SRemy Bohmer 
211123cd1385SRemy Bohmer 	/* all we really need is bulk IN/OUT */
211223cd1385SRemy Bohmer 	usb_ep_autoconfig_reset(gadget);
211323cd1385SRemy Bohmer 	in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
211423cd1385SRemy Bohmer 	if (!in_ep) {
211523cd1385SRemy Bohmer autoconf_fail:
21169b643e31SMasahiro Yamada 		pr_err("can't autoconfigure on %s\n",
211723cd1385SRemy Bohmer 			gadget->name);
211823cd1385SRemy Bohmer 		return -ENODEV;
211923cd1385SRemy Bohmer 	}
212023cd1385SRemy Bohmer 	in_ep->driver_data = in_ep;	/* claim */
212123cd1385SRemy Bohmer 
212223cd1385SRemy Bohmer 	out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
212323cd1385SRemy Bohmer 	if (!out_ep)
212423cd1385SRemy Bohmer 		goto autoconf_fail;
212523cd1385SRemy Bohmer 	out_ep->driver_data = out_ep;	/* claim */
212623cd1385SRemy Bohmer 
21272bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
21286142e0aeSVitaly Kuzmichev 	/*
21296142e0aeSVitaly Kuzmichev 	 * CDC Ethernet control interface doesn't require a status endpoint.
213023cd1385SRemy Bohmer 	 * Since some hosts expect one, try to allocate one anyway.
213123cd1385SRemy Bohmer 	 */
21327612a43dSVitaly Kuzmichev 	if (cdc || rndis) {
213323cd1385SRemy Bohmer 		status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
213423cd1385SRemy Bohmer 		if (status_ep) {
213523cd1385SRemy Bohmer 			status_ep->driver_data = status_ep;	/* claim */
21367612a43dSVitaly Kuzmichev 		} else if (rndis) {
21379b643e31SMasahiro Yamada 			pr_err("can't run RNDIS on %s", gadget->name);
21387612a43dSVitaly Kuzmichev 			return -ENODEV;
21392bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
214023cd1385SRemy Bohmer 		} else if (cdc) {
214123cd1385SRemy Bohmer 			control_intf.bNumEndpoints = 0;
214223cd1385SRemy Bohmer 			/* FIXME remove endpoint from descriptor list */
21437612a43dSVitaly Kuzmichev #endif
214423cd1385SRemy Bohmer 		}
214523cd1385SRemy Bohmer 	}
214623cd1385SRemy Bohmer #endif
214723cd1385SRemy Bohmer 
214823cd1385SRemy Bohmer 	/* one config:  cdc, else minimal subset */
214923cd1385SRemy Bohmer 	if (!cdc) {
215023cd1385SRemy Bohmer 		eth_config.bNumInterfaces = 1;
215123cd1385SRemy Bohmer 		eth_config.iConfiguration = STRING_SUBSET;
215223cd1385SRemy Bohmer 
21536142e0aeSVitaly Kuzmichev 		/*
21546142e0aeSVitaly Kuzmichev 		 * use functions to set these up, in case we're built to work
215523cd1385SRemy Bohmer 		 * with multiple controllers and must override CDC Ethernet.
215623cd1385SRemy Bohmer 		 */
215723cd1385SRemy Bohmer 		fs_subset_descriptors();
215823cd1385SRemy Bohmer 		hs_subset_descriptors();
215923cd1385SRemy Bohmer 	}
216023cd1385SRemy Bohmer 
216123cd1385SRemy Bohmer 	usb_gadget_set_selfpowered(gadget);
216223cd1385SRemy Bohmer 
21637612a43dSVitaly Kuzmichev 	/* For now RNDIS is always a second config */
21647612a43dSVitaly Kuzmichev 	if (rndis)
21657612a43dSVitaly Kuzmichev 		device_desc.bNumConfigurations = 2;
21667612a43dSVitaly Kuzmichev 
216723cd1385SRemy Bohmer 	if (gadget_is_dualspeed(gadget)) {
21687612a43dSVitaly Kuzmichev 		if (rndis)
21697612a43dSVitaly Kuzmichev 			dev_qualifier.bNumConfigurations = 2;
21707612a43dSVitaly Kuzmichev 		else if (!cdc)
217123cd1385SRemy Bohmer 			dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
217223cd1385SRemy Bohmer 
217323cd1385SRemy Bohmer 		/* assumes ep0 uses the same value for both speeds ... */
217423cd1385SRemy Bohmer 		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
217523cd1385SRemy Bohmer 
217623cd1385SRemy Bohmer 		/* and that all endpoints are dual-speed */
217723cd1385SRemy Bohmer 		hs_source_desc.bEndpointAddress =
217823cd1385SRemy Bohmer 				fs_source_desc.bEndpointAddress;
217923cd1385SRemy Bohmer 		hs_sink_desc.bEndpointAddress =
218023cd1385SRemy Bohmer 				fs_sink_desc.bEndpointAddress;
21812bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
218223cd1385SRemy Bohmer 		if (status_ep)
218323cd1385SRemy Bohmer 			hs_status_desc.bEndpointAddress =
218423cd1385SRemy Bohmer 					fs_status_desc.bEndpointAddress;
218523cd1385SRemy Bohmer #endif
218623cd1385SRemy Bohmer 	}
218723cd1385SRemy Bohmer 
218823cd1385SRemy Bohmer 	if (gadget_is_otg(gadget)) {
218923cd1385SRemy Bohmer 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
219023cd1385SRemy Bohmer 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
219123cd1385SRemy Bohmer 		eth_config.bMaxPower = 4;
21927612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
21937612a43dSVitaly Kuzmichev 		rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
21947612a43dSVitaly Kuzmichev 		rndis_config.bMaxPower = 4;
21957612a43dSVitaly Kuzmichev #endif
219623cd1385SRemy Bohmer 	}
219723cd1385SRemy Bohmer 
21987612a43dSVitaly Kuzmichev 
21997612a43dSVitaly Kuzmichev 	/* network device setup */
2200d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
22015cb3b9d7SMugunthan V N 	dev->net = &l_priv->netdev;
2202d4a37553SMugunthan V N #else
2203d4a37553SMugunthan V N 	dev->net = l_priv->netdev;
2204d4a37553SMugunthan V N #endif
220523cd1385SRemy Bohmer 
220623cd1385SRemy Bohmer 	dev->cdc = cdc;
220723cd1385SRemy Bohmer 	dev->zlp = zlp;
220823cd1385SRemy Bohmer 
220923cd1385SRemy Bohmer 	dev->in_ep = in_ep;
221023cd1385SRemy Bohmer 	dev->out_ep = out_ep;
221123cd1385SRemy Bohmer 	dev->status_ep = status_ep;
221223cd1385SRemy Bohmer 
2213d4a37553SMugunthan V N 	memset(tmp, 0, sizeof(tmp));
22146142e0aeSVitaly Kuzmichev 	/*
22156142e0aeSVitaly Kuzmichev 	 * Module params for these addresses should come from ID proms.
22167612a43dSVitaly Kuzmichev 	 * The host side address is used with CDC and RNDIS, and commonly
221723cd1385SRemy Bohmer 	 * ends up in a persistent config database.  It's not clear if
221823cd1385SRemy Bohmer 	 * host side code for the SAFE thing cares -- its original BLAN
221923cd1385SRemy Bohmer 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
222023cd1385SRemy Bohmer 	 */
2221d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
222223cd1385SRemy Bohmer 	get_ether_addr(dev_addr, dev->net->enetaddr);
222323cd1385SRemy Bohmer 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2224d4a37553SMugunthan V N #else
2225d4a37553SMugunthan V N 	get_ether_addr(dev_addr, pdata->enetaddr);
2226d4a37553SMugunthan V N 	memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2227d4a37553SMugunthan V N #endif
222823cd1385SRemy Bohmer 
222923cd1385SRemy Bohmer 	get_ether_addr(host_addr, dev->host_mac);
223023cd1385SRemy Bohmer 
223123cd1385SRemy Bohmer 	sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
223223cd1385SRemy Bohmer 		dev->host_mac[0], dev->host_mac[1],
223323cd1385SRemy Bohmer 			dev->host_mac[2], dev->host_mac[3],
223423cd1385SRemy Bohmer 			dev->host_mac[4], dev->host_mac[5]);
223523cd1385SRemy Bohmer 
22367612a43dSVitaly Kuzmichev 	if (rndis) {
22377612a43dSVitaly Kuzmichev 		status = rndis_init();
22387612a43dSVitaly Kuzmichev 		if (status < 0) {
22399b643e31SMasahiro Yamada 			pr_err("can't init RNDIS, %d", status);
22407612a43dSVitaly Kuzmichev 			goto fail;
22417612a43dSVitaly Kuzmichev 		}
224223cd1385SRemy Bohmer 	}
224323cd1385SRemy Bohmer 
22446142e0aeSVitaly Kuzmichev 	/*
22456142e0aeSVitaly Kuzmichev 	 * use PKTSIZE (or aligned... from u-boot) and set
22466142e0aeSVitaly Kuzmichev 	 * wMaxSegmentSize accordingly
22476142e0aeSVitaly Kuzmichev 	 */
224823cd1385SRemy Bohmer 	dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
224923cd1385SRemy Bohmer 
225023cd1385SRemy Bohmer 	/* preallocate control message data and buffer */
225123cd1385SRemy Bohmer 	dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
225223cd1385SRemy Bohmer 	if (!dev->req)
225323cd1385SRemy Bohmer 		goto fail;
225423cd1385SRemy Bohmer 	dev->req->buf = control_req;
225523cd1385SRemy Bohmer 	dev->req->complete = eth_setup_complete;
225623cd1385SRemy Bohmer 
225723cd1385SRemy Bohmer 	/* ... and maybe likewise for status transfer */
22582bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
225923cd1385SRemy Bohmer 	if (dev->status_ep) {
22606142e0aeSVitaly Kuzmichev 		dev->stat_req = usb_ep_alloc_request(dev->status_ep,
22616142e0aeSVitaly Kuzmichev 							GFP_KERNEL);
226223cd1385SRemy Bohmer 		if (!dev->stat_req) {
2263df559c1dSVitaly Kuzmichev 			usb_ep_free_request(dev->status_ep, dev->req);
226423cd1385SRemy Bohmer 
226523cd1385SRemy Bohmer 			goto fail;
226623cd1385SRemy Bohmer 		}
2267df559c1dSVitaly Kuzmichev 		dev->stat_req->buf = status_req;
226823cd1385SRemy Bohmer 		dev->stat_req->context = NULL;
226923cd1385SRemy Bohmer 	}
227023cd1385SRemy Bohmer #endif
227123cd1385SRemy Bohmer 
227223cd1385SRemy Bohmer 	/* finish hookup to lower layer ... */
227323cd1385SRemy Bohmer 	dev->gadget = gadget;
227423cd1385SRemy Bohmer 	set_gadget_data(gadget, dev);
227523cd1385SRemy Bohmer 	gadget->ep0->driver_data = dev;
227623cd1385SRemy Bohmer 
22776142e0aeSVitaly Kuzmichev 	/*
22786142e0aeSVitaly Kuzmichev 	 * two kinds of host-initiated state changes:
227923cd1385SRemy Bohmer 	 *  - iff DATA transfer is active, carrier is "on"
228023cd1385SRemy Bohmer 	 *  - tx queueing enabled if open *and* carrier is "on"
228123cd1385SRemy Bohmer 	 */
22827612a43dSVitaly Kuzmichev 
22837612a43dSVitaly Kuzmichev 	printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
22847612a43dSVitaly Kuzmichev 		out_ep->name, in_ep->name,
22857612a43dSVitaly Kuzmichev 		status_ep ? " STATUS " : "",
22867612a43dSVitaly Kuzmichev 		status_ep ? status_ep->name : ""
22877612a43dSVitaly Kuzmichev 		);
2288d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
2289d4a37553SMugunthan V N 	printf("MAC %pM\n", dev->net->enetaddr);
2290d4a37553SMugunthan V N #else
2291d4a37553SMugunthan V N 	printf("MAC %pM\n", pdata->enetaddr);
2292d4a37553SMugunthan V N #endif
22937612a43dSVitaly Kuzmichev 
22947612a43dSVitaly Kuzmichev 	if (cdc || rndis)
22957612a43dSVitaly Kuzmichev 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
22967612a43dSVitaly Kuzmichev 			dev->host_mac[0], dev->host_mac[1],
22977612a43dSVitaly Kuzmichev 			dev->host_mac[2], dev->host_mac[3],
22987612a43dSVitaly Kuzmichev 			dev->host_mac[4], dev->host_mac[5]);
22997612a43dSVitaly Kuzmichev 
23007612a43dSVitaly Kuzmichev 	if (rndis) {
23017612a43dSVitaly Kuzmichev 		u32	vendorID = 0;
23027612a43dSVitaly Kuzmichev 
23037612a43dSVitaly Kuzmichev 		/* FIXME RNDIS vendor id == "vendor NIC code" == ? */
23047612a43dSVitaly Kuzmichev 
23057612a43dSVitaly Kuzmichev 		dev->rndis_config = rndis_register(rndis_control_ack);
23067612a43dSVitaly Kuzmichev 		if (dev->rndis_config < 0) {
23077612a43dSVitaly Kuzmichev fail0:
23087612a43dSVitaly Kuzmichev 			eth_unbind(gadget);
23097612a43dSVitaly Kuzmichev 			debug("RNDIS setup failed\n");
23107612a43dSVitaly Kuzmichev 			status = -ENODEV;
23117612a43dSVitaly Kuzmichev 			goto fail;
23127612a43dSVitaly Kuzmichev 		}
23137612a43dSVitaly Kuzmichev 
23147612a43dSVitaly Kuzmichev 		/* these set up a lot of the OIDs that RNDIS needs */
23157612a43dSVitaly Kuzmichev 		rndis_set_host_mac(dev->rndis_config, dev->host_mac);
23167612a43dSVitaly Kuzmichev 		if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
23177612a43dSVitaly Kuzmichev 					&dev->stats, &dev->cdc_filter))
23187612a43dSVitaly Kuzmichev 			goto fail0;
23197612a43dSVitaly Kuzmichev 		if (rndis_set_param_vendor(dev->rndis_config, vendorID,
23207612a43dSVitaly Kuzmichev 					manufacturer))
23217612a43dSVitaly Kuzmichev 			goto fail0;
23227612a43dSVitaly Kuzmichev 		if (rndis_set_param_medium(dev->rndis_config,
23237612a43dSVitaly Kuzmichev 					NDIS_MEDIUM_802_3, 0))
23247612a43dSVitaly Kuzmichev 			goto fail0;
23257612a43dSVitaly Kuzmichev 		printf("RNDIS ready\n");
23267612a43dSVitaly Kuzmichev 	}
232723cd1385SRemy Bohmer 	return 0;
232823cd1385SRemy Bohmer 
232923cd1385SRemy Bohmer fail:
23309b643e31SMasahiro Yamada 	pr_err("%s failed, status = %d", __func__, status);
233123cd1385SRemy Bohmer 	eth_unbind(gadget);
23327612a43dSVitaly Kuzmichev 	return status;
233323cd1385SRemy Bohmer }
233423cd1385SRemy Bohmer 
23357612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
2336*1c1464c2SJean-Jacques Hiblot static void _usb_eth_halt(struct ether_priv *priv);
2337*1c1464c2SJean-Jacques Hiblot 
_usb_eth_init(struct ether_priv * priv)23388269ee4fSMugunthan V N static int _usb_eth_init(struct ether_priv *priv)
233923cd1385SRemy Bohmer {
2340ae70100cSMugunthan V N 	struct eth_dev *dev = &priv->ethdev;
234123cd1385SRemy Bohmer 	struct usb_gadget *gadget;
234223cd1385SRemy Bohmer 	unsigned long ts;
2343a06955aeSJean-Jacques Hiblot 	int ret;
234423cd1385SRemy Bohmer 	unsigned long timeout = USB_CONNECT_TIMEOUT;
234523cd1385SRemy Bohmer 
2346a06955aeSJean-Jacques Hiblot 	ret = usb_gadget_initialize(0);
2347a06955aeSJean-Jacques Hiblot 	if (ret)
2348a06955aeSJean-Jacques Hiblot 		return ret;
23498bfc288cSKishon Vijay Abraham I 
235058939fccSVitaly Kuzmichev 	/* Configure default mac-addresses for the USB ethernet device */
235158939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR
235258939fccSVitaly Kuzmichev 	strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
235358939fccSVitaly Kuzmichev #endif
235458939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR
235558939fccSVitaly Kuzmichev 	strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
235658939fccSVitaly Kuzmichev #endif
235758939fccSVitaly Kuzmichev 	/* Check if the user overruled the MAC addresses */
235800caae6dSSimon Glass 	if (env_get("usbnet_devaddr"))
235900caae6dSSimon Glass 		strlcpy(dev_addr, env_get("usbnet_devaddr"),
236058939fccSVitaly Kuzmichev 			sizeof(dev_addr));
236158939fccSVitaly Kuzmichev 
236200caae6dSSimon Glass 	if (env_get("usbnet_hostaddr"))
236300caae6dSSimon Glass 		strlcpy(host_addr, env_get("usbnet_hostaddr"),
236458939fccSVitaly Kuzmichev 			sizeof(host_addr));
236558939fccSVitaly Kuzmichev 
236658939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(dev_addr)) {
23679b643e31SMasahiro Yamada 		pr_err("Need valid 'usbnet_devaddr' to be set");
236858939fccSVitaly Kuzmichev 		goto fail;
236958939fccSVitaly Kuzmichev 	}
237058939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(host_addr)) {
23719b643e31SMasahiro Yamada 		pr_err("Need valid 'usbnet_hostaddr' to be set");
237258939fccSVitaly Kuzmichev 		goto fail;
237358939fccSVitaly Kuzmichev 	}
237458939fccSVitaly Kuzmichev 
2375ae70100cSMugunthan V N 	priv->eth_driver.speed		= DEVSPEED;
2376ae70100cSMugunthan V N 	priv->eth_driver.bind		= eth_bind;
2377ae70100cSMugunthan V N 	priv->eth_driver.unbind		= eth_unbind;
2378ae70100cSMugunthan V N 	priv->eth_driver.setup		= eth_setup;
2379ae70100cSMugunthan V N 	priv->eth_driver.reset		= eth_disconnect;
2380ae70100cSMugunthan V N 	priv->eth_driver.disconnect	= eth_disconnect;
2381ae70100cSMugunthan V N 	priv->eth_driver.suspend	= eth_suspend;
2382ae70100cSMugunthan V N 	priv->eth_driver.resume		= eth_resume;
2383ae70100cSMugunthan V N 	if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2384988ee3e3SLei Wen 		goto fail;
238523cd1385SRemy Bohmer 
238623cd1385SRemy Bohmer 	dev->network_started = 0;
238723cd1385SRemy Bohmer 
238823cd1385SRemy Bohmer 	packet_received = 0;
238923cd1385SRemy Bohmer 	packet_sent = 0;
239023cd1385SRemy Bohmer 
239123cd1385SRemy Bohmer 	gadget = dev->gadget;
239223cd1385SRemy Bohmer 	usb_gadget_connect(gadget);
239323cd1385SRemy Bohmer 
239400caae6dSSimon Glass 	if (env_get("cdc_connect_timeout"))
239500caae6dSSimon Glass 		timeout = simple_strtoul(env_get("cdc_connect_timeout"),
239623cd1385SRemy Bohmer 						NULL, 10) * CONFIG_SYS_HZ;
239723cd1385SRemy Bohmer 	ts = get_timer(0);
239817b4f308SMugunthan V N 	while (!dev->network_started) {
239923cd1385SRemy Bohmer 		/* Handle control-c and timeouts */
240023cd1385SRemy Bohmer 		if (ctrlc() || (get_timer(ts) > timeout)) {
24019b643e31SMasahiro Yamada 			pr_err("The remote end did not respond in time.");
240223cd1385SRemy Bohmer 			goto fail;
240323cd1385SRemy Bohmer 		}
24042d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
240523cd1385SRemy Bohmer 	}
240623cd1385SRemy Bohmer 
240798fae970SVitaly Kuzmichev 	packet_received = 0;
240823cd1385SRemy Bohmer 	rx_submit(dev, dev->rx_req, 0);
240923cd1385SRemy Bohmer 	return 0;
241023cd1385SRemy Bohmer fail:
2411*1c1464c2SJean-Jacques Hiblot 	_usb_eth_halt(priv);
241223cd1385SRemy Bohmer 	return -1;
241323cd1385SRemy Bohmer }
241423cd1385SRemy Bohmer 
_usb_eth_send(struct ether_priv * priv,void * packet,int length)24158269ee4fSMugunthan V N static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
241623cd1385SRemy Bohmer {
241723cd1385SRemy Bohmer 	int			retval;
24187612a43dSVitaly Kuzmichev 	void			*rndis_pkt = NULL;
2419ae70100cSMugunthan V N 	struct eth_dev		*dev = &priv->ethdev;
2420ac5d32d1SVitaly Kuzmichev 	struct usb_request	*req = dev->tx_req;
2421a170f2c7SStefano Babic 	unsigned long ts;
2422a170f2c7SStefano Babic 	unsigned long timeout = USB_CONNECT_TIMEOUT;
24237de73185SVitaly Kuzmichev 
24247de73185SVitaly Kuzmichev 	debug("%s:...\n", __func__);
242523cd1385SRemy Bohmer 
24267612a43dSVitaly Kuzmichev 	/* new buffer is needed to include RNDIS header */
24277612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
24287612a43dSVitaly Kuzmichev 		rndis_pkt = malloc(length +
24297612a43dSVitaly Kuzmichev 					sizeof(struct rndis_packet_msg_type));
24307612a43dSVitaly Kuzmichev 		if (!rndis_pkt) {
24319b643e31SMasahiro Yamada 			pr_err("No memory to alloc RNDIS packet");
24327612a43dSVitaly Kuzmichev 			goto drop;
24337612a43dSVitaly Kuzmichev 		}
24347612a43dSVitaly Kuzmichev 		rndis_add_hdr(rndis_pkt, length);
24357612a43dSVitaly Kuzmichev 		memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
243610cbe3b6SJoe Hershberger 				packet, length);
24377612a43dSVitaly Kuzmichev 		packet = rndis_pkt;
24387612a43dSVitaly Kuzmichev 		length += sizeof(struct rndis_packet_msg_type);
24397612a43dSVitaly Kuzmichev 	}
244010cbe3b6SJoe Hershberger 	req->buf = packet;
244123cd1385SRemy Bohmer 	req->context = NULL;
244223cd1385SRemy Bohmer 	req->complete = tx_complete;
244323cd1385SRemy Bohmer 
24446142e0aeSVitaly Kuzmichev 	/*
24456142e0aeSVitaly Kuzmichev 	 * use zlp framing on tx for strict CDC-Ether conformance,
244623cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
244723cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
244823cd1385SRemy Bohmer 	 */
244923cd1385SRemy Bohmer 	req->zero = 1;
245023cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
245123cd1385SRemy Bohmer 		length++;
245223cd1385SRemy Bohmer 
245323cd1385SRemy Bohmer 	req->length = length;
245423cd1385SRemy Bohmer #if 0
245523cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
245623cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
245723cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
245823cd1385SRemy Bohmer 			? ((dev->tx_qlen % qmult) != 0) : 0;
245923cd1385SRemy Bohmer #endif
246023cd1385SRemy Bohmer 	dev->tx_qlen = 1;
2461a170f2c7SStefano Babic 	ts = get_timer(0);
2462a170f2c7SStefano Babic 	packet_sent = 0;
246323cd1385SRemy Bohmer 
246423cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
246523cd1385SRemy Bohmer 
246623cd1385SRemy Bohmer 	if (!retval)
24677de73185SVitaly Kuzmichev 		debug("%s: packet queued\n", __func__);
24686142e0aeSVitaly Kuzmichev 	while (!packet_sent) {
2469a170f2c7SStefano Babic 		if (get_timer(ts) > timeout) {
2470a170f2c7SStefano Babic 			printf("timeout sending packets to usb ethernet\n");
2471a170f2c7SStefano Babic 			return -1;
2472a170f2c7SStefano Babic 		}
24732d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
247423cd1385SRemy Bohmer 	}
24757612a43dSVitaly Kuzmichev 	if (rndis_pkt)
24767612a43dSVitaly Kuzmichev 		free(rndis_pkt);
247723cd1385SRemy Bohmer 
247823cd1385SRemy Bohmer 	return 0;
24797612a43dSVitaly Kuzmichev drop:
24807612a43dSVitaly Kuzmichev 	dev->stats.tx_dropped++;
24817612a43dSVitaly Kuzmichev 	return -ENOMEM;
248223cd1385SRemy Bohmer }
248323cd1385SRemy Bohmer 
_usb_eth_recv(struct ether_priv * priv)24848269ee4fSMugunthan V N static int _usb_eth_recv(struct ether_priv *priv)
248523cd1385SRemy Bohmer {
24862d48aa69SKishon Vijay Abraham I 	usb_gadget_handle_interrupts(0);
248723cd1385SRemy Bohmer 
248823cd1385SRemy Bohmer 	return 0;
248923cd1385SRemy Bohmer }
249023cd1385SRemy Bohmer 
_usb_eth_halt(struct ether_priv * priv)2491*1c1464c2SJean-Jacques Hiblot static void _usb_eth_halt(struct ether_priv *priv)
249223cd1385SRemy Bohmer {
2493ae70100cSMugunthan V N 	struct eth_dev *dev = &priv->ethdev;
249423cd1385SRemy Bohmer 
2495988ee3e3SLei Wen 	/* If the gadget not registered, simple return */
2496988ee3e3SLei Wen 	if (!dev->gadget)
2497988ee3e3SLei Wen 		return;
2498988ee3e3SLei Wen 
2499e4ae6660SVitaly Kuzmichev 	/*
2500e4ae6660SVitaly Kuzmichev 	 * Some USB controllers may need additional deinitialization here
2501e4ae6660SVitaly Kuzmichev 	 * before dropping pull-up (also due to hardware issues).
2502e4ae6660SVitaly Kuzmichev 	 * For example: unhandled interrupt with status stage started may
2503e4ae6660SVitaly Kuzmichev 	 * bring the controller to fully broken state (until board reset).
2504e4ae6660SVitaly Kuzmichev 	 * There are some variants to debug and fix such cases:
2505e4ae6660SVitaly Kuzmichev 	 * 1) In the case of RNDIS connection eth_stop can perform additional
2506e4ae6660SVitaly Kuzmichev 	 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2507e4ae6660SVitaly Kuzmichev 	 * 2) 'pullup' callback in your UDC driver can be improved to perform
2508e4ae6660SVitaly Kuzmichev 	 * this deinitialization.
2509e4ae6660SVitaly Kuzmichev 	 */
25107612a43dSVitaly Kuzmichev 	eth_stop(dev);
25117612a43dSVitaly Kuzmichev 
251223cd1385SRemy Bohmer 	usb_gadget_disconnect(dev->gadget);
2513b3649f3bSVitaly Kuzmichev 
2514b3649f3bSVitaly Kuzmichev 	/* Clear pending interrupt */
2515b3649f3bSVitaly Kuzmichev 	if (dev->network_started) {
25162d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
2517b3649f3bSVitaly Kuzmichev 		dev->network_started = 0;
2518b3649f3bSVitaly Kuzmichev 	}
2519b3649f3bSVitaly Kuzmichev 
2520ae70100cSMugunthan V N 	usb_gadget_unregister_driver(&priv->eth_driver);
2521a06955aeSJean-Jacques Hiblot 	usb_gadget_release(0);
252223cd1385SRemy Bohmer }
252323cd1385SRemy Bohmer 
2524d4a37553SMugunthan V N #ifndef CONFIG_DM_ETH
usb_eth_init(struct eth_device * netdev,bd_t * bd)25258269ee4fSMugunthan V N static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
25268269ee4fSMugunthan V N {
25278269ee4fSMugunthan V N 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25288269ee4fSMugunthan V N 
25298269ee4fSMugunthan V N 	return _usb_eth_init(priv);
25308269ee4fSMugunthan V N }
25318269ee4fSMugunthan V N 
usb_eth_send(struct eth_device * netdev,void * packet,int length)25328269ee4fSMugunthan V N static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
25338269ee4fSMugunthan V N {
25348269ee4fSMugunthan V N 	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
25358269ee4fSMugunthan V N 
25368269ee4fSMugunthan V N 	return _usb_eth_send(priv, packet, length);
25378269ee4fSMugunthan V N }
25388269ee4fSMugunthan V N 
usb_eth_recv(struct eth_device * netdev)25398269ee4fSMugunthan V N static int usb_eth_recv(struct eth_device *netdev)
25408269ee4fSMugunthan V N {
25418269ee4fSMugunthan V N 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25428269ee4fSMugunthan V N 	struct eth_dev *dev = &priv->ethdev;
25438269ee4fSMugunthan V N 	int ret;
25448269ee4fSMugunthan V N 
25458269ee4fSMugunthan V N 	ret = _usb_eth_recv(priv);
25468269ee4fSMugunthan V N 	if (ret) {
25479b643e31SMasahiro Yamada 		pr_err("error packet receive\n");
25488269ee4fSMugunthan V N 		return ret;
25498269ee4fSMugunthan V N 	}
25508269ee4fSMugunthan V N 
25518269ee4fSMugunthan V N 	if (!packet_received)
25528269ee4fSMugunthan V N 		return 0;
25538269ee4fSMugunthan V N 
25548269ee4fSMugunthan V N 	if (dev->rx_req) {
25558269ee4fSMugunthan V N 		net_process_received_packet(net_rx_packets[0],
25568269ee4fSMugunthan V N 					    dev->rx_req->length);
25578269ee4fSMugunthan V N 	} else {
25589b643e31SMasahiro Yamada 		pr_err("dev->rx_req invalid");
25598269ee4fSMugunthan V N 	}
25608269ee4fSMugunthan V N 	packet_received = 0;
25618269ee4fSMugunthan V N 	rx_submit(dev, dev->rx_req, 0);
25628269ee4fSMugunthan V N 
25638269ee4fSMugunthan V N 	return 0;
25648269ee4fSMugunthan V N }
25658269ee4fSMugunthan V N 
usb_eth_halt(struct eth_device * netdev)25668269ee4fSMugunthan V N void usb_eth_halt(struct eth_device *netdev)
25678269ee4fSMugunthan V N {
25688269ee4fSMugunthan V N 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
25698269ee4fSMugunthan V N 
25708269ee4fSMugunthan V N 	_usb_eth_halt(priv);
25718269ee4fSMugunthan V N }
25728269ee4fSMugunthan V N 
usb_eth_initialize(bd_t * bi)257323cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi)
257423cd1385SRemy Bohmer {
25755cb3b9d7SMugunthan V N 	struct eth_device *netdev = &l_priv->netdev;
257623cd1385SRemy Bohmer 
25778f7aa831SVitaly Kuzmichev 	strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
257823cd1385SRemy Bohmer 
257923cd1385SRemy Bohmer 	netdev->init = usb_eth_init;
258023cd1385SRemy Bohmer 	netdev->send = usb_eth_send;
258123cd1385SRemy Bohmer 	netdev->recv = usb_eth_recv;
258223cd1385SRemy Bohmer 	netdev->halt = usb_eth_halt;
2583ae70100cSMugunthan V N 	netdev->priv = l_priv;
258423cd1385SRemy Bohmer 
258523cd1385SRemy Bohmer 	eth_register(netdev);
258623cd1385SRemy Bohmer 	return 0;
258723cd1385SRemy Bohmer }
2588d4a37553SMugunthan V N #else
usb_eth_start(struct udevice * dev)2589d4a37553SMugunthan V N static int usb_eth_start(struct udevice *dev)
2590d4a37553SMugunthan V N {
2591d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2592d4a37553SMugunthan V N 
2593d4a37553SMugunthan V N 	return _usb_eth_init(priv);
2594d4a37553SMugunthan V N }
2595d4a37553SMugunthan V N 
usb_eth_send(struct udevice * dev,void * packet,int length)2596d4a37553SMugunthan V N static int usb_eth_send(struct udevice *dev, void *packet, int length)
2597d4a37553SMugunthan V N {
2598d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2599d4a37553SMugunthan V N 
2600d4a37553SMugunthan V N 	return _usb_eth_send(priv, packet, length);
2601d4a37553SMugunthan V N }
2602d4a37553SMugunthan V N 
usb_eth_recv(struct udevice * dev,int flags,uchar ** packetp)2603d4a37553SMugunthan V N static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2604d4a37553SMugunthan V N {
2605d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2606d4a37553SMugunthan V N 	struct eth_dev *ethdev = &priv->ethdev;
2607d4a37553SMugunthan V N 	int ret;
2608d4a37553SMugunthan V N 
2609d4a37553SMugunthan V N 	ret = _usb_eth_recv(priv);
2610d4a37553SMugunthan V N 	if (ret) {
26119b643e31SMasahiro Yamada 		pr_err("error packet receive\n");
2612d4a37553SMugunthan V N 		return ret;
2613d4a37553SMugunthan V N 	}
2614d4a37553SMugunthan V N 
2615d4a37553SMugunthan V N 	if (packet_received) {
2616d4a37553SMugunthan V N 		if (ethdev->rx_req) {
2617d4a37553SMugunthan V N 			*packetp = (uchar *)net_rx_packets[0];
2618d4a37553SMugunthan V N 			return ethdev->rx_req->length;
2619d4a37553SMugunthan V N 		} else {
26209b643e31SMasahiro Yamada 			pr_err("dev->rx_req invalid");
2621d4a37553SMugunthan V N 			return -EFAULT;
2622d4a37553SMugunthan V N 		}
2623d4a37553SMugunthan V N 	}
2624d4a37553SMugunthan V N 
2625d4a37553SMugunthan V N 	return -EAGAIN;
2626d4a37553SMugunthan V N }
2627d4a37553SMugunthan V N 
usb_eth_free_pkt(struct udevice * dev,uchar * packet,int length)2628d4a37553SMugunthan V N static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2629d4a37553SMugunthan V N 				   int length)
2630d4a37553SMugunthan V N {
2631d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2632d4a37553SMugunthan V N 	struct eth_dev *ethdev = &priv->ethdev;
2633d4a37553SMugunthan V N 
2634d4a37553SMugunthan V N 	packet_received = 0;
2635d4a37553SMugunthan V N 
2636d4a37553SMugunthan V N 	return rx_submit(ethdev, ethdev->rx_req, 0);
2637d4a37553SMugunthan V N }
2638d4a37553SMugunthan V N 
usb_eth_stop(struct udevice * dev)2639d4a37553SMugunthan V N static void usb_eth_stop(struct udevice *dev)
2640d4a37553SMugunthan V N {
2641d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2642d4a37553SMugunthan V N 
2643d4a37553SMugunthan V N 	_usb_eth_halt(priv);
2644d4a37553SMugunthan V N }
2645d4a37553SMugunthan V N 
usb_eth_probe(struct udevice * dev)2646d4a37553SMugunthan V N static int usb_eth_probe(struct udevice *dev)
2647d4a37553SMugunthan V N {
2648d4a37553SMugunthan V N 	struct ether_priv *priv = dev_get_priv(dev);
2649d4a37553SMugunthan V N 	struct eth_pdata *pdata = dev_get_platdata(dev);
2650d4a37553SMugunthan V N 
2651d4a37553SMugunthan V N 	priv->netdev = dev;
2652d4a37553SMugunthan V N 	l_priv = priv;
2653d4a37553SMugunthan V N 
2654d4a37553SMugunthan V N 	get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2655fd1e959eSSimon Glass 	eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2656d4a37553SMugunthan V N 
2657d4a37553SMugunthan V N 	return 0;
2658d4a37553SMugunthan V N }
2659d4a37553SMugunthan V N 
2660d4a37553SMugunthan V N static const struct eth_ops usb_eth_ops = {
2661d4a37553SMugunthan V N 	.start		= usb_eth_start,
2662d4a37553SMugunthan V N 	.send		= usb_eth_send,
2663d4a37553SMugunthan V N 	.recv		= usb_eth_recv,
2664d4a37553SMugunthan V N 	.free_pkt	= usb_eth_free_pkt,
2665d4a37553SMugunthan V N 	.stop		= usb_eth_stop,
2666d4a37553SMugunthan V N };
2667d4a37553SMugunthan V N 
usb_ether_init(void)2668d4a37553SMugunthan V N int usb_ether_init(void)
2669d4a37553SMugunthan V N {
2670d4a37553SMugunthan V N 	struct udevice *dev;
2671d4a37553SMugunthan V N 	struct udevice *usb_dev;
2672d4a37553SMugunthan V N 	int ret;
2673d4a37553SMugunthan V N 
267401311624SJean-Jacques Hiblot 	ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2675d4a37553SMugunthan V N 	if (!usb_dev || ret) {
26769b643e31SMasahiro Yamada 		pr_err("No USB device found\n");
2677d4a37553SMugunthan V N 		return ret;
2678d4a37553SMugunthan V N 	}
2679d4a37553SMugunthan V N 
2680d4a37553SMugunthan V N 	ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2681d4a37553SMugunthan V N 	if (!dev || ret) {
26829b643e31SMasahiro Yamada 		pr_err("usb - not able to bind usb_ether device\n");
2683d4a37553SMugunthan V N 		return ret;
2684d4a37553SMugunthan V N 	}
2685d4a37553SMugunthan V N 
2686d4a37553SMugunthan V N 	return 0;
2687d4a37553SMugunthan V N }
2688d4a37553SMugunthan V N 
2689d4a37553SMugunthan V N U_BOOT_DRIVER(eth_usb) = {
2690d4a37553SMugunthan V N 	.name	= "usb_ether",
2691d4a37553SMugunthan V N 	.id	= UCLASS_ETH,
2692d4a37553SMugunthan V N 	.probe	= usb_eth_probe,
2693d4a37553SMugunthan V N 	.ops	= &usb_eth_ops,
2694d4a37553SMugunthan V N 	.priv_auto_alloc_size = sizeof(struct ether_priv),
2695d4a37553SMugunthan V N 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
2696d4a37553SMugunthan V N 	.flags = DM_FLAG_ALLOC_PRIV_DMA,
2697d4a37553SMugunthan V N };
2698d4a37553SMugunthan V N #endif /* CONFIG_DM_ETH */
2699