Manual browser: uhub(4)

USB(4) Kernel Interfaces Manual USB(4)


usbUniversal Serial Bus driver


ehci* at cardbus? function ?
ehci* at pci? dev ? function ?
ohci* at cardbus? function ?
ohci* at pci? dev ? function ?
slhci* at isa? port ? irq ?
slhci* at pcmcia? function ?
uhci* at cardbus? function ?
uhci* at pci? dev ? function ?
usb* at ehci? flags X
usb* at ohci? flags X
usb* at uhci? flags X
usb* at slhci? flags X
uhub* at usb?
uhub* at uhub? port ? configuration ? interface ? vendor ? product ? release ?
XX* at uhub? port ? configuration ? interface ? vendor ? product ? release ?


#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>


NetBSD provides machine-independent bus support and drivers for USB devices.

The NetBSD usb driver has three layers (like scsi(4) and pcmcia(4)): the controller, the bus, and the device layer. The controller attaches to a physical bus (like pci(4)). The USB bus attaches to the controller and the root hub attaches to the bus. Further devices, which may include further hubs, attach to other hubs. The attachment forms the same tree structure as the physical USB device tree. For each USB device there may be additional drivers attached to it.

The uhub device controls USB hubs and must always be present since there is at least a root hub in any USB system.

The flags argument to the usb device affects the order in which the device detection happens during cold boot. Normally, only the USB host controller and the usb device are detected during the autoconfiguration when the machine is booted. The rest of the devices are detected once the system becomes functional and the kernel thread for the usb device is started. Sometimes it is desirable to have a device detected early in the boot process, e.g., the console keyboard. To achieve this use a flags value of 1.

NetBSD supports the following machine-independent USB drivers:

Storage devices

USB Mass Storage Devices, e.g., external disk drives

Wired network interfaces

ADMtek AN986/ADM8511 Pegasus family 10/100 USB Ethernet device
ASIX Electronics AX88172/AX88178/AX88772 10/100/Gigabit USB Ethernet device
ASIX Electronics AX88178a/AX88179 10/100/Gigabit USB Ethernet device
USB Communication Device Class Ethernet device
CATC USB-EL1201A USB Ethernet device
Kawasaki LSI KL5KUSB101B USB Ethernet device
Davicom DM9601 10/100 USB Ethernet device
Realtek RTL8150L 10/100 USB Ethernet device
USB Remote NDIS Ethernet device

Wireless network interfaces

Atmel AT76C50x IEEE 802.11b wireless network device
Ralink Technology USB IEEE 802.11b/g wireless network device
Ralink Technology USB IEEE 802.11a/b/g wireless network device
Ralink Technology USB IEEE 802.11a/b/g/n wireless network device
USB Bluetooth dongles
Conexant/Intersil PrismGT SoftMAC USB 802.11b/g wireless network device
Realtek RTL8188CU/RTL8192CU USB IEEE 802.11b/g/n wireless network device
ZyDAS ZD1211/ZD1211B USB IEEE 802.11b/g wireless network device

Serial and parallel interfaces

Belkin USB serial adapter
WinChipHead CH341/340 based USB serial adapter
USB tty support
Cypress microcontroller based USB serial adapter
FT8U100AX USB serial adapter
USB generic serial adapter
USB Huawei 3G wireless modem device
iPAQ USB units
USB Kyocera AIR-EDGE PHONE device
USB printer support
MCT USB-RS232 USB serial adapter
USB modem support
Prolific PL-2303 USB serial adapter
Silicon Laboratories CP2101/CP2102 based USB serial adapter
USB Handspring Visor
SUNTAC Slipper U VS-10U USB serial adapter

Audio devices

USB audio devices
USB MIDI devices
Diamond Multimedia Rio MP3 players

Radio receiver devices

D-Link DSB-R100 USB radio device

Human Interface Devices

Generic driver for Human Interface Devices
Base driver for all Human Interface Devices
USB keyboards that follow the boot protocol
USB mouse devices

Miscellaneous devices

Sigmaltel 4116/4220 USB-IrDA bridge
KingSun/DonShine USB IrDA bridge
USB eGalax touch-panel
USB generic devices
USB IrDA bridges
Prolific based host-to-host adapters
USB scanner support
SCSI-over-USB scanners
SigmaTel STIr4200 USB IrDA bridges
Topfield TF5000PVR range of digital video recorders
USB YAP phone firmware loader


The USB 1.x is a 12 Mb/s serial bus with 1.5 Mb/s for low speed devices. USB 2.x handles 480 Mb/s. Each USB has a host controller that is the master of the bus; all other devices on the bus only speak when spoken to.

There can be up to 127 devices (apart from the host controller) on a bus, each with its own address. The addresses are assigned dynamically by the host when each device is attached to the bus.

Within each device there can be up to 16 endpoints. Each endpoint is individually addressed and the addresses are static. Each of these endpoints will communicate in one of four different modes: control, isochronous, bulk, or interrupt. A device always has at least one endpoint. This endpoint has address 0 and is a control endpoint and is used to give commands to and extract basic data, such as descriptors, from the device. Each endpoint, except the control endpoint, is unidirectional.

The endpoints in a device are grouped into interfaces. An interface is a logical unit within a device; e.g., a compound device with both a keyboard and a trackball would present one interface for each. An interface can sometimes be set into different modes, called alternate settings, which affects how it operates. Different alternate settings can have different endpoints within it.

A device may operate in different configurations. Depending on the configuration the device may present different sets of endpoints and interfaces.

Each device located on a hub has several config(1) locators:

this is the number of the port on closest upstream hub.
this is the configuration the device must be in for this driver to attach. This locator does not set the configuration; it is iterated by the bus enumeration.
this is the interface number within a device that an interface driver attaches to.
this is the 16 bit vendor id of the device.
this is the 16 bit product id of the device.
this is the 16 bit release (revision) number of the device.
The first locator can be used to pin down a particular device according to its physical position in the device tree. The last three locators can be used to pin down a particular device according to what device it actually is.

The bus enumeration of the USB bus proceeds in several steps:

  1. Any device specific driver can attach to the device.
  2. If none is found, any device class specific driver can attach.
  3. If none is found, all configurations are iterated over. For each configuration all the interface are iterated over and interface drivers can attach. If any interface driver attached in a certain configuration the iteration over configurations is stopped.
  4. If still no drivers have been found, the generic USB driver can attach.


Use the following to get access to the USB specific structures and defines.

#include <dev/usb/usb.h>

The /dev/usbN can be opened and a few operations can be performed on it. The poll(2) system call will say that I/O is possible on the controller device when a USB device has been connected or disconnected to the bus.

The following ioctl(2) commands are supported on the controller device:

USB_DEVICEINFO struct usb_device_info
This command can be used to retrieve some information about a device on the bus. The addr field should be filled before the call and the other fields will be filled by information about the device on that address. Should no such device exist an error is reported.

struct usb_device_info { 
	uint8_t	udi_bus; 
	uint8_t	udi_addr; 
	usb_event_cookie_t udi_cookie; 
	char		udi_product[USB_MAX_ENCODED_STRING_LEN]; 
	char		udi_vendor[USB_MAX_ENCODED_STRING_LEN]; 
	char		udi_release[8]; 
	char		udi_serial[USB_MAX_ENCODED_STRING_LEN]; 
	uint16_t	udi_productNo; 
	uint16_t	udi_vendorNo; 
	uint16_t	udi_releaseNo; 
	uint8_t	udi_class; 
	uint8_t	udi_subclass; 
	uint8_t	udi_protocol; 
	uint8_t	udi_config; 
	uint8_t	udi_speed; 
#define USB_SPEED_LOW  1 
#define USB_SPEED_FULL 2 
#define USB_SPEED_HIGH 3 
	int		udi_power; 
	int		udi_nports; 
	uint8_t	udi_ports[16]; 
#define USB_PORT_ENABLED 0xff 
#define USB_PORT_SUSPENDED 0xfe 
#define USB_PORT_POWERED 0xfd 
#define USB_PORT_DISABLED 0xfc 

The product, vendor, release, and serial fields contain self-explanatory descriptions of the device.

The class field contains the device class.

The config field shows the current configuration of the device.

The lowspeed field is set if the device is a USB low speed device.

The power field shows the power consumption in milli-amps drawn at 5 volts, or zero if the device is self powered.

If the device is a hub the nports field is non-zero and the ports field contains the addresses of the connected devices. If no device is connected to a port one of the USB_PORT_* values indicates its status.

USB_DEVICESTATS struct usb_device_stats
This command retrieves statistics about the controller.

struct usb_device_stats { 
	u_long	uds_requests[4]; 

The requests field is indexed by the transfer kind, i.e. UE_*, and indicates how many transfers of each kind have been completed by the controller.

USB_REQUEST struct usb_ctl_request
This command can be used to execute arbitrary requests on the control pipe. This is DANGEROUS and should be used with great care since it can destroy the bus integrity.

The include file <dev/usb/usb.h> contains definitions for the types used by the various ioctl(2) calls. The naming convention of the fields for the various USB descriptors exactly follows the naming in the USB specification. Byte sized fields can be accessed directly, but word (16 bit) sized fields must be access by the UGETW(field) and USETW(field, value) macros to handle byte order and alignment properly.

The include file <dev/usb/usbhid.h> similarly contains the definitions for Human Interface Devices (HID).


All USB events are reported via the /dev/usb device. This devices can be opened for reading and each read(2) will yield an event record (if something has happened). The poll(2) system call can be used to determine if an event record is available for reading.

The event record has the following definition:

struct usb_event { 
        int                                 ue_type; 
        struct timespec                     ue_time; 
        union { 
                struct { 
                        int                 ue_bus; 
                } ue_ctrlr; 
                struct usb_device_info      ue_device; 
                struct { 
                        usb_event_cookie_t  ue_cookie; 
                        char                ue_devname[16]; 
                } ue_driver; 
        } u; 

The ue_type field identifies the type of event that is described. The possible events are attach/detach of a host controller, a device, or a device driver. The union contains information pertinent to the different types of events.

The ue_bus contains the number of the USB bus for host controller events.

The ue_device record contains information about the device in a device event event.

The ue_cookie is an opaque value that uniquely determines which device a device driver has been attached to (i.e., it equals the cookie value in the device that the driver attached to). The ue_devname contains the name of the device (driver) as seen in, e.g., kernel messages.

Note that there is a separation between device and device driver events. A device event is generated when a physical USB device is attached or detached. A single USB device may have zero, one, or many device drivers associated with it.


For each USB bus, i.e., for each host controller, there is a kernel thread that handles attach and detach of devices on that bus. The thread is named usbN where N is the bus number.

In addition there is a kernel thread, usbtask, which handles various minor tasks that are initiated from an interrupt context, but need to sleep, e.g., time-out abort of transfers.


The usb driver appeared in NetBSD 1.4.


There should be a serial number locator, but NetBSD does not have string valued locators.
January 23, 2014 NetBSD 7.0