Patchwork [08/21] of: Add of_property_match_string() to find index into a string list

login
register
mail settings
Submitter Ike Panhc
Date June 6, 2012, 12:27 p.m.
Message ID <1338985673-26207-1-git-send-email-ike.pan@canonical.com>
Download mbox | patch
Permalink /patch/163342/
State New
Headers show

Comments

Ike Panhc - June 6, 2012, 12:27 p.m.
From: Grant Likely <grant.likely@secretlab.ca>

BugLink: http://launchpad.net/bugs/1008345

Add a helper function for finding the index of a string in a string
list property.  This helper is useful for bindings that use a separate
*-name property for attaching names to tuples in another property such
as 'reg' or 'gpios'.

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
(backported from commit 7aff0fe33033fc75b61446ba29d38b1b1354af9f)

Signed-off-by: Ike Panhc <ike.pan@canonical.com>
---
 drivers/of/base.c  |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/of.h |    3 +++
 2 files changed, 39 insertions(+)
Ike Panhc - June 6, 2012, 12:50 p.m.
On 06/06/2012 08:27 PM, Ike Panhc wrote:
> From: Grant Likely <grant.likely@secretlab.ca>
> 
> BugLink: http://launchpad.net/bugs/1008345
> 
> Add a helper function for finding the index of a string in a string
> list property.  This helper is useful for bindings that use a separate
> *-name property for attaching names to tuples in another property such
> as 'reg' or 'gpios'.
> 
> Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
> (backported from commit 7aff0fe33033fc75b61446ba29d38b1b1354af9f)

In upstream patch, it also modify

  drivers/of/selftest.c
  arch/arm/boot/dts/testcases/tests-phandle.dtsi

which not in v3.2, so its backported, not cherry-picked
> 
> Signed-off-by: Ike Panhc <ike.pan@canonical.com>
> ---
>  drivers/of/base.c  |   36 ++++++++++++++++++++++++++++++++++++
>  include/linux/of.h |    3 +++
>  2 files changed, 39 insertions(+)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index c6db9ab..ff3c1fb 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -761,6 +761,42 @@ int of_property_read_string_index(struct device_node *np, const char *propname,
>  }
>  EXPORT_SYMBOL_GPL(of_property_read_string_index);
>  
> +/**
> + * of_property_match_string() - Find string in a list and return index
> + * @np: pointer to node containing string list property
> + * @propname: string list property name
> + * @string: pointer to string to search for in string list
> + *
> + * This function searches a string list property and returns the index
> + * of a specific string value.
> + */
> +int of_property_match_string(struct device_node *np, const char *propname,
> +			     const char *string)
> +{
> +	struct property *prop = of_find_property(np, propname, NULL);
> +	size_t l;
> +	int i;
> +	const char *p, *end;
> +
> +	if (!prop)
> +		return -EINVAL;
> +	if (!prop->value)
> +		return -ENODATA;
> +
> +	p = prop->value;
> +	end = p + prop->length;
> +
> +	for (i = 0; p < end; i++, p += l) {
> +		l = strlen(p) + 1;
> +		if (p + l > end)
> +			return -EILSEQ;
> +		pr_debug("comparing %s with %s\n", string, p);
> +		if (strcmp(string, p) == 0)
> +			return i; /* Found it; return index */
> +	}
> +	return -ENODATA;
> +}
> +EXPORT_SYMBOL_GPL(of_property_match_string);
>  
>  /**
>   * of_property_count_strings - Find and return the number of strings from a
> diff --git a/include/linux/of.h b/include/linux/of.h
> index ea44fd7..f02794e 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -218,6 +218,9 @@ extern int of_property_read_string(struct device_node *np,
>  extern int of_property_read_string_index(struct device_node *np,
>  					 const char *propname,
>  					 int index, const char **output);
> +extern int of_property_match_string(struct device_node *np,
> +				    const char *propname,
> +				    const char *string);
>  extern int of_property_count_strings(struct device_node *np,
>  				     const char *propname);
>  extern int of_device_is_compatible(const struct device_node *device,

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index c6db9ab..ff3c1fb 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -761,6 +761,42 @@  int of_property_read_string_index(struct device_node *np, const char *propname,
 }
 EXPORT_SYMBOL_GPL(of_property_read_string_index);
 
+/**
+ * of_property_match_string() - Find string in a list and return index
+ * @np: pointer to node containing string list property
+ * @propname: string list property name
+ * @string: pointer to string to search for in string list
+ *
+ * This function searches a string list property and returns the index
+ * of a specific string value.
+ */
+int of_property_match_string(struct device_node *np, const char *propname,
+			     const char *string)
+{
+	struct property *prop = of_find_property(np, propname, NULL);
+	size_t l;
+	int i;
+	const char *p, *end;
+
+	if (!prop)
+		return -EINVAL;
+	if (!prop->value)
+		return -ENODATA;
+
+	p = prop->value;
+	end = p + prop->length;
+
+	for (i = 0; p < end; i++, p += l) {
+		l = strlen(p) + 1;
+		if (p + l > end)
+			return -EILSEQ;
+		pr_debug("comparing %s with %s\n", string, p);
+		if (strcmp(string, p) == 0)
+			return i; /* Found it; return index */
+	}
+	return -ENODATA;
+}
+EXPORT_SYMBOL_GPL(of_property_match_string);
 
 /**
  * of_property_count_strings - Find and return the number of strings from a
diff --git a/include/linux/of.h b/include/linux/of.h
index ea44fd7..f02794e 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -218,6 +218,9 @@  extern int of_property_read_string(struct device_node *np,
 extern int of_property_read_string_index(struct device_node *np,
 					 const char *propname,
 					 int index, const char **output);
+extern int of_property_match_string(struct device_node *np,
+				    const char *propname,
+				    const char *string);
 extern int of_property_count_strings(struct device_node *np,
 				     const char *propname);
 extern int of_device_is_compatible(const struct device_node *device,