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