From patchwork Fri Oct 26 14:48:44 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 1652211 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id AA05F3FD4E for ; Fri, 26 Oct 2012 14:52:37 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TRlEJ-0003ZF-CT; Fri, 26 Oct 2012 14:49:47 +0000 Received: from service87.mimecast.com ([91.220.42.44]) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TRlEG-0003Yt-BM for linux-arm-kernel@lists.infradead.org; Fri, 26 Oct 2012 14:49:45 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Fri, 26 Oct 2012 15:49:42 +0100 Received: from localhost.localdomain ([10.1.255.212]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.0); Fri, 26 Oct 2012 15:49:40 +0100 From: Mark Rutland To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 1/2] of: add of_property_count_phandles Date: Fri, 26 Oct 2012 15:48:44 +0100 Message-Id: <1351262925-10306-2-git-send-email-mark.rutland@arm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1351262925-10306-1-git-send-email-mark.rutland@arm.com> References: <1351262925-10306-1-git-send-email-mark.rutland@arm.com> X-OriginalArrivalTime: 26 Oct 2012 14:49:40.0277 (UTC) FILETIME=[209E9E50:01CDB389] X-MC-Unique: 112102615494219601 X-Spam-Note: CRM114 invocation failed X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [91.220.42.44 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Mark Rutland , lorenzo.pieralisi@arm.com, benh@kernel.crashing.org, devicetree-discuss@lists.ozlabs.org, will.deacon@arm.com, rob.herring@calxeda.com, grant.likely@secretlab.ca, tglx@linutronix.de X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Though of_parse_phandle handles lists of phandles, and takes an index parameter, there is no standard way of discovering how many phandles are present on a node. This patch adds a function to count how many phandles are in a phandle list. Signed-off-by: Mark Rutland --- drivers/of/base.c | 39 +++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 8 ++++++++ 2 files changed, 47 insertions(+), 0 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index af3b22a..30ae562 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -884,6 +884,45 @@ int of_property_count_strings(struct device_node *np, const char *propname) EXPORT_SYMBOL_GPL(of_property_count_strings); /** + * of_property_count_phandles - find and return the number of phandles from a + * multiple phandles property. + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * + * Search for a property in a device tree node and retrieve the number of + * phandles contained in it. Returns the number of phandles on success, -ENOENT + * if the property does not exist, -ENODATA if there are no phandles, and + * -EINVAL if the property is not correctly sized for an array of phandles. + */ +int of_property_count_phandles(struct device_node *np, const char *propname) +{ + int size, count, i; + const __be32 *phandle; + + phandle = of_get_property(np, propname, &size); + + if (!phandle) + return -ENOENT; + if (size == 0) + return -ENODATA; + if (size % (sizeof *phandle)) + return -EINVAL; + + count = size / (sizeof(*phandle)); + + /* Check the phandle targets actually exist */ + for (i = 0; i < count; i++) { + struct device_node *node = of_parse_phandle(np, propname, i); + if (!node) + return -EINVAL; + of_node_put(node); + } + + return count; +} +EXPORT_SYMBOL_GPL(of_property_count_phandles); + +/** * of_parse_phandle - Resolve a phandle property to a device_node pointer * @np: Pointer to device node holding phandle property * @phandle_name: Name of property holding a phandle value diff --git a/include/linux/of.h b/include/linux/of.h index b4e50d5..1f03f46 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -258,6 +258,8 @@ extern int of_modalias_node(struct device_node *node, char *modalias, int len); extern struct device_node *of_parse_phandle(struct device_node *np, const char *phandle_name, int index); +extern int of_property_count_phandles(struct device_node *np, + const char *propname); extern int of_parse_phandle_with_args(struct device_node *np, const char *list_name, const char *cells_name, int index, struct of_phandle_args *out_args); @@ -411,6 +413,12 @@ static inline int of_property_match_string(struct device_node *np, return -ENOSYS; } +static inline int of_property_count_phandles(struct device_node *np, + const char *propname) +{ + return -ENOSYS; +} + static inline struct device_node *of_parse_phandle(struct device_node *np, const char *phandle_name, int index)