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