Linux-GPIO Archive on lore.kernel.org
 help / color / Atom feed
From: Drew Fustini <drew@pdp7.com>
To: warthog618@gmail.com
Cc: linux-gpio@vger.kernel.org, linus.walleij@linaro.org,
	brgl@bgdev.pl, Drew Fustini <drew@pdp7.com>
Subject: Re: [RFC] gpio: expose pull-up/pull-down line flags to userspace
Date: Wed,  9 Oct 2019 13:32:33 +0200
Message-ID: <20191009113232.28852-1-drew@pdp7.com> (raw)
In-Reply-To: <20191009002211.GA11168@sol>

set pull-up/down flags in lineevent_create and add sanity checks

Check the pull-up/down flags in lineevent_create() and set the
corresponding bits.

Add sanity checks to make pull-up and pull-down flags mutually
exclusive and only valid when the line is an input.

Signed-off-by: Drew Fustini <drew@pdp7.com>
---
 drivers/gpio/gpiolib.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 646ae4cffe26..babc26267561 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -554,6 +554,20 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	     (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
 		return -EINVAL;
 
+	/*
+	 * Do not allow PULL_UP & PULL_DOWN flags to be set as they are
+	 * contradictory.
+	 */
+	if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) &&
+	    (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))
+		return -EINVAL;
+
+	/* PULL_UP and PULL_DOWN flags only make sense for input mode. */
+	if (!(lflags & GPIOHANDLE_REQUEST_INPUT) &&
+	    ((lflags & GPIOHANDLE_REQUEST_PULL_UP) ||
+	     (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)))
+		return -EINVAL;
+
 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
 	if (!lh)
 		return -ENOMEM;
@@ -941,6 +955,24 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 		goto out_free_label;
 	}
 
+	/*
+	 * Do not allow PULL_UP & PULL_DOWN flags to be set as they are
+	 * contradictory.
+	 */
+	if ((lflags & GPIOHANDLE_REQUEST_PULL_UP) &&
+	    (lflags & GPIOHANDLE_REQUEST_PULL_DOWN)) {
+		ret = -EINVAL;
+		goto out_free_label;
+	}
+
+	/* PULL_UP and PULL_DOWN flags only make sense for input mode. */
+	if (!(lflags & GPIOHANDLE_REQUEST_INPUT) &&
+	    ((lflags & GPIOHANDLE_REQUEST_PULL_UP) ||
+	     (lflags & GPIOHANDLE_REQUEST_PULL_DOWN))) {
+		ret = -EINVAL;
+		goto out_free_label;
+	}
+
 	desc = &gdev->descs[offset];
 	ret = gpiod_request(desc, le->label);
 	if (ret)
@@ -950,6 +982,10 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 
 	if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
 		set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+	if (lflags & GPIO_PULL_UP)
+		set_bit(FLAG_PULL_UP, &desc->flags);
+	else if (lflags & GPIO_PULL_DOWN)
+		set_bit(FLAG_PULL_DOWN, &desc->flags);
 
 	ret = gpiod_direction_input(desc);
 	if (ret)
-- 
2.20.1


  parent reply index

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-21 10:25 Drew Fustini
2019-09-23  8:38 ` Bartosz Golaszewski
2019-10-02 11:10   ` Drew Fustini
2019-10-03 12:47   ` Linus Walleij
2019-10-04  7:22     ` Bartosz Golaszewski
2019-10-04 12:46       ` Bartosz Golaszewski
2019-10-05 17:02       ` Linus Walleij
2019-10-06  3:12         ` Kent Gibson
2019-10-06 21:06           ` Linus Walleij
2019-10-07  4:37             ` Kent Gibson
2019-10-08  6:15 ` Kent Gibson
2019-10-08 20:56   ` Bartosz Golaszewski
2019-10-08 23:21     ` Kent Gibson
2019-10-08 23:30       ` Bartosz Golaszewski
2019-10-08 23:56         ` Kent Gibson
2019-10-09  0:03           ` Bartosz Golaszewski
2019-10-09  0:22             ` Kent Gibson
2019-10-09  6:55               ` Kent Gibson
2019-10-09 12:57                 ` Drew Fustini
2019-10-09 13:23                   ` Kent Gibson
2019-10-09 13:30                 ` Drew Fustini
2019-10-09 14:11                   ` Kent Gibson
2019-10-09 15:50                     ` Bartosz Golaszewski
2019-10-09 16:19                       ` Kent Gibson
2019-10-09 23:59                   ` Kent Gibson
2019-10-10  7:47                     ` Drew Fustini
2019-10-10 10:14                       ` Kent Gibson
2019-10-10 11:17                         ` Kent Gibson
2019-10-11 13:04                           ` Drew Fustini
2019-10-11 13:06                         ` Drew Fustini
2019-10-11 13:49                           ` Kent Gibson
2019-10-09 11:32               ` Drew Fustini [this message]
2019-10-09 13:57       ` Drew Fustini
2019-10-09 14:01         ` Kent Gibson
2019-10-09 11:46   ` Drew Fustini

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191009113232.28852-1-drew@pdp7.com \
    --to=drew@pdp7.com \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-GPIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-gpio/0 linux-gpio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-gpio linux-gpio/ https://lore.kernel.org/linux-gpio \
		linux-gpio@vger.kernel.org
	public-inbox-index linux-gpio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-gpio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git