blob: 31e9160223e9aa772669ad4128f27729e6decef5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3 *
David Brownell0391c822008-06-19 18:20:11 -07004 * Copyright (C) 2003-2005,2008 David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
David Brownell0391c822008-06-19 18:20:11 -07006 * Copyright (C) 2008 Nokia Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
David Brownell0cf4f2d2007-08-02 00:00:38 -070014/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +020017#include <linux/netdevice.h>
Michal Nazarewicz396cda92009-11-30 10:55:40 +010018
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
David Brownell0391c822008-06-19 18:20:11 -070026#include "u_ether.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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 *
David Brownell0391c822008-06-19 18:20:11 -070039 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
David Brownell11d54892006-12-11 15:59:04 -080045 * 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".)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
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
David Brownell0391c822008-06-19 18:20:11 -070061 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
64#define DRIVER_DESC "Ethernet Gadget"
David Brownell0391c822008-06-19 18:20:11 -070065#define DRIVER_VERSION "Memorial Day 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Michal Nazarewicz396cda92009-11-30 10:55:40 +010067#ifdef USB_ETH_RNDIS
David Brownell0391c822008-06-19 18:20:11 -070068#define PREFIX "RNDIS/"
69#else
70#define PREFIX ""
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
David Brownell0391c822008-06-19 18:20:11 -070073/*
74 * This driver aims for interoperability by using CDC ECM unless
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
David Brownell0391c822008-06-19 18:20:11 -070076 * can_support_ecm()
77 *
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!
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
David Brownell0391c822008-06-19 18:20:11 -070086static inline bool has_rndis(void)
87{
Michal Nazarewicz396cda92009-11-30 10:55:40 +010088#ifdef USB_ETH_RNDIS
David Brownell0391c822008-06-19 18:20:11 -070089 return true;
90#else
91 return false;
92#endif
93}
94
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +020095#include <linux/module.h>
96
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +020097#include "u_ecm.h"
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +020098#include "u_gether.h"
Michal Nazarewicz396cda92009-11-30 10:55:40 +010099#ifdef USB_ETH_RNDIS
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200100#include "u_rndis.h"
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +0200101#include "rndis.h"
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200102#else
103#define rndis_borrow_net(...) do {} while (0)
David Brownell33376c12008-08-18 17:45:07 -0700104#endif
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200105#include "u_eem.h"
David Brownell33376c12008-08-18 17:45:07 -0700106
107/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200108USB_GADGET_COMPOSITE_OPTIONS();
David Brownell33376c12008-08-18 17:45:07 -0700109
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200110USB_ETHERNET_MODULE_PARAMETERS();
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* 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 */
David Brownell0391c822008-06-19 18:20:11 -0700119#define CDC_VENDOR_NUM 0x0525 /* NetChip */
120#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
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
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500144/* For EEM gadgets */
Brian Niebuhr4238ef52009-10-14 12:04:33 -0500145#define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
146#define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*-------------------------------------------------------------------------*/
149
David Brownell0391c822008-06-19 18:20:11 -0700150static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .bLength = sizeof device_desc,
152 .bDescriptorType = USB_DT_DEVICE,
153
Harvey Harrison551509d2009-02-11 14:11:36 -0800154 .bcdUSB = cpu_to_le16 (0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 .bDeviceClass = USB_CLASS_COMM,
157 .bDeviceSubClass = 0,
158 .bDeviceProtocol = 0,
David Brownell0391c822008-06-19 18:20:11 -0700159 /* .bMaxPacketSize0 = f(hardware) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Brownell0391c822008-06-19 18:20:11 -0700161 /* 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 */
Harvey Harrison551509d2009-02-11 14:11:36 -0800165 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
166 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
David Brownell0391c822008-06-19 18:20:11 -0700167 /* .bcdDevice = f(hardware) */
168 /* .iManufacturer = DYNAMIC */
169 /* .iProduct = DYNAMIC */
170 /* NO SERIAL NUMBER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .bNumConfigurations = 1,
172};
173
Li Jun9b95236e2015-07-09 15:18:49 +0800174static const struct usb_descriptor_header *otg_desc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Brownell0391c822008-06-19 18:20:11 -0700176static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +0200177 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200178 [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
179 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell0391c822008-06-19 18:20:11 -0700180 { } /* end of list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
David Brownell0391c822008-06-19 18:20:11 -0700183static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .language = 0x0409, /* en-us */
David Brownell0391c822008-06-19 18:20:11 -0700185 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
David Brownell0391c822008-06-19 18:20:11 -0700188static struct usb_gadget_strings *dev_strings[] = {
189 &stringtab_dev,
190 NULL,
191};
192
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200193static struct usb_function_instance *fi_ecm;
194static struct usb_function *f_ecm;
195
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200196static struct usb_function_instance *fi_eem;
197static struct usb_function *f_eem;
198
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200199static struct usb_function_instance *fi_geth;
200static struct usb_function *f_geth;
201
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200202static struct usb_function_instance *fi_rndis;
203static struct usb_function *f_rndis;
204
David Brownell0391c822008-06-19 18:20:11 -0700205/*-------------------------------------------------------------------------*/
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
David Brownell0391c822008-06-19 18:20:11 -0700208 * 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).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200212static int rndis_do_config(struct usb_configuration *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200214 int status;
215
David Brownell0391c822008-06-19 18:20:11 -0700216 /* FIXME alloc iConfiguration string, set it in c->strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David Brownell0391c822008-06-19 18:20:11 -0700218 if (gadget_is_otg(c->cdev->gadget)) {
219 c->descriptors = otg_desc;
220 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200223 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
David Brownell0391c822008-06-19 18:20:11 -0700234static struct usb_configuration rndis_config_driver = {
235 .label = "RNDIS",
David Brownell0391c822008-06-19 18:20:11 -0700236 .bConfigurationValue = 2,
237 /* .iConfiguration = DYNAMIC */
238 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell0391c822008-06-19 18:20:11 -0700239};
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*-------------------------------------------------------------------------*/
242
Robert P. J. Dayc9188ad2009-12-28 06:31:08 -0500243#ifdef CONFIG_USB_ETH_EEM
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030244static bool use_eem = 1;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500245#else
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030246static bool use_eem;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500247#endif
248module_param(use_eem, bool, 0);
249MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
250
David Brownell0391c822008-06-19 18:20:11 -0700251/*
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500252 * We _always_ have an ECM, CDC Subset, or EEM configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200254static int eth_do_config(struct usb_configuration *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200256 int status = 0;
257
David Brownell0391c822008-06-19 18:20:11 -0700258 /* FIXME alloc iConfiguration string, set it in c->strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David Brownell0391c822008-06-19 18:20:11 -0700260 if (gadget_is_otg(c->cdev->gadget)) {
261 c->descriptors = otg_desc;
262 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200265 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)) {
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200276 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;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200285 } 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 }
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
David Brownell0391c822008-06-19 18:20:11 -0700299static struct usb_configuration eth_config_driver = {
300 /* .label = f(hardware) */
David Brownell0391c822008-06-19 18:20:11 -0700301 .bConfigurationValue = 1,
302 /* .iConfiguration = DYNAMIC */
303 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell0391c822008-06-19 18:20:11 -0700304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*-------------------------------------------------------------------------*/
307
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200308static int eth_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
David Brownell0391c822008-06-19 18:20:11 -0700310 struct usb_gadget *gadget = cdev->gadget;
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200311 struct f_eem_opts *eem_opts = NULL;
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200312 struct f_ecm_opts *ecm_opts = NULL;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200313 struct f_gether_opts *geth_opts = NULL;
314 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int status;
David Brownell7e27f182006-06-13 09:54:40 -0700316
David Brownell0391c822008-06-19 18:20:11 -0700317 /* set up main config label and device descriptor */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500318 if (use_eem) {
319 /* EEM */
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200320 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
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200326 net = eem_opts->net;
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200327
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500328 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);
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200331 } else if (can_support_ecm(gadget)) {
David Brownell0391c822008-06-19 18:20:11 -0700332 /* ECM */
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200333
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
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200340 net = ecm_opts->net;
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200341
David Brownell0391c822008-06-19 18:20:11 -0700342 eth_config_driver.label = "CDC Ethernet (ECM)";
343 } else {
344 /* CDC Subset */
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200345
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;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200354
David Brownell0391c822008-06-19 18:20:11 -0700355 eth_config_driver.label = "CDC Subset/SAFE";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Brownell4e19f222009-06-19 03:09:04 -0700357 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200363 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
David Brownell0391c822008-06-19 18:20:11 -0700369 if (has_rndis()) {
370 /* RNDIS plus ECM-or-Subset */
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200371 gether_set_gadget(net, cdev->gadget);
372 status = gether_register_netdev(net);
373 if (status)
374 goto fail;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200375
376 if (use_eem)
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200377 eem_opts->bound = true;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200378 else if (can_support_ecm(gadget))
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200379 ecm_opts->bound = true;
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200380 else
381 geth_opts->bound = true;
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200382
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200383 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
David Brownell4e19f222009-06-19 03:09:04 -0700391 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
392 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
David Brownell0391c822008-06-19 18:20:11 -0700393 device_desc.bNumConfigurations = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
David Brownell0391c822008-06-19 18:20:11 -0700396 /* Allocate string descriptor numbers ... note that string
397 * contents can be overridden by the composite_dev glue.
398 */
399
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200400 status = usb_string_ids_tab(cdev, strings_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (status < 0)
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200402 goto fail1;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200403 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
404 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Li Jun9b95236e2015-07-09 15:18:49 +0800406 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
David Brownell0391c822008-06-19 18:20:11 -0700417 /* register our configuration(s); RNDIS first, if it's used */
418 if (has_rndis()) {
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200419 status = usb_add_config(cdev, &rndis_config_driver,
420 rndis_do_config);
David Brownell0391c822008-06-19 18:20:11 -0700421 if (status < 0)
Li Jun9b95236e2015-07-09 15:18:49 +0800422 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200425 status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
David Brownell0391c822008-06-19 18:20:11 -0700426 if (status < 0)
Li Jun9b95236e2015-07-09 15:18:49 +0800427 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200429 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell33376c12008-08-18 17:45:07 -0700430 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
431 DRIVER_DESC);
David Brownell0391c822008-06-19 18:20:11 -0700432
433 return 0;
434
Li Jun9b95236e2015-07-09 15:18:49 +0800435fail2:
436 kfree(otg_desc[0]);
437 otg_desc[0] = NULL;
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200438fail1:
439 if (has_rndis())
440 usb_put_function_instance(fi_rndis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441fail:
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200442 if (use_eem)
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200443 usb_put_function_instance(fi_eem);
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200444 else if (can_support_ecm(gadget))
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200445 usb_put_function_instance(fi_ecm);
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200446 else
447 usb_put_function_instance(fi_geth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return status;
449}
450
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200451static int eth_unbind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Andrzej Pietrasiewicz23a113a2013-07-24 12:47:46 +0200453 if (has_rndis()) {
454 usb_put_function(f_rndis);
Andrzej Pietrasiewicz9bd4a102013-05-28 09:15:58 +0200455 usb_put_function_instance(fi_rndis);
Andrzej Pietrasiewicz23a113a2013-07-24 12:47:46 +0200456 }
457 if (use_eem) {
458 usb_put_function(f_eem);
Andrzej Pietrasiewicz94b55732013-05-28 09:15:48 +0200459 usb_put_function_instance(fi_eem);
Andrzej Pietrasiewicz23a113a2013-07-24 12:47:46 +0200460 } else if (can_support_ecm(cdev->gadget)) {
461 usb_put_function(f_ecm);
Andrzej Pietrasiewicz9c62ce82013-05-28 09:15:46 +0200462 usb_put_function_instance(fi_ecm);
Andrzej Pietrasiewicz23a113a2013-07-24 12:47:46 +0200463 } else {
464 usb_put_function(f_geth);
Andrzej Pietrasiewicz8af52322013-05-28 09:15:54 +0200465 usb_put_function_instance(fi_geth);
Andrzej Pietrasiewicz23a113a2013-07-24 12:47:46 +0200466 }
Li Jun9b95236e2015-07-09 15:18:49 +0800467 kfree(otg_desc[0]);
468 otg_desc[0] = NULL;
469
David Brownell0391c822008-06-19 18:20:11 -0700470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200473static struct usb_composite_driver eth_driver = {
David Brownell0391c822008-06-19 18:20:11 -0700474 .name = "g_ether",
475 .dev = &device_desc,
476 .strings = dev_strings,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700477 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200478 .bind = eth_bind,
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200479 .unbind = eth_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480};
481
Tobias Klauser909346a2014-07-09 18:09:56 +0200482module_usb_composite_driver(eth_driver);
483
David Brownell0391c822008-06-19 18:20:11 -0700484MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
485MODULE_AUTHOR("David Brownell, Benedikt Spanger");
486MODULE_LICENSE("GPL");