USB: gadget: Add DEVTYPE support for Ethernet functions
[pandora-kernel.git] / drivers / usb / gadget / multi.c
1 /*
2  * multi.c -- Multifunction Composite driver
3  *
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)
8  *
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.
13  *
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.
18  *
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
22  */
23
24
25 #include <linux/kernel.h>
26 #include <linux/utsname.h>
27
28
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
35
36
37 #define DRIVER_DESC             "Multifunction Composite Gadget"
38 #define DRIVER_VERSION          "2009/07/21"
39
40 /*-------------------------------------------------------------------------*/
41
42 #define MULTI_VENDOR_NUM        0x0525  /* XXX NetChip */
43 #define MULTI_PRODUCT_NUM       0xa4ab  /* XXX */
44
45 /*-------------------------------------------------------------------------*/
46
47 /*
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.
53  */
54
55 #include "composite.c"
56 #include "usbstring.c"
57 #include "config.c"
58 #include "epautoconf.c"
59
60 #include "u_serial.c"
61 #include "f_acm.c"
62
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"
70
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"
76
77 /*-------------------------------------------------------------------------*/
78
79 static struct usb_device_descriptor device_desc = {
80         .bLength =              sizeof device_desc,
81         .bDescriptorType =      USB_DT_DEVICE,
82
83         .bcdUSB =               cpu_to_le16(0x0200),
84
85         /* .bDeviceClass =              USB_CLASS_COMM, */
86         /* .bDeviceSubClass =   0, */
87         /* .bDeviceProtocol =   0, */
88         .bDeviceClass =         0xEF,
89         .bDeviceSubClass =      2,
90         .bDeviceProtocol =      1,
91         /* .bMaxPacketSize0 = f(hardware) */
92
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,
101 };
102
103 static struct usb_otg_descriptor otg_descriptor = {
104         .bLength =              sizeof otg_descriptor,
105         .bDescriptorType =      USB_DT_OTG,
106
107         /* REVISIT SRP-only hardware is possible, although
108          * it would not be called "OTG" ...
109          */
110         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
111 };
112
113 static const struct usb_descriptor_header *otg_desc[] = {
114         (struct usb_descriptor_header *) &otg_descriptor,
115         NULL,
116 };
117
118
119 /* string IDs are assigned dynamically */
120
121 #define STRING_MANUFACTURER_IDX         0
122 #define STRING_PRODUCT_IDX              1
123
124 static char manufacturer[50];
125
126 static struct usb_string strings_dev[] = {
127         [STRING_MANUFACTURER_IDX].s = manufacturer,
128         [STRING_PRODUCT_IDX].s = DRIVER_DESC,
129         {  } /* end of list */
130 };
131
132 static struct usb_gadget_strings stringtab_dev = {
133         .language       = 0x0409,       /* en-us */
134         .strings        = strings_dev,
135 };
136
137 static struct usb_gadget_strings *dev_strings[] = {
138         &stringtab_dev,
139         NULL,
140 };
141
142 static u8 hostaddr[ETH_ALEN];
143
144
145
146 /****************************** Configurations ******************************/
147
148 static struct fsg_module_parameters mod_data = {
149         .stall = 1
150 };
151 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
152
153 static struct fsg_common *fsg_common;
154
155
156 #ifdef USB_ETH_RNDIS
157
158 static int __init rndis_do_config(struct usb_configuration *c)
159 {
160         int ret;
161
162         if (gadget_is_otg(c->cdev->gadget)) {
163                 c->descriptors = otg_desc;
164                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
165         }
166
167         ret = rndis_bind_config(c, hostaddr);
168         if (ret < 0)
169                 return ret;
170
171         ret = acm_bind_config(c, 0);
172         if (ret < 0)
173                 return ret;
174
175         ret = fsg_add(c->cdev, c, fsg_common);
176         if (ret < 0)
177                 return ret;
178
179         return 0;
180 }
181
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,
188 };
189
190 #endif
191
192 #ifdef CONFIG_USB_G_MULTI_CDC
193
194 static int __init cdc_do_config(struct usb_configuration *c)
195 {
196         int ret;
197
198         if (gadget_is_otg(c->cdev->gadget)) {
199                 c->descriptors = otg_desc;
200                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
201         }
202
203         ret = ecm_bind_config(c, hostaddr);
204         if (ret < 0)
205                 return ret;
206
207         ret = acm_bind_config(c, 0);
208         if (ret < 0)
209                 return ret;
210
211         ret = fsg_add(c->cdev, c, fsg_common);
212         if (ret < 0)
213                 return ret;
214         if (ret < 0)
215                 return ret;
216
217         return 0;
218 }
219
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,
226 };
227
228 #endif
229
230
231
232 /****************************** Gadget Bind ******************************/
233
234
235 static int __init multi_bind(struct usb_composite_dev *cdev)
236 {
237         struct usb_gadget *gadget = cdev->gadget;
238         int status, gcnum;
239
240         if (!can_support_ecm(cdev->gadget)) {
241                 dev_err(&gadget->dev, "controller '%s' not usable\n",
242                         gadget->name);
243                 return -EINVAL;
244         }
245
246         /* set up network link layer */
247         status = gether_setup(cdev->gadget, hostaddr);
248         if (status < 0)
249                 return status;
250
251         /* set up serial link layer */
252         status = gserial_setup(cdev->gadget, 1);
253         if (status < 0)
254                 goto fail0;
255
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);
260                 goto fail1;
261         }
262
263
264         gcnum = usb_gadget_controller_number(gadget);
265         if (gcnum >= 0)
266                 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
267         else {
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.
271                  */
272                 WARNING(cdev, "controller '%s' not recognized\n",
273                         gadget->name);
274                 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
275         }
276
277
278         /* Allocate string descriptor numbers ... note that string
279          * contents can be overridden by the composite_dev glue.
280          */
281
282         /* device descriptor strings: manufacturer, product */
283         snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
284                  init_utsname()->sysname, init_utsname()->release,
285                  gadget->name);
286         status = usb_string_id(cdev);
287         if (status < 0)
288                 goto fail2;
289         strings_dev[STRING_MANUFACTURER_IDX].id = status;
290         device_desc.iManufacturer = status;
291
292         status = usb_string_id(cdev);
293         if (status < 0)
294                 goto fail2;
295         strings_dev[STRING_PRODUCT_IDX].id = status;
296         device_desc.iProduct = status;
297
298 #ifdef USB_ETH_RNDIS
299         /* register our first configuration */
300         status = usb_add_config(cdev, &rndis_config_driver);
301         if (status < 0)
302                 goto fail2;
303 #endif
304
305 #ifdef CONFIG_USB_G_MULTI_CDC
306         /* register our second configuration */
307         status = usb_add_config(cdev, &cdc_config_driver);
308         if (status < 0)
309                 goto fail2;
310 #endif
311
312         dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
313         fsg_common_put(fsg_common);
314         return 0;
315
316 fail2:
317         fsg_common_put(fsg_common);
318 fail1:
319         gserial_cleanup();
320 fail0:
321         gether_cleanup();
322         return status;
323 }
324
325 static int __exit multi_unbind(struct usb_composite_dev *cdev)
326 {
327         gserial_cleanup();
328         gether_cleanup();
329         return 0;
330 }
331
332
333 /****************************** Some noise ******************************/
334
335
336 static struct usb_composite_driver multi_driver = {
337         .name           = "g_multi",
338         .dev            = &device_desc,
339         .strings        = dev_strings,
340         .bind           = multi_bind,
341         .unbind         = __exit_p(multi_unbind),
342 };
343
344 MODULE_DESCRIPTION(DRIVER_DESC);
345 MODULE_AUTHOR("Michal Nazarewicz");
346 MODULE_LICENSE("GPL");
347
348 static int __init g_multi_init(void)
349 {
350         return usb_composite_register(&multi_driver);
351 }
352 module_init(g_multi_init);
353
354 static void __exit g_multi_cleanup(void)
355 {
356         usb_composite_unregister(&multi_driver);
357 }
358 module_exit(g_multi_cleanup);