Skip to content
systemcoding.dev
Kernels

Match a USB device by vendor and product with USB_DEVICE

USB_DEVICE(vendor, product) fills a usb_device_id entry that makes the USB core bind your driver to one device, and MODULE_DEVICE_TABLE lets udev autoload it.

1 min read
Summarize in AI

A USB driver tells the core which devices it wants through an ID table. USB_DEVICE() builds an entry that matches on vendor and product ID. The code below targets Linux 6.12 and a driver that binds to interfaces.

c
static const struct usb_device_id demo_ids[] = {
	{ USB_DEVICE(0x1d50, 0x6018) },
	{ USB_DEVICE_INTERFACE_CLASS(0x1d50, 0x6019, USB_CLASS_VENDOR_SPEC) },
	{ }
};
MODULE_DEVICE_TABLE(usb, demo_ids);

static int demo_probe(struct usb_interface *intf,
		      const struct usb_device_id *id)
{
	dev_info(&intf->dev, "bound to %04x:%04x\n",
		 id->idVendor, id->idProduct);
	return 0;
}

static struct usb_driver demo_driver = {
	.name = "demo",
	.probe = demo_probe,
	.id_table = demo_ids,
};
module_usb_driver(demo_driver);
MODULE_LICENSE("GPL");
Text
demo 1-2:1.0: bound to 1d50:6018

MODULE_DEVICE_TABLE(usb, ...) writes the pairs into the module so depmod can build the alias list, which is how plugging in the device loads the module. The IDs here are examples.

Two things catch people. A USB driver binds to an interface, not the whole device, so probe() receives a usb_interface; a device with three interfaces can be claimed by three drivers. And vendor-specific devices usually declare class 0xff, so a generic USB_DEVICE() entry can also match interfaces you do not want. Narrow it with the interface-class variant above. See the USB API.

Also inHardware
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.