USB: gadget: f_mass_storage: fsg_add() renamed to fsg_bind_config()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / multi.c
blobd3d31403302263ec49b301d92050a9d41ac3fd43
1 /*
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
30 # undef USB_ETH_RNDIS
31 #endif
32 #ifdef CONFIG_USB_G_MULTI_RNDIS
33 # define USB_ETH_RNDIS y
34 #endif
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"
57 #include "config.c"
58 #include "epautoconf.c"
60 #include "u_serial.c"
61 #include "f_acm.c"
63 #include "f_ecm.c"
64 #include "f_subset.c"
65 #ifdef USB_ETH_RNDIS
66 # include "f_rndis.c"
67 # include "rndis.c"
68 #endif
69 #include "u_ether.c"
71 #undef DBG /* u_ether.c has broken idea about macros */
72 #undef VDBG /* so clean up after it */
73 #undef ERROR
74 #undef INFO
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, */
88 .bDeviceClass = 0xEF,
89 .bDeviceSubClass = 2,
90 .bDeviceProtocol = 1,
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,
115 NULL,
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[] = {
138 &stringtab_dev,
139 NULL,
142 static u8 hostaddr[ETH_ALEN];
146 /****************************** Configurations ******************************/
148 static struct fsg_module_parameters mod_data = {
149 .stall = 1
151 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
153 static struct fsg_common *fsg_common;
156 #ifdef USB_ETH_RNDIS
158 static int __init rndis_do_config(struct usb_configuration *c)
160 int ret;
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);
168 if (ret < 0)
169 return ret;
171 ret = acm_bind_config(c, 0);
172 if (ret < 0)
173 return ret;
175 ret = fsg_bind_config(c->cdev, c, fsg_common);
176 if (ret < 0)
177 return ret;
179 return 0;
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,
190 #endif
192 #ifdef CONFIG_USB_G_MULTI_CDC
194 static int __init cdc_do_config(struct usb_configuration *c)
196 int ret;
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);
204 if (ret < 0)
205 return ret;
207 ret = acm_bind_config(c, 0);
208 if (ret < 0)
209 return ret;
211 ret = fsg_bind_config(c->cdev, c, fsg_common);
212 if (ret < 0)
213 return ret;
215 return 0;
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,
226 #endif
230 /****************************** Gadget Bind ******************************/
233 static int __init multi_bind(struct usb_composite_dev *cdev)
235 struct usb_gadget *gadget = cdev->gadget;
236 int status, gcnum;
238 if (!can_support_ecm(cdev->gadget)) {
239 dev_err(&gadget->dev, "controller '%s' not usable\n",
240 gadget->name);
241 return -EINVAL;
244 /* set up network link layer */
245 status = gether_setup(cdev->gadget, hostaddr);
246 if (status < 0)
247 return status;
249 /* set up serial link layer */
250 status = gserial_setup(cdev->gadget, 1);
251 if (status < 0)
252 goto fail0;
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);
258 goto fail1;
262 gcnum = usb_gadget_controller_number(gadget);
263 if (gcnum >= 0)
264 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
265 else {
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",
271 gadget->name);
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,
283 gadget->name);
284 status = usb_string_id(cdev);
285 if (status < 0)
286 goto fail2;
287 strings_dev[STRING_MANUFACTURER_IDX].id = status;
288 device_desc.iManufacturer = status;
290 status = usb_string_id(cdev);
291 if (status < 0)
292 goto fail2;
293 strings_dev[STRING_PRODUCT_IDX].id = status;
294 device_desc.iProduct = status;
296 #ifdef USB_ETH_RNDIS
297 /* register our first configuration */
298 status = usb_add_config(cdev, &rndis_config_driver);
299 if (status < 0)
300 goto fail2;
301 #endif
303 #ifdef CONFIG_USB_G_MULTI_CDC
304 /* register our second configuration */
305 status = usb_add_config(cdev, &cdc_config_driver);
306 if (status < 0)
307 goto fail2;
308 #endif
310 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
311 fsg_common_put(fsg_common);
312 return 0;
314 fail2:
315 fsg_common_put(fsg_common);
316 fail1:
317 gserial_cleanup();
318 fail0:
319 gether_cleanup();
320 return status;
323 static int __exit multi_unbind(struct usb_composite_dev *cdev)
325 gserial_cleanup();
326 gether_cleanup();
327 return 0;
331 /****************************** Some noise ******************************/
334 static struct usb_composite_driver multi_driver = {
335 .name = "g_multi",
336 .dev = &device_desc,
337 .strings = dev_strings,
338 .bind = multi_bind,
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);