blob: db930d3ee31259f6a17b9dd33eb16caee4e12f5c [file] [log] [blame]
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -08001/*
2 * module.c - module sysfs fun for drivers
3 *
4 * This file is released under the GPLv2
5 *
6 */
7#include <linux/device.h>
8#include <linux/module.h>
9#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080011#include <linux/string.h>
12#include "base.h"
13
14static char *make_driver_name(struct device_driver *drv)
15{
16 char *driver_name;
17
Julia Lawall16532682010-03-10 22:18:13 +010018 driver_name = kasprintf(GFP_KERNEL, "%s:%s", drv->bus->name, drv->name);
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080019 if (!driver_name)
20 return NULL;
21
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080022 return driver_name;
23}
24
25static void module_create_drivers_dir(struct module_kobject *mk)
26{
27 if (!mk || mk->drivers_dir)
28 return;
29
30 mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
31}
32
33void module_add_driver(struct module *mod, struct device_driver *drv)
34{
35 char *driver_name;
36 int no_warn;
37 struct module_kobject *mk = NULL;
38
39 if (!drv)
40 return;
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 */
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080052 drv->p->mkobj = mk;
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080053 /* kset_find_obj took a reference */
54 kobject_put(mkobj);
55 }
56 }
57
58 if (!mk)
59 return;
60
61 /* Don't check return codes; these calls are idempotent */
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080062 no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080063 driver_name = make_driver_name(drv);
64 if (driver_name) {
65 module_create_drivers_dir(mk);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080066 no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080067 driver_name);
68 kfree(driver_name);
69 }
70}
71
72void module_remove_driver(struct device_driver *drv)
73{
74 struct module_kobject *mk = NULL;
75 char *driver_name;
76
77 if (!drv)
78 return;
79
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080080 sysfs_remove_link(&drv->p->kobj, "module");
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080081
82 if (drv->owner)
83 mk = &drv->owner->mkobj;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080084 else if (drv->p->mkobj)
85 mk = drv->p->mkobj;
Greg Kroah-Hartmanc63469a2007-11-28 12:23:18 -080086 if (mk && mk->drivers_dir) {
87 driver_name = make_driver_name(drv);
88 if (driver_name) {
89 sysfs_remove_link(mk->drivers_dir, driver_name);
90 kfree(driver_name);
91 }
92 }
93}