From patchwork Fri Aug 30 07:46:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 19626 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ve0-f198.google.com (mail-ve0-f198.google.com [209.85.128.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 6291B25DFF for ; Fri, 30 Aug 2013 07:46:47 +0000 (UTC) Received: by mail-ve0-f198.google.com with SMTP id 15sf1685250vea.9 for ; Fri, 30 Aug 2013 00:46:46 -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=uCWeIJejuxJsYr8IdGF5fBmAQj74yGVOeRHxgyBf3U8=; b=ffA9nbU4/V7BmRz43L9e/zQCnAtZiBx/5v6bkrp61QjEFEd6UxwNBdUZiRiBWZy7eU +BcfLSFx0VKcPDLF2qBuQ3qqgspFmjD9hede7uvoPEdhADOqQYlo3cz+wuRWC6JwB+Es VnCAitJ1pFkyKiPieNmhmje1R8Gv5QsJ4/1hTIByeOXfy1XwogBnK7zpneo8BkK0uiLr 0CbSzqBlszNzeN2NFoZXoEg9G2/Z6MdsmwLcaNuRaaZpSd0Gs3mK3I6n6Zr3hK3pJdoP WJ7BJQza9sQAejwz1LfwIxbEZ73Chc0+l6CIcR4Qq+7zjc4+CgkuCGmW124/OmQDdMjx rY8A== X-Received: by 10.224.4.138 with SMTP id 10mr8647001qar.8.1377848806775; Fri, 30 Aug 2013 00:46:46 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.47.42 with SMTP id a10ls1189713qen.30.gmail; Fri, 30 Aug 2013 00:46:46 -0700 (PDT) X-Received: by 10.221.40.10 with SMTP id to10mr5784715vcb.22.1377848806641; Fri, 30 Aug 2013 00:46:46 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id se9si2283740vdc.23.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 30 Aug 2013 00:46:46 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.169 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.169; Received: by mail-vc0-f169.google.com with SMTP id ib11so1107692vcb.28 for ; Fri, 30 Aug 2013 00:46:46 -0700 (PDT) X-Gm-Message-State: ALoCoQmLRPF7JCRG1aXQMXZvR8VWwfTqTuv7DI7vzv+riWkSbdg7irWYqY1ReazNn2AqFC2YO5oE X-Received: by 10.52.92.15 with SMTP id ci15mr79275vdb.34.1377848806530; Fri, 30 Aug 2013 00:46:46 -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 u4csp73727vcz; Fri, 30 Aug 2013 00:46:46 -0700 (PDT) X-Received: by 10.15.41.77 with SMTP id r53mr1127887eev.64.1377848805432; Fri, 30 Aug 2013 00:46:45 -0700 (PDT) Received: from mail-ee0-f52.google.com (mail-ee0-f52.google.com [74.125.83.52]) by mx.google.com with ESMTPS id i5si6834491eeg.175.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 30 Aug 2013 00:46:45 -0700 (PDT) Received-SPF: neutral (google.com: 74.125.83.52 is neither permitted nor denied by best guess record for domain of linus.walleij@linaro.org) client-ip=74.125.83.52; Received: by mail-ee0-f52.google.com with SMTP id c41so713510eek.25 for ; Fri, 30 Aug 2013 00:46:45 -0700 (PDT) X-Received: by 10.14.184.3 with SMTP id r3mr2599762eem.49.1377848804897; Fri, 30 Aug 2013 00:46:44 -0700 (PDT) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id n48sm52475663eeg.17.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 30 Aug 2013 00:46:43 -0700 (PDT) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Alexandre Courbot , Linus Walleij , Frank Rowand , Tim Bird Subject: [PATCH] gpio: improve error path in gpiolib Date: Fri, 30 Aug 2013 09:46:35 +0200 Message-Id: <1377848795-28070-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.169 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. Suggested-by: Alexandre Courbot Cc: Frank Rowand Cc: Tim Bird Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d6413b2..f041f9e 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,10 @@ 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__); + } status = gpio_ensure_requested(desc); if (status < 0)