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