xref: /rk3399_rockchip-uboot/drivers/usb/gadget/ether.c (revision 8b6b66b427209b5b117487bfefba90e1587c2d5d)
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  *
823cd1385SRemy Bohmer  * This program is free software; you can redistribute it and/or modify
923cd1385SRemy Bohmer  * it under the terms of the GNU General Public License as published by
1023cd1385SRemy Bohmer  * the Free Software Foundation; either version 2 of the License, or
1123cd1385SRemy Bohmer  * (at your option) any later version.
1223cd1385SRemy Bohmer  *
1323cd1385SRemy Bohmer  * This program is distributed in the hope that it will be useful,
1423cd1385SRemy Bohmer  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1523cd1385SRemy Bohmer  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1623cd1385SRemy Bohmer  * GNU General Public License for more details.
1723cd1385SRemy Bohmer  *
1823cd1385SRemy Bohmer  * You should have received a copy of the GNU General Public License
1923cd1385SRemy Bohmer  * along with this program; if not, write to the Free Software
2023cd1385SRemy Bohmer  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2123cd1385SRemy Bohmer  */
2223cd1385SRemy Bohmer 
2323cd1385SRemy Bohmer #include <common.h>
2423cd1385SRemy Bohmer #include <asm/errno.h>
25c85d70efSVitaly Kuzmichev #include <linux/netdevice.h>
2623cd1385SRemy Bohmer #include <linux/usb/ch9.h>
2723cd1385SRemy Bohmer #include <linux/usb/cdc.h>
2823cd1385SRemy Bohmer #include <linux/usb/gadget.h>
2923cd1385SRemy Bohmer #include <net.h>
3023cd1385SRemy Bohmer #include <linux/ctype.h>
3123cd1385SRemy Bohmer 
3223cd1385SRemy Bohmer #include "gadget_chips.h"
3323cd1385SRemy Bohmer 
348f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether"
357de73185SVitaly Kuzmichev 
3623cd1385SRemy Bohmer #define atomic_read
3723cd1385SRemy Bohmer extern struct platform_data brd;
3823cd1385SRemy Bohmer #define spin_lock(x)
3923cd1385SRemy Bohmer #define spin_unlock(x)
4023cd1385SRemy Bohmer 
4123cd1385SRemy Bohmer 
4223cd1385SRemy Bohmer unsigned packet_received, packet_sent;
4323cd1385SRemy Bohmer 
4423cd1385SRemy Bohmer #define DEV_CONFIG_CDC	1
4523cd1385SRemy Bohmer #define GFP_ATOMIC ((gfp_t) 0)
4623cd1385SRemy Bohmer #define GFP_KERNEL ((gfp_t) 0)
4723cd1385SRemy Bohmer 
4823cd1385SRemy Bohmer /*
4923cd1385SRemy Bohmer  * Ethernet gadget driver -- with CDC and non-CDC options
5023cd1385SRemy Bohmer  * Builds on hardware support for a full duplex link.
5123cd1385SRemy Bohmer  *
5223cd1385SRemy Bohmer  * CDC Ethernet is the standard USB solution for sending Ethernet frames
5323cd1385SRemy Bohmer  * using USB.  Real hardware tends to use the same framing protocol but look
5423cd1385SRemy Bohmer  * different for control features.  This driver strongly prefers to use
5523cd1385SRemy Bohmer  * this USB-IF standard as its open-systems interoperability solution;
5623cd1385SRemy Bohmer  * most host side USB stacks (except from Microsoft) support it.
5723cd1385SRemy Bohmer  *
5823cd1385SRemy Bohmer  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
5923cd1385SRemy Bohmer  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
6023cd1385SRemy Bohmer  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
6123cd1385SRemy Bohmer  *
6223cd1385SRemy Bohmer  * There's some hardware that can't talk CDC ECM.  We make that hardware
6323cd1385SRemy Bohmer  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
6423cd1385SRemy Bohmer  * link-level setup only requires activating the configuration.  Only the
6523cd1385SRemy Bohmer  * endpoint descriptors, and product/vendor IDs, are relevant; no control
6623cd1385SRemy Bohmer  * operations are available.  Linux supports it, but other host operating
6723cd1385SRemy Bohmer  * systems may not.  (This is a subset of CDC Ethernet.)
6823cd1385SRemy Bohmer  *
6923cd1385SRemy Bohmer  * It turns out that if you add a few descriptors to that "CDC Subset",
7023cd1385SRemy Bohmer  * (Windows) host side drivers from MCCI can treat it as one submode of
7123cd1385SRemy Bohmer  * a proprietary scheme called "SAFE" ... without needing to know about
7223cd1385SRemy Bohmer  * specific product/vendor IDs.  So we do that, making it easier to use
7323cd1385SRemy Bohmer  * those MS-Windows drivers.  Those added descriptors make it resemble a
7423cd1385SRemy Bohmer  * CDC MDLM device, but they don't change device behavior at all.  (See
7523cd1385SRemy Bohmer  * MCCI Engineering report 950198 "SAFE Networking Functions".)
7623cd1385SRemy Bohmer  *
7723cd1385SRemy Bohmer  * A third option is also in use.  Rather than CDC Ethernet, or something
7823cd1385SRemy Bohmer  * simpler, Microsoft pushes their own approach: RNDIS.  The published
7923cd1385SRemy Bohmer  * RNDIS specs are ambiguous and appear to be incomplete, and are also
8023cd1385SRemy Bohmer  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
8123cd1385SRemy Bohmer  */
8223cd1385SRemy Bohmer #define ETH_ALEN	6		/* Octets in one ethernet addr	 */
8323cd1385SRemy Bohmer #define ETH_HLEN	14		/* Total octets in header.	 */
8423cd1385SRemy Bohmer #define ETH_ZLEN	60		/* Min. octets in frame sans FCS */
8523cd1385SRemy Bohmer #define ETH_DATA_LEN	1500		/* Max. octets in payload	 */
8623cd1385SRemy Bohmer #define ETH_FRAME_LEN	PKTSIZE_ALIGN	/* Max. octets in frame sans FCS */
8723cd1385SRemy Bohmer #define ETH_FCS_LEN	4		/* Octets in the FCS		 */
8823cd1385SRemy Bohmer 
8923cd1385SRemy Bohmer #define DRIVER_DESC		"Ethernet Gadget"
9023cd1385SRemy Bohmer /* Based on linux 2.6.27 version */
9123cd1385SRemy Bohmer #define DRIVER_VERSION		"May Day 2005"
9223cd1385SRemy Bohmer 
9323cd1385SRemy Bohmer static const char shortname[] = "ether";
9423cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC;
9523cd1385SRemy Bohmer 
9623cd1385SRemy Bohmer #define RX_EXTRA	20		/* guard against rx overflows */
9723cd1385SRemy Bohmer 
9823cd1385SRemy Bohmer /* CDC support the same host-chosen outgoing packet filters. */
9923cd1385SRemy Bohmer #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
10023cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
10123cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
10223cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_DIRECTED)
10323cd1385SRemy Bohmer 
10423cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
10523cd1385SRemy Bohmer 
10623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
107*8b6b66b4SVitaly Kuzmichev 
108*8b6b66b4SVitaly Kuzmichev struct eth_dev {
109*8b6b66b4SVitaly Kuzmichev 	struct usb_gadget	*gadget;
110*8b6b66b4SVitaly Kuzmichev 	struct usb_request	*req;		/* for control responses */
111*8b6b66b4SVitaly Kuzmichev 	struct usb_request	*stat_req;	/* for cdc status */
112*8b6b66b4SVitaly Kuzmichev 
113*8b6b66b4SVitaly Kuzmichev 	u8			config;
114*8b6b66b4SVitaly Kuzmichev 	struct usb_ep		*in_ep, *out_ep, *status_ep;
115*8b6b66b4SVitaly Kuzmichev 	const struct usb_endpoint_descriptor
116*8b6b66b4SVitaly Kuzmichev 				*in, *out, *status;
117*8b6b66b4SVitaly Kuzmichev 
118*8b6b66b4SVitaly Kuzmichev 	struct usb_request	*tx_req, *rx_req;
119*8b6b66b4SVitaly Kuzmichev 
120*8b6b66b4SVitaly Kuzmichev 	struct eth_device	*net;
121*8b6b66b4SVitaly Kuzmichev 	struct net_device_stats	stats;
122*8b6b66b4SVitaly Kuzmichev 	unsigned int		tx_qlen;
123*8b6b66b4SVitaly Kuzmichev 
124*8b6b66b4SVitaly Kuzmichev 	unsigned		zlp:1;
125*8b6b66b4SVitaly Kuzmichev 	unsigned		cdc:1;
126*8b6b66b4SVitaly Kuzmichev 	unsigned		suspended:1;
127*8b6b66b4SVitaly Kuzmichev 	unsigned		network_started:1;
128*8b6b66b4SVitaly Kuzmichev 	u16			cdc_filter;
129*8b6b66b4SVitaly Kuzmichev 	unsigned long		todo;
130*8b6b66b4SVitaly Kuzmichev 	int			mtu;
131*8b6b66b4SVitaly Kuzmichev #define	WORK_RX_MEMORY		0
132*8b6b66b4SVitaly Kuzmichev 	u8			host_mac[ETH_ALEN];
133*8b6b66b4SVitaly Kuzmichev };
134*8b6b66b4SVitaly Kuzmichev 
135*8b6b66b4SVitaly Kuzmichev /*
136*8b6b66b4SVitaly Kuzmichev  * This version autoconfigures as much as possible at run-time.
137*8b6b66b4SVitaly Kuzmichev  *
138*8b6b66b4SVitaly Kuzmichev  * It also ASSUMES a self-powered device, without remote wakeup,
139*8b6b66b4SVitaly Kuzmichev  * although remote wakeup support would make sense.
140*8b6b66b4SVitaly Kuzmichev  */
141*8b6b66b4SVitaly Kuzmichev 
142*8b6b66b4SVitaly Kuzmichev /*-------------------------------------------------------------------------*/
14323cd1385SRemy Bohmer static struct eth_dev l_ethdev;
14423cd1385SRemy Bohmer static struct eth_device l_netdev;
14523cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver;
14623cd1385SRemy Bohmer 
14723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
14823cd1385SRemy Bohmer 
14923cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */
15023cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev)
15123cd1385SRemy Bohmer {
15223cd1385SRemy Bohmer #if	!defined(DEV_CONFIG_SUBSET)
15323cd1385SRemy Bohmer 	return 1;		/* only cdc possible */
15423cd1385SRemy Bohmer #elif	!defined(DEV_CONFIG_CDC)
15523cd1385SRemy Bohmer 	return 0;		/* only subset possible */
15623cd1385SRemy Bohmer #else
15723cd1385SRemy Bohmer 	return dev->cdc;	/* depends on what hardware we found */
15823cd1385SRemy Bohmer #endif
15923cd1385SRemy Bohmer }
16023cd1385SRemy Bohmer 
16123cd1385SRemy Bohmer #define	subset_active(dev)	(!is_cdc(dev))
16223cd1385SRemy Bohmer #define	cdc_active(dev)		(is_cdc(dev))
16323cd1385SRemy Bohmer 
16423cd1385SRemy Bohmer #define DEFAULT_QLEN	2	/* double buffering by default */
16523cd1385SRemy Bohmer 
16623cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */
16723cd1385SRemy Bohmer #define	HS_BPS		(13 * 512 * 8 * 1000 * 8)
16823cd1385SRemy Bohmer #define	FS_BPS		(19 *  64 * 1 * 1000 * 8)
16923cd1385SRemy Bohmer 
17023cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
17123cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_HIGH
17223cd1385SRemy Bohmer 
1732721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT
1742721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT
1752721dbf1SVitaly Kuzmichev #else
1762721dbf1SVitaly Kuzmichev #define qmult 5
1772721dbf1SVitaly Kuzmichev #endif
1782721dbf1SVitaly Kuzmichev 
17923cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */
18023cd1385SRemy Bohmer #define qlen(gadget) \
18123cd1385SRemy Bohmer 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
18223cd1385SRemy Bohmer 
18323cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
18423cd1385SRemy Bohmer {
18523cd1385SRemy Bohmer 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
18623cd1385SRemy Bohmer }
18723cd1385SRemy Bohmer 
18823cd1385SRemy Bohmer #else	/* full speed (low speed doesn't do bulk) */
18923cd1385SRemy Bohmer 
19023cd1385SRemy Bohmer #define qmult		1
19123cd1385SRemy Bohmer 
19223cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_FULL
19323cd1385SRemy Bohmer 
19423cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN
19523cd1385SRemy Bohmer 
19623cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
19723cd1385SRemy Bohmer {
19823cd1385SRemy Bohmer 	return FS_BPS;
19923cd1385SRemy Bohmer }
20023cd1385SRemy Bohmer #endif
20123cd1385SRemy Bohmer 
20223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
20323cd1385SRemy Bohmer 
2046142e0aeSVitaly Kuzmichev /*
2056142e0aeSVitaly Kuzmichev  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
20623cd1385SRemy Bohmer  * Instead:  allocate your own, using normal USB-IF procedures.
20723cd1385SRemy Bohmer  */
20823cd1385SRemy Bohmer 
2096142e0aeSVitaly Kuzmichev /*
2106142e0aeSVitaly Kuzmichev  * Thanks to NetChip Technologies for donating this product ID.
21123cd1385SRemy Bohmer  * It's for devices with only CDC Ethernet configurations.
21223cd1385SRemy Bohmer  */
21323cd1385SRemy Bohmer #define CDC_VENDOR_NUM		0x0525	/* NetChip */
21423cd1385SRemy Bohmer #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
21523cd1385SRemy Bohmer 
2166142e0aeSVitaly Kuzmichev /*
2176142e0aeSVitaly Kuzmichev  * For hardware that can't talk CDC, we use the same vendor ID that
21823cd1385SRemy Bohmer  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
21923cd1385SRemy Bohmer  * with pxa250.  We're protocol-compatible, if the host-side drivers
22023cd1385SRemy Bohmer  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
22123cd1385SRemy Bohmer  * drivers that need to hard-wire endpoint numbers have a hook.
22223cd1385SRemy Bohmer  *
22323cd1385SRemy Bohmer  * The protocol is a minimal subset of CDC Ether, which works on any bulk
22423cd1385SRemy Bohmer  * hardware that's not deeply broken ... even on hardware that can't talk
22523cd1385SRemy Bohmer  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
22623cd1385SRemy Bohmer  * doesn't handle control-OUT).
22723cd1385SRemy Bohmer  */
22823cd1385SRemy Bohmer #define	SIMPLE_VENDOR_NUM	0x049f
22923cd1385SRemy Bohmer #define	SIMPLE_PRODUCT_NUM	0x505a
23023cd1385SRemy Bohmer 
2316142e0aeSVitaly Kuzmichev /*
2326142e0aeSVitaly Kuzmichev  * Some systems will want different product identifers published in the
23323cd1385SRemy Bohmer  * device descriptor, either numbers or strings or both.  These string
23423cd1385SRemy Bohmer  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
23523cd1385SRemy Bohmer  */
23623cd1385SRemy Bohmer 
23723cd1385SRemy Bohmer static ushort bcdDevice;
23823cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER)
23923cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
24023cd1385SRemy Bohmer #else
24123cd1385SRemy Bohmer static char *iManufacturer = "U-boot";
24223cd1385SRemy Bohmer #endif
24323cd1385SRemy Bohmer static char *iProduct;
24423cd1385SRemy Bohmer static char *iSerialNumber;
24523cd1385SRemy Bohmer static char dev_addr[18];
24623cd1385SRemy Bohmer static char host_addr[18];
24723cd1385SRemy Bohmer 
24823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
24923cd1385SRemy Bohmer 
2506142e0aeSVitaly Kuzmichev /*
2516142e0aeSVitaly Kuzmichev  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
25223cd1385SRemy Bohmer  * ep0 implementation:  descriptors, config management, setup().
25323cd1385SRemy Bohmer  * also optional class-specific notification interrupt transfer.
25423cd1385SRemy Bohmer  */
25523cd1385SRemy Bohmer 
25623cd1385SRemy Bohmer /*
25723cd1385SRemy Bohmer  * DESCRIPTORS ... most are static, but strings and (full) configuration
25823cd1385SRemy Bohmer  * descriptors are built on demand.  For now we do either full CDC, or
25923cd1385SRemy Bohmer  * our simple subset.
26023cd1385SRemy Bohmer  */
26123cd1385SRemy Bohmer 
26223cd1385SRemy Bohmer #define STRING_MANUFACTURER		1
26323cd1385SRemy Bohmer #define STRING_PRODUCT			2
26423cd1385SRemy Bohmer #define STRING_ETHADDR			3
26523cd1385SRemy Bohmer #define STRING_DATA			4
26623cd1385SRemy Bohmer #define STRING_CONTROL			5
26723cd1385SRemy Bohmer #define STRING_CDC			7
26823cd1385SRemy Bohmer #define STRING_SUBSET			8
26923cd1385SRemy Bohmer #define STRING_SERIALNUMBER		10
27023cd1385SRemy Bohmer 
27123cd1385SRemy Bohmer /* holds our biggest descriptor */
27223cd1385SRemy Bohmer #define USB_BUFSIZ	256
27323cd1385SRemy Bohmer 
27423cd1385SRemy Bohmer /*
27523cd1385SRemy Bohmer  * This device advertises one configuration, eth_config,
27623cd1385SRemy Bohmer  * on hardware supporting at least two configs.
27723cd1385SRemy Bohmer  *
27823cd1385SRemy Bohmer  * FIXME define some higher-powered configurations to make it easier
27923cd1385SRemy Bohmer  * to recharge batteries ...
28023cd1385SRemy Bohmer  */
28123cd1385SRemy Bohmer 
28223cd1385SRemy Bohmer #define DEV_CONFIG_VALUE	1	/* cdc or subset */
28323cd1385SRemy Bohmer 
28423cd1385SRemy Bohmer static struct usb_device_descriptor
28523cd1385SRemy Bohmer device_desc = {
28623cd1385SRemy Bohmer 	.bLength =		sizeof device_desc,
28723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE,
28823cd1385SRemy Bohmer 
28923cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
29023cd1385SRemy Bohmer 
29123cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
29223cd1385SRemy Bohmer 	.bDeviceSubClass =	0,
29323cd1385SRemy Bohmer 	.bDeviceProtocol =	0,
29423cd1385SRemy Bohmer 
29523cd1385SRemy Bohmer 	.idVendor =		__constant_cpu_to_le16(CDC_VENDOR_NUM),
29623cd1385SRemy Bohmer 	.idProduct =		__constant_cpu_to_le16(CDC_PRODUCT_NUM),
29723cd1385SRemy Bohmer 	.iManufacturer =	STRING_MANUFACTURER,
29823cd1385SRemy Bohmer 	.iProduct =		STRING_PRODUCT,
29923cd1385SRemy Bohmer 	.bNumConfigurations =	1,
30023cd1385SRemy Bohmer };
30123cd1385SRemy Bohmer 
30223cd1385SRemy Bohmer static struct usb_otg_descriptor
30323cd1385SRemy Bohmer otg_descriptor = {
30423cd1385SRemy Bohmer 	.bLength =		sizeof otg_descriptor,
30523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_OTG,
30623cd1385SRemy Bohmer 
30723cd1385SRemy Bohmer 	.bmAttributes =		USB_OTG_SRP,
30823cd1385SRemy Bohmer };
30923cd1385SRemy Bohmer 
31023cd1385SRemy Bohmer static struct usb_config_descriptor
31123cd1385SRemy Bohmer eth_config = {
31223cd1385SRemy Bohmer 	.bLength =		sizeof eth_config,
31323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CONFIG,
31423cd1385SRemy Bohmer 
31523cd1385SRemy Bohmer 	/* compute wTotalLength on the fly */
31623cd1385SRemy Bohmer 	.bNumInterfaces =	2,
31723cd1385SRemy Bohmer 	.bConfigurationValue =	DEV_CONFIG_VALUE,
31823cd1385SRemy Bohmer 	.iConfiguration =	STRING_CDC,
31923cd1385SRemy Bohmer 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
32023cd1385SRemy Bohmer 	.bMaxPower =		1,
32123cd1385SRemy Bohmer };
32223cd1385SRemy Bohmer 
32323cd1385SRemy Bohmer /*
32423cd1385SRemy Bohmer  * Compared to the simple CDC subset, the full CDC Ethernet model adds
32523cd1385SRemy Bohmer  * three class descriptors, two interface descriptors, optional status
32623cd1385SRemy Bohmer  * endpoint.  Both have a "data" interface and two bulk endpoints.
32723cd1385SRemy Bohmer  * There are also differences in how control requests are handled.
32823cd1385SRemy Bohmer  */
32923cd1385SRemy Bohmer 
33023cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
33123cd1385SRemy Bohmer static struct usb_interface_descriptor
33223cd1385SRemy Bohmer control_intf = {
33323cd1385SRemy Bohmer 	.bLength =		sizeof control_intf,
33423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
33523cd1385SRemy Bohmer 
33623cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
33723cd1385SRemy Bohmer 	/* status endpoint is optional; this may be patched later */
33823cd1385SRemy Bohmer 	.bNumEndpoints =	1,
33923cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_COMM,
34023cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
34123cd1385SRemy Bohmer 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
34223cd1385SRemy Bohmer 	.iInterface =		STRING_CONTROL,
34323cd1385SRemy Bohmer };
34423cd1385SRemy Bohmer #endif
34523cd1385SRemy Bohmer 
34623cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = {
34723cd1385SRemy Bohmer 	.bLength =		sizeof header_desc,
34823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
34923cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
35023cd1385SRemy Bohmer 
35123cd1385SRemy Bohmer 	.bcdCDC =		__constant_cpu_to_le16(0x0110),
35223cd1385SRemy Bohmer };
35323cd1385SRemy Bohmer 
35423cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
35523cd1385SRemy Bohmer 
35623cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = {
35723cd1385SRemy Bohmer 	.bLength =		sizeof union_desc,
35823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
35923cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
36023cd1385SRemy Bohmer 
36123cd1385SRemy Bohmer 	.bMasterInterface0 =	0,	/* index of control interface */
36223cd1385SRemy Bohmer 	.bSlaveInterface0 =	1,	/* index of DATA interface */
36323cd1385SRemy Bohmer };
36423cd1385SRemy Bohmer 
36523cd1385SRemy Bohmer #endif	/* CDC */
36623cd1385SRemy Bohmer 
36723cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC
36823cd1385SRemy Bohmer 
3696142e0aeSVitaly Kuzmichev /*
3706142e0aeSVitaly Kuzmichev  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
37123cd1385SRemy Bohmer  * ways:  data endpoints live in the control interface, there's no data
37223cd1385SRemy Bohmer  * interface, and it's not used to talk to a cell phone radio.
37323cd1385SRemy Bohmer  */
37423cd1385SRemy Bohmer 
37523cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = {
37623cd1385SRemy Bohmer 	.bLength =		sizeof mdlm_desc,
37723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
37823cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_MDLM_TYPE,
37923cd1385SRemy Bohmer 
38023cd1385SRemy Bohmer 	.bcdVersion =		__constant_cpu_to_le16(0x0100),
38123cd1385SRemy Bohmer 	.bGUID = {
38223cd1385SRemy Bohmer 		0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
38323cd1385SRemy Bohmer 		0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
38423cd1385SRemy Bohmer 	},
38523cd1385SRemy Bohmer };
38623cd1385SRemy Bohmer 
3876142e0aeSVitaly Kuzmichev /*
3886142e0aeSVitaly Kuzmichev  * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
38923cd1385SRemy Bohmer  * can't really use its struct.  All we do here is say that we're using
39023cd1385SRemy Bohmer  * the submode of "SAFE" which directly matches the CDC Subset.
39123cd1385SRemy Bohmer  */
39223cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = {
39323cd1385SRemy Bohmer 	6,
39423cd1385SRemy Bohmer 	USB_DT_CS_INTERFACE,
39523cd1385SRemy Bohmer 	USB_CDC_MDLM_DETAIL_TYPE,
39623cd1385SRemy Bohmer 
39723cd1385SRemy Bohmer 	0,	/* "SAFE" */
39823cd1385SRemy Bohmer 	0,	/* network control capabilities (none) */
39923cd1385SRemy Bohmer 	0,	/* network data capabilities ("raw" encapsulation) */
40023cd1385SRemy Bohmer };
40123cd1385SRemy Bohmer 
40223cd1385SRemy Bohmer #endif
40323cd1385SRemy Bohmer 
40423cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = {
40523cd1385SRemy Bohmer 	.bLength =		sizeof(ether_desc),
40623cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
40723cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
40823cd1385SRemy Bohmer 
40923cd1385SRemy Bohmer 	/* this descriptor actually adds value, surprise! */
41023cd1385SRemy Bohmer 	.iMACAddress =		STRING_ETHADDR,
41123cd1385SRemy Bohmer 	.bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
41223cd1385SRemy Bohmer 	.wMaxSegmentSize =	__constant_cpu_to_le16(ETH_FRAME_LEN),
41323cd1385SRemy Bohmer 	.wNumberMCFilters =	__constant_cpu_to_le16(0),
41423cd1385SRemy Bohmer 	.bNumberPowerFilters =	0,
41523cd1385SRemy Bohmer };
41623cd1385SRemy Bohmer 
41723cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
41823cd1385SRemy Bohmer 
4196142e0aeSVitaly Kuzmichev /*
4206142e0aeSVitaly Kuzmichev  * include the status endpoint if we can, even where it's optional.
42123cd1385SRemy Bohmer  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
42223cd1385SRemy Bohmer  * packet, to simplify cancellation; and a big transfer interval, to
42323cd1385SRemy Bohmer  * waste less bandwidth.
42423cd1385SRemy Bohmer  *
42523cd1385SRemy Bohmer  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
42623cd1385SRemy Bohmer  * if they ignore the connect/disconnect notifications that real aether
42723cd1385SRemy Bohmer  * can provide.  more advanced cdc configurations might want to support
42823cd1385SRemy Bohmer  * encapsulated commands (vendor-specific, using control-OUT).
42923cd1385SRemy Bohmer  */
43023cd1385SRemy Bohmer 
43123cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
43223cd1385SRemy Bohmer #define STATUS_BYTECOUNT		16	/* 8 byte header + data */
43323cd1385SRemy Bohmer 
43423cd1385SRemy Bohmer static struct usb_endpoint_descriptor
43523cd1385SRemy Bohmer fs_status_desc = {
43623cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
43723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
43823cd1385SRemy Bohmer 
43923cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
44023cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
44123cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
44223cd1385SRemy Bohmer 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
44323cd1385SRemy Bohmer };
44423cd1385SRemy Bohmer #endif
44523cd1385SRemy Bohmer 
44623cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
44723cd1385SRemy Bohmer 
44823cd1385SRemy Bohmer /* the default data interface has no endpoints ... */
44923cd1385SRemy Bohmer 
45023cd1385SRemy Bohmer static const struct usb_interface_descriptor
45123cd1385SRemy Bohmer data_nop_intf = {
45223cd1385SRemy Bohmer 	.bLength =		sizeof data_nop_intf,
45323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
45423cd1385SRemy Bohmer 
45523cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
45623cd1385SRemy Bohmer 	.bAlternateSetting =	0,
45723cd1385SRemy Bohmer 	.bNumEndpoints =	0,
45823cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
45923cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
46023cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
46123cd1385SRemy Bohmer };
46223cd1385SRemy Bohmer 
46323cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */
46423cd1385SRemy Bohmer 
46523cd1385SRemy Bohmer static const struct usb_interface_descriptor
46623cd1385SRemy Bohmer data_intf = {
46723cd1385SRemy Bohmer 	.bLength =		sizeof data_intf,
46823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
46923cd1385SRemy Bohmer 
47023cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
47123cd1385SRemy Bohmer 	.bAlternateSetting =	1,
47223cd1385SRemy Bohmer 	.bNumEndpoints =	2,
47323cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
47423cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
47523cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
47623cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
47723cd1385SRemy Bohmer };
47823cd1385SRemy Bohmer 
47923cd1385SRemy Bohmer #endif
48023cd1385SRemy Bohmer 
48123cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
48223cd1385SRemy Bohmer 
48323cd1385SRemy Bohmer /*
48423cd1385SRemy Bohmer  * "Simple" CDC-subset option is a simple vendor-neutral model that most
48523cd1385SRemy Bohmer  * full speed controllers can handle:  one interface, two bulk endpoints.
48623cd1385SRemy Bohmer  *
48723cd1385SRemy Bohmer  * To assist host side drivers, we fancy it up a bit, and add descriptors
48823cd1385SRemy Bohmer  * so some host side drivers will understand it as a "SAFE" variant.
48923cd1385SRemy Bohmer  */
49023cd1385SRemy Bohmer 
49123cd1385SRemy Bohmer static const struct usb_interface_descriptor
49223cd1385SRemy Bohmer subset_data_intf = {
49323cd1385SRemy Bohmer 	.bLength =		sizeof subset_data_intf,
49423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
49523cd1385SRemy Bohmer 
49623cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
49723cd1385SRemy Bohmer 	.bAlternateSetting =	0,
49823cd1385SRemy Bohmer 	.bNumEndpoints =	2,
49923cd1385SRemy Bohmer 	.bInterfaceClass =      USB_CLASS_COMM,
50023cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_MDLM,
50123cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
50223cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
50323cd1385SRemy Bohmer };
50423cd1385SRemy Bohmer 
50523cd1385SRemy Bohmer #endif	/* SUBSET */
50623cd1385SRemy Bohmer 
50723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
50823cd1385SRemy Bohmer fs_source_desc = {
50923cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
51023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
51123cd1385SRemy Bohmer 
51223cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
51323cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
51423cd1385SRemy Bohmer };
51523cd1385SRemy Bohmer 
51623cd1385SRemy Bohmer static struct usb_endpoint_descriptor
51723cd1385SRemy Bohmer fs_sink_desc = {
51823cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
51923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
52023cd1385SRemy Bohmer 
52123cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_OUT,
52223cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
52323cd1385SRemy Bohmer };
52423cd1385SRemy Bohmer 
52523cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = {
52623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
52723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
52823cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
52923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
53023cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
53123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
53223cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
53323cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
53423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_status_desc,
53523cd1385SRemy Bohmer 	/* data interface, with altsetting */
53623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
53723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
53823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_source_desc,
53923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_sink_desc,
54023cd1385SRemy Bohmer 	NULL,
54123cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
54223cd1385SRemy Bohmer };
54323cd1385SRemy Bohmer 
54423cd1385SRemy Bohmer static inline void fs_subset_descriptors(void)
54523cd1385SRemy Bohmer {
54623cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
54723cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
54823cd1385SRemy Bohmer 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
54923cd1385SRemy Bohmer 	fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
55023cd1385SRemy Bohmer 	fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
55123cd1385SRemy Bohmer 	fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
55223cd1385SRemy Bohmer 	fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
55323cd1385SRemy Bohmer 	fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
55423cd1385SRemy Bohmer 	fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
55523cd1385SRemy Bohmer 	fs_eth_function[8] = NULL;
55623cd1385SRemy Bohmer #else
55723cd1385SRemy Bohmer 	fs_eth_function[1] = NULL;
55823cd1385SRemy Bohmer #endif
55923cd1385SRemy Bohmer }
56023cd1385SRemy Bohmer 
56123cd1385SRemy Bohmer /*
56223cd1385SRemy Bohmer  * usb 2.0 devices need to expose both high speed and full speed
56323cd1385SRemy Bohmer  * descriptors, unless they only run at full speed.
56423cd1385SRemy Bohmer  */
56523cd1385SRemy Bohmer 
56623cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
56723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
56823cd1385SRemy Bohmer hs_status_desc = {
56923cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
57023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
57123cd1385SRemy Bohmer 
57223cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
57323cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
57423cd1385SRemy Bohmer 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
57523cd1385SRemy Bohmer };
57623cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
57723cd1385SRemy Bohmer 
57823cd1385SRemy Bohmer static struct usb_endpoint_descriptor
57923cd1385SRemy Bohmer hs_source_desc = {
58023cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
58123cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
58223cd1385SRemy Bohmer 
58323cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
58423cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
58523cd1385SRemy Bohmer };
58623cd1385SRemy Bohmer 
58723cd1385SRemy Bohmer static struct usb_endpoint_descriptor
58823cd1385SRemy Bohmer hs_sink_desc = {
58923cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
59023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
59123cd1385SRemy Bohmer 
59223cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
59323cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
59423cd1385SRemy Bohmer };
59523cd1385SRemy Bohmer 
59623cd1385SRemy Bohmer static struct usb_qualifier_descriptor
59723cd1385SRemy Bohmer dev_qualifier = {
59823cd1385SRemy Bohmer 	.bLength =		sizeof dev_qualifier,
59923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
60023cd1385SRemy Bohmer 
60123cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
60223cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
60323cd1385SRemy Bohmer 
60423cd1385SRemy Bohmer 	.bNumConfigurations =	1,
60523cd1385SRemy Bohmer };
60623cd1385SRemy Bohmer 
60723cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = {
60823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
60923cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
61023cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
61123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
61223cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
61323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
61423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
61523cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
61623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_status_desc,
61723cd1385SRemy Bohmer 	/* data interface, with altsetting */
61823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
61923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
62023cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_source_desc,
62123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_sink_desc,
62223cd1385SRemy Bohmer 	NULL,
62323cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
62423cd1385SRemy Bohmer };
62523cd1385SRemy Bohmer 
62623cd1385SRemy Bohmer static inline void hs_subset_descriptors(void)
62723cd1385SRemy Bohmer {
62823cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
62923cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
63023cd1385SRemy Bohmer 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
63123cd1385SRemy Bohmer 	hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
63223cd1385SRemy Bohmer 	hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
63323cd1385SRemy Bohmer 	hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
63423cd1385SRemy Bohmer 	hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
63523cd1385SRemy Bohmer 	hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
63623cd1385SRemy Bohmer 	hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
63723cd1385SRemy Bohmer 	hs_eth_function[8] = NULL;
63823cd1385SRemy Bohmer #else
63923cd1385SRemy Bohmer 	hs_eth_function[1] = NULL;
64023cd1385SRemy Bohmer #endif
64123cd1385SRemy Bohmer }
64223cd1385SRemy Bohmer 
64323cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */
64423cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor *
64523cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
64623cd1385SRemy Bohmer 		struct usb_endpoint_descriptor *fs)
64723cd1385SRemy Bohmer {
64823cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
64923cd1385SRemy Bohmer 		return hs;
65023cd1385SRemy Bohmer 	return fs;
65123cd1385SRemy Bohmer }
65223cd1385SRemy Bohmer 
65323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
65423cd1385SRemy Bohmer 
65523cd1385SRemy Bohmer /* descriptors that are built on-demand */
65623cd1385SRemy Bohmer 
65723cd1385SRemy Bohmer static char manufacturer[50];
65823cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC;
65923cd1385SRemy Bohmer static char serial_number[20];
66023cd1385SRemy Bohmer 
66123cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */
66223cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1];
66323cd1385SRemy Bohmer 
66423cd1385SRemy Bohmer /* static strings, in UTF-8 */
66523cd1385SRemy Bohmer static struct usb_string		strings[] = {
66623cd1385SRemy Bohmer 	{ STRING_MANUFACTURER,	manufacturer, },
66723cd1385SRemy Bohmer 	{ STRING_PRODUCT,	product_desc, },
66823cd1385SRemy Bohmer 	{ STRING_SERIALNUMBER,	serial_number, },
66923cd1385SRemy Bohmer 	{ STRING_DATA,		"Ethernet Data", },
67023cd1385SRemy Bohmer 	{ STRING_ETHADDR,	ethaddr, },
67123cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
67223cd1385SRemy Bohmer 	{ STRING_CDC,		"CDC Ethernet", },
67323cd1385SRemy Bohmer 	{ STRING_CONTROL,	"CDC Communications Control", },
67423cd1385SRemy Bohmer #endif
67523cd1385SRemy Bohmer #ifdef	DEV_CONFIG_SUBSET
67623cd1385SRemy Bohmer 	{ STRING_SUBSET,	"CDC Ethernet Subset", },
67723cd1385SRemy Bohmer #endif
67823cd1385SRemy Bohmer 	{  }		/* end of list */
67923cd1385SRemy Bohmer };
68023cd1385SRemy Bohmer 
68123cd1385SRemy Bohmer static struct usb_gadget_strings	stringtab = {
68223cd1385SRemy Bohmer 	.language	= 0x0409,	/* en-us */
68323cd1385SRemy Bohmer 	.strings	= strings,
68423cd1385SRemy Bohmer };
68523cd1385SRemy Bohmer 
68623cd1385SRemy Bohmer /*============================================================================*/
68723cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ];
68880460772SStefano Babic static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
68923cd1385SRemy Bohmer 
69023cd1385SRemy Bohmer 
69123cd1385SRemy Bohmer /**
69223cd1385SRemy Bohmer  * strlcpy - Copy a %NUL terminated string into a sized buffer
69323cd1385SRemy Bohmer  * @dest: Where to copy the string to
69423cd1385SRemy Bohmer  * @src: Where to copy the string from
69523cd1385SRemy Bohmer  * @size: size of destination buffer
69623cd1385SRemy Bohmer  *
69723cd1385SRemy Bohmer  * Compatible with *BSD: the result is always a valid
69823cd1385SRemy Bohmer  * NUL-terminated string that fits in the buffer (unless,
69923cd1385SRemy Bohmer  * of course, the buffer size is zero). It does not pad
70023cd1385SRemy Bohmer  * out the result like strncpy() does.
70123cd1385SRemy Bohmer  */
70223cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size)
70323cd1385SRemy Bohmer {
70423cd1385SRemy Bohmer 	size_t ret = strlen(src);
70523cd1385SRemy Bohmer 
70623cd1385SRemy Bohmer 	if (size) {
70723cd1385SRemy Bohmer 		size_t len = (ret >= size) ? size - 1 : ret;
70823cd1385SRemy Bohmer 		memcpy(dest, src, len);
70923cd1385SRemy Bohmer 		dest[len] = '\0';
71023cd1385SRemy Bohmer 	}
71123cd1385SRemy Bohmer 	return ret;
71223cd1385SRemy Bohmer }
71323cd1385SRemy Bohmer 
71423cd1385SRemy Bohmer /*============================================================================*/
71523cd1385SRemy Bohmer 
71623cd1385SRemy Bohmer /*
71723cd1385SRemy Bohmer  * one config, two interfaces:  control, data.
71823cd1385SRemy Bohmer  * complications: class descriptors, and an altsetting.
71923cd1385SRemy Bohmer  */
72023cd1385SRemy Bohmer static int
72123cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
72223cd1385SRemy Bohmer {
72323cd1385SRemy Bohmer 	int					len;
72423cd1385SRemy Bohmer 	const struct usb_config_descriptor	*config;
72523cd1385SRemy Bohmer 	const struct usb_descriptor_header	**function;
72623cd1385SRemy Bohmer 	int					hs = 0;
72723cd1385SRemy Bohmer 
72823cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g)) {
72923cd1385SRemy Bohmer 		hs = (g->speed == USB_SPEED_HIGH);
73023cd1385SRemy Bohmer 		if (type == USB_DT_OTHER_SPEED_CONFIG)
73123cd1385SRemy Bohmer 			hs = !hs;
73223cd1385SRemy Bohmer 	}
73323cd1385SRemy Bohmer #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function)
73423cd1385SRemy Bohmer 
73523cd1385SRemy Bohmer 	if (index >= device_desc.bNumConfigurations)
73623cd1385SRemy Bohmer 		return -EINVAL;
73723cd1385SRemy Bohmer 
73823cd1385SRemy Bohmer 	config = &eth_config;
73923cd1385SRemy Bohmer 	function = which_fn(eth);
74023cd1385SRemy Bohmer 
74123cd1385SRemy Bohmer 	/* for now, don't advertise srp-only devices */
74223cd1385SRemy Bohmer 	if (!is_otg)
74323cd1385SRemy Bohmer 		function++;
74423cd1385SRemy Bohmer 
74523cd1385SRemy Bohmer 	len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
74623cd1385SRemy Bohmer 	if (len < 0)
74723cd1385SRemy Bohmer 		return len;
74823cd1385SRemy Bohmer 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
74923cd1385SRemy Bohmer 	return len;
75023cd1385SRemy Bohmer }
75123cd1385SRemy Bohmer 
75223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
75323cd1385SRemy Bohmer 
75423cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
75523cd1385SRemy Bohmer 
75623cd1385SRemy Bohmer static int
75723cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
75823cd1385SRemy Bohmer {
75923cd1385SRemy Bohmer 	int					result = 0;
76023cd1385SRemy Bohmer 	struct usb_gadget			*gadget = dev->gadget;
76123cd1385SRemy Bohmer 
76223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
76323cd1385SRemy Bohmer 	/* status endpoint used for (optionally) CDC */
76423cd1385SRemy Bohmer 	if (!subset_active(dev) && dev->status_ep) {
76523cd1385SRemy Bohmer 		dev->status = ep_desc(gadget, &hs_status_desc,
76623cd1385SRemy Bohmer 						&fs_status_desc);
76723cd1385SRemy Bohmer 		dev->status_ep->driver_data = dev;
76823cd1385SRemy Bohmer 
76923cd1385SRemy Bohmer 		result = usb_ep_enable(dev->status_ep, dev->status);
77023cd1385SRemy Bohmer 		if (result != 0) {
7717de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
77223cd1385SRemy Bohmer 				dev->status_ep->name, result);
77323cd1385SRemy Bohmer 			goto done;
77423cd1385SRemy Bohmer 		}
77523cd1385SRemy Bohmer 	}
77623cd1385SRemy Bohmer #endif
77723cd1385SRemy Bohmer 
77823cd1385SRemy Bohmer 	dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
77923cd1385SRemy Bohmer 	dev->in_ep->driver_data = dev;
78023cd1385SRemy Bohmer 
78123cd1385SRemy Bohmer 	dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
78223cd1385SRemy Bohmer 	dev->out_ep->driver_data = dev;
78323cd1385SRemy Bohmer 
7846142e0aeSVitaly Kuzmichev 	/*
7856142e0aeSVitaly Kuzmichev 	 * With CDC,  the host isn't allowed to use these two data
78623cd1385SRemy Bohmer 	 * endpoints in the default altsetting for the interface.
78723cd1385SRemy Bohmer 	 * so we don't activate them yet.  Reset from SET_INTERFACE.
78823cd1385SRemy Bohmer 	 */
78923cd1385SRemy Bohmer 	if (!cdc_active(dev)) {
79023cd1385SRemy Bohmer 		result = usb_ep_enable(dev->in_ep, dev->in);
79123cd1385SRemy Bohmer 		if (result != 0) {
7927de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
79323cd1385SRemy Bohmer 				dev->in_ep->name, result);
79423cd1385SRemy Bohmer 			goto done;
79523cd1385SRemy Bohmer 		}
79623cd1385SRemy Bohmer 
79723cd1385SRemy Bohmer 		result = usb_ep_enable(dev->out_ep, dev->out);
79823cd1385SRemy Bohmer 		if (result != 0) {
7997de73185SVitaly Kuzmichev 			debug("enable %s --> %d\n",
80023cd1385SRemy Bohmer 				dev->out_ep->name, result);
80123cd1385SRemy Bohmer 			goto done;
80223cd1385SRemy Bohmer 		}
80323cd1385SRemy Bohmer 	}
80423cd1385SRemy Bohmer 
80523cd1385SRemy Bohmer done:
80623cd1385SRemy Bohmer 	if (result == 0)
80723cd1385SRemy Bohmer 		result = alloc_requests(dev, qlen(gadget), gfp_flags);
80823cd1385SRemy Bohmer 
80923cd1385SRemy Bohmer 	/* on error, disable any endpoints  */
81023cd1385SRemy Bohmer 	if (result < 0) {
811d5292c16SVitaly Kuzmichev 		if (!subset_active(dev) && dev->status_ep)
81223cd1385SRemy Bohmer 			(void) usb_ep_disable(dev->status_ep);
81323cd1385SRemy Bohmer 		dev->status = NULL;
81423cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->in_ep);
81523cd1385SRemy Bohmer 		(void) usb_ep_disable(dev->out_ep);
81623cd1385SRemy Bohmer 		dev->in = NULL;
81723cd1385SRemy Bohmer 		dev->out = NULL;
81823cd1385SRemy Bohmer 	}
81923cd1385SRemy Bohmer 
82023cd1385SRemy Bohmer 	/* caller is responsible for cleanup on error */
82123cd1385SRemy Bohmer 	return result;
82223cd1385SRemy Bohmer }
82323cd1385SRemy Bohmer 
82423cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev)
82523cd1385SRemy Bohmer {
82623cd1385SRemy Bohmer 	if (dev->config == 0)
82723cd1385SRemy Bohmer 		return;
82823cd1385SRemy Bohmer 
8297de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
8307de73185SVitaly Kuzmichev 
8316142e0aeSVitaly Kuzmichev 	/*
8326142e0aeSVitaly Kuzmichev 	 * disable endpoints, forcing (synchronous) completion of
83323cd1385SRemy Bohmer 	 * pending i/o.  then free the requests.
83423cd1385SRemy Bohmer 	 */
83523cd1385SRemy Bohmer 
83623cd1385SRemy Bohmer 	if (dev->in) {
83723cd1385SRemy Bohmer 		usb_ep_disable(dev->in_ep);
83823cd1385SRemy Bohmer 		if (dev->tx_req) {
83923cd1385SRemy Bohmer 			usb_ep_free_request(dev->in_ep, dev->tx_req);
84023cd1385SRemy Bohmer 			dev->tx_req = NULL;
84123cd1385SRemy Bohmer 		}
84223cd1385SRemy Bohmer 	}
84323cd1385SRemy Bohmer 	if (dev->out) {
84423cd1385SRemy Bohmer 		usb_ep_disable(dev->out_ep);
84523cd1385SRemy Bohmer 		if (dev->rx_req) {
8460129e327SVitaly Kuzmichev 			usb_ep_free_request(dev->out_ep, dev->rx_req);
84723cd1385SRemy Bohmer 			dev->rx_req = NULL;
84823cd1385SRemy Bohmer 		}
84923cd1385SRemy Bohmer 	}
8506142e0aeSVitaly Kuzmichev 	if (dev->status)
85123cd1385SRemy Bohmer 		usb_ep_disable(dev->status_ep);
8526142e0aeSVitaly Kuzmichev 
85323cd1385SRemy Bohmer 	dev->cdc_filter = 0;
85423cd1385SRemy Bohmer 	dev->config = 0;
85523cd1385SRemy Bohmer }
85623cd1385SRemy Bohmer 
8576142e0aeSVitaly Kuzmichev /*
8586142e0aeSVitaly Kuzmichev  * change our operational config.  must agree with the code
85923cd1385SRemy Bohmer  * that returns config descriptors, and altsetting code.
86023cd1385SRemy Bohmer  */
8616142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number,
8626142e0aeSVitaly Kuzmichev 				gfp_t gfp_flags)
86323cd1385SRemy Bohmer {
86423cd1385SRemy Bohmer 	int			result = 0;
86523cd1385SRemy Bohmer 	struct usb_gadget	*gadget = dev->gadget;
86623cd1385SRemy Bohmer 
86723cd1385SRemy Bohmer 	if (gadget_is_sa1100(gadget)
86823cd1385SRemy Bohmer 			&& dev->config
86923cd1385SRemy Bohmer 			&& dev->tx_qlen != 0) {
87023cd1385SRemy Bohmer 		/* tx fifo is full, but we can't clear it...*/
8717de73185SVitaly Kuzmichev 		error("can't change configurations");
87223cd1385SRemy Bohmer 		return -ESPIPE;
87323cd1385SRemy Bohmer 	}
87423cd1385SRemy Bohmer 	eth_reset_config(dev);
87523cd1385SRemy Bohmer 
87623cd1385SRemy Bohmer 	switch (number) {
87723cd1385SRemy Bohmer 	case DEV_CONFIG_VALUE:
87823cd1385SRemy Bohmer 		result = set_ether_config(dev, gfp_flags);
87923cd1385SRemy Bohmer 		break;
88023cd1385SRemy Bohmer 	default:
88123cd1385SRemy Bohmer 		result = -EINVAL;
88223cd1385SRemy Bohmer 		/* FALL THROUGH */
88323cd1385SRemy Bohmer 	case 0:
88423cd1385SRemy Bohmer 		break;
88523cd1385SRemy Bohmer 	}
88623cd1385SRemy Bohmer 
88723cd1385SRemy Bohmer 	if (result) {
88823cd1385SRemy Bohmer 		if (number)
88923cd1385SRemy Bohmer 			eth_reset_config(dev);
89023cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget,
89123cd1385SRemy Bohmer 				gadget_is_otg(dev->gadget) ? 8 : 100);
89223cd1385SRemy Bohmer 	} else {
89323cd1385SRemy Bohmer 		char *speed;
89423cd1385SRemy Bohmer 		unsigned power;
89523cd1385SRemy Bohmer 
89623cd1385SRemy Bohmer 		power = 2 * eth_config.bMaxPower;
89723cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget, power);
89823cd1385SRemy Bohmer 
89923cd1385SRemy Bohmer 		switch (gadget->speed) {
9006142e0aeSVitaly Kuzmichev 		case USB_SPEED_FULL:
9016142e0aeSVitaly Kuzmichev 			speed = "full"; break;
90223cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
9036142e0aeSVitaly Kuzmichev 		case USB_SPEED_HIGH:
9046142e0aeSVitaly Kuzmichev 			speed = "high"; break;
90523cd1385SRemy Bohmer #endif
9066142e0aeSVitaly Kuzmichev 		default:
9076142e0aeSVitaly Kuzmichev 			speed = "?"; break;
90823cd1385SRemy Bohmer 		}
90923cd1385SRemy Bohmer 
91023cd1385SRemy Bohmer 		dev->config = number;
9117de73185SVitaly Kuzmichev 		printf("%s speed config #%d: %d mA, %s, using %s\n",
91223cd1385SRemy Bohmer 				speed, number, power, driver_desc,
91323cd1385SRemy Bohmer 				(cdc_active(dev) ? "CDC Ethernet"
91423cd1385SRemy Bohmer 						: "CDC Ethernet Subset"));
91523cd1385SRemy Bohmer 	}
91623cd1385SRemy Bohmer 	return result;
91723cd1385SRemy Bohmer }
91823cd1385SRemy Bohmer 
91923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
92023cd1385SRemy Bohmer 
92123cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
92223cd1385SRemy Bohmer 
9236142e0aeSVitaly Kuzmichev /*
9246142e0aeSVitaly Kuzmichev  * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
92523cd1385SRemy Bohmer  * only to notify the host about link status changes (which we support) or
92623cd1385SRemy Bohmer  * report completion of some encapsulated command.  Since
92723cd1385SRemy Bohmer  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
92823cd1385SRemy Bohmer  * command mechanism; and only one status request is ever queued.
92923cd1385SRemy Bohmer  */
93023cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
93123cd1385SRemy Bohmer {
93223cd1385SRemy Bohmer 	struct usb_cdc_notification	*event = req->buf;
93323cd1385SRemy Bohmer 	int				value = req->status;
93423cd1385SRemy Bohmer 	struct eth_dev			*dev = ep->driver_data;
93523cd1385SRemy Bohmer 
93623cd1385SRemy Bohmer 	/* issue the second notification if host reads the first */
93723cd1385SRemy Bohmer 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
93823cd1385SRemy Bohmer 			&& value == 0) {
93923cd1385SRemy Bohmer 		__le32	*data = req->buf + sizeof *event;
94023cd1385SRemy Bohmer 
94123cd1385SRemy Bohmer 		event->bmRequestType = 0xA1;
94223cd1385SRemy Bohmer 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
94323cd1385SRemy Bohmer 		event->wValue = __constant_cpu_to_le16(0);
94423cd1385SRemy Bohmer 		event->wIndex = __constant_cpu_to_le16(1);
94523cd1385SRemy Bohmer 		event->wLength = __constant_cpu_to_le16(8);
94623cd1385SRemy Bohmer 
94723cd1385SRemy Bohmer 		/* SPEED_CHANGE data is up/down speeds in bits/sec */
94823cd1385SRemy Bohmer 		data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
94923cd1385SRemy Bohmer 
95023cd1385SRemy Bohmer 		req->length = STATUS_BYTECOUNT;
95123cd1385SRemy Bohmer 		value = usb_ep_queue(ep, req, GFP_ATOMIC);
9527de73185SVitaly Kuzmichev 		debug("send SPEED_CHANGE --> %d\n", value);
95323cd1385SRemy Bohmer 		if (value == 0)
95423cd1385SRemy Bohmer 			return;
95523cd1385SRemy Bohmer 	} else if (value != -ECONNRESET) {
9567de73185SVitaly Kuzmichev 		debug("event %02x --> %d\n",
95723cd1385SRemy Bohmer 			event->bNotificationType, value);
95823cd1385SRemy Bohmer 		if (event->bNotificationType ==
9596142e0aeSVitaly Kuzmichev 				USB_CDC_NOTIFY_SPEED_CHANGE) {
96023cd1385SRemy Bohmer 			l_ethdev.network_started = 1;
96123cd1385SRemy Bohmer 			printf("USB network up!\n");
96223cd1385SRemy Bohmer 		}
96323cd1385SRemy Bohmer 	}
96423cd1385SRemy Bohmer 	req->context = NULL;
96523cd1385SRemy Bohmer }
96623cd1385SRemy Bohmer 
96723cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev)
96823cd1385SRemy Bohmer {
96923cd1385SRemy Bohmer 	struct usb_request		*req = dev->stat_req;
97023cd1385SRemy Bohmer 	struct usb_cdc_notification	*event;
97123cd1385SRemy Bohmer 	int				value;
97223cd1385SRemy Bohmer 
9736142e0aeSVitaly Kuzmichev 	/*
9746142e0aeSVitaly Kuzmichev 	 * flush old status
97523cd1385SRemy Bohmer 	 *
97623cd1385SRemy Bohmer 	 * FIXME ugly idiom, maybe we'd be better with just
97723cd1385SRemy Bohmer 	 * a "cancel the whole queue" primitive since any
97823cd1385SRemy Bohmer 	 * unlink-one primitive has way too many error modes.
97923cd1385SRemy Bohmer 	 * here, we "know" toggle is already clear...
98023cd1385SRemy Bohmer 	 *
98123cd1385SRemy Bohmer 	 * FIXME iff req->context != null just dequeue it
98223cd1385SRemy Bohmer 	 */
98323cd1385SRemy Bohmer 	usb_ep_disable(dev->status_ep);
98423cd1385SRemy Bohmer 	usb_ep_enable(dev->status_ep, dev->status);
98523cd1385SRemy Bohmer 
9866142e0aeSVitaly Kuzmichev 	/*
9876142e0aeSVitaly Kuzmichev 	 * 3.8.1 says to issue first NETWORK_CONNECTION, then
98823cd1385SRemy Bohmer 	 * a SPEED_CHANGE.  could be useful in some configs.
98923cd1385SRemy Bohmer 	 */
99023cd1385SRemy Bohmer 	event = req->buf;
99123cd1385SRemy Bohmer 	event->bmRequestType = 0xA1;
99223cd1385SRemy Bohmer 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
99323cd1385SRemy Bohmer 	event->wValue = __constant_cpu_to_le16(1);	/* connected */
99423cd1385SRemy Bohmer 	event->wIndex = __constant_cpu_to_le16(1);
99523cd1385SRemy Bohmer 	event->wLength = 0;
99623cd1385SRemy Bohmer 
99723cd1385SRemy Bohmer 	req->length = sizeof *event;
99823cd1385SRemy Bohmer 	req->complete = eth_status_complete;
99923cd1385SRemy Bohmer 	req->context = dev;
100023cd1385SRemy Bohmer 
100123cd1385SRemy Bohmer 	value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
100223cd1385SRemy Bohmer 	if (value < 0)
10037de73185SVitaly Kuzmichev 		debug("status buf queue --> %d\n", value);
100423cd1385SRemy Bohmer }
100523cd1385SRemy Bohmer 
100623cd1385SRemy Bohmer #endif
100723cd1385SRemy Bohmer 
100823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
100923cd1385SRemy Bohmer 
101023cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
101123cd1385SRemy Bohmer {
101223cd1385SRemy Bohmer 	if (req->status || req->actual != req->length)
10137de73185SVitaly Kuzmichev 		debug("setup complete --> %d, %d/%d\n",
101423cd1385SRemy Bohmer 				req->status, req->actual, req->length);
101523cd1385SRemy Bohmer }
101623cd1385SRemy Bohmer 
101723cd1385SRemy Bohmer /*
101823cd1385SRemy Bohmer  * The setup() callback implements all the ep0 functionality that's not
101923cd1385SRemy Bohmer  * handled lower down.  CDC has a number of less-common features:
102023cd1385SRemy Bohmer  *
102123cd1385SRemy Bohmer  *  - two interfaces:  control, and ethernet data
102223cd1385SRemy Bohmer  *  - Ethernet data interface has two altsettings:  default, and active
102323cd1385SRemy Bohmer  *  - class-specific descriptors for the control interface
102423cd1385SRemy Bohmer  *  - class-specific control requests
102523cd1385SRemy Bohmer  */
102623cd1385SRemy Bohmer static int
102723cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
102823cd1385SRemy Bohmer {
102923cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data(gadget);
103023cd1385SRemy Bohmer 	struct usb_request	*req = dev->req;
103123cd1385SRemy Bohmer 	int			value = -EOPNOTSUPP;
103223cd1385SRemy Bohmer 	u16			wIndex = le16_to_cpu(ctrl->wIndex);
103323cd1385SRemy Bohmer 	u16			wValue = le16_to_cpu(ctrl->wValue);
103423cd1385SRemy Bohmer 	u16			wLength = le16_to_cpu(ctrl->wLength);
103523cd1385SRemy Bohmer 
10366142e0aeSVitaly Kuzmichev 	/*
10376142e0aeSVitaly Kuzmichev 	 * descriptors just go into the pre-allocated ep0 buffer,
103823cd1385SRemy Bohmer 	 * while config change events may enable network traffic.
103923cd1385SRemy Bohmer 	 */
104023cd1385SRemy Bohmer 
10417de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
104223cd1385SRemy Bohmer 
104323cd1385SRemy Bohmer 	req->complete = eth_setup_complete;
104423cd1385SRemy Bohmer 	switch (ctrl->bRequest) {
104523cd1385SRemy Bohmer 
104623cd1385SRemy Bohmer 	case USB_REQ_GET_DESCRIPTOR:
104723cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
104823cd1385SRemy Bohmer 			break;
104923cd1385SRemy Bohmer 		switch (wValue >> 8) {
105023cd1385SRemy Bohmer 
105123cd1385SRemy Bohmer 		case USB_DT_DEVICE:
105223cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof device_desc);
105323cd1385SRemy Bohmer 			memcpy(req->buf, &device_desc, value);
105423cd1385SRemy Bohmer 			break;
105523cd1385SRemy Bohmer 		case USB_DT_DEVICE_QUALIFIER:
105623cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
105723cd1385SRemy Bohmer 				break;
105823cd1385SRemy Bohmer 			value = min(wLength, (u16) sizeof dev_qualifier);
105923cd1385SRemy Bohmer 			memcpy(req->buf, &dev_qualifier, value);
106023cd1385SRemy Bohmer 			break;
106123cd1385SRemy Bohmer 
106223cd1385SRemy Bohmer 		case USB_DT_OTHER_SPEED_CONFIG:
106323cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
106423cd1385SRemy Bohmer 				break;
106523cd1385SRemy Bohmer 			/* FALLTHROUGH */
106623cd1385SRemy Bohmer 		case USB_DT_CONFIG:
106723cd1385SRemy Bohmer 			value = config_buf(gadget, req->buf,
106823cd1385SRemy Bohmer 					wValue >> 8,
106923cd1385SRemy Bohmer 					wValue & 0xff,
107023cd1385SRemy Bohmer 					gadget_is_otg(gadget));
107123cd1385SRemy Bohmer 			if (value >= 0)
107223cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
107323cd1385SRemy Bohmer 			break;
107423cd1385SRemy Bohmer 
107523cd1385SRemy Bohmer 		case USB_DT_STRING:
107623cd1385SRemy Bohmer 			value = usb_gadget_get_string(&stringtab,
107723cd1385SRemy Bohmer 					wValue & 0xff, req->buf);
107823cd1385SRemy Bohmer 
107923cd1385SRemy Bohmer 			if (value >= 0)
108023cd1385SRemy Bohmer 				value = min(wLength, (u16) value);
108123cd1385SRemy Bohmer 
108223cd1385SRemy Bohmer 			break;
108323cd1385SRemy Bohmer 		}
108423cd1385SRemy Bohmer 		break;
108523cd1385SRemy Bohmer 
108623cd1385SRemy Bohmer 	case USB_REQ_SET_CONFIGURATION:
108723cd1385SRemy Bohmer 		if (ctrl->bRequestType != 0)
108823cd1385SRemy Bohmer 			break;
108923cd1385SRemy Bohmer 		if (gadget->a_hnp_support)
10907de73185SVitaly Kuzmichev 			debug("HNP available\n");
109123cd1385SRemy Bohmer 		else if (gadget->a_alt_hnp_support)
10927de73185SVitaly Kuzmichev 			debug("HNP needs a different root port\n");
109323cd1385SRemy Bohmer 		value = eth_set_config(dev, wValue, GFP_ATOMIC);
109423cd1385SRemy Bohmer 		break;
109523cd1385SRemy Bohmer 	case USB_REQ_GET_CONFIGURATION:
109623cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
109723cd1385SRemy Bohmer 			break;
109823cd1385SRemy Bohmer 		*(u8 *)req->buf = dev->config;
109923cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
110023cd1385SRemy Bohmer 		break;
110123cd1385SRemy Bohmer 
110223cd1385SRemy Bohmer 	case USB_REQ_SET_INTERFACE:
110323cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_RECIP_INTERFACE
110423cd1385SRemy Bohmer 				|| !dev->config
110523cd1385SRemy Bohmer 				|| wIndex > 1)
110623cd1385SRemy Bohmer 			break;
110723cd1385SRemy Bohmer 		if (!cdc_active(dev) && wIndex != 0)
110823cd1385SRemy Bohmer 			break;
110923cd1385SRemy Bohmer 
11106142e0aeSVitaly Kuzmichev 		/*
11116142e0aeSVitaly Kuzmichev 		 * PXA hardware partially handles SET_INTERFACE;
111223cd1385SRemy Bohmer 		 * we need to kluge around that interference.
111323cd1385SRemy Bohmer 		 */
111423cd1385SRemy Bohmer 		if (gadget_is_pxa(gadget)) {
111523cd1385SRemy Bohmer 			value = eth_set_config(dev, DEV_CONFIG_VALUE,
111623cd1385SRemy Bohmer 						GFP_ATOMIC);
111723cd1385SRemy Bohmer 			goto done_set_intf;
111823cd1385SRemy Bohmer 		}
111923cd1385SRemy Bohmer 
112023cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
112123cd1385SRemy Bohmer 		switch (wIndex) {
112223cd1385SRemy Bohmer 		case 0:		/* control/master intf */
112323cd1385SRemy Bohmer 			if (wValue != 0)
112423cd1385SRemy Bohmer 				break;
112523cd1385SRemy Bohmer 			if (dev->status) {
112623cd1385SRemy Bohmer 				usb_ep_disable(dev->status_ep);
112723cd1385SRemy Bohmer 				usb_ep_enable(dev->status_ep, dev->status);
112823cd1385SRemy Bohmer 			}
112923cd1385SRemy Bohmer 			value = 0;
113023cd1385SRemy Bohmer 			break;
113123cd1385SRemy Bohmer 		case 1:		/* data intf */
113223cd1385SRemy Bohmer 			if (wValue > 1)
113323cd1385SRemy Bohmer 				break;
113423cd1385SRemy Bohmer 			usb_ep_disable(dev->in_ep);
113523cd1385SRemy Bohmer 			usb_ep_disable(dev->out_ep);
113623cd1385SRemy Bohmer 
11376142e0aeSVitaly Kuzmichev 			/*
11386142e0aeSVitaly Kuzmichev 			 * CDC requires the data transfers not be done from
113923cd1385SRemy Bohmer 			 * the default interface setting ... also, setting
114023cd1385SRemy Bohmer 			 * the non-default interface resets filters etc.
114123cd1385SRemy Bohmer 			 */
114223cd1385SRemy Bohmer 			if (wValue == 1) {
114323cd1385SRemy Bohmer 				if (!cdc_active(dev))
114423cd1385SRemy Bohmer 					break;
114523cd1385SRemy Bohmer 				usb_ep_enable(dev->in_ep, dev->in);
114623cd1385SRemy Bohmer 				usb_ep_enable(dev->out_ep, dev->out);
114723cd1385SRemy Bohmer 				dev->cdc_filter = DEFAULT_FILTER;
114823cd1385SRemy Bohmer 				if (dev->status)
114923cd1385SRemy Bohmer 					issue_start_status(dev);
115023cd1385SRemy Bohmer 			}
115123cd1385SRemy Bohmer 
115223cd1385SRemy Bohmer 			value = 0;
115323cd1385SRemy Bohmer 			break;
115423cd1385SRemy Bohmer 		}
115523cd1385SRemy Bohmer #else
11566142e0aeSVitaly Kuzmichev 		/*
11576142e0aeSVitaly Kuzmichev 		 * FIXME this is wrong, as is the assumption that
115823cd1385SRemy Bohmer 		 * all non-PXA hardware talks real CDC ...
115923cd1385SRemy Bohmer 		 */
11607de73185SVitaly Kuzmichev 		debug("set_interface ignored!\n");
116123cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
116223cd1385SRemy Bohmer 
116323cd1385SRemy Bohmer done_set_intf:
116423cd1385SRemy Bohmer 		break;
116523cd1385SRemy Bohmer 	case USB_REQ_GET_INTERFACE:
116623cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
116723cd1385SRemy Bohmer 				|| !dev->config
116823cd1385SRemy Bohmer 				|| wIndex > 1)
116923cd1385SRemy Bohmer 			break;
117023cd1385SRemy Bohmer 		if (!(cdc_active(dev)) && wIndex != 0)
117123cd1385SRemy Bohmer 			break;
117223cd1385SRemy Bohmer 
117323cd1385SRemy Bohmer 		/* for CDC, iff carrier is on, data interface is active. */
117423cd1385SRemy Bohmer 		if (wIndex != 1)
117523cd1385SRemy Bohmer 			*(u8 *)req->buf = 0;
117623cd1385SRemy Bohmer 		else {
117723cd1385SRemy Bohmer 			/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
117823cd1385SRemy Bohmer 			/* carrier always ok ...*/
117923cd1385SRemy Bohmer 			*(u8 *)req->buf = 1 ;
118023cd1385SRemy Bohmer 		}
118123cd1385SRemy Bohmer 		value = min(wLength, (u16) 1);
118223cd1385SRemy Bohmer 		break;
118323cd1385SRemy Bohmer 
118423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
118523cd1385SRemy Bohmer 	case USB_CDC_SET_ETHERNET_PACKET_FILTER:
11866142e0aeSVitaly Kuzmichev 		/*
11876142e0aeSVitaly Kuzmichev 		 * see 6.2.30: no data, wIndex = interface,
118823cd1385SRemy Bohmer 		 * wValue = packet filter bitmap
118923cd1385SRemy Bohmer 		 */
119023cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
119123cd1385SRemy Bohmer 				|| !cdc_active(dev)
119223cd1385SRemy Bohmer 				|| wLength != 0
119323cd1385SRemy Bohmer 				|| wIndex > 1)
119423cd1385SRemy Bohmer 			break;
11957de73185SVitaly Kuzmichev 		debug("packet filter %02x\n", wValue);
119623cd1385SRemy Bohmer 		dev->cdc_filter = wValue;
119723cd1385SRemy Bohmer 		value = 0;
119823cd1385SRemy Bohmer 		break;
119923cd1385SRemy Bohmer 
12006142e0aeSVitaly Kuzmichev 	/*
12016142e0aeSVitaly Kuzmichev 	 * and potentially:
120223cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
120323cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
120423cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
120523cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_STATISTIC:
120623cd1385SRemy Bohmer 	 */
120723cd1385SRemy Bohmer 
120823cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
120923cd1385SRemy Bohmer 
121023cd1385SRemy Bohmer 	default:
12117de73185SVitaly Kuzmichev 		debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
121223cd1385SRemy Bohmer 			ctrl->bRequestType, ctrl->bRequest,
121323cd1385SRemy Bohmer 			wValue, wIndex, wLength);
121423cd1385SRemy Bohmer 	}
121523cd1385SRemy Bohmer 
121623cd1385SRemy Bohmer 	/* respond with data transfer before status phase? */
121723cd1385SRemy Bohmer 	if (value >= 0) {
12187de73185SVitaly Kuzmichev 		debug("respond with data transfer before status phase\n");
121923cd1385SRemy Bohmer 		req->length = value;
122023cd1385SRemy Bohmer 		req->zero = value < wLength
122123cd1385SRemy Bohmer 				&& (value % gadget->ep0->maxpacket) == 0;
122223cd1385SRemy Bohmer 		value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
122323cd1385SRemy Bohmer 		if (value < 0) {
12247de73185SVitaly Kuzmichev 			debug("ep_queue --> %d\n", value);
122523cd1385SRemy Bohmer 			req->status = 0;
122623cd1385SRemy Bohmer 			eth_setup_complete(gadget->ep0, req);
122723cd1385SRemy Bohmer 		}
122823cd1385SRemy Bohmer 	}
122923cd1385SRemy Bohmer 
123023cd1385SRemy Bohmer 	/* host either stalls (value < 0) or reports success */
123123cd1385SRemy Bohmer 	return value;
123223cd1385SRemy Bohmer }
123323cd1385SRemy Bohmer 
123423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
123523cd1385SRemy Bohmer 
123623cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req);
123723cd1385SRemy Bohmer 
12386142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req,
123923cd1385SRemy Bohmer 				gfp_t gfp_flags)
124023cd1385SRemy Bohmer {
124123cd1385SRemy Bohmer 	int			retval = -ENOMEM;
124223cd1385SRemy Bohmer 	size_t			size;
124323cd1385SRemy Bohmer 
12446142e0aeSVitaly Kuzmichev 	/*
12456142e0aeSVitaly Kuzmichev 	 * Padding up to RX_EXTRA handles minor disagreements with host.
124623cd1385SRemy Bohmer 	 * Normally we use the USB "terminate on short read" convention;
124723cd1385SRemy Bohmer 	 * so allow up to (N*maxpacket), since that memory is normally
124823cd1385SRemy Bohmer 	 * already allocated.  Some hardware doesn't deal well with short
124923cd1385SRemy Bohmer 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
125023cd1385SRemy Bohmer 	 * byte off the end (to force hardware errors on overflow).
125123cd1385SRemy Bohmer 	 */
125223cd1385SRemy Bohmer 
12537de73185SVitaly Kuzmichev 	debug("%s\n", __func__);
125423cd1385SRemy Bohmer 
125523cd1385SRemy Bohmer 	size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
125623cd1385SRemy Bohmer 	size += dev->out_ep->maxpacket - 1;
125723cd1385SRemy Bohmer 	size -= size % dev->out_ep->maxpacket;
125823cd1385SRemy Bohmer 
12596142e0aeSVitaly Kuzmichev 	/*
12606142e0aeSVitaly Kuzmichev 	 * Some platforms perform better when IP packets are aligned,
126123cd1385SRemy Bohmer 	 * but on at least one, checksumming fails otherwise.
126223cd1385SRemy Bohmer 	 */
126323cd1385SRemy Bohmer 
126423cd1385SRemy Bohmer 	req->buf = (u8 *) NetRxPackets[0];
126523cd1385SRemy Bohmer 	req->length = size;
126623cd1385SRemy Bohmer 	req->complete = rx_complete;
126723cd1385SRemy Bohmer 
126823cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
126923cd1385SRemy Bohmer 
12706142e0aeSVitaly Kuzmichev 	if (retval)
12717de73185SVitaly Kuzmichev 		error("rx submit --> %d", retval);
12726142e0aeSVitaly Kuzmichev 
127323cd1385SRemy Bohmer 	return retval;
127423cd1385SRemy Bohmer }
127523cd1385SRemy Bohmer 
127623cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req)
127723cd1385SRemy Bohmer {
127823cd1385SRemy Bohmer 	struct eth_dev	*dev = ep->driver_data;
127923cd1385SRemy Bohmer 
12807de73185SVitaly Kuzmichev 	debug("%s: status %d\n", __func__, req->status);
1281c85d70efSVitaly Kuzmichev 	switch (req->status) {
1282c85d70efSVitaly Kuzmichev 	/* normal completion */
1283c85d70efSVitaly Kuzmichev 	case 0:
1284c85d70efSVitaly Kuzmichev 		dev->stats.rx_packets++;
1285c85d70efSVitaly Kuzmichev 		dev->stats.rx_bytes += req->length;
1286c85d70efSVitaly Kuzmichev 		break;
1287c85d70efSVitaly Kuzmichev 
1288c85d70efSVitaly Kuzmichev 	/* software-driven interface shutdown */
1289c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1290c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1291c85d70efSVitaly Kuzmichev 	/* for hardware automagic (such as pxa) */
1292c85d70efSVitaly Kuzmichev 	case -ECONNABORTED:		/* endpoint reset */
1293c85d70efSVitaly Kuzmichev 		break;
1294c85d70efSVitaly Kuzmichev 
1295c85d70efSVitaly Kuzmichev 	/* data overrun */
1296c85d70efSVitaly Kuzmichev 	case -EOVERFLOW:
1297c85d70efSVitaly Kuzmichev 		dev->stats.rx_over_errors++;
1298c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1299c85d70efSVitaly Kuzmichev 	default:
1300c85d70efSVitaly Kuzmichev 		dev->stats.rx_errors++;
1301c85d70efSVitaly Kuzmichev 		break;
1302c85d70efSVitaly Kuzmichev 	}
130323cd1385SRemy Bohmer 
130423cd1385SRemy Bohmer 	packet_received = 1;
130523cd1385SRemy Bohmer }
130623cd1385SRemy Bohmer 
130723cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
130823cd1385SRemy Bohmer {
130923cd1385SRemy Bohmer 
131023cd1385SRemy Bohmer 	dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
131123cd1385SRemy Bohmer 
131223cd1385SRemy Bohmer 	if (!dev->tx_req)
1313ac5d32d1SVitaly Kuzmichev 		goto fail1;
131423cd1385SRemy Bohmer 
131523cd1385SRemy Bohmer 	dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
131623cd1385SRemy Bohmer 
131723cd1385SRemy Bohmer 	if (!dev->rx_req)
1318ac5d32d1SVitaly Kuzmichev 		goto fail2;
131923cd1385SRemy Bohmer 
132023cd1385SRemy Bohmer 	return 0;
132123cd1385SRemy Bohmer 
1322ac5d32d1SVitaly Kuzmichev fail2:
1323ac5d32d1SVitaly Kuzmichev 	usb_ep_free_request(dev->in_ep, dev->tx_req);
1324ac5d32d1SVitaly Kuzmichev fail1:
13257de73185SVitaly Kuzmichev 	error("can't alloc requests");
132623cd1385SRemy Bohmer 	return -1;
132723cd1385SRemy Bohmer }
132823cd1385SRemy Bohmer 
132923cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req)
133023cd1385SRemy Bohmer {
1331c85d70efSVitaly Kuzmichev 	struct eth_dev	*dev = ep->driver_data;
1332c85d70efSVitaly Kuzmichev 
13337de73185SVitaly Kuzmichev 	debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1334c85d70efSVitaly Kuzmichev 	switch (req->status) {
1335c85d70efSVitaly Kuzmichev 	default:
1336c85d70efSVitaly Kuzmichev 		dev->stats.tx_errors++;
1337c85d70efSVitaly Kuzmichev 		debug("tx err %d\n", req->status);
1338c85d70efSVitaly Kuzmichev 		/* FALLTHROUGH */
1339c85d70efSVitaly Kuzmichev 	case -ECONNRESET:		/* unlink */
1340c85d70efSVitaly Kuzmichev 	case -ESHUTDOWN:		/* disconnect etc */
1341c85d70efSVitaly Kuzmichev 		break;
1342c85d70efSVitaly Kuzmichev 	case 0:
1343c85d70efSVitaly Kuzmichev 		dev->stats.tx_bytes += req->length;
1344c85d70efSVitaly Kuzmichev 	}
1345c85d70efSVitaly Kuzmichev 	dev->stats.tx_packets++;
1346c85d70efSVitaly Kuzmichev 
134723cd1385SRemy Bohmer 	packet_sent = 1;
134823cd1385SRemy Bohmer }
134923cd1385SRemy Bohmer 
135023cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev)
135123cd1385SRemy Bohmer {
135223cd1385SRemy Bohmer 	/* no filters for the CDC subset; always promisc */
135323cd1385SRemy Bohmer 	if (subset_active(dev))
135423cd1385SRemy Bohmer 		return 1;
135523cd1385SRemy Bohmer 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
135623cd1385SRemy Bohmer }
135723cd1385SRemy Bohmer 
135823cd1385SRemy Bohmer #if 0
135923cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
136023cd1385SRemy Bohmer {
136123cd1385SRemy Bohmer 	struct eth_dev		*dev = netdev_priv(net);
136223cd1385SRemy Bohmer 	int			length = skb->len;
136323cd1385SRemy Bohmer 	int			retval;
136423cd1385SRemy Bohmer 	struct usb_request	*req = NULL;
136523cd1385SRemy Bohmer 	unsigned long		flags;
136623cd1385SRemy Bohmer 
136723cd1385SRemy Bohmer 	/* apply outgoing CDC or RNDIS filters */
136823cd1385SRemy Bohmer 	if (!eth_is_promisc (dev)) {
136923cd1385SRemy Bohmer 		u8		*dest = skb->data;
137023cd1385SRemy Bohmer 
137123cd1385SRemy Bohmer 		if (is_multicast_ether_addr(dest)) {
137223cd1385SRemy Bohmer 			u16	type;
137323cd1385SRemy Bohmer 
137423cd1385SRemy Bohmer 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
137523cd1385SRemy Bohmer 			 * SET_ETHERNET_MULTICAST_FILTERS requests
137623cd1385SRemy Bohmer 			 */
137723cd1385SRemy Bohmer 			if (is_broadcast_ether_addr(dest))
137823cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_BROADCAST;
137923cd1385SRemy Bohmer 			else
138023cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
138123cd1385SRemy Bohmer 			if (!(dev->cdc_filter & type)) {
138223cd1385SRemy Bohmer 				dev_kfree_skb_any (skb);
138323cd1385SRemy Bohmer 				return 0;
138423cd1385SRemy Bohmer 			}
138523cd1385SRemy Bohmer 		}
138623cd1385SRemy Bohmer 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
138723cd1385SRemy Bohmer 	}
138823cd1385SRemy Bohmer 
138923cd1385SRemy Bohmer 	spin_lock_irqsave(&dev->req_lock, flags);
139023cd1385SRemy Bohmer 	/*
139123cd1385SRemy Bohmer 	 * this freelist can be empty if an interrupt triggered disconnect()
139223cd1385SRemy Bohmer 	 * and reconfigured the gadget (shutting down this queue) after the
139323cd1385SRemy Bohmer 	 * network stack decided to xmit but before we got the spinlock.
139423cd1385SRemy Bohmer 	 */
139523cd1385SRemy Bohmer 	if (list_empty(&dev->tx_reqs)) {
139623cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
139723cd1385SRemy Bohmer 		return 1;
139823cd1385SRemy Bohmer 	}
139923cd1385SRemy Bohmer 
140023cd1385SRemy Bohmer 	req = container_of (dev->tx_reqs.next, struct usb_request, list);
140123cd1385SRemy Bohmer 	list_del (&req->list);
140223cd1385SRemy Bohmer 
140323cd1385SRemy Bohmer 	/* temporarily stop TX queue when the freelist empties */
140423cd1385SRemy Bohmer 	if (list_empty (&dev->tx_reqs))
140523cd1385SRemy Bohmer 		netif_stop_queue (net);
140623cd1385SRemy Bohmer 	spin_unlock_irqrestore(&dev->req_lock, flags);
140723cd1385SRemy Bohmer 
140823cd1385SRemy Bohmer 	/* no buffer copies needed, unless the network stack did it
140923cd1385SRemy Bohmer 	 * or the hardware can't use skb buffers.
141023cd1385SRemy Bohmer 	 * or there's not enough space for any RNDIS headers we need
141123cd1385SRemy Bohmer 	 */
141223cd1385SRemy Bohmer 	if (rndis_active(dev)) {
141323cd1385SRemy Bohmer 		struct sk_buff	*skb_rndis;
141423cd1385SRemy Bohmer 
141523cd1385SRemy Bohmer 		skb_rndis = skb_realloc_headroom (skb,
141623cd1385SRemy Bohmer 				sizeof (struct rndis_packet_msg_type));
141723cd1385SRemy Bohmer 		if (!skb_rndis)
141823cd1385SRemy Bohmer 			goto drop;
141923cd1385SRemy Bohmer 
142023cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
142123cd1385SRemy Bohmer 		skb = skb_rndis;
142223cd1385SRemy Bohmer 		rndis_add_hdr (skb);
142323cd1385SRemy Bohmer 		length = skb->len;
142423cd1385SRemy Bohmer 	}
142523cd1385SRemy Bohmer 	req->buf = skb->data;
142623cd1385SRemy Bohmer 	req->context = skb;
142723cd1385SRemy Bohmer 	req->complete = tx_complete;
142823cd1385SRemy Bohmer 
142923cd1385SRemy Bohmer 	/* use zlp framing on tx for strict CDC-Ether conformance,
143023cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
143123cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
143223cd1385SRemy Bohmer 	 */
143323cd1385SRemy Bohmer 	req->zero = 1;
143423cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
143523cd1385SRemy Bohmer 		length++;
143623cd1385SRemy Bohmer 
143723cd1385SRemy Bohmer 	req->length = length;
143823cd1385SRemy Bohmer 
143923cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
144023cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
144123cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
144223cd1385SRemy Bohmer 			? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
144323cd1385SRemy Bohmer 			: 0;
144423cd1385SRemy Bohmer 
144523cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
144623cd1385SRemy Bohmer 	switch (retval) {
144723cd1385SRemy Bohmer 	default:
144823cd1385SRemy Bohmer 		DEBUG (dev, "tx queue err %d\n", retval);
144923cd1385SRemy Bohmer 		break;
145023cd1385SRemy Bohmer 	case 0:
145123cd1385SRemy Bohmer 		net->trans_start = jiffies;
145223cd1385SRemy Bohmer 		atomic_inc (&dev->tx_qlen);
145323cd1385SRemy Bohmer 	}
145423cd1385SRemy Bohmer 
145523cd1385SRemy Bohmer 	if (retval) {
145623cd1385SRemy Bohmer drop:
145723cd1385SRemy Bohmer 		dev->stats.tx_dropped++;
145823cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
145923cd1385SRemy Bohmer 		spin_lock_irqsave(&dev->req_lock, flags);
146023cd1385SRemy Bohmer 		if (list_empty (&dev->tx_reqs))
146123cd1385SRemy Bohmer 			netif_start_queue (net);
146223cd1385SRemy Bohmer 		list_add (&req->list, &dev->tx_reqs);
146323cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
146423cd1385SRemy Bohmer 	}
146523cd1385SRemy Bohmer 	return 0;
146623cd1385SRemy Bohmer }
146723cd1385SRemy Bohmer 
146823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
146923cd1385SRemy Bohmer #endif
147023cd1385SRemy Bohmer 
147123cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget)
147223cd1385SRemy Bohmer {
147323cd1385SRemy Bohmer 	struct eth_dev *dev = get_gadget_data(gadget);
147423cd1385SRemy Bohmer 
14757de73185SVitaly Kuzmichev 	debug("%s...\n", __func__);
147623cd1385SRemy Bohmer 
14770129e327SVitaly Kuzmichev 	/* we've already been disconnected ... no i/o is active */
14780129e327SVitaly Kuzmichev 	if (dev->req) {
14790129e327SVitaly Kuzmichev 		usb_ep_free_request(gadget->ep0, dev->req);
14800129e327SVitaly Kuzmichev 		dev->req = NULL;
14810129e327SVitaly Kuzmichev 	}
148223cd1385SRemy Bohmer 	if (dev->stat_req) {
148323cd1385SRemy Bohmer 		usb_ep_free_request(dev->status_ep, dev->stat_req);
148423cd1385SRemy Bohmer 		dev->stat_req = NULL;
148523cd1385SRemy Bohmer 	}
148623cd1385SRemy Bohmer 
148723cd1385SRemy Bohmer 	if (dev->tx_req) {
148823cd1385SRemy Bohmer 		usb_ep_free_request(dev->in_ep, dev->tx_req);
148923cd1385SRemy Bohmer 		dev->tx_req = NULL;
149023cd1385SRemy Bohmer 	}
149123cd1385SRemy Bohmer 
149223cd1385SRemy Bohmer 	if (dev->rx_req) {
14930129e327SVitaly Kuzmichev 		usb_ep_free_request(dev->out_ep, dev->rx_req);
149423cd1385SRemy Bohmer 		dev->rx_req = NULL;
149523cd1385SRemy Bohmer 	}
149623cd1385SRemy Bohmer 
149723cd1385SRemy Bohmer /*	unregister_netdev (dev->net);*/
149823cd1385SRemy Bohmer /*	free_netdev(dev->net);*/
149923cd1385SRemy Bohmer 
1500988ee3e3SLei Wen 	dev->gadget = NULL;
150123cd1385SRemy Bohmer 	set_gadget_data(gadget, NULL);
150223cd1385SRemy Bohmer }
150323cd1385SRemy Bohmer 
150423cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget)
150523cd1385SRemy Bohmer {
150623cd1385SRemy Bohmer 	eth_reset_config(get_gadget_data(gadget));
150723cd1385SRemy Bohmer }
150823cd1385SRemy Bohmer 
150923cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget)
151023cd1385SRemy Bohmer {
151123cd1385SRemy Bohmer 	/* Not used */
151223cd1385SRemy Bohmer }
151323cd1385SRemy Bohmer 
151423cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget)
151523cd1385SRemy Bohmer {
151623cd1385SRemy Bohmer 	/* Not used */
151723cd1385SRemy Bohmer }
151823cd1385SRemy Bohmer 
151923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
152023cd1385SRemy Bohmer 
152123cd1385SRemy Bohmer static int is_eth_addr_valid(char *str)
152223cd1385SRemy Bohmer {
152323cd1385SRemy Bohmer 	if (strlen(str) == 17) {
152423cd1385SRemy Bohmer 		int i;
152523cd1385SRemy Bohmer 		char *p, *q;
152623cd1385SRemy Bohmer 		uchar ea[6];
152723cd1385SRemy Bohmer 
152823cd1385SRemy Bohmer 		/* see if it looks like an ethernet address */
152923cd1385SRemy Bohmer 
153023cd1385SRemy Bohmer 		p = str;
153123cd1385SRemy Bohmer 
153223cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
153323cd1385SRemy Bohmer 			char term = (i == 5 ? '\0' : ':');
153423cd1385SRemy Bohmer 
153523cd1385SRemy Bohmer 			ea[i] = simple_strtol(p, &q, 16);
153623cd1385SRemy Bohmer 
153723cd1385SRemy Bohmer 			if ((q - p) != 2 || *q++ != term)
153823cd1385SRemy Bohmer 				break;
153923cd1385SRemy Bohmer 
154023cd1385SRemy Bohmer 			p = q;
154123cd1385SRemy Bohmer 		}
154223cd1385SRemy Bohmer 
154323cd1385SRemy Bohmer 		if (i == 6) /* it looks ok */
154423cd1385SRemy Bohmer 			return 1;
154523cd1385SRemy Bohmer 	}
154623cd1385SRemy Bohmer 	return 0;
154723cd1385SRemy Bohmer }
154823cd1385SRemy Bohmer 
154923cd1385SRemy Bohmer static u8 nibble(unsigned char c)
155023cd1385SRemy Bohmer {
155123cd1385SRemy Bohmer 	if (likely(isdigit(c)))
155223cd1385SRemy Bohmer 		return c - '0';
155323cd1385SRemy Bohmer 	c = toupper(c);
155423cd1385SRemy Bohmer 	if (likely(isxdigit(c)))
155523cd1385SRemy Bohmer 		return 10 + c - 'A';
155623cd1385SRemy Bohmer 	return 0;
155723cd1385SRemy Bohmer }
155823cd1385SRemy Bohmer 
155923cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr)
156023cd1385SRemy Bohmer {
156123cd1385SRemy Bohmer 	if (str) {
156223cd1385SRemy Bohmer 		unsigned	i;
156323cd1385SRemy Bohmer 
156423cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
156523cd1385SRemy Bohmer 			unsigned char num;
156623cd1385SRemy Bohmer 
156723cd1385SRemy Bohmer 			if ((*str == '.') || (*str == ':'))
156823cd1385SRemy Bohmer 				str++;
156923cd1385SRemy Bohmer 			num = nibble(*str++) << 4;
157023cd1385SRemy Bohmer 			num |= (nibble(*str++));
157123cd1385SRemy Bohmer 			dev_addr[i] = num;
157223cd1385SRemy Bohmer 		}
157323cd1385SRemy Bohmer 		if (is_valid_ether_addr(dev_addr))
157423cd1385SRemy Bohmer 			return 0;
157523cd1385SRemy Bohmer 	}
157623cd1385SRemy Bohmer 	return 1;
157723cd1385SRemy Bohmer }
157823cd1385SRemy Bohmer 
157923cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget)
158023cd1385SRemy Bohmer {
158123cd1385SRemy Bohmer 	struct eth_dev		*dev = &l_ethdev;
158223cd1385SRemy Bohmer 	u8			cdc = 1, zlp = 1;
158323cd1385SRemy Bohmer 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
158423cd1385SRemy Bohmer 	int			gcnum;
158523cd1385SRemy Bohmer 	u8			tmp[7];
158623cd1385SRemy Bohmer 
158723cd1385SRemy Bohmer 	/* these flags are only ever cleared; compiler take note */
158823cd1385SRemy Bohmer #ifndef	DEV_CONFIG_CDC
158923cd1385SRemy Bohmer 	cdc = 0;
159023cd1385SRemy Bohmer #endif
15916142e0aeSVitaly Kuzmichev 	/*
15926142e0aeSVitaly Kuzmichev 	 * Because most host side USB stacks handle CDC Ethernet, that
159323cd1385SRemy Bohmer 	 * standard protocol is _strongly_ preferred for interop purposes.
159423cd1385SRemy Bohmer 	 * (By everyone except Microsoft.)
159523cd1385SRemy Bohmer 	 */
159623cd1385SRemy Bohmer 	if (gadget_is_pxa(gadget)) {
159723cd1385SRemy Bohmer 		/* pxa doesn't support altsettings */
159823cd1385SRemy Bohmer 		cdc = 0;
159923cd1385SRemy Bohmer 	} else if (gadget_is_musbhdrc(gadget)) {
160023cd1385SRemy Bohmer 		/* reduce tx dma overhead by avoiding special cases */
160123cd1385SRemy Bohmer 		zlp = 0;
160223cd1385SRemy Bohmer 	} else if (gadget_is_sh(gadget)) {
160323cd1385SRemy Bohmer 		/* sh doesn't support multiple interfaces or configs */
160423cd1385SRemy Bohmer 		cdc = 0;
160523cd1385SRemy Bohmer 	} else if (gadget_is_sa1100(gadget)) {
160623cd1385SRemy Bohmer 		/* hardware can't write zlps */
160723cd1385SRemy Bohmer 		zlp = 0;
16086142e0aeSVitaly Kuzmichev 		/*
16096142e0aeSVitaly Kuzmichev 		 * sa1100 CAN do CDC, without status endpoint ... we use
161023cd1385SRemy Bohmer 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
161123cd1385SRemy Bohmer 		 */
161223cd1385SRemy Bohmer 		cdc = 0;
161323cd1385SRemy Bohmer 	}
161423cd1385SRemy Bohmer 
161523cd1385SRemy Bohmer 	gcnum = usb_gadget_controller_number(gadget);
161623cd1385SRemy Bohmer 	if (gcnum >= 0)
161723cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
161823cd1385SRemy Bohmer 	else {
16196142e0aeSVitaly Kuzmichev 		/*
16206142e0aeSVitaly Kuzmichev 		 * can't assume CDC works.  don't want to default to
162123cd1385SRemy Bohmer 		 * anything less functional on CDC-capable hardware,
162223cd1385SRemy Bohmer 		 * so we fail in this case.
162323cd1385SRemy Bohmer 		 */
16247de73185SVitaly Kuzmichev 		error("controller '%s' not recognized",
162523cd1385SRemy Bohmer 			gadget->name);
162623cd1385SRemy Bohmer 		return -ENODEV;
162723cd1385SRemy Bohmer 	}
162823cd1385SRemy Bohmer 
16296142e0aeSVitaly Kuzmichev 	/*
16306142e0aeSVitaly Kuzmichev 	 * CDC subset ... recognized by Linux since 2.4.10, but Windows
163123cd1385SRemy Bohmer 	 * drivers aren't widely available.  (That may be improved by
163223cd1385SRemy Bohmer 	 * supporting one submode of the "SAFE" variant of MDLM.)
163323cd1385SRemy Bohmer 	 */
163423cd1385SRemy Bohmer 	if (!cdc) {
163523cd1385SRemy Bohmer 		device_desc.idVendor =
163623cd1385SRemy Bohmer 			__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
163723cd1385SRemy Bohmer 		device_desc.idProduct =
163823cd1385SRemy Bohmer 			__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
163923cd1385SRemy Bohmer 	}
164023cd1385SRemy Bohmer 
164123cd1385SRemy Bohmer 	/* support optional vendor/distro customization */
164223cd1385SRemy Bohmer #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
164323cd1385SRemy Bohmer 	device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
164423cd1385SRemy Bohmer 	device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
164523cd1385SRemy Bohmer #endif
164623cd1385SRemy Bohmer 	if (bcdDevice)
164723cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(bcdDevice);
164823cd1385SRemy Bohmer 	if (iManufacturer)
16492e12abe6SVitaly Kuzmichev 		strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
165023cd1385SRemy Bohmer 	if (iProduct)
16512e12abe6SVitaly Kuzmichev 		strlcpy(product_desc, iProduct, sizeof product_desc);
165223cd1385SRemy Bohmer 	if (iSerialNumber) {
165323cd1385SRemy Bohmer 		device_desc.iSerialNumber = STRING_SERIALNUMBER,
16542e12abe6SVitaly Kuzmichev 		strlcpy(serial_number, iSerialNumber, sizeof serial_number);
165523cd1385SRemy Bohmer 	}
165623cd1385SRemy Bohmer 
165723cd1385SRemy Bohmer 	/* all we really need is bulk IN/OUT */
165823cd1385SRemy Bohmer 	usb_ep_autoconfig_reset(gadget);
165923cd1385SRemy Bohmer 	in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
166023cd1385SRemy Bohmer 	if (!in_ep) {
166123cd1385SRemy Bohmer autoconf_fail:
16627de73185SVitaly Kuzmichev 		error("can't autoconfigure on %s\n",
166323cd1385SRemy Bohmer 			gadget->name);
166423cd1385SRemy Bohmer 		return -ENODEV;
166523cd1385SRemy Bohmer 	}
166623cd1385SRemy Bohmer 	in_ep->driver_data = in_ep;	/* claim */
166723cd1385SRemy Bohmer 
166823cd1385SRemy Bohmer 	out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
166923cd1385SRemy Bohmer 	if (!out_ep)
167023cd1385SRemy Bohmer 		goto autoconf_fail;
167123cd1385SRemy Bohmer 	out_ep->driver_data = out_ep;	/* claim */
167223cd1385SRemy Bohmer 
167323cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
16746142e0aeSVitaly Kuzmichev 	/*
16756142e0aeSVitaly Kuzmichev 	 * CDC Ethernet control interface doesn't require a status endpoint.
167623cd1385SRemy Bohmer 	 * Since some hosts expect one, try to allocate one anyway.
167723cd1385SRemy Bohmer 	 */
167823cd1385SRemy Bohmer 	if (cdc) {
167923cd1385SRemy Bohmer 		status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
168023cd1385SRemy Bohmer 		if (status_ep) {
168123cd1385SRemy Bohmer 			status_ep->driver_data = status_ep;	/* claim */
168223cd1385SRemy Bohmer 		} else if (cdc) {
168323cd1385SRemy Bohmer 			control_intf.bNumEndpoints = 0;
168423cd1385SRemy Bohmer 			/* FIXME remove endpoint from descriptor list */
168523cd1385SRemy Bohmer 		}
168623cd1385SRemy Bohmer 	}
168723cd1385SRemy Bohmer #endif
168823cd1385SRemy Bohmer 
168923cd1385SRemy Bohmer 	/* one config:  cdc, else minimal subset */
169023cd1385SRemy Bohmer 	if (!cdc) {
169123cd1385SRemy Bohmer 		eth_config.bNumInterfaces = 1;
169223cd1385SRemy Bohmer 		eth_config.iConfiguration = STRING_SUBSET;
169323cd1385SRemy Bohmer 
16946142e0aeSVitaly Kuzmichev 		/*
16956142e0aeSVitaly Kuzmichev 		 * use functions to set these up, in case we're built to work
169623cd1385SRemy Bohmer 		 * with multiple controllers and must override CDC Ethernet.
169723cd1385SRemy Bohmer 		 */
169823cd1385SRemy Bohmer 		fs_subset_descriptors();
169923cd1385SRemy Bohmer 		hs_subset_descriptors();
170023cd1385SRemy Bohmer 	}
170123cd1385SRemy Bohmer 
170223cd1385SRemy Bohmer 	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
170323cd1385SRemy Bohmer 	usb_gadget_set_selfpowered(gadget);
170423cd1385SRemy Bohmer 
170523cd1385SRemy Bohmer 	if (gadget_is_dualspeed(gadget)) {
170623cd1385SRemy Bohmer 		if (!cdc)
170723cd1385SRemy Bohmer 			dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
170823cd1385SRemy Bohmer 
170923cd1385SRemy Bohmer 		/* assumes ep0 uses the same value for both speeds ... */
171023cd1385SRemy Bohmer 		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
171123cd1385SRemy Bohmer 
171223cd1385SRemy Bohmer 		/* and that all endpoints are dual-speed */
171323cd1385SRemy Bohmer 		hs_source_desc.bEndpointAddress =
171423cd1385SRemy Bohmer 				fs_source_desc.bEndpointAddress;
171523cd1385SRemy Bohmer 		hs_sink_desc.bEndpointAddress =
171623cd1385SRemy Bohmer 				fs_sink_desc.bEndpointAddress;
171723cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
171823cd1385SRemy Bohmer 		if (status_ep)
171923cd1385SRemy Bohmer 			hs_status_desc.bEndpointAddress =
172023cd1385SRemy Bohmer 					fs_status_desc.bEndpointAddress;
172123cd1385SRemy Bohmer #endif
172223cd1385SRemy Bohmer 	}
172323cd1385SRemy Bohmer 
172423cd1385SRemy Bohmer 	if (gadget_is_otg(gadget)) {
172523cd1385SRemy Bohmer 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
172623cd1385SRemy Bohmer 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
172723cd1385SRemy Bohmer 		eth_config.bMaxPower = 4;
172823cd1385SRemy Bohmer 	}
172923cd1385SRemy Bohmer 
173023cd1385SRemy Bohmer 	dev->net = &l_netdev;
173123cd1385SRemy Bohmer 
173223cd1385SRemy Bohmer 	dev->cdc = cdc;
173323cd1385SRemy Bohmer 	dev->zlp = zlp;
173423cd1385SRemy Bohmer 
173523cd1385SRemy Bohmer 	dev->in_ep = in_ep;
173623cd1385SRemy Bohmer 	dev->out_ep = out_ep;
173723cd1385SRemy Bohmer 	dev->status_ep = status_ep;
173823cd1385SRemy Bohmer 
17396142e0aeSVitaly Kuzmichev 	/*
17406142e0aeSVitaly Kuzmichev 	 * Module params for these addresses should come from ID proms.
174123cd1385SRemy Bohmer 	 * The host side address is used with CDC, and commonly
174223cd1385SRemy Bohmer 	 * ends up in a persistent config database.  It's not clear if
174323cd1385SRemy Bohmer 	 * host side code for the SAFE thing cares -- its original BLAN
174423cd1385SRemy Bohmer 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
174523cd1385SRemy Bohmer 	 */
174623cd1385SRemy Bohmer 	get_ether_addr(dev_addr, dev->net->enetaddr);
174723cd1385SRemy Bohmer 
174823cd1385SRemy Bohmer 	memset(tmp, 0, sizeof(tmp));
174923cd1385SRemy Bohmer 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
175023cd1385SRemy Bohmer 
175123cd1385SRemy Bohmer 	get_ether_addr(host_addr, dev->host_mac);
175223cd1385SRemy Bohmer 
175323cd1385SRemy Bohmer 	sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
175423cd1385SRemy Bohmer 		dev->host_mac[0], dev->host_mac[1],
175523cd1385SRemy Bohmer 			dev->host_mac[2], dev->host_mac[3],
175623cd1385SRemy Bohmer 			dev->host_mac[4], dev->host_mac[5]);
175723cd1385SRemy Bohmer 
17587de73185SVitaly Kuzmichev 	printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
175923cd1385SRemy Bohmer 		out_ep->name, in_ep->name,
176023cd1385SRemy Bohmer 		status_ep ? " STATUS " : "",
176123cd1385SRemy Bohmer 		status_ep ? status_ep->name : ""
176223cd1385SRemy Bohmer 		);
17637de73185SVitaly Kuzmichev 	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
176423cd1385SRemy Bohmer 		dev->net->enetaddr[0], dev->net->enetaddr[1],
176523cd1385SRemy Bohmer 		dev->net->enetaddr[2], dev->net->enetaddr[3],
176623cd1385SRemy Bohmer 		dev->net->enetaddr[4], dev->net->enetaddr[5]);
176723cd1385SRemy Bohmer 
176823cd1385SRemy Bohmer 	if (cdc) {
17697de73185SVitaly Kuzmichev 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
177023cd1385SRemy Bohmer 			dev->host_mac[0], dev->host_mac[1],
177123cd1385SRemy Bohmer 			dev->host_mac[2], dev->host_mac[3],
177223cd1385SRemy Bohmer 			dev->host_mac[4], dev->host_mac[5]);
177323cd1385SRemy Bohmer 	}
177423cd1385SRemy Bohmer 
17756142e0aeSVitaly Kuzmichev 	/*
17766142e0aeSVitaly Kuzmichev 	 * use PKTSIZE (or aligned... from u-boot) and set
17776142e0aeSVitaly Kuzmichev 	 * wMaxSegmentSize accordingly
17786142e0aeSVitaly Kuzmichev 	 */
177923cd1385SRemy Bohmer 	dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
178023cd1385SRemy Bohmer 
178123cd1385SRemy Bohmer 	/* preallocate control message data and buffer */
178223cd1385SRemy Bohmer 	dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
178323cd1385SRemy Bohmer 	if (!dev->req)
178423cd1385SRemy Bohmer 		goto fail;
178523cd1385SRemy Bohmer 	dev->req->buf = control_req;
178623cd1385SRemy Bohmer 	dev->req->complete = eth_setup_complete;
178723cd1385SRemy Bohmer 
178823cd1385SRemy Bohmer 	/* ... and maybe likewise for status transfer */
178923cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
179023cd1385SRemy Bohmer 	if (dev->status_ep) {
17916142e0aeSVitaly Kuzmichev 		dev->stat_req = usb_ep_alloc_request(dev->status_ep,
17926142e0aeSVitaly Kuzmichev 							GFP_KERNEL);
179323cd1385SRemy Bohmer 		if (!dev->stat_req) {
1794df559c1dSVitaly Kuzmichev 			usb_ep_free_request(dev->status_ep, dev->req);
179523cd1385SRemy Bohmer 
179623cd1385SRemy Bohmer 			goto fail;
179723cd1385SRemy Bohmer 		}
1798df559c1dSVitaly Kuzmichev 		dev->stat_req->buf = status_req;
179923cd1385SRemy Bohmer 		dev->stat_req->context = NULL;
180023cd1385SRemy Bohmer 	}
180123cd1385SRemy Bohmer #endif
180223cd1385SRemy Bohmer 
180323cd1385SRemy Bohmer 	/* finish hookup to lower layer ... */
180423cd1385SRemy Bohmer 	dev->gadget = gadget;
180523cd1385SRemy Bohmer 	set_gadget_data(gadget, dev);
180623cd1385SRemy Bohmer 	gadget->ep0->driver_data = dev;
180723cd1385SRemy Bohmer 
18086142e0aeSVitaly Kuzmichev 	/*
18096142e0aeSVitaly Kuzmichev 	 * two kinds of host-initiated state changes:
181023cd1385SRemy Bohmer 	 *  - iff DATA transfer is active, carrier is "on"
181123cd1385SRemy Bohmer 	 *  - tx queueing enabled if open *and* carrier is "on"
181223cd1385SRemy Bohmer 	 */
181323cd1385SRemy Bohmer 	return 0;
181423cd1385SRemy Bohmer 
181523cd1385SRemy Bohmer fail:
18167de73185SVitaly Kuzmichev 	error("%s failed", __func__);
181723cd1385SRemy Bohmer 	eth_unbind(gadget);
181823cd1385SRemy Bohmer 	return -ENOMEM;
181923cd1385SRemy Bohmer }
182023cd1385SRemy Bohmer 
182123cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
182223cd1385SRemy Bohmer {
182323cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
182423cd1385SRemy Bohmer 	struct usb_gadget *gadget;
182523cd1385SRemy Bohmer 	unsigned long ts;
182623cd1385SRemy Bohmer 	unsigned long timeout = USB_CONNECT_TIMEOUT;
182723cd1385SRemy Bohmer 
182823cd1385SRemy Bohmer 	if (!netdev) {
18297de73185SVitaly Kuzmichev 		error("received NULL ptr");
183023cd1385SRemy Bohmer 		goto fail;
183123cd1385SRemy Bohmer 	}
183258939fccSVitaly Kuzmichev 
183358939fccSVitaly Kuzmichev 	/* Configure default mac-addresses for the USB ethernet device */
183458939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR
183558939fccSVitaly Kuzmichev 	strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
183658939fccSVitaly Kuzmichev #endif
183758939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR
183858939fccSVitaly Kuzmichev 	strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
183958939fccSVitaly Kuzmichev #endif
184058939fccSVitaly Kuzmichev 	/* Check if the user overruled the MAC addresses */
184158939fccSVitaly Kuzmichev 	if (getenv("usbnet_devaddr"))
184258939fccSVitaly Kuzmichev 		strlcpy(dev_addr, getenv("usbnet_devaddr"),
184358939fccSVitaly Kuzmichev 			sizeof(dev_addr));
184458939fccSVitaly Kuzmichev 
184558939fccSVitaly Kuzmichev 	if (getenv("usbnet_hostaddr"))
184658939fccSVitaly Kuzmichev 		strlcpy(host_addr, getenv("usbnet_hostaddr"),
184758939fccSVitaly Kuzmichev 			sizeof(host_addr));
184858939fccSVitaly Kuzmichev 
184958939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(dev_addr)) {
185058939fccSVitaly Kuzmichev 		error("Need valid 'usbnet_devaddr' to be set");
185158939fccSVitaly Kuzmichev 		goto fail;
185258939fccSVitaly Kuzmichev 	}
185358939fccSVitaly Kuzmichev 	if (!is_eth_addr_valid(host_addr)) {
185458939fccSVitaly Kuzmichev 		error("Need valid 'usbnet_hostaddr' to be set");
185558939fccSVitaly Kuzmichev 		goto fail;
185658939fccSVitaly Kuzmichev 	}
185758939fccSVitaly Kuzmichev 
1858988ee3e3SLei Wen 	if (usb_gadget_register_driver(&eth_driver) < 0)
1859988ee3e3SLei Wen 		goto fail;
186023cd1385SRemy Bohmer 
186123cd1385SRemy Bohmer 	dev->network_started = 0;
186223cd1385SRemy Bohmer 
186323cd1385SRemy Bohmer 	packet_received = 0;
186423cd1385SRemy Bohmer 	packet_sent = 0;
186523cd1385SRemy Bohmer 
186623cd1385SRemy Bohmer 	gadget = dev->gadget;
186723cd1385SRemy Bohmer 	usb_gadget_connect(gadget);
186823cd1385SRemy Bohmer 
186923cd1385SRemy Bohmer 	if (getenv("cdc_connect_timeout"))
187023cd1385SRemy Bohmer 		timeout = simple_strtoul(getenv("cdc_connect_timeout"),
187123cd1385SRemy Bohmer 						NULL, 10) * CONFIG_SYS_HZ;
187223cd1385SRemy Bohmer 	ts = get_timer(0);
18736142e0aeSVitaly Kuzmichev 	while (!l_ethdev.network_started) {
187423cd1385SRemy Bohmer 		/* Handle control-c and timeouts */
187523cd1385SRemy Bohmer 		if (ctrlc() || (get_timer(ts) > timeout)) {
18767de73185SVitaly Kuzmichev 			error("The remote end did not respond in time.");
187723cd1385SRemy Bohmer 			goto fail;
187823cd1385SRemy Bohmer 		}
187923cd1385SRemy Bohmer 		usb_gadget_handle_interrupts();
188023cd1385SRemy Bohmer 	}
188123cd1385SRemy Bohmer 
188298fae970SVitaly Kuzmichev 	packet_received = 0;
188323cd1385SRemy Bohmer 	rx_submit(dev, dev->rx_req, 0);
188423cd1385SRemy Bohmer 	return 0;
188523cd1385SRemy Bohmer fail:
188623cd1385SRemy Bohmer 	return -1;
188723cd1385SRemy Bohmer }
188823cd1385SRemy Bohmer 
18896142e0aeSVitaly Kuzmichev static int usb_eth_send(struct eth_device *netdev,
18906142e0aeSVitaly Kuzmichev 			volatile void *packet, int length)
189123cd1385SRemy Bohmer {
189223cd1385SRemy Bohmer 	int			retval;
189323cd1385SRemy Bohmer 	struct eth_dev		*dev = &l_ethdev;
1894ac5d32d1SVitaly Kuzmichev 	struct usb_request	*req = dev->tx_req;
1895a170f2c7SStefano Babic 	unsigned long ts;
1896a170f2c7SStefano Babic 	unsigned long timeout = USB_CONNECT_TIMEOUT;
18977de73185SVitaly Kuzmichev 
18987de73185SVitaly Kuzmichev 	debug("%s:...\n", __func__);
189923cd1385SRemy Bohmer 
190023cd1385SRemy Bohmer 	req->buf = (void *)packet;
190123cd1385SRemy Bohmer 	req->context = NULL;
190223cd1385SRemy Bohmer 	req->complete = tx_complete;
190323cd1385SRemy Bohmer 
19046142e0aeSVitaly Kuzmichev 	/*
19056142e0aeSVitaly Kuzmichev 	 * use zlp framing on tx for strict CDC-Ether conformance,
190623cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
190723cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
190823cd1385SRemy Bohmer 	 */
190923cd1385SRemy Bohmer 	req->zero = 1;
191023cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
191123cd1385SRemy Bohmer 		length++;
191223cd1385SRemy Bohmer 
191323cd1385SRemy Bohmer 	req->length = length;
191423cd1385SRemy Bohmer #if 0
191523cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
191623cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
191723cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
191823cd1385SRemy Bohmer 			? ((dev->tx_qlen % qmult) != 0) : 0;
191923cd1385SRemy Bohmer #endif
192023cd1385SRemy Bohmer 	dev->tx_qlen = 1;
1921a170f2c7SStefano Babic 	ts = get_timer(0);
1922a170f2c7SStefano Babic 	packet_sent = 0;
192323cd1385SRemy Bohmer 
192423cd1385SRemy Bohmer 	retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
192523cd1385SRemy Bohmer 
192623cd1385SRemy Bohmer 	if (!retval)
19277de73185SVitaly Kuzmichev 		debug("%s: packet queued\n", __func__);
19286142e0aeSVitaly Kuzmichev 	while (!packet_sent) {
1929a170f2c7SStefano Babic 		if (get_timer(ts) > timeout) {
1930a170f2c7SStefano Babic 			printf("timeout sending packets to usb ethernet\n");
1931a170f2c7SStefano Babic 			return -1;
1932a170f2c7SStefano Babic 		}
1933a170f2c7SStefano Babic 		usb_gadget_handle_interrupts();
193423cd1385SRemy Bohmer 	}
193523cd1385SRemy Bohmer 
193623cd1385SRemy Bohmer 	return 0;
193723cd1385SRemy Bohmer }
193823cd1385SRemy Bohmer 
193923cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev)
194023cd1385SRemy Bohmer {
194123cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
194223cd1385SRemy Bohmer 
194323cd1385SRemy Bohmer 	usb_gadget_handle_interrupts();
194423cd1385SRemy Bohmer 
19456142e0aeSVitaly Kuzmichev 	if (packet_received) {
19467de73185SVitaly Kuzmichev 		debug("%s: packet received\n", __func__);
19476142e0aeSVitaly Kuzmichev 		if (dev->rx_req) {
194823cd1385SRemy Bohmer 			NetReceive(NetRxPackets[0], dev->rx_req->length);
194923cd1385SRemy Bohmer 			packet_received = 0;
195023cd1385SRemy Bohmer 
195123cd1385SRemy Bohmer 			rx_submit(dev, dev->rx_req, 0);
19526142e0aeSVitaly Kuzmichev 		} else
19536142e0aeSVitaly Kuzmichev 			error("dev->rx_req invalid");
195423cd1385SRemy Bohmer 	}
195523cd1385SRemy Bohmer 	return 0;
195623cd1385SRemy Bohmer }
195723cd1385SRemy Bohmer 
195823cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev)
195923cd1385SRemy Bohmer {
196023cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
196123cd1385SRemy Bohmer 
19626142e0aeSVitaly Kuzmichev 	if (!netdev) {
19637de73185SVitaly Kuzmichev 		error("received NULL ptr");
196423cd1385SRemy Bohmer 		return;
196523cd1385SRemy Bohmer 	}
196623cd1385SRemy Bohmer 
1967988ee3e3SLei Wen 	/* If the gadget not registered, simple return */
1968988ee3e3SLei Wen 	if (!dev->gadget)
1969988ee3e3SLei Wen 		return;
1970988ee3e3SLei Wen 
197123cd1385SRemy Bohmer 	usb_gadget_disconnect(dev->gadget);
1972b3649f3bSVitaly Kuzmichev 
1973b3649f3bSVitaly Kuzmichev 	/* Clear pending interrupt */
1974b3649f3bSVitaly Kuzmichev 	if (dev->network_started) {
1975b3649f3bSVitaly Kuzmichev 		usb_gadget_handle_interrupts();
1976b3649f3bSVitaly Kuzmichev 		dev->network_started = 0;
1977b3649f3bSVitaly Kuzmichev 	}
1978b3649f3bSVitaly Kuzmichev 
1979988ee3e3SLei Wen 	usb_gadget_unregister_driver(&eth_driver);
198023cd1385SRemy Bohmer }
198123cd1385SRemy Bohmer 
198223cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = {
198323cd1385SRemy Bohmer 	.speed		= DEVSPEED,
198423cd1385SRemy Bohmer 
198523cd1385SRemy Bohmer 	.bind		= eth_bind,
198623cd1385SRemy Bohmer 	.unbind		= eth_unbind,
198723cd1385SRemy Bohmer 
198823cd1385SRemy Bohmer 	.setup		= eth_setup,
198923cd1385SRemy Bohmer 	.disconnect	= eth_disconnect,
199023cd1385SRemy Bohmer 
199123cd1385SRemy Bohmer 	.suspend	= eth_suspend,
199223cd1385SRemy Bohmer 	.resume		= eth_resume,
199323cd1385SRemy Bohmer };
199423cd1385SRemy Bohmer 
199523cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi)
199623cd1385SRemy Bohmer {
199723cd1385SRemy Bohmer 	struct eth_device *netdev = &l_netdev;
199823cd1385SRemy Bohmer 
19998f7aa831SVitaly Kuzmichev 	strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
200023cd1385SRemy Bohmer 
200123cd1385SRemy Bohmer 	netdev->init = usb_eth_init;
200223cd1385SRemy Bohmer 	netdev->send = usb_eth_send;
200323cd1385SRemy Bohmer 	netdev->recv = usb_eth_recv;
200423cd1385SRemy Bohmer 	netdev->halt = usb_eth_halt;
200523cd1385SRemy Bohmer 
200623cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP
200723cd1385SRemy Bohmer   #error not supported
200823cd1385SRemy Bohmer #endif
200923cd1385SRemy Bohmer 	eth_register(netdev);
201023cd1385SRemy Bohmer 	return 0;
201123cd1385SRemy Bohmer }
2012