From: Helmut Grohne <helmut@subdivi.de>
To: linux-gpio@vger.kernel.org
Subject: How to use a bidirectional gpio from userspace?
Date: Wed, 3 Jan 2018 08:50:20 +0100 [thread overview]
Message-ID: <20180103075019.GA29466@alf.mars> (raw)
Hi,
For controlling gpios from userspace there currently are two APIs. The
older sysfs based API (which seems to be deprecated according to Linux
Walleij) and the /dev/gpiochipN ioctl API. I was trying to use the
latter from userspace and have a question on correctly using it.
For some devices it is necessary to toggle the direction of a gpio. For
instance W1 devices communicate over a single vcc/data wire. They have
an internal capacitor and communication is initiated by the host by
pulling the vcc/data line to ground and then listening to it. This
necessitates changing the direction of a gpio quite often and in-kernel
drivers for such devices tend to do that using gpiod_direction_input and
gpiod_direction_output (or the older non-d variant of them). Examples
include w1-gpio.c and dht11.c. Just how does one do that from userspace?
It seems that for driving a gpio as output one needs to obtain a
linehandle fd. To read the gpio, one needs a lineevent fd. If I
understand the API correctly, I cannot create both at the same time, so
switching the direction means closing a linehandle and obtaining a
linevent fd or vice versa. Is that correct?
If yes, I see a couple of downsides. First of all, I need to keep the
gpiochip fd open. Closing it would be good from a hardening pov. Then
closing the linehandle or lineevent releases the gpio and makes it
available to other users temporarily. Potentially a race condition is
introduced. Furthermore this switch of direction is relatively costly as
it allocates and frees quite some data structures.
With the sysfs API one can change the direction of an exported gpio with
a single write to the direction file.
Please Cc me in replies.
Helmut
reply other threads:[~2018-01-03 7:59 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly 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=20180103075019.GA29466@alf.mars \
--to=helmut@subdivi.de \
--cc=linux-gpio@vger.kernel.org \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.