xref: /linux/drivers/base/module.c (revision 886b7e80ab19841f640cafd8b5ab053409b9b931)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * module.c - module sysfs fun for drivers
4  */
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>
10 #include "base.h"
11 
make_driver_name(const struct device_driver * drv)12 static char *make_driver_name(const struct device_driver *drv)
13 {
14 	char *driver_name;
15 
16 	driver_name = kasprintf(GFP_KERNEL, "%s:%s", drv->bus->name, drv->name);
17 	if (!driver_name)
18 		return NULL;
19 
20 	return driver_name;
21 }
22 
module_create_drivers_dir(struct module_kobject * mk)23 static void module_create_drivers_dir(struct module_kobject *mk)
24 {
25 	static DEFINE_MUTEX(drivers_dir_mutex);
26 
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);
31 }
32 
module_add_driver(struct module * mod,const struct device_driver * drv)33 int module_add_driver(struct module *mod, const struct device_driver *drv)
34 {
35 	char *driver_name;
36 	struct module_kobject *mk = NULL;
37 	int ret;
38 
39 	if (!drv)
40 		return 0;
41 
42 	if (mod)
43 		mk = &mod->mkobj;
44 	else if (drv->mod_name) {
45 		struct kobject *mkobj;
46 
47 		/* Lookup built-in module entry in /sys/modules */
48 		mkobj = kset_find_obj(module_kset, drv->mod_name);
49 		if (mkobj) {
50 			mk = container_of(mkobj, struct module_kobject, kobj);
51 			/* remember our module structure */
52 			drv->p->mkobj = mk;
53 			/* kset_find_obj took a reference */
54 			kobject_put(mkobj);
55 		}
56 	}
57 
58 	if (!mk)
59 		return 0;
60 
61 	ret = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
62 	if (ret)
63 		return ret;
64 
65 	driver_name = make_driver_name(drv);
66 	if (!driver_name) {
67 		ret = -ENOMEM;
68 		goto out_remove_kobj;
69 	}
70 
71 	module_create_drivers_dir(mk);
72 	if (!mk->drivers_dir) {
73 		ret = -EINVAL;
74 		goto out_free_driver_name;
75 	}
76 
77 	ret = sysfs_create_link(mk->drivers_dir, &drv->p->kobj, driver_name);
78 	if (ret)
79 		goto out_remove_drivers_dir;
80 
81 	kfree(driver_name);
82 
83 	return 0;
84 
85 out_remove_drivers_dir:
86 	sysfs_remove_link(mk->drivers_dir, driver_name);
87 
88 out_free_driver_name:
89 	kfree(driver_name);
90 
91 out_remove_kobj:
92 	sysfs_remove_link(&drv->p->kobj, "module");
93 	return ret;
94 }
95 
module_remove_driver(const struct device_driver * drv)96 void module_remove_driver(const struct device_driver *drv)
97 {
98 	struct module_kobject *mk = NULL;
99 	char *driver_name;
100 
101 	if (!drv)
102 		return;
103 
104 	sysfs_remove_link(&drv->p->kobj, "module");
105 
106 	if (drv->owner)
107 		mk = &drv->owner->mkobj;
108 	else if (drv->p->mkobj)
109 		mk = drv->p->mkobj;
110 	if (mk && mk->drivers_dir) {
111 		driver_name = make_driver_name(drv);
112 		if (driver_name) {
113 			sysfs_remove_link(mk->drivers_dir, driver_name);
114 			kfree(driver_name);
115 		}
116 	}
117 }
118