Background/Usage
This is a simple character driver meant as a reference design. A simple character string can be written to the driver and then recalled multiple times.
For example:
echo "Hello Universe" > /dev/simpleCharDevice0 cat /dev/simpleCharDevice0
The cat command will playback the string written. The driver is robust enough to handle the fact that read() doesn’t always return the exact number of bytes as are in the string.
On the user side, the echo command acts as a system write() function, so this is tantamount to a C sysopen() and write(). Likewise the cat is a sysopen() and read().
Usage
Tested on Ubuntu 20. % git clone https://github.com/rogerpease/LinuxKernelSimpleCharacterDriver % cd LinuxSimpleCharacterDriver/src % make % insmod SimpleCharacterDriver.ko # (if you get an Operation Not Permitted you may need to Sign the module) % mknod /dev/simpleCharDevice0 c 228 0 % echo "Hello Universe" > /dev/simpleCharDevice0 % cat /dev/simpleCharDevice0
Notes
The Major Device number hardcoding is a bit antequated.. I know there are ways of dynamically allocating those device numbers. Otherwise, this is essentially a file operation:
// Declare File operations structure.
static const struct file_operations my_dev_fops = {
.owner = THIS_MODULE,
.read = charDriverFileRead, // Called when read() is called.
.write = charDriverFileWrite, // Called when write() is called
.open = charDriverFileOpen, // Called when open() is called.
.release = charDriverFileClose, // called with close() is called.
.unlocked_ioctl = charDriverFileIOCTL,
};
Each filehandle is passed as a context. For example, the read function
ssize_t charDriverFileRead (struct file *filp, char * buf, size_t byteCount, loff_t * fileOffset)
can be used to keep information about where this individual file access left off, just like you can have multiple processes reading a disk file but keeping separate location pointers.
Relevance to work experience
I have done plenty of work on TMS320C6X devices (DMA, HPIs, etc) including Device drivers in assembly, C and C with SYS/BIOS.