Skip to main content

TECH VEDA

Corporate on-site training - Submit proposal Pick your modulesSharpen your kernel skills: deep dives, drivers, Yocto, CVEs, careers — updated daily. Read the blog →
System Design

Kernel Driver vs Userspace Driver: Where Should Your Device Code Run?

Kernel driver vs userspace driver on embedded Linux: compare spidev, i2c-dev, UIO and GPIO character devices with a kernel driver, and when each is right.

Kernel Driver vs Userspace Driver: Where Should Your Device Code Run?

For a product that ships in volume, put the device code in a kernel driver when the device fits an existing kernel subsystem, raises interrupts that need prompt service, or must take part in suspend and resume. Use a userspace driver, through spidev, i2c-dev, UIO or the GPIO character device, when the device has a private protocol that fits no subsystem, when that protocol changes often, or when the hardware is a prototype or a one-off machine. A kernel driver gives standard interfaces and kernel services but needs kernel skills and kernel-level maintenance. A userspace driver is easier to write and update, but your application must then handle interrupts, power management and sharing.

Every board has at least one peripheral that the SoC vendor did not provide software for: a sensor, a companion microcontroller, an FPGA register block or a few control lines. The team must decide where the code for that peripheral runs. The choice of kernel driver vs userspace driver is often made by habit, according to the skills the team already has.

The context

Linux allows both approaches. A kernel driver binds to the device and presents it through a subsystem. For several buses the kernel also provides generic drivers that pass raw access to a process, so that the device logic can be an ordinary program. Five forces shape the choice:

  • Fit with a subsystem. For an ADC, a temperature sensor, a key, an LED or a regulator, the kernel already has a subsystem with a defined userspace interface.
  • Interrupts and timing. Where the interrupt-handling code runs decides how soon the device is serviced.
  • Power management. A device that enters a low-power state needs code that runs during suspend and resume.
  • Rate of change. A protocol that changes every week costs more to maintain in the kernel than a fixed one.
  • Team skills and licensing. Kernel code requires kernel knowledge and is covered by the kernel licence.

The options

Option 1: a kernel driver

The driver is written against a bus API (SPI, I2C, platform) and registers with a subsystem such as IIO, hwmon, input or LED. Applications use the interface of that subsystem and do not need to know which chip is fitted.

  • Pros: interrupt handlers run in the kernel. The driver can use the clock, regulator, reset and runtime PM frameworks, and it receives suspend and resume callbacks. Several processes can share the device through one interface.
  • Cons: a defect can stop the whole system. Development needs kernel skills. Unless the code is accepted upstream, it must be updated for kernel API changes at each kernel upgrade.

Option 2: a userspace driver on a generic kernel interface

The kernel provides a small, generic driver, and the device logic runs in a process. There are four common interfaces:

  • spidev (CONFIG_SPI_SPIDEV) exposes an SPI device as /dev/spidevB.C. read() and write() are half-duplex, and the SPI_IOC_MESSAGE ioctl gives full-duplex and multi-part transfers. The kernel documentation calls the interface limited and names two of the reasons to use it: prototyping, and simple protocols to a microcontroller that change often. All I/O is synchronous.
  • i2c-dev (CONFIG_I2C_CHARDEV) exposes each I2C adapter as /dev/i2c-N. A process selects a device address with the I2C_SLAVE ioctl and uses the I2C_RDWR ioctl for combined transactions.
  • UIO is intended for memory-mapped devices. The process maps the registers with mmap() on /dev/uioX, and a blocking read() returns when an interrupt occurs, with the total interrupt count as its value. For a platform device with a dedicated interrupt line, the generic uio_pdrv_genirq driver (CONFIG_UIO_PDRV_GENIRQ) means that no device-specific kernel code is needed: its handler disables the interrupt line, and the process enables it again by writing to the device file. On a device tree system, this driver binds only to the compatible string given in its of_id module parameter. UIO also allows a small device-specific kernel module with the main logic in userspace.
  • The GPIO character device (/dev/gpiochipN), normally used through libgpiod, gives a process control of individual lines. We covered it in Control GPIO with libgpiod.
  • Pros: a defect ends one process and does not stop the kernel. The code can use any language, floating point and any library. A change to the device logic is an application update and needs no kernel rebuild.
  • Cons: the process sees an interrupt only after the scheduler runs it. Standard tools and other programs cannot use the device, and no subsystem arbitrates access. The process receives no suspend or resume callback for the device.

Binding spidev is a deliberate step. The documentation states that a device tree node with compatible = "spidev" is no longer supported and that the driver fails to probe without a real device name. A device that is not in the driver’s tables can be bound at run time:

raghu@techveda.org:~$ echo spidev | sudo tee /sys/bus/spi/devices/spi0.0/driver_override
spidev
raghu@techveda.org:~$ echo spi0.0 | sudo tee /sys/bus/spi/drivers/spidev/bind
spi0.0

The decision: kernel driver vs userspace driver

For a product manufactured in volume, the default should be a kernel driver. Take these steps in order:

  1. Check whether a mainline driver already exists. The kernel’s list of subsystem drivers that use GPIO includes leds-gpio, gpio-keys, gpio-regulator and gpio-wdt, and the documentation advises using them and not driving the lines from userspace.
  2. If the device fits a subsystem, write a kernel driver for that subsystem.
  3. If the device needs prompt interrupt service or power management, keep at least that part in the kernel. The UIO documentation gives the rule for a split design: any action the hardware needs after every interrupt must be done in the kernel module, because the userspace program can terminate at any time.

The kernel’s GPIO guidance supports this default. It states that the userspace ABI is intended for one-off deployments such as prototypes and factory lines, and that uniform products should not use GPIO from userspace.

A userspace driver is the right choice in these conditions:

  • The device has a private protocol and fits no subsystem. A companion microcontroller on SPI with a message format defined by your own team is the case the spidev documentation names.
  • The device is a register block in an FPGA whose function is specific to your product, has no hard interrupt deadline, and is used by one process.
  • The protocol is still changing, the code is a bring-up or factory test tool, or the equipment is built in small numbers for operators who know the hardware.

A userspace driver is not suitable for a device that an existing subsystem handles. The UIO documentation excludes devices already handled well by subsystems such as networking, serial or USB.

Consequences

The kernel driver vs userspace driver decision sets costs that remain for the life of the product.

If you choose a kernel driver:

  • The team needs engineers who can write and debug kernel code, and reviews must be stricter because a defect affects the whole system.
  • The driver has a maintenance cost at every kernel upgrade. Where the code lives changes that cost, which we discussed in In-Tree vs Out-of-Tree Driver.
  • The kernel is licensed under GPL-2.0 only, and a source file that is part of it must carry a licence compatible with GPL-2.0. The licensing rules state that the system call interface does not extend the GPL requirements to software that uses it. This summary is not legal advice.

If you choose a userspace driver:

  • One process must own the device, so other programs need an IPC interface to it that you design and maintain.
  • The process must leave the hardware in a safe state when it stops and restore it when it restarts. With UIO, the device is not serviced while the process is not running.
  • Device nodes need correct ownership and permissions, normally set by udev or mdev rules. A process with access to /dev/i2c-N can address every device on that adapter, which matters when a PMIC or an EEPROM is on the same bus.
  • Your own software must coordinate system suspend, because no kernel driver knows the state of the device.

Writing kernel drivers for I2C and SPI devices, including interrupt handling, is covered in our Linux Device Drivers training.

Key takeaways

  • Decide kernel driver vs userspace driver by subsystem fit, interrupt timing and power management before team preference.
  • spidev, i2c-dev, UIO and the GPIO character device suit private protocols, prototypes, test tools and one-off equipment.
  • A userspace driver moves ownership, permissions, restart behaviour and suspend handling from the kernel to your application.
Was this worth your time?

Frequently asked questions

Can I put compatible = “spidev” in my device tree?
No. The spidev documentation states that this is no longer supported and that the driver fails to probe without a real SPI device name. Use the compatible string of the actual device, or bind spidev at run time through driver_override.

Can a userspace driver handle interrupts?
Yes, with UIO. A blocking read() on /dev/uioX returns when an interrupt occurs and gives the total interrupt count. The process runs only when the scheduler selects it, so any action the hardware needs after every interrupt must be in the kernel part.

Is controlling GPIO from userspace acceptable in a product?
The kernel documentation states that the GPIO userspace ABI is intended for one-off deployments such as prototypes and factory lines, and that uniform products should use kernel drivers. Where userspace access is appropriate, it recommends libgpiod.

Further reading

RB
Raghu Bharadwaj

Founder, TECH VEDA — 20+ years teaching the Linux kernel, device drivers and embedded systems.

Follow on LinkedIn

Get new posts by email

Kernel, embedded Linux and AI-era engineering — a few sharp reads a month. No spam.

We email occasionally and never share your address.