1 // SPDX-License-Identifier: GPL-2.0+
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>
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.
14 #define pr_fmt(fmt) "g_ffs: " fmt
16 #include <linux/module.h>
18 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
19 #include <linux/netdevice.h>
21 # if defined USB_ETH_RNDIS
24 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
25 # define USB_ETH_RNDIS y
29 # include "u_gether.h"
36 USB_ETHERNET_MODULE_PARAMETERS();
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
;
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
;
54 #define DRIVER_NAME "g_ffs"
55 #define DRIVER_DESC "USB Function Filesystem"
56 #define DRIVER_VERSION "24 Aug 2004"
58 MODULE_DESCRIPTION(DRIVER_DESC
);
59 MODULE_AUTHOR("Michal Nazarewicz");
60 MODULE_LICENSE("GPL");
62 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
63 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
65 #define GFS_MAX_DEVS 10
67 USB_GADGET_COMPOSITE_OPTIONS();
69 static struct usb_device_descriptor gfs_dev_desc
= {
70 .bLength
= sizeof gfs_dev_desc
,
71 .bDescriptorType
= USB_DT_DEVICE
,
73 /* .bcdUSB = DYNAMIC */
74 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
76 .idVendor
= cpu_to_le16(GFS_VENDOR_ID
),
77 .idProduct
= cpu_to_le16(GFS_PRODUCT_ID
),
80 static char *func_names
[GFS_MAX_DEVS
];
81 static unsigned int func_num
;
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");
92 static const struct usb_descriptor_header
*gfs_otg_desc
[2];
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" },
102 #ifdef CONFIG_USB_FUNCTIONFS_ETH
103 { .s
= "FunctionFS + ECM" },
105 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
106 { .s
= "FunctionFS" },
108 { } /* end of list */
111 static struct usb_gadget_strings
*gfs_dev_strings
[] = {
112 &(struct usb_gadget_strings
) {
113 .language
= 0x0409, /* en-us */
114 .strings
= gfs_strings
,
119 struct gfs_configuration
{
120 struct usb_configuration c
;
121 int (*eth
)(struct usb_configuration
*c
);
125 static struct gfs_configuration gfs_configurations
[] = {
126 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
128 .eth
= bind_rndis_config
,
132 #ifdef CONFIG_USB_FUNCTIONFS_ETH
134 .eth
= eth_bind_config
,
138 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
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
);
153 static struct usb_composite_driver gfs_driver
= {
155 .dev
= &gfs_dev_desc
,
156 .strings
= gfs_dev_strings
,
157 .max_speed
= USB_SPEED_HIGH
,
159 .unbind
= gfs_unbind
,
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
171 #ifdef CONFIG_USB_FUNCTIONFS_ETH
175 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
180 #define N_CONF ARRAY_SIZE(f_ffs)
182 static int __init
gfs_init(void)
184 struct f_fs_opts
*opts
;
191 gfs_single_func
= true;
196 * Allocate in one chunk for easier maintenance
198 f_ffs
[0] = kcalloc(func_num
* N_CONF
, sizeof(*f_ffs
), GFP_KERNEL
);
203 for (i
= 1; i
< N_CONF
; ++i
)
204 f_ffs
[i
] = f_ffs
[0] + i
* func_num
;
206 fi_ffs
= kcalloc(func_num
, sizeof(*fi_ffs
), GFP_KERNEL
);
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
]);
219 opts
= to_f_fs_opts(fi_ffs
[i
]);
221 ret
= ffs_single_dev(opts
->dev
);
223 ret
= ffs_name_dev(opts
->dev
, func_names
[i
]);
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;
233 missing_funcs
= func_num
;
238 usb_put_function_instance(fi_ffs
[i
--]);
244 module_init(gfs_init
);
246 static void __exit
gfs_exit(void)
253 usb_composite_unregister(&gfs_driver
);
254 gfs_registered
= false;
258 for (i
= 0; i
< func_num
; i
++)
259 usb_put_function_instance(fi_ffs
[i
]);
263 module_exit(gfs_exit
);
265 static void *functionfs_acquire_dev(struct ffs_dev
*dev
)
267 if (!try_module_get(THIS_MODULE
))
268 return ERR_PTR(-ENOENT
);
273 static void functionfs_release_dev(struct ffs_dev
*dev
)
275 module_put(THIS_MODULE
);
279 * The caller of this function takes ffs_lock
281 static int functionfs_ready_callback(struct ffs_data
*ffs
)
291 gfs_registered
= true;
293 ret
= usb_composite_probe(&gfs_driver
);
294 if (unlikely(ret
< 0)) {
296 gfs_registered
= false;
303 * The caller of this function takes ffs_lock
305 static void functionfs_closed_callback(struct ffs_data
*ffs
)
310 usb_composite_unregister(&gfs_driver
);
311 gfs_registered
= false;
315 * It is assumed that gfs_bind is called from a context where ffs_lock is held
317 static int gfs_bind(struct usb_composite_dev
*cdev
)
319 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
320 struct net_device
*net
;
328 #if defined CONFIG_USB_FUNCTIONFS_ETH
329 if (can_support_ecm(cdev
->gadget
)) {
330 struct f_ecm_opts
*ecm_opts
;
332 fi_ecm
= usb_get_function_instance("ecm");
334 return PTR_ERR(fi_ecm
);
335 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
338 struct f_gether_opts
*geth_opts
;
340 fi_geth
= usb_get_function_instance("geth");
342 return PTR_ERR(fi_geth
);
343 geth_opts
= container_of(fi_geth
, struct f_gether_opts
,
345 net
= geth_opts
->net
;
349 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
351 fi_rndis
= usb_get_function_instance("rndis");
352 if (IS_ERR(fi_rndis
)) {
353 ret
= PTR_ERR(fi_rndis
);
356 #ifndef CONFIG_USB_FUNCTIONFS_ETH
357 net
= container_of(fi_rndis
, struct f_rndis_opts
,
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
);
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
);
377 if (can_support_ecm(cdev
->gadget
)) {
378 struct f_ecm_opts
*ecm_opts
;
380 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
381 ecm_opts
->bound
= true;
383 struct f_gether_opts
*geth_opts
;
385 geth_opts
= container_of(fi_geth
, struct f_gether_opts
,
387 geth_opts
->bound
= true;
390 rndis_borrow_net(fi_rndis
, net
);
393 /* TODO: gstrings_attach? */
394 ret
= usb_string_ids_tab(cdev
, gfs_strings
);
395 if (unlikely(ret
< 0))
397 gfs_dev_desc
.iProduct
= gfs_strings
[USB_GADGET_PRODUCT_IDX
].id
;
399 if (gadget_is_otg(cdev
->gadget
) && !gfs_otg_desc
[0]) {
400 struct usb_descriptor_header
*usb_desc
;
402 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
405 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
406 gfs_otg_desc
[0] = usb_desc
;
407 gfs_otg_desc
[1] = NULL
;
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
;
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
;
421 ret
= usb_add_config(cdev
, &c
->c
, gfs_do_config
);
422 if (unlikely(ret
< 0))
425 usb_composite_overwrite_options(cdev
, &coverwrite
);
430 kfree(gfs_otg_desc
[0]);
431 gfs_otg_desc
[0] = NULL
;
433 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
434 usb_put_function_instance(fi_rndis
);
437 #if defined CONFIG_USB_FUNCTIONFS_ETH
438 if (can_support_ecm(cdev
->gadget
))
439 usb_put_function_instance(fi_ecm
);
441 usb_put_function_instance(fi_geth
);
447 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
449 static int gfs_unbind(struct usb_composite_dev
*cdev
)
456 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
457 usb_put_function(f_rndis
);
458 usb_put_function_instance(fi_rndis
);
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
);
466 usb_put_function(f_geth
);
467 usb_put_function_instance(fi_geth
);
470 for (i
= 0; i
< N_CONF
* func_num
; ++i
)
471 usb_put_function(*(f_ffs
[0] + i
));
473 kfree(gfs_otg_desc
[0]);
474 gfs_otg_desc
[0] = NULL
;
480 * It is assumed that gfs_do_config is called from a context where
483 static int gfs_do_config(struct usb_configuration
*c
)
485 struct gfs_configuration
*gc
=
486 container_of(c
, struct gfs_configuration
, c
);
493 if (gadget_is_otg(c
->cdev
->gadget
)) {
494 c
->descriptors
= gfs_otg_desc
;
495 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
500 if (unlikely(ret
< 0))
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
]);
510 ret
= usb_add_function(c
, f_ffs
[gc
->num
][i
]);
512 usb_put_function(f_ffs
[gc
->num
][i
]);
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.
527 if (c
->next_interface_id
< ARRAY_SIZE(c
->interface
))
528 c
->interface
[c
->next_interface_id
] = NULL
;
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
]);
540 #ifdef CONFIG_USB_FUNCTIONFS_ETH
542 static int eth_bind_config(struct usb_configuration
*c
)
546 if (can_support_ecm(c
->cdev
->gadget
)) {
547 f_ecm
= usb_get_function(fi_ecm
);
549 return PTR_ERR(f_ecm
);
551 status
= usb_add_function(c
, f_ecm
);
553 usb_put_function(f_ecm
);
556 f_geth
= usb_get_function(fi_geth
);
558 return PTR_ERR(f_geth
);
560 status
= usb_add_function(c
, f_geth
);
562 usb_put_function(f_geth
);
569 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
571 static int bind_rndis_config(struct usb_configuration
*c
)
575 f_rndis
= usb_get_function(fi_rndis
);
577 return PTR_ERR(f_rndis
);
579 status
= usb_add_function(c
, f_rndis
);
581 usb_put_function(f_rndis
);