Write a Linux USB Driver With usb_driver and an id_table
Build the smallest working Linux 6.12 USB driver: an id_table, probe and disconnect callbacks, per-interface state, and the module_usb_driver registration.

Every Linux USB driver, from a flash drive to a webcam, has the same skeleton: a table that says which interfaces it wants, a probe function that runs when one appears, a disconnect function that runs when it goes away, and one structure that ties them together. This post writes that skeleton for Linux 6.12, explains each piece and the reference counting around it, and shows the build line and the kernel log you should expect. Later posts in the cluster add real transfers to it, such as bulk and control requests.
The reference text is the kernel's writing USB device drivers guide. The structure definitions are in include/linux/usb.h in the kernel tree.
The id_table decides who gets probed
The USB core calls your probe only for interfaces that match an entry in the driver's struct usb_device_id table. The table ends with an empty entry. Three macros build the common entries:
| Macro | Matches |
|---|---|
USB_DEVICE(vid, pid) | One vendor and product pair |
USB_DEVICE_INTERFACE_CLASS(vid, pid, cl) | A vendor and product pair, but only interfaces of one class |
USB_INTERFACE_INFO(cl, sc, pr) | Any device, by interface class, subclass and protocol |
A driver for one specific gadget uses the first. A class driver uses the third. Pick the narrowest match that is true, since a table entry that is too wide makes your probe run for interfaces owned by other drivers, and the kernel offers the interface to drivers in no guaranteed order.
MODULE_DEVICE_TABLE(usb, name) exports the table into the module's metadata. depmod turns it into the alias file that udev reads, which is how plugging the device loads the module without anyone running modprobe.
The complete skeleton
This driver binds to a made-up device with vendor ID 0x1234 and product ID 0x5678. It keeps a small per-interface structure and releases it on disconnect:
// SPDX-License-Identifier: GPL-2.0
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#define SAMPLE_VID 0x1234
#define SAMPLE_PID 0x5678
static const struct usb_device_id sample_table[] = {
{ USB_DEVICE(SAMPLE_VID, SAMPLE_PID) },
{ }
};
MODULE_DEVICE_TABLE(usb, sample_table);
struct sample {
struct usb_device *udev;
struct usb_interface *intf;
};
static int sample_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct sample *s;
s = kzalloc(sizeof(*s), GFP_KERNEL);
if (!s)
return -ENOMEM;
s->udev = usb_get_dev(interface_to_usbdev(intf));
s->intf = usb_get_intf(intf);
usb_set_intfdata(intf, s);
dev_info(&intf->dev, "bound to %04x:%04x interface %u\n",
id->idVendor, id->idProduct,
intf->cur_altsetting->desc.bInterfaceNumber);
return 0;
}
static void sample_disconnect(struct usb_interface *intf)
{
struct sample *s = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
usb_put_intf(s->intf);
usb_put_dev(s->udev);
kfree(s);
dev_info(&intf->dev, "disconnected\n");
}
static struct usb_driver sample_driver = {
.name = "sample_usb",
.probe = sample_probe,
.disconnect = sample_disconnect,
.id_table = sample_table,
};
module_usb_driver(sample_driver);
MODULE_DESCRIPTION("Minimal USB driver skeleton");
MODULE_LICENSE("GPL");module_usb_driver() expands to the usual module_init and module_exit pair that call usb_register() and usb_deregister(). You never write those two functions for a driver that only registers one structure.
Why probe takes references
probe gets a pointer to the interface, and the core owns it. If you store that pointer and use it after disconnect returns, you read freed memory. Two calls prevent that:
usb_get_dev() takes a reference on the struct usb_device, which you obtain from the interface with interface_to_usbdev(). usb_get_intf() takes a reference on the interface. Each reference is dropped with the matching usb_put_dev() and usb_put_intf(). A driver that submits URBs or runs a work item may still be using the structure after disconnect begins, so those references are what keep the memory alive until the last user lets go.
usb_set_intfdata() and usb_get_intfdata() attach your private structure to the interface. Setting it back to NULL in disconnect before freeing is a convention that pays for itself: any other callback that races with disconnect and reads the pointer sees NULL and can return early, instead of dereferencing freed memory.
probe returns zero to claim the interface and a negative errno to decline. Returning -ENODEV or -ENXIO for an interface you do not want is normal and silent. Any other error prints a message in the kernel log, so reserve those for real failures.
Build the module
An out-of-tree module needs the headers for the running kernel and a two-line Kbuild file:
obj-m += sample_usb.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleanRun make, and then load the module with sudo insmod sample_usb.ko. The kernel log shows registration. When a device with the matching IDs is present, the probe message follows:
usbcore: registered new interface driver sample_usb
sample_usb 1-2:1.0: bound to 1234:5678 interface 0Unplugging the device prints the last line:
sample_usb 1-2:1.0: disconnectedThe 1-2:1.0 prefix is the interface name from sysfs, and dev_info adds it for you because you passed &intf->dev. Confirm the binding with readlink /sys/bus/usb/devices/1-2:1.0/driver, which should end in sample_usb.
What happens if another driver already owns it
The core binds one driver to an interface. If a class driver such as usb-storage already claimed the interface, yours is never probed, and loading it prints only the registration line. This is the most common reason a new driver "does nothing". Detach the other driver as shown in the sysfs post, or pick a device the kernel does not already handle.
You can also ask for the interface explicitly from probe, which is what a driver does when it needs a second interface of the same device:
static int sample_claim_sibling(struct sample *s, int ifnum)
{
struct usb_interface *other;
other = usb_ifnum_to_if(s->udev, ifnum);
if (!other)
return -ENODEV;
return usb_driver_claim_interface(&sample_driver, other, s);
}usb_ifnum_to_if() finds the interface by number in the active configuration, and usb_driver_claim_interface() returns -EBUSY when another driver holds it. Interfaces claimed this way must be released with usb_driver_release_interface() in disconnect, which is easy to forget.
Beyond the skeleton
Real drivers add three things this skeleton leaves out.
An entry in .id_table can carry driver_info, a number or pointer that probe reads from id->driver_info, so one driver can serve several chip revisions with different quirks. You write it with the long form { USB_DEVICE(vid, pid), .driver_info = FLAG }.
Suspend support adds .suspend, .resume and .reset_resume callbacks and sets .supports_autosuspend = 1. The core suspends an idle device on its own after a timeout once the driver says it can cope, and the driver must stop all URBs in suspend.
A device node for user space comes from usb_register_dev() with a struct usb_class_driver, as in the kernel's usb-skeleton.c. It gives you a /dev/ entry and a file_operations table. A driver that exposes its function through an existing subsystem (input, network, tty) does not need that, and should not create one.
Testing the unbind path
A driver that works on plug and crashes on unplug is a common outcome of a first attempt, so exercise the other direction on purpose. Write the interface name to the driver's unbind file in sysfs and watch the log: the disconnected line must appear, and a following rmmod sample_usb must succeed without warnings. Then write the name to bind and confirm probe runs again. If the second probe fails with -ENOMEM or the module refuses to unload, a reference or an allocation was leaked in the first pass, and the leak is easier to find now than after the driver grows.


