[v2,04/21] of: Move of_modalias() to module.c
Commit Message
Create a specific .c file for of related module handling.
Move of_modalias() inside as a first step.
Suggested-by: Rob Herring <robh+dt@kernel.org>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
drivers/of/Makefile | 2 +-
drivers/of/device.c | 37 -------------------------------------
drivers/of/module.c | 43 +++++++++++++++++++++++++++++++++++++++++++
include/linux/of.h | 8 ++++++++
4 files changed, 52 insertions(+), 38 deletions(-)
create mode 100644 drivers/of/module.c
Comments
On Tue, Mar 07, 2023 at 05:53:42PM +0100, Miquel Raynal wrote:
> Create a specific .c file for of related module handling.
> Move of_modalias() inside as a first step.
Perhaps a comment as to why it needs to be public? Or is it just shared
within the DT core? If so, we have of_private.h for that.
>
> Suggested-by: Rob Herring <robh+dt@kernel.org>
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
> drivers/of/Makefile | 2 +-
> drivers/of/device.c | 37 -------------------------------------
> drivers/of/module.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> include/linux/of.h | 8 ++++++++
> 4 files changed, 52 insertions(+), 38 deletions(-)
> create mode 100644 drivers/of/module.c
>
> diff --git a/drivers/of/Makefile b/drivers/of/Makefile
> index e0360a44306e..ae9923fd2940 100644
> --- a/drivers/of/Makefile
> +++ b/drivers/of/Makefile
> @@ -1,5 +1,5 @@
> # SPDX-License-Identifier: GPL-2.0
> -obj-y = base.o device.o platform.o property.o
> +obj-y = base.o device.o module.o platform.o property.o
> obj-$(CONFIG_OF_KOBJ) += kobj.o
> obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
> obj-$(CONFIG_OF_FLATTREE) += fdt.o
> diff --git a/drivers/of/device.c b/drivers/of/device.c
> index 2bbb67798916..44f1f2ef12b7 100644
> --- a/drivers/of/device.c
> +++ b/drivers/of/device.c
> @@ -1,5 +1,4 @@
> // SPDX-License-Identifier: GPL-2.0
> -#include <linux/string.h>
> #include <linux/kernel.h>
> #include <linux/of.h>
> #include <linux/of_device.h>
> @@ -248,42 +247,6 @@ const void *of_device_get_match_data(const struct device *dev)
> }
> EXPORT_SYMBOL(of_device_get_match_data);
>
> -static ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
> -{
> - const char *compat;
> - char *c;
> - struct property *p;
> - ssize_t csize;
> - ssize_t tsize;
> -
> - /* Name & Type */
> - /* %p eats all alphanum characters, so %c must be used here */
> - csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
> - of_node_get_device_type(np));
> - tsize = csize;
> - len -= csize;
> - if (str)
> - str += csize;
> -
> - of_property_for_each_string(np, "compatible", p, compat) {
> - csize = strlen(compat) + 1;
> - tsize += csize;
> - if (csize > len)
> - continue;
> -
> - csize = snprintf(str, len, "C%s", compat);
> - for (c = str; c; ) {
> - c = strchr(c, ' ');
> - if (c)
> - *c++ = '_';
> - }
> - len -= csize;
> - str += csize;
> - }
> -
> - return tsize;
> -}
> -
> int of_device_request_module(struct device *dev)
> {
> char *str;
> diff --git a/drivers/of/module.c b/drivers/of/module.c
> new file mode 100644
> index 000000000000..9c6a53f32c0f
> --- /dev/null
> +++ b/drivers/of/module.c
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: GPL-2.0+
Existing license was GPL-2.0 (-only).
> +/*
> + * Linux kernel module helpers.
> + */
> +
> +#include <linux/of.h>
> +#include <linux/string.h>
> +
> +ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
> +{
> + const char *compat;
> + char *c;
> + struct property *p;
> + ssize_t csize;
> + ssize_t tsize;
> +
> + /* Name & Type */
> + /* %p eats all alphanum characters, so %c must be used here */
> + csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
> + of_node_get_device_type(np));
> + tsize = csize;
> + len -= csize;
> + if (str)
> + str += csize;
> +
> + of_property_for_each_string(np, "compatible", p, compat) {
> + csize = strlen(compat) + 1;
> + tsize += csize;
> + if (csize > len)
> + continue;
> +
> + csize = snprintf(str, len, "C%s", compat);
> + for (c = str; c; ) {
> + c = strchr(c, ' ');
> + if (c)
> + *c++ = '_';
> + }
> + len -= csize;
> + str += csize;
> + }
> +
> + return tsize;
> +}
> diff --git a/include/linux/of.h b/include/linux/of.h
> index fc7ada57df33..1372f8647272 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -373,6 +373,9 @@ extern int of_parse_phandle_with_args_map(const struct device_node *np,
> extern int of_count_phandle_with_args(const struct device_node *np,
> const char *list_name, const char *cells_name);
>
> +/* module functions */
> +extern ssize_t of_modalias(struct device_node *np, char *str, ssize_t len);
> +
> /* phandle iterator functions */
> extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
> const struct device_node *np,
> @@ -730,6 +733,11 @@ static inline int of_count_phandle_with_args(const struct device_node *np,
> return -ENOSYS;
> }
>
> +static inline ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
> +{
> + return -ENODEV;
> +}
> +
> static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
> const struct device_node *np,
> const char *list_name,
> --
> 2.34.1
>
Hi Rob,
robh@kernel.org wrote on Tue, 7 Mar 2023 18:23:06 -0600:
> On Tue, Mar 07, 2023 at 05:53:42PM +0100, Miquel Raynal wrote:
> > Create a specific .c file for of related module handling.
> > Move of_modalias() inside as a first step.
>
> Perhaps a comment as to why it needs to be public? Or is it just shared
> within the DT core? If so, we have of_private.h for that.
Good point. This helper is actually only used internally (was static
before). At first I wanted to convert all users to use the "new" OF
module-related helpers, but unfortunately, the "dev->of_node_reused"
check makes this impossible. I thus need to keep a few users of
of_modalias() in of_device.h in the coming patches. I could move
of_modalias() to of_private.h but that would mean exposing all the
internals and private definitions to the drivers including of_device.h,
which seemed extremely unsatisfying to me.
I've updated the commit log with:
The helper is exposed through of.h even though it is only used by core
files because the users from device.c will soon be split into an OF-only
helper in module.c as well as a device-oriented inline helper in
of_device.h. Putting this helper in of_private.h would require to
include of_private.h from of_device.h, which is not acceptable.
>
> >
> > Suggested-by: Rob Herring <robh+dt@kernel.org>
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> > drivers/of/Makefile | 2 +-
> > drivers/of/device.c | 37 -------------------------------------
> > drivers/of/module.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> > include/linux/of.h | 8 ++++++++
> > 4 files changed, 52 insertions(+), 38 deletions(-)
> > create mode 100644 drivers/of/module.c
> >
> > diff --git a/drivers/of/Makefile b/drivers/of/Makefile
> > index e0360a44306e..ae9923fd2940 100644
> > --- a/drivers/of/Makefile
> > +++ b/drivers/of/Makefile
> > @@ -1,5 +1,5 @@
> > # SPDX-License-Identifier: GPL-2.0
> > -obj-y = base.o device.o platform.o property.o
> > +obj-y = base.o device.o module.o platform.o property.o
> > obj-$(CONFIG_OF_KOBJ) += kobj.o
> > obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
> > obj-$(CONFIG_OF_FLATTREE) += fdt.o
> > diff --git a/drivers/of/device.c b/drivers/of/device.c
> > index 2bbb67798916..44f1f2ef12b7 100644
> > --- a/drivers/of/device.c
> > +++ b/drivers/of/device.c
[...]
> > diff --git a/drivers/of/module.c b/drivers/of/module.c
> > new file mode 100644
> > index 000000000000..9c6a53f32c0f
> > --- /dev/null
> > +++ b/drivers/of/module.c
> > @@ -0,0 +1,43 @@
> > +// SPDX-License-Identifier: GPL-2.0+
>
> Existing license was GPL-2.0 (-only).
Oh right, I took the license from base.c, you're right I should have
taken the one from device.c.
Thanks,
Miquèl
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
-obj-y = base.o device.o platform.o property.o
+obj-y = base.o device.o module.o platform.o property.o
obj-$(CONFIG_OF_KOBJ) += kobj.o
obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
obj-$(CONFIG_OF_FLATTREE) += fdt.o
@@ -1,5 +1,4 @@
// SPDX-License-Identifier: GPL-2.0
-#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_device.h>
@@ -248,42 +247,6 @@ const void *of_device_get_match_data(const struct device *dev)
}
EXPORT_SYMBOL(of_device_get_match_data);
-static ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
-{
- const char *compat;
- char *c;
- struct property *p;
- ssize_t csize;
- ssize_t tsize;
-
- /* Name & Type */
- /* %p eats all alphanum characters, so %c must be used here */
- csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
- of_node_get_device_type(np));
- tsize = csize;
- len -= csize;
- if (str)
- str += csize;
-
- of_property_for_each_string(np, "compatible", p, compat) {
- csize = strlen(compat) + 1;
- tsize += csize;
- if (csize > len)
- continue;
-
- csize = snprintf(str, len, "C%s", compat);
- for (c = str; c; ) {
- c = strchr(c, ' ');
- if (c)
- *c++ = '_';
- }
- len -= csize;
- str += csize;
- }
-
- return tsize;
-}
-
int of_device_request_module(struct device *dev)
{
char *str;
new file mode 100644
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Linux kernel module helpers.
+ */
+
+#include <linux/of.h>
+#include <linux/string.h>
+
+ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
+{
+ const char *compat;
+ char *c;
+ struct property *p;
+ ssize_t csize;
+ ssize_t tsize;
+
+ /* Name & Type */
+ /* %p eats all alphanum characters, so %c must be used here */
+ csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
+ of_node_get_device_type(np));
+ tsize = csize;
+ len -= csize;
+ if (str)
+ str += csize;
+
+ of_property_for_each_string(np, "compatible", p, compat) {
+ csize = strlen(compat) + 1;
+ tsize += csize;
+ if (csize > len)
+ continue;
+
+ csize = snprintf(str, len, "C%s", compat);
+ for (c = str; c; ) {
+ c = strchr(c, ' ');
+ if (c)
+ *c++ = '_';
+ }
+ len -= csize;
+ str += csize;
+ }
+
+ return tsize;
+}
@@ -373,6 +373,9 @@ extern int of_parse_phandle_with_args_map(const struct device_node *np,
extern int of_count_phandle_with_args(const struct device_node *np,
const char *list_name, const char *cells_name);
+/* module functions */
+extern ssize_t of_modalias(struct device_node *np, char *str, ssize_t len);
+
/* phandle iterator functions */
extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
const struct device_node *np,
@@ -730,6 +733,11 @@ static inline int of_count_phandle_with_args(const struct device_node *np,
return -ENOSYS;
}
+static inline ssize_t of_modalias(struct device_node *np, char *str, ssize_t len)
+{
+ return -ENODEV;
+}
+
static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
const struct device_node *np,
const char *list_name,