From patchwork Fri Feb 26 14:37:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bamvor Zhang X-Patchwork-Id: 63064 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp752430lbc; Fri, 26 Feb 2016 06:37:38 -0800 (PST) X-Received: by 10.98.32.5 with SMTP id g5mr2554637pfg.74.1456497458464; Fri, 26 Feb 2016 06:37:38 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ss1si1482304pab.18.2016.02.26.06.37.38; Fri, 26 Feb 2016 06:37:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-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 linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@gmail.com; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754449AbcBZOhh (ORCPT + 4 others); Fri, 26 Feb 2016 09:37:37 -0500 Received: from mail-pf0-f173.google.com ([209.85.192.173]:34651 "EHLO mail-pf0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753853AbcBZOhh (ORCPT ); Fri, 26 Feb 2016 09:37:37 -0500 Received: by mail-pf0-f173.google.com with SMTP id x65so53130016pfb.1 for ; Fri, 26 Feb 2016 06:37:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=b67D45pJ/BYjDcsqrpjf6dZKW4XegR6E/5snHwa7uPo=; b=g+ul8g9QjsNmgVXpeCW9QV0rp7OJpVieck96c/8uUFXOeN6hSIN9RcDS1GJuD18OzT bEdKH/V8Thznt42u2GmenmP7sscWixWAWkqlLzKXZz8zOO2cwIKN0F4JEZ2GZ29YaWe2 dE5gPnOFic3kTzfv1366gnKHzWUIn6diznBBruLaOWNW5g38RIATkOI+nPKpzikurbdy bcFSOzUG9HM/XCJ+x64LL0RCuK3SH/oGm766u3xeRRF5AgUYj2aO64+WpuhZGCQCKnWH /V7s3GjMA3dA/CeYSlra3/XXhF/6cBVvxPoCH6dlzdATkNLRG9Nt4q+c+hfuixBuewq8 z5ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=b67D45pJ/BYjDcsqrpjf6dZKW4XegR6E/5snHwa7uPo=; b=Y4PsQm5Ql2hXwd70fX+cG7FVIeB6ecFM7QHOBigNO7zLBGsDhruq2EZ/edD3wZRBR4 lcXgg3PYknAsDphiV6OholCOZkzxsYx16Fkwi9+SDAPZ4HimFbmHrJv3+8QoSyh/uwDD L2m3XQq2sm5H3qu6NiTixKLrWqcYO72QuLaSDjUmYF/PjygkmryYKs4X2D5+kDecC1LO 1mgI207x3R7jEi+WCnG1Lb/6880PjtlUifNGwORowGUrlJuOepnTCxGG7paIDHKhYlyq rmVqR5WcCRXkJnWwiVW850bcr9gn4mLFNTZijrnLrh/IqoAPMAX5UakxYnLSdgECAjSb I6FQ== X-Gm-Message-State: AD7BkJKtUNfKfbalMoYdYqALXgxSp5KhG0+hP/2YpCFxB4E7vg1v9i6sCoSLDVVRNYC8bg== X-Received: by 10.98.42.20 with SMTP id q20mr2590379pfq.146.1456497456257; Fri, 26 Feb 2016 06:37:36 -0800 (PST) Received: from linux-5iys.lan ([116.251.213.186]) by smtp.gmail.com with ESMTPSA id r77sm19940505pfa.47.2016.02.26.06.37.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 06:37:35 -0800 (PST) From: Bamvor Jian Zhang To: linux-gpio@vger.kernel.org Cc: linus.walleij@linaro.org, broonie@kernel.org, julien.grossholtz@savoirfairelinux.com, arnd@arndb.de, Bamvor Jian Zhang Subject: [PATCH v2] gpiolib: rewrite gpiodev_add_to_list Date: Fri, 26 Feb 2016 22:37:14 +0800 Message-Id: <1456497434-11656-1-git-send-email-bamv2005@gmail.com> X-Mailer: git-send-email 2.6.2 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bamvor Jian Zhang The original code of gpiodev_add_to_list is not very clear which lead to bugs or compiling warning, reference the following patches: Bugs: 1. Commit ef7c7553039b ("gpiolib: improve overlap check of range of gpio"). 2. Commit 96098df125c0 ("gpiolib: fix chip order in gpio list") Warning: 1. Commit e28ecca6eac4 ("gpio: fix warning about iterator"). of gpio"). There is a off-list discussion about how to improve it consequently. This commit try to follow this by rewriting the whole functions. Tested pass with my gpio mockup driver and test scripts[1]. [1] http://www.spinics.net/lists/linux-gpio/msg09598.html Suggested-by: Arnd Bergmann Signed-off-by: Bamvor Jian Zhang --- drivers/gpio/gpiolib.c | 65 +++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) -- 2.6.2 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" 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/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index bc788b9..1c10bd8 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -206,58 +206,43 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); */ static int gpiodev_add_to_list(struct gpio_device *gdev) { - struct gpio_device *iterator; - struct gpio_device *previous = NULL; - - if (!gdev->chip) - return -EINVAL; + struct gpio_device *prev, *next; if (list_empty(&gpio_devices)) { + /* initial entry in list */ list_add_tail(&gdev->list, &gpio_devices); return 0; } - list_for_each_entry(iterator, &gpio_devices, list) { - if (iterator->base >= gdev->base + gdev->ngpio) { - /* - * Iterator is the first GPIO chip so there is no - * previous one - */ - if (!previous) { - goto found; - } else { - /* - * We found a valid range(means - * [base, base + ngpio - 1]) between previous - * and iterator chip. - */ - if (previous->base + previous->ngpio - <= gdev->base) - goto found; - } - } - previous = iterator; + next = list_entry(gpio_devices.next, struct gpio_device, list); + if (gdev->base + gdev->ngpio <= next->base) { + /* add before first entry */ + list_add(&gdev->list, &gpio_devices); + return 0; } - /* - * We are beyond the last chip in the list and iterator now - * points to the head. - * Let iterator point to the last chip in the list. - */ - - iterator = list_last_entry(&gpio_devices, struct gpio_device, list); - if (iterator->base + iterator->ngpio <= gdev->base) { - list_add(&gdev->list, &iterator->list); + prev = list_entry(gpio_devices.prev, struct gpio_device, list); + if (prev->base + prev->ngpio <= gdev->base) { + /* add behind last entry */ + list_add_tail(&gdev->list, &gpio_devices); return 0; } - dev_err(&gdev->dev, - "GPIO integer space overlap, cannot add chip\n"); - return -EBUSY; + list_for_each_entry_safe(prev, next, &gpio_devices, list) { + /* at the end of the list */ + if (&next->list == &gpio_devices) + break; -found: - list_add_tail(&gdev->list, &iterator->list); - return 0; + /* add between prev and next */ + if (prev->base + prev->ngpio <= gdev->base + && gdev->base + gdev->ngpio <= next->base) { + list_add(&gdev->list, &prev->list); + return 0; + } + } + + dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); + return -EBUSY; } /**