chipreg.c 2.4 KB

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