usb gadget: link fixes for cdc composite gadget
[deliverable/linux.git] / drivers / usb / gadget / ether.c
CommitLineData
1da177e4
LT
1/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3 *
0391c828 4 * Copyright (C) 2003-2005,2008 David Brownell
1da177e4 5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
0391c828 6 * Copyright (C) 2008 Nokia Corporation
1da177e4
LT
7 *
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.
12 *
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.
17 *
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
21 */
22
0cf4f2de 23/* #define VERBOSE_DEBUG */
1da177e4 24
1da177e4 25#include <linux/kernel.h>
1da177e4 26#include <linux/utsname.h>
1da177e4 27
0391c828 28#include "u_ether.h"
1da177e4 29
1da177e4
LT
30
31/*
32 * Ethernet gadget driver -- with CDC and non-CDC options
33 * Builds on hardware support for a full duplex link.
34 *
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.
40 *
0391c828
DB
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
1da177e4 46 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
11d54898
DB
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.)
51 *
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".)
1da177e4
LT
59 *
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
0391c828 63 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
1da177e4
LT
64 */
65
66#define DRIVER_DESC "Ethernet Gadget"
0391c828 67#define DRIVER_VERSION "Memorial Day 2008"
1da177e4 68
0391c828
DB
69#ifdef CONFIG_USB_ETH_RNDIS
70#define PREFIX "RNDIS/"
71#else
72#define PREFIX ""
1da177e4
LT
73#endif
74
0391c828
DB
75/*
76 * This driver aims for interoperability by using CDC ECM unless
77 *
78 * can_support_ecm()
1da177e4 79 *
0391c828
DB
80 * returns false, in which case it supports the CDC Subset. By default,
81 * that returns true; most hardware has no problems with CDC ECM, that's
82 * a good default. Previous versions of this driver had no default; this
83 * version changes that, removing overhead for new controller support.
84 *
85 * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
1da177e4 86 */
1da177e4 87
0391c828
DB
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
1da177e4
LT
97/*-------------------------------------------------------------------------*/
98
99/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
100 * Instead: allocate your own, using normal USB-IF procedures.
101 */
102
103/* Thanks to NetChip Technologies for donating this product ID.
104 * It's for devices with only CDC Ethernet configurations.
105 */
0391c828
DB
106#define CDC_VENDOR_NUM 0x0525 /* NetChip */
107#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
1da177e4
LT
108
109/* For hardware that can't talk CDC, we use the same vendor ID that
110 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
111 * with pxa250. We're protocol-compatible, if the host-side drivers
112 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
113 * drivers that need to hard-wire endpoint numbers have a hook.
114 *
115 * The protocol is a minimal subset of CDC Ether, which works on any bulk
116 * hardware that's not deeply broken ... even on hardware that can't talk
117 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
118 * doesn't handle control-OUT).
119 */
120#define SIMPLE_VENDOR_NUM 0x049f
121#define SIMPLE_PRODUCT_NUM 0x505a
122
123/* For hardware that can talk RNDIS and either of the above protocols,
124 * use this ID ... the windows INF files will know it. Unless it's
125 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
126 * the non-RNDIS configuration.
127 */
128#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
129#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
130
1da177e4
LT
131/*-------------------------------------------------------------------------*/
132
0391c828 133static struct usb_device_descriptor device_desc = {
1da177e4
LT
134 .bLength = sizeof device_desc,
135 .bDescriptorType = USB_DT_DEVICE,
136
137 .bcdUSB = __constant_cpu_to_le16 (0x0200),
138
139 .bDeviceClass = USB_CLASS_COMM,
140 .bDeviceSubClass = 0,
141 .bDeviceProtocol = 0,
0391c828 142 /* .bMaxPacketSize0 = f(hardware) */
1da177e4 143
0391c828
DB
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 */
1da177e4
LT
148 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
149 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
0391c828
DB
150 /* .bcdDevice = f(hardware) */
151 /* .iManufacturer = DYNAMIC */
152 /* .iProduct = DYNAMIC */
153 /* NO SERIAL NUMBER */
1da177e4
LT
154 .bNumConfigurations = 1,
155};
156
0391c828 157static struct usb_otg_descriptor otg_descriptor = {
1da177e4
LT
158 .bLength = sizeof otg_descriptor,
159 .bDescriptorType = USB_DT_OTG,
160
0391c828
DB
161 /* REVISIT SRP-only hardware is possible, although
162 * it would not be called "OTG" ...
163 */
164 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
1da177e4 165};
7e27f18c 166
0391c828
DB
167static const struct usb_descriptor_header *otg_desc[] = {
168 (struct usb_descriptor_header *) &otg_descriptor,
169 NULL,
1da177e4 170};
1da177e4 171
1da177e4 172
0391c828 173/* string IDs are assigned dynamically */
1da177e4 174
0391c828
DB
175#define STRING_MANUFACTURER_IDX 0
176#define STRING_PRODUCT_IDX 1
11d54898 177
0391c828 178static char manufacturer[50];
1da177e4 179
0391c828
DB
180static struct usb_string strings_dev[] = {
181 [STRING_MANUFACTURER_IDX].s = manufacturer,
182 [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
183 { } /* end of list */
1da177e4
LT
184};
185
0391c828
DB
186static struct usb_gadget_strings stringtab_dev = {
187 .language = 0x0409, /* en-us */
188 .strings = strings_dev,
1da177e4
LT
189};
190
0391c828
DB
191static struct usb_gadget_strings *dev_strings[] = {
192 &stringtab_dev,
193 NULL,
1da177e4
LT
194};
195
0391c828 196static u8 hostaddr[ETH_ALEN];
11d54898 197
0391c828 198/*-------------------------------------------------------------------------*/
11d54898 199
0391c828
DB
200/*
201 * We may not have an RNDIS configuration, but if we do it needs to be
202 * the first one present. That's to make Microsoft's drivers happy,
203 * and to follow DOCSIS 1.0 (cable modem standard).
11d54898 204 */
0391c828
DB
205static int __init rndis_do_config(struct usb_configuration *c)
206{
207 /* FIXME alloc iConfiguration string, set it in c->strings */
11d54898 208
0391c828
DB
209 if (gadget_is_otg(c->cdev->gadget)) {
210 c->descriptors = otg_desc;
211 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
212 }
1da177e4 213
0391c828
DB
214 return rndis_bind_config(c, hostaddr);
215}
1da177e4 216
0391c828
DB
217static struct usb_configuration rndis_config_driver = {
218 .label = "RNDIS",
219 .bind = rndis_do_config,
220 .bConfigurationValue = 2,
221 /* .iConfiguration = DYNAMIC */
222 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
223 .bMaxPower = 1, /* 2 mA, minimal */
1da177e4
LT
224};
225
0391c828 226/*-------------------------------------------------------------------------*/
1da177e4 227
0391c828
DB
228/*
229 * We _always_ have an ECM or CDC Subset configuration.
1da177e4 230 */
0391c828
DB
231static int __init eth_do_config(struct usb_configuration *c)
232{
233 /* FIXME alloc iConfiguration string, set it in c->strings */
7e27f18c 234
0391c828
DB
235 if (gadget_is_otg(c->cdev->gadget)) {
236 c->descriptors = otg_desc;
237 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
238 }
1da177e4 239
0391c828
DB
240 if (can_support_ecm(c->cdev->gadget))
241 return ecm_bind_config(c, hostaddr);
242 else
243 return geth_bind_config(c, hostaddr);
244}
1da177e4 245
0391c828
DB
246static struct usb_configuration eth_config_driver = {
247 /* .label = f(hardware) */
248 .bind = eth_do_config,
249 .bConfigurationValue = 1,
250 /* .iConfiguration = DYNAMIC */
251 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
252 .bMaxPower = 1, /* 2 mA, minimal */
1da177e4
LT
253};
254
0391c828 255/*-------------------------------------------------------------------------*/
1da177e4 256
0391c828
DB
257static int __init eth_bind(struct usb_composite_dev *cdev)
258{
259 int gcnum;
260 struct usb_gadget *gadget = cdev->gadget;
261 int status;
1da177e4 262
0391c828
DB
263 /* set up network link layer */
264 status = gether_setup(cdev->gadget, hostaddr);
265 if (status < 0)
266 return status;
1da177e4 267
0391c828
DB
268 /* set up main config label and device descriptor */
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";
1da177e4 275
0391c828
DB
276 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM),
277 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM),
278 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
279 }
1da177e4 280
0391c828
DB
281 if (has_rndis()) {
282 /* RNDIS plus ECM-or-Subset */
283 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM),
284 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM),
285 device_desc.bNumConfigurations = 2;
286 }
1da177e4 287
0391c828
DB
288 gcnum = usb_gadget_controller_number(gadget);
289 if (gcnum >= 0)
290 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
291 else {
292 /* We assume that can_support_ecm() tells the truth;
293 * but if the controller isn't recognized at all then
294 * that assumption is a bit more likely to be wrong.
295 */
b6c63937 296 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
0391c828
DB
297 gadget->name,
298 eth_config_driver.label);
299 device_desc.bcdDevice =
300 __constant_cpu_to_le16(0x0300 | 0x0099);
301 }
1da177e4 302
1da177e4 303
0391c828
DB
304 /* Allocate string descriptor numbers ... note that string
305 * contents can be overridden by the composite_dev glue.
306 */
1da177e4 307
0391c828
DB
308 /* device descriptor strings: manufacturer, product */
309 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
310 init_utsname()->sysname, init_utsname()->release,
311 gadget->name);
312 status = usb_string_id(cdev);
313 if (status < 0)
314 goto fail;
315 strings_dev[STRING_MANUFACTURER_IDX].id = status;
316 device_desc.iManufacturer = status;
1da177e4 317
0391c828
DB
318 status = usb_string_id(cdev);
319 if (status < 0)
320 goto fail;
321 strings_dev[STRING_PRODUCT_IDX].id = status;
322 device_desc.iProduct = status;
1da177e4 323
0391c828
DB
324 /* register our configuration(s); RNDIS first, if it's used */
325 if (has_rndis()) {
326 status = usb_add_config(cdev, &rndis_config_driver);
327 if (status < 0)
328 goto fail;
329 }
1da177e4 330
0391c828
DB
331 status = usb_add_config(cdev, &eth_config_driver);
332 if (status < 0)
333 goto fail;
1da177e4 334
0391c828 335 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC);
1da177e4 336
0391c828 337 return 0;
1da177e4 338
0391c828
DB
339fail:
340 gether_cleanup();
341 return status;
342}
1da177e4 343
0391c828
DB
344static int __exit eth_unbind(struct usb_composite_dev *cdev)
345{
346 gether_cleanup();
347 return 0;
348}
1da177e4 349
0391c828
DB
350static struct usb_composite_driver eth_driver = {
351 .name = "g_ether",
352 .dev = &device_desc,
353 .strings = dev_strings,
354 .bind = eth_bind,
355 .unbind = __exit_p(eth_unbind),
1da177e4
LT
356};
357
0391c828
DB
358MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
359MODULE_AUTHOR("David Brownell, Benedikt Spanger");
360MODULE_LICENSE("GPL");
1da177e4 361
0391c828 362static int __init init(void)
1da177e4 363{
0391c828 364 return usb_composite_register(&eth_driver);
1da177e4 365}
0391c828 366module_init(init);
1da177e4 367
0391c828 368static void __exit cleanup(void)
1da177e4 369{
0391c828 370 usb_composite_unregister(&eth_driver);
1da177e4 371}
0391c828 372module_exit(cleanup);
This page took 0.45515 seconds and 5 git commands to generate.