Linux Device Drivers: Complete Tutorial Guide
A comprehensive, in-depth guide to Linux device driver development from prerequisites to advanced topics.
π― Why Learn Device Drivers?
TL;DR: High-paying careers ($120k-$180k), work on cool hardware (Tesla, SpaceX, NVIDIA), understand how computers really work, and build amazing IoT/robotics/automotive projects.
Read more: WHY-LEARN-DRIVERS.md - Career paths, real-world applications, success stories, and industry trends.
Quick Motivation
- π° Career: Device driver developers are in high demand and well-paid
- π Hardware Projects: Build robots, IoT devices, custom hardware
- π§ Understanding: Learn how computers really work at the lowest level
- π Impact: Your code can run on millions of devices
- π Job Security: Essential skill that canβt be outsourced or automated
π Tutorial Structure
This tutorial is organized into progressive modules, starting from fundamentals:
Part 0: Prerequisites & Fundamentals
- 00-prerequisites.md - Essential Knowledge Before Starting
- C programming essentials (pointers, structures)
- Linux system fundamentals
- Computer architecture basics
- Build system basics
- Self-assessment quiz
Part 1: Kernel Fundamentals
- 01-basics.md - Kernel Modules & Driver Fundamentals
- Kernel architecture overview
- Kernel vs userspace
- Building and loading modules
- Module parameters and dependencies
Part 2: Core Driver Development
- 02-char-drivers.md - Character Device Drivers
- Device registration and management
- Major/minor numbers
- cdev structure and operations
- 03-file-operations.md - File Operations in Detail
- open, release, read, write
- ioctl and unlocked_ioctl
- poll and select mechanisms
- mmap implementation
Part 3: Resource Management
- 04-memory.md - Memory Management
- kmalloc, vmalloc, and friends
- Memory pools and slabs
- DMA-capable memory
- User-kernel memory transfer
- 05-concurrency.md - Locking & Synchronization
- Spinlocks and mutexes
- Semaphores and completion
- Read-write locks
- RCU (Read-Copy-Update)
- Atomic operations
Part 4: Hardware Interaction
- 06-interrupts.md - Interrupt Handling
- Requesting and freeing IRQs
- Top-half vs bottom-half
- Tasklets and work queues
- Threaded interrupts
- MSI/MSI-X
- 07-dma.md - Direct Memory Access (DMA)
- DMA mapping APIs
- Coherent vs streaming mappings
- Scatter-gather DMA
- DMA pools
Part 5: Advanced Driver Types
- 08-platform-drivers.md - Platform Device Drivers
- Platform device model
- Device tree bindings
- Resources and IOMEM
- Power management
- 09-11-advanced-debugging.md - Advanced Topics
- Block device drivers
- Network device drivers
- Debugging techniques and tools
Part 6: Practical Guides
- DEVELOPMENT-SETUP.md - Environment Setup
- VM configuration (VirtualBox, QEMU)
- Required software installation
- Editor setup and workflow
- Debugging environment
- TROUBLESHOOTING.md - Problem Solving
- Common issues and solutions
- Crash recovery procedures
- Debugging techniques
- Error reference
π§ Code Examples
The examples/ directory contains 9 complete, compilable examples:
Prerequisites Review
- 00-c-review/ - C Programming Fundamentals
- Pointer mastery (essential!)
- Structure fundamentals
- Userspace practice before kernel
Kernel Module Examples
- 01-hello/ - Hello World Module
- 02-chardev/ - Character Device
- 03-ioctl/ - Device Control Interface
- 04-memory/ - Memory Allocation
- 05-locks/ - Synchronization (with race conditions!)
- 06-timer/ - Kernel Timers
- 07-procfs/ - /proc Filesystem
- 08-workqueue/ - Work Queues
All examples include:
- Heavily commented source code
- Makefiles for building
- README with usage instructions
- Test programs where applicable
π How to Use This Tutorial
Prerequisites Check
Start here if youβre new: 00-prerequisites.md
Take the self-assessment quiz. If you canβt answer most questions:
- Review C programming fundamentals
- Practice with examples in
examples/00-c-review/ - Study Linux command line basics
- Return when comfortable
Development Environment Setup
Essential: DEVELOPMENT-SETUP.md
β οΈ NEVER test kernel modules on production systems!
Set up a safe environment:
- VM Setup (VirtualBox or QEMU) - Recommended
- Install Required Packages:
```bash
Ubuntu/Debian
sudo apt-get install build-essential linux-headers-$(uname -r) git
Fedora/RHEL
sudo dnf install gcc make kernel-devel kernel-headers git
Arch
sudo pacman -S base-devel linux-headers
3. **Take VM Snapshot** before each testing session
### Learning Path
#### **Beginner Level** (Weeks 1-2)
1. β
Complete prerequisites review
2. π Read: 00-prerequisites.md
3. π» Practice: examples/00-c-review/
4. π Read: 01-basics.md
5. π» Code: examples/01-hello/
6. π Read: 02-char-drivers.md
7. π» Code: examples/02-chardev/
#### **Intermediate Level** (Weeks 3-6)
1. π Read: 03-file-operations.md, 04-memory.md
2. π» Code: examples/03-ioctl/, 04-memory/
3. π Read: 05-concurrency.md
4. π» Code: examples/05-locks/ (see race conditions!)
5. π Read: 06-interrupts.md
6. π» Code: examples/06-timer/, 08-workqueue/
#### **Advanced Level** (Weeks 7-12)
1. π Read: 07-dma.md, 08-platform-drivers.md
2. π Read: 09-11-advanced-debugging.md
3. π» Code: examples/07-procfs/
4. π§ Study TROUBLESHOOTING.md
5. π Build your own driver!
## β οΈ Important Warnings
### Before You Start
- **Kernel Code is Dangerous**: Bugs can crash the system, corrupt data, or create security vulnerabilities
- **Always Test in VMs**: Use virtual machines or test hardware for development
- **Backup Everything**: Keep backups before testing kernel code
- **Check Kernel Version**: APIs change between kernel versions (this tutorial targets 5.x/6.x kernels)
- **No Standard Library**: Can't use printf, malloc, etc. (use printk, kmalloc instead)
### Safety Checklist
Before loading ANY kernel module:
- [ ] Code is tested in VM
- [ ] VM snapshot taken
- [ ] Code compiled without warnings
- [ ] Reviewed for obvious bugs
- [ ] Know how to force reboot (SysRq keys)
- [ ] Not on production machine
### Emergency Recovery
If system hangs:
1. Try: `Ctrl+Alt+F1` (switch to console)
2. Try: `Alt+SysRq+B` (force reboot)
3. Last resort: Power cycle
## π― Learning Objectives
By completing this tutorial, you will:
- β
Understand Linux kernel architecture and driver model
- β
Master C programming for kernel development
- β
Write character, block, and network device drivers
- β
Handle hardware interrupts and DMA operations
- β
Implement proper synchronization and locking
- β
Debug kernel code effectively
- β
Follow kernel coding standards and best practices
- β
Navigate and contribute to kernel source code
## π Conventions Used
- **Code Comments**: Extensive inline comments explain every concept
- **Warning Boxes**: Highlight common pitfalls and security issues
- **Theory Sections**: Explain "why" before "how"
- **Kernel Version**: Code targets modern kernels (5.x/6.x) with notes on compatibility
## π Quick Start (If You're Experienced)
```bash
# 1. Check prerequisites
cd examples/00-c-review && make test
# 2. Hello world module
cd ../01-hello && make
sudo insmod hello.ko
dmesg | tail
sudo rmmod hello
# 3. Character device
cd ../02-chardev && make
sudo insmod simple_char.ko
echo "test" > /dev/simple_char
cat /dev/simple_char
sudo rmmod simple_char
# 4. See race conditions in action!
cd ../05-locks && make
sudo insmod lock_demo.ko
dmesg | tail -50 # Watch unsafe counter lose updates
sudo rmmod lock_demo
π Tutorial Statistics
- 12 Theory Chapters (~65,000 words)
- 9 Working Examples (~2,000 lines of code)
- 4 Practical Guides (Setup, Troubleshooting, Getting Started)
- Complete Self-Assessment Quizzes and exercises
π€ Contributing
This is a living tutorial. If you find errors or have improvements:
- Test all code before submitting
- Follow kernel coding style (checkpatch.pl)
- Add detailed explanations for complex concepts
π Additional Resources
Official Documentation
Recommended Books
- βLinux Device Driversβ by Corbet, Rubini, and Kroah-Hartman
- βLinux Kernel Developmentβ by Robert Love
- βUnderstanding the Linux Kernelβ by Bovet & Cesati
- βThe C Programming Languageβ by K&R
Online Resources
π What Makes This Tutorial Special
Comprehensive Coverage
- β Prerequisites through advanced topics
- β Theory AND practice for every concept
- β Real working code, not pseudocode
- β Complete development environment setup
Safety First
- β VM setup instructions
- β Troubleshooting guide
- β Emergency recovery procedures
- β Common pitfalls highlighted
Practical Focus
- β 9 complete examples
- β Race condition demonstrations
- β Userspace test programs
- β Real-world patterns
Beginner Friendly
- β Prerequisites chapter
- β C programming review
- β Self-assessment quizzes
- β Progressive difficulty
π Letβs Begin!
New to kernel development? β Start with 00-prerequisites.md
Comfortable with C and Linux? β Jump to 01-basics.md
Need environment setup? β Read DEVELOPMENT-SETUP.md
Having problems? β Check TROUBLESHOOTING.md
Last Updated: December 2025
Target Kernel Version: 5.15+ / 6.x
License: Educational use - adapt and share freely
β οΈ Remember: Always develop in a safe, isolated environment!
Happy kernel hacking! π§