Writing Linux Device Drivers

What happens between pressing a physical button and seeing a program respond? This series begins with a small C kernel module, then follows that question through character devices, blocking reads, poll(), GPIO interrupts, and Device Tree. You’ll work with I²C, SPI, and USB peripherals, observe how Linux binds a driver to a device, and test what happens when initialization fails or hardware is removed. The final step is to package a driver in a Yocto image and verify it on real hardware after a cold boot.

From Three Paths to One Loop

繁體中文:學習感言

For a long time I studied these as three separate things.

System architecture showed me how a machine is organized: the buses, the memory, the boot order, and who waits on whom. Programming let me write something that actually runs, from a userspace program to a module that loads into the kernel. Hardware design turned a schematic and a PCB into a board I could wire up and power on.

Each one matters on its own. Studied apart, though, something in the middle was always missing. Architecture knew how the system should be shaped. Software knew how to run. Hardware knew which pin was which. The path from a button to an application, from a sensor to /dev, and from all of that into an image that boots on real hardware, was usually a layer someone else had already written.

That layer is the device driver.

These labs start with a first kernel module in QEMU. After insmod, dmesg shows hello_lab: loaded. It is only a greeting, but it is the first time my own code is standing inside the kernel. Then comes /dev, blocking reads, wait queues, and poll(). When a physical button is wired to a Raspberry Pi 3 GPIO, and a falling edge wakes a sleeping read(), the hardware, the kernel, and the application are finally on the same path.

The later labs widen that path: SSD1306 and DS3231 on I²C, ADXL345 on SPI, fault injection and rebinding across the driver lifecycle, USB hotplug and a V4L2 camera, then Bluetooth GATT, A2DP, and HFP. The last lab packages an external module into a Yocto image. A cold boot, the button, the OLED, and the RTC after a power loss are checked together, as one machine.

For me this is not only fourteen chapters finished. It is system architecture, programming, and hardware design closing into one loop.

The same path shows up in other work. AIY-Box-Rebirth uses a custom Yocto layer to turn a Raspberry Pi 3 and a Google AIY Voice Kit into a multi-function AI Box. AI-Now Linux is a system grown from source. On 26 November 2026, v0.1 Genesis booted on its own in QEMU/KVM. On 28 November it came up from the internal disk of an ASUS Eee PC 1005PE, where the display, touchpad, Wi-Fi, SSH, and MP3 playback through the built-in speaker were all confirmed.

From a module’s loaded line to a machine’s boot screen, the work is not inspiration. It is getting the architecture clear, the code right, and the hardware connected, one time after another. Studied apart, they are three subjects. Taken together, they are one job.

There is still road ahead. Raspberry Pi and FPGA/Zynq targets remain planned. But I now know how this loop closes.

John Tsai
November 2026

Start with a kernel module in QEMU, then follow hardware events through device drivers and into userspace. The labs finish with driver recovery tests and deployment in a Yocto image.

Lab environment

Task Environment
Edit source Linux development host with VS Code or your preferred editor
Build, load, and unload practice modules QEMU Linux guest with matching kernel headers
Verify physical peripherals Raspberry Pi 3 Model B and the hardware specified in each lab

Build external modules for the kernel that will load them. Use uname -r on the target to identify the running release.

Contents

Kernel fundamentals

On-board peripherals and driver lifecycle

USB and Bluetooth

Deployment

System integration check

  1. Cold boot a newly built Yocto image and verify that the board configuration and required modules load.
  2. Press the physical button. Confirm that the application receives the event and the OLED reflects the application state.
  3. Power off and disconnect network access. After booting again, verify RTC time retention and system clock restoration separately.

Verify the examples against your target kernel and hardware.

Source on GitHub