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