1 // SPDX-License-Identifier: GPL-2.0
3 * module.c - module sysfs fun for drivers
5 #include <linux/device.h>
6 #include <linux/module.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/string.h>
12 static char *make_driver_name(const struct device_driver
*drv
)
16 driver_name
= kasprintf(GFP_KERNEL
, "%s:%s", drv
->bus
->name
, drv
->name
);
23 static void module_create_drivers_dir(struct module_kobject
*mk
)
25 static DEFINE_MUTEX(drivers_dir_mutex
);
27 mutex_lock(&drivers_dir_mutex
);
28 if (mk
&& !mk
->drivers_dir
)
29 mk
->drivers_dir
= kobject_create_and_add("drivers", &mk
->kobj
);
30 mutex_unlock(&drivers_dir_mutex
);
33 int module_add_driver(struct module
*mod
, const struct device_driver
*drv
)
36 struct module_kobject
*mk
= NULL
;
44 else if (drv
->mod_name
) {
45 struct kobject
*mkobj
;
47 /* Lookup built-in module entry in /sys/modules */
48 mkobj
= kset_find_obj(module_kset
, drv
->mod_name
);
50 mk
= container_of(mkobj
, struct module_kobject
, kobj
);
51 /* remember our module structure */
53 /* kset_find_obj took a reference */
61 ret
= sysfs_create_link(&drv
->p
->kobj
, &mk
->kobj
, "module");
65 driver_name
= make_driver_name(drv
);
71 module_create_drivers_dir(mk
);
72 if (!mk
->drivers_dir
) {
74 goto out_free_driver_name
;
77 ret
= sysfs_create_link(mk
->drivers_dir
, &drv
->p
->kobj
, driver_name
);
79 goto out_remove_drivers_dir
;
85 out_remove_drivers_dir
:
86 sysfs_remove_link(mk
->drivers_dir
, driver_name
);
92 sysfs_remove_link(&drv
->p
->kobj
, "module");
96 void module_remove_driver(const struct device_driver
*drv
)
98 struct module_kobject
*mk
= NULL
;
104 sysfs_remove_link(&drv
->p
->kobj
, "module");
107 mk
= &drv
->owner
->mkobj
;
108 else if (drv
->p
->mkobj
)
110 if (mk
&& mk
->drivers_dir
) {
111 driver_name
= make_driver_name(drv
);
113 sysfs_remove_link(mk
->drivers_dir
, driver_name
);