]> git.sur5r.net Git - u-boot/blob - drivers/usb/gadget/ether.c
3d871fa10ed92ce1c8db5fa013bba366aabb0d4c
[u-boot] / drivers / usb / gadget / ether.c
1 /*
2  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3  *
4  * Copyright (C) 2003-2005,2008 David Brownell
5  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6  * Copyright (C) 2008 Nokia Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <common.h>
24 #include <asm/errno.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/cdc.h>
27 #include <linux/usb/gadget.h>
28 #include <net.h>
29 #include <linux/ctype.h>
30
31 #include "gadget_chips.h"
32
33 #define USB_NET_NAME "usb0"
34 #define dprintf(x, ...)
35 #undef INFO
36 #define INFO(x, s...)   printf(s)
37 #define dev_err(x, stuff...) printf(stuff)
38 #define dev_dbg dev_err
39 #define dev_warn dev_err
40 #define DEBUG dev_err
41 #define VDEBUG DEBUG
42 #define atomic_read
43 extern struct platform_data brd;
44 #define spin_lock(x)
45 #define spin_unlock(x)
46
47
48 unsigned packet_received, packet_sent;
49
50 #define DEV_CONFIG_CDC  1
51 #define GFP_ATOMIC ((gfp_t) 0)
52 #define GFP_KERNEL ((gfp_t) 0)
53
54 /*
55  * Ethernet gadget driver -- with CDC and non-CDC options
56  * Builds on hardware support for a full duplex link.
57  *
58  * CDC Ethernet is the standard USB solution for sending Ethernet frames
59  * using USB.  Real hardware tends to use the same framing protocol but look
60  * different for control features.  This driver strongly prefers to use
61  * this USB-IF standard as its open-systems interoperability solution;
62  * most host side USB stacks (except from Microsoft) support it.
63  *
64  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
65  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
66  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
67  *
68  * There's some hardware that can't talk CDC ECM.  We make that hardware
69  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
70  * link-level setup only requires activating the configuration.  Only the
71  * endpoint descriptors, and product/vendor IDs, are relevant; no control
72  * operations are available.  Linux supports it, but other host operating
73  * systems may not.  (This is a subset of CDC Ethernet.)
74  *
75  * It turns out that if you add a few descriptors to that "CDC Subset",
76  * (Windows) host side drivers from MCCI can treat it as one submode of
77  * a proprietary scheme called "SAFE" ... without needing to know about
78  * specific product/vendor IDs.  So we do that, making it easier to use
79  * those MS-Windows drivers.  Those added descriptors make it resemble a
80  * CDC MDLM device, but they don't change device behavior at all.  (See
81  * MCCI Engineering report 950198 "SAFE Networking Functions".)
82  *
83  * A third option is also in use.  Rather than CDC Ethernet, or something
84  * simpler, Microsoft pushes their own approach: RNDIS.  The published
85  * RNDIS specs are ambiguous and appear to be incomplete, and are also
86  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
87  */
88 #define ETH_ALEN        6               /* Octets in one ethernet addr   */
89 #define ETH_HLEN        14              /* Total octets in header.       */
90 #define ETH_ZLEN        60              /* Min. octets in frame sans FCS */
91 #define ETH_DATA_LEN    1500            /* Max. octets in payload        */
92 #define ETH_FRAME_LEN   PKTSIZE_ALIGN   /* Max. octets in frame sans FCS */
93 #define ETH_FCS_LEN     4               /* Octets in the FCS             */
94
95 #define DRIVER_DESC             "Ethernet Gadget"
96 /* Based on linux 2.6.27 version */
97 #define DRIVER_VERSION          "May Day 2005"
98
99 static const char shortname [] = "ether";
100 static const char driver_desc [] = DRIVER_DESC;
101
102 #define RX_EXTRA        20              /* guard against rx overflows */
103
104 /* CDC support the same host-chosen outgoing packet filters. */
105 #define DEFAULT_FILTER  (USB_CDC_PACKET_TYPE_BROADCAST \
106                         |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
107                         |USB_CDC_PACKET_TYPE_PROMISCUOUS \
108                         |USB_CDC_PACKET_TYPE_DIRECTED)
109
110 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
111
112 /*-------------------------------------------------------------------------*/
113 static struct eth_dev l_ethdev;
114 static struct eth_device l_netdev;
115 static struct usb_gadget_driver eth_driver;
116
117 /*-------------------------------------------------------------------------*/
118
119 /* "main" config is either CDC, or its simple subset */
120 static inline int is_cdc(struct eth_dev *dev)
121 {
122 #if     !defined(DEV_CONFIG_SUBSET)
123         return 1;               /* only cdc possible */
124 #elif   !defined (DEV_CONFIG_CDC)
125         return 0;               /* only subset possible */
126 #else
127         return dev->cdc;        /* depends on what hardware we found */
128 #endif
129 }
130
131 #define subset_active(dev)      (!is_cdc(dev))
132 #define cdc_active(dev)         ( is_cdc(dev))
133
134 #define DEFAULT_QLEN    2       /* double buffering by default */
135
136 /* peak bulk transfer bits-per-second */
137 #define HS_BPS          (13 * 512 * 8 * 1000 * 8)
138 #define FS_BPS          (19 *  64 * 1 * 1000 * 8)
139
140 #ifdef CONFIG_USB_GADGET_DUALSPEED
141 #define DEVSPEED        USB_SPEED_HIGH
142
143 /* for dual-speed hardware, use deeper queues at highspeed */
144 #define qlen(gadget) \
145         (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
146
147 static inline int BITRATE(struct usb_gadget *g)
148 {
149         return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
150 }
151
152 #else   /* full speed (low speed doesn't do bulk) */
153
154 #define qmult           1
155
156 #define DEVSPEED        USB_SPEED_FULL
157
158 #define qlen(gadget) DEFAULT_QLEN
159
160 static inline int BITRATE(struct usb_gadget *g)
161 {
162         return FS_BPS;
163 }
164 #endif
165
166 struct eth_dev {
167         struct usb_gadget       *gadget;
168         struct usb_request      *req;           /* for control responses */
169         struct usb_request      *stat_req;      /* for cdc status */
170
171         u8                      config;
172         struct usb_ep           *in_ep, *out_ep, *status_ep;
173         const struct usb_endpoint_descriptor
174                                 *in, *out, *status;
175
176         struct usb_request      *tx_req, *rx_req;
177
178         struct eth_device       *net;
179         unsigned int            tx_qlen;
180
181         unsigned                zlp:1;
182         unsigned                cdc:1;
183         unsigned                suspended:1;
184         unsigned                network_started:1;
185         u16                     cdc_filter;
186         unsigned long           todo;
187         int                     mtu;
188 #define WORK_RX_MEMORY          0
189         u8                      host_mac [ETH_ALEN];
190 };
191
192 /* This version autoconfigures as much as possible at run-time.
193  *
194  * It also ASSUMES a self-powered device, without remote wakeup,
195  * although remote wakeup support would make sense.
196  */
197
198 /*-------------------------------------------------------------------------*/
199
200 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
201  * Instead:  allocate your own, using normal USB-IF procedures.
202  */
203
204 /* Thanks to NetChip Technologies for donating this product ID.
205  * It's for devices with only CDC Ethernet configurations.
206  */
207 #define CDC_VENDOR_NUM          0x0525  /* NetChip */
208 #define CDC_PRODUCT_NUM         0xa4a1  /* Linux-USB Ethernet Gadget */
209
210 /* For hardware that can't talk CDC, we use the same vendor ID that
211  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
212  * with pxa250.  We're protocol-compatible, if the host-side drivers
213  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
214  * drivers that need to hard-wire endpoint numbers have a hook.
215  *
216  * The protocol is a minimal subset of CDC Ether, which works on any bulk
217  * hardware that's not deeply broken ... even on hardware that can't talk
218  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
219  * doesn't handle control-OUT).
220  */
221 #define SIMPLE_VENDOR_NUM       0x049f
222 #define SIMPLE_PRODUCT_NUM      0x505a
223
224 /* Some systems will want different product identifers published in the
225  * device descriptor, either numbers or strings or both.  These string
226  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
227  */
228
229 static ushort bcdDevice;
230 #if defined(CONFIG_USBNET_MANUFACTURER)
231 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
232 #else
233 static char *iManufacturer = "U-boot";
234 #endif
235 static char *iProduct;
236 static char *iSerialNumber;
237 static char dev_addr[18];
238 static char host_addr[18];
239
240 /*-------------------------------------------------------------------------*/
241
242 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
243  * ep0 implementation:  descriptors, config management, setup().
244  * also optional class-specific notification interrupt transfer.
245  */
246
247 /*
248  * DESCRIPTORS ... most are static, but strings and (full) configuration
249  * descriptors are built on demand.  For now we do either full CDC, or
250  * our simple subset.
251  */
252
253 #define STRING_MANUFACTURER             1
254 #define STRING_PRODUCT                  2
255 #define STRING_ETHADDR                  3
256 #define STRING_DATA                     4
257 #define STRING_CONTROL                  5
258 #define STRING_CDC                      7
259 #define STRING_SUBSET                   8
260 #define STRING_SERIALNUMBER             10
261
262 /* holds our biggest descriptor */
263 #define USB_BUFSIZ      256
264
265 /*
266  * This device advertises one configuration, eth_config,
267  * on hardware supporting at least two configs.
268  *
269  * FIXME define some higher-powered configurations to make it easier
270  * to recharge batteries ...
271  */
272
273 #define DEV_CONFIG_VALUE        1       /* cdc or subset */
274
275 static struct usb_device_descriptor
276 device_desc = {
277         .bLength =              sizeof device_desc,
278         .bDescriptorType =      USB_DT_DEVICE,
279
280         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
281
282         .bDeviceClass =         USB_CLASS_COMM,
283         .bDeviceSubClass =      0,
284         .bDeviceProtocol =      0,
285
286         .idVendor =             __constant_cpu_to_le16 (CDC_VENDOR_NUM),
287         .idProduct =            __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
288         .iManufacturer =        STRING_MANUFACTURER,
289         .iProduct =             STRING_PRODUCT,
290         .bNumConfigurations =   1,
291 };
292
293 static struct usb_otg_descriptor
294 otg_descriptor = {
295         .bLength =              sizeof otg_descriptor,
296         .bDescriptorType =      USB_DT_OTG,
297
298         .bmAttributes =         USB_OTG_SRP,
299 };
300
301 static struct usb_config_descriptor
302 eth_config = {
303         .bLength =              sizeof eth_config,
304         .bDescriptorType =      USB_DT_CONFIG,
305
306         /* compute wTotalLength on the fly */
307         .bNumInterfaces =       2,
308         .bConfigurationValue =  DEV_CONFIG_VALUE,
309         .iConfiguration =       STRING_CDC,
310         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
311         .bMaxPower =            1,
312 };
313
314 /*
315  * Compared to the simple CDC subset, the full CDC Ethernet model adds
316  * three class descriptors, two interface descriptors, optional status
317  * endpoint.  Both have a "data" interface and two bulk endpoints.
318  * There are also differences in how control requests are handled.
319  */
320
321 #ifdef  DEV_CONFIG_CDC
322 static struct usb_interface_descriptor
323 control_intf = {
324         .bLength =              sizeof control_intf,
325         .bDescriptorType =      USB_DT_INTERFACE,
326
327         .bInterfaceNumber =     0,
328         /* status endpoint is optional; this may be patched later */
329         .bNumEndpoints =        1,
330         .bInterfaceClass =      USB_CLASS_COMM,
331         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ETHERNET,
332         .bInterfaceProtocol =   USB_CDC_PROTO_NONE,
333         .iInterface =           STRING_CONTROL,
334 };
335 #endif
336
337 static const struct usb_cdc_header_desc header_desc = {
338         .bLength =              sizeof header_desc,
339         .bDescriptorType =      USB_DT_CS_INTERFACE,
340         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
341
342         .bcdCDC =               __constant_cpu_to_le16 (0x0110),
343 };
344
345 #if defined(DEV_CONFIG_CDC)
346
347 static const struct usb_cdc_union_desc union_desc = {
348         .bLength =              sizeof union_desc,
349         .bDescriptorType =      USB_DT_CS_INTERFACE,
350         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
351
352         .bMasterInterface0 =    0,      /* index of control interface */
353         .bSlaveInterface0 =     1,      /* index of DATA interface */
354 };
355
356 #endif  /* CDC */
357
358 #ifndef DEV_CONFIG_CDC
359
360 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
361  * ways:  data endpoints live in the control interface, there's no data
362  * interface, and it's not used to talk to a cell phone radio.
363  */
364
365 static const struct usb_cdc_mdlm_desc mdlm_desc = {
366         .bLength =              sizeof mdlm_desc,
367         .bDescriptorType =      USB_DT_CS_INTERFACE,
368         .bDescriptorSubType =   USB_CDC_MDLM_TYPE,
369
370         .bcdVersion =           __constant_cpu_to_le16(0x0100),
371         .bGUID = {
372                 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
373                 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
374         },
375 };
376
377 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
378  * can't really use its struct.  All we do here is say that we're using
379  * the submode of "SAFE" which directly matches the CDC Subset.
380  */
381 static const u8 mdlm_detail_desc[] = {
382         6,
383         USB_DT_CS_INTERFACE,
384         USB_CDC_MDLM_DETAIL_TYPE,
385
386         0,      /* "SAFE" */
387         0,      /* network control capabilities (none) */
388         0,      /* network data capabilities ("raw" encapsulation) */
389 };
390
391 #endif
392
393
394 static const struct usb_cdc_ether_desc ether_desc = {
395         .bLength =              sizeof (ether_desc),
396         .bDescriptorType =      USB_DT_CS_INTERFACE,
397         .bDescriptorSubType =   USB_CDC_ETHERNET_TYPE,
398
399         /* this descriptor actually adds value, surprise! */
400         .iMACAddress =          STRING_ETHADDR,
401         .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
402         .wMaxSegmentSize =      __constant_cpu_to_le16 (ETH_FRAME_LEN),
403         .wNumberMCFilters =     __constant_cpu_to_le16 (0),
404         .bNumberPowerFilters =  0,
405 };
406
407
408 #if defined(DEV_CONFIG_CDC)
409
410 /* include the status endpoint if we can, even where it's optional.
411  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
412  * packet, to simplify cancellation; and a big transfer interval, to
413  * waste less bandwidth.
414  *
415  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
416  * if they ignore the connect/disconnect notifications that real aether
417  * can provide.  more advanced cdc configurations might want to support
418  * encapsulated commands (vendor-specific, using control-OUT).
419  */
420
421 #define LOG2_STATUS_INTERVAL_MSEC       5       /* 1 << 5 == 32 msec */
422 #define STATUS_BYTECOUNT                16      /* 8 byte header + data */
423
424 static struct usb_endpoint_descriptor
425 fs_status_desc = {
426         .bLength =              USB_DT_ENDPOINT_SIZE,
427         .bDescriptorType =      USB_DT_ENDPOINT,
428
429         .bEndpointAddress =     USB_DIR_IN,
430         .bmAttributes =         USB_ENDPOINT_XFER_INT,
431         .wMaxPacketSize =       __constant_cpu_to_le16 (STATUS_BYTECOUNT),
432         .bInterval =            1 << LOG2_STATUS_INTERVAL_MSEC,
433 };
434 #endif
435
436 #ifdef  DEV_CONFIG_CDC
437
438 /* the default data interface has no endpoints ... */
439
440 static const struct usb_interface_descriptor
441 data_nop_intf = {
442         .bLength =              sizeof data_nop_intf,
443         .bDescriptorType =      USB_DT_INTERFACE,
444
445         .bInterfaceNumber =     1,
446         .bAlternateSetting =    0,
447         .bNumEndpoints =        0,
448         .bInterfaceClass =      USB_CLASS_CDC_DATA,
449         .bInterfaceSubClass =   0,
450         .bInterfaceProtocol =   0,
451 };
452
453 /* ... but the "real" data interface has two bulk endpoints */
454
455 static const struct usb_interface_descriptor
456 data_intf = {
457         .bLength =              sizeof data_intf,
458         .bDescriptorType =      USB_DT_INTERFACE,
459
460         .bInterfaceNumber =     1,
461         .bAlternateSetting =    1,
462         .bNumEndpoints =        2,
463         .bInterfaceClass =      USB_CLASS_CDC_DATA,
464         .bInterfaceSubClass =   0,
465         .bInterfaceProtocol =   0,
466         .iInterface =           STRING_DATA,
467 };
468
469 #endif
470
471 #ifdef DEV_CONFIG_SUBSET
472
473 /*
474  * "Simple" CDC-subset option is a simple vendor-neutral model that most
475  * full speed controllers can handle:  one interface, two bulk endpoints.
476  *
477  * To assist host side drivers, we fancy it up a bit, and add descriptors
478  * so some host side drivers will understand it as a "SAFE" variant.
479  */
480
481 static const struct usb_interface_descriptor
482 subset_data_intf = {
483         .bLength =              sizeof subset_data_intf,
484         .bDescriptorType =      USB_DT_INTERFACE,
485
486         .bInterfaceNumber =     0,
487         .bAlternateSetting =    0,
488         .bNumEndpoints =        2,
489         .bInterfaceClass =      USB_CLASS_COMM,
490         .bInterfaceSubClass =   USB_CDC_SUBCLASS_MDLM,
491         .bInterfaceProtocol =   0,
492         .iInterface =           STRING_DATA,
493 };
494
495 #endif  /* SUBSET */
496
497
498 static struct usb_endpoint_descriptor
499 fs_source_desc = {
500         .bLength =              USB_DT_ENDPOINT_SIZE,
501         .bDescriptorType =      USB_DT_ENDPOINT,
502
503         .bEndpointAddress =     USB_DIR_IN,
504         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
505 };
506
507 static struct usb_endpoint_descriptor
508 fs_sink_desc = {
509         .bLength =              USB_DT_ENDPOINT_SIZE,
510         .bDescriptorType =      USB_DT_ENDPOINT,
511
512         .bEndpointAddress =     USB_DIR_OUT,
513         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
514 };
515
516 static const struct usb_descriptor_header *fs_eth_function [11] = {
517         (struct usb_descriptor_header *) &otg_descriptor,
518 #ifdef DEV_CONFIG_CDC
519         /* "cdc" mode descriptors */
520         (struct usb_descriptor_header *) &control_intf,
521         (struct usb_descriptor_header *) &header_desc,
522         (struct usb_descriptor_header *) &union_desc,
523         (struct usb_descriptor_header *) &ether_desc,
524         /* NOTE: status endpoint may need to be removed */
525         (struct usb_descriptor_header *) &fs_status_desc,
526         /* data interface, with altsetting */
527         (struct usb_descriptor_header *) &data_nop_intf,
528         (struct usb_descriptor_header *) &data_intf,
529         (struct usb_descriptor_header *) &fs_source_desc,
530         (struct usb_descriptor_header *) &fs_sink_desc,
531         NULL,
532 #endif /* DEV_CONFIG_CDC */
533 };
534
535 static inline void fs_subset_descriptors(void)
536 {
537 #ifdef DEV_CONFIG_SUBSET
538         /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
539         fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
540         fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
541         fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
542         fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
543         fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
544         fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
545         fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
546         fs_eth_function[8] = NULL;
547 #else
548         fs_eth_function[1] = NULL;
549 #endif
550 }
551
552 /*
553  * usb 2.0 devices need to expose both high speed and full speed
554  * descriptors, unless they only run at full speed.
555  */
556
557 #if defined(DEV_CONFIG_CDC)
558 static struct usb_endpoint_descriptor
559 hs_status_desc = {
560         .bLength =              USB_DT_ENDPOINT_SIZE,
561         .bDescriptorType =      USB_DT_ENDPOINT,
562
563         .bmAttributes =         USB_ENDPOINT_XFER_INT,
564         .wMaxPacketSize =       __constant_cpu_to_le16 (STATUS_BYTECOUNT),
565         .bInterval =            LOG2_STATUS_INTERVAL_MSEC + 4,
566 };
567 #endif /* DEV_CONFIG_CDC */
568
569 static struct usb_endpoint_descriptor
570 hs_source_desc = {
571         .bLength =              USB_DT_ENDPOINT_SIZE,
572         .bDescriptorType =      USB_DT_ENDPOINT,
573
574         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
575         .wMaxPacketSize =       __constant_cpu_to_le16 (512),
576 };
577
578 static struct usb_endpoint_descriptor
579 hs_sink_desc = {
580         .bLength =              USB_DT_ENDPOINT_SIZE,
581         .bDescriptorType =      USB_DT_ENDPOINT,
582
583         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
584         .wMaxPacketSize =       __constant_cpu_to_le16 (512),
585 };
586
587 static struct usb_qualifier_descriptor
588 dev_qualifier = {
589         .bLength =              sizeof dev_qualifier,
590         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
591
592         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
593         .bDeviceClass =         USB_CLASS_COMM,
594
595         .bNumConfigurations =   1,
596 };
597
598 static const struct usb_descriptor_header *hs_eth_function [11] = {
599         (struct usb_descriptor_header *) &otg_descriptor,
600 #ifdef DEV_CONFIG_CDC
601         /* "cdc" mode descriptors */
602         (struct usb_descriptor_header *) &control_intf,
603         (struct usb_descriptor_header *) &header_desc,
604         (struct usb_descriptor_header *) &union_desc,
605         (struct usb_descriptor_header *) &ether_desc,
606         /* NOTE: status endpoint may need to be removed */
607         (struct usb_descriptor_header *) &hs_status_desc,
608         /* data interface, with altsetting */
609         (struct usb_descriptor_header *) &data_nop_intf,
610         (struct usb_descriptor_header *) &data_intf,
611         (struct usb_descriptor_header *) &hs_source_desc,
612         (struct usb_descriptor_header *) &hs_sink_desc,
613         NULL,
614 #endif /* DEV_CONFIG_CDC */
615 };
616
617 static inline void hs_subset_descriptors(void)
618 {
619 #ifdef DEV_CONFIG_SUBSET
620         /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
621         hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
622         hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
623         hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
624         hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
625         hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
626         hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
627         hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
628         hs_eth_function[8] = NULL;
629 #else
630         hs_eth_function[1] = NULL;
631 #endif
632 }
633
634 /* maxpacket and other transfer characteristics vary by speed. */
635 static inline struct usb_endpoint_descriptor *
636 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
637                 struct usb_endpoint_descriptor *fs)
638 {
639         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
640                 return hs;
641         return fs;
642 }
643
644
645 /*-------------------------------------------------------------------------*/
646
647 /* descriptors that are built on-demand */
648
649 static char manufacturer [50];
650 static char product_desc [40] = DRIVER_DESC;
651 static char serial_number [20];
652
653 /* address that the host will use ... usually assigned at random */
654 static char ethaddr [2 * ETH_ALEN + 1];
655
656 /* static strings, in UTF-8 */
657 static struct usb_string                strings [] = {
658         { STRING_MANUFACTURER,  manufacturer, },
659         { STRING_PRODUCT,       product_desc, },
660         { STRING_SERIALNUMBER,  serial_number, },
661         { STRING_DATA,          "Ethernet Data", },
662         { STRING_ETHADDR,       ethaddr, },
663 #ifdef  DEV_CONFIG_CDC
664         { STRING_CDC,           "CDC Ethernet", },
665         { STRING_CONTROL,       "CDC Communications Control", },
666 #endif
667 #ifdef  DEV_CONFIG_SUBSET
668         { STRING_SUBSET,        "CDC Ethernet Subset", },
669 #endif
670         {  }            /* end of list */
671 };
672
673 static struct usb_gadget_strings        stringtab = {
674         .language       = 0x0409,       /* en-us */
675         .strings        = strings,
676 };
677
678
679 /*============================================================================*/
680 static u8 control_req[USB_BUFSIZ];
681 static u8 status_req[STATUS_BYTECOUNT];
682
683
684
685 /**
686  * strlcpy - Copy a %NUL terminated string into a sized buffer
687  * @dest: Where to copy the string to
688  * @src: Where to copy the string from
689  * @size: size of destination buffer
690  *
691  * Compatible with *BSD: the result is always a valid
692  * NUL-terminated string that fits in the buffer (unless,
693  * of course, the buffer size is zero). It does not pad
694  * out the result like strncpy() does.
695  */
696 size_t strlcpy(char *dest, const char *src, size_t size)
697 {
698         size_t ret = strlen(src);
699
700         if (size) {
701                 size_t len = (ret >= size) ? size - 1 : ret;
702                 memcpy(dest, src, len);
703                 dest[len] = '\0';
704         }
705         return ret;
706 }
707
708
709 /*============================================================================*/
710
711 /*
712  * one config, two interfaces:  control, data.
713  * complications: class descriptors, and an altsetting.
714  */
715 static int
716 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
717 {
718         int                                     len;
719         const struct usb_config_descriptor      *config;
720         const struct usb_descriptor_header      **function;
721         int                                     hs = 0;
722
723         if (gadget_is_dualspeed(g)) {
724                 hs = (g->speed == USB_SPEED_HIGH);
725                 if (type == USB_DT_OTHER_SPEED_CONFIG)
726                         hs = !hs;
727         }
728 #define which_fn(t)     (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
729
730         if (index >= device_desc.bNumConfigurations)
731                 return -EINVAL;
732
733         config = &eth_config;
734         function = which_fn (eth);
735
736         /* for now, don't advertise srp-only devices */
737         if (!is_otg)
738                 function++;
739
740         len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
741         if (len < 0)
742                 return len;
743         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
744         return len;
745 }
746
747 /*-------------------------------------------------------------------------*/
748
749 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
750
751 static int
752 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
753 {
754         int                                     result = 0;
755         struct usb_gadget                       *gadget = dev->gadget;
756
757 #if defined(DEV_CONFIG_CDC)
758         /* status endpoint used for (optionally) CDC */
759         if (!subset_active(dev) && dev->status_ep) {
760                 dev->status = ep_desc (gadget, &hs_status_desc,
761                                                 &fs_status_desc);
762                 dev->status_ep->driver_data = dev;
763
764                 result = usb_ep_enable (dev->status_ep, dev->status);
765                 if (result != 0) {
766                         printf ("enable %s --> %d\n",
767                                 dev->status_ep->name, result);
768                         goto done;
769                 }
770         }
771 #endif
772
773         dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
774         dev->in_ep->driver_data = dev;
775
776         dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
777         dev->out_ep->driver_data = dev;
778
779         /* With CDC,  the host isn't allowed to use these two data
780          * endpoints in the default altsetting for the interface.
781          * so we don't activate them yet.  Reset from SET_INTERFACE.
782          */
783         if (!cdc_active(dev)) {
784                 result = usb_ep_enable (dev->in_ep, dev->in);
785                 if (result != 0) {
786                         printf ("enable %s --> %d\n",
787                                 dev->in_ep->name, result);
788                         goto done;
789                 }
790
791                 result = usb_ep_enable (dev->out_ep, dev->out);
792                 if (result != 0) {
793                         printf ("enable %s --> %d\n",
794                                 dev->out_ep->name, result);
795                         goto done;
796                 }
797         }
798
799 done:
800         if (result == 0)
801                 result = alloc_requests (dev, qlen (gadget), gfp_flags);
802
803         /* on error, disable any endpoints  */
804         if (result < 0) {
805                 if (!subset_active(dev))
806                         (void) usb_ep_disable (dev->status_ep);
807                 dev->status = NULL;
808                 (void) usb_ep_disable (dev->in_ep);
809                 (void) usb_ep_disable (dev->out_ep);
810                 dev->in = NULL;
811                 dev->out = NULL;
812         }
813
814         /* caller is responsible for cleanup on error */
815         return result;
816 }
817
818
819 static void eth_reset_config (struct eth_dev *dev)
820 {
821         if (dev->config == 0)
822                 return;
823
824         /* disable endpoints, forcing (synchronous) completion of
825          * pending i/o.  then free the requests.
826          */
827
828         if (dev->in) {
829                 usb_ep_disable (dev->in_ep);
830                 if (dev->tx_req) {
831                         usb_ep_free_request (dev->in_ep, dev->tx_req);
832                         dev->tx_req=NULL;
833                 }
834         }
835         if (dev->out) {
836                 usb_ep_disable (dev->out_ep);
837                 if (dev->rx_req) {
838                         usb_ep_free_request (dev->in_ep, dev->rx_req);
839                         dev->rx_req=NULL;
840                 }
841         }
842         if (dev->status) {
843                 usb_ep_disable (dev->status_ep);
844         }
845         dev->cdc_filter = 0;
846         dev->config = 0;
847 }
848
849 /* change our operational config.  must agree with the code
850  * that returns config descriptors, and altsetting code.
851  */
852 static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
853 {
854         int                     result = 0;
855         struct usb_gadget       *gadget = dev->gadget;
856
857         if (gadget_is_sa1100 (gadget)
858                         && dev->config
859                         && dev->tx_qlen != 0) {
860                 /* tx fifo is full, but we can't clear it...*/
861                 INFO (dev, "can't change configurations\n");
862                 return -ESPIPE;
863         }
864         eth_reset_config (dev);
865
866         switch (number) {
867         case DEV_CONFIG_VALUE:
868                 result = set_ether_config (dev, gfp_flags);
869                 break;
870         default:
871                 result = -EINVAL;
872                 /* FALL THROUGH */
873         case 0:
874                 break;
875         }
876
877         if (result) {
878                 if (number)
879                         eth_reset_config (dev);
880                 usb_gadget_vbus_draw(dev->gadget,
881                                 gadget_is_otg(dev->gadget) ? 8 : 100);
882         } else {
883                 char *speed;
884                 unsigned power;
885
886                 power = 2 * eth_config.bMaxPower;
887                 usb_gadget_vbus_draw(dev->gadget, power);
888
889                 switch (gadget->speed) {
890                 case USB_SPEED_FULL:    speed = "full"; break;
891 #ifdef CONFIG_USB_GADGET_DUALSPEED
892                 case USB_SPEED_HIGH:    speed = "high"; break;
893 #endif
894                 default:                speed = "?"; break;
895                 }
896
897                 dev->config = number;
898                 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
899                                 speed, number, power, driver_desc,
900                                 (cdc_active(dev)? "CDC Ethernet"
901                                                 : "CDC Ethernet Subset"));
902         }
903         return result;
904 }
905
906 /*-------------------------------------------------------------------------*/
907
908 #ifdef  DEV_CONFIG_CDC
909
910 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
911  * only to notify the host about link status changes (which we support) or
912  * report completion of some encapsulated command.  Since
913  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
914  * command mechanism; and only one status request is ever queued.
915  */
916 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
917 {
918         struct usb_cdc_notification     *event = req->buf;
919         int                             value = req->status;
920         struct eth_dev                  *dev = ep->driver_data;
921
922         /* issue the second notification if host reads the first */
923         if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
924                         && value == 0) {
925                 __le32  *data = req->buf + sizeof *event;
926
927                 event->bmRequestType = 0xA1;
928                 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
929                 event->wValue = __constant_cpu_to_le16 (0);
930                 event->wIndex = __constant_cpu_to_le16 (1);
931                 event->wLength = __constant_cpu_to_le16 (8);
932
933                 /* SPEED_CHANGE data is up/down speeds in bits/sec */
934                 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
935
936                 req->length = STATUS_BYTECOUNT;
937                 value = usb_ep_queue (ep, req, GFP_ATOMIC);
938                 dprintf ("send SPEED_CHANGE --> %d\n", value);
939                 if (value == 0)
940                         return;
941         } else if (value != -ECONNRESET) {
942                 dprintf("event %02x --> %d\n",
943                         event->bNotificationType, value);
944                 if (event->bNotificationType==
945                                 USB_CDC_NOTIFY_SPEED_CHANGE)
946                 {
947                         l_ethdev.network_started=1;
948                         printf("USB network up!\n");
949                 }
950         }
951         req->context = NULL;
952 }
953
954 static void issue_start_status (struct eth_dev *dev)
955 {
956         struct usb_request              *req = dev->stat_req;
957         struct usb_cdc_notification     *event;
958         int                             value;
959
960         /* flush old status
961          *
962          * FIXME ugly idiom, maybe we'd be better with just
963          * a "cancel the whole queue" primitive since any
964          * unlink-one primitive has way too many error modes.
965          * here, we "know" toggle is already clear...
966          *
967          * FIXME iff req->context != null just dequeue it
968          */
969         usb_ep_disable (dev->status_ep);
970         usb_ep_enable (dev->status_ep, dev->status);
971
972         /* 3.8.1 says to issue first NETWORK_CONNECTION, then
973          * a SPEED_CHANGE.  could be useful in some configs.
974          */
975         event = req->buf;
976         event->bmRequestType = 0xA1;
977         event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
978         event->wValue = __constant_cpu_to_le16 (1);     /* connected */
979         event->wIndex = __constant_cpu_to_le16 (1);
980         event->wLength = 0;
981
982         req->length = sizeof *event;
983         req->complete = eth_status_complete;
984         req->context = dev;
985
986         value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
987         if (value < 0)
988                 printf ("status buf queue --> %d\n", value);
989 }
990
991 #endif
992
993 /*-------------------------------------------------------------------------*/
994
995 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
996 {
997         if (req->status || req->actual != req->length)
998                 dprintf  (/*(struct eth_dev *) ep->driver_data*/
999                                 "setup complete --> %d, %d/%d\n",
1000                                 req->status, req->actual, req->length);
1001 }
1002
1003 /*
1004  * The setup() callback implements all the ep0 functionality that's not
1005  * handled lower down.  CDC has a number of less-common features:
1006  *
1007  *  - two interfaces:  control, and ethernet data
1008  *  - Ethernet data interface has two altsettings:  default, and active
1009  *  - class-specific descriptors for the control interface
1010  *  - class-specific control requests
1011  */
1012 static int
1013 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1014 {
1015         struct eth_dev          *dev = get_gadget_data (gadget);
1016         struct usb_request      *req = dev->req;
1017         int                     value = -EOPNOTSUPP;
1018         u16                     wIndex = le16_to_cpu(ctrl->wIndex);
1019         u16                     wValue = le16_to_cpu(ctrl->wValue);
1020         u16                     wLength = le16_to_cpu(ctrl->wLength);
1021
1022         /* descriptors just go into the pre-allocated ep0 buffer,
1023          * while config change events may enable network traffic.
1024          */
1025
1026         dprintf("eth_setup:...\n");
1027
1028         req->complete = eth_setup_complete;
1029         switch (ctrl->bRequest) {
1030
1031         case USB_REQ_GET_DESCRIPTOR:
1032                 if (ctrl->bRequestType != USB_DIR_IN)
1033                         break;
1034                 switch (wValue >> 8) {
1035
1036                 case USB_DT_DEVICE:
1037                         value = min (wLength, (u16) sizeof device_desc);
1038                         memcpy (req->buf, &device_desc, value);
1039                         break;
1040                 case USB_DT_DEVICE_QUALIFIER:
1041                         if (!gadget_is_dualspeed(gadget))
1042                                 break;
1043                         value = min (wLength, (u16) sizeof dev_qualifier);
1044                         memcpy (req->buf, &dev_qualifier, value);
1045                         break;
1046
1047                 case USB_DT_OTHER_SPEED_CONFIG:
1048                         if (!gadget_is_dualspeed(gadget))
1049                                 break;
1050                         /* FALLTHROUGH */
1051                 case USB_DT_CONFIG:
1052                         value = config_buf(gadget, req->buf,
1053                                         wValue >> 8,
1054                                         wValue & 0xff,
1055                                         gadget_is_otg(gadget));
1056                         if (value >= 0)
1057                                 value = min (wLength, (u16) value);
1058                         break;
1059
1060                 case USB_DT_STRING:
1061                         value = usb_gadget_get_string (&stringtab,
1062                                         wValue & 0xff, req->buf);
1063
1064                         if (value >= 0)
1065                                 value = min (wLength, (u16) value);
1066
1067                         break;
1068                 }
1069                 break;
1070
1071         case USB_REQ_SET_CONFIGURATION:
1072                 if (ctrl->bRequestType != 0)
1073                         break;
1074                 if (gadget->a_hnp_support)
1075                         DEBUG (dev, "HNP available\n");
1076                 else if (gadget->a_alt_hnp_support)
1077                         DEBUG (dev, "HNP needs a different root port\n");
1078                 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1079                 break;
1080         case USB_REQ_GET_CONFIGURATION:
1081                 if (ctrl->bRequestType != USB_DIR_IN)
1082                         break;
1083                 *(u8 *)req->buf = dev->config;
1084                 value = min (wLength, (u16) 1);
1085                 break;
1086
1087         case USB_REQ_SET_INTERFACE:
1088                 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1089                                 || !dev->config
1090                                 || wIndex > 1)
1091                         break;
1092                 if (!cdc_active(dev) && wIndex != 0)
1093                         break;
1094
1095                 /* PXA hardware partially handles SET_INTERFACE;
1096                  * we need to kluge around that interference.
1097                  */
1098                 if (gadget_is_pxa (gadget)) {
1099                         value = eth_set_config (dev, DEV_CONFIG_VALUE,
1100                                                 GFP_ATOMIC);
1101                         goto done_set_intf;
1102                 }
1103
1104 #ifdef DEV_CONFIG_CDC
1105                 switch (wIndex) {
1106                 case 0:         /* control/master intf */
1107                         if (wValue != 0)
1108                                 break;
1109                         if (dev->status) {
1110                                 usb_ep_disable (dev->status_ep);
1111                                 usb_ep_enable (dev->status_ep, dev->status);
1112                         }
1113                         value = 0;
1114                         break;
1115                 case 1:         /* data intf */
1116                         if (wValue > 1)
1117                                 break;
1118                         usb_ep_disable (dev->in_ep);
1119                         usb_ep_disable (dev->out_ep);
1120
1121                         /* CDC requires the data transfers not be done from
1122                          * the default interface setting ... also, setting
1123                          * the non-default interface resets filters etc.
1124                          */
1125                         if (wValue == 1) {
1126                                 if (!cdc_active (dev))
1127                                         break;
1128                                 usb_ep_enable (dev->in_ep, dev->in);
1129                                 usb_ep_enable (dev->out_ep, dev->out);
1130                                 dev->cdc_filter = DEFAULT_FILTER;
1131                                 if (dev->status)
1132                                         issue_start_status (dev);
1133                         }
1134
1135                         value = 0;
1136                         break;
1137                 }
1138 #else
1139                 /* FIXME this is wrong, as is the assumption that
1140                  * all non-PXA hardware talks real CDC ...
1141                  */
1142                 dev_warn (&gadget->dev, "set_interface ignored!\n");
1143 #endif /* DEV_CONFIG_CDC */
1144
1145 done_set_intf:
1146                 break;
1147         case USB_REQ_GET_INTERFACE:
1148                 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1149                                 || !dev->config
1150                                 || wIndex > 1)
1151                         break;
1152                 if (!(cdc_active(dev)) && wIndex != 0)
1153                         break;
1154
1155                 /* for CDC, iff carrier is on, data interface is active. */
1156                 if (wIndex != 1)
1157                         *(u8 *)req->buf = 0;
1158                 else {
1159                         /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1160                         /* carrier always ok ...*/
1161                         *(u8 *)req->buf = 1 ;
1162                 }
1163                 value = min (wLength, (u16) 1);
1164                 break;
1165
1166 #ifdef DEV_CONFIG_CDC
1167         case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1168                 /* see 6.2.30: no data, wIndex = interface,
1169                  * wValue = packet filter bitmap
1170                  */
1171                 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1172                                 || !cdc_active(dev)
1173                                 || wLength != 0
1174                                 || wIndex > 1)
1175                         break;
1176                 printf ("packet filter %02x\n", wValue);
1177                 dev->cdc_filter = wValue;
1178                 value = 0;
1179                 break;
1180
1181         /* and potentially:
1182          * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1183          * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1184          * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1185          * case USB_CDC_GET_ETHERNET_STATISTIC:
1186          */
1187
1188 #endif /* DEV_CONFIG_CDC */
1189
1190         default:
1191                 printf (
1192                         "unknown control req%02x.%02x v%04x i%04x l%d\n",
1193                         ctrl->bRequestType, ctrl->bRequest,
1194                         wValue, wIndex, wLength);
1195         }
1196
1197         /* respond with data transfer before status phase? */
1198         if (value >= 0) {
1199                 dprintf("respond with data transfer before status phase\n");
1200                 req->length = value;
1201                 req->zero = value < wLength
1202                                 && (value % gadget->ep0->maxpacket) == 0;
1203                 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1204                 if (value < 0) {
1205                         DEBUG (dev, "ep_queue --> %d\n", value);
1206                         req->status = 0;
1207                         eth_setup_complete (gadget->ep0, req);
1208                 }
1209         }
1210
1211         /* host either stalls (value < 0) or reports success */
1212         return value;
1213 }
1214
1215
1216 /*-------------------------------------------------------------------------*/
1217
1218 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1219
1220 static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
1221                                 gfp_t gfp_flags)
1222 {
1223         int                     retval = -ENOMEM;
1224         size_t                  size;
1225
1226         /* Padding up to RX_EXTRA handles minor disagreements with host.
1227          * Normally we use the USB "terminate on short read" convention;
1228          * so allow up to (N*maxpacket), since that memory is normally
1229          * already allocated.  Some hardware doesn't deal well with short
1230          * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1231          * byte off the end (to force hardware errors on overflow).
1232          */
1233
1234         dprintf("%s\n", __func__);
1235
1236         size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1237         size += dev->out_ep->maxpacket - 1;
1238         size -= size % dev->out_ep->maxpacket;
1239
1240
1241         /* Some platforms perform better when IP packets are aligned,
1242          * but on at least one, checksumming fails otherwise.
1243          */
1244
1245         req->buf = (u8 *) NetRxPackets[0];
1246         req->length = size;
1247         req->complete = rx_complete;
1248
1249         retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1250
1251         if (retval) {
1252                 dprintf ("rx submit --> %d\n", retval);
1253         }
1254         return retval;
1255 }
1256
1257
1258 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1259 {
1260         struct eth_dev  *dev = ep->driver_data;
1261
1262         dprintf("%s\n", __func__);
1263         dprintf("rx status %d\n", req->status);
1264
1265         packet_received=1;
1266
1267         if (req)
1268                 dev->rx_req=req;
1269 }
1270
1271
1272 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1273 {
1274
1275         dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0);
1276
1277         if (!dev->tx_req)
1278                 goto fail;
1279
1280         dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0);
1281
1282         if (!dev->rx_req)
1283                 goto fail;
1284
1285         return 0;
1286
1287 fail:
1288         DEBUG (dev, "can't alloc requests\n");
1289         return -1;
1290 }
1291
1292
1293 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1294 {
1295         dprintf("%s, status: %s\n", __func__,(req->status) ? "failed":"ok");
1296         packet_sent=1;
1297 }
1298
1299 static inline int eth_is_promisc (struct eth_dev *dev)
1300 {
1301         /* no filters for the CDC subset; always promisc */
1302         if (subset_active (dev))
1303                 return 1;
1304         return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1305 }
1306
1307 #if 0
1308 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1309 {
1310         struct eth_dev          *dev = netdev_priv(net);
1311         int                     length = skb->len;
1312         int                     retval;
1313         struct usb_request      *req = NULL;
1314         unsigned long           flags;
1315
1316         /* apply outgoing CDC or RNDIS filters */
1317         if (!eth_is_promisc (dev)) {
1318                 u8              *dest = skb->data;
1319
1320                 if (is_multicast_ether_addr(dest)) {
1321                         u16     type;
1322
1323                         /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1324                          * SET_ETHERNET_MULTICAST_FILTERS requests
1325                          */
1326                         if (is_broadcast_ether_addr(dest))
1327                                 type = USB_CDC_PACKET_TYPE_BROADCAST;
1328                         else
1329                                 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1330                         if (!(dev->cdc_filter & type)) {
1331                                 dev_kfree_skb_any (skb);
1332                                 return 0;
1333                         }
1334                 }
1335                 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1336         }
1337
1338         spin_lock_irqsave(&dev->req_lock, flags);
1339         /*
1340          * this freelist can be empty if an interrupt triggered disconnect()
1341          * and reconfigured the gadget (shutting down this queue) after the
1342          * network stack decided to xmit but before we got the spinlock.
1343          */
1344         if (list_empty(&dev->tx_reqs)) {
1345                 spin_unlock_irqrestore(&dev->req_lock, flags);
1346                 return 1;
1347         }
1348
1349         req = container_of (dev->tx_reqs.next, struct usb_request, list);
1350         list_del (&req->list);
1351
1352         /* temporarily stop TX queue when the freelist empties */
1353         if (list_empty (&dev->tx_reqs))
1354                 netif_stop_queue (net);
1355         spin_unlock_irqrestore(&dev->req_lock, flags);
1356
1357         /* no buffer copies needed, unless the network stack did it
1358          * or the hardware can't use skb buffers.
1359          * or there's not enough space for any RNDIS headers we need
1360          */
1361         if (rndis_active(dev)) {
1362                 struct sk_buff  *skb_rndis;
1363
1364                 skb_rndis = skb_realloc_headroom (skb,
1365                                 sizeof (struct rndis_packet_msg_type));
1366                 if (!skb_rndis)
1367                         goto drop;
1368
1369                 dev_kfree_skb_any (skb);
1370                 skb = skb_rndis;
1371                 rndis_add_hdr (skb);
1372                 length = skb->len;
1373         }
1374         req->buf = skb->data;
1375         req->context = skb;
1376         req->complete = tx_complete;
1377
1378         /* use zlp framing on tx for strict CDC-Ether conformance,
1379          * though any robust network rx path ignores extra padding.
1380          * and some hardware doesn't like to write zlps.
1381          */
1382         req->zero = 1;
1383         if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1384                 length++;
1385
1386         req->length = length;
1387
1388         /* throttle highspeed IRQ rate back slightly */
1389         if (gadget_is_dualspeed(dev->gadget))
1390                 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1391                         ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1392                         : 0;
1393
1394         retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1395         switch (retval) {
1396         default:
1397                 DEBUG (dev, "tx queue err %d\n", retval);
1398                 break;
1399         case 0:
1400                 net->trans_start = jiffies;
1401                 atomic_inc (&dev->tx_qlen);
1402         }
1403
1404         if (retval) {
1405 drop:
1406                 dev->stats.tx_dropped++;
1407                 dev_kfree_skb_any (skb);
1408                 spin_lock_irqsave(&dev->req_lock, flags);
1409                 if (list_empty (&dev->tx_reqs))
1410                         netif_start_queue (net);
1411                 list_add (&req->list, &dev->tx_reqs);
1412                 spin_unlock_irqrestore(&dev->req_lock, flags);
1413         }
1414         return 0;
1415 }
1416
1417 /*-------------------------------------------------------------------------*/
1418 #endif
1419
1420 static void eth_unbind (struct usb_gadget *gadget)
1421 {
1422         struct eth_dev *dev = get_gadget_data (gadget);
1423
1424         printf("eth_unbind:...\n");
1425
1426         if (dev->stat_req) {
1427                 usb_ep_free_request (dev->status_ep, dev->stat_req);
1428                 dev->stat_req = NULL;
1429         }
1430
1431         if (dev->tx_req) {
1432                 usb_ep_free_request (dev->in_ep, dev->tx_req);
1433                 dev->tx_req=NULL;
1434         }
1435
1436         if (dev->rx_req) {
1437                 usb_ep_free_request (dev->in_ep, dev->rx_req);
1438                 dev->rx_req=NULL;
1439         }
1440
1441 /*      unregister_netdev (dev->net);*/
1442 /*      free_netdev(dev->net);*/
1443
1444         set_gadget_data (gadget, NULL);
1445 }
1446
1447 static void eth_disconnect (struct usb_gadget *gadget)
1448 {
1449         eth_reset_config (get_gadget_data (gadget));
1450 }
1451
1452 static void eth_suspend (struct usb_gadget *gadget)
1453 {
1454         /* Not used */
1455 }
1456
1457 static void eth_resume (struct usb_gadget *gadget)
1458 {
1459         /* Not used */
1460 }
1461
1462 /*-------------------------------------------------------------------------*/
1463
1464 static int is_eth_addr_valid(char *str)
1465 {
1466         if (strlen(str) == 17) {
1467                 int i;
1468                 char *p, *q;
1469                 uchar ea[6];
1470
1471                 /* see if it looks like an ethernet address */
1472
1473                 p = str;
1474
1475                 for (i = 0; i < 6; i++) {
1476                         char term = (i == 5 ? '\0' : ':');
1477
1478                         ea[i] = simple_strtol(p, &q, 16);
1479
1480                         if ((q - p) != 2 || *q++ != term)
1481                                 break;
1482
1483                         p = q;
1484                 }
1485
1486                 if (i == 6) /* it looks ok */
1487                         return 1;
1488         }
1489         return 0;
1490 }
1491
1492 static u8 nibble (unsigned char c)
1493 {
1494         if (likely (isdigit (c)))
1495                 return c - '0';
1496         c = toupper (c);
1497         if (likely (isxdigit (c)))
1498                 return 10 + c - 'A';
1499         return 0;
1500 }
1501
1502 static int get_ether_addr(const char *str, u8 *dev_addr)
1503 {
1504         if (str) {
1505                 unsigned        i;
1506
1507                 for (i = 0; i < 6; i++) {
1508                         unsigned char num;
1509
1510                         if((*str == '.') || (*str == ':'))
1511                                 str++;
1512                         num = nibble(*str++) << 4;
1513                         num |= (nibble(*str++));
1514                         dev_addr [i] = num;
1515                 }
1516                 if (is_valid_ether_addr (dev_addr))
1517                         return 0;
1518         }
1519         return 1;
1520 }
1521
1522 static int eth_bind(struct usb_gadget *gadget)
1523 {
1524         struct eth_dev          *dev = &l_ethdev;
1525         u8                      cdc = 1, zlp = 1;
1526         struct usb_ep           *in_ep, *out_ep, *status_ep = NULL;
1527         int                     gcnum;
1528         u8                      tmp[7];
1529
1530         /* these flags are only ever cleared; compiler take note */
1531 #ifndef DEV_CONFIG_CDC
1532         cdc = 0;
1533 #endif
1534         /* Because most host side USB stacks handle CDC Ethernet, that
1535          * standard protocol is _strongly_ preferred for interop purposes.
1536          * (By everyone except Microsoft.)
1537          */
1538         if (gadget_is_pxa (gadget)) {
1539                 /* pxa doesn't support altsettings */
1540                 cdc = 0;
1541         } else if (gadget_is_musbhdrc(gadget)) {
1542                 /* reduce tx dma overhead by avoiding special cases */
1543                 zlp = 0;
1544         } else if (gadget_is_sh(gadget)) {
1545                 /* sh doesn't support multiple interfaces or configs */
1546                 cdc = 0;
1547         } else if (gadget_is_sa1100 (gadget)) {
1548                 /* hardware can't write zlps */
1549                 zlp = 0;
1550                 /* sa1100 CAN do CDC, without status endpoint ... we use
1551                  * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
1552                  */
1553                 cdc = 0;
1554         }
1555
1556         gcnum = usb_gadget_controller_number (gadget);
1557         if (gcnum >= 0)
1558                 device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum);
1559         else {
1560                 /* can't assume CDC works.  don't want to default to
1561                  * anything less functional on CDC-capable hardware,
1562                  * so we fail in this case.
1563                  */
1564                 dev_err (&gadget->dev,
1565                         "controller '%s' not recognized\n",
1566                         gadget->name);
1567                 return -ENODEV;
1568         }
1569
1570         /* CDC subset ... recognized by Linux since 2.4.10, but Windows
1571          * drivers aren't widely available.  (That may be improved by
1572          * supporting one submode of the "SAFE" variant of MDLM.)
1573          */
1574         if (!cdc) {
1575                 device_desc.idVendor =
1576                         __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
1577                 device_desc.idProduct =
1578                         __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
1579         }
1580
1581         /* support optional vendor/distro customization */
1582 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
1583         device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
1584         device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
1585 #endif
1586         if (bcdDevice)
1587                 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1588         if (iManufacturer)
1589                 strcpy (manufacturer, iManufacturer);
1590         if (iProduct)
1591                 strcpy (product_desc, iProduct);
1592         if (iSerialNumber) {
1593                 device_desc.iSerialNumber = STRING_SERIALNUMBER,
1594                 strcpy(serial_number, iSerialNumber);
1595         }
1596
1597         /* all we really need is bulk IN/OUT */
1598         usb_ep_autoconfig_reset (gadget);
1599         in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1600         if (!in_ep) {
1601 autoconf_fail:
1602                 dev_err (&gadget->dev,
1603                         "can't autoconfigure on %s\n",
1604                         gadget->name);
1605                 return -ENODEV;
1606         }
1607         in_ep->driver_data = in_ep;     /* claim */
1608
1609         out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1610         if (!out_ep)
1611                 goto autoconf_fail;
1612         out_ep->driver_data = out_ep;   /* claim */
1613
1614 #if defined(DEV_CONFIG_CDC)
1615         /* CDC Ethernet control interface doesn't require a status endpoint.
1616          * Since some hosts expect one, try to allocate one anyway.
1617          */
1618         if (cdc) {
1619                 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
1620                 if (status_ep) {
1621                         status_ep->driver_data = status_ep;     /* claim */
1622                 } else if (cdc) {
1623                         control_intf.bNumEndpoints = 0;
1624                         /* FIXME remove endpoint from descriptor list */
1625                 }
1626         }
1627 #endif
1628
1629         /* one config:  cdc, else minimal subset */
1630         if (!cdc) {
1631                 eth_config.bNumInterfaces = 1;
1632                 eth_config.iConfiguration = STRING_SUBSET;
1633
1634                 /* use functions to set these up, in case we're built to work
1635                  * with multiple controllers and must override CDC Ethernet.
1636                  */
1637                 fs_subset_descriptors();
1638                 hs_subset_descriptors();
1639         }
1640
1641         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1642         usb_gadget_set_selfpowered (gadget);
1643
1644         if (gadget_is_dualspeed(gadget)) {
1645                 if (!cdc)
1646                         dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
1647
1648                 /* assumes ep0 uses the same value for both speeds ... */
1649                 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1650
1651                 /* and that all endpoints are dual-speed */
1652                 hs_source_desc.bEndpointAddress =
1653                                 fs_source_desc.bEndpointAddress;
1654                 hs_sink_desc.bEndpointAddress =
1655                                 fs_sink_desc.bEndpointAddress;
1656 #if defined(DEV_CONFIG_CDC)
1657                 if (status_ep)
1658                         hs_status_desc.bEndpointAddress =
1659                                         fs_status_desc.bEndpointAddress;
1660 #endif
1661         }
1662
1663         if (gadget_is_otg(gadget)) {
1664                 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1665                 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1666                 eth_config.bMaxPower = 4;
1667         }
1668
1669         dev->net = &l_netdev;
1670         strcpy (dev->net->name, USB_NET_NAME);
1671
1672         dev->cdc = cdc;
1673         dev->zlp = zlp;
1674
1675         dev->in_ep = in_ep;
1676         dev->out_ep = out_ep;
1677         dev->status_ep = status_ep;
1678
1679         /* Module params for these addresses should come from ID proms.
1680          * The host side address is used with CDC, and commonly
1681          * ends up in a persistent config database.  It's not clear if
1682          * host side code for the SAFE thing cares -- its original BLAN
1683          * thing didn't, Sharp never assigned those addresses on Zaurii.
1684          */
1685         get_ether_addr(dev_addr, dev->net->enetaddr);
1686
1687         memset(tmp, 0, sizeof(tmp));
1688         memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
1689
1690         get_ether_addr(host_addr, dev->host_mac);
1691
1692         sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X",
1693                 dev->host_mac [0], dev->host_mac [1],
1694                         dev->host_mac [2], dev->host_mac [3],
1695                         dev->host_mac [4], dev->host_mac [5]);
1696
1697         INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
1698                 out_ep->name, in_ep->name,
1699                 status_ep ? " STATUS " : "",
1700                 status_ep ? status_ep->name : ""
1701                 );
1702         INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1703                 dev->net->enetaddr [0], dev->net->enetaddr [1],
1704                 dev->net->enetaddr [2], dev->net->enetaddr [3],
1705                 dev->net->enetaddr [4], dev->net->enetaddr [5]);
1706
1707         if (cdc) {
1708                 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1709                         dev->host_mac [0], dev->host_mac [1],
1710                         dev->host_mac [2], dev->host_mac [3],
1711                         dev->host_mac [4], dev->host_mac [5]);
1712         }
1713
1714         /* use PKTSIZE (or aligned... from u-boot) and set
1715          * wMaxSegmentSize accordingly*/
1716         dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
1717
1718         /* preallocate control message data and buffer */
1719         dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1720         if (!dev->req)
1721                 goto fail;
1722         dev->req->buf = control_req;
1723         dev->req->complete = eth_setup_complete;
1724
1725         /* ... and maybe likewise for status transfer */
1726 #if defined(DEV_CONFIG_CDC)
1727         if (dev->status_ep) {
1728                 dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1729                 dev->stat_req->buf = status_req;
1730                 if (!dev->stat_req) {
1731                         dev->stat_req->buf=NULL;
1732                         usb_ep_free_request (gadget->ep0, dev->req);
1733
1734                         goto fail;
1735                 }
1736                 dev->stat_req->context = NULL;
1737         }
1738 #endif
1739
1740         /* finish hookup to lower layer ... */
1741         dev->gadget = gadget;
1742         set_gadget_data (gadget, dev);
1743         gadget->ep0->driver_data = dev;
1744
1745         /* two kinds of host-initiated state changes:
1746          *  - iff DATA transfer is active, carrier is "on"
1747          *  - tx queueing enabled if open *and* carrier is "on"
1748          */
1749         return 0;
1750
1751 fail:
1752         dev_dbg(&gadget->dev, "register_netdev failed\n");
1753         eth_unbind (gadget);
1754         return -ENOMEM;
1755 }
1756
1757 static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
1758 {
1759         struct eth_dev *dev=&l_ethdev;
1760         struct usb_gadget *gadget;
1761         unsigned long ts;
1762         unsigned long timeout = USB_CONNECT_TIMEOUT;
1763
1764         if (!netdev) {
1765                 printf("ERROR: received NULL ptr\n");
1766                 goto fail;
1767         }
1768
1769         dev->network_started = 0;
1770         dev->tx_req = NULL;
1771         dev->rx_req = NULL;
1772
1773         packet_received = 0;
1774         packet_sent = 0;
1775
1776         gadget = dev->gadget;
1777         usb_gadget_connect(gadget);
1778
1779         if (getenv("cdc_connect_timeout"))
1780                 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
1781                                                 NULL, 10) * CONFIG_SYS_HZ;
1782         ts = get_timer(0);
1783         while (!l_ethdev.network_started)
1784         {
1785                 /* Handle control-c and timeouts */
1786                 if (ctrlc() || (get_timer(ts) > timeout)) {
1787                         printf("The remote end did not respond in time.\n");
1788                         goto fail;
1789                 }
1790                 usb_gadget_handle_interrupts();
1791         }
1792
1793         rx_submit (dev, dev->rx_req, 0);
1794         return 0;
1795 fail:
1796         return -1;
1797 }
1798
1799 static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length)
1800 {
1801         int                     retval;
1802         struct usb_request      *req = NULL;
1803
1804         struct eth_dev *dev = &l_ethdev;
1805         dprintf("%s:...\n",__func__);
1806
1807         req = dev->tx_req;
1808
1809         req->buf = (void *)packet;
1810         req->context = NULL;
1811         req->complete = tx_complete;
1812
1813         /* use zlp framing on tx for strict CDC-Ether conformance,
1814          * though any robust network rx path ignores extra padding.
1815          * and some hardware doesn't like to write zlps.
1816          */
1817         req->zero = 1;
1818         if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1819                 length++;
1820
1821         req->length = length;
1822 #if 0
1823         /* throttle highspeed IRQ rate back slightly */
1824         if (gadget_is_dualspeed(dev->gadget))
1825                 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1826                         ? ((dev->tx_qlen % qmult) != 0) : 0;
1827 #endif
1828         dev->tx_qlen=1;
1829
1830         retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1831
1832         if (!retval)
1833                 dprintf("%s: packet queued\n",__func__);
1834         while(!packet_sent)
1835         {
1836                 packet_sent=0;
1837         }
1838
1839         return 0;
1840 }
1841
1842 static int usb_eth_recv(struct eth_device* netdev)
1843 {
1844         struct eth_dev *dev = &l_ethdev;
1845
1846         usb_gadget_handle_interrupts();
1847
1848         if (packet_received)
1849         {
1850                 dprintf("%s: packet received \n",__func__);
1851                 if (dev->rx_req)
1852                 {
1853                         NetReceive(NetRxPackets[0],dev->rx_req->length);
1854                         packet_received=0;
1855
1856                         if (dev->rx_req)
1857                                 rx_submit (dev, dev->rx_req, 0);
1858                 }
1859                 else printf("dev->rx_req invalid\n");
1860         }
1861         return 0;
1862 }
1863
1864 void usb_eth_halt(struct eth_device* netdev)
1865 {
1866         struct eth_dev *dev =&l_ethdev;
1867
1868         if (!netdev)
1869         {
1870                 printf("ERROR: received NULL ptr\n");
1871                 return;
1872         }
1873
1874         usb_gadget_disconnect(dev->gadget);
1875 }
1876
1877 static struct usb_gadget_driver eth_driver = {
1878         .speed          = DEVSPEED,
1879
1880         .bind           = eth_bind,
1881         .unbind         = eth_unbind,
1882
1883         .setup          = eth_setup,
1884         .disconnect     = eth_disconnect,
1885
1886         .suspend        = eth_suspend,
1887         .resume         = eth_resume,
1888 };
1889
1890 int usb_eth_initialize(bd_t *bi)
1891 {
1892         int status = 0;
1893         struct eth_device *netdev=&l_netdev;
1894
1895         sprintf(netdev->name,"usb_ether");
1896
1897         netdev->init = usb_eth_init;
1898         netdev->send = usb_eth_send;
1899         netdev->recv = usb_eth_recv;
1900         netdev->halt = usb_eth_halt;
1901
1902 #ifdef CONFIG_MCAST_TFTP
1903   #error not supported
1904 #endif
1905         /* Configure default mac-addresses for the USB ethernet device */
1906 #ifdef CONFIG_USBNET_DEV_ADDR
1907         strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
1908 #endif
1909 #ifdef CONFIG_USBNET_HOST_ADDR
1910         strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
1911 #endif
1912         /* Check if the user overruled the MAC addresses */
1913         if (getenv("usbnet_devaddr"))
1914                 strncpy(dev_addr, getenv("usbnet_devaddr"),
1915                         sizeof(dev_addr));
1916
1917         if (getenv("usbnet_hostaddr"))
1918                 strncpy(host_addr, getenv("usbnet_hostaddr"),
1919                         sizeof(host_addr));
1920
1921         /* Make sure both strings are terminated */
1922         dev_addr[sizeof(dev_addr)-1] = '\0';
1923         host_addr[sizeof(host_addr)-1] = '\0';
1924
1925         if (!is_eth_addr_valid(dev_addr)) {
1926                 printf("ERROR: Need valid 'usbnet_devaddr' to be set\n");
1927                 status = -1;
1928         }
1929         if (!is_eth_addr_valid(host_addr)) {
1930                 printf("ERROR: Need valid 'usbnet_hostaddr' to be set\n");
1931                 status = -1;
1932         }
1933         if (status)
1934                 goto fail;
1935
1936         status = usb_gadget_register_driver(&eth_driver);
1937         if (status < 0)
1938                 goto fail;
1939
1940         eth_register(netdev);
1941         return 0;
1942
1943 fail:
1944         printf("%s failed\n", __func__ );
1945         return status;
1946 }
1947