module.c 2.0 KB

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