From patchwork Fri Mar 14 17:11:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Grant Likely X-Patchwork-Id: 26273 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pb0-f72.google.com (mail-pb0-f72.google.com [209.85.160.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D789620143 for ; Fri, 14 Mar 2014 17:12:30 +0000 (UTC) Received: by mail-pb0-f72.google.com with SMTP id jt11sf6604211pbb.3 for ; Fri, 14 Mar 2014 10:12:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=h2FrAw/rD9BcoT3NnGWDVjbOnWmbIdJVYr8KSYtRnY0=; b=YEU6tjS07eeENIAFxsoEvSKrMuTeDo28EvMo05BPnF+uY93FD770LANTdW8YW2IKI/ VSOy20VGzbwtZROca99i+MLBl2tY5IDfnoe5xSCiZbWIR8/aj09pOnULWCLX34M/Xr0+ cZWcwX9yvFmD65Yhx3C8LUH8b6oa+3jgGxIh3fsayNngFisznsxOILuEMK1ritJ+Jq9z bsuIfPLsuIDlX9sVUIYsdY/Lh8oQ8dTmZe+NdNpRgVnX1eJqrFT3+A+rgYYXuzrohkre U2zYIZPOyF3VNIiARDy5eW8C8NZGWj6tD9dw6l6cBLStIgckICdgfQ2+VZDjBotHWgD/ u+CQ== X-Gm-Message-State: ALoCoQnWPo3cykbUWOYRNqumnF3VRqJi4a6Y9dCZT8hn5c7+zYZQaZNj2q3hreTbbZIY62/bV1Lv X-Received: by 10.66.190.202 with SMTP id gs10mr3596129pac.0.1394817150159; Fri, 14 Mar 2014 10:12:30 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.97.70 with SMTP id l64ls768877qge.16.gmail; Fri, 14 Mar 2014 10:12:30 -0700 (PDT) X-Received: by 10.220.200.6 with SMTP id eu6mr645097vcb.35.1394817150054; Fri, 14 Mar 2014 10:12:30 -0700 (PDT) Received: from mail-vc0-f174.google.com (mail-vc0-f174.google.com [209.85.220.174]) by mx.google.com with ESMTPS id jl10si723975veb.177.2014.03.14.10.12.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 14 Mar 2014 10:12:30 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.174 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.174; Received: by mail-vc0-f174.google.com with SMTP id ld13so3068029vcb.33 for ; Fri, 14 Mar 2014 10:12:30 -0700 (PDT) X-Received: by 10.52.15.132 with SMTP id x4mr1613782vdc.31.1394817149955; Fri, 14 Mar 2014 10:12:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp35767vck; Fri, 14 Mar 2014 10:12:29 -0700 (PDT) X-Received: by 10.66.156.137 with SMTP id we9mr10548728pab.30.1394817149123; Fri, 14 Mar 2014 10:12:29 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i7si4255336pav.342.2014.03.14.10.12.28; Fri, 14 Mar 2014 10:12:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756213AbaCNRMK (ORCPT + 9 others); Fri, 14 Mar 2014 13:12:10 -0400 Received: from mail-ee0-f44.google.com ([74.125.83.44]:57320 "EHLO mail-ee0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755432AbaCNRME (ORCPT ); Fri, 14 Mar 2014 13:12:04 -0400 Received: by mail-ee0-f44.google.com with SMTP id e49so1626316eek.17 for ; Fri, 14 Mar 2014 10:12:03 -0700 (PDT) X-Received: by 10.14.193.201 with SMTP id k49mr9408940een.50.1394817123062; Fri, 14 Mar 2014 10:12:03 -0700 (PDT) Received: from trevor.secretlab.ca (host109-153-30-112.range109-153.btcentralplus.com. [109.153.30.112]) by mx.google.com with ESMTPSA id y51sm18420252eeu.0.2014.03.14.10.12.01 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 14 Mar 2014 10:12:01 -0700 (PDT) Received: by trevor.secretlab.ca (Postfix, from userid 1000) id EC82BC40A1B; Fri, 14 Mar 2014 17:11:58 +0000 (GMT) From: Grant Likely To: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: Pantelis Antoniou , Grant Likely , David Daney Subject: [RFC v3 3/3] of: Make of_find_node_by_path() handle /aliases Date: Fri, 14 Mar 2014 17:11:49 +0000 Message-Id: <1394817109-26199-4-git-send-email-grant.likely@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1394817109-26199-1-git-send-email-grant.likely@linaro.org> References: <1394817109-26199-1-git-send-email-grant.likely@linaro.org> Sender: devicetree-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: devicetree@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: grant.likely@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.174 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Make of_find_node_by_path() handle aliases as prefixes. To make this work the name search is refactored to search by path component instead of by full string. This should be a more efficient search, and it makes it possible to start a search at a subnode of a tree. Signed-off-by: David Daney Signed-off-by: Pantelis Antoniou [grant.likely: Rework to not require allocating at runtime] Signed-off-by: Grant Likely Acked-by: Rob Herring --- drivers/of/base.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 7083fad079a6..b8b673526dbd 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -770,9 +770,38 @@ struct device_node *of_get_child_by_name(const struct device_node *node, } EXPORT_SYMBOL(of_get_child_by_name); +static struct device_node *__of_find_node_by_path(struct device_node *parent, + const char *path) +{ + struct device_node *child; + int len = strchrnul(path, '/') - path; + + if (!len) + return parent; + + for_each_child_of_node(parent, child) { + const char *name = strrchr(child->full_name, '/'); + if (WARN(!name, "malformed device_node %s\n", child->full_name)) + continue; + name++; + if (strncmp(path, name, len) == 0 && (strlen(name) == len)) + return child; + } + return NULL; +} + /** * of_find_node_by_path - Find a node matching a full OF path * @path: The full path to match + * @path: Either the full path to match, or if the path does not + * start with '/', the name of a property of the /aliases + * node (an alias). In the case of an alias, the node + * matching the alias' value will be returned. + * + * Valid paths: + * /foo/bar Full path + * foo Valid alias + * foo/bar Valid alias + relative path * * Returns a node pointer with refcount incremented, use * of_node_put() on it when done. @@ -780,13 +809,36 @@ EXPORT_SYMBOL(of_get_child_by_name); struct device_node *of_find_node_by_path(const char *path) { struct device_node *np = of_allnodes; + struct property *pp; unsigned long flags; + /* The path could begin with an alias */ + if (*path != '/') { + char *p = strchrnul(path, '/'); + int len = p - path; + + /* of_aliases must not be NULL */ + if (!of_aliases) + return NULL; + + np = NULL; + for_each_property_of_node(of_aliases, pp) { + if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { + np = of_find_node_by_path(pp->value); + break; + } + } + if (!np) + return NULL; + path = p; + } + + /* Step down the tree matching path components */ raw_spin_lock_irqsave(&devtree_lock, flags); - for (; np; np = np->allnext) { - if (np->full_name && (of_node_cmp(np->full_name, path) == 0) - && of_node_get(np)) - break; + while (np && *path == '/') { + path++; /* Increment past '/' delimiter */ + np = __of_find_node_by_path(np, path); + path = strchrnul(path, '/'); } raw_spin_unlock_irqrestore(&devtree_lock, flags); return np;