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