module.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if (!mk || mk->drivers_dir)
  24. return;
  25. mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
  26. }
  27. void module_add_driver(struct module *mod, struct device_driver *drv)
  28. {
  29. char *driver_name;
  30. int no_warn;
  31. struct module_kobject *mk = NULL;
  32. if (!drv)
  33. return;
  34. if (mod)
  35. mk = &mod->mkobj;
  36. else if (drv->mod_name) {
  37. struct kobject *mkobj;
  38. /* Lookup built-in module entry in /sys/modules */
  39. mkobj = kset_find_obj(module_kset, drv->mod_name);
  40. if (mkobj) {
  41. mk = container_of(mkobj, struct module_kobject, kobj);
  42. /* remember our module structure */
  43. drv->p->mkobj = mk;
  44. /* kset_find_obj took a reference */
  45. kobject_put(mkobj);
  46. }
  47. }
  48. if (!mk)
  49. return;
  50. /* Don't check return codes; these calls are idempotent */
  51. no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj, "module");
  52. driver_name = make_driver_name(drv);
  53. if (driver_name) {
  54. module_create_drivers_dir(mk);
  55. no_warn = sysfs_create_link(mk->drivers_dir, &drv->p->kobj,
  56. driver_name);
  57. kfree(driver_name);
  58. }
  59. }
  60. void module_remove_driver(struct device_driver *drv)
  61. {
  62. struct module_kobject *mk = NULL;
  63. char *driver_name;
  64. if (!drv)
  65. return;
  66. sysfs_remove_link(&drv->p->kobj, "module");
  67. if (drv->owner)
  68. mk = &drv->owner->mkobj;
  69. else if (drv->p->mkobj)
  70. mk = drv->p->mkobj;
  71. if (mk && mk->drivers_dir) {
  72. driver_name = make_driver_name(drv);
  73. if (driver_name) {
  74. sysfs_remove_link(mk->drivers_dir, driver_name);
  75. kfree(driver_name);
  76. }
  77. }
  78. }