067-v4.17-0001-mtd-partitions-add-of_match_table-parser-matching.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. From bb2192123ec70470d6ea33f138846b175403a968 Mon Sep 17 00:00:00 2001
  2. From: Brian Norris <[email protected]>
  3. Date: Thu, 4 Jan 2018 08:05:33 +0100
  4. Subject: [PATCH] mtd: partitions: add of_match_table parser matching
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Partition parsers can now provide an of_match_table to enable
  9. flash<-->parser matching via device tree as documented in the
  10. mtd/partition.txt.
  11. It works by looking for a matching parser for every string in the
  12. "compatibility" property (starting with the most specific one).
  13. This support is currently limited to built-in parsers as it uses
  14. request_module() and friends. This should be sufficient for most cases
  15. though as compiling parsers as modules isn't a common choice.
  16. Signed-off-by: Brian Norris <[email protected]>
  17. Signed-off-by: Rafał Miłecki <[email protected]>
  18. Signed-off-by: Boris Brezillon <[email protected]>
  19. ---
  20. drivers/mtd/mtdpart.c | 59 ++++++++++++++++++++++++++++++++++++++++++
  21. include/linux/mtd/partitions.h | 1 +
  22. 2 files changed, 60 insertions(+)
  23. --- a/drivers/mtd/mtdpart.c
  24. +++ b/drivers/mtd/mtdpart.c
  25. @@ -30,6 +30,7 @@
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/err.h>
  29. +#include <linux/of.h>
  30. #include "mtdcore.h"
  31. @@ -886,6 +887,45 @@ static int mtd_part_do_parse(struct mtd_
  32. }
  33. /**
  34. + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
  35. + *
  36. + * @compat: compatible string describing partitions in a device tree
  37. + *
  38. + * MTD parsers can specify supported partitions by providing a table of
  39. + * compatibility strings. This function finds a parser that advertises support
  40. + * for a passed value of "compatible".
  41. + */
  42. +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
  43. +{
  44. + struct mtd_part_parser *p, *ret = NULL;
  45. +
  46. + spin_lock(&part_parser_lock);
  47. +
  48. + list_for_each_entry(p, &part_parsers, list) {
  49. + const struct of_device_id *matches;
  50. +
  51. + matches = p->of_match_table;
  52. + if (!matches)
  53. + continue;
  54. +
  55. + for (; matches->compatible[0]; matches++) {
  56. + if (!strcmp(matches->compatible, compat) &&
  57. + try_module_get(p->owner)) {
  58. + ret = p;
  59. + break;
  60. + }
  61. + }
  62. +
  63. + if (ret)
  64. + break;
  65. + }
  66. +
  67. + spin_unlock(&part_parser_lock);
  68. +
  69. + return ret;
  70. +}
  71. +
  72. +/**
  73. * parse_mtd_partitions - parse MTD partitions
  74. * @master: the master partition (describes whole MTD device)
  75. * @types: names of partition parsers to try or %NULL
  76. @@ -911,8 +951,27 @@ int parse_mtd_partitions(struct mtd_info
  77. struct mtd_part_parser_data *data)
  78. {
  79. struct mtd_part_parser *parser;
  80. + struct device_node *np;
  81. + struct property *prop;
  82. + const char *compat;
  83. int ret, err = 0;
  84. + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
  85. + of_property_for_each_string(np, "compatible", prop, compat) {
  86. + parser = mtd_part_get_compatible_parser(compat);
  87. + if (!parser)
  88. + continue;
  89. + ret = mtd_part_do_parse(parser, master, pparts, data);
  90. + if (ret > 0) {
  91. + of_node_put(np);
  92. + return 0;
  93. + }
  94. + mtd_part_parser_put(parser);
  95. + if (ret < 0 && !err)
  96. + err = ret;
  97. + }
  98. + of_node_put(np);
  99. +
  100. if (!types)
  101. types = default_mtd_part_types;
  102. --- a/include/linux/mtd/partitions.h
  103. +++ b/include/linux/mtd/partitions.h
  104. @@ -77,6 +77,7 @@ struct mtd_part_parser {
  105. struct list_head list;
  106. struct module *owner;
  107. const char *name;
  108. + const struct of_device_id *of_match_table;
  109. int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
  110. struct mtd_part_parser_data *);
  111. void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);