chipreg.c 2.4 KB

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