Featured image of post Linux Device Drivers in a Nutshell

Linux Device Drivers in a Nutshell

Intro to Device Drivers in Linux

History of Device Drivers in Linux

In the early 1990s, a Finnish student named Linus Torvalds embarked on a personal project to create a free operating system kernel.

Linux was born.

Early Days: Monolithic Kernel and Static Drivers

Initially, Linux adopted a monolithic kernel architecture, where the entire operating system, including device drivers, ran in a single address space.

In these early versions, device drivers were often statically compiled into the kernel.

This approach meant that to add or update a driver, one had to recompile and reboot the entire kernel—a process that was both time-consuming and impractical for systems requiring high availability.

Mnay other flavors of Unix took this approach at the time..

I can remember the geeks comparing notes as to who survived a Kernel compile or not…

Introduction of Loadable Kernel Modules

To address the limitations of static drivers, the concept of Loadable Kernel Modules (LKMs) was introduced.

LKMs allow drivers to be loaded and unloaded into the kernel at runtime without needing a reboot.

This enabled easier updates and the ability to add support for new hardware on-the-fly.

Simple example of a Loadable Kernel Module:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <linux/module.h>
#include <linux/kernel.h>

static int __init lkm_example_init(void) {
    printk(KERN_INFO "LKM Example: Module loaded.\n");
    return 0;
}

static void __exit lkm_example_exit(void) {
    printk(KERN_INFO "LKM Example: Module unloaded.\n");
}

module_init(lkm_example_init);
module_exit(lkm_example_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple Loadable Kernel Module example.");

To compile and load this module:

1
2
3
4
make
sudo insmod lkm_example.ko
sudo rmmod lkm_example
dmesg

The /dev Directory and Device Files

In Linux, devices are represented as files in the /dev directory.

This allows user-space applications to interact with hardware devices using standard file operations like read and write. Device files are categorized into:

  • Character Devices: Accessed sequentially (e.g., keyboards, mice).
  • Block Devices: Accessed randomly, typically used for storage devices (e.g., hard drives).

Each device file is associated with a major and minor number, which the kernel uses to identify the appropriate driver to handle operations on that device.

The Role of udev and Dynamic Device Management

As systems became more dynamic, with devices being added and removed on-the-fly (especially with the advent of USB), static device files in /dev became insufficient.

To handle this, the udev system was introduced. udev is a device manager for the Linux kernel that handles the creation and removal of device nodes in /dev dynamically, providing a flexible and efficient way to manage device files.

Modern Driver Development: Embracing Open Source

Today, Linux supports a metric TON (how can a device driver weigh anything?) of hardware devices, likely more than any other operating system in history.

References