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