4
votes

Assume that the gpio X can be exported in sysfs as an input pin, after doing that a directory called gpioX will be created into /sys/class/gpio/. gpioX/ contains few file such as "value" which represents the current state of the gpio X (high or low).

What happens (in kernel space) when the signal applied to the pin X changes its state (for example from low to high)?

I mean, before the transition gpioX/value contains "low", but after that it will contain "high" value. How is this file updated by the OS?

I think that an interrupt mechanism is required.Does it use an interrupt mechanism to update sysfs?

1
I put together some notes about sysfs and the pseudo files in an answer to this question, what is the /sys/class/gpio/export and `/sys/class/gpio/unexport mechanism and what is the underlying sysfs functionality?. It doesn't exactly answer your question however there are some thoughts on the underlying sysfs functionality that someone may find helpful.Richard Chambers

1 Answers

6
votes

How is this file updated by the OS? I think that an interrupt mechanism is required.

It does not require an interrupt mechanism unless it supports polling (man poll) or alternate asynchronous notifications. At least with most version, the /sys/class/gpio/ only does a read of the GPIO level when someone reads the file.

sysfs, debugfs, configfs, procfs, etc are virtual file systems. When you access the file, code within the Linux kernel runs to provide the value. sysfs only provides a file like interface; that doesn't mean it is backed with actual state. The state is the GPIO level which can be read at any time.

gpio_value_show() appears to be the current implementation. What you describe with interrupts is possible. It can be done through the sysfs_set_active_low() function or the sysfs file /sys/class/gpio/gpioN/edge. Writing to the file may return an error if the GPIO doesn't support interrupts. See gpio.txt for more (especially for your particular version of Linux).