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