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