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 (m.nazarewicz@samsung.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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/utsname.h>
29 #if defined USB_ETH_RNDIS
32 #ifdef CONFIG_USB_ETH_RNDIS
33 # define USB_ETH_RNDIS y
37 #define DRIVER_DESC "Multifunction Composite Gadget"
38 #define DRIVER_VERSION "2009/07/21"
40 /*-------------------------------------------------------------------------*/
42 #define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
43 #define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
45 /*-------------------------------------------------------------------------*/
48 * kbuild is not very cooperative with respect to linking separately
49 * compiled library objects into one module. So for now we won't use
50 * separate compilation ... ensuring init/exit sections work to shrink
51 * the runtime footprint, and giving us at least some parts of what
52 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
55 #include "composite.c"
56 #include "usbstring.c"
58 #include "epautoconf.c"
71 #undef DBG /* u_ether.c has broken idea about macros */
72 #undef VDBG /* so clean up after it */
75 #include "f_mass_storage.c"
77 /*-------------------------------------------------------------------------*/
79 static struct usb_device_descriptor device_desc = {
80 .bLength = sizeof device_desc,
81 .bDescriptorType = USB_DT_DEVICE,
83 .bcdUSB = cpu_to_le16(0x0200),
85 /* .bDeviceClass = USB_CLASS_COMM, */
86 /* .bDeviceSubClass = 0, */
87 /* .bDeviceProtocol = 0, */
91 /* .bMaxPacketSize0 = f(hardware) */
93 /* Vendor and product id can be overridden by module parameters. */
94 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
95 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
96 /* .bcdDevice = f(hardware) */
97 /* .iManufacturer = DYNAMIC */
98 /* .iProduct = DYNAMIC */
99 /* NO SERIAL NUMBER */
100 .bNumConfigurations = 1,
103 static struct usb_otg_descriptor otg_descriptor = {
104 .bLength = sizeof otg_descriptor,
105 .bDescriptorType = USB_DT_OTG,
107 /* REVISIT SRP-only hardware is possible, although
108 * it would not be called "OTG" ...
110 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
113 static const struct usb_descriptor_header *otg_desc[] = {
114 (struct usb_descriptor_header *) &otg_descriptor,
119 /* string IDs are assigned dynamically */
121 #define STRING_MANUFACTURER_IDX 0
122 #define STRING_PRODUCT_IDX 1
124 static char manufacturer[50];
126 static struct usb_string strings_dev[] = {
127 [STRING_MANUFACTURER_IDX].s = manufacturer,
128 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
129 { } /* end of list */
132 static struct usb_gadget_strings stringtab_dev = {
133 .language = 0x0409, /* en-us */
134 .strings = strings_dev,
137 static struct usb_gadget_strings *dev_strings[] = {
142 static u8 hostaddr[ETH_ALEN];
146 /****************************** Configurations ******************************/
148 static struct fsg_module_parameters mod_data = {
151 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
153 static struct fsg_common *fsg_common;
158 static int __init rndis_do_config(struct usb_configuration *c)
162 if (gadget_is_otg(c->cdev->gadget)) {
163 c->descriptors = otg_desc;
164 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
167 ret = rndis_bind_config(c, hostaddr);
171 ret = acm_bind_config(c, 0);
175 ret = fsg_add(c->cdev, c, fsg_common);
182 static struct usb_configuration rndis_config_driver = {
183 .label = "Multifunction Composite (RNDIS + MS + ACM)",
184 .bind = rndis_do_config,
185 .bConfigurationValue = 2,
186 /* .iConfiguration = DYNAMIC */
187 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
192 #ifdef CONFIG_USB_G_MULTI_CDC
194 static int __init cdc_do_config(struct usb_configuration *c)
198 if (gadget_is_otg(c->cdev->gadget)) {
199 c->descriptors = otg_desc;
200 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
203 ret = ecm_bind_config(c, hostaddr);
207 ret = acm_bind_config(c, 0);
211 ret = fsg_add(c->cdev, c, fsg_common);
220 static struct usb_configuration cdc_config_driver = {
221 .label = "Multifunction Composite (CDC + MS + ACM)",
222 .bind = cdc_do_config,
223 .bConfigurationValue = 1,
224 /* .iConfiguration = DYNAMIC */
225 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
232 /****************************** Gadget Bind ******************************/
235 static int __init multi_bind(struct usb_composite_dev *cdev)
237 struct usb_gadget *gadget = cdev->gadget;
240 if (!can_support_ecm(cdev->gadget)) {
241 dev_err(&gadget->dev, "controller '%s' not usable\n",
246 /* set up network link layer */
247 status = gether_setup(cdev->gadget, hostaddr);
251 /* set up serial link layer */
252 status = gserial_setup(cdev->gadget, 1);
256 /* set up mass storage function */
257 fsg_common = fsg_common_from_params(0, cdev, &mod_data);
258 if (IS_ERR(fsg_common)) {
259 status = PTR_ERR(fsg_common);
264 gcnum = usb_gadget_controller_number(gadget);
266 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
268 /* We assume that can_support_ecm() tells the truth;
269 * but if the controller isn't recognized at all then
270 * that assumption is a bit more likely to be wrong.
272 WARNING(cdev, "controller '%s' not recognized\n",
274 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
278 /* Allocate string descriptor numbers ... note that string
279 * contents can be overridden by the composite_dev glue.
282 /* device descriptor strings: manufacturer, product */
283 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
284 init_utsname()->sysname, init_utsname()->release,
286 status = usb_string_id(cdev);
289 strings_dev[STRING_MANUFACTURER_IDX].id = status;
290 device_desc.iManufacturer = status;
292 status = usb_string_id(cdev);
295 strings_dev[STRING_PRODUCT_IDX].id = status;
296 device_desc.iProduct = status;
299 /* register our first configuration */
300 status = usb_add_config(cdev, &rndis_config_driver);
305 #ifdef CONFIG_USB_G_MULTI_CDC
306 /* register our second configuration */
307 status = usb_add_config(cdev, &cdc_config_driver);
312 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
313 fsg_common_put(fsg_common);
317 fsg_common_put(fsg_common);
325 static int __exit multi_unbind(struct usb_composite_dev *cdev)
333 /****************************** Some noise ******************************/
336 static struct usb_composite_driver multi_driver = {
339 .strings = dev_strings,
341 .unbind = __exit_p(multi_unbind),
344 MODULE_DESCRIPTION(DRIVER_DESC);
345 MODULE_AUTHOR("Michal Nazarewicz");
346 MODULE_LICENSE("GPL");
348 static int __init g_multi_init(void)
350 return usb_composite_register(&multi_driver);
352 module_init(g_multi_init);
354 static void __exit g_multi_cleanup(void)
356 usb_composite_unregister(&multi_driver);
358 module_exit(g_multi_cleanup);