What is libusb?
libusb wraps libusb-1.0 — the de-facto standard C library for USB device access from userspace. It lets you communicate with any USB device without writing a kernel driver, provided you have the right permissions. It is cross-platform: Linux (via usbfs), macOS (IOKit), and Windows (via WinUSB or libusb-win32).
Well-known projects built on libusb include OpenOCD (on-chip debugging), sigrok (logic analyzers & oscilloscopes), librealsense (Intel RealSense depth cameras), libfprint (fingerprint readers), and USBView.
Requires libusb-1.0-0-dev. Ubuntu/Debian: sudo apt install libusb-1.0-0-dev
USB Transfer Types
| Type | Use Case | Characteristics |
|---|---|---|
| Control | Device configuration & setup | Required by all USB devices; used for descriptor requests and class commands |
| Bulk | Large data, no timing guarantee | High throughput, error-corrected, retried on error — used by storage, printers |
| Interrupt | Small periodic data | Guaranteed latency, polled at fixed interval — used by keyboards, mice, HID |
| Isochronous | Real-time streaming | Constant bandwidth, no error retry — used by audio and video devices |
USB Concepts
Context — one library instance per application, created with NewContext.
Device — a USB device descriptor obtained by enumeration; not yet opened.
Handle — an open device; required for all I/O operations.
Interface — a logical function within a device (e.g., HID, audio, CDC); must be claimed before use.
Endpoint — a unidirectional data pipe. IN = device→host (bit 7 set, e.g. $81). OUT = host→device.
Types
Opaque Handles
Close(H).TUSBSpeed
| Value | Constant | Speed | USB Generation |
|---|---|---|---|
| 0 | usUnknown | Unknown | Not detected |
| 1 | usLow | 1.5 Mbps | USB 1.0 |
| 2 | usFull | 12 Mbps | USB 1.1 |
| 3 | usHigh | 480 Mbps | USB 2.0 |
| 4 | usSuper | 5 Gbps | USB 3.0 |
| 5 | usSuperPlus | 10 Gbps | USB 3.1 |
TUSBClass
| Constant | Code | Device Class |
|---|---|---|
| ucAudio | $01 | Audio devices |
| ucHID | $03 | Human Interface Device (keyboards, mice, gamepads) |
| ucPrinter | $07 | Printers |
| ucStorage | $08 | Mass storage (USB drives, card readers) |
| ucHub | $09 | USB hubs |
| ucCDC | $0A | Communications Device Class (serial adapters) |
| ucVideo | $0E | Video capture devices |
| ucWireless | $E0 | Wireless controllers (Bluetooth, Wi-Fi dongles) |
| ucVendor | $FF | Vendor-specific protocol |
TUSBDeviceInfo
VendorID, ProductID : Integer { VID:PID in hex, e.g. $045E:$028E }
DeviceClass : Integer { see TUSBClass }
Manufacturer, Product, SerialNumber : string
Speed : TUSBSpeed
NumConfigs : Integer { usually 1 }
TUSBEndpoint
IsIN : Boolean { True = device→host }
TransferType : Integer { 0=control 1=isoc 2=bulk 3=interrupt }
MaxPacket : Integer { max packet size in bytes }
Interval : Integer { polling interval for interrupt/isoc endpoints }
TUSBHotplugEvent
type TUSBHotplugEvent = (
heArrived = 1, { device plugged in }
heLeft = 2 { device removed }
);
API Reference
Context
Enumeration
GetDeviceInfo on each.Open / Close
FindDevice + OpenDevice.Interface
Synchronous Transfers
RequestType, Request, Value, Index follow the USB spec (bmRequestType, bRequest, wValue, wIndex). TimeoutMs = 0 means no timeout. Returns bytes transferred.MaxBytes from a bulk IN endpoint. Returns raw bytes as string.MaxBytes from an interrupt IN endpoint (e.g., HID reports). Returns received bytes as string.Descriptors
IsIN, TransferType, and MaxPacket to find the right endpoint for your transfer.Hotplug
VendorID and ProductID may be 0 to match any device. CallbackID identifies the event for routing in an agent handler.on tick handler.Code Example — Enumerate & Read HID
uses libusb;
var
Ctx: TUSBContext;
H: TUSBHandle;
Data: string;
Info: TUSBDeviceInfo;
begin
Ctx := NewContext;
SetDebug(Ctx, 1);
H := Open(Ctx, $046D, $C52B); { Logitech Unifying Receiver }
if H = 0 then begin
WriteLn('Device not found');
Exit;
end;
Info := GetDeviceInfo(FindDevice(Ctx, $046D, $C52B));
WriteLn('Product: ', Info.Product);
WriteLn('Speed: ', Ord(Info.Speed));
DetachKernelDriver(H, 0);
ClaimInterface(H, 0);
Data := InterruptRead(H, $81, 64, 1000); { EP 0x81 = IN endpoint 1 }
WriteLn('Received ', Length(Data), ' bytes');
ReleaseInterface(H, 0);
Close(H);
FreeContext(Ctx);
end.
By default, USB device nodes are owned by root. Add a udev rule to allow non-root access for your specific device:
SUBSYSTEM=="usb", ATTR{idVendor}=="046d", ATTR{idProduct}=="c52b", MODE="0666"
Save to /etc/udev/rules.d/99-libusb.rules and run sudo udevadm control --reload-rules.
Package Info
Install
ppm install libusbRequires system library:
libusb-1.0-0-devUbuntu/Debian:
sudo apt install libusb-1.0-0-dev
Notable Users
Functions
- NewContext / FreeContext
- SetDebug
- ListDevices
- GetDeviceInfo
- FindDevice / FindDevices
- GetSpeed
- Open / OpenDevice
- Close
- ClaimInterface
- ReleaseInterface
- DetachKernelDriver
- SetConfiguration
- SetAlternateSetting
- ControlTransfer
- BulkTransfer / BulkRead
- InterruptTransfer
- InterruptRead
- GetStringDescriptor
- GetDeviceDescriptor
- GetEndpoints
- RegisterHotplug
- HandleEvents
- LibUSBVersion