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