diff mbox series

[RFC,04/12] of: base: introduce of_match_string()

Message ID 20210208222203.22335-5-info@metux.net
State New
Headers show
Series [RFC,01/12] of: base: improve error message in of_phandle_iterator_next() | expand

Commit Message

Enrico Weigelt, metux IT consult Feb. 8, 2021, 10:21 p.m. UTC
Introduce a new helper function that looks up a given propery and
matches all string elements against a given string. This is useful
if we want to check wether one string element of some property
matches a given string.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
---
 drivers/of/base.c  | 32 ++++++++++++++++++++++++++++++++
 include/linux/of.h |  2 ++
 2 files changed, 34 insertions(+)

Comments

Rob Herring Feb. 8, 2021, 11:52 p.m. UTC | #1
On Mon, Feb 8, 2021 at 4:24 PM Enrico Weigelt, metux IT consult
<info@metux.net> wrote:
>
> Introduce a new helper function that looks up a given propery and

typo

> matches all string elements against a given string. This is useful
> if we want to check wether one string element of some property

typo


> matches a given string.

Is there a user? Didn't see one in the series, but may have missed it.
I'd think we have some existing cases that could be converted.

> Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
> ---
>  drivers/of/base.c  | 32 ++++++++++++++++++++++++++++++++
>  include/linux/of.h |  2 ++
>  2 files changed, 34 insertions(+)
>
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index e5ef611ed233..649c2a32bb48 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -287,6 +287,38 @@ const void *of_get_property(const struct device_node *np, const char *name,
>  EXPORT_SYMBOL(of_get_property);
>
>  /*
> + * of_match_string - match a propery against given string
> + * @node: device_node to look up at
> + * @name: name of the property
> + * @value: value to match against
> + *
> + * Look for property by name and match all string elements against value.
> + * Returns true if the property exists and any one of the string elements
> + * matches the given value.
> + */
> +bool of_match_string(const struct device_node *node, const char* name,
> +                    const char* value)
> +{
> +       struct property *prop;
> +       const char *walk;
> +
> +       if (!name || !value)
> +               return false;
> +
> +       prop = of_find_property(node, name, NULL);
> +       if (!prop)
> +               return false;
> +
> +       for (walk=of_prop_next_string(prop, NULL); walk;
> +            walk=of_prop_next_string(prop, walk)) {
> +               if (strcmp(walk, value)==0)
> +                       return true;
> +       }
> +       return true;
> +}
> +EXPORT_SYMBOL_GPL(of_match_string);
> +
> +/*
>   * arch_match_cpu_phys_id - Match the given logical CPU and physical id
>   *
>   * @cpu: logical cpu index of a core/thread
> diff --git a/include/linux/of.h b/include/linux/of.h
> index dbf2c7442389..3612429632f4 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -355,6 +355,8 @@ extern bool of_device_is_big_endian(const struct device_node *device);
>  extern const void *of_get_property(const struct device_node *node,
>                                 const char *name,
>                                 int *lenp);
> +extern bool of_match_string(const struct device_node *node, const char* name,
> +                           const char* value);
>  extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
>  extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
>  extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
> --
> 2.11.0
>
diff mbox series

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index e5ef611ed233..649c2a32bb48 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -287,6 +287,38 @@  const void *of_get_property(const struct device_node *np, const char *name,
 EXPORT_SYMBOL(of_get_property);
 
 /*
+ * of_match_string - match a propery against given string
+ * @node: device_node to look up at
+ * @name: name of the property
+ * @value: value to match against
+ *
+ * Look for property by name and match all string elements against value.
+ * Returns true if the property exists and any one of the string elements
+ * matches the given value.
+ */
+bool of_match_string(const struct device_node *node, const char* name,
+		     const char* value)
+{
+	struct property *prop;
+	const char *walk;
+
+	if (!name || !value)
+		return false;
+
+	prop = of_find_property(node, name, NULL);
+	if (!prop)
+		return false;
+
+	for (walk=of_prop_next_string(prop, NULL); walk;
+	     walk=of_prop_next_string(prop, walk)) {
+		if (strcmp(walk, value)==0)
+			return true;
+	}
+	return true;
+}
+EXPORT_SYMBOL_GPL(of_match_string);
+
+/*
  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
  *
  * @cpu: logical cpu index of a core/thread
diff --git a/include/linux/of.h b/include/linux/of.h
index dbf2c7442389..3612429632f4 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -355,6 +355,8 @@  extern bool of_device_is_big_endian(const struct device_node *device);
 extern const void *of_get_property(const struct device_node *node,
 				const char *name,
 				int *lenp);
+extern bool of_match_string(const struct device_node *node, const char* name,
+			    const char* value);
 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,