1 // SPDX-License-Identifier: GPL-2.0+
3 * multi.c -- Multifunction Composite driver
5 * Copyright (C) 2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (mina86@mina86.com)
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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
22 #if defined USB_ETH_RNDIS
25 #ifdef CONFIG_USB_G_MULTI_RNDIS
26 # define USB_ETH_RNDIS y
30 #define DRIVER_DESC "Multifunction Composite Gadget"
32 MODULE_DESCRIPTION(DRIVER_DESC);
33 MODULE_AUTHOR("Michal Nazarewicz");
34 MODULE_LICENSE("GPL");
37 #include "f_mass_storage.h"
46 USB_GADGET_COMPOSITE_OPTIONS();
48 USB_ETHERNET_MODULE_PARAMETERS();
50 /***************************** Device Descriptor ****************************/
52 #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
53 #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
58 #ifdef CONFIG_USB_G_MULTI_RNDIS
59 MULTI_RNDIS_CONFIG_NUM,
61 #ifdef CONFIG_USB_G_MULTI_CDC
67 static struct usb_device_descriptor device_desc = {
68 .bLength = sizeof device_desc,
69 .bDescriptorType = USB_DT_DEVICE,
71 /* .bcdUSB = DYNAMIC */
73 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
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),
82 static const struct usb_descriptor_header *otg_desc[2];
85 MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
86 MULTI_STRING_CDC_CONFIG_IDX,
89 static struct usb_string strings_dev[] = {
90 [USB_GADGET_MANUFACTURER_IDX].s = "",
91 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
92 [USB_GADGET_SERIAL_IDX].s = "",
93 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
94 [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
98 static struct usb_gadget_strings *dev_strings[] = {
99 &(struct usb_gadget_strings){
100 .language = 0x0409, /* en-us */
101 .strings = strings_dev,
109 /****************************** Configurations ******************************/
111 static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
112 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
114 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
119 * Number of buffers we will use.
120 * 2 is usually enough for good buffering pipeline
122 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
124 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
126 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
128 static struct usb_function_instance *fi_acm;
129 static struct usb_function_instance *fi_msg;
131 /********** RNDIS **********/
134 static struct usb_function_instance *fi_rndis;
135 static struct usb_function *f_acm_rndis;
136 static struct usb_function *f_rndis;
137 static struct usb_function *f_msg_rndis;
139 static int rndis_do_config(struct usb_configuration *c)
143 if (gadget_is_otg(c->cdev->gadget)) {
144 c->descriptors = otg_desc;
145 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
148 f_rndis = usb_get_function(fi_rndis);
150 return PTR_ERR(f_rndis);
152 ret = usb_add_function(c, f_rndis);
156 f_acm_rndis = usb_get_function(fi_acm);
157 if (IS_ERR(f_acm_rndis)) {
158 ret = PTR_ERR(f_acm_rndis);
162 ret = usb_add_function(c, f_acm_rndis);
166 f_msg_rndis = usb_get_function(fi_msg);
167 if (IS_ERR(f_msg_rndis)) {
168 ret = PTR_ERR(f_msg_rndis);
172 ret = usb_add_function(c, f_msg_rndis);
178 usb_put_function(f_msg_rndis);
180 usb_remove_function(c, f_acm_rndis);
182 usb_put_function(f_acm_rndis);
184 usb_remove_function(c, f_rndis);
186 usb_put_function(f_rndis);
190 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
192 static struct usb_configuration config = {
193 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
194 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
197 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
198 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
200 return usb_add_config(cdev, &config, rndis_do_config);
205 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
213 /********** CDC ECM **********/
215 #ifdef CONFIG_USB_G_MULTI_CDC
216 static struct usb_function_instance *fi_ecm;
217 static struct usb_function *f_acm_multi;
218 static struct usb_function *f_ecm;
219 static struct usb_function *f_msg_multi;
221 static int cdc_do_config(struct usb_configuration *c)
225 if (gadget_is_otg(c->cdev->gadget)) {
226 c->descriptors = otg_desc;
227 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
230 f_ecm = usb_get_function(fi_ecm);
232 return PTR_ERR(f_ecm);
234 ret = usb_add_function(c, f_ecm);
238 /* implicit port_num is zero */
239 f_acm_multi = usb_get_function(fi_acm);
240 if (IS_ERR(f_acm_multi)) {
241 ret = PTR_ERR(f_acm_multi);
245 ret = usb_add_function(c, f_acm_multi);
249 f_msg_multi = usb_get_function(fi_msg);
250 if (IS_ERR(f_msg_multi)) {
251 ret = PTR_ERR(f_msg_multi);
255 ret = usb_add_function(c, f_msg_multi);
261 usb_put_function(f_msg_multi);
263 usb_remove_function(c, f_acm_multi);
265 usb_put_function(f_acm_multi);
267 usb_remove_function(c, f_ecm);
269 usb_put_function(f_ecm);
273 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
275 static struct usb_configuration config = {
276 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
277 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
280 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
281 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
283 return usb_add_config(cdev, &config, cdc_do_config);
288 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
297 /****************************** Gadget Bind ******************************/
299 static int __ref multi_bind(struct usb_composite_dev *cdev)
301 struct usb_gadget *gadget = cdev->gadget;
302 #ifdef CONFIG_USB_G_MULTI_CDC
303 struct f_ecm_opts *ecm_opts;
306 struct f_rndis_opts *rndis_opts;
308 struct fsg_opts *fsg_opts;
309 struct fsg_config config;
312 if (!can_support_ecm(cdev->gadget)) {
313 dev_err(&gadget->dev, "controller '%s' not usable\n",
318 #ifdef CONFIG_USB_G_MULTI_CDC
319 fi_ecm = usb_get_function_instance("ecm");
321 return PTR_ERR(fi_ecm);
323 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
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);
333 fi_rndis = usb_get_function_instance("rndis");
334 if (IS_ERR(fi_rndis)) {
335 status = PTR_ERR(fi_rndis);
339 rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
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);
348 #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
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.
355 gether_set_gadget(ecm_opts->net, cdev->gadget);
356 status = gether_register_netdev(ecm_opts->net);
360 rndis_borrow_net(fi_rndis, ecm_opts->net);
361 ecm_opts->bound = true;
364 /* set up serial link layer */
365 fi_acm = usb_get_function_instance("acm");
366 if (IS_ERR(fi_acm)) {
367 status = PTR_ERR(fi_acm);
371 /* set up mass storage function */
372 fi_msg = usb_get_function_instance("mass_storage");
373 if (IS_ERR(fi_msg)) {
374 status = PTR_ERR(fi_msg);
377 fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
378 fsg_opts = fsg_opts_from_func_inst(fi_msg);
380 fsg_opts->no_configfs = true;
381 status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
385 status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
389 fsg_common_set_sysfs(fsg_opts->common, true);
390 status = fsg_common_create_luns(fsg_opts->common, &config);
394 fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
395 config.product_name);
397 /* allocate string IDs */
398 status = usb_string_ids_tab(cdev, strings_dev);
399 if (unlikely(status < 0))
400 goto fail_string_ids;
401 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
403 if (gadget_is_otg(gadget) && !otg_desc[0]) {
404 struct usb_descriptor_header *usb_desc;
406 usb_desc = usb_otg_descriptor_alloc(gadget);
408 goto fail_string_ids;
409 usb_otg_descriptor_init(gadget, usb_desc);
410 otg_desc[0] = usb_desc;
414 /* register configurations */
415 status = rndis_config_register(cdev);
416 if (unlikely(status < 0))
419 status = cdc_config_register(cdev);
420 if (unlikely(status < 0))
422 usb_composite_overwrite_options(cdev, &coverwrite);
425 dev_info(&gadget->dev, DRIVER_DESC "\n");
434 fsg_common_remove_luns(fsg_opts->common);
436 fsg_common_free_buffers(fsg_opts->common);
438 usb_put_function_instance(fi_msg);
440 usb_put_function_instance(fi_acm);
443 usb_put_function_instance(fi_rndis);
446 #ifdef CONFIG_USB_G_MULTI_CDC
447 usb_put_function_instance(fi_ecm);
452 static int multi_unbind(struct usb_composite_dev *cdev)
454 #ifdef CONFIG_USB_G_MULTI_CDC
455 usb_put_function(f_msg_multi);
458 usb_put_function(f_msg_rndis);
460 usb_put_function_instance(fi_msg);
461 #ifdef CONFIG_USB_G_MULTI_CDC
462 usb_put_function(f_acm_multi);
465 usb_put_function(f_acm_rndis);
467 usb_put_function_instance(fi_acm);
469 usb_put_function(f_rndis);
470 usb_put_function_instance(fi_rndis);
472 #ifdef CONFIG_USB_G_MULTI_CDC
473 usb_put_function(f_ecm);
474 usb_put_function_instance(fi_ecm);
483 /****************************** Some noise ******************************/
486 static struct usb_composite_driver multi_driver = {
489 .strings = dev_strings,
490 .max_speed = USB_SPEED_HIGH,
492 .unbind = multi_unbind,
496 module_usb_composite_driver(multi_driver);