495-mtd-core-add-get_mtd_device_by_node.patch 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. From 1bd1b740f208d1cf4071932cc51860d37266c402 Mon Sep 17 00:00:00 2001
  2. From: Bernhard Frauendienst <kernel@nospam.obeliks.de>
  3. Date: Sat, 1 Sep 2018 00:30:11 +0200
  4. Subject: [PATCH 495/497] mtd: core: add get_mtd_device_by_node
  5. Add function to retrieve a mtd device by its OF node. Since drivers can
  6. assign arbitrary names to mtd devices in the absence of a label
  7. property, there is no other reliable way to retrieve a mtd device for a
  8. given OF node.
  9. Signed-off-by: Bernhard Frauendienst <kernel@nospam.obeliks.de>
  10. Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
  11. ---
  12. drivers/mtd/mtdcore.c | 38 ++++++++++++++++++++++++++++++++++++++
  13. include/linux/mtd/mtd.h | 2 ++
  14. 2 files changed, 40 insertions(+)
  15. --- a/drivers/mtd/mtdcore.c
  16. +++ b/drivers/mtd/mtdcore.c
  17. @@ -924,6 +924,44 @@ out_unlock:
  18. }
  19. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  20. +/**
  21. + * get_mtd_device_by_node - obtain a validated handle for an MTD device
  22. + * by of_node
  23. + * @of_node: OF node of MTD device to open
  24. + *
  25. + * This function returns MTD device description structure in case of
  26. + * success and an error code in case of failure.
  27. + */
  28. +struct mtd_info *get_mtd_device_by_node(const struct device_node *of_node)
  29. +{
  30. + int err = -ENODEV;
  31. + struct mtd_info *mtd = NULL, *other;
  32. +
  33. + mutex_lock(&mtd_table_mutex);
  34. +
  35. + mtd_for_each_device(other) {
  36. + if (of_node == other->dev.of_node) {
  37. + mtd = other;
  38. + break;
  39. + }
  40. + }
  41. +
  42. + if (!mtd)
  43. + goto out_unlock;
  44. +
  45. + err = __get_mtd_device(mtd);
  46. + if (err)
  47. + goto out_unlock;
  48. +
  49. + mutex_unlock(&mtd_table_mutex);
  50. + return mtd;
  51. +
  52. +out_unlock:
  53. + mutex_unlock(&mtd_table_mutex);
  54. + return ERR_PTR(err);
  55. +}
  56. +EXPORT_SYMBOL_GPL(get_mtd_device_by_node);
  57. +
  58. void put_mtd_device(struct mtd_info *mtd)
  59. {
  60. mutex_lock(&mtd_table_mutex);
  61. --- a/include/linux/mtd/mtd.h
  62. +++ b/include/linux/mtd/mtd.h
  63. @@ -580,6 +580,8 @@ extern struct mtd_info *get_mtd_device(s
  64. extern int __get_mtd_device(struct mtd_info *mtd);
  65. extern void __put_mtd_device(struct mtd_info *mtd);
  66. extern struct mtd_info *get_mtd_device_nm(const char *name);
  67. +extern struct mtd_info *get_mtd_device_by_node(
  68. + const struct device_node *of_node);
  69. extern void put_mtd_device(struct mtd_info *mtd);