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