usb: gadget: ether: convert to new interface of f_subset
[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.
1da177e4
LT
12 */
13
0cf4f2de 14/* #define VERBOSE_DEBUG */
1da177e4 15
1da177e4 16#include <linux/kernel.h>
8af5232d 17#include <linux/netdevice.h>
396cda90
MN
18
19#if defined USB_ETH_RNDIS
20# undef USB_ETH_RNDIS
21#endif
22#ifdef CONFIG_USB_ETH_RNDIS
23# define USB_ETH_RNDIS y
24#endif
25
0391c828 26#include "u_ether.h"
1da177e4 27
1da177e4
LT
28
29/*
30 * Ethernet gadget driver -- with CDC and non-CDC options
31 * Builds on hardware support for a full duplex link.
32 *
33 * CDC Ethernet is the standard USB solution for sending Ethernet frames
34 * using USB. Real hardware tends to use the same framing protocol but look
35 * different for control features. This driver strongly prefers to use
36 * this USB-IF standard as its open-systems interoperability solution;
37 * most host side USB stacks (except from Microsoft) support it.
38 *
0391c828
DB
39 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
40 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
41 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
42 *
43 * There's some hardware that can't talk CDC ECM. We make that hardware
1da177e4 44 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
11d54898
DB
45 * link-level setup only requires activating the configuration. Only the
46 * endpoint descriptors, and product/vendor IDs, are relevant; no control
47 * operations are available. Linux supports it, but other host operating
48 * systems may not. (This is a subset of CDC Ethernet.)
49 *
50 * It turns out that if you add a few descriptors to that "CDC Subset",
51 * (Windows) host side drivers from MCCI can treat it as one submode of
52 * a proprietary scheme called "SAFE" ... without needing to know about
53 * specific product/vendor IDs. So we do that, making it easier to use
54 * those MS-Windows drivers. Those added descriptors make it resemble a
55 * CDC MDLM device, but they don't change device behavior at all. (See
56 * MCCI Engineering report 950198 "SAFE Networking Functions".)
1da177e4
LT
57 *
58 * A third option is also in use. Rather than CDC Ethernet, or something
59 * simpler, Microsoft pushes their own approach: RNDIS. The published
60 * RNDIS specs are ambiguous and appear to be incomplete, and are also
0391c828 61 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
1da177e4
LT
62 */
63
64#define DRIVER_DESC "Ethernet Gadget"
0391c828 65#define DRIVER_VERSION "Memorial Day 2008"
1da177e4 66
396cda90 67#ifdef USB_ETH_RNDIS
0391c828
DB
68#define PREFIX "RNDIS/"
69#else
70#define PREFIX ""
1da177e4
LT
71#endif
72
0391c828
DB
73/*
74 * This driver aims for interoperability by using CDC ECM unless
75 *
76 * can_support_ecm()
1da177e4 77 *
0391c828
DB
78 * returns false, in which case it supports the CDC Subset. By default,
79 * that returns true; most hardware has no problems with CDC ECM, that's
80 * a good default. Previous versions of this driver had no default; this
81 * version changes that, removing overhead for new controller support.
82 *
83 * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
1da177e4 84 */
1da177e4 85
0391c828
DB
86static inline bool has_rndis(void)
87{
396cda90 88#ifdef USB_ETH_RNDIS
0391c828
DB
89 return true;
90#else
91 return false;
92#endif
93}
94
cbbd14a9
AP
95#include <linux/module.h>
96
1da177e4
LT
97/*-------------------------------------------------------------------------*/
98
33376c1c
DB
99/*
100 * Kbuild is not very cooperative with respect to linking separately
101 * compiled library objects into one module. So for now we won't use
102 * separate compilation ... ensuring init/exit sections work to shrink
103 * the runtime footprint, and giving us at least some parts of what
104 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
105 */
9c62ce83 106#include "u_ecm.h"
8af5232d 107#include "u_gether.h"
396cda90 108#ifdef USB_ETH_RNDIS
33376c1c 109#include "f_rndis.c"
cbbd14a9 110#include "rndis.h"
33376c1c 111#endif
b29002a1 112
94b5573e 113#include "u_eem.h"
33376c1c
DB
114
115/*-------------------------------------------------------------------------*/
7d16e8d3 116USB_GADGET_COMPOSITE_OPTIONS();
33376c1c 117
f1a1823f
AP
118USB_ETHERNET_MODULE_PARAMETERS();
119
1da177e4
LT
120/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
121 * Instead: allocate your own, using normal USB-IF procedures.
122 */
123
124/* Thanks to NetChip Technologies for donating this product ID.
125 * It's for devices with only CDC Ethernet configurations.
126 */
0391c828
DB
127#define CDC_VENDOR_NUM 0x0525 /* NetChip */
128#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
1da177e4
LT
129
130/* For hardware that can't talk CDC, we use the same vendor ID that
131 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
132 * with pxa250. We're protocol-compatible, if the host-side drivers
133 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
134 * drivers that need to hard-wire endpoint numbers have a hook.
135 *
136 * The protocol is a minimal subset of CDC Ether, which works on any bulk
137 * hardware that's not deeply broken ... even on hardware that can't talk
138 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
139 * doesn't handle control-OUT).
140 */
141#define SIMPLE_VENDOR_NUM 0x049f
142#define SIMPLE_PRODUCT_NUM 0x505a
143
144/* For hardware that can talk RNDIS and either of the above protocols,
145 * use this ID ... the windows INF files will know it. Unless it's
146 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
147 * the non-RNDIS configuration.
148 */
149#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
150#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
151
9b39e9dd 152/* For EEM gadgets */
4238ef54
BN
153#define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
154#define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
9b39e9dd 155
1da177e4
LT
156/*-------------------------------------------------------------------------*/
157
0391c828 158static struct usb_device_descriptor device_desc = {
1da177e4
LT
159 .bLength = sizeof device_desc,
160 .bDescriptorType = USB_DT_DEVICE,
161
551509d2 162 .bcdUSB = cpu_to_le16 (0x0200),
1da177e4
LT
163
164 .bDeviceClass = USB_CLASS_COMM,
165 .bDeviceSubClass = 0,
166 .bDeviceProtocol = 0,
0391c828 167 /* .bMaxPacketSize0 = f(hardware) */
1da177e4 168
0391c828
DB
169 /* Vendor and product id defaults change according to what configs
170 * we support. (As does bNumConfigurations.) These values can
171 * also be overridden by module parameters.
172 */
551509d2
HH
173 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
174 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
0391c828
DB
175 /* .bcdDevice = f(hardware) */
176 /* .iManufacturer = DYNAMIC */
177 /* .iProduct = DYNAMIC */
178 /* NO SERIAL NUMBER */
1da177e4
LT
179 .bNumConfigurations = 1,
180};
181
0391c828 182static struct usb_otg_descriptor otg_descriptor = {
1da177e4
LT
183 .bLength = sizeof otg_descriptor,
184 .bDescriptorType = USB_DT_OTG,
185
0391c828
DB
186 /* REVISIT SRP-only hardware is possible, although
187 * it would not be called "OTG" ...
188 */
189 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
1da177e4 190};
7e27f18c 191
0391c828
DB
192static const struct usb_descriptor_header *otg_desc[] = {
193 (struct usb_descriptor_header *) &otg_descriptor,
194 NULL,
1da177e4 195};
1da177e4 196
0391c828 197static struct usb_string strings_dev[] = {
cc2683c3 198 [USB_GADGET_MANUFACTURER_IDX].s = "",
276e2e4f
SAS
199 [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
200 [USB_GADGET_SERIAL_IDX].s = "",
0391c828 201 { } /* end of list */
1da177e4
LT
202};
203
0391c828
DB
204static struct usb_gadget_strings stringtab_dev = {
205 .language = 0x0409, /* en-us */
206 .strings = strings_dev,
1da177e4
LT
207};
208
0391c828
DB
209static struct usb_gadget_strings *dev_strings[] = {
210 &stringtab_dev,
211 NULL,
1da177e4
LT
212};
213
f1a1823f 214static u8 host_mac[ETH_ALEN];
d6a01439 215static struct eth_dev *the_dev;
9c62ce83
AP
216
217static struct usb_function_instance *fi_ecm;
218static struct usb_function *f_ecm;
219
94b5573e
AP
220static struct usb_function_instance *fi_eem;
221static struct usb_function *f_eem;
222
8af5232d
AP
223static struct usb_function_instance *fi_geth;
224static struct usb_function *f_geth;
225
0391c828 226/*-------------------------------------------------------------------------*/
11d54898 227
0391c828
DB
228/*
229 * We may not have an RNDIS configuration, but if we do it needs to be
230 * the first one present. That's to make Microsoft's drivers happy,
231 * and to follow DOCSIS 1.0 (cable modem standard).
11d54898 232 */
e12995ec 233static int __init rndis_do_config(struct usb_configuration *c)
0391c828
DB
234{
235 /* FIXME alloc iConfiguration string, set it in c->strings */
11d54898 236
0391c828
DB
237 if (gadget_is_otg(c->cdev->gadget)) {
238 c->descriptors = otg_desc;
239 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
240 }
1da177e4 241
f1a1823f 242 return rndis_bind_config(c, host_mac, the_dev);
0391c828 243}
1da177e4 244
0391c828
DB
245static struct usb_configuration rndis_config_driver = {
246 .label = "RNDIS",
0391c828
DB
247 .bConfigurationValue = 2,
248 /* .iConfiguration = DYNAMIC */
249 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
1da177e4
LT
250};
251
0391c828 252/*-------------------------------------------------------------------------*/
1da177e4 253
c9188ad2 254#ifdef CONFIG_USB_ETH_EEM
90ab5ee9 255static bool use_eem = 1;
9b39e9dd 256#else
90ab5ee9 257static bool use_eem;
9b39e9dd
BN
258#endif
259module_param(use_eem, bool, 0);
260MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
261
0391c828 262/*
9b39e9dd 263 * We _always_ have an ECM, CDC Subset, or EEM configuration.
1da177e4 264 */
e12995ec 265static int __init eth_do_config(struct usb_configuration *c)
0391c828 266{
9c62ce83
AP
267 int status = 0;
268
0391c828 269 /* FIXME alloc iConfiguration string, set it in c->strings */
7e27f18c 270
0391c828
DB
271 if (gadget_is_otg(c->cdev->gadget)) {
272 c->descriptors = otg_desc;
273 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
274 }
1da177e4 275
94b5573e
AP
276 if (use_eem) {
277 f_eem = usb_get_function(fi_eem);
278 if (IS_ERR(f_eem))
279 return PTR_ERR(f_eem);
280
281 status = usb_add_function(c, f_eem);
282 if (status < 0)
283 usb_put_function(f_eem);
284
285 return status;
286 } else if (can_support_ecm(c->cdev->gadget)) {
9c62ce83
AP
287 f_ecm = usb_get_function(fi_ecm);
288 if (IS_ERR(f_ecm))
289 return PTR_ERR(f_ecm);
290
291 status = usb_add_function(c, f_ecm);
292 if (status < 0)
293 usb_put_function(f_ecm);
294
295 return status;
8af5232d
AP
296 } else {
297 f_geth = usb_get_function(fi_geth);
298 if (IS_ERR(f_geth))
299 return PTR_ERR(f_geth);
300
301 status = usb_add_function(c, f_geth);
302 if (status < 0)
303 usb_put_function(f_geth);
304
305 return status;
306 }
9c62ce83 307
0391c828 308}
1da177e4 309
0391c828
DB
310static struct usb_configuration eth_config_driver = {
311 /* .label = f(hardware) */
0391c828
DB
312 .bConfigurationValue = 1,
313 /* .iConfiguration = DYNAMIC */
314 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
1da177e4
LT
315};
316
0391c828 317/*-------------------------------------------------------------------------*/
1da177e4 318
e12995ec 319static int __init eth_bind(struct usb_composite_dev *cdev)
0391c828 320{
0391c828 321 struct usb_gadget *gadget = cdev->gadget;
94b5573e 322 struct f_eem_opts *eem_opts = NULL;
9c62ce83 323 struct f_ecm_opts *ecm_opts = NULL;
8af5232d
AP
324 struct f_gether_opts *geth_opts = NULL;
325 struct net_device *net;
0391c828 326 int status;
1da177e4 327
0391c828 328 /* set up main config label and device descriptor */
9b39e9dd
BN
329 if (use_eem) {
330 /* EEM */
94b5573e
AP
331 fi_eem = usb_get_function_instance("eem");
332 if (IS_ERR(fi_eem))
333 return PTR_ERR(fi_eem);
334
335 eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
336
8af5232d
AP
337 net = eem_opts->net;
338 the_dev = netdev_priv(net);
94b5573e 339
9b39e9dd
BN
340 eth_config_driver.label = "CDC Ethernet (EEM)";
341 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
342 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
9c62ce83 343 } else if (can_support_ecm(gadget)) {
0391c828 344 /* ECM */
9c62ce83
AP
345
346 fi_ecm = usb_get_function_instance("ecm");
347 if (IS_ERR(fi_ecm))
348 return PTR_ERR(fi_ecm);
349
350 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
351
8af5232d
AP
352 net = ecm_opts->net;
353 the_dev = netdev_priv(net);
9c62ce83 354
0391c828
DB
355 eth_config_driver.label = "CDC Ethernet (ECM)";
356 } else {
357 /* CDC Subset */
8af5232d
AP
358
359 fi_geth = usb_get_function_instance("geth");
360 if (IS_ERR(fi_geth))
361 return PTR_ERR(fi_geth);
362
363 geth_opts = container_of(fi_geth, struct f_gether_opts,
364 func_inst);
365
366 net = geth_opts->net;
367 the_dev = netdev_priv(net);
368
0391c828 369 eth_config_driver.label = "CDC Subset/SAFE";
1da177e4 370
4e19f220
DB
371 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
372 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
373 if (!has_rndis())
374 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
0391c828 375 }
1da177e4 376
8af5232d
AP
377 gether_set_qmult(net, qmult);
378 if (!gether_set_host_addr(net, host_addr))
379 pr_info("using host ethernet address: %s", host_addr);
380 if (!gether_set_dev_addr(net, dev_addr))
381 pr_info("using self ethernet address: %s", dev_addr);
382
0391c828
DB
383 if (has_rndis()) {
384 /* RNDIS plus ECM-or-Subset */
8af5232d
AP
385 gether_set_gadget(net, cdev->gadget);
386 status = gether_register_netdev(net);
387 if (status)
388 goto fail;
389 gether_get_host_addr_u8(net, host_mac);
390
391 if (use_eem)
94b5573e 392 eem_opts->bound = true;
8af5232d 393 else if (can_support_ecm(gadget))
9c62ce83 394 ecm_opts->bound = true;
8af5232d
AP
395 else
396 geth_opts->bound = true;
9c62ce83 397
4e19f220
DB
398 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
399 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
0391c828
DB
400 device_desc.bNumConfigurations = 2;
401 }
1da177e4 402
0391c828
DB
403 /* Allocate string descriptor numbers ... note that string
404 * contents can be overridden by the composite_dev glue.
405 */
1da177e4 406
e1f15ccb 407 status = usb_string_ids_tab(cdev, strings_dev);
0391c828
DB
408 if (status < 0)
409 goto fail;
276e2e4f
SAS
410 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
411 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
1da177e4 412
0391c828
DB
413 /* register our configuration(s); RNDIS first, if it's used */
414 if (has_rndis()) {
c9bfff9c
UKK
415 status = usb_add_config(cdev, &rndis_config_driver,
416 rndis_do_config);
0391c828
DB
417 if (status < 0)
418 goto fail;
419 }
1da177e4 420
c9bfff9c 421 status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
0391c828
DB
422 if (status < 0)
423 goto fail;
1da177e4 424
7d16e8d3 425 usb_composite_overwrite_options(cdev, &coverwrite);
33376c1c
DB
426 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
427 DRIVER_DESC);
1da177e4 428
0391c828 429 return 0;
1da177e4 430
0391c828 431fail:
8af5232d 432 if (use_eem)
94b5573e 433 usb_put_function_instance(fi_eem);
8af5232d 434 else if (can_support_ecm(gadget))
9c62ce83 435 usb_put_function_instance(fi_ecm);
8af5232d
AP
436 else
437 usb_put_function_instance(fi_geth);
0391c828
DB
438 return status;
439}
1da177e4 440
0391c828
DB
441static int __exit eth_unbind(struct usb_composite_dev *cdev)
442{
8af5232d 443 if (use_eem)
94b5573e 444 usb_put_function_instance(fi_eem);
8af5232d 445 else if (can_support_ecm(cdev->gadget))
9c62ce83 446 usb_put_function_instance(fi_ecm);
8af5232d
AP
447 else
448 usb_put_function_instance(fi_geth);
0391c828
DB
449 return 0;
450}
1da177e4 451
c2ec75c2 452static __refdata struct usb_composite_driver eth_driver = {
0391c828
DB
453 .name = "g_ether",
454 .dev = &device_desc,
455 .strings = dev_strings,
04617db7 456 .max_speed = USB_SPEED_SUPER,
03e42bd5 457 .bind = eth_bind,
0391c828 458 .unbind = __exit_p(eth_unbind),
1da177e4
LT
459};
460
0391c828
DB
461MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
462MODULE_AUTHOR("David Brownell, Benedikt Spanger");
463MODULE_LICENSE("GPL");
1da177e4 464
0391c828 465static int __init init(void)
1da177e4 466{
03e42bd5 467 return usb_composite_probe(&eth_driver);
1da177e4 468}
0391c828 469module_init(init);
1da177e4 470
0391c828 471static void __exit cleanup(void)
1da177e4 472{
0391c828 473 usb_composite_unregister(&eth_driver);
1da177e4 474}
0391c828 475module_exit(cleanup);
This page took 1.345115 seconds and 5 git commands to generate.