of: Add of_property_match_string() to find index into a string list
authorGrant Likely <grant.likely@secretlab.ca>
Mon, 12 Dec 2011 16:25:58 +0000 (09:25 -0700)
committerGrant Likely <grant.likely@secretlab.ca>
Sat, 28 Jan 2012 17:29:20 +0000 (10:29 -0700)
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>
arch/arm/boot/dts/testcases/tests-phandle.dtsi
drivers/of/base.c
drivers/of/selftest.c
include/linux/of.h

index ec0c4e6212c998819bf113e31c2a93eb11ecd6a6..0007d3cd7dc25c5d674309079a24bd6e725b7195 100644 (file)
@@ -31,6 +31,8 @@
                                phandle-list-bad-phandle = <12345678 0 0>;
                                phandle-list-bad-args = <&provider2 1 0>,
                                                        <&provider3 0>;
+                               empty-property;
+                               unterminated-string = [40 41 42 43];
                        };
                };
        };
index 133908a6fd8db5270f3845ce387b3724d3845b7d..13ba72875e256bcee7afd1bf1c6123d09e3c564f 100644 (file)
@@ -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
index 9d2b4803a9d67011b2de99cdbcfb6c9ceffb7dc2..f24ffd7088d20c590c14da6082c6354ff433bce4 100644 (file)
@@ -120,6 +120,34 @@ static void __init of_selftest_parse_phandle_with_args(void)
        pr_info("end - %s\n", passed_all ? "PASS" : "FAIL");
 }
 
+static void __init of_selftest_property_match_string(void)
+{
+       struct device_node *np;
+       int rc;
+
+       pr_info("start\n");
+       np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
+       if (!np) {
+               pr_err("No testcase data in device tree\n");
+               return;
+       }
+
+       rc = of_property_match_string(np, "phandle-list-names", "first");
+       selftest(rc == 0, "first expected:0 got:%i\n", rc);
+       rc = of_property_match_string(np, "phandle-list-names", "second");
+       selftest(rc == 1, "second expected:0 got:%i\n", rc);
+       rc = of_property_match_string(np, "phandle-list-names", "third");
+       selftest(rc == 2, "third expected:0 got:%i\n", rc);
+       rc = of_property_match_string(np, "phandle-list-names", "fourth");
+       selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
+       rc = of_property_match_string(np, "missing-property", "blah");
+       selftest(rc == -EINVAL, "missing property; rc=%i", rc);
+       rc = of_property_match_string(np, "empty-property", "blah");
+       selftest(rc == -ENODATA, "empty property; rc=%i", rc);
+       rc = of_property_match_string(np, "unterminated-string", "blah");
+       selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
+}
+
 static int __init of_selftest(void)
 {
        struct device_node *np;
@@ -133,6 +161,7 @@ static int __init of_selftest(void)
 
        pr_info("start of selftest - you will see error messages\n");
        of_selftest_parse_phandle_with_args();
+       of_selftest_property_match_string();
        pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
        return 0;
 }
index a75a831e2057f96e3519d100f988a584e18ac669..5a4a3adb17e5f9f30bba75909c58fd73ecb9eb7c 100644 (file)
@@ -217,6 +217,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,