Session 5: Character Device Drivers
Tasks
Download and unzip the tasks archive sesiune-05 on the virtual machine.
Simple character device driver
Use the code in the archive to implement a simple device that has a text buffer that can be read and written.
The module will use major 42 and minor 0. Make use of this information when creating device node file
sudo mknod /dev/mydev c 42 0
All the code is implemented by commented out. Take each TODO
and analyse the commented code and then uncomment it step by step.
After implementing TODO 1a and TODO 1b, compile, load the module and view the contents of /proc/devices
to see that the kernel module registered the major number.
After implementing TODO 5, compile, load the module and do a cat /dev/mydev
and and echo something>/dev/mydev
and see the dmesg messages.
After implementing all TODOs, write something into /dev/mydev and read from it.
Double the work, same effort
Unique access
Create a new character device that prints out the string “42” forever (sort of like /dev/zero).
Implement only the open, read, and release file operation (make mocks for the rest).
Try to access the device driver with two cat processes (in two terminals, for example).
Attach an atomic variable to the char_device structure that keeps track of how many processes access the device
In the open handler, set the variable to 1 if is set to 0; if it's already set to 1, return -EBUSY
Try to access the device driver again with two cat processes.