Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 10 | #include <linux/string.h> |
| 11 | #include "base.h" |
| 12 | |
| 13 | static char *make_driver_name(struct device_driver *drv) |
| 14 | { |
| 15 | char *driver_name; |
| 16 | |
| 17 | driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2, |
| 18 | GFP_KERNEL); |
| 19 | if (!driver_name) |
| 20 | return NULL; |
| 21 | |
| 22 | sprintf(driver_name, "%s:%s", drv->bus->name, drv->name); |
| 23 | return driver_name; |
| 24 | } |
| 25 | |
| 26 | static void module_create_drivers_dir(struct module_kobject *mk) |
| 27 | { |
| 28 | if (!mk || mk->drivers_dir) |
| 29 | return; |
| 30 | |
| 31 | mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj); |
| 32 | } |
| 33 | |
| 34 | void module_add_driver(struct module *mod, struct device_driver *drv) |
| 35 | { |
| 36 | char *driver_name; |
| 37 | int no_warn; |
| 38 | struct module_kobject *mk = NULL; |
| 39 | |
| 40 | if (!drv) |
| 41 | return; |
| 42 | |
| 43 | if (mod) |
| 44 | mk = &mod->mkobj; |
| 45 | else if (drv->mod_name) { |
| 46 | struct kobject *mkobj; |
| 47 | |
| 48 | /* Lookup built-in module entry in /sys/modules */ |
| 49 | mkobj = kset_find_obj(module_kset, drv->mod_name); |
| 50 | if (mkobj) { |
| 51 | mk = container_of(mkobj, struct module_kobject, kobj); |
| 52 | /* remember our module structure */ |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 53 | drv->p->mkobj = mk; |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 54 | /* kset_find_obj took a reference */ |
| 55 | kobject_put(mkobj); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (!mk) |
| 60 | return; |
| 61 | |
| 62 | /* Don't check return codes; these calls are idempotent */ |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 63 | no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module"); |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 64 | driver_name = make_driver_name(drv); |
| 65 | if (driver_name) { |
| 66 | module_create_drivers_dir(mk); |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 67 | no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj, |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 68 | driver_name); |
| 69 | kfree(driver_name); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void module_remove_driver(struct device_driver *drv) |
| 74 | { |
| 75 | struct module_kobject *mk = NULL; |
| 76 | char *driver_name; |
| 77 | |
| 78 | if (!drv) |
| 79 | return; |
| 80 | |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 81 | sysfs_remove_link(&drv->p->kobj, "module"); |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 82 | |
| 83 | if (drv->owner) |
| 84 | mk = &drv->owner->mkobj; |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 85 | else if (drv->p->mkobj) |
| 86 | mk = drv->p->mkobj; |
Greg Kroah-Hartman | c63469a | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 87 | if (mk && mk->drivers_dir) { |
| 88 | driver_name = make_driver_name(drv); |
| 89 | if (driver_name) { |
| 90 | sysfs_remove_link(mk->drivers_dir, driver_name); |
| 91 | kfree(driver_name); |
| 92 | } |
| 93 | } |
| 94 | } |