OS Notes
Understanding device drivers — their role as hardware-software interface, driver architecture, how drivers are loaded, and the relationship between drivers and the kernel.
Introduction
Your computer can work with thousands of different hardware devices — printers from HP, Canon, and Epson, graphics cards from NVIDIA and AMD, network adapters from Intel and Broadcom. How does one operating system support all these different devices without being rewritten for each one? The answer is device drivers — specialized software modules that translate the OS's generic commands into device-specific operations.
A device driver is like a translator at the United Nations. The OS speaks one language (standard I/O interface), and each hardware device speaks its own proprietary language. The driver translates between them, allowing the OS to communicate with any device without knowing its internal details.
What Device Drivers Do
A driver provides a standard interface to the OS while handling device-specific details:
| Application | write(fd, data, size) |
| OS Kernel | "Write data to this device" |
| Hardware | Physical device operation |
Specifically, drivers handle:
- Initialization: Set up device to a known state at boot/plug-in
- Command translation: Convert generic OS commands to device-specific register writes
- Data transfer: Move data between device and memory (using DMA or programmed I/O)
- Interrupt handling: Respond to device interrupts (data ready, error occurred)
- Error handling: Detect and recover from device errors
- Power management: Suspend/resume device for power saving
Driver Architecture in Linux
| NVIDIA | Intel | AHCI | ||||
|---|---|---|---|---|---|---|
| GPU drv | NIC drv | disk drv |
Linux Kernel Module (Loadable Driver)
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
// Called when module is loaded
static int __init my_driver_init(void) {
printk(KERN_INFO "My device driver loaded\n");
// Register with kernel, set up device
return 0;
}
// Called when module is unloaded
static void __exit my_driver_exit(void) {
printk(KERN_INFO "My device driver unloaded\n");
// Cleanup, unregister
}
module_init(my_driver_init);
module_exit(my_driver_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Example device driver");# Load a driver module
sudo insmod my_driver.ko
# Or use modprobe (handles dependencies)
sudo modprobe my_driver
# List loaded modules
lsmod | grep my_driver
# Remove module
sudo rmmod my_driverTypes of Drivers
| Type | Purpose | Examples |
|---|---|---|
| Character driver | Byte-stream devices | Keyboard, serial port, mouse |
| Block driver | Block-addressable storage | HDD, SSD, USB drives |
| Network driver | Network interface | Ethernet, WiFi adapters |
| Display driver | Graphics output | GPU drivers (NVIDIA, AMD) |
| File system driver | Storage organization | ext4, NTFS, FAT32 |
User-Space vs Kernel-Space Drivers
Kernel-space drivers run in kernel mode with full hardware access. Most traditional drivers work this way. A bug crashes the whole system.
User-space drivers run in user mode. Safer (bugs cannot crash kernel) but slower (requires system calls to access hardware). Used for USB devices (libusb), printers (CUPS), and some GPU functionality.
Plug and Play
Modern systems detect new hardware automatically:
- Device connected (USB plugged in, PCI card detected at boot)
- Bus enumerates device (reads vendor/product ID)
- OS searches for matching driver (driver database)
- Driver loaded and device initialized
- Device available for use
Real-World Analogy
Device drivers are like electrical adapters for international travel. Your laptop charger (OS) has a standard plug, but outlets (devices) differ in every country. The adapter (driver) knows the specific outlet type and translates your standard plug into the correct form. Without the right adapter, your charger cannot connect — just like without the right driver, the OS cannot communicate with the device.
Key Takeaways
- Device drivers translate between the OS's standard interface and device-specific hardware protocols
- Drivers run in kernel space (fast but risky) or user space (safe but slower)
- Linux uses loadable kernel modules that can be inserted/removed at runtime
- Drivers handle initialization, command translation, data transfer, interrupts, and error recovery
- Plug and Play automates driver loading when new hardware is detected
- Driver bugs are the #1 cause of OS crashes — more than kernel bugs
- Writing correct drivers requires deep knowledge of both hardware and kernel internals
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Device Drivers.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, input, output, device, drivers
Related Operating Systems Topics