From patchwork Thu Oct 3 18:51:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Kleine-Budde X-Patchwork-Id: 2985341 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2655ABFF0B for ; Thu, 3 Oct 2013 18:57:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F28C720306 for ; Thu, 3 Oct 2013 18:57:07 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 813B820304 for ; Thu, 3 Oct 2013 18:57:06 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VRo52-0004RY-2z; Thu, 03 Oct 2013 18:56:56 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VRo0A-00058v-98; Thu, 03 Oct 2013 18:51:54 +0000 Received: from metis.ext.pengutronix.de ([2001:6f8:1178:4:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VRo07-00058E-ER for linux-arm-kernel@lists.infradead.org; Thu, 03 Oct 2013 18:51:52 +0000 Received: from dude.hi.pengutronix.de ([2001:6f8:1178:2:21e:67ff:fe11:9c5c]) by metis.ext.pengutronix.de with esmtp (Exim 4.72) (envelope-from ) id 1VRnzi-00072D-05; Thu, 03 Oct 2013 20:51:26 +0200 Received: from mkl by dude.hi.pengutronix.de with local (Exim 4.80) (envelope-from ) id 1VRnzh-0004nB-Dt; Thu, 03 Oct 2013 20:51:25 +0200 From: Marc Kleine-Budde To: LKML Subject: [PATCH|RFC] of: let of_match_device() always return best match Date: Thu, 3 Oct 2013 20:51:24 +0200 Message-Id: <1380826284-18381-1-git-send-email-mkl@pengutronix.de> X-Mailer: git-send-email 1.8.4.rc3 X-SA-Exim-Connect-IP: 2001:6f8:1178:2:21e:67ff:fe11:9c5c X-SA-Exim-Mail-From: mkl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131003_145151_859797_549C1B99 X-CRM114-Status: GOOD ( 20.63 ) X-Spam-Score: -2.6 (--) Cc: devicetree@vger.kernel.org, Rob Herring , Marc Kleine-Budde , "kernel@pengutronix.de" , Grant Likely , linux-arm-kernel X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The function of_match_device() should tell if a struct device matches an of_device_id list and return the specific entry of that table matches the device best. The underlying __of_match_node() implements the wrong search algorithm. It iterates over the list of of_device_ids, comparing the first compatible with _all_ compatibles of the struct device, then the second compatible of of_device_id and so on. This leads to a problem, if the device has more than one compatible that match the of_device_id list. The implemented search algorithm may find not the "best" match. As the compatible list in the device is sorted from most to least specific. For example: The imx28.dtsi gives this compatible string for its CAN core: > compatible = "fsl,imx28-flexcan", "fsl,p1010-flexcan"; The flexcan driver defines: > static const struct of_device_id flexcan_of_match[] = { > { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, }, > { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, }, > { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, }, > { /* sentinel */ }, > }; The "p1010" was the first Freescale SoC with the flexcan core. But this SoC has a bug, so a workaround has to be enabled in the driver. The mx28 has this bug fixed, so we don't need this quite costly workaround. The __of_match_node() will compare: from of_device_id from device fsl,p1010-flexcan fsl,imx28-flexcan fsl,p1010-flexcan fsl,p1010-flexcan -> MATCH The of_match_device() function as it currently is implemented will always return p1010 not the mx28. This patch fixes the problem by exchanging outer and inner loop. The first compatible of a device is compared against all compatible from the of_device_id list, then the second device compatible and so on. Signed-off-by: Marc Kleine-Budde --- drivers/of/base.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 865d3f6..5bff2fe 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -349,6 +349,35 @@ static int __of_device_is_compatible(const struct device_node *device, return 0; } +/** Returns the specific of_device_id, if the it matches one of the + * strings in the device's "compatible" property + */ +static +const struct of_device_id *__of_device_matches_compatible(const struct device_node *device, + const struct of_device_id *matches) +{ + const struct of_device_id *m; + const char* cp; + int cplen, l; + + cp = __of_get_property(device, "compatible", &cplen); + if (cp == NULL) + return NULL; + while (cplen > 0) { + m = matches; + while (m->compatible[0]) { + if (of_compat_cmp(cp, m->compatible, strlen(m->compatible)) == 0) + return m; + m++; + } + l = strlen(cp) + 1; + cp += l; + cplen -= l; + } + + return NULL; +} + /** Checks if the given "compat" string matches one of the strings in * the device's "compatible" property */ @@ -717,22 +746,23 @@ const struct of_device_id *__of_match_node(const struct of_device_id *matches, { if (!matches) return NULL; - - while (matches->name[0] || matches->type[0] || matches->compatible[0]) { + while (matches->name[0] || matches->type[0]) { int match = 1; + if (matches->name[0]) match &= node->name && !strcmp(matches->name, node->name); if (matches->type[0]) match &= node->type && !strcmp(matches->type, node->type); - if (matches->compatible[0]) - match &= __of_device_is_compatible(node, - matches->compatible); if (match) return matches; matches++; } + + if (matches->compatible[0]) + return __of_device_matches_compatible(node, matches); + return NULL; }