32a87d34d5a0af1c7850f6be0b18a6132917ca16
[openwrt/openwrt.git] / target / linux / generic / pending-4.14 / 495-mtd-core-add-get_mtd_device_by_node.patch
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
6 Add function to retrieve a mtd device by its OF node. Since drivers can
7 assign arbitrary names to mtd devices in the absence of a label
8 property, there is no other reliable way to retrieve a mtd device for a
9 given OF node.
10
11 Signed-off-by: Bernhard Frauendienst <kernel@nospam.obeliks.de>
12 Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
13 ---
14 drivers/mtd/mtdcore.c | 38 ++++++++++++++++++++++++++++++++++++++
15 include/linux/mtd/mtd.h | 2 ++
16 2 files changed, 40 insertions(+)
17
18 diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
19 index e7ea842ba3db..1134bb81d2e5 100644
20 --- a/drivers/mtd/mtdcore.c
21 +++ b/drivers/mtd/mtdcore.c
22 @@ -936,6 +936,44 @@ struct mtd_info *get_mtd_device_nm(const char *name)
23 }
24 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
25
26 +/**
27 + * get_mtd_device_by_node - obtain a validated handle for an MTD device
28 + * by of_node
29 + * @of_node: OF node of MTD device to open
30 + *
31 + * This function returns MTD device description structure in case of
32 + * success and an error code in case of failure.
33 + */
34 +struct mtd_info *get_mtd_device_by_node(const struct device_node *of_node)
35 +{
36 + int err = -ENODEV;
37 + struct mtd_info *mtd = NULL, *other;
38 +
39 + mutex_lock(&mtd_table_mutex);
40 +
41 + mtd_for_each_device(other) {
42 + if (of_node == other->dev.of_node) {
43 + mtd = other;
44 + break;
45 + }
46 + }
47 +
48 + if (!mtd)
49 + goto out_unlock;
50 +
51 + err = __get_mtd_device(mtd);
52 + if (err)
53 + goto out_unlock;
54 +
55 + mutex_unlock(&mtd_table_mutex);
56 + return mtd;
57 +
58 +out_unlock:
59 + mutex_unlock(&mtd_table_mutex);
60 + return ERR_PTR(err);
61 +}
62 +EXPORT_SYMBOL_GPL(get_mtd_device_by_node);
63 +
64 void put_mtd_device(struct mtd_info *mtd)
65 {
66 mutex_lock(&mtd_table_mutex);
67 diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
68 index 6cd0f6b7658b..cf7c8030cd8e 100644
69 --- a/include/linux/mtd/mtd.h
70 +++ b/include/linux/mtd/mtd.h
71 @@ -557,6 +557,8 @@ extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
72 extern int __get_mtd_device(struct mtd_info *mtd);
73 extern void __put_mtd_device(struct mtd_info *mtd);
74 extern struct mtd_info *get_mtd_device_nm(const char *name);
75 +extern struct mtd_info *get_mtd_device_by_node(
76 + const struct device_node *of_node);
77 extern void put_mtd_device(struct mtd_info *mtd);
78
79
80 --
81 2.18.0
82