summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2013-04-21 16:28:54 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2013-04-22 09:23:35 +0200
commit266dea34a53bf985ff851662309b5c07c250b4d0 (patch)
tree285aaaee4786920693cc7c92881a7ea140695d08
parent8e9d9bc7e6d9eaabd8287f5cd1db059518aec385 (diff)
downloadbarebox-266dea34a53bf985ff851662309b5c07c250b4d0.tar.gz
barebox-266dea34a53bf985ff851662309b5c07c250b4d0.tar.xz
of: Add of_property_read_string_index()
Directly imported from the Kernel Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--drivers/of/base.c46
-rw-r--r--include/of.h2
2 files changed, 48 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 65d81715b4..c086f7e231 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -579,6 +579,52 @@ int of_property_read_string(struct device_node *np, const char *propname,
}
EXPORT_SYMBOL_GPL(of_property_read_string);
+/**
+ * of_property_read_string_index - Find and read a string from a multiple
+ * strings property.
+ * @np: device node from which the property value is to be read.
+ * @propname: name of the property to be searched.
+ * @index: index of the string in the list of strings
+ * @out_string: pointer to null terminated return string, modified only if
+ * return value is 0.
+ *
+ * Search for a property in a device tree node and retrieve a null
+ * terminated string value (pointer to data, not a copy) in the list of strings
+ * contained in that property.
+ * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
+ * property does not have a value, and -EILSEQ if the string is not
+ * null-terminated within the length of the property data.
+ *
+ * The out_string pointer is modified only if a valid string can be decoded.
+ */
+int of_property_read_string_index(struct device_node *np, const char *propname,
+ int index, const char **output)
+{
+ struct property *prop = of_find_property(np, propname);
+ int i = 0;
+ size_t l = 0, total = 0;
+ const char *p;
+
+ if (!prop)
+ return -EINVAL;
+ if (!prop->value)
+ return -ENODATA;
+ if (strnlen(prop->value, prop->length) >= prop->length)
+ return -EILSEQ;
+
+ p = prop->value;
+
+ for (i = 0; total < prop->length; total += l, p += l) {
+ l = strlen(p) + 1;
+ if (i++ == index) {
+ *output = p;
+ return 0;
+ }
+ }
+ return -ENODATA;
+}
+EXPORT_SYMBOL_GPL(of_property_read_string_index);
+
struct device_node *of_get_root_node(void)
{
return root_node;
diff --git a/include/of.h b/include/of.h
index 94ccfd8739..852231e7a4 100644
--- a/include/of.h
+++ b/include/of.h
@@ -160,6 +160,8 @@ void of_delete_property(struct property *pp);
int of_property_read_string(struct device_node *np, const char *propname,
const char **out_string);
+int of_property_read_string_index(struct device_node *np, const char *propname,
+ int index, const char **output);
int of_set_property(struct device_node *node, const char *p, const void *val, int len,
int create);
struct device_node *of_create_node(struct device_node *root, const char *path);