Merge branch 'pm-cpufreq'
[linux-2.6-block.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
70 .bcdUSB = cpu_to_le16(0x0200),
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 139{
1bcce939 140 struct fsg_opts *fsg_opts;
f176a5d8
MN
141 int ret;
142
143 if (gadget_is_otg(c->cdev->gadget)) {
144 c->descriptors = otg_desc;
145 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
146 }
147
4d1a8f68
AP
148 f_rndis = usb_get_function(fi_rndis);
149 if (IS_ERR(f_rndis))
150 return PTR_ERR(f_rndis);
151
152 ret = usb_add_function(c, f_rndis);
f176a5d8 153 if (ret < 0)
4d1a8f68 154 goto err_func_rndis;
f176a5d8 155
59835ad7 156 f_acm_rndis = usb_get_function(fi_acm);
4d1a8f68
AP
157 if (IS_ERR(f_acm_rndis)) {
158 ret = PTR_ERR(f_acm_rndis);
159 goto err_func_acm;
160 }
59835ad7
SAS
161
162 ret = usb_add_function(c, f_acm_rndis);
163 if (ret)
164 goto err_conf;
f176a5d8 165
1bcce939
AP
166 f_msg_rndis = usb_get_function(fi_msg);
167 if (IS_ERR(f_msg_rndis)) {
168 ret = PTR_ERR(f_msg_rndis);
59835ad7 169 goto err_fsg;
1bcce939
AP
170 }
171
172 fsg_opts = fsg_opts_from_func_inst(fi_msg);
173 ret = fsg_common_run_thread(fsg_opts->common);
174 if (ret)
175 goto err_run;
176
177 ret = usb_add_function(c, f_msg_rndis);
178 if (ret)
179 goto err_run;
f176a5d8
MN
180
181 return 0;
1bcce939
AP
182err_run:
183 usb_put_function(f_msg_rndis);
59835ad7
SAS
184err_fsg:
185 usb_remove_function(c, f_acm_rndis);
186err_conf:
187 usb_put_function(f_acm_rndis);
4d1a8f68
AP
188err_func_acm:
189 usb_remove_function(c, f_rndis);
190err_func_rndis:
191 usb_put_function(f_rndis);
59835ad7 192 return ret;
f176a5d8
MN
193}
194
780cc0f3 195static __ref int rndis_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
196{
197 static struct usb_configuration config = {
279cc49a
MN
198 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
199 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
200 };
201
202 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
203 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
204
c9bfff9c 205 return usb_add_config(cdev, &config, rndis_do_config);
279cc49a
MN
206}
207
208#else
209
780cc0f3 210static __ref int rndis_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
211{
212 return 0;
213}
f176a5d8
MN
214
215#endif
216
279cc49a
MN
217
218/********** CDC ECM **********/
219
f176a5d8 220#ifdef CONFIG_USB_G_MULTI_CDC
73889015 221static struct usb_function_instance *fi_ecm;
59835ad7 222static struct usb_function *f_acm_multi;
73889015 223static struct usb_function *f_ecm;
1bcce939 224static struct usb_function *f_msg_multi;
f176a5d8 225
c94e289f 226static int cdc_do_config(struct usb_configuration *c)
f176a5d8 227{
1bcce939 228 struct fsg_opts *fsg_opts;
f176a5d8
MN
229 int ret;
230
231 if (gadget_is_otg(c->cdev->gadget)) {
232 c->descriptors = otg_desc;
233 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
234 }
235
73889015
AP
236 f_ecm = usb_get_function(fi_ecm);
237 if (IS_ERR(f_ecm))
238 return PTR_ERR(f_ecm);
239
240 ret = usb_add_function(c, f_ecm);
f176a5d8 241 if (ret < 0)
73889015 242 goto err_func_ecm;
f176a5d8 243
59835ad7
SAS
244 /* implicit port_num is zero */
245 f_acm_multi = usb_get_function(fi_acm);
73889015
AP
246 if (IS_ERR(f_acm_multi)) {
247 ret = PTR_ERR(f_acm_multi);
248 goto err_func_acm;
249 }
59835ad7
SAS
250
251 ret = usb_add_function(c, f_acm_multi);
252 if (ret)
253 goto err_conf;
f176a5d8 254
1bcce939
AP
255 f_msg_multi = usb_get_function(fi_msg);
256 if (IS_ERR(f_msg_multi)) {
257 ret = PTR_ERR(f_msg_multi);
59835ad7 258 goto err_fsg;
1bcce939
AP
259 }
260
261 fsg_opts = fsg_opts_from_func_inst(fi_msg);
262 ret = fsg_common_run_thread(fsg_opts->common);
263 if (ret)
264 goto err_run;
265
266 ret = usb_add_function(c, f_msg_multi);
267 if (ret)
268 goto err_run;
f176a5d8
MN
269
270 return 0;
1bcce939
AP
271err_run:
272 usb_put_function(f_msg_multi);
59835ad7
SAS
273err_fsg:
274 usb_remove_function(c, f_acm_multi);
275err_conf:
276 usb_put_function(f_acm_multi);
73889015
AP
277err_func_acm:
278 usb_remove_function(c, f_ecm);
279err_func_ecm:
280 usb_put_function(f_ecm);
59835ad7 281 return ret;
f176a5d8
MN
282}
283
780cc0f3 284static __ref int cdc_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
285{
286 static struct usb_configuration config = {
279cc49a
MN
287 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
288 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
289 };
290
291 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
292 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
293
c9bfff9c 294 return usb_add_config(cdev, &config, cdc_do_config);
279cc49a
MN
295}
296
297#else
298
780cc0f3 299static __ref int cdc_config_register(struct usb_composite_dev *cdev)
279cc49a
MN
300{
301 return 0;
302}
f176a5d8
MN
303
304#endif
305
306
307
308/****************************** Gadget Bind ******************************/
309
279cc49a 310static int __ref multi_bind(struct usb_composite_dev *cdev)
f176a5d8
MN
311{
312 struct usb_gadget *gadget = cdev->gadget;
73889015
AP
313#ifdef CONFIG_USB_G_MULTI_CDC
314 struct f_ecm_opts *ecm_opts;
4d1a8f68
AP
315#endif
316#ifdef USB_ETH_RNDIS
317 struct f_rndis_opts *rndis_opts;
73889015 318#endif
1bcce939
AP
319 struct fsg_opts *fsg_opts;
320 struct fsg_config config;
ed9cbda6 321 int status;
f176a5d8
MN
322
323 if (!can_support_ecm(cdev->gadget)) {
324 dev_err(&gadget->dev, "controller '%s' not usable\n",
4d1a8f68 325 gadget->name);
f176a5d8
MN
326 return -EINVAL;
327 }
328
73889015
AP
329#ifdef CONFIG_USB_G_MULTI_CDC
330 fi_ecm = usb_get_function_instance("ecm");
331 if (IS_ERR(fi_ecm))
332 return PTR_ERR(fi_ecm);
333
334 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
335
336 gether_set_qmult(ecm_opts->net, qmult);
337 if (!gether_set_host_addr(ecm_opts->net, host_addr))
338 pr_info("using host ethernet address: %s", host_addr);
339 if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
340 pr_info("using self ethernet address: %s", dev_addr);
4d1a8f68 341#endif
73889015 342
4d1a8f68
AP
343#ifdef USB_ETH_RNDIS
344 fi_rndis = usb_get_function_instance("rndis");
345 if (IS_ERR(fi_rndis)) {
346 status = PTR_ERR(fi_rndis);
347 goto fail;
348 }
73889015 349
4d1a8f68 350 rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
73889015 351
4d1a8f68
AP
352 gether_set_qmult(rndis_opts->net, qmult);
353 if (!gether_set_host_addr(rndis_opts->net, host_addr))
354 pr_info("using host ethernet address: %s", host_addr);
355 if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
356 pr_info("using self ethernet address: %s", dev_addr);
73889015
AP
357#endif
358
359#if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
4d1a8f68
AP
360 /*
361 * If both ecm and rndis are selected then:
362 * 1) rndis borrows the net interface from ecm
363 * 2) since the interface is shared it must not be bound
364 * twice - in ecm's _and_ rndis' binds, so do it here.
365 */
73889015
AP
366 gether_set_gadget(ecm_opts->net, cdev->gadget);
367 status = gether_register_netdev(ecm_opts->net);
368 if (status)
369 goto fail0;
73889015 370
4d1a8f68
AP
371 rndis_borrow_net(fi_rndis, ecm_opts->net);
372 ecm_opts->bound = true;
73889015 373#endif
f176a5d8
MN
374
375 /* set up serial link layer */
59835ad7
SAS
376 fi_acm = usb_get_function_instance("acm");
377 if (IS_ERR(fi_acm)) {
378 status = PTR_ERR(fi_acm);
c4ed4ac1 379 goto fail0;
59835ad7
SAS
380 }
381
f176a5d8 382 /* set up mass storage function */
1bcce939
AP
383 fi_msg = usb_get_function_instance("mass_storage");
384 if (IS_ERR(fi_msg)) {
385 status = PTR_ERR(fi_msg);
386 goto fail1;
f176a5d8 387 }
1bcce939
AP
388 fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
389 fsg_opts = fsg_opts_from_func_inst(fi_msg);
390
391 fsg_opts->no_configfs = true;
392 status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
393 if (status)
394 goto fail2;
395
1bcce939
AP
396 status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
397 if (status)
398 goto fail_set_cdev;
399
400 fsg_common_set_sysfs(fsg_opts->common, true);
401 status = fsg_common_create_luns(fsg_opts->common, &config);
402 if (status)
403 goto fail_set_cdev;
404
405 fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
406 config.product_name);
f176a5d8 407
a99d8a45 408 /* allocate string IDs */
279cc49a
MN
409 status = usb_string_ids_tab(cdev, strings_dev);
410 if (unlikely(status < 0))
1bcce939 411 goto fail_string_ids;
d33f74fc 412 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
f176a5d8 413
21ddc2a8
LJ
414 if (gadget_is_otg(gadget) && !otg_desc[0]) {
415 struct usb_descriptor_header *usb_desc;
416
417 usb_desc = usb_otg_descriptor_alloc(gadget);
418 if (!usb_desc)
419 goto fail_string_ids;
420 usb_otg_descriptor_init(gadget, usb_desc);
421 otg_desc[0] = usb_desc;
422 otg_desc[1] = NULL;
423 }
424
279cc49a
MN
425 /* register configurations */
426 status = rndis_config_register(cdev);
427 if (unlikely(status < 0))
21ddc2a8 428 goto fail_otg_desc;
f176a5d8 429
279cc49a
MN
430 status = cdc_config_register(cdev);
431 if (unlikely(status < 0))
21ddc2a8 432 goto fail_otg_desc;
7d16e8d3 433 usb_composite_overwrite_options(cdev, &coverwrite);
f176a5d8 434
279cc49a
MN
435 /* we're done */
436 dev_info(&gadget->dev, DRIVER_DESC "\n");
f176a5d8
MN
437 return 0;
438
279cc49a
MN
439
440 /* error recovery */
21ddc2a8
LJ
441fail_otg_desc:
442 kfree(otg_desc[0]);
443 otg_desc[0] = NULL;
1bcce939
AP
444fail_string_ids:
445 fsg_common_remove_luns(fsg_opts->common);
446fail_set_cdev:
1bcce939 447 fsg_common_free_buffers(fsg_opts->common);
f176a5d8 448fail2:
1bcce939 449 usb_put_function_instance(fi_msg);
f176a5d8 450fail1:
59835ad7 451 usb_put_function_instance(fi_acm);
f176a5d8 452fail0:
4d1a8f68
AP
453#ifdef USB_ETH_RNDIS
454 usb_put_function_instance(fi_rndis);
455fail:
456#endif
73889015
AP
457#ifdef CONFIG_USB_G_MULTI_CDC
458 usb_put_function_instance(fi_ecm);
73889015 459#endif
f176a5d8
MN
460 return status;
461}
462
c94e289f 463static int multi_unbind(struct usb_composite_dev *cdev)
f176a5d8 464{
1bcce939
AP
465#ifdef CONFIG_USB_G_MULTI_CDC
466 usb_put_function(f_msg_multi);
467#endif
468#ifdef USB_ETH_RNDIS
469 usb_put_function(f_msg_rndis);
470#endif
471 usb_put_function_instance(fi_msg);
59835ad7
SAS
472#ifdef CONFIG_USB_G_MULTI_CDC
473 usb_put_function(f_acm_multi);
474#endif
475#ifdef USB_ETH_RNDIS
476 usb_put_function(f_acm_rndis);
477#endif
478 usb_put_function_instance(fi_acm);
4d1a8f68
AP
479#ifdef USB_ETH_RNDIS
480 usb_put_function(f_rndis);
481 usb_put_function_instance(fi_rndis);
482#endif
73889015
AP
483#ifdef CONFIG_USB_G_MULTI_CDC
484 usb_put_function(f_ecm);
485 usb_put_function_instance(fi_ecm);
73889015 486#endif
21ddc2a8
LJ
487 kfree(otg_desc[0]);
488 otg_desc[0] = NULL;
489
f176a5d8
MN
490 return 0;
491}
492
493
494/****************************** Some noise ******************************/
495
496
c94e289f 497static struct usb_composite_driver multi_driver = {
f176a5d8
MN
498 .name = "g_multi",
499 .dev = &device_desc,
500 .strings = dev_strings,
35a0e0bf 501 .max_speed = USB_SPEED_HIGH,
03e42bd5 502 .bind = multi_bind,
c94e289f 503 .unbind = multi_unbind,
a99d8a45 504 .needs_serial = 1,
f176a5d8
MN
505};
506
909346a8 507module_usb_composite_driver(multi_driver);