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