All of lore.kernel.org
 help / color / mirror / Atom feed
From: Grant Likely <grant.likely@linaro.org>
To: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Cc: Rob Herring <robh+dt@kernel.org>, Mark Brown <broonie@kernel.org>,
	Wolfram Sang <wsa@the-dreams.de>,
	Grant Likely <grant.likely@linaro.org>
Subject: [PATCH v2 02/14] of: Refactor __of_node_alloc() into __of_node_dup()
Date: Mon, 24 Nov 2014 22:33:30 +0000	[thread overview]
Message-ID: <1416868422-22103-3-git-send-email-grant.likely@linaro.org> (raw)
In-Reply-To: <1416868422-22103-1-git-send-email-grant.likely@linaro.org>

Add a node argument to __of_node_alloc() and rename it to
__of_node_dup() so that it can also be used to duplicate a node with
its properties. This is important for the overlay code so that it can
create new nodes without using separate changeset items for every single
property.

At the same time rework the overlay code to use the new function and
drop the extra changeset items.

Signed-off-by: Grant Likely <grant.likely@linaro.org>
---
 drivers/of/dynamic.c    | 40 +++++++++++++++++++++++++++++-----------
 drivers/of/of_private.h |  2 +-
 drivers/of/unittest.c   | 14 ++++++++++----
 3 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index af1b1ecd6a3d..661ad2f5c00c 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -272,15 +272,16 @@ struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
 }
 
 /**
- * __of_node_alloc() - Create an empty device node dynamically.
- * @full_name:	Full name of the new device node
+ * __of_node_dup() - Duplicate or create an empty device node dynamically.
+ * @fmt: Format string (plus vargs) for new full name of the device node
  *
- * Create an empty device tree node, suitable for further modification.
- * The node data are dynamically allocated and all the node flags
- * have the OF_DYNAMIC & OF_DETACHED bits set.
- * Returns the newly allocated node or NULL on out of memory error.
+ * Create an device tree node, either by duplicating an empty node or by allocating
+ * an empty one suitable for further modification.  The node data are
+ * dynamically allocated and all the node flags have the OF_DYNAMIC &
+ * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of
+ * memory error.
  */
-struct device_node *__of_node_alloc(const char *fmt, ...)
+struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
 {
 	va_list vargs;
 	struct device_node *node;
@@ -291,17 +292,34 @@ struct device_node *__of_node_alloc(const char *fmt, ...)
 	va_start(vargs, fmt);
 	node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
 	va_end(vargs);
-	if (!node->full_name)
-		goto err_free;
+	if (!node->full_name) {
+		kfree(node);
+		return NULL;
+	}
 
 	of_node_set_flag(node, OF_DYNAMIC);
 	of_node_set_flag(node, OF_DETACHED);
 	of_node_init(node);
 
+	/* Iterate over and duplicate all properties */
+	if (np) {
+		struct property *pp, *new_pp;
+		for_each_property_of_node(np, pp) {
+			new_pp = __of_prop_dup(pp, GFP_KERNEL);
+			if (!new_pp)
+				goto err_prop;
+			if (__of_add_property(node, new_pp)) {
+				kfree(new_pp->name);
+				kfree(new_pp->value);
+				kfree(new_pp);
+				goto err_prop;
+			}
+		}
+	}
 	return node;
 
- err_free:
-	kfree(node);
+ err_prop:
+	of_node_put(node); /* Frees the node and properties */
 	return NULL;
 }
 
diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h
index 618abcad307e..8e882e706cd8 100644
--- a/drivers/of/of_private.h
+++ b/drivers/of/of_private.h
@@ -61,7 +61,7 @@ static inline int of_property_notify(int action, struct device_node *np,
  * own the devtree lock or work on detached trees only.
  */
 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags);
-__printf(1, 2) struct device_node *__of_node_alloc(const char *fmt, ...);
+__printf(2, 3) struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...);
 
 extern const void *__of_get_property(const struct device_node *np,
 				     const char *name, int *lenp);
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 7634a17af1d5..1720b039cac7 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -445,15 +445,15 @@ static void __init of_selftest_changeset(void)
 	struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
 	struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
 	struct property *ppremove;
-	struct device_node *n1, *n2, *n21, *nremove, *parent;
+	struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
 	struct of_changeset chgset;
 
 	of_changeset_init(&chgset);
-	n1 = __of_node_alloc("/testcase-data/changeset/n1");
+	n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
 	selftest(n1, "testcase setup failure\n");
-	n2 = __of_node_alloc("/testcase-data/changeset/n2");
+	n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
 	selftest(n2, "testcase setup failure\n");
-	n21 = __of_node_alloc("/testcase-data/changeset/n2/n21");
+	n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
 	selftest(n21, "testcase setup failure %p\n", n21);
 	nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
 	selftest(nremove, "testcase setup failure\n");
@@ -481,6 +481,12 @@ static void __init of_selftest_changeset(void)
 	selftest(!of_changeset_apply(&chgset), "apply failed\n");
 	mutex_unlock(&of_mutex);
 
+	/* Make sure node names are constructed correctly */
+	selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
+		 "'%s' not added\n", n21->full_name);
+	if (np)
+		of_node_put(np);
+
 	mutex_lock(&of_mutex);
 	selftest(!of_changeset_revert(&chgset), "revert failed\n");
 	mutex_unlock(&of_mutex);
-- 
1.9.1


  parent reply	other threads:[~2014-11-24 22:38 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-24 22:33 [PATCH v2 00/14] of: DT Overlay patches to be merged Grant Likely
2014-11-24 22:33 ` Grant Likely
2014-11-24 22:33 ` [PATCH v2 01/14] of: Use vargs in __of_node_alloc Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-24 22:33 ` Grant Likely [this message]
2014-11-24 22:33 ` [PATCH v2 03/14] of/resolver: Switch to new local fixups format Grant Likely
2014-11-24 22:33 ` [PATCH v2 04/14] of/reconfig: Add of_reconfig_get_state_change() of notifier helper Grant Likely
2014-11-24 22:33 ` [PATCH v2 05/14] of/reconfig: Add empty stubs for the of_reconfig methods Grant Likely
2014-11-24 22:33 ` [PATCH v2 06/14] of/reconfig: Add debug output for OF_RECONFIG notifiers Grant Likely
2014-11-24 22:33 ` [PATCH v2 07/14] of/reconfig: Always use the same structure for notifiers Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-25 23:07   ` Benjamin Herrenschmidt
2014-11-25 23:07     ` Benjamin Herrenschmidt
2014-11-25 23:07     ` Benjamin Herrenschmidt
2014-11-26  3:11     ` Nathan Fontenot
2014-11-26 13:16       ` Grant Likely
2014-11-26 13:16         ` Grant Likely
2014-11-24 22:33 ` [PATCH v2 08/14] of/reconfig: Add OF_DYNAMIC notifier for platform_bus_type Grant Likely
2014-11-24 22:33 ` [PATCH v2 09/14] of/overlay: Introduce DT overlay support Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-24 22:33 ` [PATCH v2 10/14] of/overlay: Add overlay unittests Grant Likely
2014-11-24 22:33 ` [PATCH v2 11/14] i2c/of: Factor out Devicetree registration code Grant Likely
2014-11-25 14:11   ` Wolfram Sang
2014-11-24 22:33 ` [PATCH v2 12/14] i2c/of: Add OF_RECONFIG notifier handler Grant Likely
2014-11-25 14:11   ` Wolfram Sang
2014-11-24 22:33 ` [PATCH v2 13/14] spi/of: Create new device registration method and accessors Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-25 11:32   ` Mark Brown
2014-11-24 22:33 ` [PATCH v2 14/14] spi/of: Add OF notifier handler Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-24 22:33   ` Grant Likely
2014-11-25 11:44   ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1416868422-22103-3-git-send-email-grant.likely@linaro.org \
    --to=grant.likely@linaro.org \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pantelis.antoniou@konsulko.com \
    --cc=robh+dt@kernel.org \
    --cc=wsa@the-dreams.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.