Shweta, at her PC in her hostel room, was all set to explore the characters of Linux character drivers, before it was taught in class. She recalled the following lines from professor Gopi’s class: “… today’s first driver would be the template for any driver you write in Linux. Writing any specialised/advanced driver is just a matter of what gets filled into its constructor and destructor…”
With that, she took out the first driver’s code, and pulled out various reference books, to start writing a character driver on her own. She also downloaded the online book, Linux Device Drivers by Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman. Here is the summary of what she learnt.
W’s of character drivers
We already know what drivers are, and why we need them. What is so special about character drivers? If we write drivers for byte-oriented operations (or, in C lingo, character-oriented operations), then we refer to them as character drivers. Since the majority of devices are byte-oriented, the majority of device drivers are character device drivers.
Take, for example, serial drivers, audio drivers, video drivers, camera drivers, and basic I/O drivers. In fact, all device drivers that are neither storage nor network device drivers are some type of a character driver. Let’s look into the commonalities of these character drivers, and how Shweta wrote one of them.
The complete connection
Figure 1: Character driver overview
As shown in Figure 1, for any user-space application to operate on a byte-oriented device (in hardware space), it should use the corresponding character device driver (in kernel space). Character driver usage is done through the corresponding character device file(s), linked to it through the virtual file system (VFS). What this means is that an application does the usual file operations on the character device file. Those operations are translated to the corresponding functions in the linked character device driver by the VFS. Those functions then do the final low-level access to the actual device to achieve the desired results.
Note that though the application does the usual file operations, their outcome may not be the usual ones. Rather, they would be as driven by the corresponding functions in the device driver. For example, a write followed by a read may not fetch what has just been written to the character device file, unlike for regular files. Remember that this is the usual expected behaviour for device files. Let’s take an audio device file as an example. What we write into it is the audio data we want to play back, say through a speaker. However, the read would get us audio data that we are recording, say through a microphone. The recorded data need not be the played-back data.
In this complete connection from the application to the device, there are four major entities involved:
- Character device file
- Character device driver
- Character device
The interesting thing is that all of these can exist independently on a system, without the other being present. The mere existence of these on a system doesn’t mean they are linked to form the complete connection. Rather, they need to be explicitly connected. An application gets connected to a device file by invoking the open system call on the device file.
Device file(s) are linked to the device driver by specific registrations done by the driver. The driver is linked to a device by its device-specific low-level operations. Thus we form the complete connection. With this, note that the character device file is not the actual device, but just a place-holder for the actual device.
Major and minor numbers
The connection between the application and the device file is based on the name of the device file. However, the connection between the device file and the device driver is based on the number of the device file, not the name. This allows a user-space application to have any name for the device file, and enables the kernel-space to have a trivial index-based linkage between the device file and the device driver. This device file number is more commonly referred to as the
<major, minor> pair, or the major and minor numbers of the device file.
Earlier (till kernel 2.4), one major number was for one driver, and the minor number used to represent the sub-functionalities of the driver. With kernel 2.6, this distinction is no longer mandatory; there could be multiple drivers under the same major number, but obviously, with different minor number ranges.
However, this is more common with the non-reserved major numbers, and standard major numbers are typically preserved for single drivers. For example, 4 for serial interfaces, 13 for mice, 14 for audio devices, and so on. The following command would list the various character device files on your system:
<major, minor> related support in kernel 2.6
Type (defined in kernel header
dev_tcontains both major and minor numbers
Macros (defined in kernel header
MAJOR(dev_t dev)extracts the major number from
MINOR(dev_t dev)extracts the minor number from
MKDEV(int major, int minor)creates the
devfrom major and minor.
Connecting the device file with the device driver involves two steps:
- Registering for the
<major, minor>range of device files.
- Linking the device file operations to the device driver functions.
The first step is achieved using either of the following two APIs, defined in the kernel header
The first API registers the cnt number of device file numbers, starting from first, with the given name. The second API dynamically figures out a free major number, and registers the cnt number of device file numbers starting from
<the free major, firstminor>, with the given name. In either case, the
/proc/devices kernel window lists the name with the registered major number. With this information, Shweta added the following into the first driver code:
In the constructor, she added:
In the destructor, she added:
It’s all put together, as follows:
Then, Shweta repeated the usual steps that she’d learnt for the first driver:
- Build the driver (
.kofile) by running
- Load the driver using
- List the loaded modules using
- Unload the driver using
Additionally, before unloading the driver, she peeped into the
/proc/devices kernel window to look for the registered major number with the name “Shweta”, using
cat /proc/devices. It was right there. However, she couldn’t find any device file created under
/dev with the same major number, so she created them by hand, using
mknod, and then tried reading and writing those. Figure 2 shows all these steps.
Figure 2: Character device file experiments
Please note that the major number
250 may vary from system to system, based on availability. Figure 2 also shows the results Shweta got from reading and writing one of the device files. That reminded her that the second step to connect the device file with the device driver — which is linking the device file operations to the device driver functions — was not yet done. She realised that she needed to dig around for more information to complete this step, and also to figure out the reason for the missing device files under
We will deal with her further learning in our next article.