Merge remote-tracking branch 'fuse/for-next'
[deliverable/linux.git] / drivers / usb / gadget / legacy / 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
9c62ce83 97#include "u_ecm.h"
8af5232d 98#include "u_gether.h"
396cda90 99#ifdef USB_ETH_RNDIS
9bd4a10e 100#include "u_rndis.h"
cbbd14a9 101#include "rndis.h"
9bd4a10e
AP
102#else
103#define rndis_borrow_net(...) do {} while (0)
33376c1c 104#endif
94b5573e 105#include "u_eem.h"
33376c1c
DB
106
107/*-------------------------------------------------------------------------*/
7d16e8d3 108USB_GADGET_COMPOSITE_OPTIONS();
33376c1c 109
f1a1823f
AP
110USB_ETHERNET_MODULE_PARAMETERS();
111
1da177e4
LT
112/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
113 * Instead: allocate your own, using normal USB-IF procedures.
114 */
115
116/* Thanks to NetChip Technologies for donating this product ID.
117 * It's for devices with only CDC Ethernet configurations.
118 */
0391c828
DB
119#define CDC_VENDOR_NUM 0x0525 /* NetChip */
120#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
1da177e4
LT
121
122/* For hardware that can't talk CDC, we use the same vendor ID that
123 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
124 * with pxa250. We're protocol-compatible, if the host-side drivers
125 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
126 * drivers that need to hard-wire endpoint numbers have a hook.
127 *
128 * The protocol is a minimal subset of CDC Ether, which works on any bulk
129 * hardware that's not deeply broken ... even on hardware that can't talk
130 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
131 * doesn't handle control-OUT).
132 */
133#define SIMPLE_VENDOR_NUM 0x049f
134#define SIMPLE_PRODUCT_NUM 0x505a
135
136/* For hardware that can talk RNDIS and either of the above protocols,
137 * use this ID ... the windows INF files will know it. Unless it's
138 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
139 * the non-RNDIS configuration.
140 */
141#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
142#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
143
9b39e9dd 144/* For EEM gadgets */
4238ef54
BN
145#define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
146#define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
9b39e9dd 147
1da177e4
LT
148/*-------------------------------------------------------------------------*/
149
0391c828 150static struct usb_device_descriptor device_desc = {
1da177e4
LT
151 .bLength = sizeof device_desc,
152 .bDescriptorType = USB_DT_DEVICE,
153
0aecfc1b 154 /* .bcdUSB = DYNAMIC */
1da177e4
LT
155
156 .bDeviceClass = USB_CLASS_COMM,
157 .bDeviceSubClass = 0,
158 .bDeviceProtocol = 0,
0391c828 159 /* .bMaxPacketSize0 = f(hardware) */
1da177e4 160
0391c828
DB
161 /* Vendor and product id defaults change according to what configs
162 * we support. (As does bNumConfigurations.) These values can
163 * also be overridden by module parameters.
164 */
551509d2
HH
165 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
166 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
0391c828
DB
167 /* .bcdDevice = f(hardware) */
168 /* .iManufacturer = DYNAMIC */
169 /* .iProduct = DYNAMIC */
170 /* NO SERIAL NUMBER */
1da177e4
LT
171 .bNumConfigurations = 1,
172};
173
9b95236e 174static const struct usb_descriptor_header *otg_desc[2];
1da177e4 175
0391c828 176static struct usb_string strings_dev[] = {
cc2683c3 177 [USB_GADGET_MANUFACTURER_IDX].s = "",
276e2e4f
SAS
178 [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
179 [USB_GADGET_SERIAL_IDX].s = "",
0391c828 180 { } /* end of list */
1da177e4
LT
181};
182
0391c828
DB
183static struct usb_gadget_strings stringtab_dev = {
184 .language = 0x0409, /* en-us */
185 .strings = strings_dev,
1da177e4
LT
186};
187
0391c828
DB
188static struct usb_gadget_strings *dev_strings[] = {
189 &stringtab_dev,
190 NULL,
1da177e4
LT
191};
192
9c62ce83
AP
193static struct usb_function_instance *fi_ecm;
194static struct usb_function *f_ecm;
195
94b5573e
AP
196static struct usb_function_instance *fi_eem;
197static struct usb_function *f_eem;
198
8af5232d
AP
199static struct usb_function_instance *fi_geth;
200static struct usb_function *f_geth;
201
9bd4a10e
AP
202static struct usb_function_instance *fi_rndis;
203static struct usb_function *f_rndis;
204
0391c828 205/*-------------------------------------------------------------------------*/
11d54898 206
0391c828
DB
207/*
208 * We may not have an RNDIS configuration, but if we do it needs to be
209 * the first one present. That's to make Microsoft's drivers happy,
210 * and to follow DOCSIS 1.0 (cable modem standard).
11d54898 211 */
c94e289f 212static int rndis_do_config(struct usb_configuration *c)
0391c828 213{
9bd4a10e
AP
214 int status;
215
0391c828 216 /* FIXME alloc iConfiguration string, set it in c->strings */
11d54898 217
0391c828
DB
218 if (gadget_is_otg(c->cdev->gadget)) {
219 c->descriptors = otg_desc;
220 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
221 }
1da177e4 222
9bd4a10e
AP
223 f_rndis = usb_get_function(fi_rndis);
224 if (IS_ERR(f_rndis))
225 return PTR_ERR(f_rndis);
226
227 status = usb_add_function(c, f_rndis);
228 if (status < 0)
229 usb_put_function(f_rndis);
230
231 return status;
0391c828 232}
1da177e4 233
0391c828
DB
234static struct usb_configuration rndis_config_driver = {
235 .label = "RNDIS",
0391c828
DB
236 .bConfigurationValue = 2,
237 /* .iConfiguration = DYNAMIC */
238 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
1da177e4
LT
239};
240
0391c828 241/*-------------------------------------------------------------------------*/
1da177e4 242
c9188ad2 243#ifdef CONFIG_USB_ETH_EEM
90ab5ee9 244static bool use_eem = 1;
9b39e9dd 245#else
90ab5ee9 246static bool use_eem;
9b39e9dd
BN
247#endif
248module_param(use_eem, bool, 0);
249MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
250
0391c828 251/*
9b39e9dd 252 * We _always_ have an ECM, CDC Subset, or EEM configuration.
1da177e4 253 */
c94e289f 254static int eth_do_config(struct usb_configuration *c)
0391c828 255{
9c62ce83
AP
256 int status = 0;
257
0391c828 258 /* FIXME alloc iConfiguration string, set it in c->strings */
7e27f18c 259
0391c828
DB
260 if (gadget_is_otg(c->cdev->gadget)) {
261 c->descriptors = otg_desc;
262 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
263 }
1da177e4 264
94b5573e
AP
265 if (use_eem) {
266 f_eem = usb_get_function(fi_eem);
267 if (IS_ERR(f_eem))
268 return PTR_ERR(f_eem);
269
270 status = usb_add_function(c, f_eem);
271 if (status < 0)
272 usb_put_function(f_eem);
273
274 return status;
275 } else if (can_support_ecm(c->cdev->gadget)) {
9c62ce83
AP
276 f_ecm = usb_get_function(fi_ecm);
277 if (IS_ERR(f_ecm))
278 return PTR_ERR(f_ecm);
279
280 status = usb_add_function(c, f_ecm);
281 if (status < 0)
282 usb_put_function(f_ecm);
283
284 return status;
8af5232d
AP
285 } else {
286 f_geth = usb_get_function(fi_geth);
287 if (IS_ERR(f_geth))
288 return PTR_ERR(f_geth);
289
290 status = usb_add_function(c, f_geth);
291 if (status < 0)
292 usb_put_function(f_geth);
293
294 return status;
295 }
9c62ce83 296
0391c828 297}
1da177e4 298
0391c828
DB
299static struct usb_configuration eth_config_driver = {
300 /* .label = f(hardware) */
0391c828
DB
301 .bConfigurationValue = 1,
302 /* .iConfiguration = DYNAMIC */
303 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
1da177e4
LT
304};
305
0391c828 306/*-------------------------------------------------------------------------*/
1da177e4 307
c94e289f 308static int eth_bind(struct usb_composite_dev *cdev)
0391c828 309{
0391c828 310 struct usb_gadget *gadget = cdev->gadget;
94b5573e 311 struct f_eem_opts *eem_opts = NULL;
9c62ce83 312 struct f_ecm_opts *ecm_opts = NULL;
8af5232d
AP
313 struct f_gether_opts *geth_opts = NULL;
314 struct net_device *net;
0391c828 315 int status;
1da177e4 316
0391c828 317 /* set up main config label and device descriptor */
9b39e9dd
BN
318 if (use_eem) {
319 /* EEM */
94b5573e
AP
320 fi_eem = usb_get_function_instance("eem");
321 if (IS_ERR(fi_eem))
322 return PTR_ERR(fi_eem);
323
324 eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
325
8af5232d 326 net = eem_opts->net;
94b5573e 327
9b39e9dd
BN
328 eth_config_driver.label = "CDC Ethernet (EEM)";
329 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
330 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
9c62ce83 331 } else if (can_support_ecm(gadget)) {
0391c828 332 /* ECM */
9c62ce83
AP
333
334 fi_ecm = usb_get_function_instance("ecm");
335 if (IS_ERR(fi_ecm))
336 return PTR_ERR(fi_ecm);
337
338 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
339
8af5232d 340 net = ecm_opts->net;
9c62ce83 341
0391c828
DB
342 eth_config_driver.label = "CDC Ethernet (ECM)";
343 } else {
344 /* CDC Subset */
8af5232d
AP
345
346 fi_geth = usb_get_function_instance("geth");
347 if (IS_ERR(fi_geth))
348 return PTR_ERR(fi_geth);
349
350 geth_opts = container_of(fi_geth, struct f_gether_opts,
351 func_inst);
352
353 net = geth_opts->net;
8af5232d 354
0391c828 355 eth_config_driver.label = "CDC Subset/SAFE";
1da177e4 356
4e19f220
DB
357 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
358 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
359 if (!has_rndis())
360 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
0391c828 361 }
1da177e4 362
8af5232d
AP
363 gether_set_qmult(net, qmult);
364 if (!gether_set_host_addr(net, host_addr))
365 pr_info("using host ethernet address: %s", host_addr);
366 if (!gether_set_dev_addr(net, dev_addr))
367 pr_info("using self ethernet address: %s", dev_addr);
368
0391c828
DB
369 if (has_rndis()) {
370 /* RNDIS plus ECM-or-Subset */
8af5232d
AP
371 gether_set_gadget(net, cdev->gadget);
372 status = gether_register_netdev(net);
373 if (status)
374 goto fail;
8af5232d
AP
375
376 if (use_eem)
94b5573e 377 eem_opts->bound = true;
8af5232d 378 else if (can_support_ecm(gadget))
9c62ce83 379 ecm_opts->bound = true;
8af5232d
AP
380 else
381 geth_opts->bound = true;
9c62ce83 382
9bd4a10e
AP
383 fi_rndis = usb_get_function_instance("rndis");
384 if (IS_ERR(fi_rndis)) {
385 status = PTR_ERR(fi_rndis);
386 goto fail;
387 }
388
389 rndis_borrow_net(fi_rndis, net);
390
4e19f220
DB
391 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
392 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
0391c828
DB
393 device_desc.bNumConfigurations = 2;
394 }
1da177e4 395
0391c828
DB
396 /* Allocate string descriptor numbers ... note that string
397 * contents can be overridden by the composite_dev glue.
398 */
1da177e4 399
e1f15ccb 400 status = usb_string_ids_tab(cdev, strings_dev);
0391c828 401 if (status < 0)
9bd4a10e 402 goto fail1;
276e2e4f
SAS
403 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
404 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
1da177e4 405
9b95236e
LJ
406 if (gadget_is_otg(gadget) && !otg_desc[0]) {
407 struct usb_descriptor_header *usb_desc;
408
409 usb_desc = usb_otg_descriptor_alloc(gadget);
410 if (!usb_desc)
411 goto fail1;
412 usb_otg_descriptor_init(gadget, usb_desc);
413 otg_desc[0] = usb_desc;
414 otg_desc[1] = NULL;
415 }
416
0391c828
DB
417 /* register our configuration(s); RNDIS first, if it's used */
418 if (has_rndis()) {
c9bfff9c
UKK
419 status = usb_add_config(cdev, &rndis_config_driver,
420 rndis_do_config);
0391c828 421 if (status < 0)
9b95236e 422 goto fail2;
0391c828 423 }
1da177e4 424
c9bfff9c 425 status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
0391c828 426 if (status < 0)
9b95236e 427 goto fail2;
1da177e4 428
7d16e8d3 429 usb_composite_overwrite_options(cdev, &coverwrite);
33376c1c
DB
430 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
431 DRIVER_DESC);
1da177e4 432
0391c828 433 return 0;
1da177e4 434
9b95236e
LJ
435fail2:
436 kfree(otg_desc[0]);
437 otg_desc[0] = NULL;
9bd4a10e
AP
438fail1:
439 if (has_rndis())
440 usb_put_function_instance(fi_rndis);
0391c828 441fail:
8af5232d 442 if (use_eem)
94b5573e 443 usb_put_function_instance(fi_eem);
8af5232d 444 else if (can_support_ecm(gadget))
9c62ce83 445 usb_put_function_instance(fi_ecm);
8af5232d
AP
446 else
447 usb_put_function_instance(fi_geth);
0391c828
DB
448 return status;
449}
1da177e4 450
c94e289f 451static int eth_unbind(struct usb_composite_dev *cdev)
0391c828 452{
23a113a0
AP
453 if (has_rndis()) {
454 usb_put_function(f_rndis);
9bd4a10e 455 usb_put_function_instance(fi_rndis);
23a113a0
AP
456 }
457 if (use_eem) {
458 usb_put_function(f_eem);
94b5573e 459 usb_put_function_instance(fi_eem);
23a113a0
AP
460 } else if (can_support_ecm(cdev->gadget)) {
461 usb_put_function(f_ecm);
9c62ce83 462 usb_put_function_instance(fi_ecm);
23a113a0
AP
463 } else {
464 usb_put_function(f_geth);
8af5232d 465 usb_put_function_instance(fi_geth);
23a113a0 466 }
9b95236e
LJ
467 kfree(otg_desc[0]);
468 otg_desc[0] = NULL;
469
0391c828
DB
470 return 0;
471}
1da177e4 472
c94e289f 473static struct usb_composite_driver eth_driver = {
0391c828
DB
474 .name = "g_ether",
475 .dev = &device_desc,
476 .strings = dev_strings,
04617db7 477 .max_speed = USB_SPEED_SUPER,
03e42bd5 478 .bind = eth_bind,
c94e289f 479 .unbind = eth_unbind,
1da177e4
LT
480};
481
909346a8
TK
482module_usb_composite_driver(eth_driver);
483
0391c828
DB
484MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
485MODULE_AUTHOR("David Brownell, Benedikt Spanger");
486MODULE_LICENSE("GPL");
This page took 1.074006 seconds and 5 git commands to generate.