What is a Device Driver and How Does It Function in Linux?

Beaglebone black board

In this article, we explain what a device driver is and specifically how device drivers function in a linux operating system.

So a device driver is a piece of code that configures and manages a device.

The device driver knows how to work with the particular device, such as configuring the device, sending data to the device, or processing requests which originate from the device.

The device driver is written in kernel space, but when the device drier is loaded into an operating system such as linux, it exposes interfaces to the user-space so that the user application can communicate with the device.

Without the device driver, the OS/Application will not have a clear picture of how to deal with the device. And this is the reason why devices need device drivers in order to function with operating systems.

If you've ever plugged a device into a computer and see a description of the device appear on the operating system, then you've seen a device driver in operation. For example, with an SD card or an external hard drive, you will most likely see a description of the device appear and then a means of interacting with the device, such as opening up the external storage medium via a directory, which you can begin adding files. This is the user space interacting with the device driver to use the device.

All of this is possible due to device drivers.

Below is a list of devices that the linux operating system is aware of.

Devices on the linux operating system

For example, sda is a hard drive. sda1, sda2, and sda5 are partitions of this hard drive.

To get an idea of how device drivers interact with hardware and the user space, we have the following diagram shown below.

How device drivers interact with hardware and the user space

So let's say there is a device, and this device can be anything, such as a SD memory card, a microphone, a speaker, an amplifier, etc.

The device itself represents the hardware, which is what you see at the bottom of the diagram shown above.

The device driver, which instructs the operating system about the functioning of the device exists in the kernel space.

Then, lastly, we have the user space. This is what the user of the operating system deals with in regard to the software on the computer to interact with the hardware. For a SD memory card, this could mean opening up the directory that stores the files for the card. The user can open it, add files to it, delete files from it, modify files in it, and close the application. The user should also be able to see the device present in the dev directory in the linux operating system.

What you see in the square box at the top- write, read, open, close- represent system calls which are handled by the device driver code to interact with various registers of the device.

Again, just think of an SD card, where you open up the directory storing the files and you add a file. The device driver has to know how to add this file into the actual SD card, so it has where to store this.

So this is the function of a device driver. The linux operating system isn't going to know which registers in a device to manipulate or even what the device is. Therefore, a device is necessary for each device that gets input into a comjputer.

So device drivers removes the burden of interacting with hardware devices and exports interfaces that applications and other modules of the kernel can use to access the device.

Device drivers address many aspects that the operating system kernel will know nothing about, including the register addresses, various configurations needed, and how to deal with interrupts that originate from the device.

The device driver utilizes kernel services to talk to the user space. The job of the kernel is to connect system call executions from the user space to the driver's system call handler methods.

Types of Device Drivers

There are different types of device drivers.

It's not just all one category.

There are character device drivers, block device drivers, and network device drivers.

This is shown in the diagram below.

Types of device drivers

Character Drivers

Character device drivers are drivers that access data from the device sequentially, i.e., byte by byte (like a stream of characters), not as a chunk data.

Sophisticated buffering strategies are usually not involved in character drives. Because when you want to write 1 byte, it directly goes to the device without any intermediate buffering, delayed write back, or dirty buffer management.

Character devices include sensors, keyboard, mouse, serial port, parallel port, etc.

Block Drivers

Block drivers are drivers which handle data in chunks or blocks.

Block drivers are more complicated than character drivers because block drivers should implement advanced buffering strategies to read and write to the block devices, and disk caches are involved.

Examples of block drivers are mass storage devices such as hard disks, S flash memory, USB camera, etc.

Network Drivers

Lastly, network drivers deal with network connections such as bluetooth, ethernet connections, or wifi connections.

Device Files in Linux

In linux, devices are accessed as a file (same in Unix).

A device file is a special file or a node which gets populated in the /dev directory during kernel boot time or device/driver hot plug events.

By using a device file, a user application and a driver can communicate with each other.

Device files are managed as part of a virtual file system (VFS) subsystem of the kernel.

Below we use the command, ls -l, to check what type of device driver are each of the devices in the /dev directory in linux.

Checking the type of device driver in linux

Looking all the way to the left can let us know what each is.

sda, which is the hard drive, you can see starts with a 'b'; therefore, it is a block device.

tty, which is a serial port, starts with 'c', so it is a character device.

"If starting with a 'd', that signifies a directory.

So this is what a device driver is and how it works in the linux operating systm.

So this is how to create a custom makefile that can build a linux kernel module on a local or target host device.

Related Resources

HTML Comment Box is loading comments...