1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c63469a3SGreg Kroah-Hartman /*
3c63469a3SGreg Kroah-Hartman * module.c - module sysfs fun for drivers
4c63469a3SGreg Kroah-Hartman */
5c63469a3SGreg Kroah-Hartman #include <linux/device.h>
6c63469a3SGreg Kroah-Hartman #include <linux/module.h>
7c63469a3SGreg Kroah-Hartman #include <linux/errno.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
9c63469a3SGreg Kroah-Hartman #include <linux/string.h>
10c63469a3SGreg Kroah-Hartman #include "base.h"
11c63469a3SGreg Kroah-Hartman
make_driver_name(const struct device_driver * drv)1267c1ba55SGreg Kroah-Hartman static char *make_driver_name(const struct device_driver *drv)
13c63469a3SGreg Kroah-Hartman {
14c63469a3SGreg Kroah-Hartman char *driver_name;
15c63469a3SGreg Kroah-Hartman
161653268bSJulia Lawall driver_name = kasprintf(GFP_KERNEL, "%s:%s", drv->bus->name, drv->name);
17c63469a3SGreg Kroah-Hartman if (!driver_name)
18c63469a3SGreg Kroah-Hartman return NULL;
19c63469a3SGreg Kroah-Hartman
20c63469a3SGreg Kroah-Hartman return driver_name;
21c63469a3SGreg Kroah-Hartman }
22c63469a3SGreg Kroah-Hartman
module_create_drivers_dir(struct module_kobject * mk)23c63469a3SGreg Kroah-Hartman static void module_create_drivers_dir(struct module_kobject *mk)
24c63469a3SGreg Kroah-Hartman {
257e1b1fc4SJiri Slaby static DEFINE_MUTEX(drivers_dir_mutex);
26c63469a3SGreg Kroah-Hartman
277e1b1fc4SJiri Slaby mutex_lock(&drivers_dir_mutex);
287e1b1fc4SJiri Slaby if (mk && !mk->drivers_dir)
29c63469a3SGreg Kroah-Hartman mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
307e1b1fc4SJiri Slaby mutex_unlock(&drivers_dir_mutex);
31c63469a3SGreg Kroah-Hartman }
32c63469a3SGreg Kroah-Hartman
module_add_driver(struct module * mod,const struct device_driver * drv)3367c1ba55SGreg Kroah-Hartman int module_add_driver(struct module *mod, const struct device_driver *drv)
34c63469a3SGreg Kroah-Hartman {
35c63469a3SGreg Kroah-Hartman char *driver_name;
36c63469a3SGreg Kroah-Hartman struct module_kobject *mk = NULL;
3785d2b0aaSArnd Bergmann int ret;
38c63469a3SGreg Kroah-Hartman
39c63469a3SGreg Kroah-Hartman if (!drv)
4085d2b0aaSArnd Bergmann return 0;
41c63469a3SGreg Kroah-Hartman
42c63469a3SGreg Kroah-Hartman if (mod)
43c63469a3SGreg Kroah-Hartman mk = &mod->mkobj;
44c63469a3SGreg Kroah-Hartman else if (drv->mod_name) {
45c63469a3SGreg Kroah-Hartman struct kobject *mkobj;
46c63469a3SGreg Kroah-Hartman
47c63469a3SGreg Kroah-Hartman /* Lookup built-in module entry in /sys/modules */
48c63469a3SGreg Kroah-Hartman mkobj = kset_find_obj(module_kset, drv->mod_name);
49c63469a3SGreg Kroah-Hartman if (mkobj) {
50c63469a3SGreg Kroah-Hartman mk = container_of(mkobj, struct module_kobject, kobj);
51c63469a3SGreg Kroah-Hartman /* remember our module structure */
52e5dd1278SGreg Kroah-Hartman drv->p->mkobj = mk;
53c63469a3SGreg Kroah-Hartman /* kset_find_obj took a reference */
54c63469a3SGreg Kroah-Hartman kobject_put(mkobj);
55c63469a3SGreg Kroah-Hartman }
56c63469a3SGreg Kroah-Hartman }
57c63469a3SGreg Kroah-Hartman
58c63469a3SGreg Kroah-Hartman if (!mk)
5985d2b0aaSArnd Bergmann return 0;
60c63469a3SGreg Kroah-Hartman
6185d2b0aaSArnd Bergmann ret = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
6285d2b0aaSArnd Bergmann if (ret)
6385d2b0aaSArnd Bergmann return ret;
6485d2b0aaSArnd Bergmann
65c63469a3SGreg Kroah-Hartman driver_name = make_driver_name(drv);
6685d2b0aaSArnd Bergmann if (!driver_name) {
6785d2b0aaSArnd Bergmann ret = -ENOMEM;
68*18ec12c9SJinjie Ruan goto out_remove_kobj;
69c63469a3SGreg Kroah-Hartman }
7085d2b0aaSArnd Bergmann
7185d2b0aaSArnd Bergmann module_create_drivers_dir(mk);
7285d2b0aaSArnd Bergmann if (!mk->drivers_dir) {
7385d2b0aaSArnd Bergmann ret = -EINVAL;
74*18ec12c9SJinjie Ruan goto out_free_driver_name;
7585d2b0aaSArnd Bergmann }
7685d2b0aaSArnd Bergmann
7785d2b0aaSArnd Bergmann ret = sysfs_create_link(mk->drivers_dir, &drv->p->kobj, driver_name);
7885d2b0aaSArnd Bergmann if (ret)
79*18ec12c9SJinjie Ruan goto out_remove_drivers_dir;
8085d2b0aaSArnd Bergmann
8185d2b0aaSArnd Bergmann kfree(driver_name);
8285d2b0aaSArnd Bergmann
8385d2b0aaSArnd Bergmann return 0;
84*18ec12c9SJinjie Ruan
85*18ec12c9SJinjie Ruan out_remove_drivers_dir:
8685d2b0aaSArnd Bergmann sysfs_remove_link(mk->drivers_dir, driver_name);
87*18ec12c9SJinjie Ruan
88*18ec12c9SJinjie Ruan out_free_driver_name:
8985d2b0aaSArnd Bergmann kfree(driver_name);
9085d2b0aaSArnd Bergmann
91*18ec12c9SJinjie Ruan out_remove_kobj:
92*18ec12c9SJinjie Ruan sysfs_remove_link(&drv->p->kobj, "module");
9385d2b0aaSArnd Bergmann return ret;
94c63469a3SGreg Kroah-Hartman }
95c63469a3SGreg Kroah-Hartman
module_remove_driver(const struct device_driver * drv)9667c1ba55SGreg Kroah-Hartman void module_remove_driver(const struct device_driver *drv)
97c63469a3SGreg Kroah-Hartman {
98c63469a3SGreg Kroah-Hartman struct module_kobject *mk = NULL;
99c63469a3SGreg Kroah-Hartman char *driver_name;
100c63469a3SGreg Kroah-Hartman
101c63469a3SGreg Kroah-Hartman if (!drv)
102c63469a3SGreg Kroah-Hartman return;
103c63469a3SGreg Kroah-Hartman
104e5dd1278SGreg Kroah-Hartman sysfs_remove_link(&drv->p->kobj, "module");
105c63469a3SGreg Kroah-Hartman
106c63469a3SGreg Kroah-Hartman if (drv->owner)
107c63469a3SGreg Kroah-Hartman mk = &drv->owner->mkobj;
108e5dd1278SGreg Kroah-Hartman else if (drv->p->mkobj)
109e5dd1278SGreg Kroah-Hartman mk = drv->p->mkobj;
110c63469a3SGreg Kroah-Hartman if (mk && mk->drivers_dir) {
111c63469a3SGreg Kroah-Hartman driver_name = make_driver_name(drv);
112c63469a3SGreg Kroah-Hartman if (driver_name) {
113c63469a3SGreg Kroah-Hartman sysfs_remove_link(mk->drivers_dir, driver_name);
114c63469a3SGreg Kroah-Hartman kfree(driver_name);
115c63469a3SGreg Kroah-Hartman }
116c63469a3SGreg Kroah-Hartman }
117c63469a3SGreg Kroah-Hartman }
118