kernel: Make the patches apply on top of 4.19
[openwrt/staging/chunkeey.git] / target / linux / generic / pending-4.19 / 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 --- a/drivers/mtd/mtdcore.c
19 +++ b/drivers/mtd/mtdcore.c
20 @@ -938,6 +938,44 @@ out_unlock:
21 }
22 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
23
24 +/**
25 + * get_mtd_device_by_node - obtain a validated handle for an MTD device
26 + * by of_node
27 + * @of_node: OF node of MTD device to open
28 + *
29 + * This function returns MTD device description structure in case of
30 + * success and an error code in case of failure.
31 + */
32 +struct mtd_info *get_mtd_device_by_node(const struct device_node *of_node)
33 +{
34 + int err = -ENODEV;
35 + struct mtd_info *mtd = NULL, *other;
36 +
37 + mutex_lock(&mtd_table_mutex);
38 +
39 + mtd_for_each_device(other) {
40 + if (of_node == other->dev.of_node) {
41 + mtd = other;
42 + break;
43 + }
44 + }
45 +
46 + if (!mtd)
47 + goto out_unlock;
48 +
49 + err = __get_mtd_device(mtd);
50 + if (err)
51 + goto out_unlock;
52 +
53 + mutex_unlock(&mtd_table_mutex);
54 + return mtd;
55 +
56 +out_unlock:
57 + mutex_unlock(&mtd_table_mutex);
58 + return ERR_PTR(err);
59 +}
60 +EXPORT_SYMBOL_GPL(get_mtd_device_by_node);
61 +
62 void put_mtd_device(struct mtd_info *mtd)
63 {
64 mutex_lock(&mtd_table_mutex);
65 --- a/include/linux/mtd/mtd.h
66 +++ b/include/linux/mtd/mtd.h
67 @@ -589,6 +589,8 @@ extern struct mtd_info *get_mtd_device(s
68 extern int __get_mtd_device(struct mtd_info *mtd);
69 extern void __put_mtd_device(struct mtd_info *mtd);
70 extern struct mtd_info *get_mtd_device_nm(const char *name);
71 +extern struct mtd_info *get_mtd_device_by_node(
72 + const struct device_node *of_node);
73 extern void put_mtd_device(struct mtd_info *mtd);
74
75