A simple linux device character driver.
├── modules
│ ├── simple_char_driver.c # module code
│ └── Makefile # defines module object
└── userspacetest.c # test program code
-
cdinto the modules directory, which should contain simple_char_driver.c and the Makefile -
Create the module object:
make -C /lib/modules/$(uname -r)/build M=$PWD modulesyou should now see a file named simple_char_driver.ko within the modules directory. -
Create the device file:
sudo mknod -m 777 /dev/simple_char_driver c 240 0. 240 is the major number of the driver and it is possible that this number may be taken, so some other major number may need to be picked. -
Install the module:
sudo insmod simple_char_driver.ko -
Confirm installation:
lsmod. You should see simple_char_driver listed. -
Compile the test app:
gcc -o test driver_test.c -
Run the test app:
./test -
View module kernel outputs with:
dmesg -
You can uninstall the module with:
sudo rmmod simple_char_driver
The interactive test app gives the following options:
- Press r to read from the device.
- Press w to write to the device.
- Press s to seek into the device.
- Press e to exit from the device/test app.
If the user presses r then they will be asked for the number of bytes to read and a buffer of that size will be created to read the data.
Data read from the device file is printed starting from the current position.
If the user presses w then the app will ask for the data to be written from the user.
If the user presses s then they will be promped for an offset value and and value for whence.
If the user presses e then the app is shut down.