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