From patchwork Fri Jan 8 06:37:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bamvor Zhang Jian X-Patchwork-Id: 59326 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp366229lbb; Thu, 7 Jan 2016 22:38:00 -0800 (PST) X-Received: by 10.98.43.73 with SMTP id r70mr2110840pfr.4.1452235080595; Thu, 07 Jan 2016 22:38:00 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id oj5si49921071pab.211.2016.01.07.22.38.00; Thu, 07 Jan 2016 22:38:00 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751135AbcAHGiA (ORCPT + 4 others); Fri, 8 Jan 2016 01:38:00 -0500 Received: from mail-pf0-f177.google.com ([209.85.192.177]:36499 "EHLO mail-pf0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751074AbcAHGh7 (ORCPT ); Fri, 8 Jan 2016 01:37:59 -0500 Received: by mail-pf0-f177.google.com with SMTP id n128so5336451pfn.3 for ; Thu, 07 Jan 2016 22:37:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Dp6audVLyJksu8rz1/47juUl9OwBmZvJ0CNrVFl+dLs=; b=TGhUGO+RTBMthJq+sFhoyZQCRelChFbjaNdMpSzfsT9MSDtpvLRCnTt1dB/M2P7S9J wJxxi3IV2n+Ri/NgWXKUPwTXf1wQEf/QmmOOW0hBpFAbjMsokuN10SHT4CsHO24g+S81 oZwhTBuY6emxmsH24pva0DlsrI3ws0036buig= 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=Dp6audVLyJksu8rz1/47juUl9OwBmZvJ0CNrVFl+dLs=; b=gfFbv65GKrYzzmvtBZ8ZvKJXAWmMC9Ws66LdEQAy4+WRY+Jo1en2145hOjdg1BOrYP HTmRI94OfJ5Qk7kk0vsBvFw42yuBkT7CmcGVa7NY/7NZ63Gb15uXN/fdNG85LJqfhpno MJvBqDiG6pDTqP7hab4qPJStHfwfe90FzG+wTEg0Z8eYsSOSBs7OZr/lmoxmehUCJQsn zJVwPpFH0ERE0YoFphRQ8XTL1f6fHRRLcm4apMeZKzUWW0guoxLaHbiuD/tJXvSDOdjY Lo2vRGETwjbI+D7U/+pZnM+vN2+Hy4U5r1ojOnMoZAcKyC3fO5brOQ2anv9KvnU4+YVH k+FA== X-Gm-Message-State: ALoCoQlQ+qchltAl9GWVcT8n4AJ77T2HDfhBHhhEcXycRrG/wMBY5N9hRNLQZtDn1xcJgq07cxBe+VExx2aIGr8sg5m8LfbpDg== X-Received: by 10.98.71.211 with SMTP id p80mr2094536pfi.135.1452235078998; Thu, 07 Jan 2016 22:37:58 -0800 (PST) Received: from linux-j170.lan ([111.202.0.63]) by smtp.gmail.com with ESMTPSA id y18sm1935633pfi.84.2016.01.07.22.37.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 Jan 2016 22:37:58 -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] gpiolib: rewrite gpiochip_add_to_list Date: Fri, 8 Jan 2016 14:37:15 +0800 Message-Id: <1452235035-828-2-git-send-email-bamvor.zhangjian@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1452235035-828-1-git-send-email-bamvor.zhangjian@linaro.org> References: <1452235035-828-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 The original code of gpiochip_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. "[PATCH] gpiolib: fix chip order in gpio list" [1] 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[2]. [1] http://www.spinics.net/lists/kernel/msg2156917.html [2] 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, 28 insertions(+), 37 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 3db34e7..b9cc01f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); */ static int gpiochip_add_to_list(struct gpio_chip *chip) { - struct gpio_chip *iterator; - struct gpio_chip *previous = NULL; + struct gpio_chip *prev, *next; if (list_empty(&gpio_chips)) { - list_add_tail(&chip->list, &gpio_chips); + /* initial entry in list */ + list_add(&chip->list, &gpio_chips); return 0; } - list_for_each_entry(iterator, &gpio_chips, list) { - if (iterator->base >= chip->base + chip->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 - <= chip->base) - goto found; - } - } - previous = iterator; + next = list_entry(gpio_chips.next, struct gpio_chip, list); + if (chip->base + chip->ngpio <= next->base) { + /* add before first entry */ + list_add(&chip->list, &gpio_chips); + 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. - */ + prev = list_entry(gpio_chips.prev, struct gpio_chip, list); + if (prev->base + prev->ngpio <= chip->base) { + /* add behind last entry */ + list_add_tail(&chip->list, &gpio_chips); + return 0; + } - iterator = list_last_entry(&gpio_chips, struct gpio_chip, list); - if (iterator->base + iterator->ngpio <= chip->base) - goto found; + list_for_each_entry_safe(prev, next, &gpio_chips, list) + if (chip->base > prev->base) + break; - dev_err(chip->parent, - "GPIO integer space overlap, cannot add chip\n"); - return -EBUSY; + if (&prev->list != &gpio_chips && + &next->list != &gpio_chips && + prev->base + prev->ngpio <= chip->base && + chip->base + chip->ngpio <= next->base) { + /* add between prev and next */ + list_add(&chip->list, &prev->list); + return 0; + } -found: - list_add_tail(&chip->list, &iterator->list); - return 0; + dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n"); + return -EBUSY; } + /** * Convert a GPIO name to its descriptor */