Merge remote-tracking branch 'fuse/for-next'
[deliverable/linux.git] / drivers / usb / gadget / legacy / multi.c
CommitLineData
f176a5d8
MN
1/*
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
54b8360f 7 * Author: Michal Nazarewicz (mina86@mina86.com)
f176a5d8
MN
8 *
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.
f176a5d8
MN
13 */
14
15
16#include <linux/kernel.h>
279cc49a 17#include <linux/module.h>
73889015 18#include <linux/netdevice.h>
f176a5d8 19
59835ad7 20#include "u_serial.h"
396cda90
MN
21#if defined USB_ETH_RNDIS
22# undef USB_ETH_RNDIS
23#endif
dbe4a99d 24#ifdef CONFIG_USB_G_MULTI_RNDIS
396cda90 25# define USB_ETH_RNDIS y
f176a5d8
MN
26#endif
27
28
29#define DRIVER_DESC "Multifunction Composite Gadget"
f176a5d8 30
279cc49a
MN
31MODULE_DESCRIPTION(DRIVER_DESC);
32MODULE_AUTHOR("Michal Nazarewicz");
33MODULE_LICENSE("GPL");
f176a5d8 34
f176a5d8 35
1bcce939 36#include "f_mass_storage.h"
f176a5d8 37
73889015 38#include "u_ecm.h"
396cda90 39#ifdef USB_ETH_RNDIS
4d1a8f68 40# include "u_rndis.h"
cbbd14a9 41# include "rndis.h"
f176a5d8 42#endif
f1a1823f 43#include "u_ether.h"
f176a5d8 44
7d16e8d3 45USB_GADGET_COMPOSITE_OPTIONS();
279cc49a 46
f1a1823f
AP
47USB_ETHERNET_MODULE_PARAMETERS();
48
279cc49a
MN
49/***************************** Device Descriptor ****************************/
50
1c6529e9
MN
51#define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
52#define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
279cc49a
MN
53
54
55enum {
56 __MULTI_NO_CONFIG,
57#ifdef CONFIG_USB_G_MULTI_RNDIS
58 MULTI_RNDIS_CONFIG_NUM,
59#endif
60#ifdef CONFIG_USB_G_MULTI_CDC
61 MULTI_CDC_CONFIG_NUM,
62#endif
63};
64
f176a5d8
MN
65
66static struct usb_device_descriptor device_desc = {
67 .bLength = sizeof device_desc,
68 .bDescriptorType = USB_DT_DEVICE,
69
0aecfc1b 70 /* .bcdUSB = DYNAMIC */
f176a5d8 71
279cc49a 72 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
f176a5d8
MN
73 .bDeviceSubClass = 2,
74 .bDeviceProtocol = 1,
f176a5d8
MN
75
76 /* Vendor and product id can be overridden by module parameters. */
77 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
78 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
f176a5d8
MN
79};
80
21ddc2a8 81static const struct usb_descriptor_header *otg_desc[2];
f176a5d8 82
279cc49a 83enum {
276e2e4f 84 MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
279cc49a 85 MULTI_STRING_CDC_CONFIG_IDX,
279cc49a 86};
f176a5d8 87
f176a5d8 88static struct usb_string strings_dev[] = {
276e2e4f 89 [USB_GADGET_MANUFACTURER_IDX].s = "",
d33f74fc 90 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
276e2e4f 91 [USB_GADGET_SERIAL_IDX].s = "",
279cc49a 92 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
279cc49a 93 [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
f176a5d8
MN
94 { } /* end of list */
95};
96
f176a5d8 97static struct usb_gadget_strings *dev_strings[] = {
279cc49a
MN
98 &(struct usb_gadget_strings){
99 .language = 0x0409, /* en-us */
100 .strings = strings_dev,
101 },
f176a5d8
MN
102 NULL,
103};
104
f176a5d8
MN
105
106
107
108/****************************** Configurations ******************************/
109
279cc49a 110static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
6fdc5dd2
AP
111#ifdef CONFIG_USB_GADGET_DEBUG_FILES
112
113static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
114
115#else
116
117/*
118 * Number of buffers we will use.
119 * 2 is usually enough for good buffering pipeline
120 */
121#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
122
0c8b1992 123#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
6fdc5dd2 124
279cc49a
MN
125FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
126
59835ad7 127static struct usb_function_instance *fi_acm;
1bcce939 128static struct usb_function_instance *fi_msg;
f176a5d8 129
279cc49a 130/********** RNDIS **********/
f176a5d8 131
396cda90 132#ifdef USB_ETH_RNDIS
4d1a8f68 133static struct usb_function_instance *fi_rndis;
59835ad7 134static struct usb_function *f_acm_rndis;
4d1a8f68 135static struct usb_function *f_rndis;
1bcce939 136static struct usb_function *f_msg_rndis;
f176a5d8 137
c94e289f 138static int rndis_do_config(struct usb_configuration *c)
f176a5d8
MN
139{
140 int ret;
141
142 if (gadget_is_otg(c->cdev->gadget)) {
143 c->descriptors = otg_desc;
144 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
145 }
146
4d1a8f68
AP
147 f_rndis = usb_get_function(fi_rndis);
148 if (IS_ERR(f_rndis))
149 return PTR_ERR(f_rndis);
150
151 ret = usb_add_function(c, f_rndis);
f176a5d8 152 if (ret < 0)
4d1a8f68 153 goto err_func_rndis;
f176a5d8 154
59835ad7 155 f_acm_rndis = usb_get_function(fi_acm);
4d1a8f68
AP
156 if (IS_ERR(f_acm_rndis)) {
157 ret = PTR_ERR(f_acm_rndis);
158 goto err_func_acm;
159 }
59835ad7
SAS
160
161 ret = usb_add_function(c, f_acm_rndis);
162 if (ret)
163 goto err_conf;
f176a5d8 164
1bcce939
AP
165 f_msg_rndis = usb_get_function(fi_msg);
166 if (IS_ERR(f_msg_rndis)) {
167 ret = PTR_ERR(f_msg_rndis);
59835ad7 168 goto err_fsg;
1bcce939
AP
169 }
170
1bcce939
AP
171 ret = usb_add_function(c, f_msg_rndis);
172 if (ret)
173 goto err_run;
f176a5d8
MN
174
175 return 0;
1bcce939
AP
176err_run:
177 usb_put_function(f_msg_rndis);
59835ad7
SAS
178err_fsg:
179 usb_remove_function(c, f_acm_rndis);
180err_conf:
181 usb_put_function(f_acm_rndis);
4d1a8f68
AP
182err_func_acm:
183 usb_remove_function(c, f_rndis);
184err_func_rndis:
185 usb_put_function(f_rndis);
59835ad7 186 return ret;
f176a5d8
MN
187}
188
780cc0f3 189static __ref int rndis_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
190{
191 static struct usb_configuration config = {
279cc49a
MN
192 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
193 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
194 };
195
196 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
197 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
198
c9bfff9c 199 return usb_add_config(cdev, &config, rndis_do_config);
279cc49a
MN
200}
201
202#else
203
780cc0f3 204static __ref int rndis_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
205{
206 return 0;
207}
f176a5d8
MN
208
209#endif
210
279cc49a
MN
211
212/********** CDC ECM **********/
213
f176a5d8 214#ifdef CONFIG_USB_G_MULTI_CDC
73889015 215static struct usb_function_instance *fi_ecm;
59835ad7 216static struct usb_function *f_acm_multi;
73889015 217static struct usb_function *f_ecm;
1bcce939 218static struct usb_function *f_msg_multi;
f176a5d8 219
c94e289f 220static int cdc_do_config(struct usb_configuration *c)
f176a5d8
MN
221{
222 int ret;
223
224 if (gadget_is_otg(c->cdev->gadget)) {
225 c->descriptors = otg_desc;
226 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
227 }
228
73889015
AP
229 f_ecm = usb_get_function(fi_ecm);
230 if (IS_ERR(f_ecm))
231 return PTR_ERR(f_ecm);
232
233 ret = usb_add_function(c, f_ecm);
f176a5d8 234 if (ret < 0)
73889015 235 goto err_func_ecm;
f176a5d8 236
59835ad7
SAS
237 /* implicit port_num is zero */
238 f_acm_multi = usb_get_function(fi_acm);
73889015
AP
239 if (IS_ERR(f_acm_multi)) {
240 ret = PTR_ERR(f_acm_multi);
241 goto err_func_acm;
242 }
59835ad7
SAS
243
244 ret = usb_add_function(c, f_acm_multi);
245 if (ret)
246 goto err_conf;
f176a5d8 247
1bcce939
AP
248 f_msg_multi = usb_get_function(fi_msg);
249 if (IS_ERR(f_msg_multi)) {
250 ret = PTR_ERR(f_msg_multi);
59835ad7 251 goto err_fsg;
1bcce939
AP
252 }
253
1bcce939
AP
254 ret = usb_add_function(c, f_msg_multi);
255 if (ret)
256 goto err_run;
f176a5d8
MN
257
258 return 0;
1bcce939
AP
259err_run:
260 usb_put_function(f_msg_multi);
59835ad7
SAS
261err_fsg:
262 usb_remove_function(c, f_acm_multi);
263err_conf:
264 usb_put_function(f_acm_multi);
73889015
AP
265err_func_acm:
266 usb_remove_function(c, f_ecm);
267err_func_ecm:
268 usb_put_function(f_ecm);
59835ad7 269 return ret;
f176a5d8
MN
270}
271
780cc0f3 272static __ref int cdc_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
273{
274 static struct usb_configuration config = {
279cc49a
MN
275 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
276 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
277 };
278
279 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
280 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
281
c9bfff9c 282 return usb_add_config(cdev, &config, cdc_do_config);
279cc49a
MN
283}
284
285#else
286
780cc0f3 287static __ref int cdc_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
288{
289 return 0;
290}
f176a5d8
MN
291
292#endif
293
294
295
296/****************************** Gadget Bind ******************************/
297
279cc49a 298static int __ref multi_bind(struct usb_composite_dev *cdev)
f176a5d8
MN
299{
300 struct usb_gadget *gadget = cdev->gadget;
73889015
AP
301#ifdef CONFIG_USB_G_MULTI_CDC
302 struct f_ecm_opts *ecm_opts;
4d1a8f68
AP
303#endif
304#ifdef USB_ETH_RNDIS
305 struct f_rndis_opts *rndis_opts;
73889015 306#endif
1bcce939
AP
307 struct fsg_opts *fsg_opts;
308 struct fsg_config config;
ed9cbda6 309 int status;
f176a5d8
MN
310
311 if (!can_support_ecm(cdev->gadget)) {
312 dev_err(&gadget->dev, "controller '%s' not usable\n",
4d1a8f68 313 gadget->name);
f176a5d8
MN
314 return -EINVAL;
315 }
316
73889015
AP
317#ifdef CONFIG_USB_G_MULTI_CDC
318 fi_ecm = usb_get_function_instance("ecm");
319 if (IS_ERR(fi_ecm))
320 return PTR_ERR(fi_ecm);
321
322 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
323
324 gether_set_qmult(ecm_opts->net, qmult);
325 if (!gether_set_host_addr(ecm_opts->net, host_addr))
326 pr_info("using host ethernet address: %s", host_addr);
327 if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
328 pr_info("using self ethernet address: %s", dev_addr);
4d1a8f68 329#endif
73889015 330
4d1a8f68
AP
331#ifdef USB_ETH_RNDIS
332 fi_rndis = usb_get_function_instance("rndis");
333 if (IS_ERR(fi_rndis)) {
334 status = PTR_ERR(fi_rndis);
335 goto fail;
336 }
73889015 337
4d1a8f68 338 rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
73889015 339
4d1a8f68
AP
340 gether_set_qmult(rndis_opts->net, qmult);
341 if (!gether_set_host_addr(rndis_opts->net, host_addr))
342 pr_info("using host ethernet address: %s", host_addr);
343 if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
344 pr_info("using self ethernet address: %s", dev_addr);
73889015
AP
345#endif
346
347#if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
4d1a8f68
AP
348 /*
349 * If both ecm and rndis are selected then:
350 * 1) rndis borrows the net interface from ecm
351 * 2) since the interface is shared it must not be bound
352 * twice - in ecm's _and_ rndis' binds, so do it here.
353 */
73889015
AP
354 gether_set_gadget(ecm_opts->net, cdev->gadget);
355 status = gether_register_netdev(ecm_opts->net);
356 if (status)
357 goto fail0;
73889015 358
4d1a8f68
AP
359 rndis_borrow_net(fi_rndis, ecm_opts->net);
360 ecm_opts->bound = true;
73889015 361#endif
f176a5d8
MN
362
363 /* set up serial link layer */
59835ad7
SAS
364 fi_acm = usb_get_function_instance("acm");
365 if (IS_ERR(fi_acm)) {
366 status = PTR_ERR(fi_acm);
c4ed4ac1 367 goto fail0;
59835ad7
SAS
368 }
369
f176a5d8 370 /* set up mass storage function */
1bcce939
AP
371 fi_msg = usb_get_function_instance("mass_storage");
372 if (IS_ERR(fi_msg)) {
373 status = PTR_ERR(fi_msg);
374 goto fail1;
f176a5d8 375 }
1bcce939
AP
376 fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
377 fsg_opts = fsg_opts_from_func_inst(fi_msg);
378
379 fsg_opts->no_configfs = true;
380 status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
381 if (status)
382 goto fail2;
383
1bcce939
AP
384 status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
385 if (status)
386 goto fail_set_cdev;
387
388 fsg_common_set_sysfs(fsg_opts->common, true);
389 status = fsg_common_create_luns(fsg_opts->common, &config);
390 if (status)
391 goto fail_set_cdev;
392
393 fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
394 config.product_name);
f176a5d8 395
a99d8a45 396 /* allocate string IDs */
279cc49a
MN
397 status = usb_string_ids_tab(cdev, strings_dev);
398 if (unlikely(status < 0))
1bcce939 399 goto fail_string_ids;
d33f74fc 400 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
f176a5d8 401
21ddc2a8
LJ
402 if (gadget_is_otg(gadget) && !otg_desc[0]) {
403 struct usb_descriptor_header *usb_desc;
404
405 usb_desc = usb_otg_descriptor_alloc(gadget);
406 if (!usb_desc)
407 goto fail_string_ids;
408 usb_otg_descriptor_init(gadget, usb_desc);
409 otg_desc[0] = usb_desc;
410 otg_desc[1] = NULL;
411 }
412
279cc49a
MN
413 /* register configurations */
414 status = rndis_config_register(cdev);
415 if (unlikely(status < 0))
21ddc2a8 416 goto fail_otg_desc;
f176a5d8 417
279cc49a
MN
418 status = cdc_config_register(cdev);
419 if (unlikely(status < 0))
21ddc2a8 420 goto fail_otg_desc;
7d16e8d3 421 usb_composite_overwrite_options(cdev, &coverwrite);
f176a5d8 422
279cc49a
MN
423 /* we're done */
424 dev_info(&gadget->dev, DRIVER_DESC "\n");
f176a5d8
MN
425 return 0;
426
279cc49a
MN
427
428 /* error recovery */
21ddc2a8
LJ
429fail_otg_desc:
430 kfree(otg_desc[0]);
431 otg_desc[0] = NULL;
1bcce939
AP
432fail_string_ids:
433 fsg_common_remove_luns(fsg_opts->common);
434fail_set_cdev:
1bcce939 435 fsg_common_free_buffers(fsg_opts->common);
f176a5d8 436fail2:
1bcce939 437 usb_put_function_instance(fi_msg);
f176a5d8 438fail1:
59835ad7 439 usb_put_function_instance(fi_acm);
f176a5d8 440fail0:
4d1a8f68
AP
441#ifdef USB_ETH_RNDIS
442 usb_put_function_instance(fi_rndis);
443fail:
444#endif
73889015
AP
445#ifdef CONFIG_USB_G_MULTI_CDC
446 usb_put_function_instance(fi_ecm);
73889015 447#endif
f176a5d8
MN
448 return status;
449}
450
c94e289f 451static int multi_unbind(struct usb_composite_dev *cdev)
f176a5d8 452{
1bcce939
AP
453#ifdef CONFIG_USB_G_MULTI_CDC
454 usb_put_function(f_msg_multi);
455#endif
456#ifdef USB_ETH_RNDIS
457 usb_put_function(f_msg_rndis);
458#endif
459 usb_put_function_instance(fi_msg);
59835ad7
SAS
460#ifdef CONFIG_USB_G_MULTI_CDC
461 usb_put_function(f_acm_multi);
462#endif
463#ifdef USB_ETH_RNDIS
464 usb_put_function(f_acm_rndis);
465#endif
466 usb_put_function_instance(fi_acm);
4d1a8f68
AP
467#ifdef USB_ETH_RNDIS
468 usb_put_function(f_rndis);
469 usb_put_function_instance(fi_rndis);
470#endif
73889015
AP
471#ifdef CONFIG_USB_G_MULTI_CDC
472 usb_put_function(f_ecm);
473 usb_put_function_instance(fi_ecm);
73889015 474#endif
21ddc2a8
LJ
475 kfree(otg_desc[0]);
476 otg_desc[0] = NULL;
477
f176a5d8
MN
478 return 0;
479}
480
481
482/****************************** Some noise ******************************/
483
484
c94e289f 485static struct usb_composite_driver multi_driver = {
f176a5d8
MN
486 .name = "g_multi",
487 .dev = &device_desc,
488 .strings = dev_strings,
35a0e0bf 489 .max_speed = USB_SPEED_HIGH,
03e42bd5 490 .bind = multi_bind,
c94e289f 491 .unbind = multi_unbind,
a99d8a45 492 .needs_serial = 1,
f176a5d8
MN
493};
494
909346a8 495module_usb_composite_driver(multi_driver);
This page took 0.448367 seconds and 5 git commands to generate.