dt: add helper functions to read u32 and string property values
authorThomas Abraham <thomas.abraham@linaro.org>
Thu, 30 Jun 2011 15:56:10 +0000 (21:26 +0530)
committerGrant Likely <grant.likely@secretlab.ca>
Thu, 30 Jun 2011 19:02:10 +0000 (13:02 -0600)
Add helper functions to retrieve unsigned integer and string property
values from properties of a device node. These helper functions can be
used to lookup a property in a device node, perform error checking and
read the property value.

[grant.likely@secretlab.ca: Proposal and initial implementation]
Signed-off-by: Thomas Abraham <thomas.abraham@linaro.org>
[grant.likely: some word smithing and be more defensive validating the string]
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
drivers/of/base.c
include/linux/of.h

index 632ebae7f17a4444f7aa3ad193bdbce0212c6c09..b8b65fddbeb5979a668c705f5878724bcec519be 100644 (file)
@@ -595,6 +595,64 @@ struct device_node *of_find_node_by_phandle(phandle handle)
 }
 EXPORT_SYMBOL(of_find_node_by_phandle);
 
+/**
+ * of_property_read_u32 - Find and read a 32 bit integer from a property
+ * @np:                device node from which the property value is to be read.
+ * @propname:  name of the property to be searched.
+ * @out_value: pointer to return value, modified only if return value is 0.
+ *
+ * Search for a property in a device node and read a 32-bit value from
+ * it. Returns 0 on success, -EINVAL if the property does not exist,
+ * -ENODATA if property does not have a value, and -EOVERFLOW if the
+ * property data isn't large enough.
+ *
+ * The out_value is modified only if a valid u32 value can be decoded.
+ */
+int of_property_read_u32(struct device_node *np, char *propname, u32 *out_value)
+{
+       struct property *prop = of_find_property(np, propname, NULL);
+
+       if (!prop)
+               return -EINVAL;
+       if (!prop->value)
+               return -ENODATA;
+       if (sizeof(*out_value) > prop->length)
+               return -EOVERFLOW;
+       *out_value = be32_to_cpup(prop->value);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(of_property_read_u32);
+
+/**
+ * of_property_read_string - Find and read a string from a property
+ * @np:                device node from which the property value is to be read.
+ * @propname:  name of the property to be searched.
+ * @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). 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(struct device_node *np, char *propname,
+                               char **out_string)
+{
+       struct property *prop = of_find_property(np, propname, NULL);
+       if (!prop)
+               return -EINVAL;
+       if (!prop->value)
+               return -ENODATA;
+       if (strnlen(prop->value, prop->length) >= prop->length)
+               return -EILSEQ;
+       *out_string = prop->value;
+       return 0;
+}
+EXPORT_SYMBOL_GPL(of_property_read_string);
+
 /**
  * of_parse_phandle - Resolve a phandle property to a device_node pointer
  * @np: Pointer to device node holding phandle property
index bfc0ed1b0ced1405a5d6544a4a855f2a94e00326..4fc4c1b8d5d59e6617c9109489ff9bad5c91d787 100644 (file)
@@ -195,6 +195,10 @@ extern struct device_node *of_find_node_with_property(
 extern struct property *of_find_property(const struct device_node *np,
                                         const char *name,
                                         int *lenp);
+extern int of_property_read_u32(struct device_node *np, char *propname,
+                                       u32 *out_value);
+extern int of_property_read_string(struct device_node *np, char *propname,
+                                       char **out_string);
 extern int of_device_is_compatible(const struct device_node *device,
                                   const char *);
 extern int of_device_is_available(const struct device_node *device);
This page took 0.029975 seconds and 5 git commands to generate.