xref: /openbmc/u-boot/drivers/usb/gadget/ether.c (revision 71a988aa)
1 /*
2  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3  *
4  * Copyright (C) 2003-2005,2008 David Brownell
5  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6  * Copyright (C) 2008 Nokia Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <common.h>
24 #include <asm/errno.h>
25 #include <linux/netdevice.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/usb/gadget.h>
29 #include <net.h>
30 #include <malloc.h>
31 #include <linux/ctype.h>
32 
33 #include "gadget_chips.h"
34 #include "rndis.h"
35 
36 #define USB_NET_NAME "usb_ether"
37 
38 #define atomic_read
39 extern struct platform_data brd;
40 #define spin_lock(x)
41 #define spin_unlock(x)
42 
43 
44 unsigned packet_received, packet_sent;
45 
46 #define GFP_ATOMIC ((gfp_t) 0)
47 #define GFP_KERNEL ((gfp_t) 0)
48 
49 /*
50  * Ethernet gadget driver -- with CDC and non-CDC options
51  * Builds on hardware support for a full duplex link.
52  *
53  * CDC Ethernet is the standard USB solution for sending Ethernet frames
54  * using USB.  Real hardware tends to use the same framing protocol but look
55  * different for control features.  This driver strongly prefers to use
56  * this USB-IF standard as its open-systems interoperability solution;
57  * most host side USB stacks (except from Microsoft) support it.
58  *
59  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
60  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
61  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
62  *
63  * There's some hardware that can't talk CDC ECM.  We make that hardware
64  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
65  * link-level setup only requires activating the configuration.  Only the
66  * endpoint descriptors, and product/vendor IDs, are relevant; no control
67  * operations are available.  Linux supports it, but other host operating
68  * systems may not.  (This is a subset of CDC Ethernet.)
69  *
70  * It turns out that if you add a few descriptors to that "CDC Subset",
71  * (Windows) host side drivers from MCCI can treat it as one submode of
72  * a proprietary scheme called "SAFE" ... without needing to know about
73  * specific product/vendor IDs.  So we do that, making it easier to use
74  * those MS-Windows drivers.  Those added descriptors make it resemble a
75  * CDC MDLM device, but they don't change device behavior at all.  (See
76  * MCCI Engineering report 950198 "SAFE Networking Functions".)
77  *
78  * A third option is also in use.  Rather than CDC Ethernet, or something
79  * simpler, Microsoft pushes their own approach: RNDIS.  The published
80  * RNDIS specs are ambiguous and appear to be incomplete, and are also
81  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
82  */
83 #define ETH_ALEN	6		/* Octets in one ethernet addr	 */
84 #define ETH_HLEN	14		/* Total octets in header.	 */
85 #define ETH_ZLEN	60		/* Min. octets in frame sans FCS */
86 #define ETH_DATA_LEN	1500		/* Max. octets in payload	 */
87 #define ETH_FRAME_LEN	PKTSIZE_ALIGN	/* Max. octets in frame sans FCS */
88 #define ETH_FCS_LEN	4		/* Octets in the FCS		 */
89 
90 #define DRIVER_DESC		"Ethernet Gadget"
91 /* Based on linux 2.6.27 version */
92 #define DRIVER_VERSION		"May Day 2005"
93 
94 static const char shortname[] = "ether";
95 static const char driver_desc[] = DRIVER_DESC;
96 
97 #define RX_EXTRA	20		/* guard against rx overflows */
98 
99 #ifndef	CONFIG_USB_ETH_RNDIS
100 #define rndis_uninit(x)		do {} while (0)
101 #define rndis_deregister(c)	do {} while (0)
102 #define rndis_exit()		do {} while (0)
103 #endif
104 
105 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
106 #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
107 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
108 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
109 			|USB_CDC_PACKET_TYPE_DIRECTED)
110 
111 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
112 
113 /*-------------------------------------------------------------------------*/
114 
115 struct eth_dev {
116 	struct usb_gadget	*gadget;
117 	struct usb_request	*req;		/* for control responses */
118 	struct usb_request	*stat_req;	/* for cdc & rndis status */
119 
120 	u8			config;
121 	struct usb_ep		*in_ep, *out_ep, *status_ep;
122 	const struct usb_endpoint_descriptor
123 				*in, *out, *status;
124 
125 	struct usb_request	*tx_req, *rx_req;
126 
127 	struct eth_device	*net;
128 	struct net_device_stats	stats;
129 	unsigned int		tx_qlen;
130 
131 	unsigned		zlp:1;
132 	unsigned		cdc:1;
133 	unsigned		rndis:1;
134 	unsigned		suspended:1;
135 	unsigned		network_started:1;
136 	u16			cdc_filter;
137 	unsigned long		todo;
138 	int			mtu;
139 #define	WORK_RX_MEMORY		0
140 	int			rndis_config;
141 	u8			host_mac[ETH_ALEN];
142 };
143 
144 /*
145  * This version autoconfigures as much as possible at run-time.
146  *
147  * It also ASSUMES a self-powered device, without remote wakeup,
148  * although remote wakeup support would make sense.
149  */
150 
151 /*-------------------------------------------------------------------------*/
152 static struct eth_dev l_ethdev;
153 static struct eth_device l_netdev;
154 static struct usb_gadget_driver eth_driver;
155 
156 /*-------------------------------------------------------------------------*/
157 
158 /* "main" config is either CDC, or its simple subset */
159 static inline int is_cdc(struct eth_dev *dev)
160 {
161 #if	!defined(CONFIG_USB_ETH_SUBSET)
162 	return 1;		/* only cdc possible */
163 #elif	!defined(CONFIG_USB_ETH_CDC)
164 	return 0;		/* only subset possible */
165 #else
166 	return dev->cdc;	/* depends on what hardware we found */
167 #endif
168 }
169 
170 /* "secondary" RNDIS config may sometimes be activated */
171 static inline int rndis_active(struct eth_dev *dev)
172 {
173 #ifdef	CONFIG_USB_ETH_RNDIS
174 	return dev->rndis;
175 #else
176 	return 0;
177 #endif
178 }
179 
180 #define	subset_active(dev)	(!is_cdc(dev) && !rndis_active(dev))
181 #define	cdc_active(dev)		(is_cdc(dev) && !rndis_active(dev))
182 
183 #define DEFAULT_QLEN	2	/* double buffering by default */
184 
185 /* peak bulk transfer bits-per-second */
186 #define	HS_BPS		(13 * 512 * 8 * 1000 * 8)
187 #define	FS_BPS		(19 *  64 * 1 * 1000 * 8)
188 
189 #ifdef CONFIG_USB_GADGET_DUALSPEED
190 #define	DEVSPEED	USB_SPEED_HIGH
191 
192 #ifdef CONFIG_USB_ETH_QMULT
193 #define qmult CONFIG_USB_ETH_QMULT
194 #else
195 #define qmult 5
196 #endif
197 
198 /* for dual-speed hardware, use deeper queues at highspeed */
199 #define qlen(gadget) \
200 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
201 
202 static inline int BITRATE(struct usb_gadget *g)
203 {
204 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
205 }
206 
207 #else	/* full speed (low speed doesn't do bulk) */
208 
209 #define qmult		1
210 
211 #define	DEVSPEED	USB_SPEED_FULL
212 
213 #define qlen(gadget) DEFAULT_QLEN
214 
215 static inline int BITRATE(struct usb_gadget *g)
216 {
217 	return FS_BPS;
218 }
219 #endif
220 
221 /*-------------------------------------------------------------------------*/
222 
223 /*
224  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
225  * Instead:  allocate your own, using normal USB-IF procedures.
226  */
227 
228 /*
229  * Thanks to NetChip Technologies for donating this product ID.
230  * It's for devices with only CDC Ethernet configurations.
231  */
232 #define CDC_VENDOR_NUM		0x0525	/* NetChip */
233 #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
234 
235 /*
236  * For hardware that can't talk CDC, we use the same vendor ID that
237  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
238  * with pxa250.  We're protocol-compatible, if the host-side drivers
239  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
240  * drivers that need to hard-wire endpoint numbers have a hook.
241  *
242  * The protocol is a minimal subset of CDC Ether, which works on any bulk
243  * hardware that's not deeply broken ... even on hardware that can't talk
244  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
245  * doesn't handle control-OUT).
246  */
247 #define	SIMPLE_VENDOR_NUM	0x049f	/* Compaq Computer Corp. */
248 #define	SIMPLE_PRODUCT_NUM	0x505a	/* Linux-USB "CDC Subset" Device */
249 
250 /*
251  * For hardware that can talk RNDIS and either of the above protocols,
252  * use this ID ... the windows INF files will know it.  Unless it's
253  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
254  * the non-RNDIS configuration.
255  */
256 #define RNDIS_VENDOR_NUM	0x0525	/* NetChip */
257 #define RNDIS_PRODUCT_NUM	0xa4a2	/* Ethernet/RNDIS Gadget */
258 
259 /*
260  * Some systems will want different product identifers published in the
261  * device descriptor, either numbers or strings or both.  These string
262  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
263  */
264 
265 /*
266  * Emulating them in eth_bind:
267  * static ushort idVendor;
268  * static ushort idProduct;
269  */
270 
271 #if defined(CONFIG_USBNET_MANUFACTURER)
272 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
273 #else
274 static char *iManufacturer = "U-boot";
275 #endif
276 
277 /* These probably need to be configurable. */
278 static ushort bcdDevice;
279 static char *iProduct;
280 static char *iSerialNumber;
281 
282 static char dev_addr[18];
283 
284 static char host_addr[18];
285 
286 
287 /*-------------------------------------------------------------------------*/
288 
289 /*
290  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
291  * ep0 implementation:  descriptors, config management, setup().
292  * also optional class-specific notification interrupt transfer.
293  */
294 
295 /*
296  * DESCRIPTORS ... most are static, but strings and (full) configuration
297  * descriptors are built on demand.  For now we do either full CDC, or
298  * our simple subset, with RNDIS as an optional second configuration.
299  *
300  * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet.  But
301  * the class descriptors match a modem (they're ignored; it's really just
302  * Ethernet functionality), they don't need the NOP altsetting, and the
303  * status transfer endpoint isn't optional.
304  */
305 
306 #define STRING_MANUFACTURER		1
307 #define STRING_PRODUCT			2
308 #define STRING_ETHADDR			3
309 #define STRING_DATA			4
310 #define STRING_CONTROL			5
311 #define STRING_RNDIS_CONTROL		6
312 #define STRING_CDC			7
313 #define STRING_SUBSET			8
314 #define STRING_RNDIS			9
315 #define STRING_SERIALNUMBER		10
316 
317 /* holds our biggest descriptor (or RNDIS response) */
318 #define USB_BUFSIZ	256
319 
320 /*
321  * This device advertises one configuration, eth_config, unless RNDIS
322  * is enabled (rndis_config) on hardware supporting at least two configs.
323  *
324  * NOTE:  Controllers like superh_udc should probably be able to use
325  * an RNDIS-only configuration.
326  *
327  * FIXME define some higher-powered configurations to make it easier
328  * to recharge batteries ...
329  */
330 
331 #define DEV_CONFIG_VALUE	1	/* cdc or subset */
332 #define DEV_RNDIS_CONFIG_VALUE	2	/* rndis; optional */
333 
334 static struct usb_device_descriptor
335 device_desc = {
336 	.bLength =		sizeof device_desc,
337 	.bDescriptorType =	USB_DT_DEVICE,
338 
339 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
340 
341 	.bDeviceClass =		USB_CLASS_COMM,
342 	.bDeviceSubClass =	0,
343 	.bDeviceProtocol =	0,
344 
345 	.idVendor =		__constant_cpu_to_le16(CDC_VENDOR_NUM),
346 	.idProduct =		__constant_cpu_to_le16(CDC_PRODUCT_NUM),
347 	.iManufacturer =	STRING_MANUFACTURER,
348 	.iProduct =		STRING_PRODUCT,
349 	.bNumConfigurations =	1,
350 };
351 
352 static struct usb_otg_descriptor
353 otg_descriptor = {
354 	.bLength =		sizeof otg_descriptor,
355 	.bDescriptorType =	USB_DT_OTG,
356 
357 	.bmAttributes =		USB_OTG_SRP,
358 };
359 
360 static struct usb_config_descriptor
361 eth_config = {
362 	.bLength =		sizeof eth_config,
363 	.bDescriptorType =	USB_DT_CONFIG,
364 
365 	/* compute wTotalLength on the fly */
366 	.bNumInterfaces =	2,
367 	.bConfigurationValue =	DEV_CONFIG_VALUE,
368 	.iConfiguration =	STRING_CDC,
369 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
370 	.bMaxPower =		1,
371 };
372 
373 #ifdef	CONFIG_USB_ETH_RNDIS
374 static struct usb_config_descriptor
375 rndis_config = {
376 	.bLength =              sizeof rndis_config,
377 	.bDescriptorType =      USB_DT_CONFIG,
378 
379 	/* compute wTotalLength on the fly */
380 	.bNumInterfaces =       2,
381 	.bConfigurationValue =  DEV_RNDIS_CONFIG_VALUE,
382 	.iConfiguration =       STRING_RNDIS,
383 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
384 	.bMaxPower =            1,
385 };
386 #endif
387 
388 /*
389  * Compared to the simple CDC subset, the full CDC Ethernet model adds
390  * three class descriptors, two interface descriptors, optional status
391  * endpoint.  Both have a "data" interface and two bulk endpoints.
392  * There are also differences in how control requests are handled.
393  *
394  * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
395  * CDC-ACM (modem) spec.  Unfortunately MSFT's RNDIS driver is buggy; it
396  * may hang or oops.  Since bugfixes (or accurate specs, letting Linux
397  * work around those bugs) are unlikely to ever come from MSFT, you may
398  * wish to avoid using RNDIS.
399  *
400  * MCCI offers an alternative to RNDIS if you need to connect to Windows
401  * but have hardware that can't support CDC Ethernet.   We add descriptors
402  * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
403  * "SAFE".  That borrows from both CDC Ethernet and CDC MDLM.  You can
404  * get those drivers from MCCI, or bundled with various products.
405  */
406 
407 #ifdef	CONFIG_USB_ETH_CDC
408 static struct usb_interface_descriptor
409 control_intf = {
410 	.bLength =		sizeof control_intf,
411 	.bDescriptorType =	USB_DT_INTERFACE,
412 
413 	.bInterfaceNumber =	0,
414 	/* status endpoint is optional; this may be patched later */
415 	.bNumEndpoints =	1,
416 	.bInterfaceClass =	USB_CLASS_COMM,
417 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
418 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
419 	.iInterface =		STRING_CONTROL,
420 };
421 #endif
422 
423 #ifdef	CONFIG_USB_ETH_RNDIS
424 static const struct usb_interface_descriptor
425 rndis_control_intf = {
426 	.bLength =              sizeof rndis_control_intf,
427 	.bDescriptorType =      USB_DT_INTERFACE,
428 
429 	.bInterfaceNumber =     0,
430 	.bNumEndpoints =        1,
431 	.bInterfaceClass =      USB_CLASS_COMM,
432 	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
433 	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
434 	.iInterface =           STRING_RNDIS_CONTROL,
435 };
436 #endif
437 
438 static const struct usb_cdc_header_desc header_desc = {
439 	.bLength =		sizeof header_desc,
440 	.bDescriptorType =	USB_DT_CS_INTERFACE,
441 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
442 
443 	.bcdCDC =		__constant_cpu_to_le16(0x0110),
444 };
445 
446 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
447 
448 static const struct usb_cdc_union_desc union_desc = {
449 	.bLength =		sizeof union_desc,
450 	.bDescriptorType =	USB_DT_CS_INTERFACE,
451 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
452 
453 	.bMasterInterface0 =	0,	/* index of control interface */
454 	.bSlaveInterface0 =	1,	/* index of DATA interface */
455 };
456 
457 #endif	/* CDC || RNDIS */
458 
459 #ifdef	CONFIG_USB_ETH_RNDIS
460 
461 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
462 	.bLength =		sizeof call_mgmt_descriptor,
463 	.bDescriptorType =	USB_DT_CS_INTERFACE,
464 	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
465 
466 	.bmCapabilities =	0x00,
467 	.bDataInterface =	0x01,
468 };
469 
470 static const struct usb_cdc_acm_descriptor acm_descriptor = {
471 	.bLength =		sizeof acm_descriptor,
472 	.bDescriptorType =	USB_DT_CS_INTERFACE,
473 	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
474 
475 	.bmCapabilities =	0x00,
476 };
477 
478 #endif
479 
480 #ifndef CONFIG_USB_ETH_CDC
481 
482 /*
483  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
484  * ways:  data endpoints live in the control interface, there's no data
485  * interface, and it's not used to talk to a cell phone radio.
486  */
487 
488 static const struct usb_cdc_mdlm_desc mdlm_desc = {
489 	.bLength =		sizeof mdlm_desc,
490 	.bDescriptorType =	USB_DT_CS_INTERFACE,
491 	.bDescriptorSubType =	USB_CDC_MDLM_TYPE,
492 
493 	.bcdVersion =		__constant_cpu_to_le16(0x0100),
494 	.bGUID = {
495 		0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
496 		0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
497 	},
498 };
499 
500 /*
501  * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
502  * can't really use its struct.  All we do here is say that we're using
503  * the submode of "SAFE" which directly matches the CDC Subset.
504  */
505 static const u8 mdlm_detail_desc[] = {
506 	6,
507 	USB_DT_CS_INTERFACE,
508 	USB_CDC_MDLM_DETAIL_TYPE,
509 
510 	0,	/* "SAFE" */
511 	0,	/* network control capabilities (none) */
512 	0,	/* network data capabilities ("raw" encapsulation) */
513 };
514 
515 #endif
516 
517 static const struct usb_cdc_ether_desc ether_desc = {
518 	.bLength =		sizeof(ether_desc),
519 	.bDescriptorType =	USB_DT_CS_INTERFACE,
520 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
521 
522 	/* this descriptor actually adds value, surprise! */
523 	.iMACAddress =		STRING_ETHADDR,
524 	.bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
525 	.wMaxSegmentSize =	__constant_cpu_to_le16(ETH_FRAME_LEN),
526 	.wNumberMCFilters =	__constant_cpu_to_le16(0),
527 	.bNumberPowerFilters =	0,
528 };
529 
530 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
531 
532 /*
533  * include the status endpoint if we can, even where it's optional.
534  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
535  * packet, to simplify cancellation; and a big transfer interval, to
536  * waste less bandwidth.
537  *
538  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
539  * if they ignore the connect/disconnect notifications that real aether
540  * can provide.  more advanced cdc configurations might want to support
541  * encapsulated commands (vendor-specific, using control-OUT).
542  *
543  * RNDIS requires the status endpoint, since it uses that encapsulation
544  * mechanism for its funky RPC scheme.
545  */
546 
547 #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
548 #define STATUS_BYTECOUNT		16	/* 8 byte header + data */
549 
550 static struct usb_endpoint_descriptor
551 fs_status_desc = {
552 	.bLength =		USB_DT_ENDPOINT_SIZE,
553 	.bDescriptorType =	USB_DT_ENDPOINT,
554 
555 	.bEndpointAddress =	USB_DIR_IN,
556 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
557 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
558 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
559 };
560 #endif
561 
562 #ifdef	CONFIG_USB_ETH_CDC
563 
564 /* the default data interface has no endpoints ... */
565 
566 static const struct usb_interface_descriptor
567 data_nop_intf = {
568 	.bLength =		sizeof data_nop_intf,
569 	.bDescriptorType =	USB_DT_INTERFACE,
570 
571 	.bInterfaceNumber =	1,
572 	.bAlternateSetting =	0,
573 	.bNumEndpoints =	0,
574 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
575 	.bInterfaceSubClass =	0,
576 	.bInterfaceProtocol =	0,
577 };
578 
579 /* ... but the "real" data interface has two bulk endpoints */
580 
581 static const struct usb_interface_descriptor
582 data_intf = {
583 	.bLength =		sizeof data_intf,
584 	.bDescriptorType =	USB_DT_INTERFACE,
585 
586 	.bInterfaceNumber =	1,
587 	.bAlternateSetting =	1,
588 	.bNumEndpoints =	2,
589 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
590 	.bInterfaceSubClass =	0,
591 	.bInterfaceProtocol =	0,
592 	.iInterface =		STRING_DATA,
593 };
594 
595 #endif
596 
597 #ifdef	CONFIG_USB_ETH_RNDIS
598 
599 /* RNDIS doesn't activate by changing to the "real" altsetting */
600 
601 static const struct usb_interface_descriptor
602 rndis_data_intf = {
603 	.bLength =		sizeof rndis_data_intf,
604 	.bDescriptorType =	USB_DT_INTERFACE,
605 
606 	.bInterfaceNumber =	1,
607 	.bAlternateSetting =	0,
608 	.bNumEndpoints =	2,
609 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
610 	.bInterfaceSubClass =	0,
611 	.bInterfaceProtocol =	0,
612 	.iInterface =		STRING_DATA,
613 };
614 
615 #endif
616 
617 #ifdef CONFIG_USB_ETH_SUBSET
618 
619 /*
620  * "Simple" CDC-subset option is a simple vendor-neutral model that most
621  * full speed controllers can handle:  one interface, two bulk endpoints.
622  *
623  * To assist host side drivers, we fancy it up a bit, and add descriptors
624  * so some host side drivers will understand it as a "SAFE" variant.
625  */
626 
627 static const struct usb_interface_descriptor
628 subset_data_intf = {
629 	.bLength =		sizeof subset_data_intf,
630 	.bDescriptorType =	USB_DT_INTERFACE,
631 
632 	.bInterfaceNumber =	0,
633 	.bAlternateSetting =	0,
634 	.bNumEndpoints =	2,
635 	.bInterfaceClass =      USB_CLASS_COMM,
636 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_MDLM,
637 	.bInterfaceProtocol =	0,
638 	.iInterface =		STRING_DATA,
639 };
640 
641 #endif	/* SUBSET */
642 
643 static struct usb_endpoint_descriptor
644 fs_source_desc = {
645 	.bLength =		USB_DT_ENDPOINT_SIZE,
646 	.bDescriptorType =	USB_DT_ENDPOINT,
647 
648 	.bEndpointAddress =	USB_DIR_IN,
649 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
650 };
651 
652 static struct usb_endpoint_descriptor
653 fs_sink_desc = {
654 	.bLength =		USB_DT_ENDPOINT_SIZE,
655 	.bDescriptorType =	USB_DT_ENDPOINT,
656 
657 	.bEndpointAddress =	USB_DIR_OUT,
658 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
659 };
660 
661 static const struct usb_descriptor_header *fs_eth_function[11] = {
662 	(struct usb_descriptor_header *) &otg_descriptor,
663 #ifdef CONFIG_USB_ETH_CDC
664 	/* "cdc" mode descriptors */
665 	(struct usb_descriptor_header *) &control_intf,
666 	(struct usb_descriptor_header *) &header_desc,
667 	(struct usb_descriptor_header *) &union_desc,
668 	(struct usb_descriptor_header *) &ether_desc,
669 	/* NOTE: status endpoint may need to be removed */
670 	(struct usb_descriptor_header *) &fs_status_desc,
671 	/* data interface, with altsetting */
672 	(struct usb_descriptor_header *) &data_nop_intf,
673 	(struct usb_descriptor_header *) &data_intf,
674 	(struct usb_descriptor_header *) &fs_source_desc,
675 	(struct usb_descriptor_header *) &fs_sink_desc,
676 	NULL,
677 #endif /* CONFIG_USB_ETH_CDC */
678 };
679 
680 static inline void fs_subset_descriptors(void)
681 {
682 #ifdef CONFIG_USB_ETH_SUBSET
683 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
684 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
685 	fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
686 	fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
687 	fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
688 	fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
689 	fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
690 	fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
691 	fs_eth_function[8] = NULL;
692 #else
693 	fs_eth_function[1] = NULL;
694 #endif
695 }
696 
697 #ifdef	CONFIG_USB_ETH_RNDIS
698 static const struct usb_descriptor_header *fs_rndis_function[] = {
699 	(struct usb_descriptor_header *) &otg_descriptor,
700 	/* control interface matches ACM, not Ethernet */
701 	(struct usb_descriptor_header *) &rndis_control_intf,
702 	(struct usb_descriptor_header *) &header_desc,
703 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
704 	(struct usb_descriptor_header *) &acm_descriptor,
705 	(struct usb_descriptor_header *) &union_desc,
706 	(struct usb_descriptor_header *) &fs_status_desc,
707 	/* data interface has no altsetting */
708 	(struct usb_descriptor_header *) &rndis_data_intf,
709 	(struct usb_descriptor_header *) &fs_source_desc,
710 	(struct usb_descriptor_header *) &fs_sink_desc,
711 	NULL,
712 };
713 #endif
714 
715 /*
716  * usb 2.0 devices need to expose both high speed and full speed
717  * descriptors, unless they only run at full speed.
718  */
719 
720 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
721 static struct usb_endpoint_descriptor
722 hs_status_desc = {
723 	.bLength =		USB_DT_ENDPOINT_SIZE,
724 	.bDescriptorType =	USB_DT_ENDPOINT,
725 
726 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
727 	.wMaxPacketSize =	__constant_cpu_to_le16(STATUS_BYTECOUNT),
728 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
729 };
730 #endif /* CONFIG_USB_ETH_CDC */
731 
732 static struct usb_endpoint_descriptor
733 hs_source_desc = {
734 	.bLength =		USB_DT_ENDPOINT_SIZE,
735 	.bDescriptorType =	USB_DT_ENDPOINT,
736 
737 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
738 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
739 };
740 
741 static struct usb_endpoint_descriptor
742 hs_sink_desc = {
743 	.bLength =		USB_DT_ENDPOINT_SIZE,
744 	.bDescriptorType =	USB_DT_ENDPOINT,
745 
746 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
747 	.wMaxPacketSize =	__constant_cpu_to_le16(512),
748 };
749 
750 static struct usb_qualifier_descriptor
751 dev_qualifier = {
752 	.bLength =		sizeof dev_qualifier,
753 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
754 
755 	.bcdUSB =		__constant_cpu_to_le16(0x0200),
756 	.bDeviceClass =		USB_CLASS_COMM,
757 
758 	.bNumConfigurations =	1,
759 };
760 
761 static const struct usb_descriptor_header *hs_eth_function[11] = {
762 	(struct usb_descriptor_header *) &otg_descriptor,
763 #ifdef CONFIG_USB_ETH_CDC
764 	/* "cdc" mode descriptors */
765 	(struct usb_descriptor_header *) &control_intf,
766 	(struct usb_descriptor_header *) &header_desc,
767 	(struct usb_descriptor_header *) &union_desc,
768 	(struct usb_descriptor_header *) &ether_desc,
769 	/* NOTE: status endpoint may need to be removed */
770 	(struct usb_descriptor_header *) &hs_status_desc,
771 	/* data interface, with altsetting */
772 	(struct usb_descriptor_header *) &data_nop_intf,
773 	(struct usb_descriptor_header *) &data_intf,
774 	(struct usb_descriptor_header *) &hs_source_desc,
775 	(struct usb_descriptor_header *) &hs_sink_desc,
776 	NULL,
777 #endif /* CONFIG_USB_ETH_CDC */
778 };
779 
780 static inline void hs_subset_descriptors(void)
781 {
782 #ifdef CONFIG_USB_ETH_SUBSET
783 	/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
784 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
785 	hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
786 	hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
787 	hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
788 	hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
789 	hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
790 	hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
791 	hs_eth_function[8] = NULL;
792 #else
793 	hs_eth_function[1] = NULL;
794 #endif
795 }
796 
797 #ifdef	CONFIG_USB_ETH_RNDIS
798 static const struct usb_descriptor_header *hs_rndis_function[] = {
799 	(struct usb_descriptor_header *) &otg_descriptor,
800 	/* control interface matches ACM, not Ethernet */
801 	(struct usb_descriptor_header *) &rndis_control_intf,
802 	(struct usb_descriptor_header *) &header_desc,
803 	(struct usb_descriptor_header *) &call_mgmt_descriptor,
804 	(struct usb_descriptor_header *) &acm_descriptor,
805 	(struct usb_descriptor_header *) &union_desc,
806 	(struct usb_descriptor_header *) &hs_status_desc,
807 	/* data interface has no altsetting */
808 	(struct usb_descriptor_header *) &rndis_data_intf,
809 	(struct usb_descriptor_header *) &hs_source_desc,
810 	(struct usb_descriptor_header *) &hs_sink_desc,
811 	NULL,
812 };
813 #endif
814 
815 
816 /* maxpacket and other transfer characteristics vary by speed. */
817 static inline struct usb_endpoint_descriptor *
818 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
819 		struct usb_endpoint_descriptor *fs)
820 {
821 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
822 		return hs;
823 	return fs;
824 }
825 
826 /*-------------------------------------------------------------------------*/
827 
828 /* descriptors that are built on-demand */
829 
830 static char manufacturer[50];
831 static char product_desc[40] = DRIVER_DESC;
832 static char serial_number[20];
833 
834 /* address that the host will use ... usually assigned at random */
835 static char ethaddr[2 * ETH_ALEN + 1];
836 
837 /* static strings, in UTF-8 */
838 static struct usb_string		strings[] = {
839 	{ STRING_MANUFACTURER,	manufacturer, },
840 	{ STRING_PRODUCT,	product_desc, },
841 	{ STRING_SERIALNUMBER,	serial_number, },
842 	{ STRING_DATA,		"Ethernet Data", },
843 	{ STRING_ETHADDR,	ethaddr, },
844 #ifdef	CONFIG_USB_ETH_CDC
845 	{ STRING_CDC,		"CDC Ethernet", },
846 	{ STRING_CONTROL,	"CDC Communications Control", },
847 #endif
848 #ifdef	CONFIG_USB_ETH_SUBSET
849 	{ STRING_SUBSET,	"CDC Ethernet Subset", },
850 #endif
851 #ifdef	CONFIG_USB_ETH_RNDIS
852 	{ STRING_RNDIS,		"RNDIS", },
853 	{ STRING_RNDIS_CONTROL,	"RNDIS Communications Control", },
854 #endif
855 	{  }		/* end of list */
856 };
857 
858 static struct usb_gadget_strings	stringtab = {
859 	.language	= 0x0409,	/* en-us */
860 	.strings	= strings,
861 };
862 
863 /*============================================================================*/
864 static u8 control_req[USB_BUFSIZ];
865 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
866 static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
867 #endif
868 
869 
870 /**
871  * strlcpy - Copy a %NUL terminated string into a sized buffer
872  * @dest: Where to copy the string to
873  * @src: Where to copy the string from
874  * @size: size of destination buffer
875  *
876  * Compatible with *BSD: the result is always a valid
877  * NUL-terminated string that fits in the buffer (unless,
878  * of course, the buffer size is zero). It does not pad
879  * out the result like strncpy() does.
880  */
881 size_t strlcpy(char *dest, const char *src, size_t size)
882 {
883 	size_t ret = strlen(src);
884 
885 	if (size) {
886 		size_t len = (ret >= size) ? size - 1 : ret;
887 		memcpy(dest, src, len);
888 		dest[len] = '\0';
889 	}
890 	return ret;
891 }
892 
893 /*============================================================================*/
894 
895 /*
896  * one config, two interfaces:  control, data.
897  * complications: class descriptors, and an altsetting.
898  */
899 static int
900 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
901 {
902 	int					len;
903 	const struct usb_config_descriptor	*config;
904 	const struct usb_descriptor_header	**function;
905 	int					hs = 0;
906 
907 	if (gadget_is_dualspeed(g)) {
908 		hs = (g->speed == USB_SPEED_HIGH);
909 		if (type == USB_DT_OTHER_SPEED_CONFIG)
910 			hs = !hs;
911 	}
912 #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function)
913 
914 	if (index >= device_desc.bNumConfigurations)
915 		return -EINVAL;
916 
917 #ifdef	CONFIG_USB_ETH_RNDIS
918 	/*
919 	 * list the RNDIS config first, to make Microsoft's drivers
920 	 * happy. DOCSIS 1.0 needs this too.
921 	 */
922 	if (device_desc.bNumConfigurations == 2 && index == 0) {
923 		config = &rndis_config;
924 		function = which_fn(rndis);
925 	} else
926 #endif
927 	{
928 		config = &eth_config;
929 		function = which_fn(eth);
930 	}
931 
932 	/* for now, don't advertise srp-only devices */
933 	if (!is_otg)
934 		function++;
935 
936 	len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
937 	if (len < 0)
938 		return len;
939 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
940 	return len;
941 }
942 
943 /*-------------------------------------------------------------------------*/
944 
945 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
946 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
947 
948 static int
949 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
950 {
951 	int					result = 0;
952 	struct usb_gadget			*gadget = dev->gadget;
953 
954 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
955 	/* status endpoint used for RNDIS and (optionally) CDC */
956 	if (!subset_active(dev) && dev->status_ep) {
957 		dev->status = ep_desc(gadget, &hs_status_desc,
958 						&fs_status_desc);
959 		dev->status_ep->driver_data = dev;
960 
961 		result = usb_ep_enable(dev->status_ep, dev->status);
962 		if (result != 0) {
963 			debug("enable %s --> %d\n",
964 				dev->status_ep->name, result);
965 			goto done;
966 		}
967 	}
968 #endif
969 
970 	dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
971 	dev->in_ep->driver_data = dev;
972 
973 	dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
974 	dev->out_ep->driver_data = dev;
975 
976 	/*
977 	 * With CDC,  the host isn't allowed to use these two data
978 	 * endpoints in the default altsetting for the interface.
979 	 * so we don't activate them yet.  Reset from SET_INTERFACE.
980 	 *
981 	 * Strictly speaking RNDIS should work the same: activation is
982 	 * a side effect of setting a packet filter.  Deactivation is
983 	 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
984 	 */
985 	if (!cdc_active(dev)) {
986 		result = usb_ep_enable(dev->in_ep, dev->in);
987 		if (result != 0) {
988 			debug("enable %s --> %d\n",
989 				dev->in_ep->name, result);
990 			goto done;
991 		}
992 
993 		result = usb_ep_enable(dev->out_ep, dev->out);
994 		if (result != 0) {
995 			debug("enable %s --> %d\n",
996 				dev->out_ep->name, result);
997 			goto done;
998 		}
999 	}
1000 
1001 done:
1002 	if (result == 0)
1003 		result = alloc_requests(dev, qlen(gadget), gfp_flags);
1004 
1005 	/* on error, disable any endpoints  */
1006 	if (result < 0) {
1007 		if (!subset_active(dev) && dev->status_ep)
1008 			(void) usb_ep_disable(dev->status_ep);
1009 		dev->status = NULL;
1010 		(void) usb_ep_disable(dev->in_ep);
1011 		(void) usb_ep_disable(dev->out_ep);
1012 		dev->in = NULL;
1013 		dev->out = NULL;
1014 	} else if (!cdc_active(dev)) {
1015 		/*
1016 		 * activate non-CDC configs right away
1017 		 * this isn't strictly according to the RNDIS spec
1018 		 */
1019 		eth_start(dev, GFP_ATOMIC);
1020 	}
1021 
1022 	/* caller is responsible for cleanup on error */
1023 	return result;
1024 }
1025 
1026 static void eth_reset_config(struct eth_dev *dev)
1027 {
1028 	if (dev->config == 0)
1029 		return;
1030 
1031 	debug("%s\n", __func__);
1032 
1033 	rndis_uninit(dev->rndis_config);
1034 
1035 	/*
1036 	 * disable endpoints, forcing (synchronous) completion of
1037 	 * pending i/o.  then free the requests.
1038 	 */
1039 
1040 	if (dev->in) {
1041 		usb_ep_disable(dev->in_ep);
1042 		if (dev->tx_req) {
1043 			usb_ep_free_request(dev->in_ep, dev->tx_req);
1044 			dev->tx_req = NULL;
1045 		}
1046 	}
1047 	if (dev->out) {
1048 		usb_ep_disable(dev->out_ep);
1049 		if (dev->rx_req) {
1050 			usb_ep_free_request(dev->out_ep, dev->rx_req);
1051 			dev->rx_req = NULL;
1052 		}
1053 	}
1054 	if (dev->status)
1055 		usb_ep_disable(dev->status_ep);
1056 
1057 	dev->rndis = 0;
1058 	dev->cdc_filter = 0;
1059 	dev->config = 0;
1060 }
1061 
1062 /*
1063  * change our operational config.  must agree with the code
1064  * that returns config descriptors, and altsetting code.
1065  */
1066 static int eth_set_config(struct eth_dev *dev, unsigned number,
1067 				gfp_t gfp_flags)
1068 {
1069 	int			result = 0;
1070 	struct usb_gadget	*gadget = dev->gadget;
1071 
1072 	if (gadget_is_sa1100(gadget)
1073 			&& dev->config
1074 			&& dev->tx_qlen != 0) {
1075 		/* tx fifo is full, but we can't clear it...*/
1076 		error("can't change configurations");
1077 		return -ESPIPE;
1078 	}
1079 	eth_reset_config(dev);
1080 
1081 	switch (number) {
1082 	case DEV_CONFIG_VALUE:
1083 		result = set_ether_config(dev, gfp_flags);
1084 		break;
1085 #ifdef	CONFIG_USB_ETH_RNDIS
1086 	case DEV_RNDIS_CONFIG_VALUE:
1087 		dev->rndis = 1;
1088 		result = set_ether_config(dev, gfp_flags);
1089 		break;
1090 #endif
1091 	default:
1092 		result = -EINVAL;
1093 		/* FALL THROUGH */
1094 	case 0:
1095 		break;
1096 	}
1097 
1098 	if (result) {
1099 		if (number)
1100 			eth_reset_config(dev);
1101 		usb_gadget_vbus_draw(dev->gadget,
1102 				gadget_is_otg(dev->gadget) ? 8 : 100);
1103 	} else {
1104 		char *speed;
1105 		unsigned power;
1106 
1107 		power = 2 * eth_config.bMaxPower;
1108 		usb_gadget_vbus_draw(dev->gadget, power);
1109 
1110 		switch (gadget->speed) {
1111 		case USB_SPEED_FULL:
1112 			speed = "full"; break;
1113 #ifdef CONFIG_USB_GADGET_DUALSPEED
1114 		case USB_SPEED_HIGH:
1115 			speed = "high"; break;
1116 #endif
1117 		default:
1118 			speed = "?"; break;
1119 		}
1120 
1121 		dev->config = number;
1122 		printf("%s speed config #%d: %d mA, %s, using %s\n",
1123 				speed, number, power, driver_desc,
1124 				rndis_active(dev)
1125 					? "RNDIS"
1126 					: (cdc_active(dev)
1127 						? "CDC Ethernet"
1128 						: "CDC Ethernet Subset"));
1129 	}
1130 	return result;
1131 }
1132 
1133 /*-------------------------------------------------------------------------*/
1134 
1135 #ifdef	CONFIG_USB_ETH_CDC
1136 
1137 /*
1138  * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1139  * only to notify the host about link status changes (which we support) or
1140  * report completion of some encapsulated command (as used in RNDIS).  Since
1141  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1142  * command mechanism; and only one status request is ever queued.
1143  */
1144 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1145 {
1146 	struct usb_cdc_notification	*event = req->buf;
1147 	int				value = req->status;
1148 	struct eth_dev			*dev = ep->driver_data;
1149 
1150 	/* issue the second notification if host reads the first */
1151 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1152 			&& value == 0) {
1153 		__le32	*data = req->buf + sizeof *event;
1154 
1155 		event->bmRequestType = 0xA1;
1156 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1157 		event->wValue = __constant_cpu_to_le16(0);
1158 		event->wIndex = __constant_cpu_to_le16(1);
1159 		event->wLength = __constant_cpu_to_le16(8);
1160 
1161 		/* SPEED_CHANGE data is up/down speeds in bits/sec */
1162 		data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1163 
1164 		req->length = STATUS_BYTECOUNT;
1165 		value = usb_ep_queue(ep, req, GFP_ATOMIC);
1166 		debug("send SPEED_CHANGE --> %d\n", value);
1167 		if (value == 0)
1168 			return;
1169 	} else if (value != -ECONNRESET) {
1170 		debug("event %02x --> %d\n",
1171 			event->bNotificationType, value);
1172 		if (event->bNotificationType ==
1173 				USB_CDC_NOTIFY_SPEED_CHANGE) {
1174 			l_ethdev.network_started = 1;
1175 			printf("USB network up!\n");
1176 		}
1177 	}
1178 	req->context = NULL;
1179 }
1180 
1181 static void issue_start_status(struct eth_dev *dev)
1182 {
1183 	struct usb_request		*req = dev->stat_req;
1184 	struct usb_cdc_notification	*event;
1185 	int				value;
1186 
1187 	/*
1188 	 * flush old status
1189 	 *
1190 	 * FIXME ugly idiom, maybe we'd be better with just
1191 	 * a "cancel the whole queue" primitive since any
1192 	 * unlink-one primitive has way too many error modes.
1193 	 * here, we "know" toggle is already clear...
1194 	 *
1195 	 * FIXME iff req->context != null just dequeue it
1196 	 */
1197 	usb_ep_disable(dev->status_ep);
1198 	usb_ep_enable(dev->status_ep, dev->status);
1199 
1200 	/*
1201 	 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1202 	 * a SPEED_CHANGE.  could be useful in some configs.
1203 	 */
1204 	event = req->buf;
1205 	event->bmRequestType = 0xA1;
1206 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1207 	event->wValue = __constant_cpu_to_le16(1);	/* connected */
1208 	event->wIndex = __constant_cpu_to_le16(1);
1209 	event->wLength = 0;
1210 
1211 	req->length = sizeof *event;
1212 	req->complete = eth_status_complete;
1213 	req->context = dev;
1214 
1215 	value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1216 	if (value < 0)
1217 		debug("status buf queue --> %d\n", value);
1218 }
1219 
1220 #endif
1221 
1222 /*-------------------------------------------------------------------------*/
1223 
1224 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1225 {
1226 	if (req->status || req->actual != req->length)
1227 		debug("setup complete --> %d, %d/%d\n",
1228 				req->status, req->actual, req->length);
1229 }
1230 
1231 #ifdef CONFIG_USB_ETH_RNDIS
1232 
1233 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1234 {
1235 	if (req->status || req->actual != req->length)
1236 		debug("rndis response complete --> %d, %d/%d\n",
1237 			req->status, req->actual, req->length);
1238 
1239 	/* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1240 }
1241 
1242 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1243 {
1244 	struct eth_dev          *dev = ep->driver_data;
1245 	int			status;
1246 
1247 	/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1248 	status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1249 	if (status < 0)
1250 		error("%s: rndis parse error %d", __func__, status);
1251 }
1252 
1253 #endif	/* RNDIS */
1254 
1255 /*
1256  * The setup() callback implements all the ep0 functionality that's not
1257  * handled lower down.  CDC has a number of less-common features:
1258  *
1259  *  - two interfaces:  control, and ethernet data
1260  *  - Ethernet data interface has two altsettings:  default, and active
1261  *  - class-specific descriptors for the control interface
1262  *  - class-specific control requests
1263  */
1264 static int
1265 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1266 {
1267 	struct eth_dev		*dev = get_gadget_data(gadget);
1268 	struct usb_request	*req = dev->req;
1269 	int			value = -EOPNOTSUPP;
1270 	u16			wIndex = le16_to_cpu(ctrl->wIndex);
1271 	u16			wValue = le16_to_cpu(ctrl->wValue);
1272 	u16			wLength = le16_to_cpu(ctrl->wLength);
1273 
1274 	/*
1275 	 * descriptors just go into the pre-allocated ep0 buffer,
1276 	 * while config change events may enable network traffic.
1277 	 */
1278 
1279 	debug("%s\n", __func__);
1280 
1281 	req->complete = eth_setup_complete;
1282 	switch (ctrl->bRequest) {
1283 
1284 	case USB_REQ_GET_DESCRIPTOR:
1285 		if (ctrl->bRequestType != USB_DIR_IN)
1286 			break;
1287 		switch (wValue >> 8) {
1288 
1289 		case USB_DT_DEVICE:
1290 			value = min(wLength, (u16) sizeof device_desc);
1291 			memcpy(req->buf, &device_desc, value);
1292 			break;
1293 		case USB_DT_DEVICE_QUALIFIER:
1294 			if (!gadget_is_dualspeed(gadget))
1295 				break;
1296 			value = min(wLength, (u16) sizeof dev_qualifier);
1297 			memcpy(req->buf, &dev_qualifier, value);
1298 			break;
1299 
1300 		case USB_DT_OTHER_SPEED_CONFIG:
1301 			if (!gadget_is_dualspeed(gadget))
1302 				break;
1303 			/* FALLTHROUGH */
1304 		case USB_DT_CONFIG:
1305 			value = config_buf(gadget, req->buf,
1306 					wValue >> 8,
1307 					wValue & 0xff,
1308 					gadget_is_otg(gadget));
1309 			if (value >= 0)
1310 				value = min(wLength, (u16) value);
1311 			break;
1312 
1313 		case USB_DT_STRING:
1314 			value = usb_gadget_get_string(&stringtab,
1315 					wValue & 0xff, req->buf);
1316 
1317 			if (value >= 0)
1318 				value = min(wLength, (u16) value);
1319 
1320 			break;
1321 		}
1322 		break;
1323 
1324 	case USB_REQ_SET_CONFIGURATION:
1325 		if (ctrl->bRequestType != 0)
1326 			break;
1327 		if (gadget->a_hnp_support)
1328 			debug("HNP available\n");
1329 		else if (gadget->a_alt_hnp_support)
1330 			debug("HNP needs a different root port\n");
1331 		value = eth_set_config(dev, wValue, GFP_ATOMIC);
1332 		break;
1333 	case USB_REQ_GET_CONFIGURATION:
1334 		if (ctrl->bRequestType != USB_DIR_IN)
1335 			break;
1336 		*(u8 *)req->buf = dev->config;
1337 		value = min(wLength, (u16) 1);
1338 		break;
1339 
1340 	case USB_REQ_SET_INTERFACE:
1341 		if (ctrl->bRequestType != USB_RECIP_INTERFACE
1342 				|| !dev->config
1343 				|| wIndex > 1)
1344 			break;
1345 		if (!cdc_active(dev) && wIndex != 0)
1346 			break;
1347 
1348 		/*
1349 		 * PXA hardware partially handles SET_INTERFACE;
1350 		 * we need to kluge around that interference.
1351 		 */
1352 		if (gadget_is_pxa(gadget)) {
1353 			value = eth_set_config(dev, DEV_CONFIG_VALUE,
1354 						GFP_ATOMIC);
1355 			/*
1356 			 * PXA25x driver use non-CDC ethernet gadget.
1357 			 * But only _CDC and _RNDIS code can signalize
1358 			 * that network is working. So we signalize it
1359 			 * here.
1360 			 */
1361 			l_ethdev.network_started = 1;
1362 			debug("USB network up!\n");
1363 			goto done_set_intf;
1364 		}
1365 
1366 #ifdef CONFIG_USB_ETH_CDC
1367 		switch (wIndex) {
1368 		case 0:		/* control/master intf */
1369 			if (wValue != 0)
1370 				break;
1371 			if (dev->status) {
1372 				usb_ep_disable(dev->status_ep);
1373 				usb_ep_enable(dev->status_ep, dev->status);
1374 			}
1375 
1376 			value = 0;
1377 			break;
1378 		case 1:		/* data intf */
1379 			if (wValue > 1)
1380 				break;
1381 			usb_ep_disable(dev->in_ep);
1382 			usb_ep_disable(dev->out_ep);
1383 
1384 			/*
1385 			 * CDC requires the data transfers not be done from
1386 			 * the default interface setting ... also, setting
1387 			 * the non-default interface resets filters etc.
1388 			 */
1389 			if (wValue == 1) {
1390 				if (!cdc_active(dev))
1391 					break;
1392 				usb_ep_enable(dev->in_ep, dev->in);
1393 				usb_ep_enable(dev->out_ep, dev->out);
1394 				dev->cdc_filter = DEFAULT_FILTER;
1395 				if (dev->status)
1396 					issue_start_status(dev);
1397 				eth_start(dev, GFP_ATOMIC);
1398 			}
1399 			value = 0;
1400 			break;
1401 		}
1402 #else
1403 		/*
1404 		 * FIXME this is wrong, as is the assumption that
1405 		 * all non-PXA hardware talks real CDC ...
1406 		 */
1407 		debug("set_interface ignored!\n");
1408 #endif /* CONFIG_USB_ETH_CDC */
1409 
1410 done_set_intf:
1411 		break;
1412 	case USB_REQ_GET_INTERFACE:
1413 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1414 				|| !dev->config
1415 				|| wIndex > 1)
1416 			break;
1417 		if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1418 			break;
1419 
1420 		/* for CDC, iff carrier is on, data interface is active. */
1421 		if (rndis_active(dev) || wIndex != 1)
1422 			*(u8 *)req->buf = 0;
1423 		else {
1424 			/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1425 			/* carrier always ok ...*/
1426 			*(u8 *)req->buf = 1 ;
1427 		}
1428 		value = min(wLength, (u16) 1);
1429 		break;
1430 
1431 #ifdef CONFIG_USB_ETH_CDC
1432 	case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1433 		/*
1434 		 * see 6.2.30: no data, wIndex = interface,
1435 		 * wValue = packet filter bitmap
1436 		 */
1437 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1438 				|| !cdc_active(dev)
1439 				|| wLength != 0
1440 				|| wIndex > 1)
1441 			break;
1442 		debug("packet filter %02x\n", wValue);
1443 		dev->cdc_filter = wValue;
1444 		value = 0;
1445 		break;
1446 
1447 	/*
1448 	 * and potentially:
1449 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1450 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1451 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1452 	 * case USB_CDC_GET_ETHERNET_STATISTIC:
1453 	 */
1454 
1455 #endif /* CONFIG_USB_ETH_CDC */
1456 
1457 #ifdef CONFIG_USB_ETH_RNDIS
1458 	/*
1459 	 * RNDIS uses the CDC command encapsulation mechanism to implement
1460 	 * an RPC scheme, with much getting/setting of attributes by OID.
1461 	 */
1462 	case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1463 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1464 				|| !rndis_active(dev)
1465 				|| wLength > USB_BUFSIZ
1466 				|| wValue
1467 				|| rndis_control_intf.bInterfaceNumber
1468 					!= wIndex)
1469 			break;
1470 		/* read the request, then process it */
1471 		value = wLength;
1472 		req->complete = rndis_command_complete;
1473 		/* later, rndis_control_ack () sends a notification */
1474 		break;
1475 
1476 	case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1477 		if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1478 					== ctrl->bRequestType
1479 				&& rndis_active(dev)
1480 				/* && wLength >= 0x0400 */
1481 				&& !wValue
1482 				&& rndis_control_intf.bInterfaceNumber
1483 					== wIndex) {
1484 			u8 *buf;
1485 			u32 n;
1486 
1487 			/* return the result */
1488 			buf = rndis_get_next_response(dev->rndis_config, &n);
1489 			if (buf) {
1490 				memcpy(req->buf, buf, n);
1491 				req->complete = rndis_response_complete;
1492 				rndis_free_response(dev->rndis_config, buf);
1493 				value = n;
1494 			}
1495 			/* else stalls ... spec says to avoid that */
1496 		}
1497 		break;
1498 #endif	/* RNDIS */
1499 
1500 	default:
1501 		debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1502 			ctrl->bRequestType, ctrl->bRequest,
1503 			wValue, wIndex, wLength);
1504 	}
1505 
1506 	/* respond with data transfer before status phase? */
1507 	if (value >= 0) {
1508 		debug("respond with data transfer before status phase\n");
1509 		req->length = value;
1510 		req->zero = value < wLength
1511 				&& (value % gadget->ep0->maxpacket) == 0;
1512 		value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1513 		if (value < 0) {
1514 			debug("ep_queue --> %d\n", value);
1515 			req->status = 0;
1516 			eth_setup_complete(gadget->ep0, req);
1517 		}
1518 	}
1519 
1520 	/* host either stalls (value < 0) or reports success */
1521 	return value;
1522 }
1523 
1524 /*-------------------------------------------------------------------------*/
1525 
1526 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1527 
1528 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1529 				gfp_t gfp_flags)
1530 {
1531 	int			retval = -ENOMEM;
1532 	size_t			size;
1533 
1534 	/*
1535 	 * Padding up to RX_EXTRA handles minor disagreements with host.
1536 	 * Normally we use the USB "terminate on short read" convention;
1537 	 * so allow up to (N*maxpacket), since that memory is normally
1538 	 * already allocated.  Some hardware doesn't deal well with short
1539 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1540 	 * byte off the end (to force hardware errors on overflow).
1541 	 *
1542 	 * RNDIS uses internal framing, and explicitly allows senders to
1543 	 * pad to end-of-packet.  That's potentially nice for speed,
1544 	 * but means receivers can't recover synch on their own.
1545 	 */
1546 
1547 	debug("%s\n", __func__);
1548 
1549 	size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1550 	size += dev->out_ep->maxpacket - 1;
1551 	if (rndis_active(dev))
1552 		size += sizeof(struct rndis_packet_msg_type);
1553 	size -= size % dev->out_ep->maxpacket;
1554 
1555 	/*
1556 	 * Some platforms perform better when IP packets are aligned,
1557 	 * but on at least one, checksumming fails otherwise.  Note:
1558 	 * RNDIS headers involve variable numbers of LE32 values.
1559 	 */
1560 
1561 	req->buf = (u8 *) NetRxPackets[0];
1562 	req->length = size;
1563 	req->complete = rx_complete;
1564 
1565 	retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1566 
1567 	if (retval)
1568 		error("rx submit --> %d", retval);
1569 
1570 	return retval;
1571 }
1572 
1573 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1574 {
1575 	struct eth_dev	*dev = ep->driver_data;
1576 
1577 	debug("%s: status %d\n", __func__, req->status);
1578 	switch (req->status) {
1579 	/* normal completion */
1580 	case 0:
1581 		if (rndis_active(dev)) {
1582 			/* we know MaxPacketsPerTransfer == 1 here */
1583 			int length = rndis_rm_hdr(req->buf, req->actual);
1584 			if (length < 0)
1585 				goto length_err;
1586 			req->length -= length;
1587 			req->actual -= length;
1588 		}
1589 		if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1590 length_err:
1591 			dev->stats.rx_errors++;
1592 			dev->stats.rx_length_errors++;
1593 			debug("rx length %d\n", req->length);
1594 			break;
1595 		}
1596 
1597 		dev->stats.rx_packets++;
1598 		dev->stats.rx_bytes += req->length;
1599 		break;
1600 
1601 	/* software-driven interface shutdown */
1602 	case -ECONNRESET:		/* unlink */
1603 	case -ESHUTDOWN:		/* disconnect etc */
1604 	/* for hardware automagic (such as pxa) */
1605 	case -ECONNABORTED:		/* endpoint reset */
1606 		break;
1607 
1608 	/* data overrun */
1609 	case -EOVERFLOW:
1610 		dev->stats.rx_over_errors++;
1611 		/* FALLTHROUGH */
1612 	default:
1613 		dev->stats.rx_errors++;
1614 		break;
1615 	}
1616 
1617 	packet_received = 1;
1618 }
1619 
1620 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1621 {
1622 
1623 	dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1624 
1625 	if (!dev->tx_req)
1626 		goto fail1;
1627 
1628 	dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1629 
1630 	if (!dev->rx_req)
1631 		goto fail2;
1632 
1633 	return 0;
1634 
1635 fail2:
1636 	usb_ep_free_request(dev->in_ep, dev->tx_req);
1637 fail1:
1638 	error("can't alloc requests");
1639 	return -1;
1640 }
1641 
1642 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1643 {
1644 	struct eth_dev	*dev = ep->driver_data;
1645 
1646 	debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1647 	switch (req->status) {
1648 	default:
1649 		dev->stats.tx_errors++;
1650 		debug("tx err %d\n", req->status);
1651 		/* FALLTHROUGH */
1652 	case -ECONNRESET:		/* unlink */
1653 	case -ESHUTDOWN:		/* disconnect etc */
1654 		break;
1655 	case 0:
1656 		dev->stats.tx_bytes += req->length;
1657 	}
1658 	dev->stats.tx_packets++;
1659 
1660 	packet_sent = 1;
1661 }
1662 
1663 static inline int eth_is_promisc(struct eth_dev *dev)
1664 {
1665 	/* no filters for the CDC subset; always promisc */
1666 	if (subset_active(dev))
1667 		return 1;
1668 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1669 }
1670 
1671 #if 0
1672 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1673 {
1674 	struct eth_dev		*dev = netdev_priv(net);
1675 	int			length = skb->len;
1676 	int			retval;
1677 	struct usb_request	*req = NULL;
1678 	unsigned long		flags;
1679 
1680 	/* apply outgoing CDC or RNDIS filters */
1681 	if (!eth_is_promisc (dev)) {
1682 		u8		*dest = skb->data;
1683 
1684 		if (is_multicast_ether_addr(dest)) {
1685 			u16	type;
1686 
1687 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1688 			 * SET_ETHERNET_MULTICAST_FILTERS requests
1689 			 */
1690 			if (is_broadcast_ether_addr(dest))
1691 				type = USB_CDC_PACKET_TYPE_BROADCAST;
1692 			else
1693 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1694 			if (!(dev->cdc_filter & type)) {
1695 				dev_kfree_skb_any (skb);
1696 				return 0;
1697 			}
1698 		}
1699 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1700 	}
1701 
1702 	spin_lock_irqsave(&dev->req_lock, flags);
1703 	/*
1704 	 * this freelist can be empty if an interrupt triggered disconnect()
1705 	 * and reconfigured the gadget (shutting down this queue) after the
1706 	 * network stack decided to xmit but before we got the spinlock.
1707 	 */
1708 	if (list_empty(&dev->tx_reqs)) {
1709 		spin_unlock_irqrestore(&dev->req_lock, flags);
1710 		return 1;
1711 	}
1712 
1713 	req = container_of (dev->tx_reqs.next, struct usb_request, list);
1714 	list_del (&req->list);
1715 
1716 	/* temporarily stop TX queue when the freelist empties */
1717 	if (list_empty (&dev->tx_reqs))
1718 		netif_stop_queue (net);
1719 	spin_unlock_irqrestore(&dev->req_lock, flags);
1720 
1721 	/* no buffer copies needed, unless the network stack did it
1722 	 * or the hardware can't use skb buffers.
1723 	 * or there's not enough space for any RNDIS headers we need
1724 	 */
1725 	if (rndis_active(dev)) {
1726 		struct sk_buff	*skb_rndis;
1727 
1728 		skb_rndis = skb_realloc_headroom (skb,
1729 				sizeof (struct rndis_packet_msg_type));
1730 		if (!skb_rndis)
1731 			goto drop;
1732 
1733 		dev_kfree_skb_any (skb);
1734 		skb = skb_rndis;
1735 		rndis_add_hdr (skb);
1736 		length = skb->len;
1737 	}
1738 	req->buf = skb->data;
1739 	req->context = skb;
1740 	req->complete = tx_complete;
1741 
1742 	/* use zlp framing on tx for strict CDC-Ether conformance,
1743 	 * though any robust network rx path ignores extra padding.
1744 	 * and some hardware doesn't like to write zlps.
1745 	 */
1746 	req->zero = 1;
1747 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1748 		length++;
1749 
1750 	req->length = length;
1751 
1752 	/* throttle highspeed IRQ rate back slightly */
1753 	if (gadget_is_dualspeed(dev->gadget))
1754 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1755 			? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1756 			: 0;
1757 
1758 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1759 	switch (retval) {
1760 	default:
1761 		DEBUG (dev, "tx queue err %d\n", retval);
1762 		break;
1763 	case 0:
1764 		net->trans_start = jiffies;
1765 		atomic_inc (&dev->tx_qlen);
1766 	}
1767 
1768 	if (retval) {
1769 drop:
1770 		dev->stats.tx_dropped++;
1771 		dev_kfree_skb_any (skb);
1772 		spin_lock_irqsave(&dev->req_lock, flags);
1773 		if (list_empty (&dev->tx_reqs))
1774 			netif_start_queue (net);
1775 		list_add (&req->list, &dev->tx_reqs);
1776 		spin_unlock_irqrestore(&dev->req_lock, flags);
1777 	}
1778 	return 0;
1779 }
1780 
1781 /*-------------------------------------------------------------------------*/
1782 #endif
1783 
1784 static void eth_unbind(struct usb_gadget *gadget)
1785 {
1786 	struct eth_dev		*dev = get_gadget_data(gadget);
1787 
1788 	debug("%s...\n", __func__);
1789 	rndis_deregister(dev->rndis_config);
1790 	rndis_exit();
1791 
1792 	/* we've already been disconnected ... no i/o is active */
1793 	if (dev->req) {
1794 		usb_ep_free_request(gadget->ep0, dev->req);
1795 		dev->req = NULL;
1796 	}
1797 	if (dev->stat_req) {
1798 		usb_ep_free_request(dev->status_ep, dev->stat_req);
1799 		dev->stat_req = NULL;
1800 	}
1801 
1802 	if (dev->tx_req) {
1803 		usb_ep_free_request(dev->in_ep, dev->tx_req);
1804 		dev->tx_req = NULL;
1805 	}
1806 
1807 	if (dev->rx_req) {
1808 		usb_ep_free_request(dev->out_ep, dev->rx_req);
1809 		dev->rx_req = NULL;
1810 	}
1811 
1812 /*	unregister_netdev (dev->net);*/
1813 /*	free_netdev(dev->net);*/
1814 
1815 	dev->gadget = NULL;
1816 	set_gadget_data(gadget, NULL);
1817 }
1818 
1819 static void eth_disconnect(struct usb_gadget *gadget)
1820 {
1821 	eth_reset_config(get_gadget_data(gadget));
1822 	/* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1823 }
1824 
1825 static void eth_suspend(struct usb_gadget *gadget)
1826 {
1827 	/* Not used */
1828 }
1829 
1830 static void eth_resume(struct usb_gadget *gadget)
1831 {
1832 	/* Not used */
1833 }
1834 
1835 /*-------------------------------------------------------------------------*/
1836 
1837 #ifdef CONFIG_USB_ETH_RNDIS
1838 
1839 /*
1840  * The interrupt endpoint is used in RNDIS to notify the host when messages
1841  * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1842  * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1843  * REMOTE_NDIS_KEEPALIVE_MSG.
1844  *
1845  * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1846  * normally just one notification will be queued.
1847  */
1848 
1849 static void rndis_control_ack_complete(struct usb_ep *ep,
1850 					struct usb_request *req)
1851 {
1852 	struct eth_dev          *dev = ep->driver_data;
1853 
1854 	debug("%s...\n", __func__);
1855 	if (req->status || req->actual != req->length)
1856 		debug("rndis control ack complete --> %d, %d/%d\n",
1857 			req->status, req->actual, req->length);
1858 
1859 	if (!l_ethdev.network_started) {
1860 		if (rndis_get_state(dev->rndis_config)
1861 				== RNDIS_DATA_INITIALIZED) {
1862 			l_ethdev.network_started = 1;
1863 			printf("USB RNDIS network up!\n");
1864 		}
1865 	}
1866 
1867 	req->context = NULL;
1868 
1869 	if (req != dev->stat_req)
1870 		usb_ep_free_request(ep, req);
1871 }
1872 
1873 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1874 
1875 static int rndis_control_ack(struct eth_device *net)
1876 {
1877 	struct eth_dev		*dev = &l_ethdev;
1878 	int                     length;
1879 	struct usb_request      *resp = dev->stat_req;
1880 
1881 	/* in case RNDIS calls this after disconnect */
1882 	if (!dev->status) {
1883 		debug("status ENODEV\n");
1884 		return -ENODEV;
1885 	}
1886 
1887 	/* in case queue length > 1 */
1888 	if (resp->context) {
1889 		resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1890 		if (!resp)
1891 			return -ENOMEM;
1892 		resp->buf = rndis_resp_buf;
1893 	}
1894 
1895 	/*
1896 	 * Send RNDIS RESPONSE_AVAILABLE notification;
1897 	 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1898 	 */
1899 	resp->length = 8;
1900 	resp->complete = rndis_control_ack_complete;
1901 	resp->context = dev;
1902 
1903 	*((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1904 	*((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1905 
1906 	length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1907 	if (length < 0) {
1908 		resp->status = 0;
1909 		rndis_control_ack_complete(dev->status_ep, resp);
1910 	}
1911 
1912 	return 0;
1913 }
1914 
1915 #else
1916 
1917 #define	rndis_control_ack	NULL
1918 
1919 #endif	/* RNDIS */
1920 
1921 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1922 {
1923 	if (rndis_active(dev)) {
1924 		rndis_set_param_medium(dev->rndis_config,
1925 					NDIS_MEDIUM_802_3,
1926 					BITRATE(dev->gadget)/100);
1927 		rndis_signal_connect(dev->rndis_config);
1928 	}
1929 }
1930 
1931 static int eth_stop(struct eth_dev *dev)
1932 {
1933 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1934 	unsigned long ts;
1935 	unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1936 #endif
1937 
1938 	if (rndis_active(dev)) {
1939 		rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1940 		rndis_signal_disconnect(dev->rndis_config);
1941 
1942 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1943 		/* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1944 		ts = get_timer(0);
1945 		while (get_timer(ts) < timeout)
1946 			usb_gadget_handle_interrupts();
1947 #endif
1948 
1949 		rndis_uninit(dev->rndis_config);
1950 		dev->rndis = 0;
1951 	}
1952 
1953 	return 0;
1954 }
1955 
1956 /*-------------------------------------------------------------------------*/
1957 
1958 static int is_eth_addr_valid(char *str)
1959 {
1960 	if (strlen(str) == 17) {
1961 		int i;
1962 		char *p, *q;
1963 		uchar ea[6];
1964 
1965 		/* see if it looks like an ethernet address */
1966 
1967 		p = str;
1968 
1969 		for (i = 0; i < 6; i++) {
1970 			char term = (i == 5 ? '\0' : ':');
1971 
1972 			ea[i] = simple_strtol(p, &q, 16);
1973 
1974 			if ((q - p) != 2 || *q++ != term)
1975 				break;
1976 
1977 			p = q;
1978 		}
1979 
1980 		/* Now check the contents. */
1981 		return is_valid_ether_addr(ea);
1982 	}
1983 	return 0;
1984 }
1985 
1986 static u8 nibble(unsigned char c)
1987 {
1988 	if (likely(isdigit(c)))
1989 		return c - '0';
1990 	c = toupper(c);
1991 	if (likely(isxdigit(c)))
1992 		return 10 + c - 'A';
1993 	return 0;
1994 }
1995 
1996 static int get_ether_addr(const char *str, u8 *dev_addr)
1997 {
1998 	if (str) {
1999 		unsigned	i;
2000 
2001 		for (i = 0; i < 6; i++) {
2002 			unsigned char num;
2003 
2004 			if ((*str == '.') || (*str == ':'))
2005 				str++;
2006 			num = nibble(*str++) << 4;
2007 			num |= (nibble(*str++));
2008 			dev_addr[i] = num;
2009 		}
2010 		if (is_valid_ether_addr(dev_addr))
2011 			return 0;
2012 	}
2013 	return 1;
2014 }
2015 
2016 static int eth_bind(struct usb_gadget *gadget)
2017 {
2018 	struct eth_dev		*dev = &l_ethdev;
2019 	u8			cdc = 1, zlp = 1, rndis = 1;
2020 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
2021 	int			status = -ENOMEM;
2022 	int			gcnum;
2023 	u8			tmp[7];
2024 
2025 	/* these flags are only ever cleared; compiler take note */
2026 #ifndef	CONFIG_USB_ETH_CDC
2027 	cdc = 0;
2028 #endif
2029 #ifndef	CONFIG_USB_ETH_RNDIS
2030 	rndis = 0;
2031 #endif
2032 	/*
2033 	 * Because most host side USB stacks handle CDC Ethernet, that
2034 	 * standard protocol is _strongly_ preferred for interop purposes.
2035 	 * (By everyone except Microsoft.)
2036 	 */
2037 	if (gadget_is_pxa(gadget)) {
2038 		/* pxa doesn't support altsettings */
2039 		cdc = 0;
2040 	} else if (gadget_is_musbhdrc(gadget)) {
2041 		/* reduce tx dma overhead by avoiding special cases */
2042 		zlp = 0;
2043 	} else if (gadget_is_sh(gadget)) {
2044 		/* sh doesn't support multiple interfaces or configs */
2045 		cdc = 0;
2046 		rndis = 0;
2047 	} else if (gadget_is_sa1100(gadget)) {
2048 		/* hardware can't write zlps */
2049 		zlp = 0;
2050 		/*
2051 		 * sa1100 CAN do CDC, without status endpoint ... we use
2052 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2053 		 */
2054 		cdc = 0;
2055 	}
2056 
2057 	gcnum = usb_gadget_controller_number(gadget);
2058 	if (gcnum >= 0)
2059 		device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2060 	else {
2061 		/*
2062 		 * can't assume CDC works.  don't want to default to
2063 		 * anything less functional on CDC-capable hardware,
2064 		 * so we fail in this case.
2065 		 */
2066 		error("controller '%s' not recognized",
2067 			gadget->name);
2068 		return -ENODEV;
2069 	}
2070 
2071 	/*
2072 	 * If there's an RNDIS configuration, that's what Windows wants to
2073 	 * be using ... so use these product IDs here and in the "linux.inf"
2074 	 * needed to install MSFT drivers.  Current Linux kernels will use
2075 	 * the second configuration if it's CDC Ethernet, and need some help
2076 	 * to choose the right configuration otherwise.
2077 	 */
2078 	if (rndis) {
2079 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2080 		device_desc.idVendor =
2081 			__constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2082 		device_desc.idProduct =
2083 			__constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2084 #else
2085 		device_desc.idVendor =
2086 			__constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2087 		device_desc.idProduct =
2088 			__constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2089 #endif
2090 		sprintf(product_desc, "RNDIS/%s", driver_desc);
2091 
2092 	/*
2093 	 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2094 	 * drivers aren't widely available.  (That may be improved by
2095 	 * supporting one submode of the "SAFE" variant of MDLM.)
2096 	 */
2097 	} else {
2098 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2099 		device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2100 		device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2101 #else
2102 		if (!cdc) {
2103 			device_desc.idVendor =
2104 				__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2105 			device_desc.idProduct =
2106 				__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2107 		}
2108 #endif
2109 	}
2110 	/* support optional vendor/distro customization */
2111 	if (bcdDevice)
2112 		device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2113 	if (iManufacturer)
2114 		strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2115 	if (iProduct)
2116 		strlcpy(product_desc, iProduct, sizeof product_desc);
2117 	if (iSerialNumber) {
2118 		device_desc.iSerialNumber = STRING_SERIALNUMBER,
2119 		strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2120 	}
2121 
2122 	/* all we really need is bulk IN/OUT */
2123 	usb_ep_autoconfig_reset(gadget);
2124 	in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2125 	if (!in_ep) {
2126 autoconf_fail:
2127 		error("can't autoconfigure on %s\n",
2128 			gadget->name);
2129 		return -ENODEV;
2130 	}
2131 	in_ep->driver_data = in_ep;	/* claim */
2132 
2133 	out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2134 	if (!out_ep)
2135 		goto autoconf_fail;
2136 	out_ep->driver_data = out_ep;	/* claim */
2137 
2138 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2139 	/*
2140 	 * CDC Ethernet control interface doesn't require a status endpoint.
2141 	 * Since some hosts expect one, try to allocate one anyway.
2142 	 */
2143 	if (cdc || rndis) {
2144 		status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2145 		if (status_ep) {
2146 			status_ep->driver_data = status_ep;	/* claim */
2147 		} else if (rndis) {
2148 			error("can't run RNDIS on %s", gadget->name);
2149 			return -ENODEV;
2150 #ifdef CONFIG_USB_ETH_CDC
2151 		} else if (cdc) {
2152 			control_intf.bNumEndpoints = 0;
2153 			/* FIXME remove endpoint from descriptor list */
2154 #endif
2155 		}
2156 	}
2157 #endif
2158 
2159 	/* one config:  cdc, else minimal subset */
2160 	if (!cdc) {
2161 		eth_config.bNumInterfaces = 1;
2162 		eth_config.iConfiguration = STRING_SUBSET;
2163 
2164 		/*
2165 		 * use functions to set these up, in case we're built to work
2166 		 * with multiple controllers and must override CDC Ethernet.
2167 		 */
2168 		fs_subset_descriptors();
2169 		hs_subset_descriptors();
2170 	}
2171 
2172 	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2173 	usb_gadget_set_selfpowered(gadget);
2174 
2175 	/* For now RNDIS is always a second config */
2176 	if (rndis)
2177 		device_desc.bNumConfigurations = 2;
2178 
2179 	if (gadget_is_dualspeed(gadget)) {
2180 		if (rndis)
2181 			dev_qualifier.bNumConfigurations = 2;
2182 		else if (!cdc)
2183 			dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2184 
2185 		/* assumes ep0 uses the same value for both speeds ... */
2186 		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2187 
2188 		/* and that all endpoints are dual-speed */
2189 		hs_source_desc.bEndpointAddress =
2190 				fs_source_desc.bEndpointAddress;
2191 		hs_sink_desc.bEndpointAddress =
2192 				fs_sink_desc.bEndpointAddress;
2193 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2194 		if (status_ep)
2195 			hs_status_desc.bEndpointAddress =
2196 					fs_status_desc.bEndpointAddress;
2197 #endif
2198 	}
2199 
2200 	if (gadget_is_otg(gadget)) {
2201 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
2202 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2203 		eth_config.bMaxPower = 4;
2204 #ifdef	CONFIG_USB_ETH_RNDIS
2205 		rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2206 		rndis_config.bMaxPower = 4;
2207 #endif
2208 	}
2209 
2210 
2211 	/* network device setup */
2212 	dev->net = &l_netdev;
2213 
2214 	dev->cdc = cdc;
2215 	dev->zlp = zlp;
2216 
2217 	dev->in_ep = in_ep;
2218 	dev->out_ep = out_ep;
2219 	dev->status_ep = status_ep;
2220 
2221 	/*
2222 	 * Module params for these addresses should come from ID proms.
2223 	 * The host side address is used with CDC and RNDIS, and commonly
2224 	 * ends up in a persistent config database.  It's not clear if
2225 	 * host side code for the SAFE thing cares -- its original BLAN
2226 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
2227 	 */
2228 	get_ether_addr(dev_addr, dev->net->enetaddr);
2229 
2230 	memset(tmp, 0, sizeof(tmp));
2231 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2232 
2233 	get_ether_addr(host_addr, dev->host_mac);
2234 
2235 	sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2236 		dev->host_mac[0], dev->host_mac[1],
2237 			dev->host_mac[2], dev->host_mac[3],
2238 			dev->host_mac[4], dev->host_mac[5]);
2239 
2240 	if (rndis) {
2241 		status = rndis_init();
2242 		if (status < 0) {
2243 			error("can't init RNDIS, %d", status);
2244 			goto fail;
2245 		}
2246 	}
2247 
2248 	/*
2249 	 * use PKTSIZE (or aligned... from u-boot) and set
2250 	 * wMaxSegmentSize accordingly
2251 	 */
2252 	dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2253 
2254 	/* preallocate control message data and buffer */
2255 	dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2256 	if (!dev->req)
2257 		goto fail;
2258 	dev->req->buf = control_req;
2259 	dev->req->complete = eth_setup_complete;
2260 
2261 	/* ... and maybe likewise for status transfer */
2262 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2263 	if (dev->status_ep) {
2264 		dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2265 							GFP_KERNEL);
2266 		if (!dev->stat_req) {
2267 			usb_ep_free_request(dev->status_ep, dev->req);
2268 
2269 			goto fail;
2270 		}
2271 		dev->stat_req->buf = status_req;
2272 		dev->stat_req->context = NULL;
2273 	}
2274 #endif
2275 
2276 	/* finish hookup to lower layer ... */
2277 	dev->gadget = gadget;
2278 	set_gadget_data(gadget, dev);
2279 	gadget->ep0->driver_data = dev;
2280 
2281 	/*
2282 	 * two kinds of host-initiated state changes:
2283 	 *  - iff DATA transfer is active, carrier is "on"
2284 	 *  - tx queueing enabled if open *and* carrier is "on"
2285 	 */
2286 
2287 	printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2288 		out_ep->name, in_ep->name,
2289 		status_ep ? " STATUS " : "",
2290 		status_ep ? status_ep->name : ""
2291 		);
2292 	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2293 		dev->net->enetaddr[0], dev->net->enetaddr[1],
2294 		dev->net->enetaddr[2], dev->net->enetaddr[3],
2295 		dev->net->enetaddr[4], dev->net->enetaddr[5]);
2296 
2297 	if (cdc || rndis)
2298 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2299 			dev->host_mac[0], dev->host_mac[1],
2300 			dev->host_mac[2], dev->host_mac[3],
2301 			dev->host_mac[4], dev->host_mac[5]);
2302 
2303 	if (rndis) {
2304 		u32	vendorID = 0;
2305 
2306 		/* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2307 
2308 		dev->rndis_config = rndis_register(rndis_control_ack);
2309 		if (dev->rndis_config < 0) {
2310 fail0:
2311 			eth_unbind(gadget);
2312 			debug("RNDIS setup failed\n");
2313 			status = -ENODEV;
2314 			goto fail;
2315 		}
2316 
2317 		/* these set up a lot of the OIDs that RNDIS needs */
2318 		rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2319 		if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2320 					&dev->stats, &dev->cdc_filter))
2321 			goto fail0;
2322 		if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2323 					manufacturer))
2324 			goto fail0;
2325 		if (rndis_set_param_medium(dev->rndis_config,
2326 					NDIS_MEDIUM_802_3, 0))
2327 			goto fail0;
2328 		printf("RNDIS ready\n");
2329 	}
2330 	return 0;
2331 
2332 fail:
2333 	error("%s failed, status = %d", __func__, status);
2334 	eth_unbind(gadget);
2335 	return status;
2336 }
2337 
2338 /*-------------------------------------------------------------------------*/
2339 
2340 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2341 {
2342 	struct eth_dev *dev = &l_ethdev;
2343 	struct usb_gadget *gadget;
2344 	unsigned long ts;
2345 	unsigned long timeout = USB_CONNECT_TIMEOUT;
2346 
2347 	if (!netdev) {
2348 		error("received NULL ptr");
2349 		goto fail;
2350 	}
2351 
2352 	/* Configure default mac-addresses for the USB ethernet device */
2353 #ifdef CONFIG_USBNET_DEV_ADDR
2354 	strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2355 #endif
2356 #ifdef CONFIG_USBNET_HOST_ADDR
2357 	strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2358 #endif
2359 	/* Check if the user overruled the MAC addresses */
2360 	if (getenv("usbnet_devaddr"))
2361 		strlcpy(dev_addr, getenv("usbnet_devaddr"),
2362 			sizeof(dev_addr));
2363 
2364 	if (getenv("usbnet_hostaddr"))
2365 		strlcpy(host_addr, getenv("usbnet_hostaddr"),
2366 			sizeof(host_addr));
2367 
2368 	if (!is_eth_addr_valid(dev_addr)) {
2369 		error("Need valid 'usbnet_devaddr' to be set");
2370 		goto fail;
2371 	}
2372 	if (!is_eth_addr_valid(host_addr)) {
2373 		error("Need valid 'usbnet_hostaddr' to be set");
2374 		goto fail;
2375 	}
2376 
2377 	if (usb_gadget_register_driver(&eth_driver) < 0)
2378 		goto fail;
2379 
2380 	dev->network_started = 0;
2381 
2382 	packet_received = 0;
2383 	packet_sent = 0;
2384 
2385 	gadget = dev->gadget;
2386 	usb_gadget_connect(gadget);
2387 
2388 	if (getenv("cdc_connect_timeout"))
2389 		timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2390 						NULL, 10) * CONFIG_SYS_HZ;
2391 	ts = get_timer(0);
2392 	while (!l_ethdev.network_started) {
2393 		/* Handle control-c and timeouts */
2394 		if (ctrlc() || (get_timer(ts) > timeout)) {
2395 			error("The remote end did not respond in time.");
2396 			goto fail;
2397 		}
2398 		usb_gadget_handle_interrupts();
2399 	}
2400 
2401 	packet_received = 0;
2402 	rx_submit(dev, dev->rx_req, 0);
2403 	return 0;
2404 fail:
2405 	return -1;
2406 }
2407 
2408 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2409 {
2410 	int			retval;
2411 	void			*rndis_pkt = NULL;
2412 	struct eth_dev		*dev = &l_ethdev;
2413 	struct usb_request	*req = dev->tx_req;
2414 	unsigned long ts;
2415 	unsigned long timeout = USB_CONNECT_TIMEOUT;
2416 
2417 	debug("%s:...\n", __func__);
2418 
2419 	/* new buffer is needed to include RNDIS header */
2420 	if (rndis_active(dev)) {
2421 		rndis_pkt = malloc(length +
2422 					sizeof(struct rndis_packet_msg_type));
2423 		if (!rndis_pkt) {
2424 			error("No memory to alloc RNDIS packet");
2425 			goto drop;
2426 		}
2427 		rndis_add_hdr(rndis_pkt, length);
2428 		memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2429 				packet, length);
2430 		packet = rndis_pkt;
2431 		length += sizeof(struct rndis_packet_msg_type);
2432 	}
2433 	req->buf = packet;
2434 	req->context = NULL;
2435 	req->complete = tx_complete;
2436 
2437 	/*
2438 	 * use zlp framing on tx for strict CDC-Ether conformance,
2439 	 * though any robust network rx path ignores extra padding.
2440 	 * and some hardware doesn't like to write zlps.
2441 	 */
2442 	req->zero = 1;
2443 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2444 		length++;
2445 
2446 	req->length = length;
2447 #if 0
2448 	/* throttle highspeed IRQ rate back slightly */
2449 	if (gadget_is_dualspeed(dev->gadget))
2450 		req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2451 			? ((dev->tx_qlen % qmult) != 0) : 0;
2452 #endif
2453 	dev->tx_qlen = 1;
2454 	ts = get_timer(0);
2455 	packet_sent = 0;
2456 
2457 	retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2458 
2459 	if (!retval)
2460 		debug("%s: packet queued\n", __func__);
2461 	while (!packet_sent) {
2462 		if (get_timer(ts) > timeout) {
2463 			printf("timeout sending packets to usb ethernet\n");
2464 			return -1;
2465 		}
2466 		usb_gadget_handle_interrupts();
2467 	}
2468 	if (rndis_pkt)
2469 		free(rndis_pkt);
2470 
2471 	return 0;
2472 drop:
2473 	dev->stats.tx_dropped++;
2474 	return -ENOMEM;
2475 }
2476 
2477 static int usb_eth_recv(struct eth_device *netdev)
2478 {
2479 	struct eth_dev *dev = &l_ethdev;
2480 
2481 	usb_gadget_handle_interrupts();
2482 
2483 	if (packet_received) {
2484 		debug("%s: packet received\n", __func__);
2485 		if (dev->rx_req) {
2486 			NetReceive(NetRxPackets[0], dev->rx_req->length);
2487 			packet_received = 0;
2488 
2489 			rx_submit(dev, dev->rx_req, 0);
2490 		} else
2491 			error("dev->rx_req invalid");
2492 	}
2493 	return 0;
2494 }
2495 
2496 void usb_eth_halt(struct eth_device *netdev)
2497 {
2498 	struct eth_dev *dev = &l_ethdev;
2499 
2500 	if (!netdev) {
2501 		error("received NULL ptr");
2502 		return;
2503 	}
2504 
2505 	/* If the gadget not registered, simple return */
2506 	if (!dev->gadget)
2507 		return;
2508 
2509 	/*
2510 	 * Some USB controllers may need additional deinitialization here
2511 	 * before dropping pull-up (also due to hardware issues).
2512 	 * For example: unhandled interrupt with status stage started may
2513 	 * bring the controller to fully broken state (until board reset).
2514 	 * There are some variants to debug and fix such cases:
2515 	 * 1) In the case of RNDIS connection eth_stop can perform additional
2516 	 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2517 	 * 2) 'pullup' callback in your UDC driver can be improved to perform
2518 	 * this deinitialization.
2519 	 */
2520 	eth_stop(dev);
2521 
2522 	usb_gadget_disconnect(dev->gadget);
2523 
2524 	/* Clear pending interrupt */
2525 	if (dev->network_started) {
2526 		usb_gadget_handle_interrupts();
2527 		dev->network_started = 0;
2528 	}
2529 
2530 	usb_gadget_unregister_driver(&eth_driver);
2531 }
2532 
2533 static struct usb_gadget_driver eth_driver = {
2534 	.speed		= DEVSPEED,
2535 
2536 	.bind		= eth_bind,
2537 	.unbind		= eth_unbind,
2538 
2539 	.setup		= eth_setup,
2540 	.disconnect	= eth_disconnect,
2541 
2542 	.suspend	= eth_suspend,
2543 	.resume		= eth_resume,
2544 };
2545 
2546 int usb_eth_initialize(bd_t *bi)
2547 {
2548 	struct eth_device *netdev = &l_netdev;
2549 
2550 	strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2551 
2552 	netdev->init = usb_eth_init;
2553 	netdev->send = usb_eth_send;
2554 	netdev->recv = usb_eth_recv;
2555 	netdev->halt = usb_eth_halt;
2556 
2557 #ifdef CONFIG_MCAST_TFTP
2558   #error not supported
2559 #endif
2560 	eth_register(netdev);
2561 	return 0;
2562 }
2563