usb: gadget: start with libcomposite
[linux-block.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 (mina86@mina86.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
15
16 #include <linux/kernel.h>
17 #include <linux/utsname.h>
18 #include <linux/module.h>
19
20
21 #if defined USB_ETH_RNDIS
22 #  undef USB_ETH_RNDIS
23 #endif
24 #ifdef CONFIG_USB_G_MULTI_RNDIS
25 #  define USB_ETH_RNDIS y
26 #endif
27
28
29 #define DRIVER_DESC             "Multifunction Composite Gadget"
30
31 MODULE_DESCRIPTION(DRIVER_DESC);
32 MODULE_AUTHOR("Michal Nazarewicz");
33 MODULE_LICENSE("GPL");
34
35
36 /***************************** All the files... *****************************/
37
38 /*
39  * kbuild is not very cooperative with respect to linking separately
40  * compiled library objects into one module.  So for now we won't use
41  * separate compilation ... ensuring init/exit sections work to shrink
42  * the runtime footprint, and giving us at least some parts of what
43  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44  */
45
46 #include "composite.c"
47 #include "config.c"
48 #include "epautoconf.c"
49
50 #include "f_mass_storage.c"
51
52 #include "u_serial.c"
53 #include "f_acm.c"
54
55 #include "f_ecm.c"
56 #include "f_subset.c"
57 #ifdef USB_ETH_RNDIS
58 #  include "f_rndis.c"
59 #  include "rndis.c"
60 #endif
61 #include "u_ether.c"
62
63
64
65 /***************************** Device Descriptor ****************************/
66
67 #define MULTI_VENDOR_NUM        0x1d6b  /* Linux Foundation */
68 #define MULTI_PRODUCT_NUM       0x0104  /* Multifunction Composite Gadget */
69
70
71 enum {
72         __MULTI_NO_CONFIG,
73 #ifdef CONFIG_USB_G_MULTI_RNDIS
74         MULTI_RNDIS_CONFIG_NUM,
75 #endif
76 #ifdef CONFIG_USB_G_MULTI_CDC
77         MULTI_CDC_CONFIG_NUM,
78 #endif
79 };
80
81
82 static struct usb_device_descriptor device_desc = {
83         .bLength =              sizeof device_desc,
84         .bDescriptorType =      USB_DT_DEVICE,
85
86         .bcdUSB =               cpu_to_le16(0x0200),
87
88         .bDeviceClass =         USB_CLASS_MISC /* 0xEF */,
89         .bDeviceSubClass =      2,
90         .bDeviceProtocol =      1,
91
92         /* Vendor and product id can be overridden by module parameters.  */
93         .idVendor =             cpu_to_le16(MULTI_VENDOR_NUM),
94         .idProduct =            cpu_to_le16(MULTI_PRODUCT_NUM),
95 };
96
97
98 static const struct usb_descriptor_header *otg_desc[] = {
99         (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
100                 .bLength =              sizeof(struct usb_otg_descriptor),
101                 .bDescriptorType =      USB_DT_OTG,
102
103                 /*
104                  * REVISIT SRP-only hardware is possible, although
105                  * it would not be called "OTG" ...
106                  */
107                 .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
108         },
109         NULL,
110 };
111
112
113 enum {
114 #ifdef CONFIG_USB_G_MULTI_RNDIS
115         MULTI_STRING_RNDIS_CONFIG_IDX,
116 #endif
117 #ifdef CONFIG_USB_G_MULTI_CDC
118         MULTI_STRING_CDC_CONFIG_IDX,
119 #endif
120 };
121
122 static struct usb_string strings_dev[] = {
123 #ifdef CONFIG_USB_G_MULTI_RNDIS
124         [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
125 #endif
126 #ifdef CONFIG_USB_G_MULTI_CDC
127         [MULTI_STRING_CDC_CONFIG_IDX].s   = "Multifunction with CDC ECM",
128 #endif
129         {  } /* end of list */
130 };
131
132 static struct usb_gadget_strings *dev_strings[] = {
133         &(struct usb_gadget_strings){
134                 .language       = 0x0409,       /* en-us */
135                 .strings        = strings_dev,
136         },
137         NULL,
138 };
139
140
141
142
143 /****************************** Configurations ******************************/
144
145 static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
146 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
147
148 static struct fsg_common fsg_common;
149
150 static u8 hostaddr[ETH_ALEN];
151
152
153 /********** RNDIS **********/
154
155 #ifdef USB_ETH_RNDIS
156
157 static __init int rndis_do_config(struct usb_configuration *c)
158 {
159         int ret;
160
161         if (gadget_is_otg(c->cdev->gadget)) {
162                 c->descriptors = otg_desc;
163                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
164         }
165
166         ret = rndis_bind_config(c, hostaddr);
167         if (ret < 0)
168                 return ret;
169
170         ret = acm_bind_config(c, 0);
171         if (ret < 0)
172                 return ret;
173
174         ret = fsg_bind_config(c->cdev, c, &fsg_common);
175         if (ret < 0)
176                 return ret;
177
178         return 0;
179 }
180
181 static int rndis_config_register(struct usb_composite_dev *cdev)
182 {
183         static struct usb_configuration config = {
184                 .bConfigurationValue    = MULTI_RNDIS_CONFIG_NUM,
185                 .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
186         };
187
188         config.label          = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
189         config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
190
191         return usb_add_config(cdev, &config, rndis_do_config);
192 }
193
194 #else
195
196 static int rndis_config_register(struct usb_composite_dev *cdev)
197 {
198         return 0;
199 }
200
201 #endif
202
203
204 /********** CDC ECM **********/
205
206 #ifdef CONFIG_USB_G_MULTI_CDC
207
208 static __init int cdc_do_config(struct usb_configuration *c)
209 {
210         int ret;
211
212         if (gadget_is_otg(c->cdev->gadget)) {
213                 c->descriptors = otg_desc;
214                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
215         }
216
217         ret = ecm_bind_config(c, hostaddr);
218         if (ret < 0)
219                 return ret;
220
221         ret = acm_bind_config(c, 0);
222         if (ret < 0)
223                 return ret;
224
225         ret = fsg_bind_config(c->cdev, c, &fsg_common);
226         if (ret < 0)
227                 return ret;
228
229         return 0;
230 }
231
232 static int cdc_config_register(struct usb_composite_dev *cdev)
233 {
234         static struct usb_configuration config = {
235                 .bConfigurationValue    = MULTI_CDC_CONFIG_NUM,
236                 .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
237         };
238
239         config.label          = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
240         config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
241
242         return usb_add_config(cdev, &config, cdc_do_config);
243 }
244
245 #else
246
247 static int cdc_config_register(struct usb_composite_dev *cdev)
248 {
249         return 0;
250 }
251
252 #endif
253
254
255
256 /****************************** Gadget Bind ******************************/
257
258
259 static int __ref multi_bind(struct usb_composite_dev *cdev)
260 {
261         struct usb_gadget *gadget = cdev->gadget;
262         int status, gcnum;
263
264         if (!can_support_ecm(cdev->gadget)) {
265                 dev_err(&gadget->dev, "controller '%s' not usable\n",
266                         gadget->name);
267                 return -EINVAL;
268         }
269
270         /* set up network link layer */
271         status = gether_setup(cdev->gadget, hostaddr);
272         if (status < 0)
273                 return status;
274
275         /* set up serial link layer */
276         status = gserial_setup(cdev->gadget, 1);
277         if (status < 0)
278                 goto fail0;
279
280         /* set up mass storage function */
281         {
282                 void *retp;
283                 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
284                 if (IS_ERR(retp)) {
285                         status = PTR_ERR(retp);
286                         goto fail1;
287                 }
288         }
289
290         /* set bcdDevice */
291         gcnum = usb_gadget_controller_number(gadget);
292         if (gcnum >= 0) {
293                 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
294         } else {
295                 WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
296                 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
297         }
298
299         /* allocate string IDs */
300         status = usb_string_ids_tab(cdev, strings_dev);
301         if (unlikely(status < 0))
302                 goto fail2;
303
304         /* register configurations */
305         status = rndis_config_register(cdev);
306         if (unlikely(status < 0))
307                 goto fail2;
308
309         status = cdc_config_register(cdev);
310         if (unlikely(status < 0))
311                 goto fail2;
312
313         /* we're done */
314         dev_info(&gadget->dev, DRIVER_DESC "\n");
315         fsg_common_put(&fsg_common);
316         return 0;
317
318
319         /* error recovery */
320 fail2:
321         fsg_common_put(&fsg_common);
322 fail1:
323         gserial_cleanup();
324 fail0:
325         gether_cleanup();
326         return status;
327 }
328
329 static int __exit multi_unbind(struct usb_composite_dev *cdev)
330 {
331         gserial_cleanup();
332         gether_cleanup();
333         return 0;
334 }
335
336
337 /****************************** Some noise ******************************/
338
339
340 static __refdata struct usb_composite_driver multi_driver = {
341         .name           = "g_multi",
342         .dev            = &device_desc,
343         .strings        = dev_strings,
344         .max_speed      = USB_SPEED_HIGH,
345         .bind           = multi_bind,
346         .unbind         = __exit_p(multi_unbind),
347         .iProduct       = DRIVER_DESC,
348         .needs_serial   = 1,
349 };
350
351
352 static int __init multi_init(void)
353 {
354         return usb_composite_probe(&multi_driver);
355 }
356 module_init(multi_init);
357
358 static void __exit multi_exit(void)
359 {
360         usb_composite_unregister(&multi_driver);
361 }
362 module_exit(multi_exit);