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