Linux GPIO support

Anandu Suri a11i_clptr2iu at
Sat Aug 15 06:47:26 CEST 2020

Hello Marc,

I noticed a few archived threads asking for help or requesting for
POLPRI/exceptfds feature mainly to handle GPIO events.

> Have you tried polling with EV_READ and/or EV_WRITE? It would be a kernel
> bug if POLLPRI is signalled but POLLIN isn't, as urgent read data is also
> read data.

In Linux kernel, when the interrupt handler for a GPIO triggers, whichever
driver handles it, libgpio (kernel library) eventually calls a core kernel
function `sysfs_notify()` to notify user space. This function will only generate
a POLLPRI (poll, epoll) or be seen in exceptfds (select).

See the kernel documentation at:

>   "value" ... reads as either 0 (low) or 1 (high). If the GPIO
>       is configured as an output, this value may be written;
>       any nonzero value is treated as high.

>       If the pin can be configured as interrupt-generating interrupt
>       and if it has been configured to generate interrupts (see the
>       description of "edge"), you can poll(2) on that file and
>       poll(2) will return whenever the interrupt was triggered. If
>       you use poll(2), set the events POLLPRI and POLLERR. If you
>       use select(2), set the file descriptor in exceptfds. After
>       poll(2) returns, either lseek(2) to the beginning of the sysfs
>       file and read the new value or close the file and re-open it
>       to read the value.

If /sys/class/gpio/gpio<N>/value is initialised by ev_io_init(... EV_READ), then
the read event will be permanently active on that FD causing ev_run() to invoke
the call-back every round it gets.

> If the fd really just signals POLLPRI but not POLLIN with poll, then you
> might consider reporting this as a kernel bug.

Not sure if this is a design decision or buggy design. But given that Linux is
used extensively in modems, cameras, and many other appliances where GPIO is the
most common way to listen to button/IR/reed-switch/etc states, it would be
convenient if libev was capable of explicitly listening to POLLPRI alone. As
such it will reduce some creative solutions around libev or even prevent wheel
from being reinvented.


More information about the libev mailing list