I'm new in developing Linux driver.
Traditionally, If i want to create char device, I just need to implement read
write
and ioctl
functions, and register it by register_chrdev_region
with corresponding Major device ID
. And then use mknod
to create a device file of that Major device ID
. Then call read
write
open
in user space will then call the corresponding functions by kernel.
But now i'm studying Linux device model
and sys filesystem
, which is added in kernel 2.6. I'm told if possible, Don't use /dev filesystem
, as linux now has a good device model to handle device and driver.This confused me, so I'll summarize my confusion into some questions:
/dev/null
?udev
is based on sys filesystem
to create device file in /dev
. Why? Since I'm told "If possible, Don't use /dev filesystem
", why does udev
use /sys
to create file in /dev
?sys
have any conceptions like char device file
or block device
?/dev
, open
write
from user space will finally map to functions in file operation structure
defined by me, then what functions will be called when I open or write to files in /ssy
?Without context your statement about /dev
is not clear. Anyway:
You cannot create char devices on sysfs. The main purpose of sysfs is to export information and allow the user to adjust single values (just navigate under /sys/
for some examples). Char devices usually does much complicated things.
If you mean how you call your driver's open,read,write,ioctl, ... well, by doing open(2), read(2), write(2), ioctl(2) (look at man pages of these commands)
when a device appear, the kernel create a directory under /sys. For example take a look at ls /sys/bus/usb/devices/
. All that directories are created when an USB device appear. You can try by plug/unplug USB devices. udev
put an eye on sysfs to detect new devices and according to the information from sysfs it create the device under /dev
. This happens when the driver, somehow, calls device_add()
. Often this functions is invoked by other register functions like: device_create
, device_register
, or other from other sub-systems.
the idea of sysfs is to provide information about the devices and drivers loaded. So you can change device, bus and driver options. Or manually attach a device to a module
Actually, behind the sysfs attributes there is a set of file_operation, where open, read and write are managed by the kernel and not by your driver. In order to create a sysfs attribute you have to provide the pair of function show and store to read/write something from/to the driver. Then the kernel will route the requests to your correct attribute