2 * module.c - module sysfs fun for drivers
4 * This file is released under the GPLv2
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
14 static char *make_driver_name(struct device_driver *drv)
18 driver_name = kasprintf(GFP_KERNEL, "%s:%s", drv->bus->name, drv->name);
25 static void module_create_drivers_dir(struct module_kobject *mk)
27 if (!mk || mk->drivers_dir)
30 mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
33 void module_add_driver(struct module *mod, struct device_driver *drv)
37 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 /* Don't check return codes; these calls are idempotent */
62 no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
63 driver_name = make_driver_name(drv);
65 module_create_drivers_dir(mk);
66 no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
72 void module_remove_driver(struct device_driver *drv)
74 struct module_kobject *mk = NULL;
80 sysfs_remove_link(&drv->p->kobj, "module");
83 mk = &drv->owner->mkobj;
84 else if (drv->p->mkobj)
86 if (mk && mk->drivers_dir) {
87 driver_name = make_driver_name(drv);
89 sysfs_remove_link(mk->drivers_dir, driver_name);