The Device Filesystem
As suggested at the beginning of the chapter, recent versions of the Linux kernel offer a special filesystem for device entry points. The filesystem has been available for a while as an unofficial patch; it was made part of the official source tree in 2.3.46. A backport to 2.2 is available as well, although not included in the official 2.2 kernels.
Although use of the special filesystem is not widespread as we write
this, the new features offer a few advantages to the device driver
writer. Therefore, our version of scull
exploits devfs if it is being used in the
target system. The module uses kernel configuration information at
compile time to know whether particular features have been enabled,
and in this case we depend on CONFIG_DEVFS_FS being
defined or not.
The main advantages of devfs are as follows:
Device entry points in
/devare created at device initialization and removed at device removal.The device driver can specify device names, ownership, and permission bits, but user-space programs can still change ownership and permission (but not the filename).
There is no need to allocate a major number for the device driver and deal with minor numbers.
As a result, there is no need to run a script to create device special files when a module is loaded or unloaded, because the driver is autonomous in managing its own special files.
To handle device creation and removal, the driver should call the following functions:
#include <linux/devfs_fs_kernel.h> ...