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