// SPDX-License-Identifier: GPL-2.0+ /* * g_ffs.c -- user mode file system API for USB composite function controllers * * Copyright (C) 2010 Samsung Electronics * Author: Michal Nazarewicz <mina86@mina86.com>
*/
#define pr_fmt(fmt) "g_ffs: " fmt
#include <linux/module.h>
#ifdefined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS #include <linux/netdevice.h>
/* * Allocate in one chunk for easier maintenance
*/
f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs[0]), GFP_KERNEL); if (!f_ffs[0]) {
ret = -ENOMEM; goto no_func;
} for (i = 1; i < N_CONF; ++i)
f_ffs[i] = f_ffs[0] + i * func_num;
fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL); if (!fi_ffs) {
ret = -ENOMEM; goto no_func;
}
for (i = 0; i < func_num; i++) {
fi_ffs[i] = usb_get_function_instance("ffs"); if (IS_ERR(fi_ffs[i])) {
ret = PTR_ERR(fi_ffs[i]);
--i; goto no_dev;
}
opts = to_f_fs_opts(fi_ffs[i]); if (gfs_single_func)
ret = ffs_single_dev(opts->dev); else
ret = ffs_name_dev(opts->dev, func_names[i]); if (ret) goto no_dev;
opts->dev->ffs_ready_callback = functionfs_ready_callback;
opts->dev->ffs_closed_callback = functionfs_closed_callback;
opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
opts->dev->ffs_release_dev_callback = functionfs_release_dev;
opts->no_configfs = true;
}
missing_funcs = func_num;
return 0;
no_dev: while (i >= 0)
usb_put_function_instance(fi_ffs[i--]);
kfree(fi_ffs);
no_func:
kfree(f_ffs[0]); return ret;
}
module_init(gfs_init);
staticvoid __exit gfs_exit(void)
{ int i;
if (gfs_registered)
usb_composite_unregister(&gfs_driver);
gfs_registered = false;
kfree(f_ffs[0]);
for (i = 0; i < func_num; i++)
usb_put_function_instance(fi_ffs[i]);
kfree(fi_ffs);
}
module_exit(gfs_exit);
staticvoid *functionfs_acquire_dev(struct ffs_dev *dev)
{ if (!try_module_get(THIS_MODULE)) return ERR_PTR(-ENOENT);
/* * The caller of this function takes ffs_lock
*/ staticint functionfs_ready_callback(struct ffs_data *ffs)
{ int ret = 0;
if (--missing_funcs) return 0;
if (gfs_registered) return -EBUSY;
gfs_registered = true;
ret = usb_composite_probe(&gfs_driver); if (unlikely(ret < 0)) {
++missing_funcs;
gfs_registered = false;
}
return ret;
}
/* * The caller of this function takes ffs_lock
*/ staticvoid functionfs_closed_callback(struct ffs_data *ffs)
{
missing_funcs++;
if (gfs_registered)
usb_composite_unregister(&gfs_driver);
gfs_registered = false;
}
/* * It is assumed that gfs_bind is called from a context where ffs_lock is held
*/ staticint gfs_bind(struct usb_composite_dev *cdev)
{ #ifdefined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS struct net_device *net; #endif int ret, i;
if (missing_funcs) return -ENODEV; #ifdefined CONFIG_USB_FUNCTIONFS_ETH if (can_support_ecm(cdev->gadget)) { struct f_ecm_opts *ecm_opts;
fi_geth = usb_get_function_instance("geth"); if (IS_ERR(fi_geth)) return PTR_ERR(fi_geth);
geth_opts = container_of(fi_geth, struct f_gether_opts,
func_inst);
net = geth_opts->net;
} #endif
#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
{
fi_rndis = usb_get_function_instance("rndis"); if (IS_ERR(fi_rndis)) {
ret = PTR_ERR(fi_rndis); goto error;
} #ifndef CONFIG_USB_FUNCTIONFS_ETH
net = container_of(fi_rndis, struct f_rndis_opts,
func_inst)->net; #endif
} #endif
#ifdefined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
gether_set_qmult(net, qmult); if (!gether_set_host_addr(net, host_addr))
pr_info("using host ethernet address: %s", host_addr); if (!gether_set_dev_addr(net, dev_addr))
pr_info("using self ethernet address: %s", dev_addr); #endif
#ifdefined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
gether_set_gadget(net, cdev->gadget);
ret = gether_register_netdev(net); if (ret) goto error_rndis;
if (can_support_ecm(cdev->gadget)) { struct f_ecm_opts *ecm_opts;
#ifdefined CONFIG_USB_FUNCTIONFS_ETH if (can_support_ecm(cdev->gadget)) {
usb_put_function(f_ecm);
usb_put_function_instance(fi_ecm);
} else {
usb_put_function(f_geth);
usb_put_function_instance(fi_geth);
} #endif for (i = 0; i < N_CONF * func_num; ++i)
usb_put_function(*(f_ffs[0] + i));
kfree(gfs_otg_desc[0]);
gfs_otg_desc[0] = NULL;
return 0;
}
/* * It is assumed that gfs_do_config is called from a context where * ffs_lock is held
*/ staticint gfs_do_config(struct usb_configuration *c)
{ struct gfs_configuration *gc =
container_of(c, struct gfs_configuration, c); int i; int ret;
if (missing_funcs) return -ENODEV;
if (gadget_is_otg(c->cdev->gadget)) {
c->descriptors = gfs_otg_desc;
c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
}
if (gc->eth) {
ret = gc->eth(c); if (unlikely(ret < 0)) return ret;
}
for (i = 0; i < func_num; i++) {
f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]); if (IS_ERR(f_ffs[gc->num][i])) {
ret = PTR_ERR(f_ffs[gc->num][i]); goto error;
}
ret = usb_add_function(c, f_ffs[gc->num][i]); if (ret < 0) {
usb_put_function(f_ffs[gc->num][i]); goto error;
}
}
/* * After previous do_configs there may be some invalid * pointers in c->interface array. This happens every time * a user space function with fewer interfaces than a user * space function that was run before the new one is run. The * compasit's set_config() assumes that if there is no more * then MAX_CONFIG_INTERFACES interfaces in a configuration * then there is a NULL pointer after the last interface in * c->interface array. We need to make sure this is true.
*/ if (c->next_interface_id < ARRAY_SIZE(c->interface))
c->interface[c->next_interface_id] = NULL;
return 0;
error: while (--i >= 0) { if (!IS_ERR(f_ffs[gc->num][i]))
usb_remove_function(c, f_ffs[gc->num][i]);
usb_put_function(f_ffs[gc->num][i]);
} return ret;
}
#ifdef CONFIG_USB_FUNCTIONFS_ETH
staticint eth_bind_config(struct usb_configuration *c)
{ int status = 0;
if (can_support_ecm(c->cdev->gadget)) {
f_ecm = usb_get_function(fi_ecm); if (IS_ERR(f_ecm)) return PTR_ERR(f_ecm);
status = usb_add_function(c, f_ecm); if (status < 0)
usb_put_function(f_ecm);
} else {
f_geth = usb_get_function(fi_geth); if (IS_ERR(f_geth)) return PTR_ERR(f_geth);
status = usb_add_function(c, f_geth); if (status < 0)
usb_put_function(f_geth);
} return status;
}
#endif
#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
staticint bind_rndis_config(struct usb_configuration *c)
{ int status = 0;
f_rndis = usb_get_function(fi_rndis); if (IS_ERR(f_rndis)) return PTR_ERR(f_rndis);
status = usb_add_function(c, f_rndis); if (status < 0)
usb_put_function(f_rndis);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.