From patchwork Mon Nov 16 05:02:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bamvor Zhang Jian X-Patchwork-Id: 56566 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp1114086lbb; Sun, 15 Nov 2015 21:03:04 -0800 (PST) X-Received: by 10.66.228.33 with SMTP id sf1mr51055084pac.132.1447650184010; Sun, 15 Nov 2015 21:03:04 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o6si47645951pap.162.2015.11.15.21.03.03; Sun, 15 Nov 2015 21:03:03 -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=@linaro_org.20150623.gappssmtp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750828AbbKPFDD (ORCPT + 4 others); Mon, 16 Nov 2015 00:03:03 -0500 Received: from mail-pa0-f43.google.com ([209.85.220.43]:33161 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750712AbbKPFDC (ORCPT ); Mon, 16 Nov 2015 00:03:02 -0500 Received: by pabfh17 with SMTP id fh17so164606184pab.0 for ; Sun, 15 Nov 2015 21:03:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2JmPjthpJJ3Mi7TUzgzHdzkkxUwO/emHlvDHOXMWSPc=; b=bBC/Eh7CXkWKiPNgvQSWXCdERpUxuWv+IcmvzvdNm3AuqA6kj8Bv/A4uKhSab6zGvG yl+GC5uDuxwez1yPWLqk9NOFOeKPF0uTaAzWzJVJs48KGFZ9qOrBHJAlpAmMV/N/yNbj cn7IY5mBH49C3vXhaajR+loRYnEtXUKt8M7IauaeilvMVwQSG4feyN6+LemjJb/9CbXi O2cI8UGTiB7Xb0dnekSNwTKNn/puxsWnAyKBPBqhH59+105hoCIe/pX3tgF02iMwd0vB PWOD9i3i0+39jZABvx0erPEGO5rYDe2agN2kqJvrs6l+739UGgwerYl014V+sjEqk9kG WllA== 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:in-reply-to :references; bh=2JmPjthpJJ3Mi7TUzgzHdzkkxUwO/emHlvDHOXMWSPc=; b=kO68HI+gTWoUZgjjfAHDG8cQHt+g1L6eYQxZnDhkSJsbqL/xFvZRIWXVfnPVigOmpJ qD3z+hwDT2e0OwirsRMkQOCEhi/4CGqQOyeDWoqLMERChYrC6gJHDqE69FRH+zN33DlS h0v4VMUztZtVJwh7NwkXHhD9bm/rQtcIorjTsUpXU+zxmgOt8UA6ESGO/ejSD7ukMWgD w2EmOzF5FlBaFYpR7yQQ/8sDiuY6m/n8xSDqyWAWsgnlsY2T2/+Aab7rkMnMKk/6zJ+z hPYff922yEo3k1kVvviuEeNRvbSYK3lSUUM+WJbehRp+TBckMrUmSlThaUYv+XnyvIwq A3jA== X-Gm-Message-State: ALoCoQnwV7wV+WvaXN/B05blLvVgNt0xXJ1FfDpiwnju4NVS2rXuLKZXflUTs1iYyLqShkANpKVS X-Received: by 10.66.153.139 with SMTP id vg11mr51877919pab.118.1447650181369; Sun, 15 Nov 2015 21:03:01 -0800 (PST) Received: from linux-j170.site (li844-78.members.linode.com. [139.162.2.78]) by smtp.gmail.com with ESMTPSA id yg2sm33355959pbb.79.2015.11.15.21.02.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 15 Nov 2015 21:03:00 -0800 (PST) From: Bamvor Jian Zhang To: linux-gpio@vger.kernel.org Cc: linus.walleij@linaro.org, broonie@kernel.org, Bamvor Jian Zhang Subject: [PATCH V2 1/2] gpiolib: improve overlap check of range of gpio Date: Mon, 16 Nov 2015 13:02:46 +0800 Message-Id: <1447650167-29070-2-git-send-email-bamvor.zhangjian@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1447650167-29070-1-git-send-email-bamvor.zhangjian@linaro.org> References: <1447650167-29070-1-git-send-email-bamvor.zhangjian@linaro.org> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org There are limitations for the current checker: 1. Could not check the overlap if the new gpiochip is the secondly gpiochip. 2. Could not check the overlap if the new gpiochip is overlap with the left of gpiochip. E.g. if we insert [c, d] between [a,b] and [e, f], and e >= c + d, it will successful even if c < a + b. 3. Allow overlap of base of different gpiochip. This patch fix these issues by checking the overlap of both right and left gpiochip in the same loop statement. Signed-off-by: Bamvor Jian Zhang --- drivers/gpio/gpiolib.c | 59 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) -- 2.1.4 -- 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 6798355..270d60b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -182,7 +182,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); /* * Add a new chip to the global chips list, keeping the list of chips sorted - * by base order. + * by range(means [base, base + ngpio - 1]) order. * * Return -EBUSY if the new chip overlaps with some other chip's integer * space. @@ -190,31 +190,52 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); static int gpiochip_add_to_list(struct gpio_chip *chip) { struct list_head *pos; - struct gpio_chip *_chip; - int err = 0; + struct gpio_chip *iterator; + struct gpio_chip *previous = NULL; + int ret = 0; - /* find where to insert our chip */ - list_for_each(pos, &gpio_chips) { - _chip = list_entry(pos, struct gpio_chip, list); - /* shall we insert before _chip? */ - if (_chip->base >= chip->base + chip->ngpio) - break; + if (list_empty(&gpio_chips)) { + pos = gpio_chips.next; + goto found; } - /* are we stepping on the chip right before? */ - if (pos != &gpio_chips && pos->prev != &gpio_chips) { - _chip = list_entry(pos->prev, struct gpio_chip, list); - if (_chip->base + _chip->ngpio > chip->base) { - dev_err(chip->dev, - "GPIO integer space overlap, cannot add chip\n"); - err = -EBUSY; + list_for_each(pos, &gpio_chips) { + iterator = list_entry(pos, struct gpio_chip, list); + if (iterator->base >= chip->base + chip->ngpio) { + /* + * Iterator is the first GPIO chip so there is no + * previous one + */ + if (previous == NULL) { + goto found; + } else { + /* + * We found a valid range(means + * [base, base + ngpio - 1]) between previous + * and iterator chip. + */ + if (previous->base + previous->ngpio + <= chip->base) + goto found; + } } + previous = iterator; } + /* We are beyond the last chip in the list */ + if (iterator->base + iterator->ngpio <= chip->base) + goto found; + + dev_err(chip->dev, + "GPIO integer space overlap, cannot add chip\n"); + goto err; - if (!err) - list_add_tail(&chip->list, pos); +found: + list_add_tail(&chip->list, pos); + return ret; - return err; +err: + ret = -EBUSY; + return ret; } /**