xref: /rk3399_rockchip-uboot/drivers/usb/gadget/ether.c (revision 0adb5b761f4c789ae47d8abb015f5e017263d3f2)
123cd1385SRemy Bohmer /*
223cd1385SRemy Bohmer  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
323cd1385SRemy Bohmer  *
423cd1385SRemy Bohmer  * Copyright (C) 2003-2005,2008 David Brownell
523cd1385SRemy Bohmer  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
623cd1385SRemy Bohmer  * Copyright (C) 2008 Nokia Corporation
723cd1385SRemy Bohmer  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
923cd1385SRemy Bohmer  */
1023cd1385SRemy Bohmer 
1123cd1385SRemy Bohmer #include <common.h>
1223cd1385SRemy Bohmer #include <asm/errno.h>
13c85d70efSVitaly Kuzmichev #include <linux/netdevice.h>
1423cd1385SRemy Bohmer #include <linux/usb/ch9.h>
1523cd1385SRemy Bohmer #include <linux/usb/cdc.h>
1623cd1385SRemy Bohmer #include <linux/usb/gadget.h>
1723cd1385SRemy Bohmer #include <net.h>
187612a43dSVitaly Kuzmichev #include <malloc.h>
1923cd1385SRemy Bohmer #include <linux/ctype.h>
2023cd1385SRemy Bohmer 
2123cd1385SRemy Bohmer #include "gadget_chips.h"
227612a43dSVitaly Kuzmichev #include "rndis.h"
2323cd1385SRemy Bohmer 
248f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether"
257de73185SVitaly Kuzmichev 
2623cd1385SRemy Bohmer #define atomic_read
2723cd1385SRemy Bohmer extern struct platform_data brd;
2823cd1385SRemy Bohmer 
2923cd1385SRemy Bohmer 
3023cd1385SRemy Bohmer unsigned packet_received, packet_sent;
3123cd1385SRemy Bohmer 
3223cd1385SRemy Bohmer /*
3323cd1385SRemy Bohmer  * Ethernet gadget driver -- with CDC and non-CDC options
3423cd1385SRemy Bohmer  * Builds on hardware support for a full duplex link.
3523cd1385SRemy Bohmer  *
3623cd1385SRemy Bohmer  * CDC Ethernet is the standard USB solution for sending Ethernet frames
3723cd1385SRemy Bohmer  * using USB.  Real hardware tends to use the same framing protocol but look
3823cd1385SRemy Bohmer  * different for control features.  This driver strongly prefers to use
3923cd1385SRemy Bohmer  * this USB-IF standard as its open-systems interoperability solution;
4023cd1385SRemy Bohmer  * most host side USB stacks (except from Microsoft) support it.
4123cd1385SRemy Bohmer  *
4223cd1385SRemy Bohmer  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
4323cd1385SRemy Bohmer  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
4423cd1385SRemy Bohmer  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
4523cd1385SRemy Bohmer  *
4623cd1385SRemy Bohmer  * There's some hardware that can't talk CDC ECM.  We make that hardware
4723cd1385SRemy Bohmer  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
4823cd1385SRemy Bohmer  * link-level setup only requires activating the configuration.  Only the
4923cd1385SRemy Bohmer  * endpoint descriptors, and product/vendor IDs, are relevant; no control
5023cd1385SRemy Bohmer  * operations are available.  Linux supports it, but other host operating
5123cd1385SRemy Bohmer  * systems may not.  (This is a subset of CDC Ethernet.)
5223cd1385SRemy Bohmer  *
5323cd1385SRemy Bohmer  * It turns out that if you add a few descriptors to that "CDC Subset",
5423cd1385SRemy Bohmer  * (Windows) host side drivers from MCCI can treat it as one submode of
5523cd1385SRemy Bohmer  * a proprietary scheme called "SAFE" ... without needing to know about
5623cd1385SRemy Bohmer  * specific product/vendor IDs.  So we do that, making it easier to use
5723cd1385SRemy Bohmer  * those MS-Windows drivers.  Those added descriptors make it resemble a
5823cd1385SRemy Bohmer  * CDC MDLM device, but they don't change device behavior at all.  (See
5923cd1385SRemy Bohmer  * MCCI Engineering report 950198 "SAFE Networking Functions".)
6023cd1385SRemy Bohmer  *
6123cd1385SRemy Bohmer  * A third option is also in use.  Rather than CDC Ethernet, or something
6223cd1385SRemy Bohmer  * simpler, Microsoft pushes their own approach: RNDIS.  The published
6323cd1385SRemy Bohmer  * RNDIS specs are ambiguous and appear to be incomplete, and are also
6423cd1385SRemy Bohmer  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
6523cd1385SRemy Bohmer  */
6623cd1385SRemy Bohmer #define ETH_ALEN	6		/* Octets in one ethernet addr	 */
6723cd1385SRemy Bohmer #define ETH_HLEN	14		/* Total octets in header.	 */
6823cd1385SRemy Bohmer #define ETH_ZLEN	60		/* Min. octets in frame sans FCS */
6923cd1385SRemy Bohmer #define ETH_DATA_LEN	1500		/* Max. octets in payload	 */
7023cd1385SRemy Bohmer #define ETH_FRAME_LEN	PKTSIZE_ALIGN	/* Max. octets in frame sans FCS */
7123cd1385SRemy Bohmer 
7223cd1385SRemy Bohmer #define DRIVER_DESC		"Ethernet Gadget"
7323cd1385SRemy Bohmer /* Based on linux 2.6.27 version */
7423cd1385SRemy Bohmer #define DRIVER_VERSION		"May Day 2005"
7523cd1385SRemy Bohmer 
7623cd1385SRemy Bohmer static const char shortname[] = "ether";
7723cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC;
7823cd1385SRemy Bohmer 
7923cd1385SRemy Bohmer #define RX_EXTRA	20		/* guard against rx overflows */
8023cd1385SRemy Bohmer 
817612a43dSVitaly Kuzmichev #ifndef	CONFIG_USB_ETH_RNDIS
827612a43dSVitaly Kuzmichev #define rndis_uninit(x)		do {} while (0)
837612a43dSVitaly Kuzmichev #define rndis_deregister(c)	do {} while (0)
847612a43dSVitaly Kuzmichev #define rndis_exit()		do {} while (0)
857612a43dSVitaly Kuzmichev #endif
867612a43dSVitaly Kuzmichev 
877612a43dSVitaly Kuzmichev /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
8823cd1385SRemy Bohmer #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
8923cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
9023cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
9123cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_DIRECTED)
9223cd1385SRemy Bohmer 
9323cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
9423cd1385SRemy Bohmer 
9523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
968b6b66b4SVitaly Kuzmichev 
978b6b66b4SVitaly Kuzmichev struct eth_dev {
988b6b66b4SVitaly Kuzmichev 	struct usb_gadget	*gadget;
998b6b66b4SVitaly Kuzmichev 	struct usb_request	*req;		/* for control responses */
1007612a43dSVitaly Kuzmichev 	struct usb_request	*stat_req;	/* for cdc & rndis status */
1018b6b66b4SVitaly Kuzmichev 
1028b6b66b4SVitaly Kuzmichev 	u8			config;
1038b6b66b4SVitaly Kuzmichev 	struct usb_ep		*in_ep, *out_ep, *status_ep;
1048b6b66b4SVitaly Kuzmichev 	const struct usb_endpoint_descriptor
1058b6b66b4SVitaly Kuzmichev 				*in, *out, *status;
1068b6b66b4SVitaly Kuzmichev 
1078b6b66b4SVitaly Kuzmichev 	struct usb_request	*tx_req, *rx_req;
1088b6b66b4SVitaly Kuzmichev 
1098b6b66b4SVitaly Kuzmichev 	struct eth_device	*net;
1108b6b66b4SVitaly Kuzmichev 	struct net_device_stats	stats;
1118b6b66b4SVitaly Kuzmichev 	unsigned int		tx_qlen;
1128b6b66b4SVitaly Kuzmichev 
1138b6b66b4SVitaly Kuzmichev 	unsigned		zlp:1;
1148b6b66b4SVitaly Kuzmichev 	unsigned		cdc:1;
1157612a43dSVitaly Kuzmichev 	unsigned		rndis:1;
1168b6b66b4SVitaly Kuzmichev 	unsigned		suspended:1;
1178b6b66b4SVitaly Kuzmichev 	unsigned		network_started:1;
1188b6b66b4SVitaly Kuzmichev 	u16			cdc_filter;
1198b6b66b4SVitaly Kuzmichev 	unsigned long		todo;
1208b6b66b4SVitaly Kuzmichev 	int			mtu;
1218b6b66b4SVitaly Kuzmichev #define	WORK_RX_MEMORY		0
1227612a43dSVitaly Kuzmichev 	int			rndis_config;
1238b6b66b4SVitaly Kuzmichev 	u8			host_mac[ETH_ALEN];
1248b6b66b4SVitaly Kuzmichev };
1258b6b66b4SVitaly Kuzmichev 
1268b6b66b4SVitaly Kuzmichev /*
1278b6b66b4SVitaly Kuzmichev  * This version autoconfigures as much as possible at run-time.
1288b6b66b4SVitaly Kuzmichev  *
1298b6b66b4SVitaly Kuzmichev  * It also ASSUMES a self-powered device, without remote wakeup,
1308b6b66b4SVitaly Kuzmichev  * although remote wakeup support would make sense.
1318b6b66b4SVitaly Kuzmichev  */
1328b6b66b4SVitaly Kuzmichev 
1338b6b66b4SVitaly Kuzmichev /*-------------------------------------------------------------------------*/
13423cd1385SRemy Bohmer static struct eth_dev l_ethdev;
13523cd1385SRemy Bohmer static struct eth_device l_netdev;
13623cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver;
13723cd1385SRemy Bohmer 
13823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
13923cd1385SRemy Bohmer 
14023cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */
14123cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev)
14223cd1385SRemy Bohmer {
1432bb37884SLukasz Dalek #if	!defined(CONFIG_USB_ETH_SUBSET)
14423cd1385SRemy Bohmer 	return 1;		/* only cdc possible */
1452bb37884SLukasz Dalek #elif	!defined(CONFIG_USB_ETH_CDC)
14623cd1385SRemy Bohmer 	return 0;		/* only subset possible */
14723cd1385SRemy Bohmer #else
14823cd1385SRemy Bohmer 	return dev->cdc;	/* depends on what hardware we found */
14923cd1385SRemy Bohmer #endif
15023cd1385SRemy Bohmer }
15123cd1385SRemy Bohmer 
1527612a43dSVitaly Kuzmichev /* "secondary" RNDIS config may sometimes be activated */
1537612a43dSVitaly Kuzmichev static inline int rndis_active(struct eth_dev *dev)
1547612a43dSVitaly Kuzmichev {
1557612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
1567612a43dSVitaly Kuzmichev 	return dev->rndis;
1577612a43dSVitaly Kuzmichev #else
1587612a43dSVitaly Kuzmichev 	return 0;
1597612a43dSVitaly Kuzmichev #endif
1607612a43dSVitaly Kuzmichev }
1617612a43dSVitaly Kuzmichev 
1627612a43dSVitaly Kuzmichev #define	subset_active(dev)	(!is_cdc(dev) && !rndis_active(dev))
1637612a43dSVitaly Kuzmichev #define	cdc_active(dev)		(is_cdc(dev) && !rndis_active(dev))
16423cd1385SRemy Bohmer 
16523cd1385SRemy Bohmer #define DEFAULT_QLEN	2	/* double buffering by default */
16623cd1385SRemy Bohmer 
16723cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */
16823cd1385SRemy Bohmer #define	HS_BPS		(13 * 512 * 8 * 1000 * 8)
16923cd1385SRemy Bohmer #define	FS_BPS		(19 *  64 * 1 * 1000 * 8)
17023cd1385SRemy Bohmer 
17123cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
17223cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_HIGH
17323cd1385SRemy Bohmer 
1742721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT
1752721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT
1762721dbf1SVitaly Kuzmichev #else
1772721dbf1SVitaly Kuzmichev #define qmult 5
1782721dbf1SVitaly Kuzmichev #endif
1792721dbf1SVitaly Kuzmichev 
18023cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */
18123cd1385SRemy Bohmer #define qlen(gadget) \
18223cd1385SRemy Bohmer 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
18323cd1385SRemy Bohmer 
18423cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
18523cd1385SRemy Bohmer {
18623cd1385SRemy Bohmer 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
18723cd1385SRemy Bohmer }
18823cd1385SRemy Bohmer 
18923cd1385SRemy Bohmer #else	/* full speed (low speed doesn't do bulk) */
19023cd1385SRemy Bohmer 
19123cd1385SRemy Bohmer #define qmult		1
19223cd1385SRemy Bohmer 
19323cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_FULL
19423cd1385SRemy Bohmer 
19523cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN
19623cd1385SRemy Bohmer 
19723cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
19823cd1385SRemy Bohmer {
19923cd1385SRemy Bohmer 	return FS_BPS;
20023cd1385SRemy Bohmer }
20123cd1385SRemy Bohmer #endif
20223cd1385SRemy Bohmer 
20323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
20423cd1385SRemy Bohmer 
2056142e0aeSVitaly Kuzmichev /*
2066142e0aeSVitaly Kuzmichev  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
20723cd1385SRemy Bohmer  * Instead:  allocate your own, using normal USB-IF procedures.
20823cd1385SRemy Bohmer  */
20923cd1385SRemy Bohmer 
2106142e0aeSVitaly Kuzmichev /*
2116142e0aeSVitaly Kuzmichev  * Thanks to NetChip Technologies for donating this product ID.
21223cd1385SRemy Bohmer  * It's for devices with only CDC Ethernet configurations.
21323cd1385SRemy Bohmer  */
21423cd1385SRemy Bohmer #define CDC_VENDOR_NUM		0x0525	/* NetChip */
21523cd1385SRemy Bohmer #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
21623cd1385SRemy Bohmer 
2176142e0aeSVitaly Kuzmichev /*
2186142e0aeSVitaly Kuzmichev  * For hardware that can't talk CDC, we use the same vendor ID that
21923cd1385SRemy Bohmer  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
22023cd1385SRemy Bohmer  * with pxa250.  We're protocol-compatible, if the host-side drivers
22123cd1385SRemy Bohmer  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
22223cd1385SRemy Bohmer  * drivers that need to hard-wire endpoint numbers have a hook.
22323cd1385SRemy Bohmer  *
22423cd1385SRemy Bohmer  * The protocol is a minimal subset of CDC Ether, which works on any bulk
22523cd1385SRemy Bohmer  * hardware that's not deeply broken ... even on hardware that can't talk
22623cd1385SRemy Bohmer  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
22723cd1385SRemy Bohmer  * doesn't handle control-OUT).
22823cd1385SRemy Bohmer  */
2297612a43dSVitaly Kuzmichev #define	SIMPLE_VENDOR_NUM	0x049f	/* Compaq Computer Corp. */
2307612a43dSVitaly Kuzmichev #define	SIMPLE_PRODUCT_NUM	0x505a	/* Linux-USB "CDC Subset" Device */
2317612a43dSVitaly Kuzmichev 
2327612a43dSVitaly Kuzmichev /*
2337612a43dSVitaly Kuzmichev  * For hardware that can talk RNDIS and either of the above protocols,
2347612a43dSVitaly Kuzmichev  * use this ID ... the windows INF files will know it.  Unless it's
2357612a43dSVitaly Kuzmichev  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
2367612a43dSVitaly Kuzmichev  * the non-RNDIS configuration.
2377612a43dSVitaly Kuzmichev  */
2387612a43dSVitaly Kuzmichev #define RNDIS_VENDOR_NUM	0x0525	/* NetChip */
2397612a43dSVitaly Kuzmichev #define RNDIS_PRODUCT_NUM	0xa4a2	/* Ethernet/RNDIS Gadget */
24023cd1385SRemy Bohmer 
2416142e0aeSVitaly Kuzmichev /*
2426142e0aeSVitaly Kuzmichev  * Some systems will want different product identifers published in the
24323cd1385SRemy Bohmer  * device descriptor, either numbers or strings or both.  These string
24423cd1385SRemy Bohmer  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
24523cd1385SRemy Bohmer  */
24623cd1385SRemy Bohmer 
2477612a43dSVitaly Kuzmichev /*
2487612a43dSVitaly Kuzmichev  * Emulating them in eth_bind:
2497612a43dSVitaly Kuzmichev  * static ushort idVendor;
2507612a43dSVitaly Kuzmichev  * static ushort idProduct;
2517612a43dSVitaly Kuzmichev  */
2527612a43dSVitaly Kuzmichev 
25323cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER)
25423cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
25523cd1385SRemy Bohmer #else
25623cd1385SRemy Bohmer static char *iManufacturer = "U-boot";
25723cd1385SRemy Bohmer #endif
2587612a43dSVitaly Kuzmichev 
2597612a43dSVitaly Kuzmichev /* These probably need to be configurable. */
2607612a43dSVitaly Kuzmichev static ushort bcdDevice;
26123cd1385SRemy Bohmer static char *iProduct;
26223cd1385SRemy Bohmer static char *iSerialNumber;
2637612a43dSVitaly Kuzmichev 
26423cd1385SRemy Bohmer static char dev_addr[18];
2657612a43dSVitaly Kuzmichev 
26623cd1385SRemy Bohmer static char host_addr[18];
26723cd1385SRemy Bohmer 
2687612a43dSVitaly Kuzmichev 
26923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
27023cd1385SRemy Bohmer 
2716142e0aeSVitaly Kuzmichev /*
2726142e0aeSVitaly Kuzmichev  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
27323cd1385SRemy Bohmer  * ep0 implementation:  descriptors, config management, setup().
27423cd1385SRemy Bohmer  * also optional class-specific notification interrupt transfer.
27523cd1385SRemy Bohmer  */
27623cd1385SRemy Bohmer 
27723cd1385SRemy Bohmer /*
27823cd1385SRemy Bohmer  * DESCRIPTORS ... most are static, but strings and (full) configuration
27923cd1385SRemy Bohmer  * descriptors are built on demand.  For now we do either full CDC, or
2807612a43dSVitaly Kuzmichev  * our simple subset, with RNDIS as an optional second configuration.
2817612a43dSVitaly Kuzmichev  *
2827612a43dSVitaly Kuzmichev  * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet.  But
2837612a43dSVitaly Kuzmichev  * the class descriptors match a modem (they're ignored; it's really just
2847612a43dSVitaly Kuzmichev  * Ethernet functionality), they don't need the NOP altsetting, and the
2857612a43dSVitaly Kuzmichev  * status transfer endpoint isn't optional.
28623cd1385SRemy Bohmer  */
28723cd1385SRemy Bohmer 
28823cd1385SRemy Bohmer #define STRING_MANUFACTURER		1
28923cd1385SRemy Bohmer #define STRING_PRODUCT			2
29023cd1385SRemy Bohmer #define STRING_ETHADDR			3
29123cd1385SRemy Bohmer #define STRING_DATA			4
29223cd1385SRemy Bohmer #define STRING_CONTROL			5
2937612a43dSVitaly Kuzmichev #define STRING_RNDIS_CONTROL		6
29423cd1385SRemy Bohmer #define STRING_CDC			7
29523cd1385SRemy Bohmer #define STRING_SUBSET			8
2967612a43dSVitaly Kuzmichev #define STRING_RNDIS			9
29723cd1385SRemy Bohmer #define STRING_SERIALNUMBER		10
29823cd1385SRemy Bohmer 
2997612a43dSVitaly Kuzmichev /* holds our biggest descriptor (or RNDIS response) */
30023cd1385SRemy Bohmer #define USB_BUFSIZ	256
30123cd1385SRemy Bohmer 
30223cd1385SRemy Bohmer /*
3037612a43dSVitaly Kuzmichev  * This device advertises one configuration, eth_config, unless RNDIS
3047612a43dSVitaly Kuzmichev  * is enabled (rndis_config) on hardware supporting at least two configs.
3057612a43dSVitaly Kuzmichev  *
3067612a43dSVitaly Kuzmichev  * NOTE:  Controllers like superh_udc should probably be able to use
3077612a43dSVitaly Kuzmichev  * an RNDIS-only configuration.
30823cd1385SRemy Bohmer  *
30923cd1385SRemy Bohmer  * FIXME define some higher-powered configurations to make it easier
31023cd1385SRemy Bohmer  * to recharge batteries ...
31123cd1385SRemy Bohmer  */
31223cd1385SRemy Bohmer 
31323cd1385SRemy Bohmer #define DEV_CONFIG_VALUE	1	/* cdc or subset */
3147612a43dSVitaly Kuzmichev #define DEV_RNDIS_CONFIG_VALUE	2	/* rndis; optional */
31523cd1385SRemy Bohmer 
31623cd1385SRemy Bohmer static struct usb_device_descriptor
31723cd1385SRemy Bohmer device_desc = {
31823cd1385SRemy Bohmer 	.bLength =		sizeof device_desc,
31923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE,
32023cd1385SRemy Bohmer 
32123cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
32223cd1385SRemy Bohmer 
32323cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
32423cd1385SRemy Bohmer 	.bDeviceSubClass =	0,
32523cd1385SRemy Bohmer 	.bDeviceProtocol =	0,
32623cd1385SRemy Bohmer 
32723cd1385SRemy Bohmer 	.idVendor =		__constant_cpu_to_le16(CDC_VENDOR_NUM),
32823cd1385SRemy Bohmer 	.idProduct =		__constant_cpu_to_le16(CDC_PRODUCT_NUM),
32923cd1385SRemy Bohmer 	.iManufacturer =	STRING_MANUFACTURER,
33023cd1385SRemy Bohmer 	.iProduct =		STRING_PRODUCT,
33123cd1385SRemy Bohmer 	.bNumConfigurations =	1,
33223cd1385SRemy Bohmer };
33323cd1385SRemy Bohmer 
33423cd1385SRemy Bohmer static struct usb_otg_descriptor
33523cd1385SRemy Bohmer otg_descriptor = {
33623cd1385SRemy Bohmer 	.bLength =		sizeof otg_descriptor,
33723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_OTG,
33823cd1385SRemy Bohmer 
33923cd1385SRemy Bohmer 	.bmAttributes =		USB_OTG_SRP,
34023cd1385SRemy Bohmer };
34123cd1385SRemy Bohmer 
34223cd1385SRemy Bohmer static struct usb_config_descriptor
34323cd1385SRemy Bohmer eth_config = {
34423cd1385SRemy Bohmer 	.bLength =		sizeof eth_config,
34523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CONFIG,
34623cd1385SRemy Bohmer 
34723cd1385SRemy Bohmer 	/* compute wTotalLength on the fly */
34823cd1385SRemy Bohmer 	.bNumInterfaces =	2,
34923cd1385SRemy Bohmer 	.bConfigurationValue =	DEV_CONFIG_VALUE,
35023cd1385SRemy Bohmer 	.iConfiguration =	STRING_CDC,
35123cd1385SRemy Bohmer 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
35223cd1385SRemy Bohmer 	.bMaxPower =		1,
35323cd1385SRemy Bohmer };
35423cd1385SRemy Bohmer 
3557612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
3567612a43dSVitaly Kuzmichev static struct usb_config_descriptor
3577612a43dSVitaly Kuzmichev rndis_config = {
3587612a43dSVitaly Kuzmichev 	.bLength =              sizeof rndis_config,
3597612a43dSVitaly Kuzmichev 	.bDescriptorType =      USB_DT_CONFIG,
3607612a43dSVitaly Kuzmichev 
3617612a43dSVitaly Kuzmichev 	/* compute wTotalLength on the fly */
3627612a43dSVitaly Kuzmichev 	.bNumInterfaces =       2,
3637612a43dSVitaly Kuzmichev 	.bConfigurationValue =  DEV_RNDIS_CONFIG_VALUE,
3647612a43dSVitaly Kuzmichev 	.iConfiguration =       STRING_RNDIS,
3657612a43dSVitaly Kuzmichev 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
3667612a43dSVitaly Kuzmichev 	.bMaxPower =            1,
3677612a43dSVitaly Kuzmichev };
3687612a43dSVitaly Kuzmichev #endif
3697612a43dSVitaly Kuzmichev 
37023cd1385SRemy Bohmer /*
37123cd1385SRemy Bohmer  * Compared to the simple CDC subset, the full CDC Ethernet model adds
37223cd1385SRemy Bohmer  * three class descriptors, two interface descriptors, optional status
37323cd1385SRemy Bohmer  * endpoint.  Both have a "data" interface and two bulk endpoints.
37423cd1385SRemy Bohmer  * There are also differences in how control requests are handled.
3757612a43dSVitaly Kuzmichev  *
3767612a43dSVitaly Kuzmichev  * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
3777612a43dSVitaly Kuzmichev  * CDC-ACM (modem) spec.  Unfortunately MSFT's RNDIS driver is buggy; it
3787612a43dSVitaly Kuzmichev  * may hang or oops.  Since bugfixes (or accurate specs, letting Linux
3797612a43dSVitaly Kuzmichev  * work around those bugs) are unlikely to ever come from MSFT, you may
3807612a43dSVitaly Kuzmichev  * wish to avoid using RNDIS.
3817612a43dSVitaly Kuzmichev  *
3827612a43dSVitaly Kuzmichev  * MCCI offers an alternative to RNDIS if you need to connect to Windows
3837612a43dSVitaly Kuzmichev  * but have hardware that can't support CDC Ethernet.   We add descriptors
3847612a43dSVitaly Kuzmichev  * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
3857612a43dSVitaly Kuzmichev  * "SAFE".  That borrows from both CDC Ethernet and CDC MDLM.  You can
3867612a43dSVitaly Kuzmichev  * get those drivers from MCCI, or bundled with various products.
38723cd1385SRemy Bohmer  */
38823cd1385SRemy Bohmer 
3892bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
39023cd1385SRemy Bohmer static struct usb_interface_descriptor
39123cd1385SRemy Bohmer control_intf = {
39223cd1385SRemy Bohmer 	.bLength =		sizeof control_intf,
39323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
39423cd1385SRemy Bohmer 
39523cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
39623cd1385SRemy Bohmer 	/* status endpoint is optional; this may be patched later */
39723cd1385SRemy Bohmer 	.bNumEndpoints =	1,
39823cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_COMM,
39923cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
40023cd1385SRemy Bohmer 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
40123cd1385SRemy Bohmer 	.iInterface =		STRING_CONTROL,
40223cd1385SRemy Bohmer };
40323cd1385SRemy Bohmer #endif
40423cd1385SRemy Bohmer 
4057612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
4067612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
4077612a43dSVitaly Kuzmichev rndis_control_intf = {
4087612a43dSVitaly Kuzmichev 	.bLength =              sizeof rndis_control_intf,
4097612a43dSVitaly Kuzmichev 	.bDescriptorType =      USB_DT_INTERFACE,
4107612a43dSVitaly Kuzmichev 
4117612a43dSVitaly Kuzmichev 	.bInterfaceNumber =     0,
4127612a43dSVitaly Kuzmichev 	.bNumEndpoints =        1,
4137612a43dSVitaly Kuzmichev 	.bInterfaceClass =      USB_CLASS_COMM,
4147612a43dSVitaly Kuzmichev 	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
4157612a43dSVitaly Kuzmichev 	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
4167612a43dSVitaly Kuzmichev 	.iInterface =           STRING_RNDIS_CONTROL,
4177612a43dSVitaly Kuzmichev };
4187612a43dSVitaly Kuzmichev #endif
4197612a43dSVitaly Kuzmichev 
42023cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = {
42123cd1385SRemy Bohmer 	.bLength =		sizeof header_desc,
42223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
42323cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
42423cd1385SRemy Bohmer 
42523cd1385SRemy Bohmer 	.bcdCDC =		__constant_cpu_to_le16(0x0110),
42623cd1385SRemy Bohmer };
42723cd1385SRemy Bohmer 
4282bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
42923cd1385SRemy Bohmer 
43023cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = {
43123cd1385SRemy Bohmer 	.bLength =		sizeof union_desc,
43223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
43323cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
43423cd1385SRemy Bohmer 
43523cd1385SRemy Bohmer 	.bMasterInterface0 =	0,	/* index of control interface */
43623cd1385SRemy Bohmer 	.bSlaveInterface0 =	1,	/* index of DATA interface */
43723cd1385SRemy Bohmer };
43823cd1385SRemy Bohmer 
4397612a43dSVitaly Kuzmichev #endif	/* CDC || RNDIS */
4407612a43dSVitaly Kuzmichev 
4417612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
4427612a43dSVitaly Kuzmichev 
4437612a43dSVitaly Kuzmichev static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
4447612a43dSVitaly Kuzmichev 	.bLength =		sizeof call_mgmt_descriptor,
4457612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_CS_INTERFACE,
4467612a43dSVitaly Kuzmichev 	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
4477612a43dSVitaly Kuzmichev 
4487612a43dSVitaly Kuzmichev 	.bmCapabilities =	0x00,
4497612a43dSVitaly Kuzmichev 	.bDataInterface =	0x01,
4507612a43dSVitaly Kuzmichev };
4517612a43dSVitaly Kuzmichev 
4527612a43dSVitaly Kuzmichev static const struct usb_cdc_acm_descriptor acm_descriptor = {
4537612a43dSVitaly Kuzmichev 	.bLength =		sizeof acm_descriptor,
4547612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_CS_INTERFACE,
4557612a43dSVitaly Kuzmichev 	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
4567612a43dSVitaly Kuzmichev 
4577612a43dSVitaly Kuzmichev 	.bmCapabilities =	0x00,
4587612a43dSVitaly Kuzmichev };
4597612a43dSVitaly Kuzmichev 
4607612a43dSVitaly Kuzmichev #endif
46123cd1385SRemy Bohmer 
4622bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_CDC
46323cd1385SRemy Bohmer 
4646142e0aeSVitaly Kuzmichev /*
4656142e0aeSVitaly Kuzmichev  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
46623cd1385SRemy Bohmer  * ways:  data endpoints live in the control interface, there's no data
46723cd1385SRemy Bohmer  * interface, and it's not used to talk to a cell phone radio.
46823cd1385SRemy Bohmer  */
46923cd1385SRemy Bohmer 
47023cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = {
47123cd1385SRemy Bohmer 	.bLength =		sizeof mdlm_desc,
47223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
47323cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_MDLM_TYPE,
47423cd1385SRemy Bohmer 
47523cd1385SRemy Bohmer 	.bcdVersion =		__constant_cpu_to_le16(0x0100),
47623cd1385SRemy Bohmer 	.bGUID = {
47723cd1385SRemy Bohmer 		0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
47823cd1385SRemy Bohmer 		0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
47923cd1385SRemy Bohmer 	},
48023cd1385SRemy Bohmer };
48123cd1385SRemy Bohmer 
4826142e0aeSVitaly Kuzmichev /*
4836142e0aeSVitaly Kuzmichev  * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
48423cd1385SRemy Bohmer  * can't really use its struct.  All we do here is say that we're using
48523cd1385SRemy Bohmer  * the submode of "SAFE" which directly matches the CDC Subset.
48623cd1385SRemy Bohmer  */
48723cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = {
48823cd1385SRemy Bohmer 	6,
48923cd1385SRemy Bohmer 	USB_DT_CS_INTERFACE,
49023cd1385SRemy Bohmer 	USB_CDC_MDLM_DETAIL_TYPE,
49123cd1385SRemy Bohmer 
49223cd1385SRemy Bohmer 	0,	/* "SAFE" */
49323cd1385SRemy Bohmer 	0,	/* network control capabilities (none) */
49423cd1385SRemy Bohmer 	0,	/* network data capabilities ("raw" encapsulation) */
49523cd1385SRemy Bohmer };
49623cd1385SRemy Bohmer 
49723cd1385SRemy Bohmer #endif
49823cd1385SRemy Bohmer 
49923cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = {
50023cd1385SRemy Bohmer 	.bLength =		sizeof(ether_desc),
50123cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
50223cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
50323cd1385SRemy Bohmer 
50423cd1385SRemy Bohmer 	/* this descriptor actually adds value, surprise! */
50523cd1385SRemy Bohmer 	.iMACAddress =		STRING_ETHADDR,
50623cd1385SRemy Bohmer 	.bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
50723cd1385SRemy Bohmer 	.wMaxSegmentSize =	__constant_cpu_to_le16(ETH_FRAME_LEN),
50823cd1385SRemy Bohmer 	.wNumberMCFilters =	__constant_cpu_to_le16(0),
50923cd1385SRemy Bohmer 	.bNumberPowerFilters =	0,
51023cd1385SRemy Bohmer };
51123cd1385SRemy Bohmer 
5122bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
51323cd1385SRemy Bohmer 
5146142e0aeSVitaly Kuzmichev /*
5156142e0aeSVitaly Kuzmichev  * include the status endpoint if we can, even where it's optional.
51623cd1385SRemy Bohmer  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
51723cd1385SRemy Bohmer  * packet, to simplify cancellation; and a big transfer interval, to
51823cd1385SRemy Bohmer  * waste less bandwidth.
51923cd1385SRemy Bohmer  *
52023cd1385SRemy Bohmer  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
52123cd1385SRemy Bohmer  * if they ignore the connect/disconnect notifications that real aether
52223cd1385SRemy Bohmer  * can provide.  more advanced cdc configurations might want to support
52323cd1385SRemy Bohmer  * encapsulated commands (vendor-specific, using control-OUT).
5247612a43dSVitaly Kuzmichev  *
5257612a43dSVitaly Kuzmichev  * RNDIS requires the status endpoint, since it uses that encapsulation
5267612a43dSVitaly Kuzmichev  * mechanism for its funky RPC scheme.
52723cd1385SRemy Bohmer  */
52823cd1385SRemy Bohmer 
52923cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
53023cd1385SRemy Bohmer #define STATUS_BYTECOUNT		16	/* 8 byte header + data */
53123cd1385SRemy Bohmer 
53223cd1385SRemy Bohmer static struct usb_endpoint_descriptor
53323cd1385SRemy Bohmer fs_status_desc = {
53423cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
53523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
53623cd1385SRemy Bohmer 
53723cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
53823cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
53923cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
54023cd1385SRemy Bohmer 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
54123cd1385SRemy Bohmer };
54223cd1385SRemy Bohmer #endif
54323cd1385SRemy Bohmer 
5442bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
54523cd1385SRemy Bohmer 
54623cd1385SRemy Bohmer /* the default data interface has no endpoints ... */
54723cd1385SRemy Bohmer 
54823cd1385SRemy Bohmer static const struct usb_interface_descriptor
54923cd1385SRemy Bohmer data_nop_intf = {
55023cd1385SRemy Bohmer 	.bLength =		sizeof data_nop_intf,
55123cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
55223cd1385SRemy Bohmer 
55323cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
55423cd1385SRemy Bohmer 	.bAlternateSetting =	0,
55523cd1385SRemy Bohmer 	.bNumEndpoints =	0,
55623cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
55723cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
55823cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
55923cd1385SRemy Bohmer };
56023cd1385SRemy Bohmer 
56123cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */
56223cd1385SRemy Bohmer 
56323cd1385SRemy Bohmer static const struct usb_interface_descriptor
56423cd1385SRemy Bohmer data_intf = {
56523cd1385SRemy Bohmer 	.bLength =		sizeof data_intf,
56623cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
56723cd1385SRemy Bohmer 
56823cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
56923cd1385SRemy Bohmer 	.bAlternateSetting =	1,
57023cd1385SRemy Bohmer 	.bNumEndpoints =	2,
57123cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
57223cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
57323cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
57423cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
57523cd1385SRemy Bohmer };
57623cd1385SRemy Bohmer 
57723cd1385SRemy Bohmer #endif
57823cd1385SRemy Bohmer 
5797612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
5807612a43dSVitaly Kuzmichev 
5817612a43dSVitaly Kuzmichev /* RNDIS doesn't activate by changing to the "real" altsetting */
5827612a43dSVitaly Kuzmichev 
5837612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor
5847612a43dSVitaly Kuzmichev rndis_data_intf = {
5857612a43dSVitaly Kuzmichev 	.bLength =		sizeof rndis_data_intf,
5867612a43dSVitaly Kuzmichev 	.bDescriptorType =	USB_DT_INTERFACE,
5877612a43dSVitaly Kuzmichev 
5887612a43dSVitaly Kuzmichev 	.bInterfaceNumber =	1,
5897612a43dSVitaly Kuzmichev 	.bAlternateSetting =	0,
5907612a43dSVitaly Kuzmichev 	.bNumEndpoints =	2,
5917612a43dSVitaly Kuzmichev 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
5927612a43dSVitaly Kuzmichev 	.bInterfaceSubClass =	0,
5937612a43dSVitaly Kuzmichev 	.bInterfaceProtocol =	0,
5947612a43dSVitaly Kuzmichev 	.iInterface =		STRING_DATA,
5957612a43dSVitaly Kuzmichev };
5967612a43dSVitaly Kuzmichev 
5977612a43dSVitaly Kuzmichev #endif
5987612a43dSVitaly Kuzmichev 
5992bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
60023cd1385SRemy Bohmer 
60123cd1385SRemy Bohmer /*
60223cd1385SRemy Bohmer  * "Simple" CDC-subset option is a simple vendor-neutral model that most
60323cd1385SRemy Bohmer  * full speed controllers can handle:  one interface, two bulk endpoints.
60423cd1385SRemy Bohmer  *
60523cd1385SRemy Bohmer  * To assist host side drivers, we fancy it up a bit, and add descriptors
60623cd1385SRemy Bohmer  * so some host side drivers will understand it as a "SAFE" variant.
60723cd1385SRemy Bohmer  */
60823cd1385SRemy Bohmer 
60923cd1385SRemy Bohmer static const struct usb_interface_descriptor
61023cd1385SRemy Bohmer subset_data_intf = {
61123cd1385SRemy Bohmer 	.bLength =		sizeof subset_data_intf,
61223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
61323cd1385SRemy Bohmer 
61423cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
61523cd1385SRemy Bohmer 	.bAlternateSetting =	0,
61623cd1385SRemy Bohmer 	.bNumEndpoints =	2,
61723cd1385SRemy Bohmer 	.bInterfaceClass =      USB_CLASS_COMM,
61823cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_MDLM,
61923cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
62023cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
62123cd1385SRemy Bohmer };
62223cd1385SRemy Bohmer 
62323cd1385SRemy Bohmer #endif	/* SUBSET */
62423cd1385SRemy Bohmer 
62523cd1385SRemy Bohmer static struct usb_endpoint_descriptor
62623cd1385SRemy Bohmer fs_source_desc = {
62723cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
62823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
62923cd1385SRemy Bohmer 
63023cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
63123cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
63243880ce5STroy Kisky 	.wMaxPacketSize =	__constant_cpu_to_le16(64),
63323cd1385SRemy Bohmer };
63423cd1385SRemy Bohmer 
63523cd1385SRemy Bohmer static struct usb_endpoint_descriptor
63623cd1385SRemy Bohmer fs_sink_desc = {
63723cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
63823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
63923cd1385SRemy Bohmer 
64023cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_OUT,
64123cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
64243880ce5STroy Kisky 	.wMaxPacketSize =	__constant_cpu_to_le16(64),
64323cd1385SRemy Bohmer };
64423cd1385SRemy Bohmer 
64523cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = {
64623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
6472bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
64823cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
64923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
65023cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
65123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
65223cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
65323cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
65423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_status_desc,
65523cd1385SRemy Bohmer 	/* data interface, with altsetting */
65623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
65723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
65823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_source_desc,
65923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_sink_desc,
66023cd1385SRemy Bohmer 	NULL,
6612bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
66223cd1385SRemy Bohmer };
66323cd1385SRemy Bohmer 
66423cd1385SRemy Bohmer static inline void fs_subset_descriptors(void)
66523cd1385SRemy Bohmer {
6662bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
66723cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
66823cd1385SRemy Bohmer 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
66923cd1385SRemy Bohmer 	fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
67023cd1385SRemy Bohmer 	fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
67123cd1385SRemy Bohmer 	fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
67223cd1385SRemy Bohmer 	fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
67323cd1385SRemy Bohmer 	fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
67423cd1385SRemy Bohmer 	fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
67523cd1385SRemy Bohmer 	fs_eth_function[8] = NULL;
67623cd1385SRemy Bohmer #else
67723cd1385SRemy Bohmer 	fs_eth_function[1] = NULL;
67823cd1385SRemy Bohmer #endif
67923cd1385SRemy Bohmer }
68023cd1385SRemy Bohmer 
6817612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
6827612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *fs_rndis_function[] = {
6837612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &otg_descriptor,
6847612a43dSVitaly Kuzmichev 	/* control interface matches ACM, not Ethernet */
6857612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_control_intf,
6867612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &header_desc,
6877612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
6887612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &acm_descriptor,
6897612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &union_desc,
6907612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_status_desc,
6917612a43dSVitaly Kuzmichev 	/* data interface has no altsetting */
6927612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_data_intf,
6937612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_source_desc,
6947612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &fs_sink_desc,
6957612a43dSVitaly Kuzmichev 	NULL,
6967612a43dSVitaly Kuzmichev };
6977612a43dSVitaly Kuzmichev #endif
6987612a43dSVitaly Kuzmichev 
69923cd1385SRemy Bohmer /*
70023cd1385SRemy Bohmer  * usb 2.0 devices need to expose both high speed and full speed
70123cd1385SRemy Bohmer  * descriptors, unless they only run at full speed.
70223cd1385SRemy Bohmer  */
70323cd1385SRemy Bohmer 
7042bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
70523cd1385SRemy Bohmer static struct usb_endpoint_descriptor
70623cd1385SRemy Bohmer hs_status_desc = {
70723cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
70823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
70923cd1385SRemy Bohmer 
71023cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
71123cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
71223cd1385SRemy Bohmer 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
71323cd1385SRemy Bohmer };
7142bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
71523cd1385SRemy Bohmer 
71623cd1385SRemy Bohmer static struct usb_endpoint_descriptor
71723cd1385SRemy Bohmer hs_source_desc = {
71823cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
71923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
72023cd1385SRemy Bohmer 
72123cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
72223cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
72323cd1385SRemy Bohmer };
72423cd1385SRemy Bohmer 
72523cd1385SRemy Bohmer static struct usb_endpoint_descriptor
72623cd1385SRemy Bohmer hs_sink_desc = {
72723cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
72823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
72923cd1385SRemy Bohmer 
73023cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
73123cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
73223cd1385SRemy Bohmer };
73323cd1385SRemy Bohmer 
73423cd1385SRemy Bohmer static struct usb_qualifier_descriptor
73523cd1385SRemy Bohmer dev_qualifier = {
73623cd1385SRemy Bohmer 	.bLength =		sizeof dev_qualifier,
73723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
73823cd1385SRemy Bohmer 
73923cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
74023cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
74123cd1385SRemy Bohmer 
74223cd1385SRemy Bohmer 	.bNumConfigurations =	1,
74323cd1385SRemy Bohmer };
74423cd1385SRemy Bohmer 
74523cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = {
74623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
7472bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
74823cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
74923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
75023cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
75123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
75223cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
75323cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
75423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_status_desc,
75523cd1385SRemy Bohmer 	/* data interface, with altsetting */
75623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
75723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
75823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_source_desc,
75923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_sink_desc,
76023cd1385SRemy Bohmer 	NULL,
7612bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
76223cd1385SRemy Bohmer };
76323cd1385SRemy Bohmer 
76423cd1385SRemy Bohmer static inline void hs_subset_descriptors(void)
76523cd1385SRemy Bohmer {
7662bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_SUBSET
76723cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
76823cd1385SRemy Bohmer 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
76923cd1385SRemy Bohmer 	hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
77023cd1385SRemy Bohmer 	hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
77123cd1385SRemy Bohmer 	hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
77223cd1385SRemy Bohmer 	hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
77323cd1385SRemy Bohmer 	hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
77423cd1385SRemy Bohmer 	hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
77523cd1385SRemy Bohmer 	hs_eth_function[8] = NULL;
77623cd1385SRemy Bohmer #else
77723cd1385SRemy Bohmer 	hs_eth_function[1] = NULL;
77823cd1385SRemy Bohmer #endif
77923cd1385SRemy Bohmer }
78023cd1385SRemy Bohmer 
7817612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
7827612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *hs_rndis_function[] = {
7837612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &otg_descriptor,
7847612a43dSVitaly Kuzmichev 	/* control interface matches ACM, not Ethernet */
7857612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_control_intf,
7867612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &header_desc,
7877612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
7887612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &acm_descriptor,
7897612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &union_desc,
7907612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_status_desc,
7917612a43dSVitaly Kuzmichev 	/* data interface has no altsetting */
7927612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &rndis_data_intf,
7937612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_source_desc,
7947612a43dSVitaly Kuzmichev 	(struct usb_descriptor_header *) &hs_sink_desc,
7957612a43dSVitaly Kuzmichev 	NULL,
7967612a43dSVitaly Kuzmichev };
7977612a43dSVitaly Kuzmichev #endif
7987612a43dSVitaly Kuzmichev 
7997612a43dSVitaly Kuzmichev 
80023cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */
80123cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor *
80223cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
80323cd1385SRemy Bohmer 		struct usb_endpoint_descriptor *fs)
80423cd1385SRemy Bohmer {
80523cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
80623cd1385SRemy Bohmer 		return hs;
80723cd1385SRemy Bohmer 	return fs;
80823cd1385SRemy Bohmer }
80923cd1385SRemy Bohmer 
81023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
81123cd1385SRemy Bohmer 
81223cd1385SRemy Bohmer /* descriptors that are built on-demand */
81323cd1385SRemy Bohmer 
81423cd1385SRemy Bohmer static char manufacturer[50];
81523cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC;
81623cd1385SRemy Bohmer static char serial_number[20];
81723cd1385SRemy Bohmer 
81823cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */
81923cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1];
82023cd1385SRemy Bohmer 
82123cd1385SRemy Bohmer /* static strings, in UTF-8 */
82223cd1385SRemy Bohmer static struct usb_string		strings[] = {
82323cd1385SRemy Bohmer 	{ STRING_MANUFACTURER,	manufacturer, },
82423cd1385SRemy Bohmer 	{ STRING_PRODUCT,	product_desc, },
82523cd1385SRemy Bohmer 	{ STRING_SERIALNUMBER,	serial_number, },
82623cd1385SRemy Bohmer 	{ STRING_DATA,		"Ethernet Data", },
82723cd1385SRemy Bohmer 	{ STRING_ETHADDR,	ethaddr, },
8282bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
82923cd1385SRemy Bohmer 	{ STRING_CDC,		"CDC Ethernet", },
83023cd1385SRemy Bohmer 	{ STRING_CONTROL,	"CDC Communications Control", },
83123cd1385SRemy Bohmer #endif
8322bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_SUBSET
83323cd1385SRemy Bohmer 	{ STRING_SUBSET,	"CDC Ethernet Subset", },
83423cd1385SRemy Bohmer #endif
8357612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
8367612a43dSVitaly Kuzmichev 	{ STRING_RNDIS,		"RNDIS", },
8377612a43dSVitaly Kuzmichev 	{ STRING_RNDIS_CONTROL,	"RNDIS Communications Control", },
8387612a43dSVitaly Kuzmichev #endif
83923cd1385SRemy Bohmer 	{  }		/* end of list */
84023cd1385SRemy Bohmer };
84123cd1385SRemy Bohmer 
84223cd1385SRemy Bohmer static struct usb_gadget_strings	stringtab = {
84323cd1385SRemy Bohmer 	.language	= 0x0409,	/* en-us */
84423cd1385SRemy Bohmer 	.strings	= strings,
84523cd1385SRemy Bohmer };
84623cd1385SRemy Bohmer 
84723cd1385SRemy Bohmer /*============================================================================*/
8485290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
8495290759cSJoel Fernandes 
8502bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
8515290759cSJoel Fernandes DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
852563aed25SLukasz Dalek #endif
85323cd1385SRemy Bohmer 
85423cd1385SRemy Bohmer /*============================================================================*/
85523cd1385SRemy Bohmer 
85623cd1385SRemy Bohmer /*
85723cd1385SRemy Bohmer  * one config, two interfaces:  control, data.
85823cd1385SRemy Bohmer  * complications: class descriptors, and an altsetting.
85923cd1385SRemy Bohmer  */
86023cd1385SRemy Bohmer static int
86123cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
86223cd1385SRemy Bohmer {
86323cd1385SRemy Bohmer 	int					len;
86423cd1385SRemy Bohmer 	const struct usb_config_descriptor	*config;
86523cd1385SRemy Bohmer 	const struct usb_descriptor_header	**function;
86623cd1385SRemy Bohmer 	int					hs = 0;
86723cd1385SRemy Bohmer 
86823cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g)) {
86923cd1385SRemy Bohmer 		hs = (g->speed == USB_SPEED_HIGH);
87023cd1385SRemy Bohmer 		if (type == USB_DT_OTHER_SPEED_CONFIG)
87123cd1385SRemy Bohmer 			hs = !hs;
87223cd1385SRemy Bohmer 	}
87323cd1385SRemy Bohmer #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function)
87423cd1385SRemy Bohmer 
87523cd1385SRemy Bohmer 	if (index >= device_desc.bNumConfigurations)
87623cd1385SRemy Bohmer 		return -EINVAL;
87723cd1385SRemy Bohmer 
8787612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
8797612a43dSVitaly Kuzmichev 	/*
8807612a43dSVitaly Kuzmichev 	 * list the RNDIS config first, to make Microsoft's drivers
8817612a43dSVitaly Kuzmichev 	 * happy. DOCSIS 1.0 needs this too.
8827612a43dSVitaly Kuzmichev 	 */
8837612a43dSVitaly Kuzmichev 	if (device_desc.bNumConfigurations == 2 && index == 0) {
8847612a43dSVitaly Kuzmichev 		config = &rndis_config;
8857612a43dSVitaly Kuzmichev 		function = which_fn(rndis);
8867612a43dSVitaly Kuzmichev 	} else
8877612a43dSVitaly Kuzmichev #endif
8887612a43dSVitaly Kuzmichev 	{
88923cd1385SRemy Bohmer 		config = &eth_config;
89023cd1385SRemy Bohmer 		function = which_fn(eth);
8917612a43dSVitaly Kuzmichev 	}
89223cd1385SRemy Bohmer 
89323cd1385SRemy Bohmer 	/* for now, don't advertise srp-only devices */
89423cd1385SRemy Bohmer 	if (!is_otg)
89523cd1385SRemy Bohmer 		function++;
89623cd1385SRemy Bohmer 
89723cd1385SRemy Bohmer 	len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
89823cd1385SRemy Bohmer 	if (len < 0)
89923cd1385SRemy Bohmer 		return len;
90023cd1385SRemy Bohmer 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
90123cd1385SRemy Bohmer 	return len;
90223cd1385SRemy Bohmer }
90323cd1385SRemy Bohmer 
90423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
90523cd1385SRemy Bohmer 
9067612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
90723cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
90823cd1385SRemy Bohmer 
90923cd1385SRemy Bohmer static int
91023cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
91123cd1385SRemy Bohmer {
91223cd1385SRemy Bohmer 	int					result = 0;
91323cd1385SRemy Bohmer 	struct usb_gadget			*gadget = dev->gadget;
91423cd1385SRemy Bohmer 
9152bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
9167612a43dSVitaly Kuzmichev 	/* status endpoint used for RNDIS and (optionally) CDC */
91723cd1385SRemy Bohmer 	if (!subset_active(dev) && dev->status_ep) {
91823cd1385SRemy Bohmer 		dev->status = ep_desc(gadget, &hs_status_desc,
91923cd1385SRemy Bohmer 						&fs_status_desc);
92023cd1385SRemy Bohmer 		dev->status_ep->driver_data = dev;
92123cd1385SRemy Bohmer 
92223cd1385SRemy Bohmer 		result = usb_ep_enable(dev->status_ep, dev->status);
92323cd1385SRemy Bohmer 		if (result != 0) {
9247de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
92523cd1385SRemy Bohmer 				dev->status_ep->name, result);
92623cd1385SRemy Bohmer 			goto done;
92723cd1385SRemy Bohmer 		}
92823cd1385SRemy Bohmer 	}
92923cd1385SRemy Bohmer #endif
93023cd1385SRemy Bohmer 
93123cd1385SRemy Bohmer 	dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
93223cd1385SRemy Bohmer 	dev->in_ep->driver_data = dev;
93323cd1385SRemy Bohmer 
93423cd1385SRemy Bohmer 	dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
93523cd1385SRemy Bohmer 	dev->out_ep->driver_data = dev;
93623cd1385SRemy Bohmer 
9376142e0aeSVitaly Kuzmichev 	/*
9386142e0aeSVitaly Kuzmichev 	 * With CDC,  the host isn't allowed to use these two data
93923cd1385SRemy Bohmer 	 * endpoints in the default altsetting for the interface.
94023cd1385SRemy Bohmer 	 * so we don't activate them yet.  Reset from SET_INTERFACE.
9417612a43dSVitaly Kuzmichev 	 *
9427612a43dSVitaly Kuzmichev 	 * Strictly speaking RNDIS should work the same: activation is
9437612a43dSVitaly Kuzmichev 	 * a side effect of setting a packet filter.  Deactivation is
9447612a43dSVitaly Kuzmichev 	 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
94523cd1385SRemy Bohmer 	 */
94623cd1385SRemy Bohmer 	if (!cdc_active(dev)) {
94723cd1385SRemy Bohmer 		result = usb_ep_enable(dev->in_ep, dev->in);
94823cd1385SRemy Bohmer 		if (result != 0) {
9497de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
95023cd1385SRemy Bohmer 				dev->in_ep->name, result);
95123cd1385SRemy Bohmer 			goto done;
95223cd1385SRemy Bohmer 		}
95323cd1385SRemy Bohmer 
95423cd1385SRemy Bohmer 		result = usb_ep_enable(dev->out_ep, dev->out);
95523cd1385SRemy Bohmer 		if (result != 0) {
9567de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
95723cd1385SRemy Bohmer 				dev->out_ep->name, result);
95823cd1385SRemy Bohmer 			goto done;
95923cd1385SRemy Bohmer 		}
96023cd1385SRemy Bohmer 	}
96123cd1385SRemy Bohmer 
96223cd1385SRemy Bohmer done:
96323cd1385SRemy Bohmer 	if (result == 0)
96423cd1385SRemy Bohmer 		result = alloc_requests(dev, qlen(gadget), gfp_flags);
96523cd1385SRemy Bohmer 
96623cd1385SRemy Bohmer 	/* on error, disable any endpoints  */
96723cd1385SRemy Bohmer 	if (result < 0) {
968d5292c16SVitaly Kuzmichev 		if (!subset_active(dev) && dev->status_ep)
96923cd1385SRemy Bohmer 			(void) usb_ep_disable(dev->status_ep);
97023cd1385SRemy Bohmer 		dev->status = NULL;
97123cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->in_ep);
97223cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->out_ep);
97323cd1385SRemy Bohmer 		dev->in = NULL;
97423cd1385SRemy Bohmer 		dev->out = NULL;
9757612a43dSVitaly Kuzmichev 	} else if (!cdc_active(dev)) {
9767612a43dSVitaly Kuzmichev 		/*
9777612a43dSVitaly Kuzmichev 		 * activate non-CDC configs right away
9787612a43dSVitaly Kuzmichev 		 * this isn't strictly according to the RNDIS spec
9797612a43dSVitaly Kuzmichev 		 */
9807612a43dSVitaly Kuzmichev 		eth_start(dev, GFP_ATOMIC);
98123cd1385SRemy Bohmer 	}
98223cd1385SRemy Bohmer 
98323cd1385SRemy Bohmer 	/* caller is responsible for cleanup on error */
98423cd1385SRemy Bohmer 	return result;
98523cd1385SRemy Bohmer }
98623cd1385SRemy Bohmer 
98723cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev)
98823cd1385SRemy Bohmer {
98923cd1385SRemy Bohmer 	if (dev->config == 0)
99023cd1385SRemy Bohmer 		return;
99123cd1385SRemy Bohmer 
9927de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
9937de73185SVitaly Kuzmichev 
9947612a43dSVitaly Kuzmichev 	rndis_uninit(dev->rndis_config);
9957612a43dSVitaly Kuzmichev 
9966142e0aeSVitaly Kuzmichev 	/*
9976142e0aeSVitaly Kuzmichev 	 * disable endpoints, forcing (synchronous) completion of
99823cd1385SRemy Bohmer 	 * pending i/o.  then free the requests.
99923cd1385SRemy Bohmer 	 */
100023cd1385SRemy Bohmer 
100123cd1385SRemy Bohmer 	if (dev->in) {
100223cd1385SRemy Bohmer 		usb_ep_disable(dev->in_ep);
100323cd1385SRemy Bohmer 		if (dev->tx_req) {
100423cd1385SRemy Bohmer 			usb_ep_free_request(dev->in_ep, dev->tx_req);
100523cd1385SRemy Bohmer 			dev->tx_req = NULL;
100623cd1385SRemy Bohmer 		}
100723cd1385SRemy Bohmer 	}
100823cd1385SRemy Bohmer 	if (dev->out) {
100923cd1385SRemy Bohmer 		usb_ep_disable(dev->out_ep);
101023cd1385SRemy Bohmer 		if (dev->rx_req) {
10110129e327SVitaly Kuzmichev 			usb_ep_free_request(dev->out_ep, dev->rx_req);
101223cd1385SRemy Bohmer 			dev->rx_req = NULL;
101323cd1385SRemy Bohmer 		}
101423cd1385SRemy Bohmer 	}
10156142e0aeSVitaly Kuzmichev 	if (dev->status)
101623cd1385SRemy Bohmer 		usb_ep_disable(dev->status_ep);
10176142e0aeSVitaly Kuzmichev 
10187612a43dSVitaly Kuzmichev 	dev->rndis = 0;
101923cd1385SRemy Bohmer 	dev->cdc_filter = 0;
102023cd1385SRemy Bohmer 	dev->config = 0;
102123cd1385SRemy Bohmer }
102223cd1385SRemy Bohmer 
10236142e0aeSVitaly Kuzmichev /*
10246142e0aeSVitaly Kuzmichev  * change our operational config.  must agree with the code
102523cd1385SRemy Bohmer  * that returns config descriptors, and altsetting code.
102623cd1385SRemy Bohmer  */
10276142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number,
10286142e0aeSVitaly Kuzmichev 				gfp_t gfp_flags)
102923cd1385SRemy Bohmer {
103023cd1385SRemy Bohmer 	int			result = 0;
103123cd1385SRemy Bohmer 	struct usb_gadget	*gadget = dev->gadget;
103223cd1385SRemy Bohmer 
103323cd1385SRemy Bohmer 	if (gadget_is_sa1100(gadget)
103423cd1385SRemy Bohmer 			&& dev->config
103523cd1385SRemy Bohmer 			&& dev->tx_qlen != 0) {
103623cd1385SRemy Bohmer 		/* tx fifo is full, but we can't clear it...*/
10377de73185SVitaly Kuzmichev 		error("can't change configurations");
103823cd1385SRemy Bohmer 		return -ESPIPE;
103923cd1385SRemy Bohmer 	}
104023cd1385SRemy Bohmer 	eth_reset_config(dev);
104123cd1385SRemy Bohmer 
104223cd1385SRemy Bohmer 	switch (number) {
104323cd1385SRemy Bohmer 	case DEV_CONFIG_VALUE:
104423cd1385SRemy Bohmer 		result = set_ether_config(dev, gfp_flags);
104523cd1385SRemy Bohmer 		break;
10467612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
10477612a43dSVitaly Kuzmichev 	case DEV_RNDIS_CONFIG_VALUE:
10487612a43dSVitaly Kuzmichev 		dev->rndis = 1;
10497612a43dSVitaly Kuzmichev 		result = set_ether_config(dev, gfp_flags);
10507612a43dSVitaly Kuzmichev 		break;
10517612a43dSVitaly Kuzmichev #endif
105223cd1385SRemy Bohmer 	default:
105323cd1385SRemy Bohmer 		result = -EINVAL;
105423cd1385SRemy Bohmer 		/* FALL THROUGH */
105523cd1385SRemy Bohmer 	case 0:
105623cd1385SRemy Bohmer 		break;
105723cd1385SRemy Bohmer 	}
105823cd1385SRemy Bohmer 
105923cd1385SRemy Bohmer 	if (result) {
106023cd1385SRemy Bohmer 		if (number)
106123cd1385SRemy Bohmer 			eth_reset_config(dev);
106223cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget,
106323cd1385SRemy Bohmer 				gadget_is_otg(dev->gadget) ? 8 : 100);
106423cd1385SRemy Bohmer 	} else {
106523cd1385SRemy Bohmer 		char *speed;
106623cd1385SRemy Bohmer 		unsigned power;
106723cd1385SRemy Bohmer 
106823cd1385SRemy Bohmer 		power = 2 * eth_config.bMaxPower;
106923cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget, power);
107023cd1385SRemy Bohmer 
107123cd1385SRemy Bohmer 		switch (gadget->speed) {
10726142e0aeSVitaly Kuzmichev 		case USB_SPEED_FULL:
10736142e0aeSVitaly Kuzmichev 			speed = "full"; break;
107423cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
10756142e0aeSVitaly Kuzmichev 		case USB_SPEED_HIGH:
10766142e0aeSVitaly Kuzmichev 			speed = "high"; break;
107723cd1385SRemy Bohmer #endif
10786142e0aeSVitaly Kuzmichev 		default:
10796142e0aeSVitaly Kuzmichev 			speed = "?"; break;
108023cd1385SRemy Bohmer 		}
108123cd1385SRemy Bohmer 
108223cd1385SRemy Bohmer 		dev->config = number;
10837de73185SVitaly Kuzmichev 		printf("%s speed config #%d: %d mA, %s, using %s\n",
108423cd1385SRemy Bohmer 				speed, number, power, driver_desc,
10857612a43dSVitaly Kuzmichev 				rndis_active(dev)
10867612a43dSVitaly Kuzmichev 					? "RNDIS"
10877612a43dSVitaly Kuzmichev 					: (cdc_active(dev)
10887612a43dSVitaly Kuzmichev 						? "CDC Ethernet"
108923cd1385SRemy Bohmer 						: "CDC Ethernet Subset"));
109023cd1385SRemy Bohmer 	}
109123cd1385SRemy Bohmer 	return result;
109223cd1385SRemy Bohmer }
109323cd1385SRemy Bohmer 
109423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
109523cd1385SRemy Bohmer 
10962bb37884SLukasz Dalek #ifdef	CONFIG_USB_ETH_CDC
109723cd1385SRemy Bohmer 
10986142e0aeSVitaly Kuzmichev /*
10996142e0aeSVitaly Kuzmichev  * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
110023cd1385SRemy Bohmer  * only to notify the host about link status changes (which we support) or
11017612a43dSVitaly Kuzmichev  * report completion of some encapsulated command (as used in RNDIS).  Since
110223cd1385SRemy Bohmer  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
110323cd1385SRemy Bohmer  * command mechanism; and only one status request is ever queued.
110423cd1385SRemy Bohmer  */
110523cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
110623cd1385SRemy Bohmer {
110723cd1385SRemy Bohmer 	struct usb_cdc_notification	*event = req->buf;
110823cd1385SRemy Bohmer 	int				value = req->status;
110923cd1385SRemy Bohmer 	struct eth_dev			*dev = ep->driver_data;
111023cd1385SRemy Bohmer 
111123cd1385SRemy Bohmer 	/* issue the second notification if host reads the first */
111223cd1385SRemy Bohmer 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
111323cd1385SRemy Bohmer 			&& value == 0) {
111423cd1385SRemy Bohmer 		__le32	*data = req->buf + sizeof *event;
111523cd1385SRemy Bohmer 
111623cd1385SRemy Bohmer 		event->bmRequestType = 0xA1;
111723cd1385SRemy Bohmer 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
111823cd1385SRemy Bohmer 		event->wValue = __constant_cpu_to_le16(0);
111923cd1385SRemy Bohmer 		event->wIndex = __constant_cpu_to_le16(1);
112023cd1385SRemy Bohmer 		event->wLength = __constant_cpu_to_le16(8);
112123cd1385SRemy Bohmer 
112223cd1385SRemy Bohmer 		/* SPEED_CHANGE data is up/down speeds in bits/sec */
112323cd1385SRemy Bohmer 		data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
112423cd1385SRemy Bohmer 
112523cd1385SRemy Bohmer 		req->length = STATUS_BYTECOUNT;
112623cd1385SRemy Bohmer 		value = usb_ep_queue(ep, req, GFP_ATOMIC);
11277de73185SVitaly Kuzmichev 		debug("send SPEED_CHANGE --> %d\n", value);
112823cd1385SRemy Bohmer 		if (value == 0)
112923cd1385SRemy Bohmer 			return;
113023cd1385SRemy Bohmer 	} else if (value != -ECONNRESET) {
11317de73185SVitaly Kuzmichev 		debug("event %02x --> %d\n",
113223cd1385SRemy Bohmer 			event->bNotificationType, value);
113323cd1385SRemy Bohmer 		if (event->bNotificationType ==
11346142e0aeSVitaly Kuzmichev 				USB_CDC_NOTIFY_SPEED_CHANGE) {
113523cd1385SRemy Bohmer 			l_ethdev.network_started = 1;
113623cd1385SRemy Bohmer 			printf("USB network up!\n");
113723cd1385SRemy Bohmer 		}
113823cd1385SRemy Bohmer 	}
113923cd1385SRemy Bohmer 	req->context = NULL;
114023cd1385SRemy Bohmer }
114123cd1385SRemy Bohmer 
114223cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev)
114323cd1385SRemy Bohmer {
114423cd1385SRemy Bohmer 	struct usb_request		*req = dev->stat_req;
114523cd1385SRemy Bohmer 	struct usb_cdc_notification	*event;
114623cd1385SRemy Bohmer 	int				value;
114723cd1385SRemy Bohmer 
11486142e0aeSVitaly Kuzmichev 	/*
11496142e0aeSVitaly Kuzmichev 	 * flush old status
115023cd1385SRemy Bohmer 	 *
115123cd1385SRemy Bohmer 	 * FIXME ugly idiom, maybe we'd be better with just
115223cd1385SRemy Bohmer 	 * a "cancel the whole queue" primitive since any
115323cd1385SRemy Bohmer 	 * unlink-one primitive has way too many error modes.
115423cd1385SRemy Bohmer 	 * here, we "know" toggle is already clear...
115523cd1385SRemy Bohmer 	 *
115623cd1385SRemy Bohmer 	 * FIXME iff req->context != null just dequeue it
115723cd1385SRemy Bohmer 	 */
115823cd1385SRemy Bohmer 	usb_ep_disable(dev->status_ep);
115923cd1385SRemy Bohmer 	usb_ep_enable(dev->status_ep, dev->status);
116023cd1385SRemy Bohmer 
11616142e0aeSVitaly Kuzmichev 	/*
11626142e0aeSVitaly Kuzmichev 	 * 3.8.1 says to issue first NETWORK_CONNECTION, then
116323cd1385SRemy Bohmer 	 * a SPEED_CHANGE.  could be useful in some configs.
116423cd1385SRemy Bohmer 	 */
116523cd1385SRemy Bohmer 	event = req->buf;
116623cd1385SRemy Bohmer 	event->bmRequestType = 0xA1;
116723cd1385SRemy Bohmer 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
116823cd1385SRemy Bohmer 	event->wValue = __constant_cpu_to_le16(1);	/* connected */
116923cd1385SRemy Bohmer 	event->wIndex = __constant_cpu_to_le16(1);
117023cd1385SRemy Bohmer 	event->wLength = 0;
117123cd1385SRemy Bohmer 
117223cd1385SRemy Bohmer 	req->length = sizeof *event;
117323cd1385SRemy Bohmer 	req->complete = eth_status_complete;
117423cd1385SRemy Bohmer 	req->context = dev;
117523cd1385SRemy Bohmer 
117623cd1385SRemy Bohmer 	value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
117723cd1385SRemy Bohmer 	if (value < 0)
11787de73185SVitaly Kuzmichev 		debug("status buf queue --> %d\n", value);
117923cd1385SRemy Bohmer }
118023cd1385SRemy Bohmer 
118123cd1385SRemy Bohmer #endif
118223cd1385SRemy Bohmer 
118323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
118423cd1385SRemy Bohmer 
118523cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
118623cd1385SRemy Bohmer {
118723cd1385SRemy Bohmer 	if (req->status || req->actual != req->length)
11887de73185SVitaly Kuzmichev 		debug("setup complete --> %d, %d/%d\n",
118923cd1385SRemy Bohmer 				req->status, req->actual, req->length);
119023cd1385SRemy Bohmer }
119123cd1385SRemy Bohmer 
11927612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
11937612a43dSVitaly Kuzmichev 
11947612a43dSVitaly Kuzmichev static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
11957612a43dSVitaly Kuzmichev {
11967612a43dSVitaly Kuzmichev 	if (req->status || req->actual != req->length)
11977612a43dSVitaly Kuzmichev 		debug("rndis response complete --> %d, %d/%d\n",
11987612a43dSVitaly Kuzmichev 			req->status, req->actual, req->length);
11997612a43dSVitaly Kuzmichev 
12007612a43dSVitaly Kuzmichev 	/* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
12017612a43dSVitaly Kuzmichev }
12027612a43dSVitaly Kuzmichev 
12037612a43dSVitaly Kuzmichev static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
12047612a43dSVitaly Kuzmichev {
12057612a43dSVitaly Kuzmichev 	struct eth_dev          *dev = ep->driver_data;
12067612a43dSVitaly Kuzmichev 	int			status;
12077612a43dSVitaly Kuzmichev 
12087612a43dSVitaly Kuzmichev 	/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
12097612a43dSVitaly Kuzmichev 	status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
12107612a43dSVitaly Kuzmichev 	if (status < 0)
12117612a43dSVitaly Kuzmichev 		error("%s: rndis parse error %d", __func__, status);
12127612a43dSVitaly Kuzmichev }
12137612a43dSVitaly Kuzmichev 
12147612a43dSVitaly Kuzmichev #endif	/* RNDIS */
12157612a43dSVitaly Kuzmichev 
121623cd1385SRemy Bohmer /*
121723cd1385SRemy Bohmer  * The setup() callback implements all the ep0 functionality that's not
121823cd1385SRemy Bohmer  * handled lower down.  CDC has a number of less-common features:
121923cd1385SRemy Bohmer  *
122023cd1385SRemy Bohmer  *  - two interfaces:  control, and ethernet data
122123cd1385SRemy Bohmer  *  - Ethernet data interface has two altsettings:  default, and active
122223cd1385SRemy Bohmer  *  - class-specific descriptors for the control interface
122323cd1385SRemy Bohmer  *  - class-specific control requests
122423cd1385SRemy Bohmer  */
122523cd1385SRemy Bohmer static int
122623cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
122723cd1385SRemy Bohmer {
122823cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data(gadget);
122923cd1385SRemy Bohmer 	struct usb_request	*req = dev->req;
123023cd1385SRemy Bohmer 	int			value = -EOPNOTSUPP;
123123cd1385SRemy Bohmer 	u16			wIndex = le16_to_cpu(ctrl->wIndex);
123223cd1385SRemy Bohmer 	u16			wValue = le16_to_cpu(ctrl->wValue);
123323cd1385SRemy Bohmer 	u16			wLength = le16_to_cpu(ctrl->wLength);
123423cd1385SRemy Bohmer 
12356142e0aeSVitaly Kuzmichev 	/*
12366142e0aeSVitaly Kuzmichev 	 * descriptors just go into the pre-allocated ep0 buffer,
123723cd1385SRemy Bohmer 	 * while config change events may enable network traffic.
123823cd1385SRemy Bohmer 	 */
123923cd1385SRemy Bohmer 
12407de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
124123cd1385SRemy Bohmer 
124223cd1385SRemy Bohmer 	req->complete = eth_setup_complete;
124323cd1385SRemy Bohmer 	switch (ctrl->bRequest) {
124423cd1385SRemy Bohmer 
124523cd1385SRemy Bohmer 	case USB_REQ_GET_DESCRIPTOR:
124623cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
124723cd1385SRemy Bohmer 			break;
124823cd1385SRemy Bohmer 		switch (wValue >> 8) {
124923cd1385SRemy Bohmer 
125023cd1385SRemy Bohmer 		case USB_DT_DEVICE:
125104afd5b5SKishon Vijay Abraham I 			device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
125223cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof device_desc);
125323cd1385SRemy Bohmer 			memcpy(req->buf, &device_desc, value);
125423cd1385SRemy Bohmer 			break;
125523cd1385SRemy Bohmer 		case USB_DT_DEVICE_QUALIFIER:
125623cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
125723cd1385SRemy Bohmer 				break;
125823cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof dev_qualifier);
125923cd1385SRemy Bohmer 			memcpy(req->buf, &dev_qualifier, value);
126023cd1385SRemy Bohmer 			break;
126123cd1385SRemy Bohmer 
126223cd1385SRemy Bohmer 		case USB_DT_OTHER_SPEED_CONFIG:
126323cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
126423cd1385SRemy Bohmer 				break;
126523cd1385SRemy Bohmer 			/* FALLTHROUGH */
126623cd1385SRemy Bohmer 		case USB_DT_CONFIG:
126723cd1385SRemy Bohmer 			value = config_buf(gadget, req->buf,
126823cd1385SRemy Bohmer 					wValue >> 8,
126923cd1385SRemy Bohmer 					wValue & 0xff,
127023cd1385SRemy Bohmer 					gadget_is_otg(gadget));
127123cd1385SRemy Bohmer 			if (value >= 0)
127223cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
127323cd1385SRemy Bohmer 			break;
127423cd1385SRemy Bohmer 
127523cd1385SRemy Bohmer 		case USB_DT_STRING:
127623cd1385SRemy Bohmer 			value = usb_gadget_get_string(&stringtab,
127723cd1385SRemy Bohmer 					wValue & 0xff, req->buf);
127823cd1385SRemy Bohmer 
127923cd1385SRemy Bohmer 			if (value >= 0)
128023cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
128123cd1385SRemy Bohmer 
128223cd1385SRemy Bohmer 			break;
128323cd1385SRemy Bohmer 		}
128423cd1385SRemy Bohmer 		break;
128523cd1385SRemy Bohmer 
128623cd1385SRemy Bohmer 	case USB_REQ_SET_CONFIGURATION:
128723cd1385SRemy Bohmer 		if (ctrl->bRequestType != 0)
128823cd1385SRemy Bohmer 			break;
128923cd1385SRemy Bohmer 		if (gadget->a_hnp_support)
12907de73185SVitaly Kuzmichev 			debug("HNP available\n");
129123cd1385SRemy Bohmer 		else if (gadget->a_alt_hnp_support)
12927de73185SVitaly Kuzmichev 			debug("HNP needs a different root port\n");
129323cd1385SRemy Bohmer 		value = eth_set_config(dev, wValue, GFP_ATOMIC);
129423cd1385SRemy Bohmer 		break;
129523cd1385SRemy Bohmer 	case USB_REQ_GET_CONFIGURATION:
129623cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
129723cd1385SRemy Bohmer 			break;
129823cd1385SRemy Bohmer 		*(u8 *)req->buf = dev->config;
129923cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
130023cd1385SRemy Bohmer 		break;
130123cd1385SRemy Bohmer 
130223cd1385SRemy Bohmer 	case USB_REQ_SET_INTERFACE:
130323cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_RECIP_INTERFACE
130423cd1385SRemy Bohmer 				|| !dev->config
130523cd1385SRemy Bohmer 				|| wIndex > 1)
130623cd1385SRemy Bohmer 			break;
130723cd1385SRemy Bohmer 		if (!cdc_active(dev) && wIndex != 0)
130823cd1385SRemy Bohmer 			break;
130923cd1385SRemy Bohmer 
13106142e0aeSVitaly Kuzmichev 		/*
13116142e0aeSVitaly Kuzmichev 		 * PXA hardware partially handles SET_INTERFACE;
131223cd1385SRemy Bohmer 		 * we need to kluge around that interference.
131323cd1385SRemy Bohmer 		 */
131423cd1385SRemy Bohmer 		if (gadget_is_pxa(gadget)) {
131523cd1385SRemy Bohmer 			value = eth_set_config(dev, DEV_CONFIG_VALUE,
131623cd1385SRemy Bohmer 						GFP_ATOMIC);
1317563aed25SLukasz Dalek 			/*
1318563aed25SLukasz Dalek 			 * PXA25x driver use non-CDC ethernet gadget.
1319563aed25SLukasz Dalek 			 * But only _CDC and _RNDIS code can signalize
1320563aed25SLukasz Dalek 			 * that network is working. So we signalize it
1321563aed25SLukasz Dalek 			 * here.
1322563aed25SLukasz Dalek 			 */
1323563aed25SLukasz Dalek 			l_ethdev.network_started = 1;
1324563aed25SLukasz Dalek 			debug("USB network up!\n");
132523cd1385SRemy Bohmer 			goto done_set_intf;
132623cd1385SRemy Bohmer 		}
132723cd1385SRemy Bohmer 
13282bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
132923cd1385SRemy Bohmer 		switch (wIndex) {
133023cd1385SRemy Bohmer 		case 0:		/* control/master intf */
133123cd1385SRemy Bohmer 			if (wValue != 0)
133223cd1385SRemy Bohmer 				break;
133323cd1385SRemy Bohmer 			if (dev->status) {
133423cd1385SRemy Bohmer 				usb_ep_disable(dev->status_ep);
133523cd1385SRemy Bohmer 				usb_ep_enable(dev->status_ep, dev->status);
133623cd1385SRemy Bohmer 			}
13377612a43dSVitaly Kuzmichev 
133823cd1385SRemy Bohmer 			value = 0;
133923cd1385SRemy Bohmer 			break;
134023cd1385SRemy Bohmer 		case 1:		/* data intf */
134123cd1385SRemy Bohmer 			if (wValue > 1)
134223cd1385SRemy Bohmer 				break;
134323cd1385SRemy Bohmer 			usb_ep_disable(dev->in_ep);
134423cd1385SRemy Bohmer 			usb_ep_disable(dev->out_ep);
134523cd1385SRemy Bohmer 
13466142e0aeSVitaly Kuzmichev 			/*
13476142e0aeSVitaly Kuzmichev 			 * CDC requires the data transfers not be done from
134823cd1385SRemy Bohmer 			 * the default interface setting ... also, setting
134923cd1385SRemy Bohmer 			 * the non-default interface resets filters etc.
135023cd1385SRemy Bohmer 			 */
135123cd1385SRemy Bohmer 			if (wValue == 1) {
135223cd1385SRemy Bohmer 				if (!cdc_active(dev))
135323cd1385SRemy Bohmer 					break;
135423cd1385SRemy Bohmer 				usb_ep_enable(dev->in_ep, dev->in);
135523cd1385SRemy Bohmer 				usb_ep_enable(dev->out_ep, dev->out);
135623cd1385SRemy Bohmer 				dev->cdc_filter = DEFAULT_FILTER;
135723cd1385SRemy Bohmer 				if (dev->status)
135823cd1385SRemy Bohmer 					issue_start_status(dev);
13597612a43dSVitaly Kuzmichev 				eth_start(dev, GFP_ATOMIC);
136023cd1385SRemy Bohmer 			}
136123cd1385SRemy Bohmer 			value = 0;
136223cd1385SRemy Bohmer 			break;
136323cd1385SRemy Bohmer 		}
136423cd1385SRemy Bohmer #else
13656142e0aeSVitaly Kuzmichev 		/*
13666142e0aeSVitaly Kuzmichev 		 * FIXME this is wrong, as is the assumption that
136723cd1385SRemy Bohmer 		 * all non-PXA hardware talks real CDC ...
136823cd1385SRemy Bohmer 		 */
13697de73185SVitaly Kuzmichev 		debug("set_interface ignored!\n");
13702bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
137123cd1385SRemy Bohmer 
137223cd1385SRemy Bohmer done_set_intf:
137323cd1385SRemy Bohmer 		break;
137423cd1385SRemy Bohmer 	case USB_REQ_GET_INTERFACE:
137523cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
137623cd1385SRemy Bohmer 				|| !dev->config
137723cd1385SRemy Bohmer 				|| wIndex > 1)
137823cd1385SRemy Bohmer 			break;
13797612a43dSVitaly Kuzmichev 		if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
138023cd1385SRemy Bohmer 			break;
138123cd1385SRemy Bohmer 
138223cd1385SRemy Bohmer 		/* for CDC, iff carrier is on, data interface is active. */
13837612a43dSVitaly Kuzmichev 		if (rndis_active(dev) || wIndex != 1)
138423cd1385SRemy Bohmer 			*(u8 *)req->buf = 0;
138523cd1385SRemy Bohmer 		else {
138623cd1385SRemy Bohmer 			/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
138723cd1385SRemy Bohmer 			/* carrier always ok ...*/
138823cd1385SRemy Bohmer 			*(u8 *)req->buf = 1 ;
138923cd1385SRemy Bohmer 		}
139023cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
139123cd1385SRemy Bohmer 		break;
139223cd1385SRemy Bohmer 
13932bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
139423cd1385SRemy Bohmer 	case USB_CDC_SET_ETHERNET_PACKET_FILTER:
13956142e0aeSVitaly Kuzmichev 		/*
13966142e0aeSVitaly Kuzmichev 		 * see 6.2.30: no data, wIndex = interface,
139723cd1385SRemy Bohmer 		 * wValue = packet filter bitmap
139823cd1385SRemy Bohmer 		 */
139923cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
140023cd1385SRemy Bohmer 				|| !cdc_active(dev)
140123cd1385SRemy Bohmer 				|| wLength != 0
140223cd1385SRemy Bohmer 				|| wIndex > 1)
140323cd1385SRemy Bohmer 			break;
14047de73185SVitaly Kuzmichev 		debug("packet filter %02x\n", wValue);
140523cd1385SRemy Bohmer 		dev->cdc_filter = wValue;
140623cd1385SRemy Bohmer 		value = 0;
140723cd1385SRemy Bohmer 		break;
140823cd1385SRemy Bohmer 
14096142e0aeSVitaly Kuzmichev 	/*
14106142e0aeSVitaly Kuzmichev 	 * and potentially:
141123cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
141223cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
141323cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
141423cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_STATISTIC:
141523cd1385SRemy Bohmer 	 */
141623cd1385SRemy Bohmer 
14172bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */
141823cd1385SRemy Bohmer 
14197612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
14207612a43dSVitaly Kuzmichev 	/*
14217612a43dSVitaly Kuzmichev 	 * RNDIS uses the CDC command encapsulation mechanism to implement
14227612a43dSVitaly Kuzmichev 	 * an RPC scheme, with much getting/setting of attributes by OID.
14237612a43dSVitaly Kuzmichev 	 */
14247612a43dSVitaly Kuzmichev 	case USB_CDC_SEND_ENCAPSULATED_COMMAND:
14257612a43dSVitaly Kuzmichev 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14267612a43dSVitaly Kuzmichev 				|| !rndis_active(dev)
14277612a43dSVitaly Kuzmichev 				|| wLength > USB_BUFSIZ
14287612a43dSVitaly Kuzmichev 				|| wValue
14297612a43dSVitaly Kuzmichev 				|| rndis_control_intf.bInterfaceNumber
14307612a43dSVitaly Kuzmichev 					!= wIndex)
14317612a43dSVitaly Kuzmichev 			break;
14327612a43dSVitaly Kuzmichev 		/* read the request, then process it */
14337612a43dSVitaly Kuzmichev 		value = wLength;
14347612a43dSVitaly Kuzmichev 		req->complete = rndis_command_complete;
14357612a43dSVitaly Kuzmichev 		/* later, rndis_control_ack () sends a notification */
14367612a43dSVitaly Kuzmichev 		break;
14377612a43dSVitaly Kuzmichev 
14387612a43dSVitaly Kuzmichev 	case USB_CDC_GET_ENCAPSULATED_RESPONSE:
14397612a43dSVitaly Kuzmichev 		if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
14407612a43dSVitaly Kuzmichev 					== ctrl->bRequestType
14417612a43dSVitaly Kuzmichev 				&& rndis_active(dev)
14427612a43dSVitaly Kuzmichev 				/* && wLength >= 0x0400 */
14437612a43dSVitaly Kuzmichev 				&& !wValue
14447612a43dSVitaly Kuzmichev 				&& rndis_control_intf.bInterfaceNumber
14457612a43dSVitaly Kuzmichev 					== wIndex) {
14467612a43dSVitaly Kuzmichev 			u8 *buf;
14477612a43dSVitaly Kuzmichev 			u32 n;
14487612a43dSVitaly Kuzmichev 
14497612a43dSVitaly Kuzmichev 			/* return the result */
14507612a43dSVitaly Kuzmichev 			buf = rndis_get_next_response(dev->rndis_config, &n);
14517612a43dSVitaly Kuzmichev 			if (buf) {
14527612a43dSVitaly Kuzmichev 				memcpy(req->buf, buf, n);
14537612a43dSVitaly Kuzmichev 				req->complete = rndis_response_complete;
14547612a43dSVitaly Kuzmichev 				rndis_free_response(dev->rndis_config, buf);
14557612a43dSVitaly Kuzmichev 				value = n;
14567612a43dSVitaly Kuzmichev 			}
14577612a43dSVitaly Kuzmichev 			/* else stalls ... spec says to avoid that */
14587612a43dSVitaly Kuzmichev 		}
14597612a43dSVitaly Kuzmichev 		break;
14607612a43dSVitaly Kuzmichev #endif	/* RNDIS */
14617612a43dSVitaly Kuzmichev 
146223cd1385SRemy Bohmer 	default:
14637de73185SVitaly Kuzmichev 		debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
146423cd1385SRemy Bohmer 			ctrl->bRequestType, ctrl->bRequest,
146523cd1385SRemy Bohmer 			wValue, wIndex, wLength);
146623cd1385SRemy Bohmer 	}
146723cd1385SRemy Bohmer 
146823cd1385SRemy Bohmer 	/* respond with data transfer before status phase? */
146923cd1385SRemy Bohmer 	if (value >= 0) {
14707de73185SVitaly Kuzmichev 		debug("respond with data transfer before status phase\n");
147123cd1385SRemy Bohmer 		req->length = value;
147223cd1385SRemy Bohmer 		req->zero = value < wLength
147323cd1385SRemy Bohmer 				&& (value % gadget->ep0->maxpacket) == 0;
147423cd1385SRemy Bohmer 		value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
147523cd1385SRemy Bohmer 		if (value < 0) {
14767de73185SVitaly Kuzmichev 			debug("ep_queue --> %d\n", value);
147723cd1385SRemy Bohmer 			req->status = 0;
147823cd1385SRemy Bohmer 			eth_setup_complete(gadget->ep0, req);
147923cd1385SRemy Bohmer 		}
148023cd1385SRemy Bohmer 	}
148123cd1385SRemy Bohmer 
148223cd1385SRemy Bohmer 	/* host either stalls (value < 0) or reports success */
148323cd1385SRemy Bohmer 	return value;
148423cd1385SRemy Bohmer }
148523cd1385SRemy Bohmer 
148623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
148723cd1385SRemy Bohmer 
148823cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req);
148923cd1385SRemy Bohmer 
14906142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req,
149123cd1385SRemy Bohmer 				gfp_t gfp_flags)
149223cd1385SRemy Bohmer {
149323cd1385SRemy Bohmer 	int			retval = -ENOMEM;
149423cd1385SRemy Bohmer 	size_t			size;
149523cd1385SRemy Bohmer 
14966142e0aeSVitaly Kuzmichev 	/*
14976142e0aeSVitaly Kuzmichev 	 * Padding up to RX_EXTRA handles minor disagreements with host.
149823cd1385SRemy Bohmer 	 * Normally we use the USB "terminate on short read" convention;
149923cd1385SRemy Bohmer 	 * so allow up to (N*maxpacket), since that memory is normally
150023cd1385SRemy Bohmer 	 * already allocated.  Some hardware doesn't deal well with short
150123cd1385SRemy Bohmer 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
150223cd1385SRemy Bohmer 	 * byte off the end (to force hardware errors on overflow).
15037612a43dSVitaly Kuzmichev 	 *
15047612a43dSVitaly Kuzmichev 	 * RNDIS uses internal framing, and explicitly allows senders to
15057612a43dSVitaly Kuzmichev 	 * pad to end-of-packet.  That's potentially nice for speed,
15067612a43dSVitaly Kuzmichev 	 * but means receivers can't recover synch on their own.
150723cd1385SRemy Bohmer 	 */
150823cd1385SRemy Bohmer 
15097de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
151071fc5f91STroy Kisky 	if (!req)
151171fc5f91STroy Kisky 		return -EINVAL;
151223cd1385SRemy Bohmer 
151323cd1385SRemy Bohmer 	size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
151423cd1385SRemy Bohmer 	size += dev->out_ep->maxpacket - 1;
15157612a43dSVitaly Kuzmichev 	if (rndis_active(dev))
15167612a43dSVitaly Kuzmichev 		size += sizeof(struct rndis_packet_msg_type);
151723cd1385SRemy Bohmer 	size -= size % dev->out_ep->maxpacket;
151823cd1385SRemy Bohmer 
15196142e0aeSVitaly Kuzmichev 	/*
15206142e0aeSVitaly Kuzmichev 	 * Some platforms perform better when IP packets are aligned,
15217612a43dSVitaly Kuzmichev 	 * but on at least one, checksumming fails otherwise.  Note:
15227612a43dSVitaly Kuzmichev 	 * RNDIS headers involve variable numbers of LE32 values.
152323cd1385SRemy Bohmer 	 */
152423cd1385SRemy Bohmer 
152523cd1385SRemy Bohmer 	req->buf = (u8 *) NetRxPackets[0];
152623cd1385SRemy Bohmer 	req->length = size;
152723cd1385SRemy Bohmer 	req->complete = rx_complete;
152823cd1385SRemy Bohmer 
152923cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
153023cd1385SRemy Bohmer 
15316142e0aeSVitaly Kuzmichev 	if (retval)
15327de73185SVitaly Kuzmichev 		error("rx submit --> %d", retval);
15336142e0aeSVitaly Kuzmichev 
153423cd1385SRemy Bohmer 	return retval;
153523cd1385SRemy Bohmer }
153623cd1385SRemy Bohmer 
153723cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req)
153823cd1385SRemy Bohmer {
153923cd1385SRemy Bohmer 	struct eth_dev	*dev = ep->driver_data;
154023cd1385SRemy Bohmer 
15417de73185SVitaly Kuzmichev 	debug("%s: status %d\n", __func__, req->status);
1542c85d70efSVitaly Kuzmichev 	switch (req->status) {
1543c85d70efSVitaly Kuzmichev 	/* normal completion */
1544c85d70efSVitaly Kuzmichev 	case 0:
15457612a43dSVitaly Kuzmichev 		if (rndis_active(dev)) {
15467612a43dSVitaly Kuzmichev 			/* we know MaxPacketsPerTransfer == 1 here */
15477612a43dSVitaly Kuzmichev 			int length = rndis_rm_hdr(req->buf, req->actual);
15487612a43dSVitaly Kuzmichev 			if (length < 0)
15497612a43dSVitaly Kuzmichev 				goto length_err;
15507612a43dSVitaly Kuzmichev 			req->length -= length;
15517612a43dSVitaly Kuzmichev 			req->actual -= length;
15527612a43dSVitaly Kuzmichev 		}
15537612a43dSVitaly Kuzmichev 		if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
15547612a43dSVitaly Kuzmichev length_err:
15557612a43dSVitaly Kuzmichev 			dev->stats.rx_errors++;
15567612a43dSVitaly Kuzmichev 			dev->stats.rx_length_errors++;
15577612a43dSVitaly Kuzmichev 			debug("rx length %d\n", req->length);
15587612a43dSVitaly Kuzmichev 			break;
15597612a43dSVitaly Kuzmichev 		}
15607612a43dSVitaly Kuzmichev 
1561c85d70efSVitaly Kuzmichev 		dev->stats.rx_packets++;
1562c85d70efSVitaly Kuzmichev 		dev->stats.rx_bytes += req->length;
1563c85d70efSVitaly Kuzmichev 		break;
1564c85d70efSVitaly Kuzmichev 
1565c85d70efSVitaly Kuzmichev 	/* software-driven interface shutdown */
1566c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1567c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1568c85d70efSVitaly Kuzmichev 	/* for hardware automagic (such as pxa) */
1569c85d70efSVitaly Kuzmichev 	case -ECONNABORTED:		/* endpoint reset */
1570c85d70efSVitaly Kuzmichev 		break;
1571c85d70efSVitaly Kuzmichev 
1572c85d70efSVitaly Kuzmichev 	/* data overrun */
1573c85d70efSVitaly Kuzmichev 	case -EOVERFLOW:
1574c85d70efSVitaly Kuzmichev 		dev->stats.rx_over_errors++;
1575c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1576c85d70efSVitaly Kuzmichev 	default:
1577c85d70efSVitaly Kuzmichev 		dev->stats.rx_errors++;
1578c85d70efSVitaly Kuzmichev 		break;
1579c85d70efSVitaly Kuzmichev 	}
158023cd1385SRemy Bohmer 
158123cd1385SRemy Bohmer 	packet_received = 1;
158223cd1385SRemy Bohmer }
158323cd1385SRemy Bohmer 
158423cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
158523cd1385SRemy Bohmer {
158623cd1385SRemy Bohmer 
158723cd1385SRemy Bohmer 	dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
158823cd1385SRemy Bohmer 
158923cd1385SRemy Bohmer 	if (!dev->tx_req)
1590ac5d32d1SVitaly Kuzmichev 		goto fail1;
159123cd1385SRemy Bohmer 
159223cd1385SRemy Bohmer 	dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
159323cd1385SRemy Bohmer 
159423cd1385SRemy Bohmer 	if (!dev->rx_req)
1595ac5d32d1SVitaly Kuzmichev 		goto fail2;
159623cd1385SRemy Bohmer 
159723cd1385SRemy Bohmer 	return 0;
159823cd1385SRemy Bohmer 
1599ac5d32d1SVitaly Kuzmichev fail2:
1600ac5d32d1SVitaly Kuzmichev 	usb_ep_free_request(dev->in_ep, dev->tx_req);
1601ac5d32d1SVitaly Kuzmichev fail1:
16027de73185SVitaly Kuzmichev 	error("can't alloc requests");
160323cd1385SRemy Bohmer 	return -1;
160423cd1385SRemy Bohmer }
160523cd1385SRemy Bohmer 
160623cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req)
160723cd1385SRemy Bohmer {
1608c85d70efSVitaly Kuzmichev 	struct eth_dev	*dev = ep->driver_data;
1609c85d70efSVitaly Kuzmichev 
16107de73185SVitaly Kuzmichev 	debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1611c85d70efSVitaly Kuzmichev 	switch (req->status) {
1612c85d70efSVitaly Kuzmichev 	default:
1613c85d70efSVitaly Kuzmichev 		dev->stats.tx_errors++;
1614c85d70efSVitaly Kuzmichev 		debug("tx err %d\n", req->status);
1615c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1616c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1617c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1618c85d70efSVitaly Kuzmichev 		break;
1619c85d70efSVitaly Kuzmichev 	case 0:
1620c85d70efSVitaly Kuzmichev 		dev->stats.tx_bytes += req->length;
1621c85d70efSVitaly Kuzmichev 	}
1622c85d70efSVitaly Kuzmichev 	dev->stats.tx_packets++;
1623c85d70efSVitaly Kuzmichev 
162423cd1385SRemy Bohmer 	packet_sent = 1;
162523cd1385SRemy Bohmer }
162623cd1385SRemy Bohmer 
162723cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev)
162823cd1385SRemy Bohmer {
162923cd1385SRemy Bohmer 	/* no filters for the CDC subset; always promisc */
163023cd1385SRemy Bohmer 	if (subset_active(dev))
163123cd1385SRemy Bohmer 		return 1;
163223cd1385SRemy Bohmer 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
163323cd1385SRemy Bohmer }
163423cd1385SRemy Bohmer 
163523cd1385SRemy Bohmer #if 0
163623cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
163723cd1385SRemy Bohmer {
163823cd1385SRemy Bohmer 	struct eth_dev		*dev = netdev_priv(net);
163923cd1385SRemy Bohmer 	int			length = skb->len;
164023cd1385SRemy Bohmer 	int			retval;
164123cd1385SRemy Bohmer 	struct usb_request	*req = NULL;
164223cd1385SRemy Bohmer 	unsigned long		flags;
164323cd1385SRemy Bohmer 
164423cd1385SRemy Bohmer 	/* apply outgoing CDC or RNDIS filters */
164523cd1385SRemy Bohmer 	if (!eth_is_promisc (dev)) {
164623cd1385SRemy Bohmer 		u8		*dest = skb->data;
164723cd1385SRemy Bohmer 
1648*0adb5b76SJoe Hershberger 		if (is_multicast_ethaddr(dest)) {
164923cd1385SRemy Bohmer 			u16	type;
165023cd1385SRemy Bohmer 
165123cd1385SRemy Bohmer 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
165223cd1385SRemy Bohmer 			 * SET_ETHERNET_MULTICAST_FILTERS requests
165323cd1385SRemy Bohmer 			 */
1654*0adb5b76SJoe Hershberger 			if (is_broadcast_ethaddr(dest))
165523cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_BROADCAST;
165623cd1385SRemy Bohmer 			else
165723cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
165823cd1385SRemy Bohmer 			if (!(dev->cdc_filter & type)) {
165923cd1385SRemy Bohmer 				dev_kfree_skb_any (skb);
166023cd1385SRemy Bohmer 				return 0;
166123cd1385SRemy Bohmer 			}
166223cd1385SRemy Bohmer 		}
166323cd1385SRemy Bohmer 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
166423cd1385SRemy Bohmer 	}
166523cd1385SRemy Bohmer 
166623cd1385SRemy Bohmer 	spin_lock_irqsave(&dev->req_lock, flags);
166723cd1385SRemy Bohmer 	/*
166823cd1385SRemy Bohmer 	 * this freelist can be empty if an interrupt triggered disconnect()
166923cd1385SRemy Bohmer 	 * and reconfigured the gadget (shutting down this queue) after the
167023cd1385SRemy Bohmer 	 * network stack decided to xmit but before we got the spinlock.
167123cd1385SRemy Bohmer 	 */
167223cd1385SRemy Bohmer 	if (list_empty(&dev->tx_reqs)) {
167323cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
167423cd1385SRemy Bohmer 		return 1;
167523cd1385SRemy Bohmer 	}
167623cd1385SRemy Bohmer 
167723cd1385SRemy Bohmer 	req = container_of (dev->tx_reqs.next, struct usb_request, list);
167823cd1385SRemy Bohmer 	list_del (&req->list);
167923cd1385SRemy Bohmer 
168023cd1385SRemy Bohmer 	/* temporarily stop TX queue when the freelist empties */
168123cd1385SRemy Bohmer 	if (list_empty (&dev->tx_reqs))
168223cd1385SRemy Bohmer 		netif_stop_queue (net);
168323cd1385SRemy Bohmer 	spin_unlock_irqrestore(&dev->req_lock, flags);
168423cd1385SRemy Bohmer 
168523cd1385SRemy Bohmer 	/* no buffer copies needed, unless the network stack did it
168623cd1385SRemy Bohmer 	 * or the hardware can't use skb buffers.
168723cd1385SRemy Bohmer 	 * or there's not enough space for any RNDIS headers we need
168823cd1385SRemy Bohmer 	 */
168923cd1385SRemy Bohmer 	if (rndis_active(dev)) {
169023cd1385SRemy Bohmer 		struct sk_buff	*skb_rndis;
169123cd1385SRemy Bohmer 
169223cd1385SRemy Bohmer 		skb_rndis = skb_realloc_headroom (skb,
169323cd1385SRemy Bohmer 				sizeof (struct rndis_packet_msg_type));
169423cd1385SRemy Bohmer 		if (!skb_rndis)
169523cd1385SRemy Bohmer 			goto drop;
169623cd1385SRemy Bohmer 
169723cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
169823cd1385SRemy Bohmer 		skb = skb_rndis;
169923cd1385SRemy Bohmer 		rndis_add_hdr (skb);
170023cd1385SRemy Bohmer 		length = skb->len;
170123cd1385SRemy Bohmer 	}
170223cd1385SRemy Bohmer 	req->buf = skb->data;
170323cd1385SRemy Bohmer 	req->context = skb;
170423cd1385SRemy Bohmer 	req->complete = tx_complete;
170523cd1385SRemy Bohmer 
170623cd1385SRemy Bohmer 	/* use zlp framing on tx for strict CDC-Ether conformance,
170723cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
170823cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
170923cd1385SRemy Bohmer 	 */
171023cd1385SRemy Bohmer 	req->zero = 1;
171123cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
171223cd1385SRemy Bohmer 		length++;
171323cd1385SRemy Bohmer 
171423cd1385SRemy Bohmer 	req->length = length;
171523cd1385SRemy Bohmer 
171623cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
171723cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
171823cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
171923cd1385SRemy Bohmer 			? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
172023cd1385SRemy Bohmer 			: 0;
172123cd1385SRemy Bohmer 
172223cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
172323cd1385SRemy Bohmer 	switch (retval) {
172423cd1385SRemy Bohmer 	default:
172523cd1385SRemy Bohmer 		DEBUG (dev, "tx queue err %d\n", retval);
172623cd1385SRemy Bohmer 		break;
172723cd1385SRemy Bohmer 	case 0:
172823cd1385SRemy Bohmer 		net->trans_start = jiffies;
172923cd1385SRemy Bohmer 		atomic_inc (&dev->tx_qlen);
173023cd1385SRemy Bohmer 	}
173123cd1385SRemy Bohmer 
173223cd1385SRemy Bohmer 	if (retval) {
173323cd1385SRemy Bohmer drop:
173423cd1385SRemy Bohmer 		dev->stats.tx_dropped++;
173523cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
173623cd1385SRemy Bohmer 		spin_lock_irqsave(&dev->req_lock, flags);
173723cd1385SRemy Bohmer 		if (list_empty (&dev->tx_reqs))
173823cd1385SRemy Bohmer 			netif_start_queue (net);
173923cd1385SRemy Bohmer 		list_add (&req->list, &dev->tx_reqs);
174023cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
174123cd1385SRemy Bohmer 	}
174223cd1385SRemy Bohmer 	return 0;
174323cd1385SRemy Bohmer }
174423cd1385SRemy Bohmer 
174523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
174623cd1385SRemy Bohmer #endif
174723cd1385SRemy Bohmer 
174823cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget)
174923cd1385SRemy Bohmer {
175023cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data(gadget);
175123cd1385SRemy Bohmer 
17527de73185SVitaly Kuzmichev 	debug("%s...\n", __func__);
17537612a43dSVitaly Kuzmichev 	rndis_deregister(dev->rndis_config);
17547612a43dSVitaly Kuzmichev 	rndis_exit();
175523cd1385SRemy Bohmer 
17560129e327SVitaly Kuzmichev 	/* we've already been disconnected ... no i/o is active */
17570129e327SVitaly Kuzmichev 	if (dev->req) {
17580129e327SVitaly Kuzmichev 		usb_ep_free_request(gadget->ep0, dev->req);
17590129e327SVitaly Kuzmichev 		dev->req = NULL;
17600129e327SVitaly Kuzmichev 	}
176123cd1385SRemy Bohmer 	if (dev->stat_req) {
176223cd1385SRemy Bohmer 		usb_ep_free_request(dev->status_ep, dev->stat_req);
176323cd1385SRemy Bohmer 		dev->stat_req = NULL;
176423cd1385SRemy Bohmer 	}
176523cd1385SRemy Bohmer 
176623cd1385SRemy Bohmer 	if (dev->tx_req) {
176723cd1385SRemy Bohmer 		usb_ep_free_request(dev->in_ep, dev->tx_req);
176823cd1385SRemy Bohmer 		dev->tx_req = NULL;
176923cd1385SRemy Bohmer 	}
177023cd1385SRemy Bohmer 
177123cd1385SRemy Bohmer 	if (dev->rx_req) {
17720129e327SVitaly Kuzmichev 		usb_ep_free_request(dev->out_ep, dev->rx_req);
177323cd1385SRemy Bohmer 		dev->rx_req = NULL;
177423cd1385SRemy Bohmer 	}
177523cd1385SRemy Bohmer 
177623cd1385SRemy Bohmer /*	unregister_netdev (dev->net);*/
177723cd1385SRemy Bohmer /*	free_netdev(dev->net);*/
177823cd1385SRemy Bohmer 
1779988ee3e3SLei Wen 	dev->gadget = NULL;
178023cd1385SRemy Bohmer 	set_gadget_data(gadget, NULL);
178123cd1385SRemy Bohmer }
178223cd1385SRemy Bohmer 
178323cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget)
178423cd1385SRemy Bohmer {
178523cd1385SRemy Bohmer 	eth_reset_config(get_gadget_data(gadget));
17867612a43dSVitaly Kuzmichev 	/* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
178723cd1385SRemy Bohmer }
178823cd1385SRemy Bohmer 
178923cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget)
179023cd1385SRemy Bohmer {
179123cd1385SRemy Bohmer 	/* Not used */
179223cd1385SRemy Bohmer }
179323cd1385SRemy Bohmer 
179423cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget)
179523cd1385SRemy Bohmer {
179623cd1385SRemy Bohmer 	/* Not used */
179723cd1385SRemy Bohmer }
179823cd1385SRemy Bohmer 
179923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
180023cd1385SRemy Bohmer 
18017612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS
18027612a43dSVitaly Kuzmichev 
18037612a43dSVitaly Kuzmichev /*
18047612a43dSVitaly Kuzmichev  * The interrupt endpoint is used in RNDIS to notify the host when messages
18057612a43dSVitaly Kuzmichev  * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
18067612a43dSVitaly Kuzmichev  * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
18077612a43dSVitaly Kuzmichev  * REMOTE_NDIS_KEEPALIVE_MSG.
18087612a43dSVitaly Kuzmichev  *
18097612a43dSVitaly Kuzmichev  * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
18107612a43dSVitaly Kuzmichev  * normally just one notification will be queued.
18117612a43dSVitaly Kuzmichev  */
18127612a43dSVitaly Kuzmichev 
18137612a43dSVitaly Kuzmichev static void rndis_control_ack_complete(struct usb_ep *ep,
18147612a43dSVitaly Kuzmichev 					struct usb_request *req)
18157612a43dSVitaly Kuzmichev {
18167612a43dSVitaly Kuzmichev 	struct eth_dev          *dev = ep->driver_data;
18177612a43dSVitaly Kuzmichev 
18187612a43dSVitaly Kuzmichev 	debug("%s...\n", __func__);
18197612a43dSVitaly Kuzmichev 	if (req->status || req->actual != req->length)
18207612a43dSVitaly Kuzmichev 		debug("rndis control ack complete --> %d, %d/%d\n",
18217612a43dSVitaly Kuzmichev 			req->status, req->actual, req->length);
18227612a43dSVitaly Kuzmichev 
18237612a43dSVitaly Kuzmichev 	if (!l_ethdev.network_started) {
18247612a43dSVitaly Kuzmichev 		if (rndis_get_state(dev->rndis_config)
18257612a43dSVitaly Kuzmichev 				== RNDIS_DATA_INITIALIZED) {
18267612a43dSVitaly Kuzmichev 			l_ethdev.network_started = 1;
18277612a43dSVitaly Kuzmichev 			printf("USB RNDIS network up!\n");
18287612a43dSVitaly Kuzmichev 		}
18297612a43dSVitaly Kuzmichev 	}
18307612a43dSVitaly Kuzmichev 
18317612a43dSVitaly Kuzmichev 	req->context = NULL;
18327612a43dSVitaly Kuzmichev 
18337612a43dSVitaly Kuzmichev 	if (req != dev->stat_req)
18347612a43dSVitaly Kuzmichev 		usb_ep_free_request(ep, req);
18357612a43dSVitaly Kuzmichev }
18367612a43dSVitaly Kuzmichev 
18377612a43dSVitaly Kuzmichev static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
18387612a43dSVitaly Kuzmichev 
18397612a43dSVitaly Kuzmichev static int rndis_control_ack(struct eth_device *net)
18407612a43dSVitaly Kuzmichev {
18417612a43dSVitaly Kuzmichev 	struct eth_dev		*dev = &l_ethdev;
18427612a43dSVitaly Kuzmichev 	int                     length;
18437612a43dSVitaly Kuzmichev 	struct usb_request      *resp = dev->stat_req;
18447612a43dSVitaly Kuzmichev 
18457612a43dSVitaly Kuzmichev 	/* in case RNDIS calls this after disconnect */
18467612a43dSVitaly Kuzmichev 	if (!dev->status) {
18477612a43dSVitaly Kuzmichev 		debug("status ENODEV\n");
18487612a43dSVitaly Kuzmichev 		return -ENODEV;
18497612a43dSVitaly Kuzmichev 	}
18507612a43dSVitaly Kuzmichev 
18517612a43dSVitaly Kuzmichev 	/* in case queue length > 1 */
18527612a43dSVitaly Kuzmichev 	if (resp->context) {
18537612a43dSVitaly Kuzmichev 		resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
18547612a43dSVitaly Kuzmichev 		if (!resp)
18557612a43dSVitaly Kuzmichev 			return -ENOMEM;
18567612a43dSVitaly Kuzmichev 		resp->buf = rndis_resp_buf;
18577612a43dSVitaly Kuzmichev 	}
18587612a43dSVitaly Kuzmichev 
18597612a43dSVitaly Kuzmichev 	/*
18607612a43dSVitaly Kuzmichev 	 * Send RNDIS RESPONSE_AVAILABLE notification;
18617612a43dSVitaly Kuzmichev 	 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
18627612a43dSVitaly Kuzmichev 	 */
18637612a43dSVitaly Kuzmichev 	resp->length = 8;
18647612a43dSVitaly Kuzmichev 	resp->complete = rndis_control_ack_complete;
18657612a43dSVitaly Kuzmichev 	resp->context = dev;
18667612a43dSVitaly Kuzmichev 
18677612a43dSVitaly Kuzmichev 	*((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
18687612a43dSVitaly Kuzmichev 	*((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
18697612a43dSVitaly Kuzmichev 
18707612a43dSVitaly Kuzmichev 	length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
18717612a43dSVitaly Kuzmichev 	if (length < 0) {
18727612a43dSVitaly Kuzmichev 		resp->status = 0;
18737612a43dSVitaly Kuzmichev 		rndis_control_ack_complete(dev->status_ep, resp);
18747612a43dSVitaly Kuzmichev 	}
18757612a43dSVitaly Kuzmichev 
18767612a43dSVitaly Kuzmichev 	return 0;
18777612a43dSVitaly Kuzmichev }
18787612a43dSVitaly Kuzmichev 
18797612a43dSVitaly Kuzmichev #else
18807612a43dSVitaly Kuzmichev 
18817612a43dSVitaly Kuzmichev #define	rndis_control_ack	NULL
18827612a43dSVitaly Kuzmichev 
18837612a43dSVitaly Kuzmichev #endif	/* RNDIS */
18847612a43dSVitaly Kuzmichev 
18857612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
18867612a43dSVitaly Kuzmichev {
18877612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
18887612a43dSVitaly Kuzmichev 		rndis_set_param_medium(dev->rndis_config,
18897612a43dSVitaly Kuzmichev 					NDIS_MEDIUM_802_3,
18907612a43dSVitaly Kuzmichev 					BITRATE(dev->gadget)/100);
18917612a43dSVitaly Kuzmichev 		rndis_signal_connect(dev->rndis_config);
18927612a43dSVitaly Kuzmichev 	}
18937612a43dSVitaly Kuzmichev }
18947612a43dSVitaly Kuzmichev 
18957612a43dSVitaly Kuzmichev static int eth_stop(struct eth_dev *dev)
18967612a43dSVitaly Kuzmichev {
1897e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1898e4ae6660SVitaly Kuzmichev 	unsigned long ts;
1899e4ae6660SVitaly Kuzmichev 	unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1900e4ae6660SVitaly Kuzmichev #endif
1901e4ae6660SVitaly Kuzmichev 
19027612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
19037612a43dSVitaly Kuzmichev 		rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
19047612a43dSVitaly Kuzmichev 		rndis_signal_disconnect(dev->rndis_config);
19057612a43dSVitaly Kuzmichev 
1906e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1907e4ae6660SVitaly Kuzmichev 		/* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1908e4ae6660SVitaly Kuzmichev 		ts = get_timer(0);
1909e4ae6660SVitaly Kuzmichev 		while (get_timer(ts) < timeout)
19102d48aa69SKishon Vijay Abraham I 			usb_gadget_handle_interrupts(0);
1911e4ae6660SVitaly Kuzmichev #endif
1912e4ae6660SVitaly Kuzmichev 
19137612a43dSVitaly Kuzmichev 		rndis_uninit(dev->rndis_config);
19147612a43dSVitaly Kuzmichev 		dev->rndis = 0;
19157612a43dSVitaly Kuzmichev 	}
19167612a43dSVitaly Kuzmichev 
19177612a43dSVitaly Kuzmichev 	return 0;
19187612a43dSVitaly Kuzmichev }
19197612a43dSVitaly Kuzmichev 
19207612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
19217612a43dSVitaly Kuzmichev 
192223cd1385SRemy Bohmer static int is_eth_addr_valid(char *str)
192323cd1385SRemy Bohmer {
192423cd1385SRemy Bohmer 	if (strlen(str) == 17) {
192523cd1385SRemy Bohmer 		int i;
192623cd1385SRemy Bohmer 		char *p, *q;
192723cd1385SRemy Bohmer 		uchar ea[6];
192823cd1385SRemy Bohmer 
192923cd1385SRemy Bohmer 		/* see if it looks like an ethernet address */
193023cd1385SRemy Bohmer 
193123cd1385SRemy Bohmer 		p = str;
193223cd1385SRemy Bohmer 
193323cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
193423cd1385SRemy Bohmer 			char term = (i == 5 ? '\0' : ':');
193523cd1385SRemy Bohmer 
193623cd1385SRemy Bohmer 			ea[i] = simple_strtol(p, &q, 16);
193723cd1385SRemy Bohmer 
193823cd1385SRemy Bohmer 			if ((q - p) != 2 || *q++ != term)
193923cd1385SRemy Bohmer 				break;
194023cd1385SRemy Bohmer 
194123cd1385SRemy Bohmer 			p = q;
194223cd1385SRemy Bohmer 		}
194323cd1385SRemy Bohmer 
194457a87a25STom Rini 		/* Now check the contents. */
1945*0adb5b76SJoe Hershberger 		return is_valid_ethaddr(ea);
194623cd1385SRemy Bohmer 	}
194723cd1385SRemy Bohmer 	return 0;
194823cd1385SRemy Bohmer }
194923cd1385SRemy Bohmer 
195023cd1385SRemy Bohmer static u8 nibble(unsigned char c)
195123cd1385SRemy Bohmer {
195223cd1385SRemy Bohmer 	if (likely(isdigit(c)))
195323cd1385SRemy Bohmer 		return c - '0';
195423cd1385SRemy Bohmer 	c = toupper(c);
195523cd1385SRemy Bohmer 	if (likely(isxdigit(c)))
195623cd1385SRemy Bohmer 		return 10 + c - 'A';
195723cd1385SRemy Bohmer 	return 0;
195823cd1385SRemy Bohmer }
195923cd1385SRemy Bohmer 
196023cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr)
196123cd1385SRemy Bohmer {
196223cd1385SRemy Bohmer 	if (str) {
196323cd1385SRemy Bohmer 		unsigned	i;
196423cd1385SRemy Bohmer 
196523cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
196623cd1385SRemy Bohmer 			unsigned char num;
196723cd1385SRemy Bohmer 
196823cd1385SRemy Bohmer 			if ((*str == '.') || (*str == ':'))
196923cd1385SRemy Bohmer 				str++;
197023cd1385SRemy Bohmer 			num = nibble(*str++) << 4;
197123cd1385SRemy Bohmer 			num |= (nibble(*str++));
197223cd1385SRemy Bohmer 			dev_addr[i] = num;
197323cd1385SRemy Bohmer 		}
1974*0adb5b76SJoe Hershberger 		if (is_valid_ethaddr(dev_addr))
197523cd1385SRemy Bohmer 			return 0;
197623cd1385SRemy Bohmer 	}
197723cd1385SRemy Bohmer 	return 1;
197823cd1385SRemy Bohmer }
197923cd1385SRemy Bohmer 
198023cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget)
198123cd1385SRemy Bohmer {
198223cd1385SRemy Bohmer 	struct eth_dev		*dev = &l_ethdev;
19837612a43dSVitaly Kuzmichev 	u8			cdc = 1, zlp = 1, rndis = 1;
198423cd1385SRemy Bohmer 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
19857612a43dSVitaly Kuzmichev 	int			status = -ENOMEM;
198623cd1385SRemy Bohmer 	int			gcnum;
198723cd1385SRemy Bohmer 	u8			tmp[7];
198823cd1385SRemy Bohmer 
198923cd1385SRemy Bohmer 	/* these flags are only ever cleared; compiler take note */
19902bb37884SLukasz Dalek #ifndef	CONFIG_USB_ETH_CDC
199123cd1385SRemy Bohmer 	cdc = 0;
199223cd1385SRemy Bohmer #endif
19937612a43dSVitaly Kuzmichev #ifndef	CONFIG_USB_ETH_RNDIS
19947612a43dSVitaly Kuzmichev 	rndis = 0;
19957612a43dSVitaly Kuzmichev #endif
19966142e0aeSVitaly Kuzmichev 	/*
19976142e0aeSVitaly Kuzmichev 	 * Because most host side USB stacks handle CDC Ethernet, that
199823cd1385SRemy Bohmer 	 * standard protocol is _strongly_ preferred for interop purposes.
199923cd1385SRemy Bohmer 	 * (By everyone except Microsoft.)
200023cd1385SRemy Bohmer 	 */
200123cd1385SRemy Bohmer 	if (gadget_is_pxa(gadget)) {
200223cd1385SRemy Bohmer 		/* pxa doesn't support altsettings */
200323cd1385SRemy Bohmer 		cdc = 0;
200423cd1385SRemy Bohmer 	} else if (gadget_is_musbhdrc(gadget)) {
200523cd1385SRemy Bohmer 		/* reduce tx dma overhead by avoiding special cases */
200623cd1385SRemy Bohmer 		zlp = 0;
200723cd1385SRemy Bohmer 	} else if (gadget_is_sh(gadget)) {
200823cd1385SRemy Bohmer 		/* sh doesn't support multiple interfaces or configs */
200923cd1385SRemy Bohmer 		cdc = 0;
20107612a43dSVitaly Kuzmichev 		rndis = 0;
201123cd1385SRemy Bohmer 	} else if (gadget_is_sa1100(gadget)) {
201223cd1385SRemy Bohmer 		/* hardware can't write zlps */
201323cd1385SRemy Bohmer 		zlp = 0;
20146142e0aeSVitaly Kuzmichev 		/*
20156142e0aeSVitaly Kuzmichev 		 * sa1100 CAN do CDC, without status endpoint ... we use
201623cd1385SRemy Bohmer 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
201723cd1385SRemy Bohmer 		 */
201823cd1385SRemy Bohmer 		cdc = 0;
201923cd1385SRemy Bohmer 	}
202023cd1385SRemy Bohmer 
202123cd1385SRemy Bohmer 	gcnum = usb_gadget_controller_number(gadget);
202223cd1385SRemy Bohmer 	if (gcnum >= 0)
202323cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
202423cd1385SRemy Bohmer 	else {
20256142e0aeSVitaly Kuzmichev 		/*
20266142e0aeSVitaly Kuzmichev 		 * can't assume CDC works.  don't want to default to
202723cd1385SRemy Bohmer 		 * anything less functional on CDC-capable hardware,
202823cd1385SRemy Bohmer 		 * so we fail in this case.
202923cd1385SRemy Bohmer 		 */
20307de73185SVitaly Kuzmichev 		error("controller '%s' not recognized",
203123cd1385SRemy Bohmer 			gadget->name);
203223cd1385SRemy Bohmer 		return -ENODEV;
203323cd1385SRemy Bohmer 	}
203423cd1385SRemy Bohmer 
20356142e0aeSVitaly Kuzmichev 	/*
20367612a43dSVitaly Kuzmichev 	 * If there's an RNDIS configuration, that's what Windows wants to
20377612a43dSVitaly Kuzmichev 	 * be using ... so use these product IDs here and in the "linux.inf"
20387612a43dSVitaly Kuzmichev 	 * needed to install MSFT drivers.  Current Linux kernels will use
20397612a43dSVitaly Kuzmichev 	 * the second configuration if it's CDC Ethernet, and need some help
20407612a43dSVitaly Kuzmichev 	 * to choose the right configuration otherwise.
20417612a43dSVitaly Kuzmichev 	 */
20427612a43dSVitaly Kuzmichev 	if (rndis) {
20437612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
20447612a43dSVitaly Kuzmichev 		device_desc.idVendor =
20457612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
20467612a43dSVitaly Kuzmichev 		device_desc.idProduct =
20477612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
20487612a43dSVitaly Kuzmichev #else
20497612a43dSVitaly Kuzmichev 		device_desc.idVendor =
20507612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(RNDIS_VENDOR_NUM);
20517612a43dSVitaly Kuzmichev 		device_desc.idProduct =
20527612a43dSVitaly Kuzmichev 			__constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
20537612a43dSVitaly Kuzmichev #endif
20547612a43dSVitaly Kuzmichev 		sprintf(product_desc, "RNDIS/%s", driver_desc);
20557612a43dSVitaly Kuzmichev 
20567612a43dSVitaly Kuzmichev 	/*
20576142e0aeSVitaly Kuzmichev 	 * CDC subset ... recognized by Linux since 2.4.10, but Windows
205823cd1385SRemy Bohmer 	 * drivers aren't widely available.  (That may be improved by
205923cd1385SRemy Bohmer 	 * supporting one submode of the "SAFE" variant of MDLM.)
206023cd1385SRemy Bohmer 	 */
20617612a43dSVitaly Kuzmichev 	} else {
20627612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
20637612a43dSVitaly Kuzmichev 		device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
20647612a43dSVitaly Kuzmichev 		device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
20657612a43dSVitaly Kuzmichev #else
206623cd1385SRemy Bohmer 		if (!cdc) {
206723cd1385SRemy Bohmer 			device_desc.idVendor =
206823cd1385SRemy Bohmer 				__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
206923cd1385SRemy Bohmer 			device_desc.idProduct =
207023cd1385SRemy Bohmer 				__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
207123cd1385SRemy Bohmer 		}
207223cd1385SRemy Bohmer #endif
20737612a43dSVitaly Kuzmichev 	}
20747612a43dSVitaly Kuzmichev 	/* support optional vendor/distro customization */
207523cd1385SRemy Bohmer 	if (bcdDevice)
207623cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(bcdDevice);
207723cd1385SRemy Bohmer 	if (iManufacturer)
20782e12abe6SVitaly Kuzmichev 		strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
207923cd1385SRemy Bohmer 	if (iProduct)
20802e12abe6SVitaly Kuzmichev 		strlcpy(product_desc, iProduct, sizeof product_desc);
208123cd1385SRemy Bohmer 	if (iSerialNumber) {
208223cd1385SRemy Bohmer 		device_desc.iSerialNumber = STRING_SERIALNUMBER,
20832e12abe6SVitaly Kuzmichev 		strlcpy(serial_number, iSerialNumber, sizeof serial_number);
208423cd1385SRemy Bohmer 	}
208523cd1385SRemy Bohmer 
208623cd1385SRemy Bohmer 	/* all we really need is bulk IN/OUT */
208723cd1385SRemy Bohmer 	usb_ep_autoconfig_reset(gadget);
208823cd1385SRemy Bohmer 	in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
208923cd1385SRemy Bohmer 	if (!in_ep) {
209023cd1385SRemy Bohmer autoconf_fail:
20917de73185SVitaly Kuzmichev 		error("can't autoconfigure on %s\n",
209223cd1385SRemy Bohmer 			gadget->name);
209323cd1385SRemy Bohmer 		return -ENODEV;
209423cd1385SRemy Bohmer 	}
209523cd1385SRemy Bohmer 	in_ep->driver_data = in_ep;	/* claim */
209623cd1385SRemy Bohmer 
209723cd1385SRemy Bohmer 	out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
209823cd1385SRemy Bohmer 	if (!out_ep)
209923cd1385SRemy Bohmer 		goto autoconf_fail;
210023cd1385SRemy Bohmer 	out_ep->driver_data = out_ep;	/* claim */
210123cd1385SRemy Bohmer 
21022bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
21036142e0aeSVitaly Kuzmichev 	/*
21046142e0aeSVitaly Kuzmichev 	 * CDC Ethernet control interface doesn't require a status endpoint.
210523cd1385SRemy Bohmer 	 * Since some hosts expect one, try to allocate one anyway.
210623cd1385SRemy Bohmer 	 */
21077612a43dSVitaly Kuzmichev 	if (cdc || rndis) {
210823cd1385SRemy Bohmer 		status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
210923cd1385SRemy Bohmer 		if (status_ep) {
211023cd1385SRemy Bohmer 			status_ep->driver_data = status_ep;	/* claim */
21117612a43dSVitaly Kuzmichev 		} else if (rndis) {
21127612a43dSVitaly Kuzmichev 			error("can't run RNDIS on %s", gadget->name);
21137612a43dSVitaly Kuzmichev 			return -ENODEV;
21142bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC
211523cd1385SRemy Bohmer 		} else if (cdc) {
211623cd1385SRemy Bohmer 			control_intf.bNumEndpoints = 0;
211723cd1385SRemy Bohmer 			/* FIXME remove endpoint from descriptor list */
21187612a43dSVitaly Kuzmichev #endif
211923cd1385SRemy Bohmer 		}
212023cd1385SRemy Bohmer 	}
212123cd1385SRemy Bohmer #endif
212223cd1385SRemy Bohmer 
212323cd1385SRemy Bohmer 	/* one config:  cdc, else minimal subset */
212423cd1385SRemy Bohmer 	if (!cdc) {
212523cd1385SRemy Bohmer 		eth_config.bNumInterfaces = 1;
212623cd1385SRemy Bohmer 		eth_config.iConfiguration = STRING_SUBSET;
212723cd1385SRemy Bohmer 
21286142e0aeSVitaly Kuzmichev 		/*
21296142e0aeSVitaly Kuzmichev 		 * use functions to set these up, in case we're built to work
213023cd1385SRemy Bohmer 		 * with multiple controllers and must override CDC Ethernet.
213123cd1385SRemy Bohmer 		 */
213223cd1385SRemy Bohmer 		fs_subset_descriptors();
213323cd1385SRemy Bohmer 		hs_subset_descriptors();
213423cd1385SRemy Bohmer 	}
213523cd1385SRemy Bohmer 
213623cd1385SRemy Bohmer 	usb_gadget_set_selfpowered(gadget);
213723cd1385SRemy Bohmer 
21387612a43dSVitaly Kuzmichev 	/* For now RNDIS is always a second config */
21397612a43dSVitaly Kuzmichev 	if (rndis)
21407612a43dSVitaly Kuzmichev 		device_desc.bNumConfigurations = 2;
21417612a43dSVitaly Kuzmichev 
214223cd1385SRemy Bohmer 	if (gadget_is_dualspeed(gadget)) {
21437612a43dSVitaly Kuzmichev 		if (rndis)
21447612a43dSVitaly Kuzmichev 			dev_qualifier.bNumConfigurations = 2;
21457612a43dSVitaly Kuzmichev 		else if (!cdc)
214623cd1385SRemy Bohmer 			dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
214723cd1385SRemy Bohmer 
214823cd1385SRemy Bohmer 		/* assumes ep0 uses the same value for both speeds ... */
214923cd1385SRemy Bohmer 		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
215023cd1385SRemy Bohmer 
215123cd1385SRemy Bohmer 		/* and that all endpoints are dual-speed */
215223cd1385SRemy Bohmer 		hs_source_desc.bEndpointAddress =
215323cd1385SRemy Bohmer 				fs_source_desc.bEndpointAddress;
215423cd1385SRemy Bohmer 		hs_sink_desc.bEndpointAddress =
215523cd1385SRemy Bohmer 				fs_sink_desc.bEndpointAddress;
21562bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
215723cd1385SRemy Bohmer 		if (status_ep)
215823cd1385SRemy Bohmer 			hs_status_desc.bEndpointAddress =
215923cd1385SRemy Bohmer 					fs_status_desc.bEndpointAddress;
216023cd1385SRemy Bohmer #endif
216123cd1385SRemy Bohmer 	}
216223cd1385SRemy Bohmer 
216323cd1385SRemy Bohmer 	if (gadget_is_otg(gadget)) {
216423cd1385SRemy Bohmer 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
216523cd1385SRemy Bohmer 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
216623cd1385SRemy Bohmer 		eth_config.bMaxPower = 4;
21677612a43dSVitaly Kuzmichev #ifdef	CONFIG_USB_ETH_RNDIS
21687612a43dSVitaly Kuzmichev 		rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
21697612a43dSVitaly Kuzmichev 		rndis_config.bMaxPower = 4;
21707612a43dSVitaly Kuzmichev #endif
217123cd1385SRemy Bohmer 	}
217223cd1385SRemy Bohmer 
21737612a43dSVitaly Kuzmichev 
21747612a43dSVitaly Kuzmichev 	/* network device setup */
217523cd1385SRemy Bohmer 	dev->net = &l_netdev;
217623cd1385SRemy Bohmer 
217723cd1385SRemy Bohmer 	dev->cdc = cdc;
217823cd1385SRemy Bohmer 	dev->zlp = zlp;
217923cd1385SRemy Bohmer 
218023cd1385SRemy Bohmer 	dev->in_ep = in_ep;
218123cd1385SRemy Bohmer 	dev->out_ep = out_ep;
218223cd1385SRemy Bohmer 	dev->status_ep = status_ep;
218323cd1385SRemy Bohmer 
21846142e0aeSVitaly Kuzmichev 	/*
21856142e0aeSVitaly Kuzmichev 	 * Module params for these addresses should come from ID proms.
21867612a43dSVitaly Kuzmichev 	 * The host side address is used with CDC and RNDIS, and commonly
218723cd1385SRemy Bohmer 	 * ends up in a persistent config database.  It's not clear if
218823cd1385SRemy Bohmer 	 * host side code for the SAFE thing cares -- its original BLAN
218923cd1385SRemy Bohmer 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
219023cd1385SRemy Bohmer 	 */
219123cd1385SRemy Bohmer 	get_ether_addr(dev_addr, dev->net->enetaddr);
219223cd1385SRemy Bohmer 
219323cd1385SRemy Bohmer 	memset(tmp, 0, sizeof(tmp));
219423cd1385SRemy Bohmer 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
219523cd1385SRemy Bohmer 
219623cd1385SRemy Bohmer 	get_ether_addr(host_addr, dev->host_mac);
219723cd1385SRemy Bohmer 
219823cd1385SRemy Bohmer 	sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
219923cd1385SRemy Bohmer 		dev->host_mac[0], dev->host_mac[1],
220023cd1385SRemy Bohmer 			dev->host_mac[2], dev->host_mac[3],
220123cd1385SRemy Bohmer 			dev->host_mac[4], dev->host_mac[5]);
220223cd1385SRemy Bohmer 
22037612a43dSVitaly Kuzmichev 	if (rndis) {
22047612a43dSVitaly Kuzmichev 		status = rndis_init();
22057612a43dSVitaly Kuzmichev 		if (status < 0) {
22067612a43dSVitaly Kuzmichev 			error("can't init RNDIS, %d", status);
22077612a43dSVitaly Kuzmichev 			goto fail;
22087612a43dSVitaly Kuzmichev 		}
220923cd1385SRemy Bohmer 	}
221023cd1385SRemy Bohmer 
22116142e0aeSVitaly Kuzmichev 	/*
22126142e0aeSVitaly Kuzmichev 	 * use PKTSIZE (or aligned... from u-boot) and set
22136142e0aeSVitaly Kuzmichev 	 * wMaxSegmentSize accordingly
22146142e0aeSVitaly Kuzmichev 	 */
221523cd1385SRemy Bohmer 	dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
221623cd1385SRemy Bohmer 
221723cd1385SRemy Bohmer 	/* preallocate control message data and buffer */
221823cd1385SRemy Bohmer 	dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
221923cd1385SRemy Bohmer 	if (!dev->req)
222023cd1385SRemy Bohmer 		goto fail;
222123cd1385SRemy Bohmer 	dev->req->buf = control_req;
222223cd1385SRemy Bohmer 	dev->req->complete = eth_setup_complete;
222323cd1385SRemy Bohmer 
222423cd1385SRemy Bohmer 	/* ... and maybe likewise for status transfer */
22252bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
222623cd1385SRemy Bohmer 	if (dev->status_ep) {
22276142e0aeSVitaly Kuzmichev 		dev->stat_req = usb_ep_alloc_request(dev->status_ep,
22286142e0aeSVitaly Kuzmichev 							GFP_KERNEL);
222923cd1385SRemy Bohmer 		if (!dev->stat_req) {
2230df559c1dSVitaly Kuzmichev 			usb_ep_free_request(dev->status_ep, dev->req);
223123cd1385SRemy Bohmer 
223223cd1385SRemy Bohmer 			goto fail;
223323cd1385SRemy Bohmer 		}
2234df559c1dSVitaly Kuzmichev 		dev->stat_req->buf = status_req;
223523cd1385SRemy Bohmer 		dev->stat_req->context = NULL;
223623cd1385SRemy Bohmer 	}
223723cd1385SRemy Bohmer #endif
223823cd1385SRemy Bohmer 
223923cd1385SRemy Bohmer 	/* finish hookup to lower layer ... */
224023cd1385SRemy Bohmer 	dev->gadget = gadget;
224123cd1385SRemy Bohmer 	set_gadget_data(gadget, dev);
224223cd1385SRemy Bohmer 	gadget->ep0->driver_data = dev;
224323cd1385SRemy Bohmer 
22446142e0aeSVitaly Kuzmichev 	/*
22456142e0aeSVitaly Kuzmichev 	 * two kinds of host-initiated state changes:
224623cd1385SRemy Bohmer 	 *  - iff DATA transfer is active, carrier is "on"
224723cd1385SRemy Bohmer 	 *  - tx queueing enabled if open *and* carrier is "on"
224823cd1385SRemy Bohmer 	 */
22497612a43dSVitaly Kuzmichev 
22507612a43dSVitaly Kuzmichev 	printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
22517612a43dSVitaly Kuzmichev 		out_ep->name, in_ep->name,
22527612a43dSVitaly Kuzmichev 		status_ep ? " STATUS " : "",
22537612a43dSVitaly Kuzmichev 		status_ep ? status_ep->name : ""
22547612a43dSVitaly Kuzmichev 		);
22557612a43dSVitaly Kuzmichev 	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
22567612a43dSVitaly Kuzmichev 		dev->net->enetaddr[0], dev->net->enetaddr[1],
22577612a43dSVitaly Kuzmichev 		dev->net->enetaddr[2], dev->net->enetaddr[3],
22587612a43dSVitaly Kuzmichev 		dev->net->enetaddr[4], dev->net->enetaddr[5]);
22597612a43dSVitaly Kuzmichev 
22607612a43dSVitaly Kuzmichev 	if (cdc || rndis)
22617612a43dSVitaly Kuzmichev 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
22627612a43dSVitaly Kuzmichev 			dev->host_mac[0], dev->host_mac[1],
22637612a43dSVitaly Kuzmichev 			dev->host_mac[2], dev->host_mac[3],
22647612a43dSVitaly Kuzmichev 			dev->host_mac[4], dev->host_mac[5]);
22657612a43dSVitaly Kuzmichev 
22667612a43dSVitaly Kuzmichev 	if (rndis) {
22677612a43dSVitaly Kuzmichev 		u32	vendorID = 0;
22687612a43dSVitaly Kuzmichev 
22697612a43dSVitaly Kuzmichev 		/* FIXME RNDIS vendor id == "vendor NIC code" == ? */
22707612a43dSVitaly Kuzmichev 
22717612a43dSVitaly Kuzmichev 		dev->rndis_config = rndis_register(rndis_control_ack);
22727612a43dSVitaly Kuzmichev 		if (dev->rndis_config < 0) {
22737612a43dSVitaly Kuzmichev fail0:
22747612a43dSVitaly Kuzmichev 			eth_unbind(gadget);
22757612a43dSVitaly Kuzmichev 			debug("RNDIS setup failed\n");
22767612a43dSVitaly Kuzmichev 			status = -ENODEV;
22777612a43dSVitaly Kuzmichev 			goto fail;
22787612a43dSVitaly Kuzmichev 		}
22797612a43dSVitaly Kuzmichev 
22807612a43dSVitaly Kuzmichev 		/* these set up a lot of the OIDs that RNDIS needs */
22817612a43dSVitaly Kuzmichev 		rndis_set_host_mac(dev->rndis_config, dev->host_mac);
22827612a43dSVitaly Kuzmichev 		if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
22837612a43dSVitaly Kuzmichev 					&dev->stats, &dev->cdc_filter))
22847612a43dSVitaly Kuzmichev 			goto fail0;
22857612a43dSVitaly Kuzmichev 		if (rndis_set_param_vendor(dev->rndis_config, vendorID,
22867612a43dSVitaly Kuzmichev 					manufacturer))
22877612a43dSVitaly Kuzmichev 			goto fail0;
22887612a43dSVitaly Kuzmichev 		if (rndis_set_param_medium(dev->rndis_config,
22897612a43dSVitaly Kuzmichev 					NDIS_MEDIUM_802_3, 0))
22907612a43dSVitaly Kuzmichev 			goto fail0;
22917612a43dSVitaly Kuzmichev 		printf("RNDIS ready\n");
22927612a43dSVitaly Kuzmichev 	}
229323cd1385SRemy Bohmer 	return 0;
229423cd1385SRemy Bohmer 
229523cd1385SRemy Bohmer fail:
22967612a43dSVitaly Kuzmichev 	error("%s failed, status = %d", __func__, status);
229723cd1385SRemy Bohmer 	eth_unbind(gadget);
22987612a43dSVitaly Kuzmichev 	return status;
229923cd1385SRemy Bohmer }
230023cd1385SRemy Bohmer 
23017612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/
23027612a43dSVitaly Kuzmichev 
230323cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
230423cd1385SRemy Bohmer {
230523cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
230623cd1385SRemy Bohmer 	struct usb_gadget *gadget;
230723cd1385SRemy Bohmer 	unsigned long ts;
230823cd1385SRemy Bohmer 	unsigned long timeout = USB_CONNECT_TIMEOUT;
230923cd1385SRemy Bohmer 
231023cd1385SRemy Bohmer 	if (!netdev) {
23117de73185SVitaly Kuzmichev 		error("received NULL ptr");
231223cd1385SRemy Bohmer 		goto fail;
231323cd1385SRemy Bohmer 	}
231458939fccSVitaly Kuzmichev 
231558939fccSVitaly Kuzmichev 	/* Configure default mac-addresses for the USB ethernet device */
231658939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR
231758939fccSVitaly Kuzmichev 	strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
231858939fccSVitaly Kuzmichev #endif
231958939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR
232058939fccSVitaly Kuzmichev 	strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
232158939fccSVitaly Kuzmichev #endif
232258939fccSVitaly Kuzmichev 	/* Check if the user overruled the MAC addresses */
232358939fccSVitaly Kuzmichev 	if (getenv("usbnet_devaddr"))
232458939fccSVitaly Kuzmichev 		strlcpy(dev_addr, getenv("usbnet_devaddr"),
232558939fccSVitaly Kuzmichev 			sizeof(dev_addr));
232658939fccSVitaly Kuzmichev 
232758939fccSVitaly Kuzmichev 	if (getenv("usbnet_hostaddr"))
232858939fccSVitaly Kuzmichev 		strlcpy(host_addr, getenv("usbnet_hostaddr"),
232958939fccSVitaly Kuzmichev 			sizeof(host_addr));
233058939fccSVitaly Kuzmichev 
233158939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(dev_addr)) {
233258939fccSVitaly Kuzmichev 		error("Need valid 'usbnet_devaddr' to be set");
233358939fccSVitaly Kuzmichev 		goto fail;
233458939fccSVitaly Kuzmichev 	}
233558939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(host_addr)) {
233658939fccSVitaly Kuzmichev 		error("Need valid 'usbnet_hostaddr' to be set");
233758939fccSVitaly Kuzmichev 		goto fail;
233858939fccSVitaly Kuzmichev 	}
233958939fccSVitaly Kuzmichev 
2340988ee3e3SLei Wen 	if (usb_gadget_register_driver(&eth_driver) < 0)
2341988ee3e3SLei Wen 		goto fail;
234223cd1385SRemy Bohmer 
234323cd1385SRemy Bohmer 	dev->network_started = 0;
234423cd1385SRemy Bohmer 
234523cd1385SRemy Bohmer 	packet_received = 0;
234623cd1385SRemy Bohmer 	packet_sent = 0;
234723cd1385SRemy Bohmer 
234823cd1385SRemy Bohmer 	gadget = dev->gadget;
234923cd1385SRemy Bohmer 	usb_gadget_connect(gadget);
235023cd1385SRemy Bohmer 
235123cd1385SRemy Bohmer 	if (getenv("cdc_connect_timeout"))
235223cd1385SRemy Bohmer 		timeout = simple_strtoul(getenv("cdc_connect_timeout"),
235323cd1385SRemy Bohmer 						NULL, 10) * CONFIG_SYS_HZ;
235423cd1385SRemy Bohmer 	ts = get_timer(0);
23556142e0aeSVitaly Kuzmichev 	while (!l_ethdev.network_started) {
235623cd1385SRemy Bohmer 		/* Handle control-c and timeouts */
235723cd1385SRemy Bohmer 		if (ctrlc() || (get_timer(ts) > timeout)) {
23587de73185SVitaly Kuzmichev 			error("The remote end did not respond in time.");
235923cd1385SRemy Bohmer 			goto fail;
236023cd1385SRemy Bohmer 		}
23612d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
236223cd1385SRemy Bohmer 	}
236323cd1385SRemy Bohmer 
236498fae970SVitaly Kuzmichev 	packet_received = 0;
236523cd1385SRemy Bohmer 	rx_submit(dev, dev->rx_req, 0);
236623cd1385SRemy Bohmer 	return 0;
236723cd1385SRemy Bohmer fail:
236823cd1385SRemy Bohmer 	return -1;
236923cd1385SRemy Bohmer }
237023cd1385SRemy Bohmer 
237110cbe3b6SJoe Hershberger static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
237223cd1385SRemy Bohmer {
237323cd1385SRemy Bohmer 	int			retval;
23747612a43dSVitaly Kuzmichev 	void			*rndis_pkt = NULL;
237523cd1385SRemy Bohmer 	struct eth_dev		*dev = &l_ethdev;
2376ac5d32d1SVitaly Kuzmichev 	struct usb_request	*req = dev->tx_req;
2377a170f2c7SStefano Babic 	unsigned long ts;
2378a170f2c7SStefano Babic 	unsigned long timeout = USB_CONNECT_TIMEOUT;
23797de73185SVitaly Kuzmichev 
23807de73185SVitaly Kuzmichev 	debug("%s:...\n", __func__);
238123cd1385SRemy Bohmer 
23827612a43dSVitaly Kuzmichev 	/* new buffer is needed to include RNDIS header */
23837612a43dSVitaly Kuzmichev 	if (rndis_active(dev)) {
23847612a43dSVitaly Kuzmichev 		rndis_pkt = malloc(length +
23857612a43dSVitaly Kuzmichev 					sizeof(struct rndis_packet_msg_type));
23867612a43dSVitaly Kuzmichev 		if (!rndis_pkt) {
23877612a43dSVitaly Kuzmichev 			error("No memory to alloc RNDIS packet");
23887612a43dSVitaly Kuzmichev 			goto drop;
23897612a43dSVitaly Kuzmichev 		}
23907612a43dSVitaly Kuzmichev 		rndis_add_hdr(rndis_pkt, length);
23917612a43dSVitaly Kuzmichev 		memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
239210cbe3b6SJoe Hershberger 				packet, length);
23937612a43dSVitaly Kuzmichev 		packet = rndis_pkt;
23947612a43dSVitaly Kuzmichev 		length += sizeof(struct rndis_packet_msg_type);
23957612a43dSVitaly Kuzmichev 	}
239610cbe3b6SJoe Hershberger 	req->buf = packet;
239723cd1385SRemy Bohmer 	req->context = NULL;
239823cd1385SRemy Bohmer 	req->complete = tx_complete;
239923cd1385SRemy Bohmer 
24006142e0aeSVitaly Kuzmichev 	/*
24016142e0aeSVitaly Kuzmichev 	 * use zlp framing on tx for strict CDC-Ether conformance,
240223cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
240323cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
240423cd1385SRemy Bohmer 	 */
240523cd1385SRemy Bohmer 	req->zero = 1;
240623cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
240723cd1385SRemy Bohmer 		length++;
240823cd1385SRemy Bohmer 
240923cd1385SRemy Bohmer 	req->length = length;
241023cd1385SRemy Bohmer #if 0
241123cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
241223cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
241323cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
241423cd1385SRemy Bohmer 			? ((dev->tx_qlen % qmult) != 0) : 0;
241523cd1385SRemy Bohmer #endif
241623cd1385SRemy Bohmer 	dev->tx_qlen = 1;
2417a170f2c7SStefano Babic 	ts = get_timer(0);
2418a170f2c7SStefano Babic 	packet_sent = 0;
241923cd1385SRemy Bohmer 
242023cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
242123cd1385SRemy Bohmer 
242223cd1385SRemy Bohmer 	if (!retval)
24237de73185SVitaly Kuzmichev 		debug("%s: packet queued\n", __func__);
24246142e0aeSVitaly Kuzmichev 	while (!packet_sent) {
2425a170f2c7SStefano Babic 		if (get_timer(ts) > timeout) {
2426a170f2c7SStefano Babic 			printf("timeout sending packets to usb ethernet\n");
2427a170f2c7SStefano Babic 			return -1;
2428a170f2c7SStefano Babic 		}
24292d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
243023cd1385SRemy Bohmer 	}
24317612a43dSVitaly Kuzmichev 	if (rndis_pkt)
24327612a43dSVitaly Kuzmichev 		free(rndis_pkt);
243323cd1385SRemy Bohmer 
243423cd1385SRemy Bohmer 	return 0;
24357612a43dSVitaly Kuzmichev drop:
24367612a43dSVitaly Kuzmichev 	dev->stats.tx_dropped++;
24377612a43dSVitaly Kuzmichev 	return -ENOMEM;
243823cd1385SRemy Bohmer }
243923cd1385SRemy Bohmer 
244023cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev)
244123cd1385SRemy Bohmer {
244223cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
244323cd1385SRemy Bohmer 
24442d48aa69SKishon Vijay Abraham I 	usb_gadget_handle_interrupts(0);
244523cd1385SRemy Bohmer 
24466142e0aeSVitaly Kuzmichev 	if (packet_received) {
24477de73185SVitaly Kuzmichev 		debug("%s: packet received\n", __func__);
24486142e0aeSVitaly Kuzmichev 		if (dev->rx_req) {
244923cd1385SRemy Bohmer 			NetReceive(NetRxPackets[0], dev->rx_req->length);
245023cd1385SRemy Bohmer 			packet_received = 0;
245123cd1385SRemy Bohmer 
245223cd1385SRemy Bohmer 			rx_submit(dev, dev->rx_req, 0);
24536142e0aeSVitaly Kuzmichev 		} else
24546142e0aeSVitaly Kuzmichev 			error("dev->rx_req invalid");
245523cd1385SRemy Bohmer 	}
245623cd1385SRemy Bohmer 	return 0;
245723cd1385SRemy Bohmer }
245823cd1385SRemy Bohmer 
245923cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev)
246023cd1385SRemy Bohmer {
246123cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
246223cd1385SRemy Bohmer 
24636142e0aeSVitaly Kuzmichev 	if (!netdev) {
24647de73185SVitaly Kuzmichev 		error("received NULL ptr");
246523cd1385SRemy Bohmer 		return;
246623cd1385SRemy Bohmer 	}
246723cd1385SRemy Bohmer 
2468988ee3e3SLei Wen 	/* If the gadget not registered, simple return */
2469988ee3e3SLei Wen 	if (!dev->gadget)
2470988ee3e3SLei Wen 		return;
2471988ee3e3SLei Wen 
2472e4ae6660SVitaly Kuzmichev 	/*
2473e4ae6660SVitaly Kuzmichev 	 * Some USB controllers may need additional deinitialization here
2474e4ae6660SVitaly Kuzmichev 	 * before dropping pull-up (also due to hardware issues).
2475e4ae6660SVitaly Kuzmichev 	 * For example: unhandled interrupt with status stage started may
2476e4ae6660SVitaly Kuzmichev 	 * bring the controller to fully broken state (until board reset).
2477e4ae6660SVitaly Kuzmichev 	 * There are some variants to debug and fix such cases:
2478e4ae6660SVitaly Kuzmichev 	 * 1) In the case of RNDIS connection eth_stop can perform additional
2479e4ae6660SVitaly Kuzmichev 	 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2480e4ae6660SVitaly Kuzmichev 	 * 2) 'pullup' callback in your UDC driver can be improved to perform
2481e4ae6660SVitaly Kuzmichev 	 * this deinitialization.
2482e4ae6660SVitaly Kuzmichev 	 */
24837612a43dSVitaly Kuzmichev 	eth_stop(dev);
24847612a43dSVitaly Kuzmichev 
248523cd1385SRemy Bohmer 	usb_gadget_disconnect(dev->gadget);
2486b3649f3bSVitaly Kuzmichev 
2487b3649f3bSVitaly Kuzmichev 	/* Clear pending interrupt */
2488b3649f3bSVitaly Kuzmichev 	if (dev->network_started) {
24892d48aa69SKishon Vijay Abraham I 		usb_gadget_handle_interrupts(0);
2490b3649f3bSVitaly Kuzmichev 		dev->network_started = 0;
2491b3649f3bSVitaly Kuzmichev 	}
2492b3649f3bSVitaly Kuzmichev 
2493988ee3e3SLei Wen 	usb_gadget_unregister_driver(&eth_driver);
249423cd1385SRemy Bohmer }
249523cd1385SRemy Bohmer 
249623cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = {
249723cd1385SRemy Bohmer 	.speed		= DEVSPEED,
249823cd1385SRemy Bohmer 
249923cd1385SRemy Bohmer 	.bind		= eth_bind,
250023cd1385SRemy Bohmer 	.unbind		= eth_unbind,
250123cd1385SRemy Bohmer 
250223cd1385SRemy Bohmer 	.setup		= eth_setup,
250323cd1385SRemy Bohmer 	.disconnect	= eth_disconnect,
250423cd1385SRemy Bohmer 
250523cd1385SRemy Bohmer 	.suspend	= eth_suspend,
250623cd1385SRemy Bohmer 	.resume		= eth_resume,
250723cd1385SRemy Bohmer };
250823cd1385SRemy Bohmer 
250923cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi)
251023cd1385SRemy Bohmer {
251123cd1385SRemy Bohmer 	struct eth_device *netdev = &l_netdev;
251223cd1385SRemy Bohmer 
25138f7aa831SVitaly Kuzmichev 	strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
251423cd1385SRemy Bohmer 
251523cd1385SRemy Bohmer 	netdev->init = usb_eth_init;
251623cd1385SRemy Bohmer 	netdev->send = usb_eth_send;
251723cd1385SRemy Bohmer 	netdev->recv = usb_eth_recv;
251823cd1385SRemy Bohmer 	netdev->halt = usb_eth_halt;
251923cd1385SRemy Bohmer 
252023cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP
252123cd1385SRemy Bohmer   #error not supported
252223cd1385SRemy Bohmer #endif
252323cd1385SRemy Bohmer 	eth_register(netdev);
252423cd1385SRemy Bohmer 	return 0;
252523cd1385SRemy Bohmer }
2526