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/string.h>
13 static char *make_driver_name(struct device_driver *drv)
17 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
22 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
26 static void module_create_drivers_dir(struct module_kobject *mk)
28 if (!mk || mk->drivers_dir)
31 mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
34 void module_add_driver(struct module *mod, struct device_driver *drv)
38 struct module_kobject *mk = NULL;
45 else if (drv->mod_name) {
46 struct kobject *mkobj;
48 /* Lookup built-in module entry in /sys/modules */
49 mkobj = kset_find_obj(module_kset, drv->mod_name);
51 mk = container_of(mkobj, struct module_kobject, kobj);
52 /* remember our module structure */
54 /* kset_find_obj took a reference */
62 /* Don't check return codes; these calls are idempotent */
63 no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
64 driver_name = make_driver_name(drv);
66 module_create_drivers_dir(mk);
67 no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
73 void module_remove_driver(struct device_driver *drv)
75 struct module_kobject *mk = NULL;
81 sysfs_remove_link(&drv->p->kobj, "module");
84 mk = &drv->owner->mkobj;
85 else if (drv->p->mkobj)
87 if (mk && mk->drivers_dir) {
88 driver_name = make_driver_name(drv);
90 sysfs_remove_link(mk->drivers_dir, driver_name);