kernel: backport of_request_module()
[openwrt/staging/hauke.git] / target / linux / generic / backport-5.10 / 828-v6.4-0004-of-Move-of_modalias-to-module.c.patch
1 From bd7a7ed774afd1a4174df34227626c95573be517 Mon Sep 17 00:00:00 2001
2 From: Miquel Raynal <miquel.raynal@bootlin.com>
3 Date: Tue, 4 Apr 2023 18:21:17 +0100
4 Subject: [PATCH] of: Move of_modalias() to module.c
5
6 Create a specific .c file for OF related module handling.
7 Move of_modalias() inside as a first step.
8
9 The helper is exposed through of.h even though it is only used by core
10 files because the users from device.c will soon be split into an OF-only
11 helper in module.c as well as a device-oriented inline helper in
12 of_device.h. Putting this helper in of_private.h would require to
13 include of_private.h from of_device.h, which is not acceptable.
14
15 Suggested-by: Rob Herring <robh+dt@kernel.org>
16 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
17 Reviewed-by: Rob Herring <robh@kernel.org>
18 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
19 Link: https://lore.kernel.org/r/20230404172148.82422-10-srinivas.kandagatla@linaro.org
20 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
21 ---
22 drivers/of/Makefile | 2 +-
23 drivers/of/device.c | 37 -------------------------------------
24 drivers/of/module.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
25 include/linux/of.h | 9 +++++++++
26 4 files changed, 54 insertions(+), 38 deletions(-)
27 create mode 100644 drivers/of/module.c
28
29 --- a/drivers/of/Makefile
30 +++ b/drivers/of/Makefile
31 @@ -1,5 +1,5 @@
32 # SPDX-License-Identifier: GPL-2.0
33 -obj-y = base.o device.o platform.o property.o
34 +obj-y = base.o device.o module.o platform.o property.o
35 obj-$(CONFIG_OF_KOBJ) += kobj.o
36 obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
37 obj-$(CONFIG_OF_FLATTREE) += fdt.o
38 --- a/drivers/of/device.c
39 +++ b/drivers/of/device.c
40 @@ -1,5 +1,4 @@
41 // SPDX-License-Identifier: GPL-2.0
42 -#include <linux/string.h>
43 #include <linux/kernel.h>
44 #include <linux/of.h>
45 #include <linux/of_device.h>
46 @@ -215,42 +214,6 @@ const void *of_device_get_match_data(con
47 }
48 EXPORT_SYMBOL(of_device_get_match_data);
49
50 -static ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
51 -{
52 - const char *compat;
53 - char *c;
54 - struct property *p;
55 - ssize_t csize;
56 - ssize_t tsize;
57 -
58 - /* Name & Type */
59 - /* %p eats all alphanum characters, so %c must be used here */
60 - csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
61 - of_node_get_device_type(np));
62 - tsize = csize;
63 - len -= csize;
64 - if (str)
65 - str += csize;
66 -
67 - of_property_for_each_string(np, "compatible", p, compat) {
68 - csize = strlen(compat) + 1;
69 - tsize += csize;
70 - if (csize > len)
71 - continue;
72 -
73 - csize = snprintf(str, len, "C%s", compat);
74 - for (c = str; c; ) {
75 - c = strchr(c, ' ');
76 - if (c)
77 - *c++ = '_';
78 - }
79 - len -= csize;
80 - str += csize;
81 - }
82 -
83 - return tsize;
84 -}
85 -
86 int of_device_request_module(struct device *dev)
87 {
88 char *str;
89 --- /dev/null
90 +++ b/drivers/of/module.c
91 @@ -0,0 +1,44 @@
92 +// SPDX-License-Identifier: GPL-2.0
93 +/*
94 + * Linux kernel module helpers.
95 + */
96 +
97 +#include <linux/of.h>
98 +#include <linux/slab.h>
99 +#include <linux/string.h>
100 +
101 +ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
102 +{
103 + const char *compat;
104 + char *c;
105 + struct property *p;
106 + ssize_t csize;
107 + ssize_t tsize;
108 +
109 + /* Name & Type */
110 + /* %p eats all alphanum characters, so %c must be used here */
111 + csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
112 + of_node_get_device_type(np));
113 + tsize = csize;
114 + len -= csize;
115 + if (str)
116 + str += csize;
117 +
118 + of_property_for_each_string(np, "compatible", p, compat) {
119 + csize = strlen(compat) + 1;
120 + tsize += csize;
121 + if (csize > len)
122 + continue;
123 +
124 + csize = snprintf(str, len, "C%s", compat);
125 + for (c = str; c; ) {
126 + c = strchr(c, ' ');
127 + if (c)
128 + *c++ = '_';
129 + }
130 + len -= csize;
131 + str += csize;
132 + }
133 +
134 + return tsize;
135 +}
136 --- a/include/linux/of.h
137 +++ b/include/linux/of.h
138 @@ -373,6 +373,9 @@ extern int of_parse_phandle_with_args_ma
139 extern int of_count_phandle_with_args(const struct device_node *np,
140 const char *list_name, const char *cells_name);
141
142 +/* module functions */
143 +extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
144 +
145 /* phandle iterator functions */
146 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
147 const struct device_node *np,
148 @@ -878,6 +881,12 @@ static inline int of_count_phandle_with_
149 return -ENOSYS;
150 }
151
152 +static inline ssize_t of_modalias(const struct device_node *np, char *str,
153 + ssize_t len)
154 +{
155 + return -ENODEV;
156 +}
157 +
158 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
159 const struct device_node *np,
160 const char *list_name,