diff options
author | Thomas Abraham <thomas.abraham@linaro.org> | 2011-06-30 11:56:10 -0400 |
---|---|---|
committer | Grant Likely <grant.likely@secretlab.ca> | 2011-06-30 15:02:10 -0400 |
commit | a3b853633d78c3930b513ee219df48637ac82eed (patch) | |
tree | 30d36c4b3e03db070f72eeaeed3f2d1dd1da159b /drivers/of/base.c | |
parent | 7423734e19e7e0a90e3379152eacca2647f4377e (diff) |
dt: add helper functions to read u32 and string property values
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>
Diffstat (limited to 'drivers/of/base.c')
-rw-r--r-- | drivers/of/base.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c index 632ebae7f17a..b8b65fddbeb5 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c | |||
@@ -596,6 +596,64 @@ struct device_node *of_find_node_by_phandle(phandle handle) | |||
596 | EXPORT_SYMBOL(of_find_node_by_phandle); | 596 | EXPORT_SYMBOL(of_find_node_by_phandle); |
597 | 597 | ||
598 | /** | 598 | /** |
599 | * of_property_read_u32 - Find and read a 32 bit integer from a property | ||
600 | * @np: device node from which the property value is to be read. | ||
601 | * @propname: name of the property to be searched. | ||
602 | * @out_value: pointer to return value, modified only if return value is 0. | ||
603 | * | ||
604 | * Search for a property in a device node and read a 32-bit value from | ||
605 | * it. Returns 0 on success, -EINVAL if the property does not exist, | ||
606 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | ||
607 | * property data isn't large enough. | ||
608 | * | ||
609 | * The out_value is modified only if a valid u32 value can be decoded. | ||
610 | */ | ||
611 | int of_property_read_u32(struct device_node *np, char *propname, u32 *out_value) | ||
612 | { | ||
613 | struct property *prop = of_find_property(np, propname, NULL); | ||
614 | |||
615 | if (!prop) | ||
616 | return -EINVAL; | ||
617 | if (!prop->value) | ||
618 | return -ENODATA; | ||
619 | if (sizeof(*out_value) > prop->length) | ||
620 | return -EOVERFLOW; | ||
621 | *out_value = be32_to_cpup(prop->value); | ||
622 | return 0; | ||
623 | } | ||
624 | EXPORT_SYMBOL_GPL(of_property_read_u32); | ||
625 | |||
626 | /** | ||
627 | * of_property_read_string - Find and read a string from a property | ||
628 | * @np: device node from which the property value is to be read. | ||
629 | * @propname: name of the property to be searched. | ||
630 | * @out_string: pointer to null terminated return string, modified only if | ||
631 | * return value is 0. | ||
632 | * | ||
633 | * Search for a property in a device tree node and retrieve a null | ||
634 | * terminated string value (pointer to data, not a copy). Returns 0 on | ||
635 | * success, -EINVAL if the property does not exist, -ENODATA if property | ||
636 | * does not have a value, and -EILSEQ if the string is not null-terminated | ||
637 | * within the length of the property data. | ||
638 | * | ||
639 | * The out_string pointer is modified only if a valid string can be decoded. | ||
640 | */ | ||
641 | int of_property_read_string(struct device_node *np, char *propname, | ||
642 | char **out_string) | ||
643 | { | ||
644 | struct property *prop = of_find_property(np, propname, NULL); | ||
645 | if (!prop) | ||
646 | return -EINVAL; | ||
647 | if (!prop->value) | ||
648 | return -ENODATA; | ||
649 | if (strnlen(prop->value, prop->length) >= prop->length) | ||
650 | return -EILSEQ; | ||
651 | *out_string = prop->value; | ||
652 | return 0; | ||
653 | } | ||
654 | EXPORT_SYMBOL_GPL(of_property_read_string); | ||
655 | |||
656 | /** | ||
599 | * of_parse_phandle - Resolve a phandle property to a device_node pointer | 657 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
600 | * @np: Pointer to device node holding phandle property | 658 | * @np: Pointer to device node holding phandle property |
601 | * @phandle_name: Name of property holding a phandle value | 659 | * @phandle_name: Name of property holding a phandle value |