mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-28 12:34:26 +01:00
kernel_module: move character device example doc to README
This commit is contained in:
77
README.adoc
77
README.adoc
@@ -2875,6 +2875,83 @@ Those commits change `BR2_LINUX_KERNEL_LATEST_VERSION` in `/linux/Config.in`.
|
||||
|
||||
You should then look up if there is a branch that supports that kernel. Staying on branches is a good idea as they will get backports, in particular ones that fix the build as newer host versions come out.
|
||||
|
||||
=== Pseudo filesystems
|
||||
|
||||
==== Character device
|
||||
|
||||
....
|
||||
/character_device.sh
|
||||
echo $?
|
||||
....
|
||||
|
||||
Outcome: the test passes:
|
||||
|
||||
....
|
||||
0
|
||||
....
|
||||
|
||||
Sources:
|
||||
|
||||
* link:rootfs_overlay/character_device.sh[]
|
||||
* link:rootfs_overlay/mknoddev.sh[]
|
||||
* link:kernel_module/character_device.c[]
|
||||
|
||||
Charcter device files are created with:
|
||||
|
||||
....
|
||||
mknod </dev/path_to_dev> c <major> <minor>
|
||||
....
|
||||
|
||||
Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.
|
||||
|
||||
A single driver can drive multiple compatible devices.
|
||||
|
||||
The major and minor numbers can be observed with:
|
||||
|
||||
....
|
||||
ls -l /dev/urandom
|
||||
....
|
||||
|
||||
Output:
|
||||
|
||||
....
|
||||
crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom
|
||||
....
|
||||
|
||||
which means:
|
||||
|
||||
* `c` (first letter): this is a character device. Would be `b` for a block device.
|
||||
* `1, 9`: the major number is `1`, and the minor `9`
|
||||
|
||||
To avoid device number conflicts when registring the driver we:
|
||||
|
||||
* ask the kernel to allocate a free major number for us with: `register_chrdev(0`
|
||||
* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name
|
||||
|
||||
Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758
|
||||
|
||||
===== Automatically create character device file on insmod
|
||||
|
||||
And also destroy it on `rmmod`:
|
||||
|
||||
....
|
||||
/character_device_create.sh
|
||||
echo $?
|
||||
....
|
||||
|
||||
Outcome: the test passes:
|
||||
|
||||
....
|
||||
0
|
||||
....
|
||||
|
||||
Sources:
|
||||
|
||||
* link:kernel_module/character_device_create.c[]
|
||||
* link:rootfs_overlay/character_device_create.sh[]
|
||||
|
||||
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
|
||||
|
||||
=== Kernel panic and oops
|
||||
|
||||
To test out kernel panics and oops in controlled circumstances, try out the modules:
|
||||
|
||||
Reference in New Issue
Block a user