Skip to content

Latest commit

 

History

History
487 lines (368 loc) · 11.3 KB

File metadata and controls

487 lines (368 loc) · 11.3 KB

Examples Guide - What Each Example Teaches and Where It's Used

Overview

Each example in this tutorial demonstrates concepts used in real production drivers. Here's where you'll actually use these skills.


00-c-review/ - C Programming Review

What You Learn

  • Pointer mastery (critical!)
  • Structure fundamentals
  • Memory management
  • Function pointers

Where It's Used

Everywhere in kernel development!

Real-world examples:

// Pointers to hardware registers (like in graphics drivers)
volatile uint32_t *reg = (volatile uint32_t *)0xF8000000;
*reg = 0x01;  // Enable device

// Structures with operations (like file_operations)
struct driver_ops {
    int (*init)(void);
    void (*cleanup)(void);
};

// Function pointers for polymorphism
driver->ops->init();  // Dynamic dispatch

Used in: Every single driver ever written


01-hello/ - Hello World Module

What You Learn

  • Module initialization/cleanup
  • Kernel logging (printk)
  • Module metadata
  • Loading/unloading modules

Where It's Used

Foundation for all drivers

Real-world examples:

  • NVIDIA graphics driver initialization
  • WiFi driver loading at boot
  • USB driver module parameters
  • Filesystem modules (ext4, btrfs)

Industry use:

  • All loadable drivers use this pattern
  • Testing hardware without rebooting
  • Distributing proprietary drivers

Example: NVIDIA driver is ~25MB kernel module that initializes GPU


02-chardev/ - Character Device

What You Learn

  • Device file creation (/dev/*)
  • Read/write operations
  • Device numbers (major/minor)
  • Automatic device node creation

Where It's Used

Most hardware devices!

Real-world examples:

  1. Serial Ports (/dev/ttyS0, /dev/ttyUSB0)

    • Arduino communication
    • GPS devices
    • Industrial sensors
    • Modems
  2. Input Devices (/dev/input/*)

    • Keyboards
    • Mice
    • Touchscreens
    • Game controllers
  3. Random Generators (/dev/random, /dev/urandom)

    • Cryptography
    • Security keys
    • SSL/TLS
  4. GPIO (/dev/gpiochip0)

    • Raspberry Pi pins
    • LED control
    • Button inputs
    • Sensor interfaces
  5. I2C/SPI (/dev/i2c-*, /dev/spidev*)

    • Temperature sensors
    • Accelerometers
    • OLED displays
    • EEPROM chips

Companies using: Tesla (vehicle sensors), Ring (doorbell), Nest (thermostat)


03-ioctl/ - Device Control Interface

What You Learn

  • ioctl command encoding
  • Device configuration
  • Userspace/kernel communication
  • Input validation

Where It's Used

Almost every driver needs configuration

Real-world examples:

  1. Video4Linux2 (V4L2) - Camera control

    v4l2-ctl --set-fmt-video=width=1920,height=1080
    • Webcam settings (exposure, white balance)
    • Professional cameras
    • Video capture cards
  2. ALSA - Sound card control

    amixer set Master 50%
    • Volume control
    • Audio routing
    • Effect parameters
  3. Network Interfaces - ethtool

    ethtool -s eth0 speed 1000
    • Speed/duplex settings
    • Wake-on-LAN
    • Statistics
  4. GPU Control - AMD/NVIDIA

    • GPU clock speeds
    • Memory frequency
    • Power limits
    • Fan curves
  5. Storage - hdparm

    hdparm -tT /dev/sda
    • Drive parameters
    • Power management
    • Performance tuning

Companies using: Google (TPU control), NVIDIA (GPU tuning), Tesla (sensor config)


04-memory/ - Memory Allocation

What You Learn

  • kmalloc vs vmalloc
  • Slab caches
  • Memory pools
  • Allocation strategies

Where It's Used

Every driver, every second

Real-world examples:

  1. Network Drivers - Packet buffers

    // Intel i40e driver allocates ~256 packet buffers
    struct sk_buff *skb = netdev_alloc_skb(dev, 2048);
    • Uses slab cache for performance
    • Preallocated pools
    • Zero-copy transfers
  2. Block Drivers - I/O buffers

    • NVMe driver: Command buffers
    • SCSI driver: Request pools
    • RAM disk: Memory backing
  3. Graphics Drivers - Framebuffers

    • GPU memory management
    • Texture caches
    • Vertex buffers
  4. USB Drivers - Transfer buffers

    • Bulk transfers
    • Isochronous (audio/video)
    • Control messages

Performance impact:

  • Bad: Allocate on each packet → 20,000 packets/sec
  • Good: Preallocated pool → 2,000,000 packets/sec

Companies using: Meta (network optimization), Netflix (streaming), Cloudflare (high-performance)


05-locks/ - Synchronization

What You Learn

  • Race conditions (see them happen!)
  • Spinlocks vs mutexes
  • Atomic operations
  • Deadlock prevention

Where It's Used

Multi-core systems (everything modern)

Real-world examples:

  1. Network Stack - Packet queue protection

    // Without locks: Corrupted packets, crashes
    // With locks: 10Gbps throughput, zero corruption
    • TX ring buffer (spinlock)
    • RX queue (atomic counters)
    • Statistics (per-CPU variables)
  2. Block Layer - Request queue

    • Multiple threads issuing I/O
    • NVMe: 64,000 parallel commands
    • Must coordinate safely
  3. Graphics - GPU command submission

    • Multiple processes rendering
    • Shared GPU resources
    • Texture cache coordination
  4. File Systems - Inode locking

    • Multiple processes accessing files
    • Directory operations
    • ext4, btrfs, XFS

Bug example: Ubuntu bug #1234 - WiFi driver race condition crashed laptops. Fixed with proper spinlock. Affected 1 million users.

Companies using: Every company with multi-threaded code (everyone)


06-timer/ - Kernel Timers

What You Learn

  • Periodic execution
  • Jiffies and time conversion
  • Timer management
  • Softirq context

Where It's Used

Polling, timeouts, periodic tasks

Real-world examples:

  1. Network Drivers - Link status checking

    // Check link every 2 seconds
    mod_timer(&adapter->watchdog_timer, jiffies + 2*HZ);
    • Ethernet link detection
    • Carrier sense
    • Statistics updates
  2. Watchdog Timers - System health monitoring

    • Detect hardware hangs
    • Automatic recovery
    • Keep-alive signaling
  3. USB - Device enumeration timeout

    • Waiting for device response
    • Retry logic
    • Error recovery
  4. Bluetooth - Connection keep-alive

    • ACL links
    • Inquiry scans
    • Page scans
  5. Power Management - Idle detection

    • Screen timeout
    • CPU frequency scaling
    • Device suspend

Companies using: Apple (device timeouts), Fitbit (sensor polling), Tesla (battery monitoring)


07-procfs/ - /proc Filesystem

What You Learn

  • Exposing kernel data
  • Runtime control
  • seq_file interface
  • User-friendly debugging

Where It's Used

Debugging, monitoring, configuration

Real-world examples:

  1. Network Statistics (/proc/net/dev)

    cat /proc/net/dev
    # See packets RX/TX for all interfaces
  2. Driver Information

    cat /proc/driver/nvidia/version
    # NVIDIA driver version info
  3. Custom Hardware

    echo 1 > /proc/my_device/enable
    cat /proc/my_device/status
  4. System Monitoring Tools

    • htop reads /proc/[pid]/stat
    • iotop reads /proc/[pid]/io
    • lsof reads /proc/[pid]/fd/

Modern alternative: sysfs (/sys/) for device attributes

Companies using: Every embedded Linux system (routers, set-top boxes, IoT)


08-workqueue/ - Work Queues

What You Learn

  • Deferred work in process context
  • Work queues vs tasklets
  • Can sleep (unlike interrupts)
  • Periodic work patterns

Where It's Used

Work that can block/sleep

Real-world examples:

  1. USB Device Detection

    // USB hub detects device, schedules work
    schedule_work(&hub->events);
    // Work function: Query device, load driver
    • Device enumeration
    • Configuration reads
    • Driver loading
  2. Network Interface - Link state changes

    // Cable plugged in
    schedule_work(&netdev->carrier_work);
    // Work function: Renegotiate speed, notify system
  3. ACPI - Power button handling

    // Power button pressed
    schedule_work(&power_button_work);
    // Work function: Initiate shutdown
  4. Hot-plug Events

    • PCI device insertion
    • SD card detection
    • Thunderbolt devices
  5. Error Recovery

    • Network device reset
    • Storage device retry
    • Firmware reload

Performance:

  • Interrupt: Acknowledge hardware (1 μs)
  • Workqueue: Do heavy processing (100 ms) ✓ Allowed

Companies using: Android (device hot-plug), Chrome OS (power management)


Real-World Driver Examples

Complete Drivers Using These Concepts

Intel e1000 Network Driver

  • Uses: chardev, interrupts, DMA, memory pools, locks, timers
  • ~30,000 lines of code
  • Handles: Gigabit Ethernet
  • Found in: Millions of servers

NVIDIA Graphics Driver

  • Uses: ALL concepts from tutorial
  • ~25 MB binary
  • Handles: GPU rendering, compute, AI
  • Found in: Gaming PCs, data centers, autonomous vehicles

Raspberry Pi GPIO Driver

  • Uses: Platform drivers, chardev, interrupts
  • ~2,000 lines
  • Handles: Digital I/O pins
  • Found in: Millions of hobby projects, industrial systems

Android Sensors

  • Uses: Platform drivers, interrupts, timers, workqueues
  • Handles: Accelerometer, gyroscope, compass
  • Found in: Every Android phone

Career Path Using These Examples

Junior Developer (0-2 years)

Skills from tutorial: Basics, chardev, memory Jobs: Embedded systems engineer, firmware developer Projects: Raspberry Pi drivers, Arduino communication Salary: $70k-$100k

Mid-Level Developer (2-5 years)

Skills from tutorial: + Interrupts, DMA, synchronization Jobs: Device driver engineer, kernel developer Projects: WiFi drivers, USB devices, industrial controllers Salary: $100k-$140k

Senior Developer (5+ years)

Skills from tutorial: + Advanced topics, optimization Jobs: Senior kernel engineer, technical lead Projects: NVMe drivers, GPU compute, autonomous systems Salary: $140k-$180k+

Principal/Staff Engineer (10+ years)

Skills: Master all + architecture design Jobs: Kernel maintainer, principal engineer Projects: Design new subsystems, mentor team Salary: $180k-$250k+


Industry Sectors

Where You'll Use These Skills

Sector Examples Skills Used
Automotive Tesla, Waymo, Cruise All (real-time, safety-critical)
Networking Cisco, Juniper, Arista Interrupts, DMA, locks
Storage Western Digital, Seagate Block drivers, DMA
Graphics NVIDIA, AMD, Intel Memory, DMA, synchronization
Mobile Apple, Samsung, Qualcomm Platform drivers, power mgmt
IoT Amazon (Alexa), Google (Nest) All basics, timers
Aerospace SpaceX, Boeing, Lockheed Real-time, reliability
Medical GE Healthcare, Philips Certified drivers, validation

Next Steps

After completing examples:

  1. ✅ Pick a real device you want to control
  2. 🔍 Find similar driver in kernel source
  3. 📚 Study existing driver
  4. 💻 Adapt for your hardware
  5. 🚀 Test and iterate
  6. 🌟 Share with community

Remember: Every expert started by modifying example code!


Read next: WHY-LEARN-DRIVERS.md for career paths and motivation.