162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/kernel.h>
362306a36Sopenharmony_ci#include <linux/slab.h>
462306a36Sopenharmony_ci#include <linux/module.h>
562306a36Sopenharmony_ci#include <linux/err.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/usb/composite.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistatic LIST_HEAD(func_list);
1062306a36Sopenharmony_cistatic DEFINE_MUTEX(func_lock);
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistatic struct usb_function_instance *try_get_usb_function_instance(const char *name)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	struct usb_function_driver *fd;
1562306a36Sopenharmony_ci	struct usb_function_instance *fi;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci	fi = ERR_PTR(-ENOENT);
1862306a36Sopenharmony_ci	mutex_lock(&func_lock);
1962306a36Sopenharmony_ci	list_for_each_entry(fd, &func_list, list) {
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci		if (strcmp(name, fd->name))
2262306a36Sopenharmony_ci			continue;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci		if (!try_module_get(fd->mod)) {
2562306a36Sopenharmony_ci			fi = ERR_PTR(-EBUSY);
2662306a36Sopenharmony_ci			break;
2762306a36Sopenharmony_ci		}
2862306a36Sopenharmony_ci		fi = fd->alloc_inst();
2962306a36Sopenharmony_ci		if (IS_ERR(fi))
3062306a36Sopenharmony_ci			module_put(fd->mod);
3162306a36Sopenharmony_ci		else
3262306a36Sopenharmony_ci			fi->fd = fd;
3362306a36Sopenharmony_ci		break;
3462306a36Sopenharmony_ci	}
3562306a36Sopenharmony_ci	mutex_unlock(&func_lock);
3662306a36Sopenharmony_ci	return fi;
3762306a36Sopenharmony_ci}
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistruct usb_function_instance *usb_get_function_instance(const char *name)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	struct usb_function_instance *fi;
4262306a36Sopenharmony_ci	int ret;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	fi = try_get_usb_function_instance(name);
4562306a36Sopenharmony_ci	if (!IS_ERR(fi))
4662306a36Sopenharmony_ci		return fi;
4762306a36Sopenharmony_ci	ret = PTR_ERR(fi);
4862306a36Sopenharmony_ci	if (ret != -ENOENT)
4962306a36Sopenharmony_ci		return fi;
5062306a36Sopenharmony_ci	ret = request_module("usbfunc:%s", name);
5162306a36Sopenharmony_ci	if (ret < 0)
5262306a36Sopenharmony_ci		return ERR_PTR(ret);
5362306a36Sopenharmony_ci	return try_get_usb_function_instance(name);
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_function_instance);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct usb_function *usb_get_function(struct usb_function_instance *fi)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct usb_function *f;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	f = fi->fd->alloc_func(fi);
6262306a36Sopenharmony_ci	if (IS_ERR(f))
6362306a36Sopenharmony_ci		return f;
6462306a36Sopenharmony_ci	f->fi = fi;
6562306a36Sopenharmony_ci	return f;
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_function);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_civoid usb_put_function_instance(struct usb_function_instance *fi)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct module *mod;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	if (!fi)
7462306a36Sopenharmony_ci		return;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	mod = fi->fd->mod;
7762306a36Sopenharmony_ci	fi->free_func_inst(fi);
7862306a36Sopenharmony_ci	module_put(mod);
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_put_function_instance);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_civoid usb_put_function(struct usb_function *f)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	if (!f)
8562306a36Sopenharmony_ci		return;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	f->free_func(f);
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_put_function);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciint usb_function_register(struct usb_function_driver *newf)
9262306a36Sopenharmony_ci{
9362306a36Sopenharmony_ci	struct usb_function_driver *fd;
9462306a36Sopenharmony_ci	int ret;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	ret = -EEXIST;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	mutex_lock(&func_lock);
9962306a36Sopenharmony_ci	list_for_each_entry(fd, &func_list, list) {
10062306a36Sopenharmony_ci		if (!strcmp(fd->name, newf->name))
10162306a36Sopenharmony_ci			goto out;
10262306a36Sopenharmony_ci	}
10362306a36Sopenharmony_ci	ret = 0;
10462306a36Sopenharmony_ci	list_add_tail(&newf->list, &func_list);
10562306a36Sopenharmony_ciout:
10662306a36Sopenharmony_ci	mutex_unlock(&func_lock);
10762306a36Sopenharmony_ci	return ret;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_function_register);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_civoid usb_function_unregister(struct usb_function_driver *fd)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	mutex_lock(&func_lock);
11462306a36Sopenharmony_ci	list_del(&fd->list);
11562306a36Sopenharmony_ci	mutex_unlock(&func_lock);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_function_unregister);
118