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 CONFIG_USB_G_MULTI_RNDIS
30 # define CONFIG_USB_ETH_RNDIS y
34 #define DRIVER_DESC "Multifunction Composite Gadget"
35 #define DRIVER_VERSION "2009/07/21"
37 /*-------------------------------------------------------------------------*/
39 #define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
40 #define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
42 /*-------------------------------------------------------------------------*/
45 * kbuild is not very cooperative with respect to linking separately
46 * compiled library objects into one module. So for now we won't use
47 * separate compilation ... ensuring init/exit sections work to shrink
48 * the runtime footprint, and giving us at least some parts of what
49 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
52 #include "composite.c"
53 #include "usbstring.c"
55 #include "epautoconf.c"
62 #ifdef CONFIG_USB_ETH_RNDIS
68 #undef DBG /* u_ether.c has broken idea about macros */
69 #undef VDBG /* so clean up after it */
72 #include "f_mass_storage.c"
74 /*-------------------------------------------------------------------------*/
76 static struct usb_device_descriptor device_desc
= {
77 .bLength
= sizeof device_desc
,
78 .bDescriptorType
= USB_DT_DEVICE
,
80 .bcdUSB
= cpu_to_le16(0x0200),
82 /* .bDeviceClass = USB_CLASS_COMM, */
83 /* .bDeviceSubClass = 0, */
84 /* .bDeviceProtocol = 0, */
88 /* .bMaxPacketSize0 = f(hardware) */
90 /* Vendor and product id can be overridden by module parameters. */
91 .idVendor
= cpu_to_le16(MULTI_VENDOR_NUM
),
92 .idProduct
= cpu_to_le16(MULTI_PRODUCT_NUM
),
93 /* .bcdDevice = f(hardware) */
94 /* .iManufacturer = DYNAMIC */
95 /* .iProduct = DYNAMIC */
96 /* NO SERIAL NUMBER */
97 .bNumConfigurations
= 1,
100 static struct usb_otg_descriptor otg_descriptor
= {
101 .bLength
= sizeof otg_descriptor
,
102 .bDescriptorType
= USB_DT_OTG
,
104 /* REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
107 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
110 static const struct usb_descriptor_header
*otg_desc
[] = {
111 (struct usb_descriptor_header
*) &otg_descriptor
,
116 /* string IDs are assigned dynamically */
118 #define STRING_MANUFACTURER_IDX 0
119 #define STRING_PRODUCT_IDX 1
121 static char manufacturer
[50];
123 static struct usb_string strings_dev
[] = {
124 [STRING_MANUFACTURER_IDX
].s
= manufacturer
,
125 [STRING_PRODUCT_IDX
].s
= DRIVER_DESC
,
126 { } /* end of list */
129 static struct usb_gadget_strings stringtab_dev
= {
130 .language
= 0x0409, /* en-us */
131 .strings
= strings_dev
,
134 static struct usb_gadget_strings
*dev_strings
[] = {
139 static u8 hostaddr
[ETH_ALEN
];
143 /****************************** Configurations ******************************/
145 static struct fsg_module_parameters mod_data
= {
148 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
150 static struct fsg_common
*fsg_common
;
153 #ifdef CONFIG_USB_ETH_RNDIS
155 static int __init
rndis_do_config(struct usb_configuration
*c
)
159 if (gadget_is_otg(c
->cdev
->gadget
)) {
160 c
->descriptors
= otg_desc
;
161 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
164 ret
= rndis_bind_config(c
, hostaddr
);
168 ret
= acm_bind_config(c
, 0);
172 ret
= fsg_add(c
->cdev
, c
, fsg_common
);
179 static struct usb_configuration rndis_config_driver
= {
180 .label
= "Multifunction Composite (RNDIS + MS + ACM)",
181 .bind
= rndis_do_config
,
182 .bConfigurationValue
= 2,
183 /* .iConfiguration = DYNAMIC */
184 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
189 #ifdef CONFIG_USB_G_MULTI_CDC
191 static int __init
cdc_do_config(struct usb_configuration
*c
)
195 if (gadget_is_otg(c
->cdev
->gadget
)) {
196 c
->descriptors
= otg_desc
;
197 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
200 ret
= ecm_bind_config(c
, hostaddr
);
204 ret
= acm_bind_config(c
, 0);
208 ret
= fsg_add(c
->cdev
, c
, fsg_common
);
217 static struct usb_configuration cdc_config_driver
= {
218 .label
= "Multifunction Composite (CDC + MS + ACM)",
219 .bind
= cdc_do_config
,
220 .bConfigurationValue
= 1,
221 /* .iConfiguration = DYNAMIC */
222 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
229 /****************************** Gadget Bind ******************************/
232 static int __init
multi_bind(struct usb_composite_dev
*cdev
)
234 struct usb_gadget
*gadget
= cdev
->gadget
;
237 if (!can_support_ecm(cdev
->gadget
)) {
238 dev_err(&gadget
->dev
, "controller '%s' not usable\n",
243 /* set up network link layer */
244 status
= gether_setup(cdev
->gadget
, hostaddr
);
248 /* set up serial link layer */
249 status
= gserial_setup(cdev
->gadget
, 1);
253 /* set up mass storage function */
254 fsg_common
= fsg_common_from_params(0, cdev
, &mod_data
);
255 if (IS_ERR(fsg_common
)) {
256 status
= PTR_ERR(fsg_common
);
261 gcnum
= usb_gadget_controller_number(gadget
);
263 device_desc
.bcdDevice
= cpu_to_le16(0x0300 | gcnum
);
265 /* We assume that can_support_ecm() tells the truth;
266 * but if the controller isn't recognized at all then
267 * that assumption is a bit more likely to be wrong.
269 WARNING(cdev
, "controller '%s' not recognized\n",
271 device_desc
.bcdDevice
= cpu_to_le16(0x0300 | 0x0099);
275 /* Allocate string descriptor numbers ... note that string
276 * contents can be overridden by the composite_dev glue.
279 /* device descriptor strings: manufacturer, product */
280 snprintf(manufacturer
, sizeof manufacturer
, "%s %s with %s",
281 init_utsname()->sysname
, init_utsname()->release
,
283 status
= usb_string_id(cdev
);
286 strings_dev
[STRING_MANUFACTURER_IDX
].id
= status
;
287 device_desc
.iManufacturer
= status
;
289 status
= usb_string_id(cdev
);
292 strings_dev
[STRING_PRODUCT_IDX
].id
= status
;
293 device_desc
.iProduct
= status
;
295 #ifdef CONFIG_USB_ETH_RNDIS
296 /* register our first configuration */
297 status
= usb_add_config(cdev
, &rndis_config_driver
);
302 #ifdef CONFIG_USB_G_MULTI_CDC
303 /* register our second configuration */
304 status
= usb_add_config(cdev
, &cdc_config_driver
);
309 dev_info(&gadget
->dev
, DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
310 fsg_common_put(fsg_common
);
314 fsg_common_put(fsg_common
);
322 static int __exit
multi_unbind(struct usb_composite_dev
*cdev
)
330 /****************************** Some noise ******************************/
333 static struct usb_composite_driver multi_driver
= {
336 .strings
= dev_strings
,
338 .unbind
= __exit_p(multi_unbind
),
341 MODULE_DESCRIPTION(DRIVER_DESC
);
342 MODULE_AUTHOR("Michal Nazarewicz");
343 MODULE_LICENSE("GPL");
345 static int __init
g_multi_init(void)
347 return usb_composite_register(&multi_driver
);
349 module_init(g_multi_init
);
351 static void __exit
g_multi_cleanup(void)
353 usb_composite_unregister(&multi_driver
);
355 module_exit(g_multi_cleanup
);