[PATCH 07/29] of: Add of_set_property and of_create_node

Sascha Hauer s.hauer at pengutronix.de
Tue Feb 26 15:18:34 EST 2013


Add functions to create a new device node and to create/set
a new property based on the nodepath.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/of/base.c |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/of.h      |    3 ++
 2 files changed, 82 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index d6c346d..ecc49c4 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -662,6 +662,41 @@ void of_delete_property(struct property *pp)
 	free(pp);
 }
 
+/**
+ * of_set_property - create a property for a given node
+ * @node - the node
+ * @name - the name of the property
+ * @val - the value for the property
+ * @len - the length of the properties value
+ * @create - if true, the property is created if not existing already
+ */
+int of_set_property(struct device_node *np, const char *name, const void *val, int len,
+		int create)
+{
+	struct property *pp;
+
+	if (!np)
+		return -ENOENT;
+
+	pp = of_find_property(np, name);
+	if (pp) {
+		void *data;
+
+		free(pp->value);
+		data = xzalloc(len);
+		memcpy(data, val, len);
+		pp->value = data;
+		pp->length = len;
+	} else {
+		if (!create)
+			return -ENOENT;
+
+		pp = of_new_property(np, name, val, len);
+	}
+
+	return 0;
+}
+
 static struct device_d *add_of_device(struct device_node *node)
 {
 	struct device_d *dev;
@@ -886,6 +921,50 @@ struct device_node *of_find_child_by_name(struct device_node *node, const char *
 	return NULL;
 }
 
+/**
+ * of_create_node - create a new node including its parents
+ * @path - the nodepath to create
+ */
+struct device_node *of_create_node(struct device_node *root, const char *path)
+{
+	char *slash, *p, *freep;
+	struct device_node *tmp, *dn = root;
+
+	if (*path != '/')
+		return NULL;
+
+	path++;
+
+	p = freep = xstrdup(path);
+
+	while (1) {
+		if (!*p)
+			goto out;
+
+		slash = strchr(p, '/');
+		if (slash)
+			*slash = 0;
+
+		tmp = of_find_child_by_name(dn, p);
+		if (tmp)
+			dn = tmp;
+		else
+			dn = of_new_node(dn, p);
+
+		if (!dn)
+			goto out;
+
+		if (!slash)
+			goto out;
+
+		p = slash + 1;
+	}
+out:
+	free(freep);
+
+	return dn;
+}
+
 /*
  * Parse a flat device tree binary blob and store it in the barebox
  * internal tree format,
diff --git a/include/of.h b/include/of.h
index 933e855..12c5703 100644
--- a/include/of.h
+++ b/include/of.h
@@ -124,6 +124,9 @@ 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_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);
 
 #ifdef CONFIG_OFDEVICE
 int of_parse_partitions(const char *cdevname,
-- 
1.7.10.4




More information about the barebox mailing list