xref: /rk3399_rockchip-uboot/drivers/usb/gadget/ether.c (revision 2721dbf1ddeb32df07b64314da86d32a8157e331)
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>
2523cd1385SRemy Bohmer #include <linux/usb/ch9.h>
2623cd1385SRemy Bohmer #include <linux/usb/cdc.h>
2723cd1385SRemy Bohmer #include <linux/usb/gadget.h>
2823cd1385SRemy Bohmer #include <net.h>
2923cd1385SRemy Bohmer #include <linux/ctype.h>
3023cd1385SRemy Bohmer 
3123cd1385SRemy Bohmer #include "gadget_chips.h"
3223cd1385SRemy Bohmer 
3323cd1385SRemy Bohmer #define USB_NET_NAME "usb0"
3423cd1385SRemy Bohmer #define dprintf(x, ...)
3523cd1385SRemy Bohmer #undef INFO
3623cd1385SRemy Bohmer #define INFO(x, s...)	printf(s)
3723cd1385SRemy Bohmer #define dev_err(x, stuff...) printf(stuff)
3823cd1385SRemy Bohmer #define dev_dbg dev_err
3923cd1385SRemy Bohmer #define dev_warn dev_err
4023cd1385SRemy Bohmer #define DEBUG dev_err
4123cd1385SRemy Bohmer #define VDEBUG DEBUG
4223cd1385SRemy Bohmer #define atomic_read
4323cd1385SRemy Bohmer extern struct platform_data brd;
4423cd1385SRemy Bohmer #define spin_lock(x)
4523cd1385SRemy Bohmer #define spin_unlock(x)
4623cd1385SRemy Bohmer 
4723cd1385SRemy Bohmer 
4823cd1385SRemy Bohmer unsigned packet_received, packet_sent;
4923cd1385SRemy Bohmer 
5023cd1385SRemy Bohmer #define DEV_CONFIG_CDC	1
5123cd1385SRemy Bohmer #define GFP_ATOMIC ((gfp_t) 0)
5223cd1385SRemy Bohmer #define GFP_KERNEL ((gfp_t) 0)
5323cd1385SRemy Bohmer 
5423cd1385SRemy Bohmer /*
5523cd1385SRemy Bohmer  * Ethernet gadget driver -- with CDC and non-CDC options
5623cd1385SRemy Bohmer  * Builds on hardware support for a full duplex link.
5723cd1385SRemy Bohmer  *
5823cd1385SRemy Bohmer  * CDC Ethernet is the standard USB solution for sending Ethernet frames
5923cd1385SRemy Bohmer  * using USB.  Real hardware tends to use the same framing protocol but look
6023cd1385SRemy Bohmer  * different for control features.  This driver strongly prefers to use
6123cd1385SRemy Bohmer  * this USB-IF standard as its open-systems interoperability solution;
6223cd1385SRemy Bohmer  * most host side USB stacks (except from Microsoft) support it.
6323cd1385SRemy Bohmer  *
6423cd1385SRemy Bohmer  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
6523cd1385SRemy Bohmer  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
6623cd1385SRemy Bohmer  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
6723cd1385SRemy Bohmer  *
6823cd1385SRemy Bohmer  * There's some hardware that can't talk CDC ECM.  We make that hardware
6923cd1385SRemy Bohmer  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
7023cd1385SRemy Bohmer  * link-level setup only requires activating the configuration.  Only the
7123cd1385SRemy Bohmer  * endpoint descriptors, and product/vendor IDs, are relevant; no control
7223cd1385SRemy Bohmer  * operations are available.  Linux supports it, but other host operating
7323cd1385SRemy Bohmer  * systems may not.  (This is a subset of CDC Ethernet.)
7423cd1385SRemy Bohmer  *
7523cd1385SRemy Bohmer  * It turns out that if you add a few descriptors to that "CDC Subset",
7623cd1385SRemy Bohmer  * (Windows) host side drivers from MCCI can treat it as one submode of
7723cd1385SRemy Bohmer  * a proprietary scheme called "SAFE" ... without needing to know about
7823cd1385SRemy Bohmer  * specific product/vendor IDs.  So we do that, making it easier to use
7923cd1385SRemy Bohmer  * those MS-Windows drivers.  Those added descriptors make it resemble a
8023cd1385SRemy Bohmer  * CDC MDLM device, but they don't change device behavior at all.  (See
8123cd1385SRemy Bohmer  * MCCI Engineering report 950198 "SAFE Networking Functions".)
8223cd1385SRemy Bohmer  *
8323cd1385SRemy Bohmer  * A third option is also in use.  Rather than CDC Ethernet, or something
8423cd1385SRemy Bohmer  * simpler, Microsoft pushes their own approach: RNDIS.  The published
8523cd1385SRemy Bohmer  * RNDIS specs are ambiguous and appear to be incomplete, and are also
8623cd1385SRemy Bohmer  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
8723cd1385SRemy Bohmer  */
8823cd1385SRemy Bohmer #define ETH_ALEN	6		/* Octets in one ethernet addr	 */
8923cd1385SRemy Bohmer #define ETH_HLEN	14		/* Total octets in header.	 */
9023cd1385SRemy Bohmer #define ETH_ZLEN	60		/* Min. octets in frame sans FCS */
9123cd1385SRemy Bohmer #define ETH_DATA_LEN	1500		/* Max. octets in payload	 */
9223cd1385SRemy Bohmer #define ETH_FRAME_LEN	PKTSIZE_ALIGN	/* Max. octets in frame sans FCS */
9323cd1385SRemy Bohmer #define ETH_FCS_LEN	4		/* Octets in the FCS		 */
9423cd1385SRemy Bohmer 
9523cd1385SRemy Bohmer #define DRIVER_DESC		"Ethernet Gadget"
9623cd1385SRemy Bohmer /* Based on linux 2.6.27 version */
9723cd1385SRemy Bohmer #define DRIVER_VERSION		"May Day 2005"
9823cd1385SRemy Bohmer 
9923cd1385SRemy Bohmer static const char shortname [] = "ether";
10023cd1385SRemy Bohmer static const char driver_desc [] = DRIVER_DESC;
10123cd1385SRemy Bohmer 
10223cd1385SRemy Bohmer #define RX_EXTRA	20		/* guard against rx overflows */
10323cd1385SRemy Bohmer 
10423cd1385SRemy Bohmer /* CDC support the same host-chosen outgoing packet filters. */
10523cd1385SRemy Bohmer #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
10623cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
10723cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
10823cd1385SRemy Bohmer 			|USB_CDC_PACKET_TYPE_DIRECTED)
10923cd1385SRemy Bohmer 
11023cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
11123cd1385SRemy Bohmer 
11223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
11323cd1385SRemy Bohmer static struct eth_dev l_ethdev;
11423cd1385SRemy Bohmer static struct eth_device l_netdev;
11523cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver;
11623cd1385SRemy Bohmer 
11723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
11823cd1385SRemy Bohmer 
11923cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */
12023cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev)
12123cd1385SRemy Bohmer {
12223cd1385SRemy Bohmer #if	!defined(DEV_CONFIG_SUBSET)
12323cd1385SRemy Bohmer 	return 1;		/* only cdc possible */
12423cd1385SRemy Bohmer #elif	!defined (DEV_CONFIG_CDC)
12523cd1385SRemy Bohmer 	return 0;		/* only subset possible */
12623cd1385SRemy Bohmer #else
12723cd1385SRemy Bohmer 	return dev->cdc;	/* depends on what hardware we found */
12823cd1385SRemy Bohmer #endif
12923cd1385SRemy Bohmer }
13023cd1385SRemy Bohmer 
13123cd1385SRemy Bohmer #define	subset_active(dev)	(!is_cdc(dev))
13223cd1385SRemy Bohmer #define	cdc_active(dev)		( is_cdc(dev))
13323cd1385SRemy Bohmer 
13423cd1385SRemy Bohmer #define DEFAULT_QLEN	2	/* double buffering by default */
13523cd1385SRemy Bohmer 
13623cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */
13723cd1385SRemy Bohmer #define	HS_BPS		(13 * 512 * 8 * 1000 * 8)
13823cd1385SRemy Bohmer #define	FS_BPS		(19 *  64 * 1 * 1000 * 8)
13923cd1385SRemy Bohmer 
14023cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
14123cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_HIGH
14223cd1385SRemy Bohmer 
143*2721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT
144*2721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT
145*2721dbf1SVitaly Kuzmichev #else
146*2721dbf1SVitaly Kuzmichev #define qmult 5
147*2721dbf1SVitaly Kuzmichev #endif
148*2721dbf1SVitaly Kuzmichev 
14923cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */
15023cd1385SRemy Bohmer #define qlen(gadget) \
15123cd1385SRemy Bohmer 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
15223cd1385SRemy Bohmer 
15323cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
15423cd1385SRemy Bohmer {
15523cd1385SRemy Bohmer 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
15623cd1385SRemy Bohmer }
15723cd1385SRemy Bohmer 
15823cd1385SRemy Bohmer #else	/* full speed (low speed doesn't do bulk) */
15923cd1385SRemy Bohmer 
16023cd1385SRemy Bohmer #define qmult		1
16123cd1385SRemy Bohmer 
16223cd1385SRemy Bohmer #define	DEVSPEED	USB_SPEED_FULL
16323cd1385SRemy Bohmer 
16423cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN
16523cd1385SRemy Bohmer 
16623cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g)
16723cd1385SRemy Bohmer {
16823cd1385SRemy Bohmer 	return FS_BPS;
16923cd1385SRemy Bohmer }
17023cd1385SRemy Bohmer #endif
17123cd1385SRemy Bohmer 
17223cd1385SRemy Bohmer struct eth_dev {
17323cd1385SRemy Bohmer 	struct usb_gadget	*gadget;
17423cd1385SRemy Bohmer 	struct usb_request	*req;		/* for control responses */
17523cd1385SRemy Bohmer 	struct usb_request	*stat_req;	/* for cdc status */
17623cd1385SRemy Bohmer 
17723cd1385SRemy Bohmer 	u8			config;
17823cd1385SRemy Bohmer 	struct usb_ep		*in_ep, *out_ep, *status_ep;
17923cd1385SRemy Bohmer 	const struct usb_endpoint_descriptor
18023cd1385SRemy Bohmer 				*in, *out, *status;
18123cd1385SRemy Bohmer 
18223cd1385SRemy Bohmer 	struct usb_request	*tx_req, *rx_req;
18323cd1385SRemy Bohmer 
18423cd1385SRemy Bohmer 	struct eth_device	*net;
18523cd1385SRemy Bohmer 	unsigned int		tx_qlen;
18623cd1385SRemy Bohmer 
18723cd1385SRemy Bohmer 	unsigned		zlp:1;
18823cd1385SRemy Bohmer 	unsigned		cdc:1;
18923cd1385SRemy Bohmer 	unsigned		suspended:1;
19023cd1385SRemy Bohmer 	unsigned 		network_started:1;
19123cd1385SRemy Bohmer 	u16			cdc_filter;
19223cd1385SRemy Bohmer 	unsigned long		todo;
19323cd1385SRemy Bohmer 	int 			mtu;
19423cd1385SRemy Bohmer #define	WORK_RX_MEMORY		0
19523cd1385SRemy Bohmer 	u8			host_mac [ETH_ALEN];
19623cd1385SRemy Bohmer };
19723cd1385SRemy Bohmer 
19823cd1385SRemy Bohmer /* This version autoconfigures as much as possible at run-time.
19923cd1385SRemy Bohmer  *
20023cd1385SRemy Bohmer  * It also ASSUMES a self-powered device, without remote wakeup,
20123cd1385SRemy Bohmer  * although remote wakeup support would make sense.
20223cd1385SRemy Bohmer  */
20323cd1385SRemy Bohmer 
20423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
20523cd1385SRemy Bohmer 
20623cd1385SRemy Bohmer /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
20723cd1385SRemy Bohmer  * Instead:  allocate your own, using normal USB-IF procedures.
20823cd1385SRemy Bohmer  */
20923cd1385SRemy Bohmer 
21023cd1385SRemy Bohmer /* 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 
21623cd1385SRemy Bohmer /* For hardware that can't talk CDC, we use the same vendor ID that
21723cd1385SRemy Bohmer  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
21823cd1385SRemy Bohmer  * with pxa250.  We're protocol-compatible, if the host-side drivers
21923cd1385SRemy Bohmer  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
22023cd1385SRemy Bohmer  * drivers that need to hard-wire endpoint numbers have a hook.
22123cd1385SRemy Bohmer  *
22223cd1385SRemy Bohmer  * The protocol is a minimal subset of CDC Ether, which works on any bulk
22323cd1385SRemy Bohmer  * hardware that's not deeply broken ... even on hardware that can't talk
22423cd1385SRemy Bohmer  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
22523cd1385SRemy Bohmer  * doesn't handle control-OUT).
22623cd1385SRemy Bohmer  */
22723cd1385SRemy Bohmer #define	SIMPLE_VENDOR_NUM	0x049f
22823cd1385SRemy Bohmer #define	SIMPLE_PRODUCT_NUM	0x505a
22923cd1385SRemy Bohmer 
23023cd1385SRemy Bohmer /* Some systems will want different product identifers published in the
23123cd1385SRemy Bohmer  * device descriptor, either numbers or strings or both.  These string
23223cd1385SRemy Bohmer  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
23323cd1385SRemy Bohmer  */
23423cd1385SRemy Bohmer 
23523cd1385SRemy Bohmer static ushort bcdDevice;
23623cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER)
23723cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
23823cd1385SRemy Bohmer #else
23923cd1385SRemy Bohmer static char *iManufacturer = "U-boot";
24023cd1385SRemy Bohmer #endif
24123cd1385SRemy Bohmer static char *iProduct;
24223cd1385SRemy Bohmer static char *iSerialNumber;
24323cd1385SRemy Bohmer static char dev_addr[18];
24423cd1385SRemy Bohmer static char host_addr[18];
24523cd1385SRemy Bohmer 
24623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
24723cd1385SRemy Bohmer 
24823cd1385SRemy Bohmer /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
24923cd1385SRemy Bohmer  * ep0 implementation:  descriptors, config management, setup().
25023cd1385SRemy Bohmer  * also optional class-specific notification interrupt transfer.
25123cd1385SRemy Bohmer  */
25223cd1385SRemy Bohmer 
25323cd1385SRemy Bohmer /*
25423cd1385SRemy Bohmer  * DESCRIPTORS ... most are static, but strings and (full) configuration
25523cd1385SRemy Bohmer  * descriptors are built on demand.  For now we do either full CDC, or
25623cd1385SRemy Bohmer  * our simple subset.
25723cd1385SRemy Bohmer  */
25823cd1385SRemy Bohmer 
25923cd1385SRemy Bohmer #define STRING_MANUFACTURER		1
26023cd1385SRemy Bohmer #define STRING_PRODUCT			2
26123cd1385SRemy Bohmer #define STRING_ETHADDR			3
26223cd1385SRemy Bohmer #define STRING_DATA			4
26323cd1385SRemy Bohmer #define STRING_CONTROL			5
26423cd1385SRemy Bohmer #define STRING_CDC			7
26523cd1385SRemy Bohmer #define STRING_SUBSET			8
26623cd1385SRemy Bohmer #define STRING_SERIALNUMBER		10
26723cd1385SRemy Bohmer 
26823cd1385SRemy Bohmer /* holds our biggest descriptor */
26923cd1385SRemy Bohmer #define USB_BUFSIZ	256
27023cd1385SRemy Bohmer 
27123cd1385SRemy Bohmer /*
27223cd1385SRemy Bohmer  * This device advertises one configuration, eth_config,
27323cd1385SRemy Bohmer  * on hardware supporting at least two configs.
27423cd1385SRemy Bohmer  *
27523cd1385SRemy Bohmer  * FIXME define some higher-powered configurations to make it easier
27623cd1385SRemy Bohmer  * to recharge batteries ...
27723cd1385SRemy Bohmer  */
27823cd1385SRemy Bohmer 
27923cd1385SRemy Bohmer #define DEV_CONFIG_VALUE	1	/* cdc or subset */
28023cd1385SRemy Bohmer 
28123cd1385SRemy Bohmer static struct usb_device_descriptor
28223cd1385SRemy Bohmer device_desc = {
28323cd1385SRemy Bohmer 	.bLength =		sizeof device_desc,
28423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE,
28523cd1385SRemy Bohmer 
28623cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16 (0x0200),
28723cd1385SRemy Bohmer 
28823cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
28923cd1385SRemy Bohmer 	.bDeviceSubClass =	0,
29023cd1385SRemy Bohmer 	.bDeviceProtocol =	0,
29123cd1385SRemy Bohmer 
29223cd1385SRemy Bohmer 	.idVendor =		__constant_cpu_to_le16 (CDC_VENDOR_NUM),
29323cd1385SRemy Bohmer 	.idProduct =		__constant_cpu_to_le16 (CDC_PRODUCT_NUM),
29423cd1385SRemy Bohmer 	.iManufacturer =	STRING_MANUFACTURER,
29523cd1385SRemy Bohmer 	.iProduct =		STRING_PRODUCT,
29623cd1385SRemy Bohmer 	.bNumConfigurations =	1,
29723cd1385SRemy Bohmer };
29823cd1385SRemy Bohmer 
29923cd1385SRemy Bohmer static struct usb_otg_descriptor
30023cd1385SRemy Bohmer otg_descriptor = {
30123cd1385SRemy Bohmer 	.bLength =		sizeof otg_descriptor,
30223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_OTG,
30323cd1385SRemy Bohmer 
30423cd1385SRemy Bohmer 	.bmAttributes =		USB_OTG_SRP,
30523cd1385SRemy Bohmer };
30623cd1385SRemy Bohmer 
30723cd1385SRemy Bohmer static struct usb_config_descriptor
30823cd1385SRemy Bohmer eth_config = {
30923cd1385SRemy Bohmer 	.bLength =		sizeof eth_config,
31023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CONFIG,
31123cd1385SRemy Bohmer 
31223cd1385SRemy Bohmer 	/* compute wTotalLength on the fly */
31323cd1385SRemy Bohmer 	.bNumInterfaces =	2,
31423cd1385SRemy Bohmer 	.bConfigurationValue =	DEV_CONFIG_VALUE,
31523cd1385SRemy Bohmer 	.iConfiguration =	STRING_CDC,
31623cd1385SRemy Bohmer 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
31723cd1385SRemy Bohmer 	.bMaxPower =		1,
31823cd1385SRemy Bohmer };
31923cd1385SRemy Bohmer 
32023cd1385SRemy Bohmer /*
32123cd1385SRemy Bohmer  * Compared to the simple CDC subset, the full CDC Ethernet model adds
32223cd1385SRemy Bohmer  * three class descriptors, two interface descriptors, optional status
32323cd1385SRemy Bohmer  * endpoint.  Both have a "data" interface and two bulk endpoints.
32423cd1385SRemy Bohmer  * There are also differences in how control requests are handled.
32523cd1385SRemy Bohmer  */
32623cd1385SRemy Bohmer 
32723cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
32823cd1385SRemy Bohmer static struct usb_interface_descriptor
32923cd1385SRemy Bohmer control_intf = {
33023cd1385SRemy Bohmer 	.bLength =		sizeof control_intf,
33123cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
33223cd1385SRemy Bohmer 
33323cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
33423cd1385SRemy Bohmer 	/* status endpoint is optional; this may be patched later */
33523cd1385SRemy Bohmer 	.bNumEndpoints =	1,
33623cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_COMM,
33723cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
33823cd1385SRemy Bohmer 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
33923cd1385SRemy Bohmer 	.iInterface =		STRING_CONTROL,
34023cd1385SRemy Bohmer };
34123cd1385SRemy Bohmer #endif
34223cd1385SRemy Bohmer 
34323cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = {
34423cd1385SRemy Bohmer 	.bLength =		sizeof header_desc,
34523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
34623cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
34723cd1385SRemy Bohmer 
34823cd1385SRemy Bohmer 	.bcdCDC =		__constant_cpu_to_le16 (0x0110),
34923cd1385SRemy Bohmer };
35023cd1385SRemy Bohmer 
35123cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
35223cd1385SRemy Bohmer 
35323cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = {
35423cd1385SRemy Bohmer 	.bLength =		sizeof union_desc,
35523cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
35623cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
35723cd1385SRemy Bohmer 
35823cd1385SRemy Bohmer 	.bMasterInterface0 =	0,	/* index of control interface */
35923cd1385SRemy Bohmer 	.bSlaveInterface0 =	1,	/* index of DATA interface */
36023cd1385SRemy Bohmer };
36123cd1385SRemy Bohmer 
36223cd1385SRemy Bohmer #endif	/* CDC */
36323cd1385SRemy Bohmer 
36423cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC
36523cd1385SRemy Bohmer 
36623cd1385SRemy Bohmer /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
36723cd1385SRemy Bohmer  * ways:  data endpoints live in the control interface, there's no data
36823cd1385SRemy Bohmer  * interface, and it's not used to talk to a cell phone radio.
36923cd1385SRemy Bohmer  */
37023cd1385SRemy Bohmer 
37123cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = {
37223cd1385SRemy Bohmer 	.bLength =		sizeof mdlm_desc,
37323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
37423cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_MDLM_TYPE,
37523cd1385SRemy Bohmer 
37623cd1385SRemy Bohmer 	.bcdVersion =		__constant_cpu_to_le16(0x0100),
37723cd1385SRemy Bohmer 	.bGUID = {
37823cd1385SRemy Bohmer 		0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
37923cd1385SRemy Bohmer 		0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
38023cd1385SRemy Bohmer 	},
38123cd1385SRemy Bohmer };
38223cd1385SRemy Bohmer 
38323cd1385SRemy Bohmer /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
38423cd1385SRemy Bohmer  * can't really use its struct.  All we do here is say that we're using
38523cd1385SRemy Bohmer  * the submode of "SAFE" which directly matches the CDC Subset.
38623cd1385SRemy Bohmer  */
38723cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = {
38823cd1385SRemy Bohmer 	6,
38923cd1385SRemy Bohmer 	USB_DT_CS_INTERFACE,
39023cd1385SRemy Bohmer 	USB_CDC_MDLM_DETAIL_TYPE,
39123cd1385SRemy Bohmer 
39223cd1385SRemy Bohmer 	0,	/* "SAFE" */
39323cd1385SRemy Bohmer 	0,	/* network control capabilities (none) */
39423cd1385SRemy Bohmer 	0,	/* network data capabilities ("raw" encapsulation) */
39523cd1385SRemy Bohmer };
39623cd1385SRemy Bohmer 
39723cd1385SRemy Bohmer #endif
39823cd1385SRemy Bohmer 
39923cd1385SRemy Bohmer 
40023cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = {
40123cd1385SRemy Bohmer 	.bLength =		sizeof (ether_desc),
40223cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_CS_INTERFACE,
40323cd1385SRemy Bohmer 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
40423cd1385SRemy Bohmer 
40523cd1385SRemy Bohmer 	/* this descriptor actually adds value, surprise! */
40623cd1385SRemy Bohmer 	.iMACAddress =		STRING_ETHADDR,
40723cd1385SRemy Bohmer 	.bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
40823cd1385SRemy Bohmer 	.wMaxSegmentSize =	__constant_cpu_to_le16 (ETH_FRAME_LEN),
40923cd1385SRemy Bohmer 	.wNumberMCFilters =	__constant_cpu_to_le16 (0),
41023cd1385SRemy Bohmer 	.bNumberPowerFilters =	0,
41123cd1385SRemy Bohmer };
41223cd1385SRemy Bohmer 
41323cd1385SRemy Bohmer 
41423cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
41523cd1385SRemy Bohmer 
41623cd1385SRemy Bohmer /* include the status endpoint if we can, even where it's optional.
41723cd1385SRemy Bohmer  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
41823cd1385SRemy Bohmer  * packet, to simplify cancellation; and a big transfer interval, to
41923cd1385SRemy Bohmer  * waste less bandwidth.
42023cd1385SRemy Bohmer  *
42123cd1385SRemy Bohmer  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
42223cd1385SRemy Bohmer  * if they ignore the connect/disconnect notifications that real aether
42323cd1385SRemy Bohmer  * can provide.  more advanced cdc configurations might want to support
42423cd1385SRemy Bohmer  * encapsulated commands (vendor-specific, using control-OUT).
42523cd1385SRemy Bohmer  */
42623cd1385SRemy Bohmer 
42723cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
42823cd1385SRemy Bohmer #define STATUS_BYTECOUNT		16	/* 8 byte header + data */
42923cd1385SRemy Bohmer 
43023cd1385SRemy Bohmer static struct usb_endpoint_descriptor
43123cd1385SRemy Bohmer fs_status_desc = {
43223cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
43323cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
43423cd1385SRemy Bohmer 
43523cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
43623cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
43723cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16 (STATUS_BYTECOUNT),
43823cd1385SRemy Bohmer 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
43923cd1385SRemy Bohmer };
44023cd1385SRemy Bohmer #endif
44123cd1385SRemy Bohmer 
44223cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
44323cd1385SRemy Bohmer 
44423cd1385SRemy Bohmer /* the default data interface has no endpoints ... */
44523cd1385SRemy Bohmer 
44623cd1385SRemy Bohmer static const struct usb_interface_descriptor
44723cd1385SRemy Bohmer data_nop_intf = {
44823cd1385SRemy Bohmer 	.bLength =		sizeof data_nop_intf,
44923cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
45023cd1385SRemy Bohmer 
45123cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
45223cd1385SRemy Bohmer 	.bAlternateSetting =	0,
45323cd1385SRemy Bohmer 	.bNumEndpoints =	0,
45423cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
45523cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
45623cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
45723cd1385SRemy Bohmer };
45823cd1385SRemy Bohmer 
45923cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */
46023cd1385SRemy Bohmer 
46123cd1385SRemy Bohmer static const struct usb_interface_descriptor
46223cd1385SRemy Bohmer data_intf = {
46323cd1385SRemy Bohmer 	.bLength =		sizeof data_intf,
46423cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
46523cd1385SRemy Bohmer 
46623cd1385SRemy Bohmer 	.bInterfaceNumber =	1,
46723cd1385SRemy Bohmer 	.bAlternateSetting =	1,
46823cd1385SRemy Bohmer 	.bNumEndpoints =	2,
46923cd1385SRemy Bohmer 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
47023cd1385SRemy Bohmer 	.bInterfaceSubClass =	0,
47123cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
47223cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
47323cd1385SRemy Bohmer };
47423cd1385SRemy Bohmer 
47523cd1385SRemy Bohmer #endif
47623cd1385SRemy Bohmer 
47723cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
47823cd1385SRemy Bohmer 
47923cd1385SRemy Bohmer /*
48023cd1385SRemy Bohmer  * "Simple" CDC-subset option is a simple vendor-neutral model that most
48123cd1385SRemy Bohmer  * full speed controllers can handle:  one interface, two bulk endpoints.
48223cd1385SRemy Bohmer  *
48323cd1385SRemy Bohmer  * To assist host side drivers, we fancy it up a bit, and add descriptors
48423cd1385SRemy Bohmer  * so some host side drivers will understand it as a "SAFE" variant.
48523cd1385SRemy Bohmer  */
48623cd1385SRemy Bohmer 
48723cd1385SRemy Bohmer static const struct usb_interface_descriptor
48823cd1385SRemy Bohmer subset_data_intf = {
48923cd1385SRemy Bohmer 	.bLength =		sizeof subset_data_intf,
49023cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_INTERFACE,
49123cd1385SRemy Bohmer 
49223cd1385SRemy Bohmer 	.bInterfaceNumber =	0,
49323cd1385SRemy Bohmer 	.bAlternateSetting =	0,
49423cd1385SRemy Bohmer 	.bNumEndpoints =	2,
49523cd1385SRemy Bohmer 	.bInterfaceClass =      USB_CLASS_COMM,
49623cd1385SRemy Bohmer 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_MDLM,
49723cd1385SRemy Bohmer 	.bInterfaceProtocol =	0,
49823cd1385SRemy Bohmer 	.iInterface =		STRING_DATA,
49923cd1385SRemy Bohmer };
50023cd1385SRemy Bohmer 
50123cd1385SRemy Bohmer #endif	/* SUBSET */
50223cd1385SRemy Bohmer 
50323cd1385SRemy Bohmer 
50423cd1385SRemy Bohmer static struct usb_endpoint_descriptor
50523cd1385SRemy Bohmer fs_source_desc = {
50623cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
50723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
50823cd1385SRemy Bohmer 
50923cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_IN,
51023cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
51123cd1385SRemy Bohmer };
51223cd1385SRemy Bohmer 
51323cd1385SRemy Bohmer static struct usb_endpoint_descriptor
51423cd1385SRemy Bohmer fs_sink_desc = {
51523cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
51623cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
51723cd1385SRemy Bohmer 
51823cd1385SRemy Bohmer 	.bEndpointAddress =	USB_DIR_OUT,
51923cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
52023cd1385SRemy Bohmer };
52123cd1385SRemy Bohmer 
52223cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function [11] = {
52323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
52423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
52523cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
52623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
52723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
52823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
52923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
53023cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
53123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_status_desc,
53223cd1385SRemy Bohmer 	/* data interface, with altsetting */
53323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
53423cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
53523cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_source_desc,
53623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &fs_sink_desc,
53723cd1385SRemy Bohmer 	NULL,
53823cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
53923cd1385SRemy Bohmer };
54023cd1385SRemy Bohmer 
54123cd1385SRemy Bohmer static inline void fs_subset_descriptors(void)
54223cd1385SRemy Bohmer {
54323cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
54423cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
54523cd1385SRemy Bohmer 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
54623cd1385SRemy Bohmer 	fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
54723cd1385SRemy Bohmer 	fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
54823cd1385SRemy Bohmer 	fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
54923cd1385SRemy Bohmer 	fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
55023cd1385SRemy Bohmer 	fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
55123cd1385SRemy Bohmer 	fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
55223cd1385SRemy Bohmer 	fs_eth_function[8] = NULL;
55323cd1385SRemy Bohmer #else
55423cd1385SRemy Bohmer 	fs_eth_function[1] = NULL;
55523cd1385SRemy Bohmer #endif
55623cd1385SRemy Bohmer }
55723cd1385SRemy Bohmer 
55823cd1385SRemy Bohmer /*
55923cd1385SRemy Bohmer  * usb 2.0 devices need to expose both high speed and full speed
56023cd1385SRemy Bohmer  * descriptors, unless they only run at full speed.
56123cd1385SRemy Bohmer  */
56223cd1385SRemy Bohmer 
56323cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
56423cd1385SRemy Bohmer static struct usb_endpoint_descriptor
56523cd1385SRemy Bohmer hs_status_desc = {
56623cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
56723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
56823cd1385SRemy Bohmer 
56923cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
57023cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16 (STATUS_BYTECOUNT),
57123cd1385SRemy Bohmer 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
57223cd1385SRemy Bohmer };
57323cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
57423cd1385SRemy Bohmer 
57523cd1385SRemy Bohmer static struct usb_endpoint_descriptor
57623cd1385SRemy Bohmer hs_source_desc = {
57723cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
57823cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
57923cd1385SRemy Bohmer 
58023cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
58123cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16 (512),
58223cd1385SRemy Bohmer };
58323cd1385SRemy Bohmer 
58423cd1385SRemy Bohmer static struct usb_endpoint_descriptor
58523cd1385SRemy Bohmer hs_sink_desc = {
58623cd1385SRemy Bohmer 	.bLength =		USB_DT_ENDPOINT_SIZE,
58723cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_ENDPOINT,
58823cd1385SRemy Bohmer 
58923cd1385SRemy Bohmer 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
59023cd1385SRemy Bohmer 	.wMaxPacketSize =	__constant_cpu_to_le16 (512),
59123cd1385SRemy Bohmer };
59223cd1385SRemy Bohmer 
59323cd1385SRemy Bohmer static struct usb_qualifier_descriptor
59423cd1385SRemy Bohmer dev_qualifier = {
59523cd1385SRemy Bohmer 	.bLength =		sizeof dev_qualifier,
59623cd1385SRemy Bohmer 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
59723cd1385SRemy Bohmer 
59823cd1385SRemy Bohmer 	.bcdUSB =		__constant_cpu_to_le16 (0x0200),
59923cd1385SRemy Bohmer 	.bDeviceClass =		USB_CLASS_COMM,
60023cd1385SRemy Bohmer 
60123cd1385SRemy Bohmer 	.bNumConfigurations =	1,
60223cd1385SRemy Bohmer };
60323cd1385SRemy Bohmer 
60423cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function [11] = {
60523cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &otg_descriptor,
60623cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
60723cd1385SRemy Bohmer 	/* "cdc" mode descriptors */
60823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &control_intf,
60923cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &header_desc,
61023cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &union_desc,
61123cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &ether_desc,
61223cd1385SRemy Bohmer 	/* NOTE: status endpoint may need to be removed */
61323cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_status_desc,
61423cd1385SRemy Bohmer 	/* data interface, with altsetting */
61523cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_nop_intf,
61623cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &data_intf,
61723cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_source_desc,
61823cd1385SRemy Bohmer 	(struct usb_descriptor_header *) &hs_sink_desc,
61923cd1385SRemy Bohmer 	NULL,
62023cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
62123cd1385SRemy Bohmer };
62223cd1385SRemy Bohmer 
62323cd1385SRemy Bohmer static inline void hs_subset_descriptors(void)
62423cd1385SRemy Bohmer {
62523cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET
62623cd1385SRemy Bohmer 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
62723cd1385SRemy Bohmer 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
62823cd1385SRemy Bohmer 	hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
62923cd1385SRemy Bohmer 	hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
63023cd1385SRemy Bohmer 	hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
63123cd1385SRemy Bohmer 	hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
63223cd1385SRemy Bohmer 	hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
63323cd1385SRemy Bohmer 	hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
63423cd1385SRemy Bohmer 	hs_eth_function[8] = NULL;
63523cd1385SRemy Bohmer #else
63623cd1385SRemy Bohmer 	hs_eth_function[1] = NULL;
63723cd1385SRemy Bohmer #endif
63823cd1385SRemy Bohmer }
63923cd1385SRemy Bohmer 
64023cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */
64123cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor *
64223cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
64323cd1385SRemy Bohmer 		struct usb_endpoint_descriptor *fs)
64423cd1385SRemy Bohmer {
64523cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
64623cd1385SRemy Bohmer 		return hs;
64723cd1385SRemy Bohmer 	return fs;
64823cd1385SRemy Bohmer }
64923cd1385SRemy Bohmer 
65023cd1385SRemy Bohmer 
65123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
65223cd1385SRemy Bohmer 
65323cd1385SRemy Bohmer /* descriptors that are built on-demand */
65423cd1385SRemy Bohmer 
65523cd1385SRemy Bohmer static char manufacturer [50];
65623cd1385SRemy Bohmer static char product_desc [40] = DRIVER_DESC;
65723cd1385SRemy Bohmer static char serial_number [20];
65823cd1385SRemy Bohmer 
65923cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */
66023cd1385SRemy Bohmer static char ethaddr [2 * ETH_ALEN + 1];
66123cd1385SRemy Bohmer 
66223cd1385SRemy Bohmer /* static strings, in UTF-8 */
66323cd1385SRemy Bohmer static struct usb_string		strings [] = {
66423cd1385SRemy Bohmer 	{ STRING_MANUFACTURER,	manufacturer, },
66523cd1385SRemy Bohmer 	{ STRING_PRODUCT,	product_desc, },
66623cd1385SRemy Bohmer 	{ STRING_SERIALNUMBER,	serial_number, },
66723cd1385SRemy Bohmer 	{ STRING_DATA,		"Ethernet Data", },
66823cd1385SRemy Bohmer 	{ STRING_ETHADDR,	ethaddr, },
66923cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
67023cd1385SRemy Bohmer 	{ STRING_CDC,		"CDC Ethernet", },
67123cd1385SRemy Bohmer 	{ STRING_CONTROL,	"CDC Communications Control", },
67223cd1385SRemy Bohmer #endif
67323cd1385SRemy Bohmer #ifdef	DEV_CONFIG_SUBSET
67423cd1385SRemy Bohmer 	{ STRING_SUBSET,	"CDC Ethernet Subset", },
67523cd1385SRemy Bohmer #endif
67623cd1385SRemy Bohmer 	{  }		/* end of list */
67723cd1385SRemy Bohmer };
67823cd1385SRemy Bohmer 
67923cd1385SRemy Bohmer static struct usb_gadget_strings	stringtab = {
68023cd1385SRemy Bohmer 	.language	= 0x0409,	/* en-us */
68123cd1385SRemy Bohmer 	.strings	= strings,
68223cd1385SRemy Bohmer };
68323cd1385SRemy Bohmer 
68423cd1385SRemy Bohmer 
68523cd1385SRemy Bohmer /*============================================================================*/
68623cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ];
68723cd1385SRemy Bohmer static u8 status_req[STATUS_BYTECOUNT];
68823cd1385SRemy Bohmer 
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 /*
71823cd1385SRemy Bohmer  * one config, two interfaces:  control, data.
71923cd1385SRemy Bohmer  * complications: class descriptors, and an altsetting.
72023cd1385SRemy Bohmer  */
72123cd1385SRemy Bohmer static int
72223cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
72323cd1385SRemy Bohmer {
72423cd1385SRemy Bohmer 	int					len;
72523cd1385SRemy Bohmer 	const struct usb_config_descriptor	*config;
72623cd1385SRemy Bohmer 	const struct usb_descriptor_header	**function;
72723cd1385SRemy Bohmer 	int					hs = 0;
72823cd1385SRemy Bohmer 
72923cd1385SRemy Bohmer 	if (gadget_is_dualspeed(g)) {
73023cd1385SRemy Bohmer 		hs = (g->speed == USB_SPEED_HIGH);
73123cd1385SRemy Bohmer 		if (type == USB_DT_OTHER_SPEED_CONFIG)
73223cd1385SRemy Bohmer 			hs = !hs;
73323cd1385SRemy Bohmer 	}
73423cd1385SRemy Bohmer #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function)
73523cd1385SRemy Bohmer 
73623cd1385SRemy Bohmer 	if (index >= device_desc.bNumConfigurations)
73723cd1385SRemy Bohmer 		return -EINVAL;
73823cd1385SRemy Bohmer 
73923cd1385SRemy Bohmer 	config = &eth_config;
74023cd1385SRemy Bohmer 	function = which_fn (eth);
74123cd1385SRemy Bohmer 
74223cd1385SRemy Bohmer 	/* for now, don't advertise srp-only devices */
74323cd1385SRemy Bohmer 	if (!is_otg)
74423cd1385SRemy Bohmer 		function++;
74523cd1385SRemy Bohmer 
74623cd1385SRemy Bohmer 	len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
74723cd1385SRemy Bohmer 	if (len < 0)
74823cd1385SRemy Bohmer 		return len;
74923cd1385SRemy Bohmer 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
75023cd1385SRemy Bohmer 	return len;
75123cd1385SRemy Bohmer }
75223cd1385SRemy Bohmer 
75323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
75423cd1385SRemy Bohmer 
75523cd1385SRemy Bohmer static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
75623cd1385SRemy Bohmer 
75723cd1385SRemy Bohmer static int
75823cd1385SRemy Bohmer set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
75923cd1385SRemy Bohmer {
76023cd1385SRemy Bohmer 	int					result = 0;
76123cd1385SRemy Bohmer 	struct usb_gadget			*gadget = dev->gadget;
76223cd1385SRemy Bohmer 
76323cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
76423cd1385SRemy Bohmer 	/* status endpoint used for (optionally) CDC */
76523cd1385SRemy Bohmer 	if (!subset_active(dev) && dev->status_ep) {
76623cd1385SRemy Bohmer 		dev->status = ep_desc (gadget, &hs_status_desc,
76723cd1385SRemy Bohmer 						&fs_status_desc);
76823cd1385SRemy Bohmer 		dev->status_ep->driver_data = dev;
76923cd1385SRemy Bohmer 
77023cd1385SRemy Bohmer 		result = usb_ep_enable (dev->status_ep, dev->status);
77123cd1385SRemy Bohmer 		if (result != 0) {
77223cd1385SRemy Bohmer 			printf ("enable %s --> %d\n",
77323cd1385SRemy Bohmer 				dev->status_ep->name, result);
77423cd1385SRemy Bohmer 			goto done;
77523cd1385SRemy Bohmer 		}
77623cd1385SRemy Bohmer 	}
77723cd1385SRemy Bohmer #endif
77823cd1385SRemy Bohmer 
77923cd1385SRemy Bohmer 	dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
78023cd1385SRemy Bohmer 	dev->in_ep->driver_data = dev;
78123cd1385SRemy Bohmer 
78223cd1385SRemy Bohmer 	dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
78323cd1385SRemy Bohmer 	dev->out_ep->driver_data = dev;
78423cd1385SRemy Bohmer 
78523cd1385SRemy Bohmer 	/* 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) {
79223cd1385SRemy Bohmer 			printf ("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) {
79923cd1385SRemy Bohmer 			printf ("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) {
81123cd1385SRemy Bohmer 		if (!subset_active(dev))
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 
82523cd1385SRemy Bohmer static void eth_reset_config (struct eth_dev *dev)
82623cd1385SRemy Bohmer {
82723cd1385SRemy Bohmer 	if (dev->config == 0)
82823cd1385SRemy Bohmer 		return;
82923cd1385SRemy Bohmer 
83023cd1385SRemy Bohmer 	/* disable endpoints, forcing (synchronous) completion of
83123cd1385SRemy Bohmer 	 * pending i/o.  then free the requests.
83223cd1385SRemy Bohmer 	 */
83323cd1385SRemy Bohmer 
83423cd1385SRemy Bohmer 	if (dev->in) {
83523cd1385SRemy Bohmer 		usb_ep_disable (dev->in_ep);
83623cd1385SRemy Bohmer 		if (dev->tx_req) {
83723cd1385SRemy Bohmer 			usb_ep_free_request (dev->in_ep, dev->tx_req);
83823cd1385SRemy Bohmer 			dev->tx_req=NULL;
83923cd1385SRemy Bohmer 		}
84023cd1385SRemy Bohmer 	}
84123cd1385SRemy Bohmer 	if (dev->out) {
84223cd1385SRemy Bohmer 		usb_ep_disable (dev->out_ep);
84323cd1385SRemy Bohmer 		if (dev->rx_req) {
84423cd1385SRemy Bohmer 			usb_ep_free_request (dev->in_ep, dev->rx_req);
84523cd1385SRemy Bohmer 			dev->rx_req=NULL;
84623cd1385SRemy Bohmer 		}
84723cd1385SRemy Bohmer 	}
84823cd1385SRemy Bohmer 	if (dev->status) {
84923cd1385SRemy Bohmer 		usb_ep_disable (dev->status_ep);
85023cd1385SRemy Bohmer 	}
85123cd1385SRemy Bohmer 	dev->cdc_filter = 0;
85223cd1385SRemy Bohmer 	dev->config = 0;
85323cd1385SRemy Bohmer }
85423cd1385SRemy Bohmer 
85523cd1385SRemy Bohmer /* change our operational config.  must agree with the code
85623cd1385SRemy Bohmer  * that returns config descriptors, and altsetting code.
85723cd1385SRemy Bohmer  */
85823cd1385SRemy Bohmer static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
85923cd1385SRemy Bohmer {
86023cd1385SRemy Bohmer 	int			result = 0;
86123cd1385SRemy Bohmer 	struct usb_gadget	*gadget = dev->gadget;
86223cd1385SRemy Bohmer 
86323cd1385SRemy Bohmer 	if (gadget_is_sa1100 (gadget)
86423cd1385SRemy Bohmer 			&& dev->config
86523cd1385SRemy Bohmer 			&& dev->tx_qlen != 0) {
86623cd1385SRemy Bohmer 		/* tx fifo is full, but we can't clear it...*/
86723cd1385SRemy Bohmer 		INFO (dev, "can't change configurations\n");
86823cd1385SRemy Bohmer 		return -ESPIPE;
86923cd1385SRemy Bohmer 	}
87023cd1385SRemy Bohmer 	eth_reset_config (dev);
87123cd1385SRemy Bohmer 
87223cd1385SRemy Bohmer 	switch (number) {
87323cd1385SRemy Bohmer 	case DEV_CONFIG_VALUE:
87423cd1385SRemy Bohmer 		result = set_ether_config (dev, gfp_flags);
87523cd1385SRemy Bohmer 		break;
87623cd1385SRemy Bohmer 	default:
87723cd1385SRemy Bohmer 		result = -EINVAL;
87823cd1385SRemy Bohmer 		/* FALL THROUGH */
87923cd1385SRemy Bohmer 	case 0:
88023cd1385SRemy Bohmer 		break;
88123cd1385SRemy Bohmer 	}
88223cd1385SRemy Bohmer 
88323cd1385SRemy Bohmer 	if (result) {
88423cd1385SRemy Bohmer 		if (number)
88523cd1385SRemy Bohmer 			eth_reset_config (dev);
88623cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget,
88723cd1385SRemy Bohmer 				gadget_is_otg(dev->gadget) ? 8 : 100);
88823cd1385SRemy Bohmer 	} else {
88923cd1385SRemy Bohmer 		char *speed;
89023cd1385SRemy Bohmer 		unsigned power;
89123cd1385SRemy Bohmer 
89223cd1385SRemy Bohmer 		power = 2 * eth_config.bMaxPower;
89323cd1385SRemy Bohmer 		usb_gadget_vbus_draw(dev->gadget, power);
89423cd1385SRemy Bohmer 
89523cd1385SRemy Bohmer 		switch (gadget->speed) {
89623cd1385SRemy Bohmer 		case USB_SPEED_FULL:	speed = "full"; break;
89723cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
89823cd1385SRemy Bohmer 		case USB_SPEED_HIGH:	speed = "high"; break;
89923cd1385SRemy Bohmer #endif
90023cd1385SRemy Bohmer 		default:		speed = "?"; break;
90123cd1385SRemy Bohmer 		}
90223cd1385SRemy Bohmer 
90323cd1385SRemy Bohmer 		dev->config = number;
90423cd1385SRemy Bohmer 		INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
90523cd1385SRemy Bohmer 				speed, number, power, driver_desc,
90623cd1385SRemy Bohmer 				(cdc_active(dev)? "CDC Ethernet"
90723cd1385SRemy Bohmer 						: "CDC Ethernet Subset"));
90823cd1385SRemy Bohmer 	}
90923cd1385SRemy Bohmer 	return result;
91023cd1385SRemy Bohmer }
91123cd1385SRemy Bohmer 
91223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
91323cd1385SRemy Bohmer 
91423cd1385SRemy Bohmer #ifdef	DEV_CONFIG_CDC
91523cd1385SRemy Bohmer 
91623cd1385SRemy Bohmer /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
91723cd1385SRemy Bohmer  * only to notify the host about link status changes (which we support) or
91823cd1385SRemy Bohmer  * report completion of some encapsulated command.  Since
91923cd1385SRemy Bohmer  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
92023cd1385SRemy Bohmer  * command mechanism; and only one status request is ever queued.
92123cd1385SRemy Bohmer  */
92223cd1385SRemy Bohmer static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
92323cd1385SRemy Bohmer {
92423cd1385SRemy Bohmer 	struct usb_cdc_notification	*event = req->buf;
92523cd1385SRemy Bohmer 	int				value = req->status;
92623cd1385SRemy Bohmer 	struct eth_dev			*dev = ep->driver_data;
92723cd1385SRemy Bohmer 
92823cd1385SRemy Bohmer 	/* issue the second notification if host reads the first */
92923cd1385SRemy Bohmer 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
93023cd1385SRemy Bohmer 			&& value == 0) {
93123cd1385SRemy Bohmer 		__le32	*data = req->buf + sizeof *event;
93223cd1385SRemy Bohmer 
93323cd1385SRemy Bohmer 		event->bmRequestType = 0xA1;
93423cd1385SRemy Bohmer 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
93523cd1385SRemy Bohmer 		event->wValue = __constant_cpu_to_le16 (0);
93623cd1385SRemy Bohmer 		event->wIndex = __constant_cpu_to_le16 (1);
93723cd1385SRemy Bohmer 		event->wLength = __constant_cpu_to_le16 (8);
93823cd1385SRemy Bohmer 
93923cd1385SRemy Bohmer 		/* SPEED_CHANGE data is up/down speeds in bits/sec */
94023cd1385SRemy Bohmer 		data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
94123cd1385SRemy Bohmer 
94223cd1385SRemy Bohmer 		req->length = STATUS_BYTECOUNT;
94323cd1385SRemy Bohmer 		value = usb_ep_queue (ep, req, GFP_ATOMIC);
94423cd1385SRemy Bohmer 		dprintf ("send SPEED_CHANGE --> %d\n", value);
94523cd1385SRemy Bohmer 		if (value == 0)
94623cd1385SRemy Bohmer 			return;
94723cd1385SRemy Bohmer 	} else if (value != -ECONNRESET) {
94823cd1385SRemy Bohmer 		dprintf("event %02x --> %d\n",
94923cd1385SRemy Bohmer 			event->bNotificationType, value);
95023cd1385SRemy Bohmer 		if (event->bNotificationType==
95123cd1385SRemy Bohmer 				USB_CDC_NOTIFY_SPEED_CHANGE)
95223cd1385SRemy Bohmer 		{
95323cd1385SRemy Bohmer 			l_ethdev.network_started=1;
95423cd1385SRemy Bohmer 			printf("USB network up!\n");
95523cd1385SRemy Bohmer 		}
95623cd1385SRemy Bohmer 	}
95723cd1385SRemy Bohmer 	req->context = NULL;
95823cd1385SRemy Bohmer }
95923cd1385SRemy Bohmer 
96023cd1385SRemy Bohmer static void issue_start_status (struct eth_dev *dev)
96123cd1385SRemy Bohmer {
96223cd1385SRemy Bohmer 	struct usb_request		*req = dev->stat_req;
96323cd1385SRemy Bohmer 	struct usb_cdc_notification	*event;
96423cd1385SRemy Bohmer 	int				value;
96523cd1385SRemy Bohmer 
96623cd1385SRemy Bohmer 	/* flush old status
96723cd1385SRemy Bohmer 	 *
96823cd1385SRemy Bohmer 	 * FIXME ugly idiom, maybe we'd be better with just
96923cd1385SRemy Bohmer 	 * a "cancel the whole queue" primitive since any
97023cd1385SRemy Bohmer 	 * unlink-one primitive has way too many error modes.
97123cd1385SRemy Bohmer 	 * here, we "know" toggle is already clear...
97223cd1385SRemy Bohmer 	 *
97323cd1385SRemy Bohmer 	 * FIXME iff req->context != null just dequeue it
97423cd1385SRemy Bohmer 	 */
97523cd1385SRemy Bohmer 	usb_ep_disable (dev->status_ep);
97623cd1385SRemy Bohmer 	usb_ep_enable (dev->status_ep, dev->status);
97723cd1385SRemy Bohmer 
97823cd1385SRemy Bohmer 	/* 3.8.1 says to issue first NETWORK_CONNECTION, then
97923cd1385SRemy Bohmer 	 * a SPEED_CHANGE.  could be useful in some configs.
98023cd1385SRemy Bohmer 	 */
98123cd1385SRemy Bohmer 	event = req->buf;
98223cd1385SRemy Bohmer 	event->bmRequestType = 0xA1;
98323cd1385SRemy Bohmer 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
98423cd1385SRemy Bohmer 	event->wValue = __constant_cpu_to_le16 (1);	/* connected */
98523cd1385SRemy Bohmer 	event->wIndex = __constant_cpu_to_le16 (1);
98623cd1385SRemy Bohmer 	event->wLength = 0;
98723cd1385SRemy Bohmer 
98823cd1385SRemy Bohmer 	req->length = sizeof *event;
98923cd1385SRemy Bohmer 	req->complete = eth_status_complete;
99023cd1385SRemy Bohmer 	req->context = dev;
99123cd1385SRemy Bohmer 
99223cd1385SRemy Bohmer 	value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
99323cd1385SRemy Bohmer 	if (value < 0)
99423cd1385SRemy Bohmer 		printf ("status buf queue --> %d\n", value);
99523cd1385SRemy Bohmer }
99623cd1385SRemy Bohmer 
99723cd1385SRemy Bohmer #endif
99823cd1385SRemy Bohmer 
99923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
100023cd1385SRemy Bohmer 
100123cd1385SRemy Bohmer static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
100223cd1385SRemy Bohmer {
100323cd1385SRemy Bohmer 	if (req->status || req->actual != req->length)
100423cd1385SRemy Bohmer 		dprintf  (/*(struct eth_dev *) ep->driver_data*/
100523cd1385SRemy Bohmer 				"setup complete --> %d, %d/%d\n",
100623cd1385SRemy Bohmer 				req->status, req->actual, req->length);
100723cd1385SRemy Bohmer }
100823cd1385SRemy Bohmer 
100923cd1385SRemy Bohmer /*
101023cd1385SRemy Bohmer  * The setup() callback implements all the ep0 functionality that's not
101123cd1385SRemy Bohmer  * handled lower down.  CDC has a number of less-common features:
101223cd1385SRemy Bohmer  *
101323cd1385SRemy Bohmer  *  - two interfaces:  control, and ethernet data
101423cd1385SRemy Bohmer  *  - Ethernet data interface has two altsettings:  default, and active
101523cd1385SRemy Bohmer  *  - class-specific descriptors for the control interface
101623cd1385SRemy Bohmer  *  - class-specific control requests
101723cd1385SRemy Bohmer  */
101823cd1385SRemy Bohmer static int
101923cd1385SRemy Bohmer eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
102023cd1385SRemy Bohmer {
102123cd1385SRemy Bohmer 	struct eth_dev		*dev = get_gadget_data (gadget);
102223cd1385SRemy Bohmer 	struct usb_request	*req = dev->req;
102323cd1385SRemy Bohmer 	int			value = -EOPNOTSUPP;
102423cd1385SRemy Bohmer 	u16			wIndex = le16_to_cpu(ctrl->wIndex);
102523cd1385SRemy Bohmer 	u16			wValue = le16_to_cpu(ctrl->wValue);
102623cd1385SRemy Bohmer 	u16			wLength = le16_to_cpu(ctrl->wLength);
102723cd1385SRemy Bohmer 
102823cd1385SRemy Bohmer 	/* descriptors just go into the pre-allocated ep0 buffer,
102923cd1385SRemy Bohmer 	 * while config change events may enable network traffic.
103023cd1385SRemy Bohmer 	 */
103123cd1385SRemy Bohmer 
103223cd1385SRemy Bohmer 	dprintf("eth_setup:...\n");
103323cd1385SRemy Bohmer 
103423cd1385SRemy Bohmer 	req->complete = eth_setup_complete;
103523cd1385SRemy Bohmer 	switch (ctrl->bRequest) {
103623cd1385SRemy Bohmer 
103723cd1385SRemy Bohmer 	case USB_REQ_GET_DESCRIPTOR:
103823cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
103923cd1385SRemy Bohmer 			break;
104023cd1385SRemy Bohmer 		switch (wValue >> 8) {
104123cd1385SRemy Bohmer 
104223cd1385SRemy Bohmer 		case USB_DT_DEVICE:
104323cd1385SRemy Bohmer 			value = min (wLength, (u16) sizeof device_desc);
104423cd1385SRemy Bohmer 			memcpy (req->buf, &device_desc, value);
104523cd1385SRemy Bohmer 			break;
104623cd1385SRemy Bohmer 		case USB_DT_DEVICE_QUALIFIER:
104723cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
104823cd1385SRemy Bohmer 				break;
104923cd1385SRemy Bohmer 			value = min (wLength, (u16) sizeof dev_qualifier);
105023cd1385SRemy Bohmer 			memcpy (req->buf, &dev_qualifier, value);
105123cd1385SRemy Bohmer 			break;
105223cd1385SRemy Bohmer 
105323cd1385SRemy Bohmer 		case USB_DT_OTHER_SPEED_CONFIG:
105423cd1385SRemy Bohmer 			if (!gadget_is_dualspeed(gadget))
105523cd1385SRemy Bohmer 				break;
105623cd1385SRemy Bohmer 			/* FALLTHROUGH */
105723cd1385SRemy Bohmer 		case USB_DT_CONFIG:
105823cd1385SRemy Bohmer 			value = config_buf(gadget, req->buf,
105923cd1385SRemy Bohmer 					wValue >> 8,
106023cd1385SRemy Bohmer 					wValue & 0xff,
106123cd1385SRemy Bohmer 					gadget_is_otg(gadget));
106223cd1385SRemy Bohmer 			if (value >= 0)
106323cd1385SRemy Bohmer 				value = min (wLength, (u16) value);
106423cd1385SRemy Bohmer 			break;
106523cd1385SRemy Bohmer 
106623cd1385SRemy Bohmer 		case USB_DT_STRING:
106723cd1385SRemy Bohmer 			value = usb_gadget_get_string (&stringtab,
106823cd1385SRemy Bohmer 					wValue & 0xff, req->buf);
106923cd1385SRemy Bohmer 
107023cd1385SRemy Bohmer 			if (value >= 0)
107123cd1385SRemy Bohmer 				value = min (wLength, (u16) value);
107223cd1385SRemy Bohmer 
107323cd1385SRemy Bohmer 			break;
107423cd1385SRemy Bohmer 		}
107523cd1385SRemy Bohmer 		break;
107623cd1385SRemy Bohmer 
107723cd1385SRemy Bohmer 	case USB_REQ_SET_CONFIGURATION:
107823cd1385SRemy Bohmer 		if (ctrl->bRequestType != 0)
107923cd1385SRemy Bohmer 			break;
108023cd1385SRemy Bohmer 		if (gadget->a_hnp_support)
108123cd1385SRemy Bohmer 			DEBUG (dev, "HNP available\n");
108223cd1385SRemy Bohmer 		else if (gadget->a_alt_hnp_support)
108323cd1385SRemy Bohmer 			DEBUG (dev, "HNP needs a different root port\n");
108423cd1385SRemy Bohmer 		value = eth_set_config (dev, wValue, GFP_ATOMIC);
108523cd1385SRemy Bohmer 		break;
108623cd1385SRemy Bohmer 	case USB_REQ_GET_CONFIGURATION:
108723cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_DIR_IN)
108823cd1385SRemy Bohmer 			break;
108923cd1385SRemy Bohmer 		*(u8 *)req->buf = dev->config;
109023cd1385SRemy Bohmer 		value = min (wLength, (u16) 1);
109123cd1385SRemy Bohmer 		break;
109223cd1385SRemy Bohmer 
109323cd1385SRemy Bohmer 	case USB_REQ_SET_INTERFACE:
109423cd1385SRemy Bohmer 		if (ctrl->bRequestType != USB_RECIP_INTERFACE
109523cd1385SRemy Bohmer 				|| !dev->config
109623cd1385SRemy Bohmer 				|| wIndex > 1)
109723cd1385SRemy Bohmer 			break;
109823cd1385SRemy Bohmer 		if (!cdc_active(dev) && wIndex != 0)
109923cd1385SRemy Bohmer 			break;
110023cd1385SRemy Bohmer 
110123cd1385SRemy Bohmer 		/* PXA hardware partially handles SET_INTERFACE;
110223cd1385SRemy Bohmer 		 * we need to kluge around that interference.
110323cd1385SRemy Bohmer 		 */
110423cd1385SRemy Bohmer 		if (gadget_is_pxa (gadget)) {
110523cd1385SRemy Bohmer 			value = eth_set_config (dev, DEV_CONFIG_VALUE,
110623cd1385SRemy Bohmer 						GFP_ATOMIC);
110723cd1385SRemy Bohmer 			goto done_set_intf;
110823cd1385SRemy Bohmer 		}
110923cd1385SRemy Bohmer 
111023cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
111123cd1385SRemy Bohmer 		switch (wIndex) {
111223cd1385SRemy Bohmer 		case 0:		/* control/master intf */
111323cd1385SRemy Bohmer 			if (wValue != 0)
111423cd1385SRemy Bohmer 				break;
111523cd1385SRemy Bohmer 			if (dev->status) {
111623cd1385SRemy Bohmer 				usb_ep_disable (dev->status_ep);
111723cd1385SRemy Bohmer 				usb_ep_enable (dev->status_ep, dev->status);
111823cd1385SRemy Bohmer 			}
111923cd1385SRemy Bohmer 			value = 0;
112023cd1385SRemy Bohmer 			break;
112123cd1385SRemy Bohmer 		case 1:		/* data intf */
112223cd1385SRemy Bohmer 			if (wValue > 1)
112323cd1385SRemy Bohmer 				break;
112423cd1385SRemy Bohmer 			usb_ep_disable (dev->in_ep);
112523cd1385SRemy Bohmer 			usb_ep_disable (dev->out_ep);
112623cd1385SRemy Bohmer 
112723cd1385SRemy Bohmer 			/* CDC requires the data transfers not be done from
112823cd1385SRemy Bohmer 			 * the default interface setting ... also, setting
112923cd1385SRemy Bohmer 			 * the non-default interface resets filters etc.
113023cd1385SRemy Bohmer 			 */
113123cd1385SRemy Bohmer 			if (wValue == 1) {
113223cd1385SRemy Bohmer 				if (!cdc_active (dev))
113323cd1385SRemy Bohmer 					break;
113423cd1385SRemy Bohmer 				usb_ep_enable (dev->in_ep, dev->in);
113523cd1385SRemy Bohmer 				usb_ep_enable (dev->out_ep, dev->out);
113623cd1385SRemy Bohmer 				dev->cdc_filter = DEFAULT_FILTER;
113723cd1385SRemy Bohmer 				if (dev->status)
113823cd1385SRemy Bohmer 					issue_start_status (dev);
113923cd1385SRemy Bohmer 			}
114023cd1385SRemy Bohmer 
114123cd1385SRemy Bohmer 			value = 0;
114223cd1385SRemy Bohmer 			break;
114323cd1385SRemy Bohmer 		}
114423cd1385SRemy Bohmer #else
114523cd1385SRemy Bohmer 		/* FIXME this is wrong, as is the assumption that
114623cd1385SRemy Bohmer 		 * all non-PXA hardware talks real CDC ...
114723cd1385SRemy Bohmer 		 */
114823cd1385SRemy Bohmer 		dev_warn (&gadget->dev, "set_interface ignored!\n");
114923cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
115023cd1385SRemy Bohmer 
115123cd1385SRemy Bohmer done_set_intf:
115223cd1385SRemy Bohmer 		break;
115323cd1385SRemy Bohmer 	case USB_REQ_GET_INTERFACE:
115423cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
115523cd1385SRemy Bohmer 				|| !dev->config
115623cd1385SRemy Bohmer 				|| wIndex > 1)
115723cd1385SRemy Bohmer 			break;
115823cd1385SRemy Bohmer 		if (!(cdc_active(dev)) && wIndex != 0)
115923cd1385SRemy Bohmer 			break;
116023cd1385SRemy Bohmer 
116123cd1385SRemy Bohmer 		/* for CDC, iff carrier is on, data interface is active. */
116223cd1385SRemy Bohmer 		if (wIndex != 1)
116323cd1385SRemy Bohmer 			*(u8 *)req->buf = 0;
116423cd1385SRemy Bohmer 		else {
116523cd1385SRemy Bohmer 			/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
116623cd1385SRemy Bohmer 			/* carrier always ok ...*/
116723cd1385SRemy Bohmer 			*(u8 *)req->buf = 1 ;
116823cd1385SRemy Bohmer 		}
116923cd1385SRemy Bohmer 		value = min (wLength, (u16) 1);
117023cd1385SRemy Bohmer 		break;
117123cd1385SRemy Bohmer 
117223cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC
117323cd1385SRemy Bohmer 	case USB_CDC_SET_ETHERNET_PACKET_FILTER:
117423cd1385SRemy Bohmer 		/* see 6.2.30: no data, wIndex = interface,
117523cd1385SRemy Bohmer 		 * wValue = packet filter bitmap
117623cd1385SRemy Bohmer 		 */
117723cd1385SRemy Bohmer 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
117823cd1385SRemy Bohmer 				|| !cdc_active(dev)
117923cd1385SRemy Bohmer 				|| wLength != 0
118023cd1385SRemy Bohmer 				|| wIndex > 1)
118123cd1385SRemy Bohmer 			break;
118223cd1385SRemy Bohmer 		printf ("packet filter %02x\n", wValue);
118323cd1385SRemy Bohmer 		dev->cdc_filter = wValue;
118423cd1385SRemy Bohmer 		value = 0;
118523cd1385SRemy Bohmer 		break;
118623cd1385SRemy Bohmer 
118723cd1385SRemy Bohmer 	/* and potentially:
118823cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
118923cd1385SRemy Bohmer 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
119023cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
119123cd1385SRemy Bohmer 	 * case USB_CDC_GET_ETHERNET_STATISTIC:
119223cd1385SRemy Bohmer 	 */
119323cd1385SRemy Bohmer 
119423cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */
119523cd1385SRemy Bohmer 
119623cd1385SRemy Bohmer 	default:
119723cd1385SRemy Bohmer 		printf (
119823cd1385SRemy Bohmer 			"unknown control req%02x.%02x v%04x i%04x l%d\n",
119923cd1385SRemy Bohmer 			ctrl->bRequestType, ctrl->bRequest,
120023cd1385SRemy Bohmer 			wValue, wIndex, wLength);
120123cd1385SRemy Bohmer 	}
120223cd1385SRemy Bohmer 
120323cd1385SRemy Bohmer 	/* respond with data transfer before status phase? */
120423cd1385SRemy Bohmer 	if (value >= 0) {
120523cd1385SRemy Bohmer 		dprintf("respond with data transfer before status phase\n");
120623cd1385SRemy Bohmer 		req->length = value;
120723cd1385SRemy Bohmer 		req->zero = value < wLength
120823cd1385SRemy Bohmer 				&& (value % gadget->ep0->maxpacket) == 0;
120923cd1385SRemy Bohmer 		value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
121023cd1385SRemy Bohmer 		if (value < 0) {
121123cd1385SRemy Bohmer 			DEBUG (dev, "ep_queue --> %d\n", value);
121223cd1385SRemy Bohmer 			req->status = 0;
121323cd1385SRemy Bohmer 			eth_setup_complete (gadget->ep0, req);
121423cd1385SRemy Bohmer 		}
121523cd1385SRemy Bohmer 	}
121623cd1385SRemy Bohmer 
121723cd1385SRemy Bohmer 	/* host either stalls (value < 0) or reports success */
121823cd1385SRemy Bohmer 	return value;
121923cd1385SRemy Bohmer }
122023cd1385SRemy Bohmer 
122123cd1385SRemy Bohmer 
122223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
122323cd1385SRemy Bohmer 
122423cd1385SRemy Bohmer static void rx_complete (struct usb_ep *ep, struct usb_request *req);
122523cd1385SRemy Bohmer 
122623cd1385SRemy Bohmer static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
122723cd1385SRemy Bohmer 				gfp_t gfp_flags)
122823cd1385SRemy Bohmer {
122923cd1385SRemy Bohmer 	int			retval = -ENOMEM;
123023cd1385SRemy Bohmer 	size_t			size;
123123cd1385SRemy Bohmer 
123223cd1385SRemy Bohmer 	/* Padding up to RX_EXTRA handles minor disagreements with host.
123323cd1385SRemy Bohmer 	 * Normally we use the USB "terminate on short read" convention;
123423cd1385SRemy Bohmer 	 * so allow up to (N*maxpacket), since that memory is normally
123523cd1385SRemy Bohmer 	 * already allocated.  Some hardware doesn't deal well with short
123623cd1385SRemy Bohmer 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
123723cd1385SRemy Bohmer 	 * byte off the end (to force hardware errors on overflow).
123823cd1385SRemy Bohmer 	 */
123923cd1385SRemy Bohmer 
124023cd1385SRemy Bohmer 	dprintf("%s\n", __func__);
124123cd1385SRemy Bohmer 
124223cd1385SRemy Bohmer 	size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
124323cd1385SRemy Bohmer 	size += dev->out_ep->maxpacket - 1;
124423cd1385SRemy Bohmer 	size -= size % dev->out_ep->maxpacket;
124523cd1385SRemy Bohmer 
124623cd1385SRemy Bohmer 
124723cd1385SRemy Bohmer 	/* Some platforms perform better when IP packets are aligned,
124823cd1385SRemy Bohmer 	 * but on at least one, checksumming fails otherwise.
124923cd1385SRemy Bohmer 	 */
125023cd1385SRemy Bohmer 
125123cd1385SRemy Bohmer 	req->buf = (u8 *) NetRxPackets[0];
125223cd1385SRemy Bohmer 	req->length = size;
125323cd1385SRemy Bohmer 	req->complete = rx_complete;
125423cd1385SRemy Bohmer 
125523cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
125623cd1385SRemy Bohmer 
125723cd1385SRemy Bohmer 	if (retval) {
125823cd1385SRemy Bohmer 		dprintf ("rx submit --> %d\n", retval);
125923cd1385SRemy Bohmer 	}
126023cd1385SRemy Bohmer 	return retval;
126123cd1385SRemy Bohmer }
126223cd1385SRemy Bohmer 
126323cd1385SRemy Bohmer 
126423cd1385SRemy Bohmer static void rx_complete (struct usb_ep *ep, struct usb_request *req)
126523cd1385SRemy Bohmer {
126623cd1385SRemy Bohmer 	struct eth_dev	*dev = ep->driver_data;
126723cd1385SRemy Bohmer 
126823cd1385SRemy Bohmer 	dprintf("%s\n", __func__);
126923cd1385SRemy Bohmer 	dprintf("rx status %d\n", req->status);
127023cd1385SRemy Bohmer 
127123cd1385SRemy Bohmer 	packet_received=1;
127223cd1385SRemy Bohmer 
127323cd1385SRemy Bohmer 	if (req)
127423cd1385SRemy Bohmer 		dev->rx_req=req;
127523cd1385SRemy Bohmer }
127623cd1385SRemy Bohmer 
127723cd1385SRemy Bohmer 
127823cd1385SRemy Bohmer static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
127923cd1385SRemy Bohmer {
128023cd1385SRemy Bohmer 
128123cd1385SRemy Bohmer 	dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0);
128223cd1385SRemy Bohmer 
128323cd1385SRemy Bohmer 	if (!dev->tx_req)
128423cd1385SRemy Bohmer 		goto fail;
128523cd1385SRemy Bohmer 
128623cd1385SRemy Bohmer 	dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0);
128723cd1385SRemy Bohmer 
128823cd1385SRemy Bohmer 	if (!dev->rx_req)
128923cd1385SRemy Bohmer 		goto fail;
129023cd1385SRemy Bohmer 
129123cd1385SRemy Bohmer 	return 0;
129223cd1385SRemy Bohmer 
129323cd1385SRemy Bohmer fail:
129423cd1385SRemy Bohmer 	DEBUG (dev, "can't alloc requests\n");
129523cd1385SRemy Bohmer 	return -1;
129623cd1385SRemy Bohmer }
129723cd1385SRemy Bohmer 
129823cd1385SRemy Bohmer 
129923cd1385SRemy Bohmer static void tx_complete (struct usb_ep *ep, struct usb_request *req)
130023cd1385SRemy Bohmer {
130123cd1385SRemy Bohmer 	dprintf("%s, status: %s\n", __func__,(req->status) ? "failed":"ok");
130223cd1385SRemy Bohmer 	packet_sent=1;
130323cd1385SRemy Bohmer }
130423cd1385SRemy Bohmer 
130523cd1385SRemy Bohmer static inline int eth_is_promisc (struct eth_dev *dev)
130623cd1385SRemy Bohmer {
130723cd1385SRemy Bohmer 	/* no filters for the CDC subset; always promisc */
130823cd1385SRemy Bohmer 	if (subset_active (dev))
130923cd1385SRemy Bohmer 		return 1;
131023cd1385SRemy Bohmer 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
131123cd1385SRemy Bohmer }
131223cd1385SRemy Bohmer 
131323cd1385SRemy Bohmer #if 0
131423cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
131523cd1385SRemy Bohmer {
131623cd1385SRemy Bohmer 	struct eth_dev		*dev = netdev_priv(net);
131723cd1385SRemy Bohmer 	int			length = skb->len;
131823cd1385SRemy Bohmer 	int			retval;
131923cd1385SRemy Bohmer 	struct usb_request	*req = NULL;
132023cd1385SRemy Bohmer 	unsigned long		flags;
132123cd1385SRemy Bohmer 
132223cd1385SRemy Bohmer 	/* apply outgoing CDC or RNDIS filters */
132323cd1385SRemy Bohmer 	if (!eth_is_promisc (dev)) {
132423cd1385SRemy Bohmer 		u8		*dest = skb->data;
132523cd1385SRemy Bohmer 
132623cd1385SRemy Bohmer 		if (is_multicast_ether_addr(dest)) {
132723cd1385SRemy Bohmer 			u16	type;
132823cd1385SRemy Bohmer 
132923cd1385SRemy Bohmer 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
133023cd1385SRemy Bohmer 			 * SET_ETHERNET_MULTICAST_FILTERS requests
133123cd1385SRemy Bohmer 			 */
133223cd1385SRemy Bohmer 			if (is_broadcast_ether_addr(dest))
133323cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_BROADCAST;
133423cd1385SRemy Bohmer 			else
133523cd1385SRemy Bohmer 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
133623cd1385SRemy Bohmer 			if (!(dev->cdc_filter & type)) {
133723cd1385SRemy Bohmer 				dev_kfree_skb_any (skb);
133823cd1385SRemy Bohmer 				return 0;
133923cd1385SRemy Bohmer 			}
134023cd1385SRemy Bohmer 		}
134123cd1385SRemy Bohmer 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
134223cd1385SRemy Bohmer 	}
134323cd1385SRemy Bohmer 
134423cd1385SRemy Bohmer 	spin_lock_irqsave(&dev->req_lock, flags);
134523cd1385SRemy Bohmer 	/*
134623cd1385SRemy Bohmer 	 * this freelist can be empty if an interrupt triggered disconnect()
134723cd1385SRemy Bohmer 	 * and reconfigured the gadget (shutting down this queue) after the
134823cd1385SRemy Bohmer 	 * network stack decided to xmit but before we got the spinlock.
134923cd1385SRemy Bohmer 	 */
135023cd1385SRemy Bohmer 	if (list_empty(&dev->tx_reqs)) {
135123cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
135223cd1385SRemy Bohmer 		return 1;
135323cd1385SRemy Bohmer 	}
135423cd1385SRemy Bohmer 
135523cd1385SRemy Bohmer 	req = container_of (dev->tx_reqs.next, struct usb_request, list);
135623cd1385SRemy Bohmer 	list_del (&req->list);
135723cd1385SRemy Bohmer 
135823cd1385SRemy Bohmer 	/* temporarily stop TX queue when the freelist empties */
135923cd1385SRemy Bohmer 	if (list_empty (&dev->tx_reqs))
136023cd1385SRemy Bohmer 		netif_stop_queue (net);
136123cd1385SRemy Bohmer 	spin_unlock_irqrestore(&dev->req_lock, flags);
136223cd1385SRemy Bohmer 
136323cd1385SRemy Bohmer 	/* no buffer copies needed, unless the network stack did it
136423cd1385SRemy Bohmer 	 * or the hardware can't use skb buffers.
136523cd1385SRemy Bohmer 	 * or there's not enough space for any RNDIS headers we need
136623cd1385SRemy Bohmer 	 */
136723cd1385SRemy Bohmer 	if (rndis_active(dev)) {
136823cd1385SRemy Bohmer 		struct sk_buff	*skb_rndis;
136923cd1385SRemy Bohmer 
137023cd1385SRemy Bohmer 		skb_rndis = skb_realloc_headroom (skb,
137123cd1385SRemy Bohmer 				sizeof (struct rndis_packet_msg_type));
137223cd1385SRemy Bohmer 		if (!skb_rndis)
137323cd1385SRemy Bohmer 			goto drop;
137423cd1385SRemy Bohmer 
137523cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
137623cd1385SRemy Bohmer 		skb = skb_rndis;
137723cd1385SRemy Bohmer 		rndis_add_hdr (skb);
137823cd1385SRemy Bohmer 		length = skb->len;
137923cd1385SRemy Bohmer 	}
138023cd1385SRemy Bohmer 	req->buf = skb->data;
138123cd1385SRemy Bohmer 	req->context = skb;
138223cd1385SRemy Bohmer 	req->complete = tx_complete;
138323cd1385SRemy Bohmer 
138423cd1385SRemy Bohmer 	/* use zlp framing on tx for strict CDC-Ether conformance,
138523cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
138623cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
138723cd1385SRemy Bohmer 	 */
138823cd1385SRemy Bohmer 	req->zero = 1;
138923cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
139023cd1385SRemy Bohmer 		length++;
139123cd1385SRemy Bohmer 
139223cd1385SRemy Bohmer 	req->length = length;
139323cd1385SRemy Bohmer 
139423cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
139523cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
139623cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
139723cd1385SRemy Bohmer 			? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
139823cd1385SRemy Bohmer 			: 0;
139923cd1385SRemy Bohmer 
140023cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
140123cd1385SRemy Bohmer 	switch (retval) {
140223cd1385SRemy Bohmer 	default:
140323cd1385SRemy Bohmer 		DEBUG (dev, "tx queue err %d\n", retval);
140423cd1385SRemy Bohmer 		break;
140523cd1385SRemy Bohmer 	case 0:
140623cd1385SRemy Bohmer 		net->trans_start = jiffies;
140723cd1385SRemy Bohmer 		atomic_inc (&dev->tx_qlen);
140823cd1385SRemy Bohmer 	}
140923cd1385SRemy Bohmer 
141023cd1385SRemy Bohmer 	if (retval) {
141123cd1385SRemy Bohmer drop:
141223cd1385SRemy Bohmer 		dev->stats.tx_dropped++;
141323cd1385SRemy Bohmer 		dev_kfree_skb_any (skb);
141423cd1385SRemy Bohmer 		spin_lock_irqsave(&dev->req_lock, flags);
141523cd1385SRemy Bohmer 		if (list_empty (&dev->tx_reqs))
141623cd1385SRemy Bohmer 			netif_start_queue (net);
141723cd1385SRemy Bohmer 		list_add (&req->list, &dev->tx_reqs);
141823cd1385SRemy Bohmer 		spin_unlock_irqrestore(&dev->req_lock, flags);
141923cd1385SRemy Bohmer 	}
142023cd1385SRemy Bohmer 	return 0;
142123cd1385SRemy Bohmer }
142223cd1385SRemy Bohmer 
142323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
142423cd1385SRemy Bohmer #endif
142523cd1385SRemy Bohmer 
142623cd1385SRemy Bohmer static void eth_unbind (struct usb_gadget *gadget)
142723cd1385SRemy Bohmer {
142823cd1385SRemy Bohmer 	struct eth_dev *dev = get_gadget_data (gadget);
142923cd1385SRemy Bohmer 
143023cd1385SRemy Bohmer 	printf("eth_unbind:...\n");
143123cd1385SRemy Bohmer 
143223cd1385SRemy Bohmer 	if (dev->stat_req) {
143323cd1385SRemy Bohmer 		usb_ep_free_request (dev->status_ep, dev->stat_req);
143423cd1385SRemy Bohmer 		dev->stat_req = NULL;
143523cd1385SRemy Bohmer 	}
143623cd1385SRemy Bohmer 
143723cd1385SRemy Bohmer 	if (dev->tx_req) {
143823cd1385SRemy Bohmer 		usb_ep_free_request (dev->in_ep, dev->tx_req);
143923cd1385SRemy Bohmer 		dev->tx_req=NULL;
144023cd1385SRemy Bohmer 	}
144123cd1385SRemy Bohmer 
144223cd1385SRemy Bohmer 	if (dev->rx_req) {
144323cd1385SRemy Bohmer 		usb_ep_free_request (dev->in_ep, dev->rx_req);
144423cd1385SRemy Bohmer 		dev->rx_req=NULL;
144523cd1385SRemy Bohmer 	}
144623cd1385SRemy Bohmer 
144723cd1385SRemy Bohmer /*	unregister_netdev (dev->net);*/
144823cd1385SRemy Bohmer /*	free_netdev(dev->net);*/
144923cd1385SRemy Bohmer 
145023cd1385SRemy Bohmer 	set_gadget_data (gadget, NULL);
145123cd1385SRemy Bohmer }
145223cd1385SRemy Bohmer 
145323cd1385SRemy Bohmer static void eth_disconnect (struct usb_gadget *gadget)
145423cd1385SRemy Bohmer {
145523cd1385SRemy Bohmer 	eth_reset_config (get_gadget_data (gadget));
145623cd1385SRemy Bohmer }
145723cd1385SRemy Bohmer 
145823cd1385SRemy Bohmer static void eth_suspend (struct usb_gadget *gadget)
145923cd1385SRemy Bohmer {
146023cd1385SRemy Bohmer 	/* Not used */
146123cd1385SRemy Bohmer }
146223cd1385SRemy Bohmer 
146323cd1385SRemy Bohmer static void eth_resume (struct usb_gadget *gadget)
146423cd1385SRemy Bohmer {
146523cd1385SRemy Bohmer 	/* Not used */
146623cd1385SRemy Bohmer }
146723cd1385SRemy Bohmer 
146823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
146923cd1385SRemy Bohmer 
147023cd1385SRemy Bohmer static int is_eth_addr_valid(char *str)
147123cd1385SRemy Bohmer {
147223cd1385SRemy Bohmer 	if (strlen(str) == 17) {
147323cd1385SRemy Bohmer 		int i;
147423cd1385SRemy Bohmer 		char *p, *q;
147523cd1385SRemy Bohmer 		uchar ea[6];
147623cd1385SRemy Bohmer 
147723cd1385SRemy Bohmer 		/* see if it looks like an ethernet address */
147823cd1385SRemy Bohmer 
147923cd1385SRemy Bohmer 		p = str;
148023cd1385SRemy Bohmer 
148123cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
148223cd1385SRemy Bohmer 			char term = (i == 5 ? '\0' : ':');
148323cd1385SRemy Bohmer 
148423cd1385SRemy Bohmer 			ea[i] = simple_strtol(p, &q, 16);
148523cd1385SRemy Bohmer 
148623cd1385SRemy Bohmer 			if ((q - p) != 2 || *q++ != term)
148723cd1385SRemy Bohmer 				break;
148823cd1385SRemy Bohmer 
148923cd1385SRemy Bohmer 			p = q;
149023cd1385SRemy Bohmer 		}
149123cd1385SRemy Bohmer 
149223cd1385SRemy Bohmer 		if (i == 6) /* it looks ok */
149323cd1385SRemy Bohmer 			return 1;
149423cd1385SRemy Bohmer 	}
149523cd1385SRemy Bohmer 	return 0;
149623cd1385SRemy Bohmer }
149723cd1385SRemy Bohmer 
149823cd1385SRemy Bohmer static u8 nibble (unsigned char c)
149923cd1385SRemy Bohmer {
150023cd1385SRemy Bohmer 	if (likely (isdigit (c)))
150123cd1385SRemy Bohmer 		return c - '0';
150223cd1385SRemy Bohmer 	c = toupper (c);
150323cd1385SRemy Bohmer 	if (likely (isxdigit (c)))
150423cd1385SRemy Bohmer 		return 10 + c - 'A';
150523cd1385SRemy Bohmer 	return 0;
150623cd1385SRemy Bohmer }
150723cd1385SRemy Bohmer 
150823cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr)
150923cd1385SRemy Bohmer {
151023cd1385SRemy Bohmer 	if (str) {
151123cd1385SRemy Bohmer 		unsigned	i;
151223cd1385SRemy Bohmer 
151323cd1385SRemy Bohmer 		for (i = 0; i < 6; i++) {
151423cd1385SRemy Bohmer 			unsigned char num;
151523cd1385SRemy Bohmer 
151623cd1385SRemy Bohmer 			if((*str == '.') || (*str == ':'))
151723cd1385SRemy Bohmer 				str++;
151823cd1385SRemy Bohmer 			num = nibble(*str++) << 4;
151923cd1385SRemy Bohmer 			num |= (nibble(*str++));
152023cd1385SRemy Bohmer 			dev_addr [i] = num;
152123cd1385SRemy Bohmer 		}
152223cd1385SRemy Bohmer 		if (is_valid_ether_addr (dev_addr))
152323cd1385SRemy Bohmer 			return 0;
152423cd1385SRemy Bohmer 	}
152523cd1385SRemy Bohmer 	return 1;
152623cd1385SRemy Bohmer }
152723cd1385SRemy Bohmer 
152823cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget)
152923cd1385SRemy Bohmer {
153023cd1385SRemy Bohmer 	struct eth_dev		*dev = &l_ethdev;
153123cd1385SRemy Bohmer 	u8			cdc = 1, zlp = 1;
153223cd1385SRemy Bohmer 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
153323cd1385SRemy Bohmer 	int			gcnum;
153423cd1385SRemy Bohmer 	u8 			tmp[7];
153523cd1385SRemy Bohmer 
153623cd1385SRemy Bohmer 	/* these flags are only ever cleared; compiler take note */
153723cd1385SRemy Bohmer #ifndef	DEV_CONFIG_CDC
153823cd1385SRemy Bohmer 	cdc = 0;
153923cd1385SRemy Bohmer #endif
154023cd1385SRemy Bohmer 	/* Because most host side USB stacks handle CDC Ethernet, that
154123cd1385SRemy Bohmer 	 * standard protocol is _strongly_ preferred for interop purposes.
154223cd1385SRemy Bohmer 	 * (By everyone except Microsoft.)
154323cd1385SRemy Bohmer 	 */
154423cd1385SRemy Bohmer 	if (gadget_is_pxa (gadget)) {
154523cd1385SRemy Bohmer 		/* pxa doesn't support altsettings */
154623cd1385SRemy Bohmer 		cdc = 0;
154723cd1385SRemy Bohmer 	} else if (gadget_is_musbhdrc(gadget)) {
154823cd1385SRemy Bohmer 		/* reduce tx dma overhead by avoiding special cases */
154923cd1385SRemy Bohmer 		zlp = 0;
155023cd1385SRemy Bohmer 	} else if (gadget_is_sh(gadget)) {
155123cd1385SRemy Bohmer 		/* sh doesn't support multiple interfaces or configs */
155223cd1385SRemy Bohmer 		cdc = 0;
155323cd1385SRemy Bohmer 	} else if (gadget_is_sa1100 (gadget)) {
155423cd1385SRemy Bohmer 		/* hardware can't write zlps */
155523cd1385SRemy Bohmer 		zlp = 0;
155623cd1385SRemy Bohmer 		/* sa1100 CAN do CDC, without status endpoint ... we use
155723cd1385SRemy Bohmer 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
155823cd1385SRemy Bohmer 		 */
155923cd1385SRemy Bohmer 		cdc = 0;
156023cd1385SRemy Bohmer 	}
156123cd1385SRemy Bohmer 
156223cd1385SRemy Bohmer 	gcnum = usb_gadget_controller_number (gadget);
156323cd1385SRemy Bohmer 	if (gcnum >= 0)
156423cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum);
156523cd1385SRemy Bohmer 	else {
156623cd1385SRemy Bohmer 		/* can't assume CDC works.  don't want to default to
156723cd1385SRemy Bohmer 		 * anything less functional on CDC-capable hardware,
156823cd1385SRemy Bohmer 		 * so we fail in this case.
156923cd1385SRemy Bohmer 		 */
157023cd1385SRemy Bohmer 		dev_err (&gadget->dev,
157123cd1385SRemy Bohmer 			"controller '%s' not recognized\n",
157223cd1385SRemy Bohmer 			gadget->name);
157323cd1385SRemy Bohmer 		return -ENODEV;
157423cd1385SRemy Bohmer 	}
157523cd1385SRemy Bohmer 
157623cd1385SRemy Bohmer 	/* CDC subset ... recognized by Linux since 2.4.10, but Windows
157723cd1385SRemy Bohmer 	 * drivers aren't widely available.  (That may be improved by
157823cd1385SRemy Bohmer 	 * supporting one submode of the "SAFE" variant of MDLM.)
157923cd1385SRemy Bohmer 	 */
158023cd1385SRemy Bohmer 	if (!cdc) {
158123cd1385SRemy Bohmer 		device_desc.idVendor =
158223cd1385SRemy Bohmer 			__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
158323cd1385SRemy Bohmer 		device_desc.idProduct =
158423cd1385SRemy Bohmer 			__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
158523cd1385SRemy Bohmer 	}
158623cd1385SRemy Bohmer 
158723cd1385SRemy Bohmer 	/* support optional vendor/distro customization */
158823cd1385SRemy Bohmer #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
158923cd1385SRemy Bohmer 	device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
159023cd1385SRemy Bohmer 	device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
159123cd1385SRemy Bohmer #endif
159223cd1385SRemy Bohmer 	if (bcdDevice)
159323cd1385SRemy Bohmer 		device_desc.bcdDevice = cpu_to_le16(bcdDevice);
159423cd1385SRemy Bohmer 	if (iManufacturer)
159523cd1385SRemy Bohmer 		strcpy (manufacturer, iManufacturer);
159623cd1385SRemy Bohmer 	if (iProduct)
159723cd1385SRemy Bohmer 		strcpy (product_desc, iProduct);
159823cd1385SRemy Bohmer 	if (iSerialNumber) {
159923cd1385SRemy Bohmer 		device_desc.iSerialNumber = STRING_SERIALNUMBER,
160023cd1385SRemy Bohmer 		strcpy(serial_number, iSerialNumber);
160123cd1385SRemy Bohmer 	}
160223cd1385SRemy Bohmer 
160323cd1385SRemy Bohmer 	/* all we really need is bulk IN/OUT */
160423cd1385SRemy Bohmer 	usb_ep_autoconfig_reset (gadget);
160523cd1385SRemy Bohmer 	in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
160623cd1385SRemy Bohmer 	if (!in_ep) {
160723cd1385SRemy Bohmer autoconf_fail:
160823cd1385SRemy Bohmer 		dev_err (&gadget->dev,
160923cd1385SRemy Bohmer 			"can't autoconfigure on %s\n",
161023cd1385SRemy Bohmer 			gadget->name);
161123cd1385SRemy Bohmer 		return -ENODEV;
161223cd1385SRemy Bohmer 	}
161323cd1385SRemy Bohmer 	in_ep->driver_data = in_ep;	/* claim */
161423cd1385SRemy Bohmer 
161523cd1385SRemy Bohmer 	out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
161623cd1385SRemy Bohmer 	if (!out_ep)
161723cd1385SRemy Bohmer 		goto autoconf_fail;
161823cd1385SRemy Bohmer 	out_ep->driver_data = out_ep;	/* claim */
161923cd1385SRemy Bohmer 
162023cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
162123cd1385SRemy Bohmer 	/* CDC Ethernet control interface doesn't require a status endpoint.
162223cd1385SRemy Bohmer 	 * Since some hosts expect one, try to allocate one anyway.
162323cd1385SRemy Bohmer 	 */
162423cd1385SRemy Bohmer 	if (cdc) {
162523cd1385SRemy Bohmer 		status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
162623cd1385SRemy Bohmer 		if (status_ep) {
162723cd1385SRemy Bohmer 			status_ep->driver_data = status_ep;	/* claim */
162823cd1385SRemy Bohmer 		} else if (cdc) {
162923cd1385SRemy Bohmer 			control_intf.bNumEndpoints = 0;
163023cd1385SRemy Bohmer 			/* FIXME remove endpoint from descriptor list */
163123cd1385SRemy Bohmer 		}
163223cd1385SRemy Bohmer 	}
163323cd1385SRemy Bohmer #endif
163423cd1385SRemy Bohmer 
163523cd1385SRemy Bohmer 	/* one config:  cdc, else minimal subset */
163623cd1385SRemy Bohmer 	if (!cdc) {
163723cd1385SRemy Bohmer 		eth_config.bNumInterfaces = 1;
163823cd1385SRemy Bohmer 		eth_config.iConfiguration = STRING_SUBSET;
163923cd1385SRemy Bohmer 
164023cd1385SRemy Bohmer 		/* use functions to set these up, in case we're built to work
164123cd1385SRemy Bohmer 		 * with multiple controllers and must override CDC Ethernet.
164223cd1385SRemy Bohmer 		 */
164323cd1385SRemy Bohmer 		fs_subset_descriptors();
164423cd1385SRemy Bohmer 		hs_subset_descriptors();
164523cd1385SRemy Bohmer 	}
164623cd1385SRemy Bohmer 
164723cd1385SRemy Bohmer 	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
164823cd1385SRemy Bohmer 	usb_gadget_set_selfpowered (gadget);
164923cd1385SRemy Bohmer 
165023cd1385SRemy Bohmer 	if (gadget_is_dualspeed(gadget)) {
165123cd1385SRemy Bohmer 		if (!cdc)
165223cd1385SRemy Bohmer 			dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
165323cd1385SRemy Bohmer 
165423cd1385SRemy Bohmer 		/* assumes ep0 uses the same value for both speeds ... */
165523cd1385SRemy Bohmer 		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
165623cd1385SRemy Bohmer 
165723cd1385SRemy Bohmer 		/* and that all endpoints are dual-speed */
165823cd1385SRemy Bohmer 		hs_source_desc.bEndpointAddress =
165923cd1385SRemy Bohmer 				fs_source_desc.bEndpointAddress;
166023cd1385SRemy Bohmer 		hs_sink_desc.bEndpointAddress =
166123cd1385SRemy Bohmer 				fs_sink_desc.bEndpointAddress;
166223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
166323cd1385SRemy Bohmer 		if (status_ep)
166423cd1385SRemy Bohmer 			hs_status_desc.bEndpointAddress =
166523cd1385SRemy Bohmer 					fs_status_desc.bEndpointAddress;
166623cd1385SRemy Bohmer #endif
166723cd1385SRemy Bohmer 	}
166823cd1385SRemy Bohmer 
166923cd1385SRemy Bohmer 	if (gadget_is_otg(gadget)) {
167023cd1385SRemy Bohmer 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
167123cd1385SRemy Bohmer 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
167223cd1385SRemy Bohmer 		eth_config.bMaxPower = 4;
167323cd1385SRemy Bohmer 	}
167423cd1385SRemy Bohmer 
167523cd1385SRemy Bohmer 	dev->net = &l_netdev;
167623cd1385SRemy Bohmer 	strcpy (dev->net->name, USB_NET_NAME);
167723cd1385SRemy Bohmer 
167823cd1385SRemy Bohmer 	dev->cdc = cdc;
167923cd1385SRemy Bohmer 	dev->zlp = zlp;
168023cd1385SRemy Bohmer 
168123cd1385SRemy Bohmer 	dev->in_ep = in_ep;
168223cd1385SRemy Bohmer 	dev->out_ep = out_ep;
168323cd1385SRemy Bohmer 	dev->status_ep = status_ep;
168423cd1385SRemy Bohmer 
168523cd1385SRemy Bohmer 	/* Module params for these addresses should come from ID proms.
168623cd1385SRemy Bohmer 	 * The host side address is used with CDC, and commonly
168723cd1385SRemy Bohmer 	 * ends up in a persistent config database.  It's not clear if
168823cd1385SRemy Bohmer 	 * host side code for the SAFE thing cares -- its original BLAN
168923cd1385SRemy Bohmer 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
169023cd1385SRemy Bohmer 	 */
169123cd1385SRemy Bohmer 	get_ether_addr(dev_addr, dev->net->enetaddr);
169223cd1385SRemy Bohmer 
169323cd1385SRemy Bohmer 	memset(tmp, 0, sizeof(tmp));
169423cd1385SRemy Bohmer 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
169523cd1385SRemy Bohmer 
169623cd1385SRemy Bohmer 	get_ether_addr(host_addr, dev->host_mac);
169723cd1385SRemy Bohmer 
169823cd1385SRemy Bohmer 	sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X",
169923cd1385SRemy Bohmer 		dev->host_mac [0], dev->host_mac [1],
170023cd1385SRemy Bohmer 			dev->host_mac [2], dev->host_mac [3],
170123cd1385SRemy Bohmer 			dev->host_mac [4], dev->host_mac [5]);
170223cd1385SRemy Bohmer 
170323cd1385SRemy Bohmer 	INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
170423cd1385SRemy Bohmer 		out_ep->name, in_ep->name,
170523cd1385SRemy Bohmer 		status_ep ? " STATUS " : "",
170623cd1385SRemy Bohmer 		status_ep ? status_ep->name : ""
170723cd1385SRemy Bohmer 		);
170823cd1385SRemy Bohmer 	INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
170923cd1385SRemy Bohmer 		dev->net->enetaddr [0], dev->net->enetaddr [1],
171023cd1385SRemy Bohmer 		dev->net->enetaddr [2], dev->net->enetaddr [3],
171123cd1385SRemy Bohmer 		dev->net->enetaddr [4], dev->net->enetaddr [5]);
171223cd1385SRemy Bohmer 
171323cd1385SRemy Bohmer 	if (cdc) {
171423cd1385SRemy Bohmer 		INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
171523cd1385SRemy Bohmer 			dev->host_mac [0], dev->host_mac [1],
171623cd1385SRemy Bohmer 			dev->host_mac [2], dev->host_mac [3],
171723cd1385SRemy Bohmer 			dev->host_mac [4], dev->host_mac [5]);
171823cd1385SRemy Bohmer 	}
171923cd1385SRemy Bohmer 
172023cd1385SRemy Bohmer 	/* use PKTSIZE (or aligned... from u-boot) and set
172123cd1385SRemy Bohmer 	 * wMaxSegmentSize accordingly*/
172223cd1385SRemy Bohmer 	dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
172323cd1385SRemy Bohmer 
172423cd1385SRemy Bohmer 	/* preallocate control message data and buffer */
172523cd1385SRemy Bohmer 	dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
172623cd1385SRemy Bohmer 	if (!dev->req)
172723cd1385SRemy Bohmer 		goto fail;
172823cd1385SRemy Bohmer 	dev->req->buf = control_req;
172923cd1385SRemy Bohmer 	dev->req->complete = eth_setup_complete;
173023cd1385SRemy Bohmer 
173123cd1385SRemy Bohmer 	/* ... and maybe likewise for status transfer */
173223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC)
173323cd1385SRemy Bohmer 	if (dev->status_ep) {
173423cd1385SRemy Bohmer 		dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
173523cd1385SRemy Bohmer 		dev->stat_req->buf = status_req;
173623cd1385SRemy Bohmer 		if (!dev->stat_req) {
173723cd1385SRemy Bohmer 			dev->stat_req->buf=NULL;
173823cd1385SRemy Bohmer 			usb_ep_free_request (gadget->ep0, dev->req);
173923cd1385SRemy Bohmer 
174023cd1385SRemy Bohmer 			goto fail;
174123cd1385SRemy Bohmer 		}
174223cd1385SRemy Bohmer 		dev->stat_req->context = NULL;
174323cd1385SRemy Bohmer 	}
174423cd1385SRemy Bohmer #endif
174523cd1385SRemy Bohmer 
174623cd1385SRemy Bohmer 	/* finish hookup to lower layer ... */
174723cd1385SRemy Bohmer 	dev->gadget = gadget;
174823cd1385SRemy Bohmer 	set_gadget_data (gadget, dev);
174923cd1385SRemy Bohmer 	gadget->ep0->driver_data = dev;
175023cd1385SRemy Bohmer 
175123cd1385SRemy Bohmer 	/* two kinds of host-initiated state changes:
175223cd1385SRemy Bohmer 	 *  - iff DATA transfer is active, carrier is "on"
175323cd1385SRemy Bohmer 	 *  - tx queueing enabled if open *and* carrier is "on"
175423cd1385SRemy Bohmer 	 */
175523cd1385SRemy Bohmer 	return 0;
175623cd1385SRemy Bohmer 
175723cd1385SRemy Bohmer fail:
175823cd1385SRemy Bohmer 	dev_dbg(&gadget->dev, "register_netdev failed\n");
175923cd1385SRemy Bohmer 	eth_unbind (gadget);
176023cd1385SRemy Bohmer 	return -ENOMEM;
176123cd1385SRemy Bohmer }
176223cd1385SRemy Bohmer 
176323cd1385SRemy Bohmer static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
176423cd1385SRemy Bohmer {
176523cd1385SRemy Bohmer 	struct eth_dev *dev=&l_ethdev;
176623cd1385SRemy Bohmer 	struct usb_gadget *gadget;
176723cd1385SRemy Bohmer 	unsigned long ts;
176823cd1385SRemy Bohmer 	unsigned long timeout = USB_CONNECT_TIMEOUT;
176923cd1385SRemy Bohmer 
177023cd1385SRemy Bohmer 	if (!netdev) {
177123cd1385SRemy Bohmer 		printf("ERROR: received NULL ptr\n");
177223cd1385SRemy Bohmer 		goto fail;
177323cd1385SRemy Bohmer 	}
177423cd1385SRemy Bohmer 
177523cd1385SRemy Bohmer 	dev->network_started = 0;
177623cd1385SRemy Bohmer 	dev->tx_req = NULL;
177723cd1385SRemy Bohmer 	dev->rx_req = NULL;
177823cd1385SRemy Bohmer 
177923cd1385SRemy Bohmer 	packet_received = 0;
178023cd1385SRemy Bohmer 	packet_sent = 0;
178123cd1385SRemy Bohmer 
178223cd1385SRemy Bohmer 	gadget = dev->gadget;
178323cd1385SRemy Bohmer 	usb_gadget_connect(gadget);
178423cd1385SRemy Bohmer 
178523cd1385SRemy Bohmer 	if (getenv("cdc_connect_timeout"))
178623cd1385SRemy Bohmer 		timeout = simple_strtoul(getenv("cdc_connect_timeout"),
178723cd1385SRemy Bohmer 						NULL, 10) * CONFIG_SYS_HZ;
178823cd1385SRemy Bohmer 	ts = get_timer(0);
178923cd1385SRemy Bohmer 	while (!l_ethdev.network_started)
179023cd1385SRemy Bohmer 	{
179123cd1385SRemy Bohmer 		/* Handle control-c and timeouts */
179223cd1385SRemy Bohmer 		if (ctrlc() || (get_timer(ts) > timeout)) {
179323cd1385SRemy Bohmer 			printf("The remote end did not respond in time.\n");
179423cd1385SRemy Bohmer 			goto fail;
179523cd1385SRemy Bohmer 		}
179623cd1385SRemy Bohmer 		usb_gadget_handle_interrupts();
179723cd1385SRemy Bohmer 	}
179823cd1385SRemy Bohmer 
179923cd1385SRemy Bohmer 	rx_submit (dev, dev->rx_req, 0);
180023cd1385SRemy Bohmer 	return 0;
180123cd1385SRemy Bohmer fail:
180223cd1385SRemy Bohmer 	return -1;
180323cd1385SRemy Bohmer }
180423cd1385SRemy Bohmer 
180523cd1385SRemy Bohmer static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length)
180623cd1385SRemy Bohmer {
180723cd1385SRemy Bohmer 	int			retval;
180823cd1385SRemy Bohmer 	struct usb_request	*req = NULL;
180923cd1385SRemy Bohmer 
181023cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
181123cd1385SRemy Bohmer 	dprintf("%s:...\n",__func__);
181223cd1385SRemy Bohmer 
181323cd1385SRemy Bohmer 	req = dev->tx_req;
181423cd1385SRemy Bohmer 
181523cd1385SRemy Bohmer 	req->buf = (void *)packet;
181623cd1385SRemy Bohmer 	req->context = NULL;
181723cd1385SRemy Bohmer 	req->complete = tx_complete;
181823cd1385SRemy Bohmer 
181923cd1385SRemy Bohmer 	/* use zlp framing on tx for strict CDC-Ether conformance,
182023cd1385SRemy Bohmer 	 * though any robust network rx path ignores extra padding.
182123cd1385SRemy Bohmer 	 * and some hardware doesn't like to write zlps.
182223cd1385SRemy Bohmer 	 */
182323cd1385SRemy Bohmer 	req->zero = 1;
182423cd1385SRemy Bohmer 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
182523cd1385SRemy Bohmer 		length++;
182623cd1385SRemy Bohmer 
182723cd1385SRemy Bohmer 	req->length = length;
182823cd1385SRemy Bohmer #if 0
182923cd1385SRemy Bohmer 	/* throttle highspeed IRQ rate back slightly */
183023cd1385SRemy Bohmer 	if (gadget_is_dualspeed(dev->gadget))
183123cd1385SRemy Bohmer 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
183223cd1385SRemy Bohmer 			? ((dev->tx_qlen % qmult) != 0) : 0;
183323cd1385SRemy Bohmer #endif
183423cd1385SRemy Bohmer 	dev->tx_qlen=1;
183523cd1385SRemy Bohmer 
183623cd1385SRemy Bohmer 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
183723cd1385SRemy Bohmer 
183823cd1385SRemy Bohmer 	if (!retval)
183923cd1385SRemy Bohmer 		dprintf("%s: packet queued\n",__func__);
184023cd1385SRemy Bohmer 	while(!packet_sent)
184123cd1385SRemy Bohmer 	{
184223cd1385SRemy Bohmer 		packet_sent=0;
184323cd1385SRemy Bohmer 	}
184423cd1385SRemy Bohmer 
184523cd1385SRemy Bohmer 	return 0;
184623cd1385SRemy Bohmer }
184723cd1385SRemy Bohmer 
184823cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device* netdev)
184923cd1385SRemy Bohmer {
185023cd1385SRemy Bohmer 	struct eth_dev *dev = &l_ethdev;
185123cd1385SRemy Bohmer 
185223cd1385SRemy Bohmer 	usb_gadget_handle_interrupts();
185323cd1385SRemy Bohmer 
185423cd1385SRemy Bohmer 	if (packet_received)
185523cd1385SRemy Bohmer 	{
185623cd1385SRemy Bohmer 		dprintf("%s: packet received \n",__func__);
185723cd1385SRemy Bohmer 		if (dev->rx_req)
185823cd1385SRemy Bohmer 		{
185923cd1385SRemy Bohmer 			NetReceive(NetRxPackets[0],dev->rx_req->length);
186023cd1385SRemy Bohmer 			packet_received=0;
186123cd1385SRemy Bohmer 
186223cd1385SRemy Bohmer 			if (dev->rx_req)
186323cd1385SRemy Bohmer 				rx_submit (dev, dev->rx_req, 0);
186423cd1385SRemy Bohmer 		}
186523cd1385SRemy Bohmer 		else printf("dev->rx_req invalid\n");
186623cd1385SRemy Bohmer 	}
186723cd1385SRemy Bohmer 	return 0;
186823cd1385SRemy Bohmer }
186923cd1385SRemy Bohmer 
187023cd1385SRemy Bohmer void usb_eth_halt(struct eth_device* netdev)
187123cd1385SRemy Bohmer {
187223cd1385SRemy Bohmer 	struct eth_dev *dev =&l_ethdev;
187323cd1385SRemy Bohmer 
187423cd1385SRemy Bohmer 	if (!netdev)
187523cd1385SRemy Bohmer 	{
187623cd1385SRemy Bohmer 		printf("ERROR: received NULL ptr\n");
187723cd1385SRemy Bohmer 		return;
187823cd1385SRemy Bohmer 	}
187923cd1385SRemy Bohmer 
188023cd1385SRemy Bohmer 	usb_gadget_disconnect(dev->gadget);
188123cd1385SRemy Bohmer }
188223cd1385SRemy Bohmer 
188323cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = {
188423cd1385SRemy Bohmer 	.speed		= DEVSPEED,
188523cd1385SRemy Bohmer 
188623cd1385SRemy Bohmer 	.bind		= eth_bind,
188723cd1385SRemy Bohmer 	.unbind		= eth_unbind,
188823cd1385SRemy Bohmer 
188923cd1385SRemy Bohmer 	.setup		= eth_setup,
189023cd1385SRemy Bohmer 	.disconnect	= eth_disconnect,
189123cd1385SRemy Bohmer 
189223cd1385SRemy Bohmer 	.suspend	= eth_suspend,
189323cd1385SRemy Bohmer 	.resume		= eth_resume,
189423cd1385SRemy Bohmer };
189523cd1385SRemy Bohmer 
189623cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi)
189723cd1385SRemy Bohmer {
189823cd1385SRemy Bohmer 	int status = 0;
189923cd1385SRemy Bohmer 	struct eth_device *netdev=&l_netdev;
190023cd1385SRemy Bohmer 
190123cd1385SRemy Bohmer 	sprintf(netdev->name,"usb_ether");
190223cd1385SRemy Bohmer 
190323cd1385SRemy Bohmer 	netdev->init = usb_eth_init;
190423cd1385SRemy Bohmer 	netdev->send = usb_eth_send;
190523cd1385SRemy Bohmer 	netdev->recv = usb_eth_recv;
190623cd1385SRemy Bohmer 	netdev->halt = usb_eth_halt;
190723cd1385SRemy Bohmer 
190823cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP
190923cd1385SRemy Bohmer   #error not supported
191023cd1385SRemy Bohmer #endif
191123cd1385SRemy Bohmer 	/* Configure default mac-addresses for the USB ethernet device */
191223cd1385SRemy Bohmer #ifdef CONFIG_USBNET_DEV_ADDR
191323cd1385SRemy Bohmer 	strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
191423cd1385SRemy Bohmer #endif
191523cd1385SRemy Bohmer #ifdef CONFIG_USBNET_HOST_ADDR
191623cd1385SRemy Bohmer 	strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
191723cd1385SRemy Bohmer #endif
191823cd1385SRemy Bohmer 	/* Check if the user overruled the MAC addresses */
191923cd1385SRemy Bohmer 	if (getenv("usbnet_devaddr"))
192023cd1385SRemy Bohmer 		strncpy(dev_addr, getenv("usbnet_devaddr"),
192123cd1385SRemy Bohmer 			sizeof(dev_addr));
192223cd1385SRemy Bohmer 
192323cd1385SRemy Bohmer 	if (getenv("usbnet_hostaddr"))
192423cd1385SRemy Bohmer 		strncpy(host_addr, getenv("usbnet_hostaddr"),
192523cd1385SRemy Bohmer 			sizeof(host_addr));
192623cd1385SRemy Bohmer 
192723cd1385SRemy Bohmer 	/* Make sure both strings are terminated */
192823cd1385SRemy Bohmer 	dev_addr[sizeof(dev_addr)-1] = '\0';
192923cd1385SRemy Bohmer 	host_addr[sizeof(host_addr)-1] = '\0';
193023cd1385SRemy Bohmer 
193123cd1385SRemy Bohmer 	if (!is_eth_addr_valid(dev_addr)) {
193223cd1385SRemy Bohmer 		printf("ERROR: Need valid 'usbnet_devaddr' to be set\n");
193323cd1385SRemy Bohmer 		status = -1;
193423cd1385SRemy Bohmer 	}
193523cd1385SRemy Bohmer 	if (!is_eth_addr_valid(host_addr)) {
193623cd1385SRemy Bohmer 		printf("ERROR: Need valid 'usbnet_hostaddr' to be set\n");
193723cd1385SRemy Bohmer 		status = -1;
193823cd1385SRemy Bohmer 	}
193923cd1385SRemy Bohmer 	if (status)
194023cd1385SRemy Bohmer 		goto fail;
194123cd1385SRemy Bohmer 
194223cd1385SRemy Bohmer 	status = usb_gadget_register_driver(&eth_driver);
194323cd1385SRemy Bohmer 	if (status < 0)
194423cd1385SRemy Bohmer 		goto fail;
194523cd1385SRemy Bohmer 
194623cd1385SRemy Bohmer 	eth_register(netdev);
194723cd1385SRemy Bohmer 	return 0;
194823cd1385SRemy Bohmer 
194923cd1385SRemy Bohmer fail:
195023cd1385SRemy Bohmer 	printf("%s failed\n", __func__ );
195123cd1385SRemy Bohmer 	return status;
195223cd1385SRemy Bohmer }
195323cd1385SRemy Bohmer 
1954