1) What handles /sys/class/gpio ? A kernel module ? a driver ?
It's a kernel interface similar to the /proc
directory.
2) is it possible to have more complicated module parameters in a kernel module, with some directory structure ? Like a 'delays' directory containing the params for delays
Yes; some things in /proc
and /sys
do use directory hierarchies. If you want to modify or expand them, though, you have to modify the kernel.
#3 has a similar answer -- to make changes you need to change the relevant kernel code.
4) How does the gpio thing creates new/deletes files in /sys/class/gpio when you write to [un]export ?
These are not files on disk, they're just system interfaces.1 When you go to read data from a procfs or sysfs file, what you are really doing is making a request for information from the kernel. The data is then formatted and returned. It probably isn't stored anywhere in the form you see it, although parts of it may be stored in the kernel.
When you write to such a file -- not all of them allow this -- you're sending a request to the kernel to do something specific. This can include, e.g., activating or expanding the GPIO interface.
1. read
and write
calls are always system calls anyway, since normal files are normally on disk, and the kernel is needed to access hardware. Hence using a filesystem style API here is natural; even if they are not "real files", accessing whatever resource they represent must involve system calls.
The /sys
filesystem (sysfs) contains files that provide information about devices: whether it's powered on, the vendor name and model, what bus the device is plugged into, etc. It's of interest to applications that manage devices.
The /dev
filesystem contains files that allow programs to access the devices themselves: write data to a serial port, read a hard disk, etc. It's of interest to applications that access devices.
A metaphor is that /sys
provides access to the packaging, while /dev
provides access to the content of the box.
The files in /sys
are not device nodes, but symbolic links and regular files. Those regular files are special in that reading or writing to them invokes file-specific functions in the kernel, like device nodes. The difference is that files in /sys
work this way because of the filesystem they are on, whereas device nodes work this way due to their device node characteristics (the file type indicating a (block or character) device, and the device major and minor number indicating which device it is).
The reason for /dev
existing independently of /sys
is partly historical: /dev
dates back to the dawn of Unix, while /sys
is a much more recent invention. If Linux was designed today with no historical background, /dev/sda
might be /sys/block/sda/content
.
Best Answer
The files in
/dev
are actual devices files which UDEV creates at run time. The directory/sys/class
is exported by the kernel at run time, exposing the hierarchy of the hardware throughsysfs
.From the libudev and Sysfs Tutorial
excerpt
another excerpt
Usage?
In general you use rules in
/etc/udev/rules.d
to augment your system. Rules can be constructed to run scripts when various hardware is present.Once a system is up you can write scripts to work against either
/dev
or/sys
, and it really comes down to personal preferences, but I would usually try and work against/sys
and make use of tools such asudevadm
to query UDEV for locations of various system resources.