chipreg.c 2.3 KB

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