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