Fundamentals 12 min read

How Linux Automatically Detects and Loads USB Devices: A Deep Dive

This article explains the Linux kernel's step‑by‑step process for recognizing, loading drivers, registering, and accessing USB devices, and provides clear C and kernel‑module code examples that illustrate each stage of the USB device handling workflow.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Linux Automatically Detects and Loads USB Devices: A Deep Dive

Introduction

This article explains how Linux automatically recognizes any USB device without requiring manual driver installation. It details the kernel’s detection, driver matching, device node creation, and user‑space access processes.

USB Device Recognition Process

2.1 USB controller detects insertion

The USB controller senses a voltage change when a device is plugged in and generates an interrupt, notifying the kernel of a new device.

2.2 Kernel reads the device descriptor and matches a driver

The interrupt invokes the usbcore module, which reads the device descriptor (vendor ID, product ID, class code, etc.) and searches for a matching driver. If a specific driver exists it is loaded; otherwise a generic driver is used.

2.3 Driver registers with the USB core

The loaded driver registers the device classes it supports (e.g., storage, input) with the USB core, enabling future matching.

2.4 udev creates a device node

The udev daemon creates a character device node under /dev (e.g., /dev/sdb) and sets appropriate permissions.

2.5 Driver initializes the device

The driver configures transfer parameters, allocates buffers, and reports that the device is ready.

2.6 User‑space accesses the device

Applications open the device node and use standard system calls such as read and write to communicate with the hardware.

Code Demonstrations

3.1 Detecting device insertion (libusb user‑space example)

#include <stdio.h>
#include <libusb.h>

int main() {
    libusb_device **devs;
    libusb_context *ctx = NULL;
    int r = libusb_init(&ctx);
    if (r < 0) {
        printf("Failed to initialize libusb
");
        return 1;
    }
    ssize_t cnt = libusb_get_device_list(ctx, &devs);
    if (cnt < 0) {
        printf("Failed to get device list
");
        return 1;
    }
    for (int i = 0; i < cnt; i++) {
        libusb_device *dev = devs[i];
        struct libusb_device_descriptor desc;
        r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0) {
            printf("Failed to get device descriptor
");
            continue;
        }
        printf("Vendor ID: 0x%04x, Product ID: 0x%04x
", desc.idVendor, desc.idProduct);
    }
    libusb_free_device_list(devs, 1);
    libusb_exit(ctx);
    return 0;
}

This program enumerates all connected USB devices and prints their vendor and product IDs.

3.2 Simple kernel driver skeleton (storage device example)

#include <linux/module.h>
#include <linux/usb.h>

static struct usb_device_id storage_devices[] = {
    { USB_DEVICE(0xabcd, 0x1234) },
    { USB_DEVICE(0xffff, 0xffff) },
    {}
};
MODULE_DEVICE_TABLE(usb, storage_devices);

static int storage_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
    // Initialize device and register it
    return 0;
}

static void storage_disconnect(struct usb_interface *interface)
{
    // Release resources
}

static struct usb_driver storage_driver = {
    .name = "usb-storage",
    .probe = storage_probe,
    .disconnect = storage_disconnect,
    .id_table = storage_devices,
};

module_usb_driver(storage_driver);

The driver matches USB storage devices by vendor/product ID and provides probe and disconnect callbacks.

3.3 Retrieving the device descriptor inside the probe

static int storage_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
    struct usb_device *dev = interface_to_usbdev(interface);
    struct usb_device_descriptor desc;
    int r = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &desc, sizeof(desc));
    if (r < 0) {
        printk(KERN_ERR "Failed to get device descriptor
");
        return r;
    }
    printk(KERN_INFO "USB storage detected: Vendor ID=0x%04x, Product ID=0x%04x
",
           desc.idVendor, desc.idProduct);
    // Further initialization omitted for brevity
    return 0;
}

This snippet shows how a kernel driver obtains the device descriptor and logs the IDs.

3.4 Accessing the device from user space

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd = open("/dev/sdb", O_RDONLY);
    if (fd < 0) {
        printf("Failed to open device
");
        return 1;
    }
    char buf[1024];
    ssize_t n = read(fd, buf, sizeof(buf));
    if (n < 0) {
        printf("Failed to read device
");
        close(fd);
        return 1;
    }
    close(fd);
    return 0;
}

The program opens the device node created by udev and reads raw data using standard POSIX I/O calls.

Conclusion

Linux’s USB handling consists of four stages: controller detection, driver matching/loading, device node creation/registration, and user‑space access. Understanding these stages and the associated kernel APIs ( usbcore, usb_get_descriptor, module_usb_driver, etc.) enables developers to write, debug, and extend USB drivers effectively.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxlibusbUSBDevice Driversudev
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.