2 * multi.c -- Multifunction Composite driver
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz (mina86@mina86.com)
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
20 #if defined USB_ETH_RNDIS
23 #ifdef CONFIG_USB_G_MULTI_RNDIS
24 # define USB_ETH_RNDIS y
28 #define DRIVER_DESC "Multifunction Composite Gadget"
30 MODULE_DESCRIPTION(DRIVER_DESC);
31 MODULE_AUTHOR("Michal Nazarewicz");
32 MODULE_LICENSE("GPL");
35 /***************************** All the files... *****************************/
38 * kbuild is not very cooperative with respect to linking separately
39 * compiled library objects into one module. So for now we won't use
40 * separate compilation ... ensuring init/exit sections work to shrink
41 * the runtime footprint, and giving us at least some parts of what
42 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 #define USB_FMS_INCLUDED
45 #include "f_mass_storage.c"
47 #define USBF_ECM_INCLUDED
50 # define USB_FRNDIS_INCLUDED
56 USB_GADGET_COMPOSITE_OPTIONS();
58 USB_ETHERNET_MODULE_PARAMETERS();
60 /***************************** Device Descriptor ****************************/
62 #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
63 #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
68 #ifdef CONFIG_USB_G_MULTI_RNDIS
69 MULTI_RNDIS_CONFIG_NUM,
71 #ifdef CONFIG_USB_G_MULTI_CDC
77 static struct usb_device_descriptor device_desc = {
78 .bLength = sizeof device_desc,
79 .bDescriptorType = USB_DT_DEVICE,
81 .bcdUSB = cpu_to_le16(0x0200),
83 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
87 /* Vendor and product id can be overridden by module parameters. */
88 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
89 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
93 static const struct usb_descriptor_header *otg_desc[] = {
94 (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
95 .bLength = sizeof(struct usb_otg_descriptor),
96 .bDescriptorType = USB_DT_OTG,
99 * REVISIT SRP-only hardware is possible, although
100 * it would not be called "OTG" ...
102 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
109 MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
110 MULTI_STRING_CDC_CONFIG_IDX,
113 static struct usb_string strings_dev[] = {
114 [USB_GADGET_MANUFACTURER_IDX].s = "",
115 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
116 [USB_GADGET_SERIAL_IDX].s = "",
117 [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
118 [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
119 { } /* end of list */
122 static struct usb_gadget_strings *dev_strings[] = {
123 &(struct usb_gadget_strings){
124 .language = 0x0409, /* en-us */
125 .strings = strings_dev,
133 /****************************** Configurations ******************************/
135 static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
136 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
138 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
143 * Number of buffers we will use.
144 * 2 is usually enough for good buffering pipeline
146 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
148 #endif /* CONFIG_USB_DEBUG */
150 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
152 static struct fsg_common fsg_common;
154 static u8 host_mac[ETH_ALEN];
156 static struct usb_function_instance *fi_acm;
157 static struct eth_dev *the_dev;
159 /********** RNDIS **********/
162 static struct usb_function *f_acm_rndis;
164 static __init int rndis_do_config(struct usb_configuration *c)
168 if (gadget_is_otg(c->cdev->gadget)) {
169 c->descriptors = otg_desc;
170 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
173 ret = rndis_bind_config(c, host_mac, the_dev);
177 f_acm_rndis = usb_get_function(fi_acm);
178 if (IS_ERR(f_acm_rndis))
179 return PTR_ERR(f_acm_rndis);
181 ret = usb_add_function(c, f_acm_rndis);
185 ret = fsg_bind_config(c->cdev, c, &fsg_common);
191 usb_remove_function(c, f_acm_rndis);
193 usb_put_function(f_acm_rndis);
197 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
199 static struct usb_configuration config = {
200 .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
201 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
204 config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
205 config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
207 return usb_add_config(cdev, &config, rndis_do_config);
212 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
220 /********** CDC ECM **********/
222 #ifdef CONFIG_USB_G_MULTI_CDC
223 static struct usb_function *f_acm_multi;
225 static __init int cdc_do_config(struct usb_configuration *c)
229 if (gadget_is_otg(c->cdev->gadget)) {
230 c->descriptors = otg_desc;
231 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
234 ret = ecm_bind_config(c, host_mac, the_dev);
238 /* implicit port_num is zero */
239 f_acm_multi = usb_get_function(fi_acm);
240 if (IS_ERR(f_acm_multi))
241 return PTR_ERR(f_acm_multi);
243 ret = usb_add_function(c, f_acm_multi);
247 ret = fsg_bind_config(c->cdev, c, &fsg_common);
253 usb_remove_function(c, f_acm_multi);
255 usb_put_function(f_acm_multi);
259 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
261 static struct usb_configuration config = {
262 .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
263 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
266 config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
267 config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
269 return usb_add_config(cdev, &config, cdc_do_config);
274 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
283 /****************************** Gadget Bind ******************************/
285 static int __ref multi_bind(struct usb_composite_dev *cdev)
287 struct usb_gadget *gadget = cdev->gadget;
290 if (!can_support_ecm(cdev->gadget)) {
291 dev_err(&gadget->dev, "controller '%s' not usable\n",
296 /* set up network link layer */
297 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
300 return PTR_ERR(the_dev);
302 /* set up serial link layer */
303 fi_acm = usb_get_function_instance("acm");
304 if (IS_ERR(fi_acm)) {
305 status = PTR_ERR(fi_acm);
309 /* set up mass storage function */
312 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data,
315 status = PTR_ERR(retp);
320 /* allocate string IDs */
321 status = usb_string_ids_tab(cdev, strings_dev);
322 if (unlikely(status < 0))
324 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
326 /* register configurations */
327 status = rndis_config_register(cdev);
328 if (unlikely(status < 0))
331 status = cdc_config_register(cdev);
332 if (unlikely(status < 0))
334 usb_composite_overwrite_options(cdev, &coverwrite);
337 dev_info(&gadget->dev, DRIVER_DESC "\n");
338 fsg_common_put(&fsg_common);
344 fsg_common_put(&fsg_common);
346 usb_put_function_instance(fi_acm);
348 gether_cleanup(the_dev);
352 static int __exit multi_unbind(struct usb_composite_dev *cdev)
354 #ifdef CONFIG_USB_G_MULTI_CDC
355 usb_put_function(f_acm_multi);
358 usb_put_function(f_acm_rndis);
360 usb_put_function_instance(fi_acm);
361 gether_cleanup(the_dev);
366 /****************************** Some noise ******************************/
369 static __refdata struct usb_composite_driver multi_driver = {
372 .strings = dev_strings,
373 .max_speed = USB_SPEED_HIGH,
375 .unbind = __exit_p(multi_unbind),
380 static int __init multi_init(void)
382 return usb_composite_probe(&multi_driver);
384 module_init(multi_init);
386 static void __exit multi_exit(void)
388 usb_composite_unregister(&multi_driver);
390 module_exit(multi_exit);