From patchwork Mon Sep 2 08:39:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 19670 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ve0-f200.google.com (mail-ve0-f200.google.com [209.85.128.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 16DF8248D9 for ; Mon, 2 Sep 2013 08:40:06 +0000 (UTC) Received: by mail-ve0-f200.google.com with SMTP id oz10sf5097951veb.11 for ; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:x-gm-message-state:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=JeceQ3VwqcczCivl2RY0wqk/BDziy4qnE5GFfWVxruU=; b=Tq/ouSpG1HxMQ80i0+JP7JlvipgdL8qxGFI+mufaTLT4CmYzRH3aAqeDUOjSzBa/VG HkBDQtCOeOVWIev+17kXbuTa1+k7Y038bbtoWmB8E1o/rukRcGt0OlnQC+g2lJP2zmLb Vu0E6230MiNbu1X4Y7epT45aIr1XLftJ99OjNx666h+QSGAcVYK3LMUVUyzU1pXqkIB0 Vme/Qi2zvmPgJGLVSKGOZ53xeK/AO8P8CHcj/UAAtk0Nz8xGdlw+cMUqQZVNpEFvr4E1 tfW0vO3+KPxikMmDrO0EklmICY2uFLDhzkBDMERddnkFel3sLm7njC46tFWl7/wIIpmW vwig== X-Received: by 10.224.223.198 with SMTP id il6mr24833251qab.7.1378111205517; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.84.201 with SMTP id b9ls562581qez.62.gmail; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) X-Received: by 10.220.164.70 with SMTP id d6mr4875911vcy.19.1378111205394; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) Received: from mail-vc0-f180.google.com (mail-vc0-f180.google.com [209.85.220.180]) by mx.google.com with ESMTPS id tf3si2872081vec.68.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Sep 2013 01:40:05 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.180 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.180; Received: by mail-vc0-f180.google.com with SMTP id gf11so2730459vcb.39 for ; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) X-Gm-Message-State: ALoCoQmpBEWxGeaM0q25mfhIDq89mdx0n2rtnKY+Ia/sraJvQI6AQbqGDrLOeR8rHv005ETHchzd X-Received: by 10.52.35.171 with SMTP id i11mr17916525vdj.4.1378111205188; Mon, 02 Sep 2013 01:40:05 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp102719vcz; Mon, 2 Sep 2013 01:40:04 -0700 (PDT) X-Received: by 10.15.54.72 with SMTP id s48mr1850618eew.58.1378111203953; Mon, 02 Sep 2013 01:40:03 -0700 (PDT) Received: from mail-ee0-f45.google.com (mail-ee0-f45.google.com [74.125.83.45]) by mx.google.com with ESMTPS id 7si9282516eeo.199.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Sep 2013 01:40:03 -0700 (PDT) Received-SPF: neutral (google.com: 74.125.83.45 is neither permitted nor denied by best guess record for domain of linus.walleij@linaro.org) client-ip=74.125.83.45; Received: by mail-ee0-f45.google.com with SMTP id c50so2184430eek.4 for ; Mon, 02 Sep 2013 01:40:03 -0700 (PDT) X-Received: by 10.14.174.195 with SMTP id x43mr2778062eel.47.1378111203311; Mon, 02 Sep 2013 01:40:03 -0700 (PDT) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id bn13sm20040775eeb.11.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 02 Sep 2013 01:40:02 -0700 (PDT) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Alexandre Courbot , Linus Walleij , Tim Bird Subject: [PATCH] gpio: improve error path in gpiolib Date: Mon, 2 Sep 2013 10:39:56 +0200 Message-Id: <1378111196-4247-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 1.8.3.1 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.180 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , At several places the gpiolib will proceed to handle a GPIO descriptor even if it's ->chip member is NULL and no gpiochip is associated. Fix this by checking that both the descriptor cookie *and* the chip pointer are valid. Also bail out earlier with more specific diagnostic messages on missing operations for setting as input/output or debounce. ChangeLog v1->v2: - Also return -EIO on gpiod_set_debounce() with missing operations in the vtable - Fix indentations. Suggested-by: Alexandre Courbot Acked-by: Alexandre Courbot Reviewed-by: Frank Rowand Cc: Tim Bird Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d6413b2..a7587db 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1398,7 +1398,7 @@ static int gpiod_request(struct gpio_desc *desc, const char *label) int status = -EPROBE_DEFER; unsigned long flags; - if (!desc) { + if (!desc || !desc->chip) { pr_warn("%s: invalid GPIO\n", __func__); return -EINVAL; } @@ -1406,8 +1406,6 @@ static int gpiod_request(struct gpio_desc *desc, const char *label) spin_lock_irqsave(&gpio_lock, flags); chip = desc->chip; - if (chip == NULL) - goto done; if (!try_module_get(chip->owner)) goto done; @@ -1630,16 +1628,20 @@ static int gpiod_direction_input(struct gpio_desc *desc) int status = -EINVAL; int offset; - if (!desc) { + if (!desc || !desc->chip) { pr_warn("%s: invalid GPIO\n", __func__); return -EINVAL; } + chip = desc->chip; + if (!chip->get || !chip->direction_input) { + pr_warn("%s: missing get() or direction_input() operations\n", + __func__); + return -EIO; + } + spin_lock_irqsave(&gpio_lock, flags); - chip = desc->chip; - if (!chip || !chip->get || !chip->direction_input) - goto fail; status = gpio_ensure_requested(desc); if (status < 0) goto fail; @@ -1691,7 +1693,7 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) int status = -EINVAL; int offset; - if (!desc) { + if (!desc || !desc->chip) { pr_warn("%s: invalid GPIO\n", __func__); return -EINVAL; } @@ -1704,11 +1706,15 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) return gpiod_direction_input(desc); + chip = desc->chip; + if (!chip->set || !chip->direction_output) { + pr_warn("%s: missing set() or direction_output() operations\n", + __func__); + return -EIO; + } + spin_lock_irqsave(&gpio_lock, flags); - chip = desc->chip; - if (!chip || !chip->set || !chip->direction_output) - goto fail; status = gpio_ensure_requested(desc); if (status < 0) goto fail; @@ -1765,7 +1771,7 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) int status = -EINVAL; int offset; - if (!desc) { + if (!desc || !desc->chip) { pr_warn("%s: invalid GPIO\n", __func__); return -EINVAL; } @@ -1773,8 +1779,11 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) spin_lock_irqsave(&gpio_lock, flags); chip = desc->chip; - if (!chip || !chip->set || !chip->set_debounce) - goto fail; + if (!chip->set || !chip->set_debounce) { + pr_warn("%s: missing set() or set_debounce() operations\n", + __func__); + return -EIO; + } status = gpio_ensure_requested(desc); if (status < 0)