Character Device Driver
Character Device Driver
We would be writing a Linux device driver for a hypothetical character device which reverses
any string that is given to it. i.e. If we write any string to the device file represented by the
device and then read that file, we get the string written earlier but reversed (for eg., myDev
being our device, echo hello >/dev/myDev ; cat /dev/ myDev would print olleh).
We will be simulating the functionality of the device in the driver itself (and this is precisely
what is done in emulation tools like Daemon Tools, Alcohol etc).
We have summarized how to write a device driver, the significance of a device file and its role in
the interaction between a user program and device driver.
The devices in UNIX fall in two categories- Character devices and Block devices. Character
devices can be compared to normal files in that we can read/write arbitrary bytes at a time
(although for most part, seeking is not supported).They work with a stream of bytes. Block
devices, on the other hand, operate on blocks of data, not arbitrary bytes. Usual block size is 512
bytes or larger powers of two. However, block devices can be accessed the same was as
character devices, the driver does the block management. (Networking devices do not belong to
these categories, the interface provided by these drivers in entirely different from that of
char/block devices)
The beauty of UNIX is that devices are represented as files. Both character devices and block
devices are represented by respective files in the /dev directory. This means that you can read
and write into the device by manipulating those file using standard system calls like open, read,
write, close etc.
For eg, you could directly write or read the hard disk by accessing /dev/sd* file a dangerous act
unless you know what you are doing (for those interested, try hexdump C /dev/sda n 512
what you see then is the boot sector of your hard disk !). As another example, you could
directly see the contents of the RAM by reading /dev/mem.
Every device file represented in this manner is associated with the device driver of that device
which is actually responsible for interacting with the device on behalf of the user request. So
when you access a device file, the request is forwarded to the respective device driver which
does the processing and returns the result.
For instance, you might be knowing about the files /dev/zero (an infinite source of zeroes),
/dev/null (a data black hole), /dev/random (a source of random numbers) etc. When you actually
read these files, what happens is that a particular function in the device driver registered for the
file is invoked which returns the respective data.
In our example, we will be developing a character device represented by the device file
/dev/myDev. The mechanisms for creating this file will be explained later.
ls -l
The starting c means its a character device, 1 is the major number and 8 is the minor number.
A Linux driver is a Linux module which can be loaded and linked to the kernel at runtime. The
driver operates in kernel space and becomes part of the kernel once loaded, the kernel being
monolithic. It can then access the symbols exported by the kernel.
When the device driver module is loaded, the driver first registers itself as a driver for a
particular device specifying a particular Major number.
It uses the call register_chrdev function for registration. The call takes the Major number,
Minor number, device name and an address of a structure of the type file_operations(discussed
later) as argument. In our example, we will be using a major number of 89 . The choice of major
number is arbitrary but it has to be unique on the system.
The syntax of register_chrdev is
int register_chrdev(unsigned int major,const char *name,struct file_operations *fops)
Driver is unregistered by calling the unregister_chrdev function.
Since device driver is a kernel module, it should implement init_module and cleanup_module
functions. The register_chrdev call is done in the init_module function and unregister_chrdev
call is done in the cleanup_module function.
The register_chrdev call returns a non-negative number on success. If we specify the Major
number as 0, the kernel returns a Major number unique at that instant which can be used to create
a device file.
A device file can be created either before the driver is loaded if we know the major and minor
number beforehand or it can be created later after letting the driver specify a major number for
us.
Apart from those, the driver must also define certain callback functions that would be invoked
when file operations are done on the device file. Ie. It must define functions that would be
invoked by the kernel when a process uses open, read, write, close system calls on the file. Every
driver must implement functions for processing these requests.
When register_chrdev call is done, the fourth argument is a structure that contains the addresses
of these callback functions, callbacks for open, read, write, close system calls. The structure is of
the type file_operations and has 4 main fields that should be set read,write,open and
release. Each field must be assigned an address of a function that would be called when open,
read,write , close system calls are called respectively. For eg
mknod command
chmod, though not necessary is done because, if not done, only processes will root permission
can read or write to our device file.
cat /proc/modules
cat /proc/modules
To unload the driver, use rmmod command. (rmmod myDev.ko)
See the output. (The reason for the ugly output is because echo automatically writes a newline
character to the end of string. When the driver reverses the string, the newline is shifted to the
front of the string and there is no newline at the end. Hence the result being ugly)
To see how this can be done from our program, I wrote a demo program given below