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_G_MULTI_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
);
218 static struct usb_configuration cdc_config_driver
= {
219 .label
= "Multifunction Composite (CDC + MS + ACM)",
220 .bind
= cdc_do_config
,
221 .bConfigurationValue
= 1,
222 /* .iConfiguration = DYNAMIC */
223 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
230 /****************************** Gadget Bind ******************************/
233 static int __init
multi_bind(struct usb_composite_dev
*cdev
)
235 struct usb_gadget
*gadget
= cdev
->gadget
;
238 if (!can_support_ecm(cdev
->gadget
)) {
239 dev_err(&gadget
->dev
, "controller '%s' not usable\n",
244 /* set up network link layer */
245 status
= gether_setup(cdev
->gadget
, hostaddr
);
249 /* set up serial link layer */
250 status
= gserial_setup(cdev
->gadget
, 1);
254 /* set up mass storage function */
255 fsg_common
= fsg_common_from_params(0, cdev
, &mod_data
);
256 if (IS_ERR(fsg_common
)) {
257 status
= PTR_ERR(fsg_common
);
262 gcnum
= usb_gadget_controller_number(gadget
);
264 device_desc
.bcdDevice
= cpu_to_le16(0x0300 | gcnum
);
266 /* We assume that can_support_ecm() tells the truth;
267 * but if the controller isn't recognized at all then
268 * that assumption is a bit more likely to be wrong.
270 WARNING(cdev
, "controller '%s' not recognized\n",
272 device_desc
.bcdDevice
= cpu_to_le16(0x0300 | 0x0099);
276 /* Allocate string descriptor numbers ... note that string
277 * contents can be overridden by the composite_dev glue.
280 /* device descriptor strings: manufacturer, product */
281 snprintf(manufacturer
, sizeof manufacturer
, "%s %s with %s",
282 init_utsname()->sysname
, init_utsname()->release
,
284 status
= usb_string_id(cdev
);
287 strings_dev
[STRING_MANUFACTURER_IDX
].id
= status
;
288 device_desc
.iManufacturer
= status
;
290 status
= usb_string_id(cdev
);
293 strings_dev
[STRING_PRODUCT_IDX
].id
= status
;
294 device_desc
.iProduct
= status
;
297 /* register our first configuration */
298 status
= usb_add_config(cdev
, &rndis_config_driver
);
303 #ifdef CONFIG_USB_G_MULTI_CDC
304 /* register our second configuration */
305 status
= usb_add_config(cdev
, &cdc_config_driver
);
310 dev_info(&gadget
->dev
, DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
311 fsg_common_put(fsg_common
);
315 fsg_common_put(fsg_common
);
323 static int __exit
multi_unbind(struct usb_composite_dev
*cdev
)
331 /****************************** Some noise ******************************/
334 static struct usb_composite_driver multi_driver
= {
337 .strings
= dev_strings
,
339 .unbind
= __exit_p(multi_unbind
),
342 MODULE_DESCRIPTION(DRIVER_DESC
);
343 MODULE_AUTHOR("Michal Nazarewicz");
344 MODULE_LICENSE("GPL");
346 static int __init
g_multi_init(void)
348 return usb_composite_register(&multi_driver
);
350 module_init(g_multi_init
);
352 static void __exit
g_multi_cleanup(void)
354 usb_composite_unregister(&multi_driver
);
356 module_exit(g_multi_cleanup
);