diff mbox

[RFC,11/31] xen/device-tree: Add dt_property_count_elems_of_size helper

Message ID 1510247421-24094-12-git-send-email-olekstysh@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Oleksandr Tyshchenko Nov. 9, 2017, 5:10 p.m. UTC
From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>

This is a port from Linux.

Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Julien Grall <julien.grall@linaro.org>
---
 xen/common/device_tree.c      | 20 ++++++++++++++++++++
 xen/include/xen/device_tree.h | 15 +++++++++++++++
 2 files changed, 35 insertions(+)
diff mbox

Patch

diff --git a/xen/common/device_tree.c b/xen/common/device_tree.c
index 0fa654e..7b4cad3 100644
--- a/xen/common/device_tree.c
+++ b/xen/common/device_tree.c
@@ -278,6 +278,26 @@  const char *dt_property_next_string(const struct dt_property *prop,
     return curv;
 }
 
+int dt_property_count_elems_of_size(const struct dt_device_node *np,
+                                    const char *propname, int elem_size)
+{
+    const struct dt_property *prop = dt_find_property(np, propname, NULL);
+
+    if ( !prop )
+        return -EINVAL;
+    if ( !prop->value )
+        return -ENODATA;
+
+    if ( prop->length % elem_size != 0 )
+    {
+        printk("%s: size of %s is not a multiple of %d\n", np->full_name,
+               propname, elem_size);
+        return -EINVAL;
+    }
+
+    return prop->length / elem_size;
+}
+
 bool_t dt_device_is_compatible(const struct dt_device_node *device,
                                const char *compat)
 {
diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h
index 87b4b67..e2d7346 100644
--- a/xen/include/xen/device_tree.h
+++ b/xen/include/xen/device_tree.h
@@ -461,6 +461,21 @@  const char *dt_property_next_string(const struct dt_property *prop,
         s = dt_property_next_string(prop, s))
 
 /**
+ * dt_property_count_elems_of_size - Count the number of elements in a property
+ *
+ * @np:        device node from which the property value is to be read.
+ * @propname:  name of the property to be searched.
+ * @elem_size: size of the individual element
+ *
+ * Search for a property in a device node and count the number of elements of
+ * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
+ * property does not exist or its length does not match a multiple of elem_size
+ * and -ENODATA if the property does not have a value.
+ */
+int dt_property_count_elems_of_size(const struct dt_device_node *np,
+                                    const char *propname, int elem_size);
+
+/**
  * Checks if the given "compat" string matches one of the strings in
  * the device's "compatible" property
  */