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