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