driver-know-hows

device driver related stuff

View on GitHub

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

πŸ“š Tutorial Structure

This tutorial is organized into progressive modules, starting from fundamentals:

Part 0: Prerequisites & Fundamentals

Part 1: Kernel Fundamentals

Part 2: Core Driver Development

Part 3: Resource Management

Part 4: Hardware Interaction

Part 5: Advanced Driver Types

Part 6: Practical Guides

πŸ”§ Code Examples

The examples/ directory contains 9 complete, compilable examples:

Prerequisites Review

Kernel Module Examples

All examples include:

πŸ“– 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:

  1. Review C programming fundamentals
  2. Practice with examples in examples/00-c-review/
  3. Study Linux command line basics
  4. Return when comfortable

Development Environment Setup

Essential: DEVELOPMENT-SETUP.md

⚠️ NEVER test kernel modules on production systems!

Set up a safe environment:

  1. VM Setup (VirtualBox or QEMU) - Recommended
  2. 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

🀝 Contributing

This is a living tutorial. If you find errors or have improvements:

πŸ“š Additional Resources

Official Documentation

Online Resources

πŸŽ“ What Makes This Tutorial Special

Comprehensive Coverage

Safety First

Practical Focus

Beginner Friendly

πŸš€ 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! 🐧