blob: 94b35e5539d75739fe4e0bfa2c379cc42614a4c0 [file] [log] [blame]
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +01001/*
2 * multi.c -- Multifunction Composite driver
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01007 * Author: Michal Nazarewicz (mina86@mina86.com)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010013 */
14
15
16#include <linux/kernel.h>
17#include <linux/utsname.h>
Michal Nazarewicz279cc492010-06-21 13:57:03 +020018#include <linux/module.h>
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010019
20
Michal Nazarewicz396cda92009-11-30 10:55:40 +010021#if defined USB_ETH_RNDIS
22# undef USB_ETH_RNDIS
23#endif
Michal Nazarewiczdbe4a992010-01-22 15:18:21 +010024#ifdef CONFIG_USB_G_MULTI_RNDIS
Michal Nazarewicz396cda92009-11-30 10:55:40 +010025# define USB_ETH_RNDIS y
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010026#endif
27
28
29#define DRIVER_DESC "Multifunction Composite Gadget"
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010030
Michal Nazarewicz279cc492010-06-21 13:57:03 +020031MODULE_DESCRIPTION(DRIVER_DESC);
32MODULE_AUTHOR("Michal Nazarewicz");
33MODULE_LICENSE("GPL");
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010034
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010035
Michal Nazarewicz279cc492010-06-21 13:57:03 +020036/***************************** All the files... *****************************/
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010037
38/*
39 * kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 */
45
46#include "composite.c"
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010047
Michal Nazarewicz279cc492010-06-21 13:57:03 +020048#include "f_mass_storage.c"
49
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010050#include "u_serial.c"
51#include "f_acm.c"
52
53#include "f_ecm.c"
54#include "f_subset.c"
Michal Nazarewicz396cda92009-11-30 10:55:40 +010055#ifdef USB_ETH_RNDIS
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010056# include "f_rndis.c"
57# include "rndis.c"
58#endif
59#include "u_ether.c"
60
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020061USB_GADGET_COMPOSITE_OPTIONS();
Michal Nazarewicz279cc492010-06-21 13:57:03 +020062
63/***************************** Device Descriptor ****************************/
64
Michal Nazarewicz1c6529e2010-08-12 17:43:44 +020065#define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
66#define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
Michal Nazarewicz279cc492010-06-21 13:57:03 +020067
68
69enum {
70 __MULTI_NO_CONFIG,
71#ifdef CONFIG_USB_G_MULTI_RNDIS
72 MULTI_RNDIS_CONFIG_NUM,
73#endif
74#ifdef CONFIG_USB_G_MULTI_CDC
75 MULTI_CDC_CONFIG_NUM,
76#endif
77};
78
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010079
80static struct usb_device_descriptor device_desc = {
81 .bLength = sizeof device_desc,
82 .bDescriptorType = USB_DT_DEVICE,
83
84 .bcdUSB = cpu_to_le16(0x0200),
85
Michal Nazarewicz279cc492010-06-21 13:57:03 +020086 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010087 .bDeviceSubClass = 2,
88 .bDeviceProtocol = 1,
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010089
90 /* Vendor and product id can be overridden by module parameters. */
91 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
92 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010093};
94
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +010095
96static const struct usb_descriptor_header *otg_desc[] = {
Michal Nazarewicz279cc492010-06-21 13:57:03 +020097 (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
98 .bLength = sizeof(struct usb_otg_descriptor),
99 .bDescriptorType = USB_DT_OTG,
100
101 /*
102 * REVISIT SRP-only hardware is possible, although
103 * it would not be called "OTG" ...
104 */
105 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
106 },
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100107 NULL,
108};
109
110
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200111enum {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200112 MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200113 MULTI_STRING_CDC_CONFIG_IDX,
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200114};
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100115
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100116static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200117 [USB_GADGET_MANUFACTURER_IDX].s = "",
118 [USB_GADGET_PRODUCT_IDX].s = "",
119 [USB_GADGET_SERIAL_IDX].s = "",
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200120 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200121 [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100122 { } /* end of list */
123};
124
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100125static struct usb_gadget_strings *dev_strings[] = {
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200126 &(struct usb_gadget_strings){
127 .language = 0x0409, /* en-us */
128 .strings = strings_dev,
129 },
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100130 NULL,
131};
132
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100133
134
135
136/****************************** Configurations ******************************/
137
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200138static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
139FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100140
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200141static struct fsg_common fsg_common;
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100142
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200143static u8 hostaddr[ETH_ALEN];
144
145
146/********** RNDIS **********/
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100147
Michal Nazarewicz396cda92009-11-30 10:55:40 +0100148#ifdef USB_ETH_RNDIS
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100149
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200150static __init int rndis_do_config(struct usb_configuration *c)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100151{
152 int ret;
153
154 if (gadget_is_otg(c->cdev->gadget)) {
155 c->descriptors = otg_desc;
156 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
157 }
158
159 ret = rndis_bind_config(c, hostaddr);
160 if (ret < 0)
161 return ret;
162
163 ret = acm_bind_config(c, 0);
164 if (ret < 0)
165 return ret;
166
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200167 ret = fsg_bind_config(c->cdev, c, &fsg_common);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100168 if (ret < 0)
169 return ret;
170
171 return 0;
172}
173
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200174static int rndis_config_register(struct usb_composite_dev *cdev)
175{
176 static struct usb_configuration config = {
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200177 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
178 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
179 };
180
181 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
182 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
183
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200184 return usb_add_config(cdev, &config, rndis_do_config);
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200185}
186
187#else
188
189static int rndis_config_register(struct usb_composite_dev *cdev)
190{
191 return 0;
192}
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100193
194#endif
195
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200196
197/********** CDC ECM **********/
198
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100199#ifdef CONFIG_USB_G_MULTI_CDC
200
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200201static __init int cdc_do_config(struct usb_configuration *c)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100202{
203 int ret;
204
205 if (gadget_is_otg(c->cdev->gadget)) {
206 c->descriptors = otg_desc;
207 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
208 }
209
210 ret = ecm_bind_config(c, hostaddr);
211 if (ret < 0)
212 return ret;
213
214 ret = acm_bind_config(c, 0);
215 if (ret < 0)
216 return ret;
217
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200218 ret = fsg_bind_config(c->cdev, c, &fsg_common);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100219 if (ret < 0)
220 return ret;
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100221
222 return 0;
223}
224
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200225static int cdc_config_register(struct usb_composite_dev *cdev)
226{
227 static struct usb_configuration config = {
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200228 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
229 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
230 };
231
232 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
233 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
234
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200235 return usb_add_config(cdev, &config, cdc_do_config);
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200236}
237
238#else
239
240static int cdc_config_register(struct usb_composite_dev *cdev)
241{
242 return 0;
243}
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100244
245#endif
246
247
248
249/****************************** Gadget Bind ******************************/
250
251
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200252static int __ref multi_bind(struct usb_composite_dev *cdev)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100253{
254 struct usb_gadget *gadget = cdev->gadget;
255 int status, gcnum;
256
257 if (!can_support_ecm(cdev->gadget)) {
258 dev_err(&gadget->dev, "controller '%s' not usable\n",
259 gadget->name);
260 return -EINVAL;
261 }
262
263 /* set up network link layer */
264 status = gether_setup(cdev->gadget, hostaddr);
265 if (status < 0)
266 return status;
267
268 /* set up serial link layer */
269 status = gserial_setup(cdev->gadget, 1);
270 if (status < 0)
271 goto fail0;
272
273 /* set up mass storage function */
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200274 {
275 void *retp;
276 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
277 if (IS_ERR(retp)) {
278 status = PTR_ERR(retp);
279 goto fail1;
280 }
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100281 }
282
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200283 /* set bcdDevice */
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100284 gcnum = usb_gadget_controller_number(gadget);
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200285 if (gcnum >= 0) {
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100286 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200287 } else {
288 WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100289 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
290 }
291
Michal Nazarewicza99d8a42010-08-12 17:43:49 +0200292 /* allocate string IDs */
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200293 status = usb_string_ids_tab(cdev, strings_dev);
294 if (unlikely(status < 0))
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100295 goto fail2;
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100296
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200297 /* register configurations */
298 status = rndis_config_register(cdev);
299 if (unlikely(status < 0))
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100300 goto fail2;
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100301
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200302 status = cdc_config_register(cdev);
303 if (unlikely(status < 0))
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100304 goto fail2;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200305 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100306
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200307 /* we're done */
308 dev_info(&gadget->dev, DRIVER_DESC "\n");
309 fsg_common_put(&fsg_common);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100310 return 0;
311
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200312
313 /* error recovery */
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100314fail2:
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200315 fsg_common_put(&fsg_common);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100316fail1:
317 gserial_cleanup();
318fail0:
319 gether_cleanup();
320 return status;
321}
322
323static int __exit multi_unbind(struct usb_composite_dev *cdev)
324{
325 gserial_cleanup();
326 gether_cleanup();
327 return 0;
328}
329
330
331/****************************** Some noise ******************************/
332
333
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200334static __refdata struct usb_composite_driver multi_driver = {
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100335 .name = "g_multi",
336 .dev = &device_desc,
337 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300338 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200339 .bind = multi_bind,
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100340 .unbind = __exit_p(multi_unbind),
Michal Nazarewicza99d8a42010-08-12 17:43:49 +0200341 .iProduct = DRIVER_DESC,
342 .needs_serial = 1,
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100343};
344
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100345
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200346static int __init multi_init(void)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100347{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200348 return usb_composite_probe(&multi_driver);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100349}
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200350module_init(multi_init);
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100351
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200352static void __exit multi_exit(void)
Michal Nazarewiczf176a5d2009-11-09 14:15:27 +0100353{
354 usb_composite_unregister(&multi_driver);
355}
Michal Nazarewicz279cc492010-06-21 13:57:03 +0200356module_exit(multi_exit);