1 // SPDX-License-Identifier: GPL-2.0+
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
10 /* #define VERBOSE_DEBUG */
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
15 #if defined USB_ETH_RNDIS
18 #ifdef CONFIG_USB_ETH_RNDIS
19 # define USB_ETH_RNDIS y
26 * Ethernet gadget driver -- with CDC and non-CDC options
27 * Builds on hardware support for a full duplex link.
29 * CDC Ethernet is the standard USB solution for sending Ethernet frames
30 * using USB. Real hardware tends to use the same framing protocol but look
31 * different for control features. This driver strongly prefers to use
32 * this USB-IF standard as its open-systems interoperability solution;
33 * most host side USB stacks (except from Microsoft) support it.
35 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
36 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
37 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
39 * There's some hardware that can't talk CDC ECM. We make that hardware
40 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
41 * link-level setup only requires activating the configuration. Only the
42 * endpoint descriptors, and product/vendor IDs, are relevant; no control
43 * operations are available. Linux supports it, but other host operating
44 * systems may not. (This is a subset of CDC Ethernet.)
46 * It turns out that if you add a few descriptors to that "CDC Subset",
47 * (Windows) host side drivers from MCCI can treat it as one submode of
48 * a proprietary scheme called "SAFE" ... without needing to know about
49 * specific product/vendor IDs. So we do that, making it easier to use
50 * those MS-Windows drivers. Those added descriptors make it resemble a
51 * CDC MDLM device, but they don't change device behavior at all. (See
52 * MCCI Engineering report 950198 "SAFE Networking Functions".)
54 * A third option is also in use. Rather than CDC Ethernet, or something
55 * simpler, Microsoft pushes their own approach: RNDIS. The published
56 * RNDIS specs are ambiguous and appear to be incomplete, and are also
57 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
60 #define DRIVER_DESC "Ethernet Gadget"
61 #define DRIVER_VERSION "Memorial Day 2008"
64 #define PREFIX "RNDIS/"
70 * This driver aims for interoperability by using CDC ECM unless
74 * returns false, in which case it supports the CDC Subset. By default,
75 * that returns true; most hardware has no problems with CDC ECM, that's
76 * a good default. Previous versions of this driver had no default; this
77 * version changes that, removing overhead for new controller support.
79 * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
82 static inline bool has_rndis(void)
91 #include <linux/module.h>
99 #define rndis_borrow_net(...) do {} while (0)
103 /*-------------------------------------------------------------------------*/
104 USB_GADGET_COMPOSITE_OPTIONS();
106 USB_ETHERNET_MODULE_PARAMETERS();
108 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
109 * Instead: allocate your own, using normal USB-IF procedures.
112 /* Thanks to NetChip Technologies for donating this product ID.
113 * It's for devices with only CDC Ethernet configurations.
115 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
116 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
118 /* For hardware that can't talk CDC, we use the same vendor ID that
119 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
120 * with pxa250. We're protocol-compatible, if the host-side drivers
121 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
122 * drivers that need to hard-wire endpoint numbers have a hook.
124 * The protocol is a minimal subset of CDC Ether, which works on any bulk
125 * hardware that's not deeply broken ... even on hardware that can't talk
126 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
127 * doesn't handle control-OUT).
129 #define SIMPLE_VENDOR_NUM 0x049f
130 #define SIMPLE_PRODUCT_NUM 0x505a
132 /* For hardware that can talk RNDIS and either of the above protocols,
133 * use this ID ... the windows INF files will know it. Unless it's
134 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
135 * the non-RNDIS configuration.
137 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
138 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
140 /* For EEM gadgets */
141 #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
142 #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
144 /*-------------------------------------------------------------------------*/
146 static struct usb_device_descriptor device_desc
= {
147 .bLength
= sizeof device_desc
,
148 .bDescriptorType
= USB_DT_DEVICE
,
150 /* .bcdUSB = DYNAMIC */
152 .bDeviceClass
= USB_CLASS_COMM
,
153 .bDeviceSubClass
= 0,
154 .bDeviceProtocol
= 0,
155 /* .bMaxPacketSize0 = f(hardware) */
157 /* Vendor and product id defaults change according to what configs
158 * we support. (As does bNumConfigurations.) These values can
159 * also be overridden by module parameters.
161 .idVendor
= cpu_to_le16 (CDC_VENDOR_NUM
),
162 .idProduct
= cpu_to_le16 (CDC_PRODUCT_NUM
),
163 /* .bcdDevice = f(hardware) */
164 /* .iManufacturer = DYNAMIC */
165 /* .iProduct = DYNAMIC */
166 /* NO SERIAL NUMBER */
167 .bNumConfigurations
= 1,
170 static const struct usb_descriptor_header
*otg_desc
[2];
172 static struct usb_string strings_dev
[] = {
173 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
174 [USB_GADGET_PRODUCT_IDX
].s
= PREFIX DRIVER_DESC
,
175 [USB_GADGET_SERIAL_IDX
].s
= "",
176 { } /* end of list */
179 static struct usb_gadget_strings stringtab_dev
= {
180 .language
= 0x0409, /* en-us */
181 .strings
= strings_dev
,
184 static struct usb_gadget_strings
*dev_strings
[] = {
189 static struct usb_function_instance
*fi_ecm
;
190 static struct usb_function
*f_ecm
;
192 static struct usb_function_instance
*fi_eem
;
193 static struct usb_function
*f_eem
;
195 static struct usb_function_instance
*fi_geth
;
196 static struct usb_function
*f_geth
;
198 static struct usb_function_instance
*fi_rndis
;
199 static struct usb_function
*f_rndis
;
201 /*-------------------------------------------------------------------------*/
204 * We may not have an RNDIS configuration, but if we do it needs to be
205 * the first one present. That's to make Microsoft's drivers happy,
206 * and to follow DOCSIS 1.0 (cable modem standard).
208 static int rndis_do_config(struct usb_configuration
*c
)
212 /* FIXME alloc iConfiguration string, set it in c->strings */
214 if (gadget_is_otg(c
->cdev
->gadget
)) {
215 c
->descriptors
= otg_desc
;
216 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
219 f_rndis
= usb_get_function(fi_rndis
);
221 return PTR_ERR(f_rndis
);
223 status
= usb_add_function(c
, f_rndis
);
225 usb_put_function(f_rndis
);
230 static struct usb_configuration rndis_config_driver
= {
232 .bConfigurationValue
= 2,
233 /* .iConfiguration = DYNAMIC */
234 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
237 /*-------------------------------------------------------------------------*/
239 #ifdef CONFIG_USB_ETH_EEM
240 static bool use_eem
= 1;
244 module_param(use_eem
, bool, 0);
245 MODULE_PARM_DESC(use_eem
, "use CDC EEM mode");
248 * We _always_ have an ECM, CDC Subset, or EEM configuration.
250 static int eth_do_config(struct usb_configuration
*c
)
254 /* FIXME alloc iConfiguration string, set it in c->strings */
256 if (gadget_is_otg(c
->cdev
->gadget
)) {
257 c
->descriptors
= otg_desc
;
258 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
262 f_eem
= usb_get_function(fi_eem
);
264 return PTR_ERR(f_eem
);
266 status
= usb_add_function(c
, f_eem
);
268 usb_put_function(f_eem
);
271 } else if (can_support_ecm(c
->cdev
->gadget
)) {
272 f_ecm
= usb_get_function(fi_ecm
);
274 return PTR_ERR(f_ecm
);
276 status
= usb_add_function(c
, f_ecm
);
278 usb_put_function(f_ecm
);
282 f_geth
= usb_get_function(fi_geth
);
284 return PTR_ERR(f_geth
);
286 status
= usb_add_function(c
, f_geth
);
288 usb_put_function(f_geth
);
295 static struct usb_configuration eth_config_driver
= {
296 /* .label = f(hardware) */
297 .bConfigurationValue
= 1,
298 /* .iConfiguration = DYNAMIC */
299 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
302 /*-------------------------------------------------------------------------*/
304 static int eth_bind(struct usb_composite_dev
*cdev
)
306 struct usb_gadget
*gadget
= cdev
->gadget
;
307 struct f_eem_opts
*eem_opts
= NULL
;
308 struct f_ecm_opts
*ecm_opts
= NULL
;
309 struct f_gether_opts
*geth_opts
= NULL
;
310 struct net_device
*net
;
313 /* set up main config label and device descriptor */
316 fi_eem
= usb_get_function_instance("eem");
318 return PTR_ERR(fi_eem
);
320 eem_opts
= container_of(fi_eem
, struct f_eem_opts
, func_inst
);
324 eth_config_driver
.label
= "CDC Ethernet (EEM)";
325 device_desc
.idVendor
= cpu_to_le16(EEM_VENDOR_NUM
);
326 device_desc
.idProduct
= cpu_to_le16(EEM_PRODUCT_NUM
);
327 } else if (can_support_ecm(gadget
)) {
330 fi_ecm
= usb_get_function_instance("ecm");
332 return PTR_ERR(fi_ecm
);
334 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
338 eth_config_driver
.label
= "CDC Ethernet (ECM)";
342 fi_geth
= usb_get_function_instance("geth");
344 return PTR_ERR(fi_geth
);
346 geth_opts
= container_of(fi_geth
, struct f_gether_opts
,
349 net
= geth_opts
->net
;
351 eth_config_driver
.label
= "CDC Subset/SAFE";
353 device_desc
.idVendor
= cpu_to_le16(SIMPLE_VENDOR_NUM
);
354 device_desc
.idProduct
= cpu_to_le16(SIMPLE_PRODUCT_NUM
);
356 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
359 gether_set_qmult(net
, qmult
);
360 if (!gether_set_host_addr(net
, host_addr
))
361 pr_info("using host ethernet address: %s", host_addr
);
362 if (!gether_set_dev_addr(net
, dev_addr
))
363 pr_info("using self ethernet address: %s", dev_addr
);
366 /* RNDIS plus ECM-or-Subset */
367 gether_set_gadget(net
, cdev
->gadget
);
368 status
= gether_register_netdev(net
);
373 eem_opts
->bound
= true;
374 else if (can_support_ecm(gadget
))
375 ecm_opts
->bound
= true;
377 geth_opts
->bound
= true;
379 fi_rndis
= usb_get_function_instance("rndis");
380 if (IS_ERR(fi_rndis
)) {
381 status
= PTR_ERR(fi_rndis
);
385 rndis_borrow_net(fi_rndis
, net
);
387 device_desc
.idVendor
= cpu_to_le16(RNDIS_VENDOR_NUM
);
388 device_desc
.idProduct
= cpu_to_le16(RNDIS_PRODUCT_NUM
);
389 device_desc
.bNumConfigurations
= 2;
392 /* Allocate string descriptor numbers ... note that string
393 * contents can be overridden by the composite_dev glue.
396 status
= usb_string_ids_tab(cdev
, strings_dev
);
399 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
400 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
402 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
403 struct usb_descriptor_header
*usb_desc
;
405 usb_desc
= usb_otg_descriptor_alloc(gadget
);
408 usb_otg_descriptor_init(gadget
, usb_desc
);
409 otg_desc
[0] = usb_desc
;
413 /* register our configuration(s); RNDIS first, if it's used */
415 status
= usb_add_config(cdev
, &rndis_config_driver
,
421 status
= usb_add_config(cdev
, ð_config_driver
, eth_do_config
);
425 usb_composite_overwrite_options(cdev
, &coverwrite
);
426 dev_info(&gadget
->dev
, "%s, version: " DRIVER_VERSION
"\n",
436 usb_put_function_instance(fi_rndis
);
439 usb_put_function_instance(fi_eem
);
440 else if (can_support_ecm(gadget
))
441 usb_put_function_instance(fi_ecm
);
443 usb_put_function_instance(fi_geth
);
447 static int eth_unbind(struct usb_composite_dev
*cdev
)
450 usb_put_function(f_rndis
);
451 usb_put_function_instance(fi_rndis
);
454 usb_put_function(f_eem
);
455 usb_put_function_instance(fi_eem
);
456 } else if (can_support_ecm(cdev
->gadget
)) {
457 usb_put_function(f_ecm
);
458 usb_put_function_instance(fi_ecm
);
460 usb_put_function(f_geth
);
461 usb_put_function_instance(fi_geth
);
469 static struct usb_composite_driver eth_driver
= {
472 .strings
= dev_strings
,
473 .max_speed
= USB_SPEED_SUPER
,
475 .unbind
= eth_unbind
,
478 module_usb_composite_driver(eth_driver
);
480 MODULE_DESCRIPTION(PREFIX DRIVER_DESC
);
481 MODULE_AUTHOR("David Brownell, Benedikt Spanger");
482 MODULE_LICENSE("GPL");