]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/usb/gadget/legacy/g_ffs.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / legacy / g_ffs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * g_ffs.c -- user mode file system API for USB composite function controllers
4 *
5 * Copyright (C) 2010 Samsung Electronics
6 * Author: Michal Nazarewicz <mina86@mina86.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14 #define pr_fmt(fmt) "g_ffs: " fmt
15
16 #include <linux/module.h>
17
18 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
19 #include <linux/netdevice.h>
20
21 # if defined USB_ETH_RNDIS
22 # undef USB_ETH_RNDIS
23 # endif
24 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
25 # define USB_ETH_RNDIS y
26 # endif
27
28 # include "u_ecm.h"
29 # include "u_gether.h"
30 # ifdef USB_ETH_RNDIS
31 # include "u_rndis.h"
32 # include "rndis.h"
33 # endif
34 # include "u_ether.h"
35
36 USB_ETHERNET_MODULE_PARAMETERS();
37
38 # ifdef CONFIG_USB_FUNCTIONFS_ETH
39 static int eth_bind_config(struct usb_configuration *c);
40 static struct usb_function_instance *fi_ecm;
41 static struct usb_function *f_ecm;
42 static struct usb_function_instance *fi_geth;
43 static struct usb_function *f_geth;
44 # endif
45 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
46 static int bind_rndis_config(struct usb_configuration *c);
47 static struct usb_function_instance *fi_rndis;
48 static struct usb_function *f_rndis;
49 # endif
50 #endif
51
52 #include "u_fs.h"
53
54 #define DRIVER_NAME "g_ffs"
55 #define DRIVER_DESC "USB Function Filesystem"
56 #define DRIVER_VERSION "24 Aug 2004"
57
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_AUTHOR("Michal Nazarewicz");
60 MODULE_LICENSE("GPL");
61
62 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
63 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
64
65 #define GFS_MAX_DEVS 10
66
67 USB_GADGET_COMPOSITE_OPTIONS();
68
69 static struct usb_device_descriptor gfs_dev_desc = {
70 .bLength = sizeof gfs_dev_desc,
71 .bDescriptorType = USB_DT_DEVICE,
72
73 /* .bcdUSB = DYNAMIC */
74 .bDeviceClass = USB_CLASS_PER_INTERFACE,
75
76 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
77 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
78 };
79
80 static char *func_names[GFS_MAX_DEVS];
81 static unsigned int func_num;
82
83 module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
84 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
85 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
86 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
87 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
88 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
89 module_param_array_named(functions, func_names, charp, &func_num, 0);
90 MODULE_PARM_DESC(functions, "USB Functions list");
91
92 static const struct usb_descriptor_header *gfs_otg_desc[2];
93
94 /* String IDs are assigned dynamically */
95 static struct usb_string gfs_strings[] = {
96 [USB_GADGET_MANUFACTURER_IDX].s = "",
97 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
98 [USB_GADGET_SERIAL_IDX].s = "",
99 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
100 { .s = "FunctionFS + RNDIS" },
101 #endif
102 #ifdef CONFIG_USB_FUNCTIONFS_ETH
103 { .s = "FunctionFS + ECM" },
104 #endif
105 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
106 { .s = "FunctionFS" },
107 #endif
108 { } /* end of list */
109 };
110
111 static struct usb_gadget_strings *gfs_dev_strings[] = {
112 &(struct usb_gadget_strings) {
113 .language = 0x0409, /* en-us */
114 .strings = gfs_strings,
115 },
116 NULL,
117 };
118
119 struct gfs_configuration {
120 struct usb_configuration c;
121 int (*eth)(struct usb_configuration *c);
122 int num;
123 };
124
125 static struct gfs_configuration gfs_configurations[] = {
126 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
127 {
128 .eth = bind_rndis_config,
129 },
130 #endif
131
132 #ifdef CONFIG_USB_FUNCTIONFS_ETH
133 {
134 .eth = eth_bind_config,
135 },
136 #endif
137
138 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
139 {
140 },
141 #endif
142 };
143
144 static void *functionfs_acquire_dev(struct ffs_dev *dev);
145 static void functionfs_release_dev(struct ffs_dev *dev);
146 static int functionfs_ready_callback(struct ffs_data *ffs);
147 static void functionfs_closed_callback(struct ffs_data *ffs);
148 static int gfs_bind(struct usb_composite_dev *cdev);
149 static int gfs_unbind(struct usb_composite_dev *cdev);
150 static int gfs_do_config(struct usb_configuration *c);
151
152
153 static struct usb_composite_driver gfs_driver = {
154 .name = DRIVER_NAME,
155 .dev = &gfs_dev_desc,
156 .strings = gfs_dev_strings,
157 .max_speed = USB_SPEED_HIGH,
158 .bind = gfs_bind,
159 .unbind = gfs_unbind,
160 };
161
162 static unsigned int missing_funcs;
163 static bool gfs_registered;
164 static bool gfs_single_func;
165 static struct usb_function_instance **fi_ffs;
166 static struct usb_function **f_ffs[] = {
167 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
168 NULL,
169 #endif
170
171 #ifdef CONFIG_USB_FUNCTIONFS_ETH
172 NULL,
173 #endif
174
175 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
176 NULL,
177 #endif
178 };
179
180 #define N_CONF ARRAY_SIZE(f_ffs)
181
182 static int __init gfs_init(void)
183 {
184 struct f_fs_opts *opts;
185 int i;
186 int ret = 0;
187
188 ENTER();
189
190 if (func_num < 2) {
191 gfs_single_func = true;
192 func_num = 1;
193 }
194
195 /*
196 * Allocate in one chunk for easier maintenance
197 */
198 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
199 if (!f_ffs[0]) {
200 ret = -ENOMEM;
201 goto no_func;
202 }
203 for (i = 1; i < N_CONF; ++i)
204 f_ffs[i] = f_ffs[0] + i * func_num;
205
206 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
207 if (!fi_ffs) {
208 ret = -ENOMEM;
209 goto no_func;
210 }
211
212 for (i = 0; i < func_num; i++) {
213 fi_ffs[i] = usb_get_function_instance("ffs");
214 if (IS_ERR(fi_ffs[i])) {
215 ret = PTR_ERR(fi_ffs[i]);
216 --i;
217 goto no_dev;
218 }
219 opts = to_f_fs_opts(fi_ffs[i]);
220 if (gfs_single_func)
221 ret = ffs_single_dev(opts->dev);
222 else
223 ret = ffs_name_dev(opts->dev, func_names[i]);
224 if (ret)
225 goto no_dev;
226 opts->dev->ffs_ready_callback = functionfs_ready_callback;
227 opts->dev->ffs_closed_callback = functionfs_closed_callback;
228 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
229 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
230 opts->no_configfs = true;
231 }
232
233 missing_funcs = func_num;
234
235 return 0;
236 no_dev:
237 while (i >= 0)
238 usb_put_function_instance(fi_ffs[i--]);
239 kfree(fi_ffs);
240 no_func:
241 kfree(f_ffs[0]);
242 return ret;
243 }
244 module_init(gfs_init);
245
246 static void __exit gfs_exit(void)
247 {
248 int i;
249
250 ENTER();
251
252 if (gfs_registered)
253 usb_composite_unregister(&gfs_driver);
254 gfs_registered = false;
255
256 kfree(f_ffs[0]);
257
258 for (i = 0; i < func_num; i++)
259 usb_put_function_instance(fi_ffs[i]);
260
261 kfree(fi_ffs);
262 }
263 module_exit(gfs_exit);
264
265 static void *functionfs_acquire_dev(struct ffs_dev *dev)
266 {
267 if (!try_module_get(THIS_MODULE))
268 return ERR_PTR(-ENOENT);
269
270 return NULL;
271 }
272
273 static void functionfs_release_dev(struct ffs_dev *dev)
274 {
275 module_put(THIS_MODULE);
276 }
277
278 /*
279 * The caller of this function takes ffs_lock
280 */
281 static int functionfs_ready_callback(struct ffs_data *ffs)
282 {
283 int ret = 0;
284
285 if (--missing_funcs)
286 return 0;
287
288 if (gfs_registered)
289 return -EBUSY;
290
291 gfs_registered = true;
292
293 ret = usb_composite_probe(&gfs_driver);
294 if (unlikely(ret < 0)) {
295 ++missing_funcs;
296 gfs_registered = false;
297 }
298
299 return ret;
300 }
301
302 /*
303 * The caller of this function takes ffs_lock
304 */
305 static void functionfs_closed_callback(struct ffs_data *ffs)
306 {
307 missing_funcs++;
308
309 if (gfs_registered)
310 usb_composite_unregister(&gfs_driver);
311 gfs_registered = false;
312 }
313
314 /*
315 * It is assumed that gfs_bind is called from a context where ffs_lock is held
316 */
317 static int gfs_bind(struct usb_composite_dev *cdev)
318 {
319 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
320 struct net_device *net;
321 #endif
322 int ret, i;
323
324 ENTER();
325
326 if (missing_funcs)
327 return -ENODEV;
328 #if defined CONFIG_USB_FUNCTIONFS_ETH
329 if (can_support_ecm(cdev->gadget)) {
330 struct f_ecm_opts *ecm_opts;
331
332 fi_ecm = usb_get_function_instance("ecm");
333 if (IS_ERR(fi_ecm))
334 return PTR_ERR(fi_ecm);
335 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
336 net = ecm_opts->net;
337 } else {
338 struct f_gether_opts *geth_opts;
339
340 fi_geth = usb_get_function_instance("geth");
341 if (IS_ERR(fi_geth))
342 return PTR_ERR(fi_geth);
343 geth_opts = container_of(fi_geth, struct f_gether_opts,
344 func_inst);
345 net = geth_opts->net;
346 }
347 #endif
348
349 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
350 {
351 fi_rndis = usb_get_function_instance("rndis");
352 if (IS_ERR(fi_rndis)) {
353 ret = PTR_ERR(fi_rndis);
354 goto error;
355 }
356 #ifndef CONFIG_USB_FUNCTIONFS_ETH
357 net = container_of(fi_rndis, struct f_rndis_opts,
358 func_inst)->net;
359 #endif
360 }
361 #endif
362
363 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
364 gether_set_qmult(net, qmult);
365 if (!gether_set_host_addr(net, host_addr))
366 pr_info("using host ethernet address: %s", host_addr);
367 if (!gether_set_dev_addr(net, dev_addr))
368 pr_info("using self ethernet address: %s", dev_addr);
369 #endif
370
371 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
372 gether_set_gadget(net, cdev->gadget);
373 ret = gether_register_netdev(net);
374 if (ret)
375 goto error_rndis;
376
377 if (can_support_ecm(cdev->gadget)) {
378 struct f_ecm_opts *ecm_opts;
379
380 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
381 ecm_opts->bound = true;
382 } else {
383 struct f_gether_opts *geth_opts;
384
385 geth_opts = container_of(fi_geth, struct f_gether_opts,
386 func_inst);
387 geth_opts->bound = true;
388 }
389
390 rndis_borrow_net(fi_rndis, net);
391 #endif
392
393 /* TODO: gstrings_attach? */
394 ret = usb_string_ids_tab(cdev, gfs_strings);
395 if (unlikely(ret < 0))
396 goto error_rndis;
397 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
398
399 if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
400 struct usb_descriptor_header *usb_desc;
401
402 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
403 if (!usb_desc)
404 goto error_rndis;
405 usb_otg_descriptor_init(cdev->gadget, usb_desc);
406 gfs_otg_desc[0] = usb_desc;
407 gfs_otg_desc[1] = NULL;
408 }
409
410 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
411 struct gfs_configuration *c = gfs_configurations + i;
412 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
413
414 c->c.label = gfs_strings[sid].s;
415 c->c.iConfiguration = gfs_strings[sid].id;
416 c->c.bConfigurationValue = 1 + i;
417 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
418
419 c->num = i;
420
421 ret = usb_add_config(cdev, &c->c, gfs_do_config);
422 if (unlikely(ret < 0))
423 goto error_unbind;
424 }
425 usb_composite_overwrite_options(cdev, &coverwrite);
426 return 0;
427
428 /* TODO */
429 error_unbind:
430 kfree(gfs_otg_desc[0]);
431 gfs_otg_desc[0] = NULL;
432 error_rndis:
433 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
434 usb_put_function_instance(fi_rndis);
435 error:
436 #endif
437 #if defined CONFIG_USB_FUNCTIONFS_ETH
438 if (can_support_ecm(cdev->gadget))
439 usb_put_function_instance(fi_ecm);
440 else
441 usb_put_function_instance(fi_geth);
442 #endif
443 return ret;
444 }
445
446 /*
447 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
448 */
449 static int gfs_unbind(struct usb_composite_dev *cdev)
450 {
451 int i;
452
453 ENTER();
454
455
456 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
457 usb_put_function(f_rndis);
458 usb_put_function_instance(fi_rndis);
459 #endif
460
461 #if defined CONFIG_USB_FUNCTIONFS_ETH
462 if (can_support_ecm(cdev->gadget)) {
463 usb_put_function(f_ecm);
464 usb_put_function_instance(fi_ecm);
465 } else {
466 usb_put_function(f_geth);
467 usb_put_function_instance(fi_geth);
468 }
469 #endif
470 for (i = 0; i < N_CONF * func_num; ++i)
471 usb_put_function(*(f_ffs[0] + i));
472
473 kfree(gfs_otg_desc[0]);
474 gfs_otg_desc[0] = NULL;
475
476 return 0;
477 }
478
479 /*
480 * It is assumed that gfs_do_config is called from a context where
481 * ffs_lock is held
482 */
483 static int gfs_do_config(struct usb_configuration *c)
484 {
485 struct gfs_configuration *gc =
486 container_of(c, struct gfs_configuration, c);
487 int i;
488 int ret;
489
490 if (missing_funcs)
491 return -ENODEV;
492
493 if (gadget_is_otg(c->cdev->gadget)) {
494 c->descriptors = gfs_otg_desc;
495 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
496 }
497
498 if (gc->eth) {
499 ret = gc->eth(c);
500 if (unlikely(ret < 0))
501 return ret;
502 }
503
504 for (i = 0; i < func_num; i++) {
505 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
506 if (IS_ERR(f_ffs[gc->num][i])) {
507 ret = PTR_ERR(f_ffs[gc->num][i]);
508 goto error;
509 }
510 ret = usb_add_function(c, f_ffs[gc->num][i]);
511 if (ret < 0) {
512 usb_put_function(f_ffs[gc->num][i]);
513 goto error;
514 }
515 }
516
517 /*
518 * After previous do_configs there may be some invalid
519 * pointers in c->interface array. This happens every time
520 * a user space function with fewer interfaces than a user
521 * space function that was run before the new one is run. The
522 * compasit's set_config() assumes that if there is no more
523 * then MAX_CONFIG_INTERFACES interfaces in a configuration
524 * then there is a NULL pointer after the last interface in
525 * c->interface array. We need to make sure this is true.
526 */
527 if (c->next_interface_id < ARRAY_SIZE(c->interface))
528 c->interface[c->next_interface_id] = NULL;
529
530 return 0;
531 error:
532 while (--i >= 0) {
533 if (!IS_ERR(f_ffs[gc->num][i]))
534 usb_remove_function(c, f_ffs[gc->num][i]);
535 usb_put_function(f_ffs[gc->num][i]);
536 }
537 return ret;
538 }
539
540 #ifdef CONFIG_USB_FUNCTIONFS_ETH
541
542 static int eth_bind_config(struct usb_configuration *c)
543 {
544 int status = 0;
545
546 if (can_support_ecm(c->cdev->gadget)) {
547 f_ecm = usb_get_function(fi_ecm);
548 if (IS_ERR(f_ecm))
549 return PTR_ERR(f_ecm);
550
551 status = usb_add_function(c, f_ecm);
552 if (status < 0)
553 usb_put_function(f_ecm);
554
555 } else {
556 f_geth = usb_get_function(fi_geth);
557 if (IS_ERR(f_geth))
558 return PTR_ERR(f_geth);
559
560 status = usb_add_function(c, f_geth);
561 if (status < 0)
562 usb_put_function(f_geth);
563 }
564 return status;
565 }
566
567 #endif
568
569 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
570
571 static int bind_rndis_config(struct usb_configuration *c)
572 {
573 int status = 0;
574
575 f_rndis = usb_get_function(fi_rndis);
576 if (IS_ERR(f_rndis))
577 return PTR_ERR(f_rndis);
578
579 status = usb_add_function(c, f_rndis);
580 if (status < 0)
581 usb_put_function(f_rndis);
582
583 return status;
584 }
585
586 #endif