</>EmbeddedInterview

How do you write a user-space driver, and when is it a good idea (UIO, VFIO, spidev, libgpiod)?

MediumLinux Kernel & Device Drivers

Short answer: Some devices can be driven from user space using generic kernel interfaces: UIO maps device registers into a process and delivers interrupts via read() on /dev/uioN; VFIO does the same with IOMMU protection and full DMA support (used by DPDK, SPDK and QEMU device pass-through); spidev, i2c-dev and libgpiod (/dev/gpiochipN character device) expose buses and GPIOs. User-space drivers are easier to develop and debug and avoid GPL constraints, but they have higher latency for interrupts, cannot integrate with kernel subsystems (network stack, filesystems, input), and need care with security.

Interface Gives Typical use
UIO (uio_pdrv_genirq) mmap of registers, interrupt count via read, re-enable via write FPGA IP cores, simple custom peripherals
VFIO (vfio-pci, vfio-platform) registers, interrupts via eventfd, IOMMU-protected DMA DPDK/SPDK high-performance NIC/NVMe drivers, VM pass-through
spidev / i2c-dev raw SPI/I2C transfers prototyping, simple sensors
GPIO character device (libgpiod) line requests, events with timestamps buttons, enables, bit-banging (the old sysfs GPIO interface is deprecated)

UIO interrupt loop

int fd = open("/dev/uio0", O_RDWR);
volatile uint32_t *regs = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
uint32_t enable = 1, count;
for (;;) {
    write(fd, &enable, sizeof enable);         /* (re)enable the interrupt */
    read(fd, &count, sizeof count);            /* blocks until the next interrupt */
    handle(regs);
}

When kernel drivers are the right choice: the device must integrate with a subsystem (Ethernet, storage, display, input, audio), needs low interrupt latency, must be shared safely among processes, must work at boot before user space, or must protect the system from misbehaving software (DMA without an IOMMU).

When user space is fine: prototypes, low-rate devices, high-throughput polling designs (DPDK), and proprietary code that cannot be GPL.