Skip to content

Commit 7f36718

Browse files
committed
kernel_module: move character device example doc to README
1 parent 20b9961 commit 7f36718

File tree

6 files changed

+80
-32
lines changed

6 files changed

+80
-32
lines changed

README.adoc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,83 @@ Those commits change `BR2_LINUX_KERNEL_LATEST_VERSION` in `/linux/Config.in`.
28752875

28762876
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.
28772877

2878+
=== Pseudo filesystems
2879+
2880+
==== Character device
2881+
2882+
....
2883+
/character_device.sh
2884+
echo $?
2885+
....
2886+
2887+
Outcome: the test passes:
2888+
2889+
....
2890+
0
2891+
....
2892+
2893+
Sources:
2894+
2895+
* link:rootfs_overlay/character_device.sh[]
2896+
* link:rootfs_overlay/mknoddev.sh[]
2897+
* link:kernel_module/character_device.c[]
2898+
2899+
Charcter device files are created with:
2900+
2901+
....
2902+
mknod </dev/path_to_dev> c <major> <minor>
2903+
....
2904+
2905+
Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.
2906+
2907+
A single driver can drive multiple compatible devices.
2908+
2909+
The major and minor numbers can be observed with:
2910+
2911+
....
2912+
ls -l /dev/urandom
2913+
....
2914+
2915+
Output:
2916+
2917+
....
2918+
crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom
2919+
....
2920+
2921+
which means:
2922+
2923+
* `c` (first letter): this is a character device. Would be `b` for a block device.
2924+
* `1, 9`: the major number is `1`, and the minor `9`
2925+
2926+
To avoid device number conflicts when registring the driver we:
2927+
2928+
* ask the kernel to allocate a free major number for us with: `register_chrdev(0`
2929+
* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name
2930+
2931+
Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758
2932+
2933+
===== Automatically create character device file on insmod
2934+
2935+
And also destroy it on `rmmod`:
2936+
2937+
....
2938+
/character_device_create.sh
2939+
echo $?
2940+
....
2941+
2942+
Outcome: the test passes:
2943+
2944+
....
2945+
0
2946+
....
2947+
2948+
Sources:
2949+
2950+
* link:kernel_module/character_device_create.c[]
2951+
* link:rootfs_overlay/character_device_create.sh[]
2952+
2953+
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
2954+
28782955
=== Kernel panic and oops
28792956

28802957
To test out kernel panics and oops in controlled circumstances, try out the modules:

kernel_module/README.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
... link:dep2.c[]
2121
. Pseudo filesystems
2222
.. link:anonymous_inode.c[]
23-
.. link:character_device.c[]
24-
.. link:character_device_create.c[]
2523
.. link:debugfs.c[]
2624
.. link:fops.c[]
2725
.. link:ioctl.c[]

kernel_module/character_device.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
/*
2-
Allows us to create device files with given file operations with mknod c X.
3-
4-
Usage:
5-
6-
/character_device.sh
7-
8-
The major number determines which module owns the device file.
9-
10-
minor is to differentiate between multiple instances of the device,
11-
e.g. two NVIDIA GPUs using the same kernel module.
12-
13-
We ask the kernel to automatically allocate a major number for us to avoid
14-
conlicts with other devices.
15-
16-
Then we need to check /proc/devices to find out the assigned number,
17-
and use that for the mknod.
18-
19-
- https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758
20-
*/
21-
221
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
232
#include <linux/module.h>
243
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */

kernel_module/character_device_create.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
Automatically create the device under /dev on insmod, and remove on rmmod.
3-
4-
https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
5-
*/
6-
71
#include <linux/cdev.h>
82
#include <linux/device.h>
93
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */

rootfs_overlay/character_device.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
set -ex
2+
set -e
33
insmod /character_device.ko
44
/mknoddev.sh lkmc_character_device
55
[ "$(cat /dev/lkmc_character_device)" = 'abcd' ]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
2-
set -ex
2+
set -e
33
insmod /character_device_create.ko
44
dev='/dev/lkmc_character_device_create_dev'
5-
[ "$(cat "$dev")" = 'abcd' ]
5+
[ "$(cat "$dev")" = abcd ]
66
rmmod character_device_create
77
[ ! -e "$dev" ]

0 commit comments

Comments
 (0)