treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / g_ffs.c
blobae6d8f7092b8e97a0153bfc7758cca99348397a6
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * g_ffs.c -- user mode file system API for USB composite function controllers
5 * Copyright (C) 2010 Samsung Electronics
6 * Author: Michal Nazarewicz <mina86@mina86.com>
7 */
9 #define pr_fmt(fmt) "g_ffs: " fmt
11 #include <linux/module.h>
13 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
14 #include <linux/netdevice.h>
16 # if defined USB_ETH_RNDIS
17 # undef USB_ETH_RNDIS
18 # endif
19 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
20 # define USB_ETH_RNDIS y
21 # endif
23 # include "u_ecm.h"
24 # include "u_gether.h"
25 # ifdef USB_ETH_RNDIS
26 # include "u_rndis.h"
27 # include "rndis.h"
28 # endif
29 # include "u_ether.h"
31 USB_ETHERNET_MODULE_PARAMETERS();
33 # ifdef CONFIG_USB_FUNCTIONFS_ETH
34 static int eth_bind_config(struct usb_configuration *c);
35 static struct usb_function_instance *fi_ecm;
36 static struct usb_function *f_ecm;
37 static struct usb_function_instance *fi_geth;
38 static struct usb_function *f_geth;
39 # endif
40 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
41 static int bind_rndis_config(struct usb_configuration *c);
42 static struct usb_function_instance *fi_rndis;
43 static struct usb_function *f_rndis;
44 # endif
45 #endif
47 #include "u_fs.h"
49 #define DRIVER_NAME "g_ffs"
50 #define DRIVER_DESC "USB Function Filesystem"
51 #define DRIVER_VERSION "24 Aug 2004"
53 MODULE_DESCRIPTION(DRIVER_DESC);
54 MODULE_AUTHOR("Michal Nazarewicz");
55 MODULE_LICENSE("GPL");
57 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
58 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
60 #define GFS_MAX_DEVS 10
62 USB_GADGET_COMPOSITE_OPTIONS();
64 static struct usb_device_descriptor gfs_dev_desc = {
65 .bLength = sizeof gfs_dev_desc,
66 .bDescriptorType = USB_DT_DEVICE,
68 /* .bcdUSB = DYNAMIC */
69 .bDeviceClass = USB_CLASS_PER_INTERFACE,
71 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
72 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
75 static char *func_names[GFS_MAX_DEVS];
76 static unsigned int func_num;
78 module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
79 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
80 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
81 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
82 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
83 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
84 module_param_array_named(functions, func_names, charp, &func_num, 0);
85 MODULE_PARM_DESC(functions, "USB Functions list");
87 static const struct usb_descriptor_header *gfs_otg_desc[2];
89 /* String IDs are assigned dynamically */
90 static struct usb_string gfs_strings[] = {
91 [USB_GADGET_MANUFACTURER_IDX].s = "",
92 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
93 [USB_GADGET_SERIAL_IDX].s = "",
94 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
95 { .s = "FunctionFS + RNDIS" },
96 #endif
97 #ifdef CONFIG_USB_FUNCTIONFS_ETH
98 { .s = "FunctionFS + ECM" },
99 #endif
100 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
101 { .s = "FunctionFS" },
102 #endif
103 { } /* end of list */
106 static struct usb_gadget_strings *gfs_dev_strings[] = {
107 &(struct usb_gadget_strings) {
108 .language = 0x0409, /* en-us */
109 .strings = gfs_strings,
111 NULL,
114 struct gfs_configuration {
115 struct usb_configuration c;
116 int (*eth)(struct usb_configuration *c);
117 int num;
120 static struct gfs_configuration gfs_configurations[] = {
121 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
123 .eth = bind_rndis_config,
125 #endif
127 #ifdef CONFIG_USB_FUNCTIONFS_ETH
129 .eth = eth_bind_config,
131 #endif
133 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
136 #endif
139 static void *functionfs_acquire_dev(struct ffs_dev *dev);
140 static void functionfs_release_dev(struct ffs_dev *dev);
141 static int functionfs_ready_callback(struct ffs_data *ffs);
142 static void functionfs_closed_callback(struct ffs_data *ffs);
143 static int gfs_bind(struct usb_composite_dev *cdev);
144 static int gfs_unbind(struct usb_composite_dev *cdev);
145 static int gfs_do_config(struct usb_configuration *c);
148 static struct usb_composite_driver gfs_driver = {
149 .name = DRIVER_NAME,
150 .dev = &gfs_dev_desc,
151 .strings = gfs_dev_strings,
152 .max_speed = USB_SPEED_SUPER,
153 .bind = gfs_bind,
154 .unbind = gfs_unbind,
157 static unsigned int missing_funcs;
158 static bool gfs_registered;
159 static bool gfs_single_func;
160 static struct usb_function_instance **fi_ffs;
161 static struct usb_function **f_ffs[] = {
162 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
163 NULL,
164 #endif
166 #ifdef CONFIG_USB_FUNCTIONFS_ETH
167 NULL,
168 #endif
170 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
171 NULL,
172 #endif
175 #define N_CONF ARRAY_SIZE(f_ffs)
177 static int __init gfs_init(void)
179 struct f_fs_opts *opts;
180 int i;
181 int ret = 0;
183 ENTER();
185 if (func_num < 2) {
186 gfs_single_func = true;
187 func_num = 1;
191 * Allocate in one chunk for easier maintenance
193 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
194 if (!f_ffs[0]) {
195 ret = -ENOMEM;
196 goto no_func;
198 for (i = 1; i < N_CONF; ++i)
199 f_ffs[i] = f_ffs[0] + i * func_num;
201 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
202 if (!fi_ffs) {
203 ret = -ENOMEM;
204 goto no_func;
207 for (i = 0; i < func_num; i++) {
208 fi_ffs[i] = usb_get_function_instance("ffs");
209 if (IS_ERR(fi_ffs[i])) {
210 ret = PTR_ERR(fi_ffs[i]);
211 --i;
212 goto no_dev;
214 opts = to_f_fs_opts(fi_ffs[i]);
215 if (gfs_single_func)
216 ret = ffs_single_dev(opts->dev);
217 else
218 ret = ffs_name_dev(opts->dev, func_names[i]);
219 if (ret)
220 goto no_dev;
221 opts->dev->ffs_ready_callback = functionfs_ready_callback;
222 opts->dev->ffs_closed_callback = functionfs_closed_callback;
223 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
224 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
225 opts->no_configfs = true;
228 missing_funcs = func_num;
230 return 0;
231 no_dev:
232 while (i >= 0)
233 usb_put_function_instance(fi_ffs[i--]);
234 kfree(fi_ffs);
235 no_func:
236 kfree(f_ffs[0]);
237 return ret;
239 module_init(gfs_init);
241 static void __exit gfs_exit(void)
243 int i;
245 ENTER();
247 if (gfs_registered)
248 usb_composite_unregister(&gfs_driver);
249 gfs_registered = false;
251 kfree(f_ffs[0]);
253 for (i = 0; i < func_num; i++)
254 usb_put_function_instance(fi_ffs[i]);
256 kfree(fi_ffs);
258 module_exit(gfs_exit);
260 static void *functionfs_acquire_dev(struct ffs_dev *dev)
262 if (!try_module_get(THIS_MODULE))
263 return ERR_PTR(-ENOENT);
265 return NULL;
268 static void functionfs_release_dev(struct ffs_dev *dev)
270 module_put(THIS_MODULE);
274 * The caller of this function takes ffs_lock
276 static int functionfs_ready_callback(struct ffs_data *ffs)
278 int ret = 0;
280 if (--missing_funcs)
281 return 0;
283 if (gfs_registered)
284 return -EBUSY;
286 gfs_registered = true;
288 ret = usb_composite_probe(&gfs_driver);
289 if (unlikely(ret < 0)) {
290 ++missing_funcs;
291 gfs_registered = false;
294 return ret;
298 * The caller of this function takes ffs_lock
300 static void functionfs_closed_callback(struct ffs_data *ffs)
302 missing_funcs++;
304 if (gfs_registered)
305 usb_composite_unregister(&gfs_driver);
306 gfs_registered = false;
310 * It is assumed that gfs_bind is called from a context where ffs_lock is held
312 static int gfs_bind(struct usb_composite_dev *cdev)
314 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
315 struct net_device *net;
316 #endif
317 int ret, i;
319 ENTER();
321 if (missing_funcs)
322 return -ENODEV;
323 #if defined CONFIG_USB_FUNCTIONFS_ETH
324 if (can_support_ecm(cdev->gadget)) {
325 struct f_ecm_opts *ecm_opts;
327 fi_ecm = usb_get_function_instance("ecm");
328 if (IS_ERR(fi_ecm))
329 return PTR_ERR(fi_ecm);
330 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
331 net = ecm_opts->net;
332 } else {
333 struct f_gether_opts *geth_opts;
335 fi_geth = usb_get_function_instance("geth");
336 if (IS_ERR(fi_geth))
337 return PTR_ERR(fi_geth);
338 geth_opts = container_of(fi_geth, struct f_gether_opts,
339 func_inst);
340 net = geth_opts->net;
342 #endif
344 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
346 fi_rndis = usb_get_function_instance("rndis");
347 if (IS_ERR(fi_rndis)) {
348 ret = PTR_ERR(fi_rndis);
349 goto error;
351 #ifndef CONFIG_USB_FUNCTIONFS_ETH
352 net = container_of(fi_rndis, struct f_rndis_opts,
353 func_inst)->net;
354 #endif
356 #endif
358 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
359 gether_set_qmult(net, qmult);
360 if (!gether_set_host_addr(net, host_addr))
361 pr_info("using host ethernet address: %s", host_addr);
362 if (!gether_set_dev_addr(net, dev_addr))
363 pr_info("using self ethernet address: %s", dev_addr);
364 #endif
366 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
367 gether_set_gadget(net, cdev->gadget);
368 ret = gether_register_netdev(net);
369 if (ret)
370 goto error_rndis;
372 if (can_support_ecm(cdev->gadget)) {
373 struct f_ecm_opts *ecm_opts;
375 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
376 ecm_opts->bound = true;
377 } else {
378 struct f_gether_opts *geth_opts;
380 geth_opts = container_of(fi_geth, struct f_gether_opts,
381 func_inst);
382 geth_opts->bound = true;
385 rndis_borrow_net(fi_rndis, net);
386 #endif
388 /* TODO: gstrings_attach? */
389 ret = usb_string_ids_tab(cdev, gfs_strings);
390 if (unlikely(ret < 0))
391 goto error_rndis;
392 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
394 if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
395 struct usb_descriptor_header *usb_desc;
397 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
398 if (!usb_desc)
399 goto error_rndis;
400 usb_otg_descriptor_init(cdev->gadget, usb_desc);
401 gfs_otg_desc[0] = usb_desc;
402 gfs_otg_desc[1] = NULL;
405 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
406 struct gfs_configuration *c = gfs_configurations + i;
407 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
409 c->c.label = gfs_strings[sid].s;
410 c->c.iConfiguration = gfs_strings[sid].id;
411 c->c.bConfigurationValue = 1 + i;
412 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
414 c->num = i;
416 ret = usb_add_config(cdev, &c->c, gfs_do_config);
417 if (unlikely(ret < 0))
418 goto error_unbind;
420 usb_composite_overwrite_options(cdev, &coverwrite);
421 return 0;
423 /* TODO */
424 error_unbind:
425 kfree(gfs_otg_desc[0]);
426 gfs_otg_desc[0] = NULL;
427 error_rndis:
428 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
429 usb_put_function_instance(fi_rndis);
430 error:
431 #endif
432 #if defined CONFIG_USB_FUNCTIONFS_ETH
433 if (can_support_ecm(cdev->gadget))
434 usb_put_function_instance(fi_ecm);
435 else
436 usb_put_function_instance(fi_geth);
437 #endif
438 return ret;
442 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
444 static int gfs_unbind(struct usb_composite_dev *cdev)
446 int i;
448 ENTER();
451 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
452 usb_put_function(f_rndis);
453 usb_put_function_instance(fi_rndis);
454 #endif
456 #if defined CONFIG_USB_FUNCTIONFS_ETH
457 if (can_support_ecm(cdev->gadget)) {
458 usb_put_function(f_ecm);
459 usb_put_function_instance(fi_ecm);
460 } else {
461 usb_put_function(f_geth);
462 usb_put_function_instance(fi_geth);
464 #endif
465 for (i = 0; i < N_CONF * func_num; ++i)
466 usb_put_function(*(f_ffs[0] + i));
468 kfree(gfs_otg_desc[0]);
469 gfs_otg_desc[0] = NULL;
471 return 0;
475 * It is assumed that gfs_do_config is called from a context where
476 * ffs_lock is held
478 static int gfs_do_config(struct usb_configuration *c)
480 struct gfs_configuration *gc =
481 container_of(c, struct gfs_configuration, c);
482 int i;
483 int ret;
485 if (missing_funcs)
486 return -ENODEV;
488 if (gadget_is_otg(c->cdev->gadget)) {
489 c->descriptors = gfs_otg_desc;
490 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
493 if (gc->eth) {
494 ret = gc->eth(c);
495 if (unlikely(ret < 0))
496 return ret;
499 for (i = 0; i < func_num; i++) {
500 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
501 if (IS_ERR(f_ffs[gc->num][i])) {
502 ret = PTR_ERR(f_ffs[gc->num][i]);
503 goto error;
505 ret = usb_add_function(c, f_ffs[gc->num][i]);
506 if (ret < 0) {
507 usb_put_function(f_ffs[gc->num][i]);
508 goto error;
513 * After previous do_configs there may be some invalid
514 * pointers in c->interface array. This happens every time
515 * a user space function with fewer interfaces than a user
516 * space function that was run before the new one is run. The
517 * compasit's set_config() assumes that if there is no more
518 * then MAX_CONFIG_INTERFACES interfaces in a configuration
519 * then there is a NULL pointer after the last interface in
520 * c->interface array. We need to make sure this is true.
522 if (c->next_interface_id < ARRAY_SIZE(c->interface))
523 c->interface[c->next_interface_id] = NULL;
525 return 0;
526 error:
527 while (--i >= 0) {
528 if (!IS_ERR(f_ffs[gc->num][i]))
529 usb_remove_function(c, f_ffs[gc->num][i]);
530 usb_put_function(f_ffs[gc->num][i]);
532 return ret;
535 #ifdef CONFIG_USB_FUNCTIONFS_ETH
537 static int eth_bind_config(struct usb_configuration *c)
539 int status = 0;
541 if (can_support_ecm(c->cdev->gadget)) {
542 f_ecm = usb_get_function(fi_ecm);
543 if (IS_ERR(f_ecm))
544 return PTR_ERR(f_ecm);
546 status = usb_add_function(c, f_ecm);
547 if (status < 0)
548 usb_put_function(f_ecm);
550 } else {
551 f_geth = usb_get_function(fi_geth);
552 if (IS_ERR(f_geth))
553 return PTR_ERR(f_geth);
555 status = usb_add_function(c, f_geth);
556 if (status < 0)
557 usb_put_function(f_geth);
559 return status;
562 #endif
564 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
566 static int bind_rndis_config(struct usb_configuration *c)
568 int status = 0;
570 f_rndis = usb_get_function(fi_rndis);
571 if (IS_ERR(f_rndis))
572 return PTR_ERR(f_rndis);
574 status = usb_add_function(c, f_rndis);
575 if (status < 0)
576 usb_put_function(f_rndis);
578 return status;
581 #endif