chipreg.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Registration for chip drivers
  3. *
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/kmod.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/slab.h>
  10. #include <linux/mtd/map.h>
  11. #include <linux/mtd/mtd.h>
  12. static DEFINE_SPINLOCK(chip_drvs_lock);
  13. static LIST_HEAD(chip_drvs_list);
  14. void register_mtd_chip_driver(struct mtd_chip_driver *drv)
  15. {
  16. spin_lock(&chip_drvs_lock);
  17. list_add(&drv->list, &chip_drvs_list);
  18. spin_unlock(&chip_drvs_lock);
  19. }
  20. void unregister_mtd_chip_driver(struct mtd_chip_driver *drv)
  21. {
  22. spin_lock(&chip_drvs_lock);
  23. list_del(&drv->list);
  24. spin_unlock(&chip_drvs_lock);
  25. }
  26. static struct mtd_chip_driver *get_mtd_chip_driver (const char *name)
  27. {
  28. struct list_head *pos;
  29. struct mtd_chip_driver *ret = NULL, *this;
  30. spin_lock(&chip_drvs_lock);
  31. list_for_each(pos, &chip_drvs_list) {
  32. this = list_entry(pos, typeof(*this), list);
  33. if (!strcmp(this->name, name)) {
  34. ret = this;
  35. break;
  36. }
  37. }
  38. if (ret && !try_module_get(ret->module))
  39. ret = NULL;
  40. spin_unlock(&chip_drvs_lock);
  41. return ret;
  42. }
  43. /* Hide all the horrid details, like some silly person taking
  44. get_module_symbol() away from us, from the caller. */
  45. struct mtd_info *do_map_probe(const char *name, struct map_info *map)
  46. {
  47. struct mtd_chip_driver *drv;
  48. struct mtd_info *ret;
  49. drv = get_mtd_chip_driver(name);
  50. if (!drv && !request_module("%s", name))
  51. drv = get_mtd_chip_driver(name);
  52. if (!drv)
  53. return NULL;
  54. ret = drv->probe(map);
  55. /* We decrease the use count here. It may have been a
  56. probe-only module, which is no longer required from this
  57. point, having given us a handle on (and increased the use
  58. count of) the actual driver code.
  59. */
  60. module_put(drv->module);
  61. return ret;
  62. }
  63. /*
  64. * Destroy an MTD device which was created for a map device.
  65. * Make sure the MTD device is already unregistered before calling this
  66. */
  67. void map_destroy(struct mtd_info *mtd)
  68. {
  69. struct map_info *map = mtd->priv;
  70. if (map->fldrv->destroy)
  71. map->fldrv->destroy(mtd);
  72. module_put(map->fldrv->module);
  73. kfree(mtd);
  74. }
  75. EXPORT_SYMBOL(register_mtd_chip_driver);
  76. EXPORT_SYMBOL(unregister_mtd_chip_driver);
  77. EXPORT_SYMBOL(do_map_probe);
  78. EXPORT_SYMBOL(map_destroy);
  79. MODULE_LICENSE("GPL");
  80. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  81. MODULE_DESCRIPTION("Core routines for registering and invoking MTD chip drivers");