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