From patchwork Mon Jul 24 15:14:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Herring X-Patchwork-Id: 108587 Delivered-To: patch@linaro.org Received: by 10.182.45.195 with SMTP id p3csp3962992obm; Mon, 24 Jul 2017 08:14:48 -0700 (PDT) X-Received: by 10.101.83.197 with SMTP id z5mr16564875pgr.261.1500909288818; Mon, 24 Jul 2017 08:14:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500909288; cv=none; d=google.com; s=arc-20160816; b=gu3z6u22xpFDUokrVdYxAv8GwykiCVCum4fX56l35Fe8GyX6qIkU/5X+h5Sn0pRQTL kYjkiWV61JxUTHJXcfW86WO2DcD0x3baQ/dDQmt9dChmlUenYIkHrWAMY/E1S3E/bKb3 zs9GAEJCT6kl1LgAF5QFDXYUWB30ZA4ThPhA8VrQ6cPGszLHC14Gxhjpxp9cQLr2A0y0 XCeFJxZW0hzXDhknCwOeOaRR4/XiQTsvvHqyYAxjDJ3A1Su3i49oY132lVANxEEo8u/V gH3UBNGJ061psyEVs4JX+7HGNYpi/4nn3pk61AWbLgijVzHRZqPoqFhQdHZL/65yoznx DRNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=QDEPw9LRe57YX09v5DP1OELWxR5cCef7RmmMM8YRLh8=; b=xfHZfN+ZXuIm9TtdxG+8jW4Mpz+0XcqKUnGw4gfBKOmvyqlasxdFe2NXRDVEinc1M2 MEnlRbtlNIsdcbf6PlG1TChSYCnKgYH9V8kz18Bj0CZSseeJileQnSYKkEHdHD+GfBlj I6xmG621l1hjv1Ussghop3SOr3/nIJgJQy5P7Ndf36wS30W5w/X1ezOShePBj6n5J3B6 twW6s0MUjxoGs5eGIg26Lbb+5AYjDyKcKLtdAFyqJZ7DwTwlvthqop1EZSQ58Hwk9cm6 EJDx9xNSHjIDvEMPV+F9/gcGo1VhXXCJ/W9Xyo2SHKw0qa9WduOMjLf6PnWUsgHnbMbe PW8Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h6si6805324pfb.126.2017.07.24.08.14.48; Mon, 24 Jul 2017 08:14:48 -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; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932427AbdGXPOr (ORCPT + 7 others); Mon, 24 Jul 2017 11:14:47 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:33356 "EHLO mail-yw0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932419AbdGXPOn (ORCPT ); Mon, 24 Jul 2017 11:14:43 -0400 Received: by mail-yw0-f193.google.com with SMTP id u207so1242855ywc.0; Mon, 24 Jul 2017 08:14:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=UHwOGLOeZNzd0835bTU+OKJaqESe57fjk8zgUDMFu8M=; b=pTjuj7Sh237GbIa4sO/Oe4pQsWIiIyumDdu+5WCaG62ZgxOlHu5ljqhFwQ0p116RRM zSGKHsMh9e6gvqMYbYYRFS9fVy6z5K8VPZkZimtFKaOjdJUPxFwFTZgg8TrLFGwxW4/b B6fcHrf6jCRqLCO/3xYDsuSoLg2B3ytoE8EMUsqJhSgyqwVoFV/yqt5Ihoo81n+HKGL2 VPcMB7VqDcuSzG1opekgqecJrQAhNK/nGfFPBP96v78yLtnicLv2IbWYX0GYQoARt76N Phm0dNHiItSCdr9m4kuz8wRUpbf8olY13pPs+UoHZGLdOv8SQmjDihyhKaBTwGdiVQ1Q uFJQ== X-Gm-Message-State: AIVw110iyFCG+kTrypBCjfppztCHcBIHx6A1To5y1p8WOccdEs6dV45x VYapn6rTi4mdkw== X-Received: by 10.129.156.198 with SMTP id t189mr14752255ywg.392.1500909282294; Mon, 24 Jul 2017 08:14:42 -0700 (PDT) Received: from localhost.localdomain (24-223-123-72.static.usa-companies.net. [24.223.123.72]) by smtp.googlemail.com with ESMTPSA id g203sm4124870ywa.86.2017.07.24.08.14.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jul 2017 08:14:41 -0700 (PDT) From: Rob Herring To: Frank Rowand , Sergei Shtylyov Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] of/device: use of_property_for_each_string to parse compatible strings Date: Mon, 24 Jul 2017 10:14:37 -0500 Message-Id: <20170724151438.7069-1-robh@kernel.org> X-Mailer: git-send-email 2.11.0 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Instead of directly parsing the compatible property, use the of_property_for_each_string() helper to iterate over each compatible string. This reduces the LoC and makes the functions easier to understand. Signed-off-by: Rob Herring --- drivers/of/device.c | 66 +++++++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 43 deletions(-) -- 2.11.0 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/of/device.c b/drivers/of/device.c index f9f31d66f64d..9d0895fb53b5 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -195,9 +195,10 @@ EXPORT_SYMBOL(of_device_get_match_data); static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len) { - const char *compat; - int cplen, i; - ssize_t tsize, csize, repend; + const char *compat, *start = str; + char *c; + struct property *p; + ssize_t csize; if ((!dev) || (!dev->of_node)) return -ENODEV; @@ -205,42 +206,24 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len /* Name & Type */ csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name, dev->of_node->type); - - /* Get compatible property if any */ - compat = of_get_property(dev->of_node, "compatible", &cplen); - if (!compat) - return csize; - - /* Find true end (we tolerate multiple \0 at the end */ - for (i = (cplen - 1); i >= 0 && !compat[i]; i--) - cplen--; - if (!cplen) - return csize; - cplen++; - - /* Check space (need cplen+1 chars including final \0) */ - tsize = csize + cplen; - repend = tsize; - - if (csize >= len) /* @ the limit, all is already filled */ - return tsize; - - if (tsize >= len) { /* limit compat list */ - cplen = len - csize - 1; - repend = len; - } - - /* Copy and do char replacement */ - memcpy(&str[csize + 1], compat, cplen); - for (i = csize; i < repend; i++) { - char c = str[i]; - if (c == '\0') - str[i] = 'C'; - else if (c == ' ') - str[i] = '_'; + len -= csize; + str += csize; + + of_property_for_each_string(dev->of_node, "compatible", p, compat) { + if (strlen(compat) + 2 > len) + break; + + csize = snprintf(str, len, "C%s", compat); + for (c = str; c; ) { + c = strchr(c, ' '); + if (c) + *c++ = '_'; + } + len -= csize; + str += csize; } - return repend; + return str - start; } int of_device_request_module(struct device *dev) @@ -288,7 +271,8 @@ void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) { const char *compat; struct alias_prop *app; - int seen = 0, cplen, sl; + struct property *p; + int seen = 0; if ((!dev) || (!dev->of_node)) return; @@ -301,12 +285,8 @@ void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) /* Since the compatible field can contain pretty much anything * it's not really legal to split it out with commas. We split it * up using a number of environment variables instead. */ - compat = of_get_property(dev->of_node, "compatible", &cplen); - while (compat && *compat && cplen > 0) { + of_property_for_each_string(dev->of_node, "compatible", p, compat) { add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat); - sl = strlen(compat) + 1; - compat += sl; - cplen -= sl; seen++; } add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);