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