libusb vs a Kernel USB Driver: Where Should Your Code Live
Decide between libusb in user space and a Linux 6.12 kernel USB driver by latency, device class, security, deployment and what else needs to see the device.

A team gets a new USB device and a vendor protocol document, and the first design question arrives before any code does: write a kernel driver, or talk to the device from a normal program with libusb? Most of the time the answer is libusb, and the cases where it is not are specific enough to list. This post compares the two on Linux 6.12 and libusb 1.0.27, gives a decision table, and shows the same "read a firmware version" request both ways so you can see the real difference in code size and in what each approach costs you later.
The user-space interface is documented in the libusb API reference, and the kernel side is described in the USB driver guide.
What libusb actually is
libusb is a user-space library. It opens the device through usbfs, the character devices under /dev/bus/usb/<bus>/<device>, and submits control, bulk, interrupt and isochronous transfers through ioctls on that file. The kernel's USB core still enumerates the device, still reads the descriptors and still owns the host controller. libusb only borrows a single interface for your process, so a kernel driver already bound to that interface must be detached first (libusb_detach_kernel_driver()), or the claim fails with LIBUSB_ERROR_BUSY.
A kernel driver, by contrast, is code that runs in the kernel and is offered interfaces by the USB core through its id_table. It submits URBs directly, and it can present the device to the rest of the system through a subsystem: a block device, a network interface, an input device, a sound card, a tty.
So the question is really this: does anything other than your own program need to use the device through a standard interface?
The same request, both ways
Read a two-byte firmware version with a vendor control request. In user space with libusb:
#include <stdio.h>
#include <stdint.h>
#include <libusb-1.0/libusb.h>
int main(void)
{
libusb_context *ctx;
libusb_device_handle *h;
uint8_t buf[2];
int n;
if (libusb_init(&ctx) != 0)
return 1;
h = libusb_open_device_with_vid_pid(ctx, 0x1234, 0x5678);
if (!h) {
fprintf(stderr, "device not found\n");
libusb_exit(ctx);
return 1;
}
n = libusb_control_transfer(h,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_DEVICE,
0x01, 0, 0, buf, sizeof(buf), 500);
if (n == (int)sizeof(buf))
printf("firmware %u.%u\n", buf[1], buf[0]);
else
fprintf(stderr, "transfer failed: %s\n", libusb_error_name(n));
libusb_close(h);
libusb_exit(ctx);
return n == (int)sizeof(buf) ? 0 : 1;
}firmware 1.3The version bytes arrive little-endian, so a reply of 03 01 prints as 1.3. The program needs no root if a udev rule gives your user access to the device node, and it builds with gcc version_user.c -lusb-1.0. Compare the kernel version of the same request, which also needs the whole module around it, shown in the control transfer post. The kernel function is about the same length as the user-space one. What the kernel route adds is everything around it: the module, the id_table, the probe and disconnect lifecycle, the build against kernel headers, and the packaging per kernel version.
What the kernel driver gives you that libusb cannot
A standard device node or interface. If the device is a serial adapter and you want /dev/ttyUSB0 that every terminal program understands, or a network adapter that shows up in ip link, or a disk, a kernel driver is the only way to get there. A libusb program can emulate some of these (for example through a TUN device or FUSE), but the result is slower and awkward.
Interrupt-driven latency inside the kernel. A completion handler runs in the host controller's interrupt context in the kernel, with no scheduler hop to a user-space thread. For a control loop that must react within a few hundred microseconds of a bus event, or for isochronous audio that feeds a kernel sound path, that matters. For most devices it does not, because USB itself polls on millisecond frames.
Access before user space exists. A device that must work at boot, such as a keyboard for the initramfs prompt or a root disk, is handled by a driver in the kernel or the initramfs, not by a program started later.
Power management. A driver can implement runtime suspend and wake-up and cooperate with the USB core's autosuspend. libusb can claim an interface and the kernel will not suspend a device that has an open user-space handle in the same way.
What libusb gives you that a kernel driver cannot
A crash does not take the machine with it. A bug in a kernel driver is a kernel oops, a corrupted memory region or a hang. The same bug in a libusb program is a segmentation fault in one process. That is the strongest argument for user space, and it is the reason vendors of one-off devices, test equipment and programmers almost always ship libusb tools.
A normal development loop. You compile, run, attach a debugger, use valgrind, write unit tests and ship to any distribution without matching a kernel version. A kernel module must be rebuilt for each kernel, or merged upstream, which is a real commitment of maintenance.
Portability. The same libusb code runs on Linux, macOS, Windows and the BSDs with a small amount of conditional code. A kernel driver runs on one kernel.
Any language. Bindings exist for Python (pyusb), Rust (rusb), Go and others, so the protocol can live in the language the rest of your tool uses.
The decision table
| Question | Points to libusb | Points to a kernel driver |
|---|---|---|
| Does another program need the device as a disk, tty, network or input device? | No | Yes |
| Must it work before user space starts? | No | Yes |
| Is it one tool talking one vendor protocol? | Yes | No |
| Do you need to ship on several kernel versions or operating systems? | Yes | No |
| Is crash isolation important? | Yes | No |
| Is the latency requirement under about a millisecond? | No | Possibly |
| Will the driver be upstreamed to the kernel? | No | Yes |
| Is there a class driver already? | Check first | Use it |
The last row is the one people skip. If the device speaks a standard class (HID, mass storage, CDC ACM, audio, video), the kernel already has a driver, and writing your own is usually wrong. Use the existing one, and take libusb only for the vendor-specific part that class driver does not cover.
The middle path
Two mechanisms give you some of each. The first is a thin kernel driver for what must be in the kernel, with a small user-space daemon for the protocol. The second is the generic uinput interface: a libusb program reads the device and creates a virtual input device in the kernel, so the desktop sees a normal keyboard or gamepad, and you still keep the safety and tooling of user space. For HID devices, the hidraw node also gives a program raw reports without detaching the kernel's HID driver at all.
If you choose libusb, there is a practical step that matters on every distribution: give your program permission without running it as root. A udev rule matched on the vendor and product IDs does that:
SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="5678", MODE="0660", TAG+="uaccess"The uaccess tag grants the user logged in at the seat access to the device node, which is the right behavior for a desktop tool. For a headless service, use GROUP="plugdev" and put the service account in that group.


