On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / usb / gadget / ether.c
blobf37de283d0ab769e37c506c24e4e433ba2ffb583
1 /*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* #define VERBOSE_DEBUG */
25 #include <linux/kernel.h>
26 #include <linux/utsname.h>
28 #include "u_ether.h"
32 * Ethernet gadget driver -- with CDC and non-CDC options
33 * Builds on hardware support for a full duplex link.
35 * CDC Ethernet is the standard USB solution for sending Ethernet frames
36 * using USB. Real hardware tends to use the same framing protocol but look
37 * different for control features. This driver strongly prefers to use
38 * this USB-IF standard as its open-systems interoperability solution;
39 * most host side USB stacks (except from Microsoft) support it.
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.
45 * There's some hardware that can't talk CDC ECM. We make that hardware
46 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
47 * link-level setup only requires activating the configuration. Only the
48 * endpoint descriptors, and product/vendor IDs, are relevant; no control
49 * operations are available. Linux supports it, but other host operating
50 * systems may not. (This is a subset of CDC Ethernet.)
52 * It turns out that if you add a few descriptors to that "CDC Subset",
53 * (Windows) host side drivers from MCCI can treat it as one submode of
54 * a proprietary scheme called "SAFE" ... without needing to know about
55 * specific product/vendor IDs. So we do that, making it easier to use
56 * those MS-Windows drivers. Those added descriptors make it resemble a
57 * CDC MDLM device, but they don't change device behavior at all. (See
58 * MCCI Engineering report 950198 "SAFE Networking Functions".)
60 * A third option is also in use. Rather than CDC Ethernet, or something
61 * simpler, Microsoft pushes their own approach: RNDIS. The published
62 * RNDIS specs are ambiguous and appear to be incomplete, and are also
63 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
65 * While CDC ECM, CDC Subset, and RNDIS are designed to extend the ethernet
66 * interface to the target, CDC EEM was designed to use ethernet over the USB
67 * link between the host and target. CDC EEM is implemented as an alternative
68 * to those other protocols when that communication model is more appropriate
71 #define DRIVER_DESC "Ethernet Gadget"
72 #define DRIVER_VERSION "Memorial Day 2008"
74 #ifdef CONFIG_USB_ETH_RNDIS
75 #define PREFIX "RNDIS/"
76 #else
77 #define PREFIX ""
78 #endif
81 * This driver aims for interoperability by using CDC ECM unless
83 * can_support_ecm()
85 * returns false, in which case it supports the CDC Subset. By default,
86 * that returns true; most hardware has no problems with CDC ECM, that's
87 * a good default. Previous versions of this driver had no default; this
88 * version changes that, removing overhead for new controller support.
90 * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
93 static inline bool has_rndis(void)
95 #ifdef CONFIG_USB_ETH_RNDIS
96 return true;
97 #else
98 return false;
99 #endif
102 /*-------------------------------------------------------------------------*/
105 * Kbuild is not very cooperative with respect to linking separately
106 * compiled library objects into one module. So for now we won't use
107 * separate compilation ... ensuring init/exit sections work to shrink
108 * the runtime footprint, and giving us at least some parts of what
109 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
111 #include "composite.c"
112 #include "usbstring.c"
113 #include "config.c"
114 #include "epautoconf.c"
116 #include "f_ecm.c"
117 #include "f_subset.c"
118 #ifdef CONFIG_USB_ETH_RNDIS
119 #include "f_rndis.c"
120 #include "rndis.c"
121 #endif
122 #include "f_eem.c"
123 #include "u_ether.c"
125 /*-------------------------------------------------------------------------*/
127 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
128 * Instead: allocate your own, using normal USB-IF procedures.
131 /* Thanks to NetChip Technologies for donating this product ID.
132 * It's for devices with only CDC Ethernet configurations.
134 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
135 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
137 /* For hardware that can't talk CDC, we use the same vendor ID that
138 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
139 * with pxa250. We're protocol-compatible, if the host-side drivers
140 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
141 * drivers that need to hard-wire endpoint numbers have a hook.
143 * The protocol is a minimal subset of CDC Ether, which works on any bulk
144 * hardware that's not deeply broken ... even on hardware that can't talk
145 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
146 * doesn't handle control-OUT).
148 #define SIMPLE_VENDOR_NUM 0x049f
149 #define SIMPLE_PRODUCT_NUM 0x505a
151 /* For hardware that can talk RNDIS and either of the above protocols,
152 * use this ID ... the windows INF files will know it. Unless it's
153 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
154 * the non-RNDIS configuration.
156 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
157 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
159 /* For EEM gadgets */
160 #define EEM_VENDOR_NUM 0x0525 /* INVALID - NEEDS TO BE ALLOCATED */
161 #define EEM_PRODUCT_NUM 0xa4a1 /* INVALID - NEEDS TO BE ALLOCATED */
163 /*-------------------------------------------------------------------------*/
165 static struct usb_device_descriptor device_desc = {
166 .bLength = sizeof device_desc,
167 .bDescriptorType = USB_DT_DEVICE,
169 .bcdUSB = cpu_to_le16 (0x0200),
171 .bDeviceClass = USB_CLASS_COMM,
172 .bDeviceSubClass = 0,
173 .bDeviceProtocol = 0,
174 /* .bMaxPacketSize0 = f(hardware) */
176 /* Vendor and product id defaults change according to what configs
177 * we support. (As does bNumConfigurations.) These values can
178 * also be overridden by module parameters.
180 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
181 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
182 /* .bcdDevice = f(hardware) */
183 /* .iManufacturer = DYNAMIC */
184 /* .iProduct = DYNAMIC */
185 /* NO SERIAL NUMBER */
186 .bNumConfigurations = 1,
189 static struct usb_otg_descriptor otg_descriptor = {
190 .bLength = sizeof otg_descriptor,
191 .bDescriptorType = USB_DT_OTG,
193 /* REVISIT SRP-only hardware is possible, although
194 * it would not be called "OTG" ...
196 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
199 static const struct usb_descriptor_header *otg_desc[] = {
200 (struct usb_descriptor_header *) &otg_descriptor,
201 NULL,
205 /* string IDs are assigned dynamically */
207 #define STRING_MANUFACTURER_IDX 0
208 #define STRING_PRODUCT_IDX 1
210 static char manufacturer[50];
212 static struct usb_string strings_dev[] = {
213 [STRING_MANUFACTURER_IDX].s = manufacturer,
214 [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
215 { } /* end of list */
218 static struct usb_gadget_strings stringtab_dev = {
219 .language = 0x0409, /* en-us */
220 .strings = strings_dev,
223 static struct usb_gadget_strings *dev_strings[] = {
224 &stringtab_dev,
225 NULL,
228 static u8 hostaddr[ETH_ALEN];
230 /*-------------------------------------------------------------------------*/
233 * We may not have an RNDIS configuration, but if we do it needs to be
234 * the first one present. That's to make Microsoft's drivers happy,
235 * and to follow DOCSIS 1.0 (cable modem standard).
237 static int __init rndis_do_config(struct usb_configuration *c)
239 /* FIXME alloc iConfiguration string, set it in c->strings */
241 if (gadget_is_otg(c->cdev->gadget)) {
242 c->descriptors = otg_desc;
243 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
246 return rndis_bind_config(c, hostaddr);
249 static struct usb_configuration rndis_config_driver = {
250 .label = "RNDIS",
251 .bind = rndis_do_config,
252 .bConfigurationValue = 2,
253 /* .iConfiguration = DYNAMIC */
254 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
257 /*-------------------------------------------------------------------------*/
259 #ifdef CONFIG_USB_ETH_EEM
260 static int use_eem = 1;
261 #else
262 static int use_eem;
263 #endif
264 module_param(use_eem, bool, 0);
265 MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
268 * We _always_ have an ECM, CDC Subset, or EEM configuration.
270 static int __init eth_do_config(struct usb_configuration *c)
272 /* FIXME alloc iConfiguration string, set it in c->strings */
274 if (gadget_is_otg(c->cdev->gadget)) {
275 c->descriptors = otg_desc;
276 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
279 if (use_eem)
280 return eem_bind_config(c);
281 else if (can_support_ecm(c->cdev->gadget))
282 return ecm_bind_config(c, hostaddr);
283 else
284 return geth_bind_config(c, hostaddr);
287 static struct usb_configuration eth_config_driver = {
288 /* .label = f(hardware) */
289 .bind = eth_do_config,
290 .bConfigurationValue = 1,
291 /* .iConfiguration = DYNAMIC */
292 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
295 /*-------------------------------------------------------------------------*/
297 static int __init eth_bind(struct usb_composite_dev *cdev)
299 int gcnum;
300 struct usb_gadget *gadget = cdev->gadget;
301 int status;
303 /* set up network link layer */
304 status = gether_setup(cdev->gadget, hostaddr);
305 if (status < 0)
306 return status;
308 /* set up main config label and device descriptor */
309 if (use_eem) {
310 /* EEM */
311 eth_config_driver.label = "CDC Ethernet (EEM)";
312 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
313 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
314 } else if (can_support_ecm(cdev->gadget)) {
315 /* ECM */
316 eth_config_driver.label = "CDC Ethernet (ECM)";
317 } else {
318 /* CDC Subset */
319 eth_config_driver.label = "CDC Subset/SAFE";
321 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
322 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
323 if (!has_rndis())
324 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
327 if (has_rndis()) {
328 /* RNDIS plus ECM-or-Subset */
329 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
330 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
331 device_desc.bNumConfigurations = 2;
334 gcnum = usb_gadget_controller_number(gadget);
335 if (gcnum >= 0)
336 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
337 else {
338 /* We assume that can_support_ecm() tells the truth;
339 * but if the controller isn't recognized at all then
340 * that assumption is a bit more likely to be wrong.
342 dev_warn(&gadget->dev,
343 "controller '%s' not recognized; trying %s\n",
344 gadget->name,
345 eth_config_driver.label);
346 device_desc.bcdDevice =
347 cpu_to_le16(0x0300 | 0x0099);
351 /* Allocate string descriptor numbers ... note that string
352 * contents can be overridden by the composite_dev glue.
355 /* device descriptor strings: manufacturer, product */
356 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
357 init_utsname()->sysname, init_utsname()->release,
358 gadget->name);
359 status = usb_string_id(cdev);
360 if (status < 0)
361 goto fail;
362 strings_dev[STRING_MANUFACTURER_IDX].id = status;
363 device_desc.iManufacturer = status;
365 status = usb_string_id(cdev);
366 if (status < 0)
367 goto fail;
368 strings_dev[STRING_PRODUCT_IDX].id = status;
369 device_desc.iProduct = status;
371 /* register our configuration(s); RNDIS first, if it's used */
372 if (has_rndis()) {
373 status = usb_add_config(cdev, &rndis_config_driver);
374 if (status < 0)
375 goto fail;
378 status = usb_add_config(cdev, &eth_config_driver);
379 if (status < 0)
380 goto fail;
382 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
383 DRIVER_DESC);
385 return 0;
387 fail:
388 gether_cleanup();
389 return status;
392 static int __exit eth_unbind(struct usb_composite_dev *cdev)
394 gether_cleanup();
395 return 0;
398 static struct usb_composite_driver eth_driver = {
399 .name = "g_ether",
400 .dev = &device_desc,
401 .strings = dev_strings,
402 .bind = eth_bind,
403 .unbind = __exit_p(eth_unbind),
406 MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
407 MODULE_AUTHOR("David Brownell, Benedikt Spanger");
408 MODULE_LICENSE("GPL");
410 static int __init init(void)
412 return usb_composite_register(&eth_driver);
414 module_init(init);
416 static void __exit cleanup(void)
418 usb_composite_unregister(&eth_driver);
420 module_exit(cleanup);