How is GPIO made available in sysfs interface?

How is GPIO made available in sysfs interface?

After a kernel driver requests a GPIO, it may only be made available in the sysfs interface by gpiod_export (). The driver can control whether the signal direction may change. This helps drivers prevent userspace code from accidentally clobbering important system state.

Do you abuse sysfs interface for userspace?

And for some tasks, simple userspace GPIO drivers could be all that the system really needs. DO NOT ABUSE SYSFS TO CONTROL HARDWARE THAT HAS PROPER KERNEL DRIVERS. PLEASE READ THE DOCUMENT AT Subsystem drivers using GPIO TO AVOID REINVENTING KERNEL WHEELS IN USERSPACE. I MEAN IT. REALLY. There are three kinds of entries in /sys/class/gpio:

What can GPIO be used for in userspace?

Again depending on appropriate hardware documentation, on some systems userspace GPIO can be used to determine system configuration data that standard kernels won’t know about. And for some tasks, simple userspace GPIO drivers could be all that the system really needs.

How to manage GPIO exports in the kernel?

Kernel code can explicitly manage exports of GPIOs which have already been requested using gpio_request (): After a kernel driver requests a GPIO, it may only be made available in the sysfs interface by gpiod_export (). The driver can control whether the signal direction may change.

What is the sysfs interface for userspace in Linux?

There are three kinds of entries in /sys/class/gpio: GPIO controllers (“gpio_chip” instances). That’s in addition to standard files including the “device” symlink. The control interfaces are write-only: “export” … Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.

Can a GPIO be configured as an output?

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.