USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / gadget / legacy / multi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * multi.c -- Multifunction Composite driver
4  *
5  * Copyright (C) 2008 David Brownell
6  * Copyright (C) 2008 Nokia Corporation
7  * Copyright (C) 2009 Samsung Electronics
8  * Author: Michal Nazarewicz (mina86@mina86.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20
21 #include "u_serial.h"
22 #if defined USB_ETH_RNDIS
23 #  undef USB_ETH_RNDIS
24 #endif
25 #ifdef CONFIG_USB_G_MULTI_RNDIS
26 #  define USB_ETH_RNDIS y
27 #endif
28
29
30 #define DRIVER_DESC             "Multifunction Composite Gadget"
31
32 MODULE_DESCRIPTION(DRIVER_DESC);
33 MODULE_AUTHOR("Michal Nazarewicz");
34 MODULE_LICENSE("GPL");
35
36
37 #include "f_mass_storage.h"
38
39 #include "u_ecm.h"
40 #ifdef USB_ETH_RNDIS
41 #  include "u_rndis.h"
42 #  include "rndis.h"
43 #endif
44 #include "u_ether.h"
45
46 USB_GADGET_COMPOSITE_OPTIONS();
47
48 USB_ETHERNET_MODULE_PARAMETERS();
49
50 /***************************** Device Descriptor ****************************/
51
52 #define MULTI_VENDOR_NUM        0x1d6b  /* Linux Foundation */
53 #define MULTI_PRODUCT_NUM       0x0104  /* Multifunction Composite Gadget */
54
55
56 enum {
57         __MULTI_NO_CONFIG,
58 #ifdef CONFIG_USB_G_MULTI_RNDIS
59         MULTI_RNDIS_CONFIG_NUM,
60 #endif
61 #ifdef CONFIG_USB_G_MULTI_CDC
62         MULTI_CDC_CONFIG_NUM,
63 #endif
64 };
65
66
67 static struct usb_device_descriptor device_desc = {
68         .bLength =              sizeof device_desc,
69         .bDescriptorType =      USB_DT_DEVICE,
70
71         /* .bcdUSB = DYNAMIC */
72
73         .bDeviceClass =         USB_CLASS_MISC /* 0xEF */,
74         .bDeviceSubClass =      2,
75         .bDeviceProtocol =      1,
76
77         /* Vendor and product id can be overridden by module parameters.  */
78         .idVendor =             cpu_to_le16(MULTI_VENDOR_NUM),
79         .idProduct =            cpu_to_le16(MULTI_PRODUCT_NUM),
80 };
81
82 static const struct usb_descriptor_header *otg_desc[2];
83
84 enum {
85         MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
86         MULTI_STRING_CDC_CONFIG_IDX,
87 };
88
89 static struct usb_string strings_dev[] = {
90         [USB_GADGET_MANUFACTURER_IDX].s = "",
91         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
92         [USB_GADGET_SERIAL_IDX].s = "",
93         [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
94         [MULTI_STRING_CDC_CONFIG_IDX].s   = "Multifunction with CDC ECM",
95         {  } /* end of list */
96 };
97
98 static struct usb_gadget_strings *dev_strings[] = {
99         &(struct usb_gadget_strings){
100                 .language       = 0x0409,       /* en-us */
101                 .strings        = strings_dev,
102         },
103         NULL,
104 };
105
106
107
108
109 /****************************** Configurations ******************************/
110
111 static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
112 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
113
114 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
115
116 #else
117
118 /*
119  * Number of buffers we will use.
120  * 2 is usually enough for good buffering pipeline
121  */
122 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
123
124 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
125
126 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
127
128 static struct usb_function_instance *fi_acm;
129 static struct usb_function_instance *fi_msg;
130
131 /********** RNDIS **********/
132
133 #ifdef USB_ETH_RNDIS
134 static struct usb_function_instance *fi_rndis;
135 static struct usb_function *f_acm_rndis;
136 static struct usb_function *f_rndis;
137 static struct usb_function *f_msg_rndis;
138
139 static int rndis_do_config(struct usb_configuration *c)
140 {
141         int ret;
142
143         if (gadget_is_otg(c->cdev->gadget)) {
144                 c->descriptors = otg_desc;
145                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
146         }
147
148         f_rndis = usb_get_function(fi_rndis);
149         if (IS_ERR(f_rndis))
150                 return PTR_ERR(f_rndis);
151
152         ret = usb_add_function(c, f_rndis);
153         if (ret < 0)
154                 goto err_func_rndis;
155
156         f_acm_rndis = usb_get_function(fi_acm);
157         if (IS_ERR(f_acm_rndis)) {
158                 ret = PTR_ERR(f_acm_rndis);
159                 goto err_func_acm;
160         }
161
162         ret = usb_add_function(c, f_acm_rndis);
163         if (ret)
164                 goto err_conf;
165
166         f_msg_rndis = usb_get_function(fi_msg);
167         if (IS_ERR(f_msg_rndis)) {
168                 ret = PTR_ERR(f_msg_rndis);
169                 goto err_fsg;
170         }
171
172         ret = usb_add_function(c, f_msg_rndis);
173         if (ret)
174                 goto err_run;
175
176         return 0;
177 err_run:
178         usb_put_function(f_msg_rndis);
179 err_fsg:
180         usb_remove_function(c, f_acm_rndis);
181 err_conf:
182         usb_put_function(f_acm_rndis);
183 err_func_acm:
184         usb_remove_function(c, f_rndis);
185 err_func_rndis:
186         usb_put_function(f_rndis);
187         return ret;
188 }
189
190 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
191 {
192         static struct usb_configuration config = {
193                 .bConfigurationValue    = MULTI_RNDIS_CONFIG_NUM,
194                 .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
195         };
196
197         config.label          = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
198         config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
199
200         return usb_add_config(cdev, &config, rndis_do_config);
201 }
202
203 #else
204
205 static __ref int rndis_config_register(struct usb_composite_dev *cdev)
206 {
207         return 0;
208 }
209
210 #endif
211
212
213 /********** CDC ECM **********/
214
215 #ifdef CONFIG_USB_G_MULTI_CDC
216 static struct usb_function_instance *fi_ecm;
217 static struct usb_function *f_acm_multi;
218 static struct usb_function *f_ecm;
219 static struct usb_function *f_msg_multi;
220
221 static int cdc_do_config(struct usb_configuration *c)
222 {
223         int ret;
224
225         if (gadget_is_otg(c->cdev->gadget)) {
226                 c->descriptors = otg_desc;
227                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
228         }
229
230         f_ecm = usb_get_function(fi_ecm);
231         if (IS_ERR(f_ecm))
232                 return PTR_ERR(f_ecm);
233
234         ret = usb_add_function(c, f_ecm);
235         if (ret < 0)
236                 goto err_func_ecm;
237
238         /* implicit port_num is zero */
239         f_acm_multi = usb_get_function(fi_acm);
240         if (IS_ERR(f_acm_multi)) {
241                 ret = PTR_ERR(f_acm_multi);
242                 goto err_func_acm;
243         }
244
245         ret = usb_add_function(c, f_acm_multi);
246         if (ret)
247                 goto err_conf;
248
249         f_msg_multi = usb_get_function(fi_msg);
250         if (IS_ERR(f_msg_multi)) {
251                 ret = PTR_ERR(f_msg_multi);
252                 goto err_fsg;
253         }
254
255         ret = usb_add_function(c, f_msg_multi);
256         if (ret)
257                 goto err_run;
258
259         return 0;
260 err_run:
261         usb_put_function(f_msg_multi);
262 err_fsg:
263         usb_remove_function(c, f_acm_multi);
264 err_conf:
265         usb_put_function(f_acm_multi);
266 err_func_acm:
267         usb_remove_function(c, f_ecm);
268 err_func_ecm:
269         usb_put_function(f_ecm);
270         return ret;
271 }
272
273 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
274 {
275         static struct usb_configuration config = {
276                 .bConfigurationValue    = MULTI_CDC_CONFIG_NUM,
277                 .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
278         };
279
280         config.label          = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
281         config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
282
283         return usb_add_config(cdev, &config, cdc_do_config);
284 }
285
286 #else
287
288 static __ref int cdc_config_register(struct usb_composite_dev *cdev)
289 {
290         return 0;
291 }
292
293 #endif
294
295
296
297 /****************************** Gadget Bind ******************************/
298
299 static int __ref multi_bind(struct usb_composite_dev *cdev)
300 {
301         struct usb_gadget *gadget = cdev->gadget;
302 #ifdef CONFIG_USB_G_MULTI_CDC
303         struct f_ecm_opts *ecm_opts;
304 #endif
305 #ifdef USB_ETH_RNDIS
306         struct f_rndis_opts *rndis_opts;
307 #endif
308         struct fsg_opts *fsg_opts;
309         struct fsg_config config;
310         int status;
311
312         if (!can_support_ecm(cdev->gadget)) {
313                 dev_err(&gadget->dev, "controller '%s' not usable\n",
314                         gadget->name);
315                 return -EINVAL;
316         }
317
318 #ifdef CONFIG_USB_G_MULTI_CDC
319         fi_ecm = usb_get_function_instance("ecm");
320         if (IS_ERR(fi_ecm))
321                 return PTR_ERR(fi_ecm);
322
323         ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
324
325         gether_set_qmult(ecm_opts->net, qmult);
326         if (!gether_set_host_addr(ecm_opts->net, host_addr))
327                 pr_info("using host ethernet address: %s", host_addr);
328         if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
329                 pr_info("using self ethernet address: %s", dev_addr);
330 #endif
331
332 #ifdef USB_ETH_RNDIS
333         fi_rndis = usb_get_function_instance("rndis");
334         if (IS_ERR(fi_rndis)) {
335                 status = PTR_ERR(fi_rndis);
336                 goto fail;
337         }
338
339         rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
340
341         gether_set_qmult(rndis_opts->net, qmult);
342         if (!gether_set_host_addr(rndis_opts->net, host_addr))
343                 pr_info("using host ethernet address: %s", host_addr);
344         if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
345                 pr_info("using self ethernet address: %s", dev_addr);
346 #endif
347
348 #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
349         /*
350          * If both ecm and rndis are selected then:
351          *      1) rndis borrows the net interface from ecm
352          *      2) since the interface is shared it must not be bound
353          *      twice - in ecm's _and_ rndis' binds, so do it here.
354          */
355         gether_set_gadget(ecm_opts->net, cdev->gadget);
356         status = gether_register_netdev(ecm_opts->net);
357         if (status)
358                 goto fail0;
359
360         rndis_borrow_net(fi_rndis, ecm_opts->net);
361         ecm_opts->bound = true;
362 #endif
363
364         /* set up serial link layer */
365         fi_acm = usb_get_function_instance("acm");
366         if (IS_ERR(fi_acm)) {
367                 status = PTR_ERR(fi_acm);
368                 goto fail0;
369         }
370
371         /* set up mass storage function */
372         fi_msg = usb_get_function_instance("mass_storage");
373         if (IS_ERR(fi_msg)) {
374                 status = PTR_ERR(fi_msg);
375                 goto fail1;
376         }
377         fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
378         fsg_opts = fsg_opts_from_func_inst(fi_msg);
379
380         fsg_opts->no_configfs = true;
381         status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
382         if (status)
383                 goto fail2;
384
385         status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
386         if (status)
387                 goto fail_set_cdev;
388
389         fsg_common_set_sysfs(fsg_opts->common, true);
390         status = fsg_common_create_luns(fsg_opts->common, &config);
391         if (status)
392                 goto fail_set_cdev;
393
394         fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
395                                       config.product_name);
396
397         /* allocate string IDs */
398         status = usb_string_ids_tab(cdev, strings_dev);
399         if (unlikely(status < 0))
400                 goto fail_string_ids;
401         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
402
403         if (gadget_is_otg(gadget) && !otg_desc[0]) {
404                 struct usb_descriptor_header *usb_desc;
405
406                 usb_desc = usb_otg_descriptor_alloc(gadget);
407                 if (!usb_desc)
408                         goto fail_string_ids;
409                 usb_otg_descriptor_init(gadget, usb_desc);
410                 otg_desc[0] = usb_desc;
411                 otg_desc[1] = NULL;
412         }
413
414         /* register configurations */
415         status = rndis_config_register(cdev);
416         if (unlikely(status < 0))
417                 goto fail_otg_desc;
418
419         status = cdc_config_register(cdev);
420         if (unlikely(status < 0))
421                 goto fail_otg_desc;
422         usb_composite_overwrite_options(cdev, &coverwrite);
423
424         /* we're done */
425         dev_info(&gadget->dev, DRIVER_DESC "\n");
426         return 0;
427
428
429         /* error recovery */
430 fail_otg_desc:
431         kfree(otg_desc[0]);
432         otg_desc[0] = NULL;
433 fail_string_ids:
434         fsg_common_remove_luns(fsg_opts->common);
435 fail_set_cdev:
436         fsg_common_free_buffers(fsg_opts->common);
437 fail2:
438         usb_put_function_instance(fi_msg);
439 fail1:
440         usb_put_function_instance(fi_acm);
441 fail0:
442 #ifdef USB_ETH_RNDIS
443         usb_put_function_instance(fi_rndis);
444 fail:
445 #endif
446 #ifdef CONFIG_USB_G_MULTI_CDC
447         usb_put_function_instance(fi_ecm);
448 #endif
449         return status;
450 }
451
452 static int multi_unbind(struct usb_composite_dev *cdev)
453 {
454 #ifdef CONFIG_USB_G_MULTI_CDC
455         usb_put_function(f_msg_multi);
456 #endif
457 #ifdef USB_ETH_RNDIS
458         usb_put_function(f_msg_rndis);
459 #endif
460         usb_put_function_instance(fi_msg);
461 #ifdef CONFIG_USB_G_MULTI_CDC
462         usb_put_function(f_acm_multi);
463 #endif
464 #ifdef USB_ETH_RNDIS
465         usb_put_function(f_acm_rndis);
466 #endif
467         usb_put_function_instance(fi_acm);
468 #ifdef USB_ETH_RNDIS
469         usb_put_function(f_rndis);
470         usb_put_function_instance(fi_rndis);
471 #endif
472 #ifdef CONFIG_USB_G_MULTI_CDC
473         usb_put_function(f_ecm);
474         usb_put_function_instance(fi_ecm);
475 #endif
476         kfree(otg_desc[0]);
477         otg_desc[0] = NULL;
478
479         return 0;
480 }
481
482
483 /****************************** Some noise ******************************/
484
485
486 static struct usb_composite_driver multi_driver = {
487         .name           = "g_multi",
488         .dev            = &device_desc,
489         .strings        = dev_strings,
490         .max_speed      = USB_SPEED_HIGH,
491         .bind           = multi_bind,
492         .unbind         = multi_unbind,
493         .needs_serial   = 1,
494 };
495
496 module_usb_composite_driver(multi_driver);