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)
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
17 #if defined USB_ETH_RNDIS
20 #ifdef CONFIG_USB_G_MULTI_RNDIS
21 # define USB_ETH_RNDIS y
25 #define DRIVER_DESC "Multifunction Composite Gadget"
27 MODULE_DESCRIPTION(DRIVER_DESC
);
28 MODULE_AUTHOR("Michal Nazarewicz");
29 MODULE_LICENSE("GPL");
32 #include "f_mass_storage.h"
41 USB_GADGET_COMPOSITE_OPTIONS();
43 USB_ETHERNET_MODULE_PARAMETERS();
45 /***************************** Device Descriptor ****************************/
47 #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
48 #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
53 #ifdef CONFIG_USB_G_MULTI_RNDIS
54 MULTI_RNDIS_CONFIG_NUM
,
56 #ifdef CONFIG_USB_G_MULTI_CDC
62 static struct usb_device_descriptor device_desc
= {
63 .bLength
= sizeof device_desc
,
64 .bDescriptorType
= USB_DT_DEVICE
,
66 /* .bcdUSB = DYNAMIC */
68 .bDeviceClass
= USB_CLASS_MISC
/* 0xEF */,
72 /* Vendor and product id can be overridden by module parameters. */
73 .idVendor
= cpu_to_le16(MULTI_VENDOR_NUM
),
74 .idProduct
= cpu_to_le16(MULTI_PRODUCT_NUM
),
77 static const struct usb_descriptor_header
*otg_desc
[2];
80 MULTI_STRING_RNDIS_CONFIG_IDX
= USB_GADGET_FIRST_AVAIL_IDX
,
81 MULTI_STRING_CDC_CONFIG_IDX
,
84 static struct usb_string strings_dev
[] = {
85 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
86 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
87 [USB_GADGET_SERIAL_IDX
].s
= "",
88 [MULTI_STRING_RNDIS_CONFIG_IDX
].s
= "Multifunction with RNDIS",
89 [MULTI_STRING_CDC_CONFIG_IDX
].s
= "Multifunction with CDC ECM",
93 static struct usb_gadget_strings
*dev_strings
[] = {
94 &(struct usb_gadget_strings
){
95 .language
= 0x0409, /* en-us */
96 .strings
= strings_dev
,
104 /****************************** Configurations ******************************/
106 static struct fsg_module_parameters fsg_mod_data
= { .stall
= 1 };
107 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
109 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
114 * Number of buffers we will use.
115 * 2 is usually enough for good buffering pipeline
117 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
119 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
121 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data
);
123 static struct usb_function_instance
*fi_acm
;
124 static struct usb_function_instance
*fi_msg
;
126 /********** RNDIS **********/
129 static struct usb_function_instance
*fi_rndis
;
130 static struct usb_function
*f_acm_rndis
;
131 static struct usb_function
*f_rndis
;
132 static struct usb_function
*f_msg_rndis
;
134 static int rndis_do_config(struct usb_configuration
*c
)
138 if (gadget_is_otg(c
->cdev
->gadget
)) {
139 c
->descriptors
= otg_desc
;
140 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
143 f_rndis
= usb_get_function(fi_rndis
);
145 return PTR_ERR(f_rndis
);
147 ret
= usb_add_function(c
, f_rndis
);
151 f_acm_rndis
= usb_get_function(fi_acm
);
152 if (IS_ERR(f_acm_rndis
)) {
153 ret
= PTR_ERR(f_acm_rndis
);
157 ret
= usb_add_function(c
, f_acm_rndis
);
161 f_msg_rndis
= usb_get_function(fi_msg
);
162 if (IS_ERR(f_msg_rndis
)) {
163 ret
= PTR_ERR(f_msg_rndis
);
167 ret
= usb_add_function(c
, f_msg_rndis
);
173 usb_put_function(f_msg_rndis
);
175 usb_remove_function(c
, f_acm_rndis
);
177 usb_put_function(f_acm_rndis
);
179 usb_remove_function(c
, f_rndis
);
181 usb_put_function(f_rndis
);
185 static __ref
int rndis_config_register(struct usb_composite_dev
*cdev
)
187 static struct usb_configuration config
= {
188 .bConfigurationValue
= MULTI_RNDIS_CONFIG_NUM
,
189 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
192 config
.label
= strings_dev
[MULTI_STRING_RNDIS_CONFIG_IDX
].s
;
193 config
.iConfiguration
= strings_dev
[MULTI_STRING_RNDIS_CONFIG_IDX
].id
;
195 return usb_add_config(cdev
, &config
, rndis_do_config
);
200 static __ref
int rndis_config_register(struct usb_composite_dev
*cdev
)
208 /********** CDC ECM **********/
210 #ifdef CONFIG_USB_G_MULTI_CDC
211 static struct usb_function_instance
*fi_ecm
;
212 static struct usb_function
*f_acm_multi
;
213 static struct usb_function
*f_ecm
;
214 static struct usb_function
*f_msg_multi
;
216 static int cdc_do_config(struct usb_configuration
*c
)
220 if (gadget_is_otg(c
->cdev
->gadget
)) {
221 c
->descriptors
= otg_desc
;
222 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
225 f_ecm
= usb_get_function(fi_ecm
);
227 return PTR_ERR(f_ecm
);
229 ret
= usb_add_function(c
, f_ecm
);
233 /* implicit port_num is zero */
234 f_acm_multi
= usb_get_function(fi_acm
);
235 if (IS_ERR(f_acm_multi
)) {
236 ret
= PTR_ERR(f_acm_multi
);
240 ret
= usb_add_function(c
, f_acm_multi
);
244 f_msg_multi
= usb_get_function(fi_msg
);
245 if (IS_ERR(f_msg_multi
)) {
246 ret
= PTR_ERR(f_msg_multi
);
250 ret
= usb_add_function(c
, f_msg_multi
);
256 usb_put_function(f_msg_multi
);
258 usb_remove_function(c
, f_acm_multi
);
260 usb_put_function(f_acm_multi
);
262 usb_remove_function(c
, f_ecm
);
264 usb_put_function(f_ecm
);
268 static __ref
int cdc_config_register(struct usb_composite_dev
*cdev
)
270 static struct usb_configuration config
= {
271 .bConfigurationValue
= MULTI_CDC_CONFIG_NUM
,
272 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
275 config
.label
= strings_dev
[MULTI_STRING_CDC_CONFIG_IDX
].s
;
276 config
.iConfiguration
= strings_dev
[MULTI_STRING_CDC_CONFIG_IDX
].id
;
278 return usb_add_config(cdev
, &config
, cdc_do_config
);
283 static __ref
int cdc_config_register(struct usb_composite_dev
*cdev
)
292 /****************************** Gadget Bind ******************************/
294 static int __ref
multi_bind(struct usb_composite_dev
*cdev
)
296 struct usb_gadget
*gadget
= cdev
->gadget
;
297 #ifdef CONFIG_USB_G_MULTI_CDC
298 struct f_ecm_opts
*ecm_opts
;
301 struct f_rndis_opts
*rndis_opts
;
303 struct fsg_opts
*fsg_opts
;
304 struct fsg_config config
;
307 if (!can_support_ecm(cdev
->gadget
)) {
308 dev_err(&gadget
->dev
, "controller '%s' not usable\n",
313 #ifdef CONFIG_USB_G_MULTI_CDC
314 fi_ecm
= usb_get_function_instance("ecm");
316 return PTR_ERR(fi_ecm
);
318 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
320 gether_set_qmult(ecm_opts
->net
, qmult
);
321 if (!gether_set_host_addr(ecm_opts
->net
, host_addr
))
322 pr_info("using host ethernet address: %s", host_addr
);
323 if (!gether_set_dev_addr(ecm_opts
->net
, dev_addr
))
324 pr_info("using self ethernet address: %s", dev_addr
);
328 fi_rndis
= usb_get_function_instance("rndis");
329 if (IS_ERR(fi_rndis
)) {
330 status
= PTR_ERR(fi_rndis
);
334 rndis_opts
= container_of(fi_rndis
, struct f_rndis_opts
, func_inst
);
336 gether_set_qmult(rndis_opts
->net
, qmult
);
337 if (!gether_set_host_addr(rndis_opts
->net
, host_addr
))
338 pr_info("using host ethernet address: %s", host_addr
);
339 if (!gether_set_dev_addr(rndis_opts
->net
, dev_addr
))
340 pr_info("using self ethernet address: %s", dev_addr
);
343 #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
345 * If both ecm and rndis are selected then:
346 * 1) rndis borrows the net interface from ecm
347 * 2) since the interface is shared it must not be bound
348 * twice - in ecm's _and_ rndis' binds, so do it here.
350 gether_set_gadget(ecm_opts
->net
, cdev
->gadget
);
351 status
= gether_register_netdev(ecm_opts
->net
);
355 rndis_borrow_net(fi_rndis
, ecm_opts
->net
);
356 ecm_opts
->bound
= true;
359 /* set up serial link layer */
360 fi_acm
= usb_get_function_instance("acm");
361 if (IS_ERR(fi_acm
)) {
362 status
= PTR_ERR(fi_acm
);
366 /* set up mass storage function */
367 fi_msg
= usb_get_function_instance("mass_storage");
368 if (IS_ERR(fi_msg
)) {
369 status
= PTR_ERR(fi_msg
);
372 fsg_config_from_params(&config
, &fsg_mod_data
, fsg_num_buffers
);
373 fsg_opts
= fsg_opts_from_func_inst(fi_msg
);
375 fsg_opts
->no_configfs
= true;
376 status
= fsg_common_set_num_buffers(fsg_opts
->common
, fsg_num_buffers
);
380 status
= fsg_common_set_cdev(fsg_opts
->common
, cdev
, config
.can_stall
);
384 fsg_common_set_sysfs(fsg_opts
->common
, true);
385 status
= fsg_common_create_luns(fsg_opts
->common
, &config
);
389 fsg_common_set_inquiry_string(fsg_opts
->common
, config
.vendor_name
,
390 config
.product_name
);
392 /* allocate string IDs */
393 status
= usb_string_ids_tab(cdev
, strings_dev
);
394 if (unlikely(status
< 0))
395 goto fail_string_ids
;
396 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
398 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
399 struct usb_descriptor_header
*usb_desc
;
401 usb_desc
= usb_otg_descriptor_alloc(gadget
);
403 goto fail_string_ids
;
404 usb_otg_descriptor_init(gadget
, usb_desc
);
405 otg_desc
[0] = usb_desc
;
409 /* register configurations */
410 status
= rndis_config_register(cdev
);
411 if (unlikely(status
< 0))
414 status
= cdc_config_register(cdev
);
415 if (unlikely(status
< 0))
417 usb_composite_overwrite_options(cdev
, &coverwrite
);
420 dev_info(&gadget
->dev
, DRIVER_DESC
"\n");
429 fsg_common_remove_luns(fsg_opts
->common
);
431 fsg_common_free_buffers(fsg_opts
->common
);
433 usb_put_function_instance(fi_msg
);
435 usb_put_function_instance(fi_acm
);
438 usb_put_function_instance(fi_rndis
);
441 #ifdef CONFIG_USB_G_MULTI_CDC
442 usb_put_function_instance(fi_ecm
);
447 static int multi_unbind(struct usb_composite_dev
*cdev
)
449 #ifdef CONFIG_USB_G_MULTI_CDC
450 usb_put_function(f_msg_multi
);
453 usb_put_function(f_msg_rndis
);
455 usb_put_function_instance(fi_msg
);
456 #ifdef CONFIG_USB_G_MULTI_CDC
457 usb_put_function(f_acm_multi
);
460 usb_put_function(f_acm_rndis
);
462 usb_put_function_instance(fi_acm
);
464 usb_put_function(f_rndis
);
465 usb_put_function_instance(fi_rndis
);
467 #ifdef CONFIG_USB_G_MULTI_CDC
468 usb_put_function(f_ecm
);
469 usb_put_function_instance(fi_ecm
);
478 /****************************** Some noise ******************************/
481 static struct usb_composite_driver multi_driver
= {
484 .strings
= dev_strings
,
485 .max_speed
= USB_SPEED_HIGH
,
487 .unbind
= multi_unbind
,
491 module_usb_composite_driver(multi_driver
);