Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / usb / gadget / ether.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4  *
5  * Copyright (C) 2003-2005,2008 David Brownell
6  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7  * Copyright (C) 2008 Nokia Corporation
8  */
9
10 #include <common.h>
11 #include <console.h>
12 #include <env.h>
13 #include <log.h>
14 #include <part.h>
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/cdc.h>
19 #include <linux/usb/gadget.h>
20 #include <net.h>
21 #include <usb.h>
22 #include <malloc.h>
23 #include <memalign.h>
24 #include <linux/ctype.h>
25
26 #include "gadget_chips.h"
27 #include "rndis.h"
28
29 #include <dm.h>
30 #include <dm/lists.h>
31 #include <dm/uclass-internal.h>
32 #include <dm/device-internal.h>
33
34 #define USB_NET_NAME "usb_ether"
35
36 extern struct platform_data brd;
37
38
39 unsigned packet_received, packet_sent;
40
41 /*
42  * Ethernet gadget driver -- with CDC and non-CDC options
43  * Builds on hardware support for a full duplex link.
44  *
45  * CDC Ethernet is the standard USB solution for sending Ethernet frames
46  * using USB.  Real hardware tends to use the same framing protocol but look
47  * different for control features.  This driver strongly prefers to use
48  * this USB-IF standard as its open-systems interoperability solution;
49  * most host side USB stacks (except from Microsoft) support it.
50  *
51  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
52  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
53  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
54  *
55  * There's some hardware that can't talk CDC ECM.  We make that hardware
56  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
57  * link-level setup only requires activating the configuration.  Only the
58  * endpoint descriptors, and product/vendor IDs, are relevant; no control
59  * operations are available.  Linux supports it, but other host operating
60  * systems may not.  (This is a subset of CDC Ethernet.)
61  *
62  * It turns out that if you add a few descriptors to that "CDC Subset",
63  * (Windows) host side drivers from MCCI can treat it as one submode of
64  * a proprietary scheme called "SAFE" ... without needing to know about
65  * specific product/vendor IDs.  So we do that, making it easier to use
66  * those MS-Windows drivers.  Those added descriptors make it resemble a
67  * CDC MDLM device, but they don't change device behavior at all.  (See
68  * MCCI Engineering report 950198 "SAFE Networking Functions".)
69  *
70  * A third option is also in use.  Rather than CDC Ethernet, or something
71  * simpler, Microsoft pushes their own approach: RNDIS.  The published
72  * RNDIS specs are ambiguous and appear to be incomplete, and are also
73  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
74  */
75
76 #define DRIVER_DESC             "Ethernet Gadget"
77 /* Based on linux 2.6.27 version */
78 #define DRIVER_VERSION          "May Day 2005"
79
80 static const char driver_desc[] = DRIVER_DESC;
81
82 #define RX_EXTRA        20              /* guard against rx overflows */
83
84 #ifndef CONFIG_USB_ETH_RNDIS
85 #define rndis_uninit(x)         do {} while (0)
86 #define rndis_deregister(c)     do {} while (0)
87 #define rndis_exit()            do {} while (0)
88 #endif
89
90 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
91 #define DEFAULT_FILTER  (USB_CDC_PACKET_TYPE_BROADCAST \
92                         |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
93                         |USB_CDC_PACKET_TYPE_PROMISCUOUS \
94                         |USB_CDC_PACKET_TYPE_DIRECTED)
95
96 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
97
98 /*-------------------------------------------------------------------------*/
99
100 struct eth_dev {
101         struct usb_gadget       *gadget;
102         struct usb_request      *req;           /* for control responses */
103         struct usb_request      *stat_req;      /* for cdc & rndis status */
104
105         u8                      config;
106         struct usb_ep           *in_ep, *out_ep, *status_ep;
107         const struct usb_endpoint_descriptor
108                                 *in, *out, *status;
109
110         struct usb_request      *tx_req, *rx_req;
111
112         struct udevice          *net;
113         struct net_device_stats stats;
114         unsigned int            tx_qlen;
115
116         unsigned                zlp:1;
117         unsigned                cdc:1;
118         unsigned                rndis:1;
119         unsigned                suspended:1;
120         unsigned                network_started:1;
121         u16                     cdc_filter;
122         unsigned long           todo;
123         int                     mtu;
124 #define WORK_RX_MEMORY          0
125         int                     rndis_config;
126         u8                      host_mac[ETH_ALEN];
127 };
128
129 /*
130  * This version autoconfigures as much as possible at run-time.
131  *
132  * It also ASSUMES a self-powered device, without remote wakeup,
133  * although remote wakeup support would make sense.
134  */
135
136 /*-------------------------------------------------------------------------*/
137 struct ether_priv {
138         struct eth_dev ethdev;
139         struct udevice *netdev;
140         struct usb_gadget_driver eth_driver;
141 };
142
143 struct ether_priv eth_priv;
144 struct ether_priv *l_priv = &eth_priv;
145
146 /*-------------------------------------------------------------------------*/
147
148 /* "main" config is either CDC, or its simple subset */
149 static inline int is_cdc(struct eth_dev *dev)
150 {
151 #if     !defined(CONFIG_USB_ETH_SUBSET)
152         return 1;               /* only cdc possible */
153 #elif   !defined(CONFIG_USB_ETH_CDC)
154         return 0;               /* only subset possible */
155 #else
156         return dev->cdc;        /* depends on what hardware we found */
157 #endif
158 }
159
160 /* "secondary" RNDIS config may sometimes be activated */
161 static inline int rndis_active(struct eth_dev *dev)
162 {
163 #ifdef  CONFIG_USB_ETH_RNDIS
164         return dev->rndis;
165 #else
166         return 0;
167 #endif
168 }
169
170 #define subset_active(dev)      (!is_cdc(dev) && !rndis_active(dev))
171 #define cdc_active(dev)         (is_cdc(dev) && !rndis_active(dev))
172
173 #define DEFAULT_QLEN    2       /* double buffering by default */
174
175 /* peak bulk transfer bits-per-second */
176 #define HS_BPS          (13 * 512 * 8 * 1000 * 8)
177 #define FS_BPS          (19 *  64 * 1 * 1000 * 8)
178
179 #ifdef CONFIG_USB_GADGET_DUALSPEED
180 #define DEVSPEED        USB_SPEED_HIGH
181
182 #ifdef CONFIG_USB_ETH_QMULT
183 #define qmult CONFIG_USB_ETH_QMULT
184 #else
185 #define qmult 5
186 #endif
187
188 /* for dual-speed hardware, use deeper queues at highspeed */
189 #define qlen(gadget) \
190         (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
191
192 static inline int BITRATE(struct usb_gadget *g)
193 {
194         return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
195 }
196
197 #else   /* full speed (low speed doesn't do bulk) */
198
199 #define qmult           1
200
201 #define DEVSPEED        USB_SPEED_FULL
202
203 #define qlen(gadget) DEFAULT_QLEN
204
205 static inline int BITRATE(struct usb_gadget *g)
206 {
207         return FS_BPS;
208 }
209 #endif
210
211 /*-------------------------------------------------------------------------*/
212
213 /*
214  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
215  * Instead:  allocate your own, using normal USB-IF procedures.
216  */
217
218 /*
219  * Thanks to NetChip Technologies for donating this product ID.
220  * It's for devices with only CDC Ethernet configurations.
221  */
222 #define CDC_VENDOR_NUM          0x0525  /* NetChip */
223 #define CDC_PRODUCT_NUM         0xa4a1  /* Linux-USB Ethernet Gadget */
224
225 /*
226  * For hardware that can't talk CDC, we use the same vendor ID that
227  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
228  * with pxa250.  We're protocol-compatible, if the host-side drivers
229  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
230  * drivers that need to hard-wire endpoint numbers have a hook.
231  *
232  * The protocol is a minimal subset of CDC Ether, which works on any bulk
233  * hardware that's not deeply broken ... even on hardware that can't talk
234  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
235  * doesn't handle control-OUT).
236  */
237 #define SIMPLE_VENDOR_NUM       0x049f  /* Compaq Computer Corp. */
238 #define SIMPLE_PRODUCT_NUM      0x505a  /* Linux-USB "CDC Subset" Device */
239
240 /*
241  * For hardware that can talk RNDIS and either of the above protocols,
242  * use this ID ... the windows INF files will know it.  Unless it's
243  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
244  * the non-RNDIS configuration.
245  */
246 #define RNDIS_VENDOR_NUM        0x0525  /* NetChip */
247 #define RNDIS_PRODUCT_NUM       0xa4a2  /* Ethernet/RNDIS Gadget */
248
249 /*
250  * Some systems will want different product identifers published in the
251  * device descriptor, either numbers or strings or both.  These string
252  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
253  */
254
255 /*
256  * Emulating them in eth_bind:
257  * static ushort idVendor;
258  * static ushort idProduct;
259  */
260
261 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
262 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
263 #else
264 static char *iManufacturer = "U-Boot";
265 #endif
266
267 /* These probably need to be configurable. */
268 static ushort bcdDevice;
269 static char *iProduct;
270 static char *iSerialNumber;
271
272 static char dev_addr[18];
273
274 static char host_addr[18];
275
276
277 /*-------------------------------------------------------------------------*/
278
279 /*
280  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
281  * ep0 implementation:  descriptors, config management, setup().
282  * also optional class-specific notification interrupt transfer.
283  */
284
285 /*
286  * DESCRIPTORS ... most are static, but strings and (full) configuration
287  * descriptors are built on demand.  For now we do either full CDC, or
288  * our simple subset, with RNDIS as an optional second configuration.
289  *
290  * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet.  But
291  * the class descriptors match a modem (they're ignored; it's really just
292  * Ethernet functionality), they don't need the NOP altsetting, and the
293  * status transfer endpoint isn't optional.
294  */
295
296 #define STRING_MANUFACTURER             1
297 #define STRING_PRODUCT                  2
298 #define STRING_ETHADDR                  3
299 #define STRING_DATA                     4
300 #define STRING_CONTROL                  5
301 #define STRING_RNDIS_CONTROL            6
302 #define STRING_CDC                      7
303 #define STRING_SUBSET                   8
304 #define STRING_RNDIS                    9
305 #define STRING_SERIALNUMBER             10
306
307 /* holds our biggest descriptor (or RNDIS response) */
308 #define USB_BUFSIZ      256
309
310 /*
311  * This device advertises one configuration, eth_config, unless RNDIS
312  * is enabled (rndis_config) on hardware supporting at least two configs.
313  *
314  * NOTE:  Controllers like superh_udc should probably be able to use
315  * an RNDIS-only configuration.
316  *
317  * FIXME define some higher-powered configurations to make it easier
318  * to recharge batteries ...
319  */
320
321 #define DEV_CONFIG_VALUE        1       /* cdc or subset */
322 #define DEV_RNDIS_CONFIG_VALUE  2       /* rndis; optional */
323
324 static struct usb_device_descriptor
325 device_desc = {
326         .bLength =              sizeof device_desc,
327         .bDescriptorType =      USB_DT_DEVICE,
328
329         .bcdUSB =               __constant_cpu_to_le16(0x0200),
330
331         .bDeviceClass =         USB_CLASS_COMM,
332         .bDeviceSubClass =      0,
333         .bDeviceProtocol =      0,
334
335         .idVendor =             __constant_cpu_to_le16(CDC_VENDOR_NUM),
336         .idProduct =            __constant_cpu_to_le16(CDC_PRODUCT_NUM),
337         .iManufacturer =        STRING_MANUFACTURER,
338         .iProduct =             STRING_PRODUCT,
339         .bNumConfigurations =   1,
340 };
341
342 static struct usb_otg_descriptor
343 otg_descriptor = {
344         .bLength =              sizeof otg_descriptor,
345         .bDescriptorType =      USB_DT_OTG,
346
347         .bmAttributes =         USB_OTG_SRP,
348 };
349
350 static struct usb_config_descriptor
351 eth_config = {
352         .bLength =              sizeof eth_config,
353         .bDescriptorType =      USB_DT_CONFIG,
354
355         /* compute wTotalLength on the fly */
356         .bNumInterfaces =       2,
357         .bConfigurationValue =  DEV_CONFIG_VALUE,
358         .iConfiguration =       STRING_CDC,
359         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
360         .bMaxPower =            1,
361 };
362
363 #ifdef  CONFIG_USB_ETH_RNDIS
364 static struct usb_config_descriptor
365 rndis_config = {
366         .bLength =              sizeof rndis_config,
367         .bDescriptorType =      USB_DT_CONFIG,
368
369         /* compute wTotalLength on the fly */
370         .bNumInterfaces =       2,
371         .bConfigurationValue =  DEV_RNDIS_CONFIG_VALUE,
372         .iConfiguration =       STRING_RNDIS,
373         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
374         .bMaxPower =            1,
375 };
376 #endif
377
378 /*
379  * Compared to the simple CDC subset, the full CDC Ethernet model adds
380  * three class descriptors, two interface descriptors, optional status
381  * endpoint.  Both have a "data" interface and two bulk endpoints.
382  * There are also differences in how control requests are handled.
383  *
384  * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
385  * CDC-ACM (modem) spec.  Unfortunately MSFT's RNDIS driver is buggy; it
386  * may hang or oops.  Since bugfixes (or accurate specs, letting Linux
387  * work around those bugs) are unlikely to ever come from MSFT, you may
388  * wish to avoid using RNDIS.
389  *
390  * MCCI offers an alternative to RNDIS if you need to connect to Windows
391  * but have hardware that can't support CDC Ethernet.   We add descriptors
392  * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
393  * "SAFE".  That borrows from both CDC Ethernet and CDC MDLM.  You can
394  * get those drivers from MCCI, or bundled with various products.
395  */
396
397 #ifdef  CONFIG_USB_ETH_CDC
398 static struct usb_interface_descriptor
399 control_intf = {
400         .bLength =              sizeof control_intf,
401         .bDescriptorType =      USB_DT_INTERFACE,
402
403         .bInterfaceNumber =     0,
404         /* status endpoint is optional; this may be patched later */
405         .bNumEndpoints =        1,
406         .bInterfaceClass =      USB_CLASS_COMM,
407         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ETHERNET,
408         .bInterfaceProtocol =   USB_CDC_PROTO_NONE,
409         .iInterface =           STRING_CONTROL,
410 };
411 #endif
412
413 #ifdef  CONFIG_USB_ETH_RNDIS
414 static const struct usb_interface_descriptor
415 rndis_control_intf = {
416         .bLength =              sizeof rndis_control_intf,
417         .bDescriptorType =      USB_DT_INTERFACE,
418
419         .bInterfaceNumber =     0,
420         .bNumEndpoints =        1,
421         .bInterfaceClass =      USB_CLASS_COMM,
422         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
423         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
424         .iInterface =           STRING_RNDIS_CONTROL,
425 };
426 #endif
427
428 static const struct usb_cdc_header_desc header_desc = {
429         .bLength =              sizeof header_desc,
430         .bDescriptorType =      USB_DT_CS_INTERFACE,
431         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
432
433         .bcdCDC =               __constant_cpu_to_le16(0x0110),
434 };
435
436 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
437
438 static const struct usb_cdc_union_desc union_desc = {
439         .bLength =              sizeof union_desc,
440         .bDescriptorType =      USB_DT_CS_INTERFACE,
441         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
442
443         .bMasterInterface0 =    0,      /* index of control interface */
444         .bSlaveInterface0 =     1,      /* index of DATA interface */
445 };
446
447 #endif  /* CDC || RNDIS */
448
449 #ifdef  CONFIG_USB_ETH_RNDIS
450
451 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
452         .bLength =              sizeof call_mgmt_descriptor,
453         .bDescriptorType =      USB_DT_CS_INTERFACE,
454         .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,
455
456         .bmCapabilities =       0x00,
457         .bDataInterface =       0x01,
458 };
459
460 static const struct usb_cdc_acm_descriptor acm_descriptor = {
461         .bLength =              sizeof acm_descriptor,
462         .bDescriptorType =      USB_DT_CS_INTERFACE,
463         .bDescriptorSubType =   USB_CDC_ACM_TYPE,
464
465         .bmCapabilities =       0x00,
466 };
467
468 #endif
469
470 #ifndef CONFIG_USB_ETH_CDC
471
472 /*
473  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
474  * ways:  data endpoints live in the control interface, there's no data
475  * interface, and it's not used to talk to a cell phone radio.
476  */
477
478 static const struct usb_cdc_mdlm_desc mdlm_desc = {
479         .bLength =              sizeof mdlm_desc,
480         .bDescriptorType =      USB_DT_CS_INTERFACE,
481         .bDescriptorSubType =   USB_CDC_MDLM_TYPE,
482
483         .bcdVersion =           __constant_cpu_to_le16(0x0100),
484         .bGUID = {
485                 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
486                 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
487         },
488 };
489
490 /*
491  * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
492  * can't really use its struct.  All we do here is say that we're using
493  * the submode of "SAFE" which directly matches the CDC Subset.
494  */
495 #ifdef CONFIG_USB_ETH_SUBSET
496 static const u8 mdlm_detail_desc[] = {
497         6,
498         USB_DT_CS_INTERFACE,
499         USB_CDC_MDLM_DETAIL_TYPE,
500
501         0,      /* "SAFE" */
502         0,      /* network control capabilities (none) */
503         0,      /* network data capabilities ("raw" encapsulation) */
504 };
505 #endif
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(PKTSIZE_ALIGN),
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         eth_reset_config(dev);
1044
1045         switch (number) {
1046         case DEV_CONFIG_VALUE:
1047                 result = set_ether_config(dev, gfp_flags);
1048                 break;
1049 #ifdef  CONFIG_USB_ETH_RNDIS
1050         case DEV_RNDIS_CONFIG_VALUE:
1051                 dev->rndis = 1;
1052                 result = set_ether_config(dev, gfp_flags);
1053                 break;
1054 #endif
1055         default:
1056                 result = -EINVAL;
1057                 /* FALL THROUGH */
1058         case 0:
1059                 break;
1060         }
1061
1062         if (result) {
1063                 if (number)
1064                         eth_reset_config(dev);
1065                 usb_gadget_vbus_draw(dev->gadget,
1066                                 gadget_is_otg(dev->gadget) ? 8 : 100);
1067         } else {
1068                 char *speed;
1069                 unsigned power;
1070
1071                 power = 2 * eth_config.bMaxPower;
1072                 usb_gadget_vbus_draw(dev->gadget, power);
1073
1074                 switch (gadget->speed) {
1075                 case USB_SPEED_FULL:
1076                         speed = "full"; break;
1077 #ifdef CONFIG_USB_GADGET_DUALSPEED
1078                 case USB_SPEED_HIGH:
1079                         speed = "high"; break;
1080 #endif
1081                 default:
1082                         speed = "?"; break;
1083                 }
1084
1085                 dev->config = number;
1086                 printf("%s speed config #%d: %d mA, %s, using %s\n",
1087                                 speed, number, power, driver_desc,
1088                                 rndis_active(dev)
1089                                         ? "RNDIS"
1090                                         : (cdc_active(dev)
1091                                                 ? "CDC Ethernet"
1092                                                 : "CDC Ethernet Subset"));
1093         }
1094         return result;
1095 }
1096
1097 /*-------------------------------------------------------------------------*/
1098
1099 #ifdef  CONFIG_USB_ETH_CDC
1100
1101 /*
1102  * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1103  * only to notify the host about link status changes (which we support) or
1104  * report completion of some encapsulated command (as used in RNDIS).  Since
1105  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1106  * command mechanism; and only one status request is ever queued.
1107  */
1108 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1109 {
1110         struct usb_cdc_notification     *event = req->buf;
1111         int                             value = req->status;
1112         struct eth_dev                  *dev = ep->driver_data;
1113
1114         /* issue the second notification if host reads the first */
1115         if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1116                         && value == 0) {
1117                 __le32  *data = req->buf + sizeof *event;
1118
1119                 event->bmRequestType = 0xA1;
1120                 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1121                 event->wValue = __constant_cpu_to_le16(0);
1122                 event->wIndex = __constant_cpu_to_le16(1);
1123                 event->wLength = __constant_cpu_to_le16(8);
1124
1125                 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1126                 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1127
1128                 req->length = STATUS_BYTECOUNT;
1129                 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1130                 debug("send SPEED_CHANGE --> %d\n", value);
1131                 if (value == 0)
1132                         return;
1133         } else if (value != -ECONNRESET) {
1134                 debug("event %02x --> %d\n",
1135                         event->bNotificationType, value);
1136                 if (event->bNotificationType ==
1137                                 USB_CDC_NOTIFY_SPEED_CHANGE) {
1138                         dev->network_started = 1;
1139                         printf("USB network up!\n");
1140                 }
1141         }
1142         req->context = NULL;
1143 }
1144
1145 static void issue_start_status(struct eth_dev *dev)
1146 {
1147         struct usb_request              *req = dev->stat_req;
1148         struct usb_cdc_notification     *event;
1149         int                             value;
1150
1151         /*
1152          * flush old status
1153          *
1154          * FIXME ugly idiom, maybe we'd be better with just
1155          * a "cancel the whole queue" primitive since any
1156          * unlink-one primitive has way too many error modes.
1157          * here, we "know" toggle is already clear...
1158          *
1159          * FIXME iff req->context != null just dequeue it
1160          */
1161         usb_ep_disable(dev->status_ep);
1162         usb_ep_enable(dev->status_ep, dev->status);
1163
1164         /*
1165          * 3.8.1 says to issue first NETWORK_CONNECTION, then
1166          * a SPEED_CHANGE.  could be useful in some configs.
1167          */
1168         event = req->buf;
1169         event->bmRequestType = 0xA1;
1170         event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1171         event->wValue = __constant_cpu_to_le16(1);      /* connected */
1172         event->wIndex = __constant_cpu_to_le16(1);
1173         event->wLength = 0;
1174
1175         req->length = sizeof *event;
1176         req->complete = eth_status_complete;
1177         req->context = dev;
1178
1179         value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1180         if (value < 0)
1181                 debug("status buf queue --> %d\n", value);
1182 }
1183
1184 #endif
1185
1186 /*-------------------------------------------------------------------------*/
1187
1188 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1189 {
1190         if (req->status || req->actual != req->length)
1191                 debug("setup complete --> %d, %d/%d\n",
1192                                 req->status, req->actual, req->length);
1193 }
1194
1195 #ifdef CONFIG_USB_ETH_RNDIS
1196
1197 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1198 {
1199         if (req->status || req->actual != req->length)
1200                 debug("rndis response complete --> %d, %d/%d\n",
1201                         req->status, req->actual, req->length);
1202
1203         /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1204 }
1205
1206 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1207 {
1208         struct eth_dev          *dev = ep->driver_data;
1209         int                     status;
1210
1211         /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1212         status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1213         if (status < 0)
1214                 pr_err("%s: rndis parse error %d", __func__, status);
1215 }
1216
1217 #endif  /* RNDIS */
1218
1219 /*
1220  * The setup() callback implements all the ep0 functionality that's not
1221  * handled lower down.  CDC has a number of less-common features:
1222  *
1223  *  - two interfaces:  control, and ethernet data
1224  *  - Ethernet data interface has two altsettings:  default, and active
1225  *  - class-specific descriptors for the control interface
1226  *  - class-specific control requests
1227  */
1228 static int
1229 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1230 {
1231         struct eth_dev          *dev = get_gadget_data(gadget);
1232         struct usb_request      *req = dev->req;
1233         int                     value = -EOPNOTSUPP;
1234         u16                     wIndex = le16_to_cpu(ctrl->wIndex);
1235         u16                     wValue = le16_to_cpu(ctrl->wValue);
1236         u16                     wLength = le16_to_cpu(ctrl->wLength);
1237
1238         /*
1239          * descriptors just go into the pre-allocated ep0 buffer,
1240          * while config change events may enable network traffic.
1241          */
1242
1243         debug("%s\n", __func__);
1244
1245         req->complete = eth_setup_complete;
1246         switch (ctrl->bRequest) {
1247
1248         case USB_REQ_GET_DESCRIPTOR:
1249                 if (ctrl->bRequestType != USB_DIR_IN)
1250                         break;
1251                 switch (wValue >> 8) {
1252
1253                 case USB_DT_DEVICE:
1254                         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1255                         value = min(wLength, (u16) sizeof device_desc);
1256                         memcpy(req->buf, &device_desc, value);
1257                         break;
1258                 case USB_DT_DEVICE_QUALIFIER:
1259                         if (!gadget_is_dualspeed(gadget))
1260                                 break;
1261                         value = min(wLength, (u16) sizeof dev_qualifier);
1262                         memcpy(req->buf, &dev_qualifier, value);
1263                         break;
1264
1265                 case USB_DT_OTHER_SPEED_CONFIG:
1266                         if (!gadget_is_dualspeed(gadget))
1267                                 break;
1268                         /* FALLTHROUGH */
1269                 case USB_DT_CONFIG:
1270                         value = config_buf(gadget, req->buf,
1271                                         wValue >> 8,
1272                                         wValue & 0xff,
1273                                         gadget_is_otg(gadget));
1274                         if (value >= 0)
1275                                 value = min(wLength, (u16) value);
1276                         break;
1277
1278                 case USB_DT_STRING:
1279                         value = usb_gadget_get_string(&stringtab,
1280                                         wValue & 0xff, req->buf);
1281
1282                         if (value >= 0)
1283                                 value = min(wLength, (u16) value);
1284
1285                         break;
1286                 }
1287                 break;
1288
1289         case USB_REQ_SET_CONFIGURATION:
1290                 if (ctrl->bRequestType != 0)
1291                         break;
1292                 if (gadget->a_hnp_support)
1293                         debug("HNP available\n");
1294                 else if (gadget->a_alt_hnp_support)
1295                         debug("HNP needs a different root port\n");
1296                 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1297                 break;
1298         case USB_REQ_GET_CONFIGURATION:
1299                 if (ctrl->bRequestType != USB_DIR_IN)
1300                         break;
1301                 *(u8 *)req->buf = dev->config;
1302                 value = min(wLength, (u16) 1);
1303                 break;
1304
1305         case USB_REQ_SET_INTERFACE:
1306                 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1307                                 || !dev->config
1308                                 || wIndex > 1)
1309                         break;
1310                 if (!cdc_active(dev) && wIndex != 0)
1311                         break;
1312
1313 #ifdef CONFIG_USB_ETH_CDC
1314                 switch (wIndex) {
1315                 case 0:         /* control/master intf */
1316                         if (wValue != 0)
1317                                 break;
1318                         if (dev->status) {
1319                                 usb_ep_disable(dev->status_ep);
1320                                 usb_ep_enable(dev->status_ep, dev->status);
1321                         }
1322
1323                         value = 0;
1324                         break;
1325                 case 1:         /* data intf */
1326                         if (wValue > 1)
1327                                 break;
1328                         usb_ep_disable(dev->in_ep);
1329                         usb_ep_disable(dev->out_ep);
1330
1331                         /*
1332                          * CDC requires the data transfers not be done from
1333                          * the default interface setting ... also, setting
1334                          * the non-default interface resets filters etc.
1335                          */
1336                         if (wValue == 1) {
1337                                 if (!cdc_active(dev))
1338                                         break;
1339                                 usb_ep_enable(dev->in_ep, dev->in);
1340                                 usb_ep_enable(dev->out_ep, dev->out);
1341                                 dev->cdc_filter = DEFAULT_FILTER;
1342                                 if (dev->status)
1343                                         issue_start_status(dev);
1344                                 eth_start(dev, GFP_ATOMIC);
1345                         }
1346                         value = 0;
1347                         break;
1348                 }
1349 #else
1350                 /*
1351                  * FIXME this is wrong, as is the assumption that
1352                  * all non-PXA hardware talks real CDC ...
1353                  */
1354                 debug("set_interface ignored!\n");
1355 #endif /* CONFIG_USB_ETH_CDC */
1356                 break;
1357         case USB_REQ_GET_INTERFACE:
1358                 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1359                                 || !dev->config
1360                                 || wIndex > 1)
1361                         break;
1362                 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1363                         break;
1364
1365                 /* for CDC, iff carrier is on, data interface is active. */
1366                 if (rndis_active(dev) || wIndex != 1)
1367                         *(u8 *)req->buf = 0;
1368                 else {
1369                         /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1370                         /* carrier always ok ...*/
1371                         *(u8 *)req->buf = 1 ;
1372                 }
1373                 value = min(wLength, (u16) 1);
1374                 break;
1375
1376 #ifdef CONFIG_USB_ETH_CDC
1377         case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1378                 /*
1379                  * see 6.2.30: no data, wIndex = interface,
1380                  * wValue = packet filter bitmap
1381                  */
1382                 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1383                                 || !cdc_active(dev)
1384                                 || wLength != 0
1385                                 || wIndex > 1)
1386                         break;
1387                 debug("packet filter %02x\n", wValue);
1388                 dev->cdc_filter = wValue;
1389                 value = 0;
1390                 break;
1391
1392         /*
1393          * and potentially:
1394          * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1395          * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1396          * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1397          * case USB_CDC_GET_ETHERNET_STATISTIC:
1398          */
1399
1400 #endif /* CONFIG_USB_ETH_CDC */
1401
1402 #ifdef CONFIG_USB_ETH_RNDIS
1403         /*
1404          * RNDIS uses the CDC command encapsulation mechanism to implement
1405          * an RPC scheme, with much getting/setting of attributes by OID.
1406          */
1407         case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1408                 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1409                                 || !rndis_active(dev)
1410                                 || wLength > USB_BUFSIZ
1411                                 || wValue
1412                                 || rndis_control_intf.bInterfaceNumber
1413                                         != wIndex)
1414                         break;
1415                 /* read the request, then process it */
1416                 value = wLength;
1417                 req->complete = rndis_command_complete;
1418                 /* later, rndis_control_ack () sends a notification */
1419                 break;
1420
1421         case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1422                 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1423                                         == ctrl->bRequestType
1424                                 && rndis_active(dev)
1425                                 /* && wLength >= 0x0400 */
1426                                 && !wValue
1427                                 && rndis_control_intf.bInterfaceNumber
1428                                         == wIndex) {
1429                         u8 *buf;
1430                         u32 n;
1431
1432                         /* return the result */
1433                         buf = rndis_get_next_response(dev->rndis_config, &n);
1434                         if (buf) {
1435                                 memcpy(req->buf, buf, n);
1436                                 req->complete = rndis_response_complete;
1437                                 rndis_free_response(dev->rndis_config, buf);
1438                                 value = n;
1439                         }
1440                         /* else stalls ... spec says to avoid that */
1441                 }
1442                 break;
1443 #endif  /* RNDIS */
1444
1445         default:
1446                 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1447                         ctrl->bRequestType, ctrl->bRequest,
1448                         wValue, wIndex, wLength);
1449         }
1450
1451         /* respond with data transfer before status phase? */
1452         if (value >= 0) {
1453                 debug("respond with data transfer before status phase\n");
1454                 req->length = value;
1455                 req->zero = value < wLength
1456                                 && (value % gadget->ep0->maxpacket) == 0;
1457                 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1458                 if (value < 0) {
1459                         debug("ep_queue --> %d\n", value);
1460                         req->status = 0;
1461                         eth_setup_complete(gadget->ep0, req);
1462                 }
1463         }
1464
1465         /* host either stalls (value < 0) or reports success */
1466         return value;
1467 }
1468
1469 /*-------------------------------------------------------------------------*/
1470
1471 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1472
1473 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1474                                 gfp_t gfp_flags)
1475 {
1476         int                     retval = -ENOMEM;
1477         size_t                  size;
1478
1479         /*
1480          * Padding up to RX_EXTRA handles minor disagreements with host.
1481          * Normally we use the USB "terminate on short read" convention;
1482          * so allow up to (N*maxpacket), since that memory is normally
1483          * already allocated.  Some hardware doesn't deal well with short
1484          * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1485          * byte off the end (to force hardware errors on overflow).
1486          *
1487          * RNDIS uses internal framing, and explicitly allows senders to
1488          * pad to end-of-packet.  That's potentially nice for speed,
1489          * but means receivers can't recover synch on their own.
1490          */
1491
1492         debug("%s\n", __func__);
1493         if (!req)
1494                 return -EINVAL;
1495
1496         size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1497         size += dev->out_ep->maxpacket - 1;
1498         if (rndis_active(dev))
1499                 size += sizeof(struct rndis_packet_msg_type);
1500         size -= size % dev->out_ep->maxpacket;
1501
1502         /*
1503          * Some platforms perform better when IP packets are aligned,
1504          * but on at least one, checksumming fails otherwise.  Note:
1505          * RNDIS headers involve variable numbers of LE32 values.
1506          */
1507
1508         req->buf = (u8 *)net_rx_packets[0];
1509         req->length = size;
1510         req->complete = rx_complete;
1511
1512         retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1513
1514         if (retval)
1515                 pr_err("rx submit --> %d", retval);
1516
1517         return retval;
1518 }
1519
1520 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1521 {
1522         struct eth_dev  *dev = ep->driver_data;
1523
1524         debug("%s: status %d\n", __func__, req->status);
1525         switch (req->status) {
1526         /* normal completion */
1527         case 0:
1528                 if (rndis_active(dev)) {
1529                         /* we know MaxPacketsPerTransfer == 1 here */
1530                         int length = rndis_rm_hdr(req->buf, req->actual);
1531                         if (length < 0)
1532                                 goto length_err;
1533                         req->length -= length;
1534                         req->actual -= length;
1535                 }
1536                 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
1537 length_err:
1538                         dev->stats.rx_errors++;
1539                         dev->stats.rx_length_errors++;
1540                         debug("rx length %d\n", req->length);
1541                         break;
1542                 }
1543
1544                 dev->stats.rx_packets++;
1545                 dev->stats.rx_bytes += req->length;
1546                 break;
1547
1548         /* software-driven interface shutdown */
1549         case -ECONNRESET:               /* unlink */
1550         case -ESHUTDOWN:                /* disconnect etc */
1551         /* for hardware automagic (such as pxa) */
1552         case -ECONNABORTED:             /* endpoint reset */
1553                 break;
1554
1555         /* data overrun */
1556         case -EOVERFLOW:
1557                 dev->stats.rx_over_errors++;
1558                 /* FALLTHROUGH */
1559         default:
1560                 dev->stats.rx_errors++;
1561                 break;
1562         }
1563
1564         packet_received = 1;
1565 }
1566
1567 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1568 {
1569
1570         dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1571
1572         if (!dev->tx_req)
1573                 goto fail1;
1574
1575         dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1576
1577         if (!dev->rx_req)
1578                 goto fail2;
1579
1580         return 0;
1581
1582 fail2:
1583         usb_ep_free_request(dev->in_ep, dev->tx_req);
1584 fail1:
1585         pr_err("can't alloc requests");
1586         return -1;
1587 }
1588
1589 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1590 {
1591         struct eth_dev  *dev = ep->driver_data;
1592
1593         debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1594         switch (req->status) {
1595         default:
1596                 dev->stats.tx_errors++;
1597                 debug("tx err %d\n", req->status);
1598                 /* FALLTHROUGH */
1599         case -ECONNRESET:               /* unlink */
1600         case -ESHUTDOWN:                /* disconnect etc */
1601                 break;
1602         case 0:
1603                 dev->stats.tx_bytes += req->length;
1604         }
1605         dev->stats.tx_packets++;
1606
1607         packet_sent = 1;
1608 }
1609
1610 static inline int eth_is_promisc(struct eth_dev *dev)
1611 {
1612         /* no filters for the CDC subset; always promisc */
1613         if (subset_active(dev))
1614                 return 1;
1615         return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1616 }
1617
1618 #if 0
1619 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1620 {
1621         struct eth_dev          *dev = netdev_priv(net);
1622         int                     length = skb->len;
1623         int                     retval;
1624         struct usb_request      *req = NULL;
1625         unsigned long           flags;
1626
1627         /* apply outgoing CDC or RNDIS filters */
1628         if (!eth_is_promisc (dev)) {
1629                 u8              *dest = skb->data;
1630
1631                 if (is_multicast_ethaddr(dest)) {
1632                         u16     type;
1633
1634                         /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1635                          * SET_ETHERNET_MULTICAST_FILTERS requests
1636                          */
1637                         if (is_broadcast_ethaddr(dest))
1638                                 type = USB_CDC_PACKET_TYPE_BROADCAST;
1639                         else
1640                                 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1641                         if (!(dev->cdc_filter & type)) {
1642                                 dev_kfree_skb_any (skb);
1643                                 return 0;
1644                         }
1645                 }
1646                 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1647         }
1648
1649         spin_lock_irqsave(&dev->req_lock, flags);
1650         /*
1651          * this freelist can be empty if an interrupt triggered disconnect()
1652          * and reconfigured the gadget (shutting down this queue) after the
1653          * network stack decided to xmit but before we got the spinlock.
1654          */
1655         if (list_empty(&dev->tx_reqs)) {
1656                 spin_unlock_irqrestore(&dev->req_lock, flags);
1657                 return 1;
1658         }
1659
1660         req = container_of (dev->tx_reqs.next, struct usb_request, list);
1661         list_del (&req->list);
1662
1663         /* temporarily stop TX queue when the freelist empties */
1664         if (list_empty (&dev->tx_reqs))
1665                 netif_stop_queue (net);
1666         spin_unlock_irqrestore(&dev->req_lock, flags);
1667
1668         /* no buffer copies needed, unless the network stack did it
1669          * or the hardware can't use skb buffers.
1670          * or there's not enough space for any RNDIS headers we need
1671          */
1672         if (rndis_active(dev)) {
1673                 struct sk_buff  *skb_rndis;
1674
1675                 skb_rndis = skb_realloc_headroom (skb,
1676                                 sizeof (struct rndis_packet_msg_type));
1677                 if (!skb_rndis)
1678                         goto drop;
1679
1680                 dev_kfree_skb_any (skb);
1681                 skb = skb_rndis;
1682                 rndis_add_hdr (skb);
1683                 length = skb->len;
1684         }
1685         req->buf = skb->data;
1686         req->context = skb;
1687         req->complete = tx_complete;
1688
1689         /* use zlp framing on tx for strict CDC-Ether conformance,
1690          * though any robust network rx path ignores extra padding.
1691          * and some hardware doesn't like to write zlps.
1692          */
1693         req->zero = 1;
1694         if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1695                 length++;
1696
1697         req->length = length;
1698
1699         /* throttle highspeed IRQ rate back slightly */
1700         if (gadget_is_dualspeed(dev->gadget))
1701                 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1702                         ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1703                         : 0;
1704
1705         retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1706         switch (retval) {
1707         default:
1708                 DEBUG (dev, "tx queue err %d\n", retval);
1709                 break;
1710         case 0:
1711                 net->trans_start = jiffies;
1712                 atomic_inc (&dev->tx_qlen);
1713         }
1714
1715         if (retval) {
1716 drop:
1717                 dev->stats.tx_dropped++;
1718                 dev_kfree_skb_any (skb);
1719                 spin_lock_irqsave(&dev->req_lock, flags);
1720                 if (list_empty (&dev->tx_reqs))
1721                         netif_start_queue (net);
1722                 list_add (&req->list, &dev->tx_reqs);
1723                 spin_unlock_irqrestore(&dev->req_lock, flags);
1724         }
1725         return 0;
1726 }
1727
1728 /*-------------------------------------------------------------------------*/
1729 #endif
1730
1731 static void eth_unbind(struct usb_gadget *gadget)
1732 {
1733         struct eth_dev          *dev = get_gadget_data(gadget);
1734
1735         debug("%s...\n", __func__);
1736         rndis_deregister(dev->rndis_config);
1737         rndis_exit();
1738
1739         /* we've already been disconnected ... no i/o is active */
1740         if (dev->req) {
1741                 usb_ep_free_request(gadget->ep0, dev->req);
1742                 dev->req = NULL;
1743         }
1744         if (dev->stat_req) {
1745                 usb_ep_free_request(dev->status_ep, dev->stat_req);
1746                 dev->stat_req = NULL;
1747         }
1748
1749         if (dev->tx_req) {
1750                 usb_ep_free_request(dev->in_ep, dev->tx_req);
1751                 dev->tx_req = NULL;
1752         }
1753
1754         if (dev->rx_req) {
1755                 usb_ep_free_request(dev->out_ep, dev->rx_req);
1756                 dev->rx_req = NULL;
1757         }
1758
1759 /*      unregister_netdev (dev->net);*/
1760 /*      free_netdev(dev->net);*/
1761
1762         dev->gadget = NULL;
1763         set_gadget_data(gadget, NULL);
1764 }
1765
1766 static void eth_disconnect(struct usb_gadget *gadget)
1767 {
1768         eth_reset_config(get_gadget_data(gadget));
1769         /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1770 }
1771
1772 static void eth_suspend(struct usb_gadget *gadget)
1773 {
1774         /* Not used */
1775 }
1776
1777 static void eth_resume(struct usb_gadget *gadget)
1778 {
1779         /* Not used */
1780 }
1781
1782 /*-------------------------------------------------------------------------*/
1783
1784 #ifdef CONFIG_USB_ETH_RNDIS
1785
1786 /*
1787  * The interrupt endpoint is used in RNDIS to notify the host when messages
1788  * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1789  * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1790  * REMOTE_NDIS_KEEPALIVE_MSG.
1791  *
1792  * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1793  * normally just one notification will be queued.
1794  */
1795
1796 static void rndis_control_ack_complete(struct usb_ep *ep,
1797                                         struct usb_request *req)
1798 {
1799         struct eth_dev          *dev = ep->driver_data;
1800
1801         debug("%s...\n", __func__);
1802         if (req->status || req->actual != req->length)
1803                 debug("rndis control ack complete --> %d, %d/%d\n",
1804                         req->status, req->actual, req->length);
1805
1806         if (!dev->network_started) {
1807                 if (rndis_get_state(dev->rndis_config)
1808                                 == RNDIS_DATA_INITIALIZED) {
1809                         dev->network_started = 1;
1810                         printf("USB RNDIS network up!\n");
1811                 }
1812         }
1813
1814         req->context = NULL;
1815
1816         if (req != dev->stat_req)
1817                 usb_ep_free_request(ep, req);
1818 }
1819
1820 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1821
1822 static int rndis_control_ack(struct udevice *net)
1823 {
1824         struct ether_priv *priv;
1825         struct eth_dev *dev;
1826         int length;
1827         struct usb_request *resp;
1828
1829         priv = dev_get_priv(net);
1830         dev = &priv->ethdev;
1831         resp = dev->stat_req;
1832
1833         /* in case RNDIS calls this after disconnect */
1834         if (!dev->status) {
1835                 debug("status ENODEV\n");
1836                 return -ENODEV;
1837         }
1838
1839         /* in case queue length > 1 */
1840         if (resp->context) {
1841                 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1842                 if (!resp)
1843                         return -ENOMEM;
1844                 resp->buf = rndis_resp_buf;
1845         }
1846
1847         /*
1848          * Send RNDIS RESPONSE_AVAILABLE notification;
1849          * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1850          */
1851         resp->length = 8;
1852         resp->complete = rndis_control_ack_complete;
1853         resp->context = dev;
1854
1855         *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1856         *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1857
1858         length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1859         if (length < 0) {
1860                 resp->status = 0;
1861                 rndis_control_ack_complete(dev->status_ep, resp);
1862         }
1863
1864         return 0;
1865 }
1866
1867 #else
1868
1869 #define rndis_control_ack       NULL
1870
1871 #endif  /* RNDIS */
1872
1873 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1874 {
1875         if (rndis_active(dev)) {
1876                 rndis_set_param_medium(dev->rndis_config,
1877                                         NDIS_MEDIUM_802_3,
1878                                         BITRATE(dev->gadget)/100);
1879                 rndis_signal_connect(dev->rndis_config);
1880         }
1881 }
1882
1883 static int eth_stop(struct eth_dev *dev)
1884 {
1885 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1886         unsigned long ts;
1887         unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1888 #endif
1889
1890         if (rndis_active(dev)) {
1891                 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1892                 rndis_signal_disconnect(dev->rndis_config);
1893
1894 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1895                 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1896                 ts = get_timer(0);
1897                 while (get_timer(ts) < timeout)
1898                         usb_gadget_handle_interrupts(0);
1899 #endif
1900
1901                 rndis_uninit(dev->rndis_config);
1902                 dev->rndis = 0;
1903         }
1904
1905         return 0;
1906 }
1907
1908 /*-------------------------------------------------------------------------*/
1909
1910 static int is_eth_addr_valid(char *str)
1911 {
1912         if (strlen(str) == 17) {
1913                 int i;
1914                 char *p, *q;
1915                 uchar ea[6];
1916
1917                 /* see if it looks like an ethernet address */
1918
1919                 p = str;
1920
1921                 for (i = 0; i < 6; i++) {
1922                         char term = (i == 5 ? '\0' : ':');
1923
1924                         ea[i] = simple_strtol(p, &q, 16);
1925
1926                         if ((q - p) != 2 || *q++ != term)
1927                                 break;
1928
1929                         p = q;
1930                 }
1931
1932                 /* Now check the contents. */
1933                 return is_valid_ethaddr(ea);
1934         }
1935         return 0;
1936 }
1937
1938 static u8 nibble(unsigned char c)
1939 {
1940         if (likely(isdigit(c)))
1941                 return c - '0';
1942         c = toupper(c);
1943         if (likely(isxdigit(c)))
1944                 return 10 + c - 'A';
1945         return 0;
1946 }
1947
1948 static int get_ether_addr(const char *str, u8 *dev_addr)
1949 {
1950         if (str) {
1951                 unsigned        i;
1952
1953                 for (i = 0; i < 6; i++) {
1954                         unsigned char num;
1955
1956                         if ((*str == '.') || (*str == ':'))
1957                                 str++;
1958                         num = nibble(*str++) << 4;
1959                         num |= (nibble(*str++));
1960                         dev_addr[i] = num;
1961                 }
1962                 if (is_valid_ethaddr(dev_addr))
1963                         return 0;
1964         }
1965         return 1;
1966 }
1967
1968 static int eth_bind(struct usb_gadget *gadget)
1969 {
1970         struct eth_dev          *dev = &l_priv->ethdev;
1971         u8                      cdc = 1, zlp = 1, rndis = 1;
1972         struct usb_ep           *in_ep, *out_ep, *status_ep = NULL;
1973         int                     status = -ENOMEM;
1974         int                     gcnum;
1975         u8                      tmp[7];
1976         struct eth_pdata        *pdata = dev_get_plat(l_priv->netdev);
1977
1978         /* these flags are only ever cleared; compiler take note */
1979 #ifndef CONFIG_USB_ETH_CDC
1980         cdc = 0;
1981 #endif
1982 #ifndef CONFIG_USB_ETH_RNDIS
1983         rndis = 0;
1984 #endif
1985         /*
1986          * Because most host side USB stacks handle CDC Ethernet, that
1987          * standard protocol is _strongly_ preferred for interop purposes.
1988          * (By everyone except Microsoft.)
1989          */
1990         if (gadget_is_musbhdrc(gadget)) {
1991                 /* reduce tx dma overhead by avoiding special cases */
1992                 zlp = 0;
1993         } else if (gadget_is_sh(gadget)) {
1994                 /* sh doesn't support multiple interfaces or configs */
1995                 cdc = 0;
1996                 rndis = 0;
1997         }
1998
1999         gcnum = usb_gadget_controller_number(gadget);
2000         if (gcnum >= 0)
2001                 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2002         else {
2003                 /*
2004                  * can't assume CDC works.  don't want to default to
2005                  * anything less functional on CDC-capable hardware,
2006                  * so we fail in this case.
2007                  */
2008                 pr_err("controller '%s' not recognized",
2009                         gadget->name);
2010                 return -ENODEV;
2011         }
2012
2013         /*
2014          * If there's an RNDIS configuration, that's what Windows wants to
2015          * be using ... so use these product IDs here and in the "linux.inf"
2016          * needed to install MSFT drivers.  Current Linux kernels will use
2017          * the second configuration if it's CDC Ethernet, and need some help
2018          * to choose the right configuration otherwise.
2019          */
2020         if (rndis) {
2021 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2022                 device_desc.idVendor =
2023                         __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2024                 device_desc.idProduct =
2025                         __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2026 #else
2027                 device_desc.idVendor =
2028                         __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2029                 device_desc.idProduct =
2030                         __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2031 #endif
2032                 sprintf(product_desc, "RNDIS/%s", driver_desc);
2033
2034         /*
2035          * CDC subset ... recognized by Linux since 2.4.10, but Windows
2036          * drivers aren't widely available.  (That may be improved by
2037          * supporting one submode of the "SAFE" variant of MDLM.)
2038          */
2039         } else {
2040 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2041                 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2042                 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2043 #else
2044                 if (!cdc) {
2045                         device_desc.idVendor =
2046                                 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2047                         device_desc.idProduct =
2048                                 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2049                 }
2050 #endif
2051         }
2052         /* support optional vendor/distro customization */
2053         if (bcdDevice)
2054                 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2055         if (iManufacturer)
2056                 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2057         if (iProduct)
2058                 strlcpy(product_desc, iProduct, sizeof product_desc);
2059         if (iSerialNumber) {
2060                 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2061                 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2062         }
2063
2064         /* all we really need is bulk IN/OUT */
2065         usb_ep_autoconfig_reset(gadget);
2066         in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2067         if (!in_ep) {
2068 autoconf_fail:
2069                 pr_err("can't autoconfigure on %s\n",
2070                         gadget->name);
2071                 return -ENODEV;
2072         }
2073         in_ep->driver_data = in_ep;     /* claim */
2074
2075         out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2076         if (!out_ep)
2077                 goto autoconf_fail;
2078         out_ep->driver_data = out_ep;   /* claim */
2079
2080 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2081         /*
2082          * CDC Ethernet control interface doesn't require a status endpoint.
2083          * Since some hosts expect one, try to allocate one anyway.
2084          */
2085         if (cdc || rndis) {
2086                 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2087                 if (status_ep) {
2088                         status_ep->driver_data = status_ep;     /* claim */
2089                 } else if (rndis) {
2090                         pr_err("can't run RNDIS on %s", gadget->name);
2091                         return -ENODEV;
2092 #ifdef CONFIG_USB_ETH_CDC
2093                 } else if (cdc) {
2094                         control_intf.bNumEndpoints = 0;
2095                         /* FIXME remove endpoint from descriptor list */
2096 #endif
2097                 }
2098         }
2099 #endif
2100
2101         /* one config:  cdc, else minimal subset */
2102         if (!cdc) {
2103                 eth_config.bNumInterfaces = 1;
2104                 eth_config.iConfiguration = STRING_SUBSET;
2105
2106                 /*
2107                  * use functions to set these up, in case we're built to work
2108                  * with multiple controllers and must override CDC Ethernet.
2109                  */
2110                 fs_subset_descriptors();
2111                 hs_subset_descriptors();
2112         }
2113
2114         usb_gadget_set_selfpowered(gadget);
2115
2116         /* For now RNDIS is always a second config */
2117         if (rndis)
2118                 device_desc.bNumConfigurations = 2;
2119
2120         if (gadget_is_dualspeed(gadget)) {
2121                 if (rndis)
2122                         dev_qualifier.bNumConfigurations = 2;
2123                 else if (!cdc)
2124                         dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2125
2126                 /* assumes ep0 uses the same value for both speeds ... */
2127                 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2128
2129                 /* and that all endpoints are dual-speed */
2130                 hs_source_desc.bEndpointAddress =
2131                                 fs_source_desc.bEndpointAddress;
2132                 hs_sink_desc.bEndpointAddress =
2133                                 fs_sink_desc.bEndpointAddress;
2134 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2135                 if (status_ep)
2136                         hs_status_desc.bEndpointAddress =
2137                                         fs_status_desc.bEndpointAddress;
2138 #endif
2139         }
2140
2141         if (gadget_is_otg(gadget)) {
2142                 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2143                 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2144                 eth_config.bMaxPower = 4;
2145 #ifdef  CONFIG_USB_ETH_RNDIS
2146                 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2147                 rndis_config.bMaxPower = 4;
2148 #endif
2149         }
2150
2151
2152         /* network device setup */
2153         dev->net = l_priv->netdev;
2154
2155         dev->cdc = cdc;
2156         dev->zlp = zlp;
2157
2158         dev->in_ep = in_ep;
2159         dev->out_ep = out_ep;
2160         dev->status_ep = status_ep;
2161
2162         memset(tmp, 0, sizeof(tmp));
2163         /*
2164          * Module params for these addresses should come from ID proms.
2165          * The host side address is used with CDC and RNDIS, and commonly
2166          * ends up in a persistent config database.  It's not clear if
2167          * host side code for the SAFE thing cares -- its original BLAN
2168          * thing didn't, Sharp never assigned those addresses on Zaurii.
2169          */
2170         get_ether_addr(dev_addr, pdata->enetaddr);
2171         memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2172
2173         get_ether_addr(host_addr, dev->host_mac);
2174
2175         sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2176                 dev->host_mac[0], dev->host_mac[1],
2177                         dev->host_mac[2], dev->host_mac[3],
2178                         dev->host_mac[4], dev->host_mac[5]);
2179
2180         if (rndis) {
2181                 status = rndis_init();
2182                 if (status < 0) {
2183                         pr_err("can't init RNDIS, %d", status);
2184                         goto fail;
2185                 }
2186         }
2187
2188         /*
2189          * use PKTSIZE (or aligned... from u-boot) and set
2190          * wMaxSegmentSize accordingly
2191          */
2192         dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2193
2194         /* preallocate control message data and buffer */
2195         dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2196         if (!dev->req)
2197                 goto fail;
2198         dev->req->buf = control_req;
2199         dev->req->complete = eth_setup_complete;
2200
2201         /* ... and maybe likewise for status transfer */
2202 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2203         if (dev->status_ep) {
2204                 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2205                                                         GFP_KERNEL);
2206                 if (!dev->stat_req) {
2207                         usb_ep_free_request(dev->status_ep, dev->req);
2208
2209                         goto fail;
2210                 }
2211                 dev->stat_req->buf = status_req;
2212                 dev->stat_req->context = NULL;
2213         }
2214 #endif
2215
2216         /* finish hookup to lower layer ... */
2217         dev->gadget = gadget;
2218         set_gadget_data(gadget, dev);
2219         gadget->ep0->driver_data = dev;
2220
2221         /*
2222          * two kinds of host-initiated state changes:
2223          *  - iff DATA transfer is active, carrier is "on"
2224          *  - tx queueing enabled if open *and* carrier is "on"
2225          */
2226
2227         printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2228                 out_ep->name, in_ep->name,
2229                 status_ep ? " STATUS " : "",
2230                 status_ep ? status_ep->name : ""
2231                 );
2232         printf("MAC %pM\n", pdata->enetaddr);
2233
2234         if (cdc || rndis)
2235                 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2236                         dev->host_mac[0], dev->host_mac[1],
2237                         dev->host_mac[2], dev->host_mac[3],
2238                         dev->host_mac[4], dev->host_mac[5]);
2239
2240         if (rndis) {
2241                 u32     vendorID = 0;
2242
2243                 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2244
2245                 dev->rndis_config = rndis_register(rndis_control_ack);
2246                 if (dev->rndis_config < 0) {
2247 fail0:
2248                         eth_unbind(gadget);
2249                         debug("RNDIS setup failed\n");
2250                         status = -ENODEV;
2251                         goto fail;
2252                 }
2253
2254                 /* these set up a lot of the OIDs that RNDIS needs */
2255                 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2256                 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2257                                         &dev->stats, &dev->cdc_filter))
2258                         goto fail0;
2259                 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2260                                         manufacturer))
2261                         goto fail0;
2262                 if (rndis_set_param_medium(dev->rndis_config,
2263                                         NDIS_MEDIUM_802_3, 0))
2264                         goto fail0;
2265                 printf("RNDIS ready\n");
2266         }
2267         return 0;
2268
2269 fail:
2270         pr_err("%s failed, status = %d", __func__, status);
2271         eth_unbind(gadget);
2272         return status;
2273 }
2274
2275 /*-------------------------------------------------------------------------*/
2276 static void usb_eth_stop(struct udevice *dev);
2277
2278 static int usb_eth_start(struct udevice *udev)
2279 {
2280         struct ether_priv *priv = dev_get_priv(udev);
2281         struct eth_dev *dev = &priv->ethdev;
2282         struct usb_gadget *gadget;
2283         unsigned long ts;
2284         unsigned long timeout = USB_CONNECT_TIMEOUT;
2285
2286         dev->network_started = 0;
2287
2288         packet_received = 0;
2289         packet_sent = 0;
2290
2291         gadget = dev->gadget;
2292         usb_gadget_connect(gadget);
2293
2294         if (env_get("cdc_connect_timeout"))
2295                 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
2296         ts = get_timer(0);
2297         while (!dev->network_started) {
2298                 /* Handle control-c and timeouts */
2299                 if (ctrlc() || (get_timer(ts) > timeout)) {
2300                         pr_err("The remote end did not respond in time.");
2301                         goto fail;
2302                 }
2303                 usb_gadget_handle_interrupts(0);
2304         }
2305
2306         packet_received = 0;
2307         rx_submit(dev, dev->rx_req, 0);
2308         return 0;
2309 fail:
2310         usb_eth_stop(udev);
2311         return -1;
2312 }
2313
2314 static int usb_eth_send(struct udevice *udev, void *packet, int length)
2315 {
2316         struct ether_priv *priv = dev_get_priv(udev);
2317         int                     retval;
2318         void                    *rndis_pkt = NULL;
2319         struct eth_dev          *dev = &priv->ethdev;
2320         struct usb_request      *req = dev->tx_req;
2321         unsigned long ts;
2322         unsigned long timeout = USB_CONNECT_TIMEOUT;
2323
2324         debug("%s:...\n", __func__);
2325
2326         /* new buffer is needed to include RNDIS header */
2327         if (rndis_active(dev)) {
2328                 rndis_pkt = malloc(length +
2329                                         sizeof(struct rndis_packet_msg_type));
2330                 if (!rndis_pkt) {
2331                         pr_err("No memory to alloc RNDIS packet");
2332                         goto drop;
2333                 }
2334                 rndis_add_hdr(rndis_pkt, length);
2335                 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2336                                 packet, length);
2337                 packet = rndis_pkt;
2338                 length += sizeof(struct rndis_packet_msg_type);
2339         }
2340         req->buf = packet;
2341         req->context = NULL;
2342         req->complete = tx_complete;
2343
2344         /*
2345          * use zlp framing on tx for strict CDC-Ether conformance,
2346          * though any robust network rx path ignores extra padding.
2347          * and some hardware doesn't like to write zlps.
2348          */
2349         req->zero = 1;
2350         if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2351                 length++;
2352
2353         req->length = length;
2354 #if 0
2355         /* throttle highspeed IRQ rate back slightly */
2356         if (gadget_is_dualspeed(dev->gadget))
2357                 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2358                         ? ((dev->tx_qlen % qmult) != 0) : 0;
2359 #endif
2360         dev->tx_qlen = 1;
2361         ts = get_timer(0);
2362         packet_sent = 0;
2363
2364         retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2365
2366         if (!retval)
2367                 debug("%s: packet queued\n", __func__);
2368         while (!packet_sent) {
2369                 if (get_timer(ts) > timeout) {
2370                         printf("timeout sending packets to usb ethernet\n");
2371                         return -1;
2372                 }
2373                 usb_gadget_handle_interrupts(0);
2374         }
2375         free(rndis_pkt);
2376
2377         return 0;
2378 drop:
2379         dev->stats.tx_dropped++;
2380         return -ENOMEM;
2381 }
2382
2383 static void usb_eth_stop(struct udevice *udev)
2384 {
2385         struct ether_priv *priv = dev_get_priv(udev);
2386         struct eth_dev *dev = &priv->ethdev;
2387
2388         /* If the gadget not registered, simple return */
2389         if (!dev->gadget)
2390                 return;
2391
2392         /*
2393          * Some USB controllers may need additional deinitialization here
2394          * before dropping pull-up (also due to hardware issues).
2395          * For example: unhandled interrupt with status stage started may
2396          * bring the controller to fully broken state (until board reset).
2397          * There are some variants to debug and fix such cases:
2398          * 1) In the case of RNDIS connection eth_stop can perform additional
2399          * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2400          * 2) 'pullup' callback in your UDC driver can be improved to perform
2401          * this deinitialization.
2402          */
2403         eth_stop(dev);
2404
2405         usb_gadget_disconnect(dev->gadget);
2406
2407         /* Clear pending interrupt */
2408         if (dev->network_started) {
2409                 usb_gadget_handle_interrupts(0);
2410                 dev->network_started = 0;
2411         }
2412 }
2413
2414 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2415 {
2416         struct ether_priv *priv = dev_get_priv(dev);
2417         struct eth_dev *ethdev = &priv->ethdev;
2418
2419         usb_gadget_handle_interrupts(0);
2420
2421         if (packet_received) {
2422                 if (ethdev->rx_req) {
2423                         *packetp = (uchar *)net_rx_packets[0];
2424                         return ethdev->rx_req->length;
2425                 } else {
2426                         pr_err("dev->rx_req invalid");
2427                         return -EFAULT;
2428                 }
2429         }
2430
2431         return -EAGAIN;
2432 }
2433
2434 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2435                                    int length)
2436 {
2437         struct ether_priv *priv = dev_get_priv(dev);
2438         struct eth_dev *ethdev = &priv->ethdev;
2439
2440         packet_received = 0;
2441
2442         return rx_submit(ethdev, ethdev->rx_req, 0);
2443 }
2444
2445 static const struct eth_ops usb_eth_ops = {
2446         .start          = usb_eth_start,
2447         .send           = usb_eth_send,
2448         .recv           = usb_eth_recv,
2449         .free_pkt       = usb_eth_free_pkt,
2450         .stop           = usb_eth_stop,
2451 };
2452
2453 int usb_ether_init(void)
2454 {
2455         struct udevice *usb_dev;
2456         int ret;
2457
2458         uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2459         if (!usb_dev) {
2460                 pr_err("No USB device found\n");
2461                 return -ENODEV;
2462         }
2463
2464         ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", NULL);
2465         if (ret) {
2466                 pr_err("usb - not able to bind usb_ether device\n");
2467                 return ret;
2468         }
2469
2470         return usb_gadget_initialize(0);
2471 }
2472
2473 static int usb_eth_probe(struct udevice *dev)
2474 {
2475         struct ether_priv *priv = dev_get_priv(dev);
2476         struct eth_pdata *pdata = dev_get_plat(dev);
2477
2478         priv->netdev = dev;
2479         l_priv = priv;
2480
2481         get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
2482         eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2483
2484         /* Configure default mac-addresses for the USB ethernet device */
2485 #ifdef CONFIG_USBNET_DEV_ADDR
2486         strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2487 #endif
2488 #ifdef CONFIG_USBNET_HOST_ADDR
2489         strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2490 #endif
2491         /* Check if the user overruled the MAC addresses */
2492         if (env_get("usbnet_devaddr"))
2493                 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2494                         sizeof(dev_addr));
2495
2496         if (env_get("usbnet_hostaddr"))
2497                 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2498                         sizeof(host_addr));
2499
2500         if (!is_eth_addr_valid(dev_addr)) {
2501                 pr_err("Need valid 'usbnet_devaddr' to be set");
2502                 return -EINVAL;
2503         }
2504         if (!is_eth_addr_valid(host_addr)) {
2505                 pr_err("Need valid 'usbnet_hostaddr' to be set");
2506                 return -EINVAL;
2507         }
2508
2509         priv->eth_driver.speed          = DEVSPEED;
2510         priv->eth_driver.bind           = eth_bind;
2511         priv->eth_driver.unbind         = eth_unbind;
2512         priv->eth_driver.setup          = eth_setup;
2513         priv->eth_driver.reset          = eth_disconnect;
2514         priv->eth_driver.disconnect     = eth_disconnect;
2515         priv->eth_driver.suspend        = eth_suspend;
2516         priv->eth_driver.resume         = eth_resume;
2517         return usb_gadget_register_driver(&priv->eth_driver);
2518 }
2519
2520 static int usb_eth_remove(struct udevice *dev)
2521 {
2522         struct ether_priv *priv = dev_get_priv(dev);
2523
2524         usb_gadget_unregister_driver(&priv->eth_driver);
2525
2526         return 0;
2527 }
2528
2529 static int usb_eth_unbind(struct udevice *dev)
2530 {
2531         usb_gadget_release(0);
2532
2533         return 0;
2534 }
2535
2536 U_BOOT_DRIVER(eth_usb) = {
2537         .name   = "usb_ether",
2538         .id     = UCLASS_ETH,
2539         .probe  = usb_eth_probe,
2540         .remove = usb_eth_remove,
2541         .unbind = usb_eth_unbind,
2542         .ops    = &usb_eth_ops,
2543         .priv_auto      = sizeof(struct ether_priv),
2544         .plat_auto      = sizeof(struct eth_pdata),
2545         .flags = DM_FLAG_ALLOC_PRIV_DMA,
2546 };