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