chipreg.c 2.3 KB

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