USB controller drivers
USB controller drivers connect TinyUSB's portable host and device cores to USB hardware in an MCU or external controller. They keep USB policy in the cores and translate that policy into controller actions for each target. This page complements Platform integration, Board support and build targets, and OS abstraction and runtime services.
Controller architecture
The controller layer has two role contracts and a set of hardware adaptations. A host controller driver (HCD) controls a USB bus and its attached devices. A device controller driver (DCD) makes the target act as a USB device for an external host.
Portability boundary
The host and device cores own USB state and policy. They manage enumeration, control requests, endpoint state, class-driver routing, and application events. The controller driver performs the hardware actions and returns results as core events.
This boundary lets the same USB classes run on different controllers. It also lets one IP-family driver serve many MCU families when their controller behavior matches. Hardware limits must remain visible through endpoint, speed, FIFO, DMA, and role capabilities so the core does not schedule work that the controller cannot perform.
References: src/…/hcd.h• L91-165 src/…/dcd.h• L97-113 src/…/usbh.c• L675-849 src/…/usbd.c• L673-849
Root-hub ports
A root-hub port, or rhport, is TinyUSB's logical name for one controller port. The common API passes it to initialization, interrupt, port, and endpoint operations. Target code maps that logical port to a hardware instance and a host or device role.
The port initialization data carries both the role and requested speed. This allows one target to expose full-speed, high-speed, or more than one controller port without changing the core interface.
References: src/…/tusb_types.h• L60-73 src/…/tusb_types.h• L312-318 src/tusb.c• L65-145
Controller selection
MCU metadata selects a reusable USB IP family and records the facts that affect its use. USB IP means a reusable controller block inside a chip. The shared driver handles transaction behavior. The vendor adaptation supplies the register base, interrupt mapping, endpoint and FIFO limits, clock and PHY actions, role controls, wakeup timing, and memory rules that differ between chips.
This split keeps silicon-specific work out of the common host or device contract. A native driver is used when a controller does not match an existing IP family. The same role contract still applies.
References: src/…/tusb_mcu.h• L30-35 src/…/tusb_mcu.h• L79-121 src/…/dwc2_common.h• L22-56 src/…/dwc2_type.h• L23-31
Event flow
Controller interrupts turn bus and transfer conditions into HCD or DCD events. The core consumes those events from its normal event path and then advances enumeration, control transfers, endpoint work, or class callbacks.
The following diagram shows the direction of requests and events.
flowchart LR
A[Host or device core] -->|controller request| B[HCD or DCD]
B --> C[Vendor or IP-family adaptation]
C --> D[USB controller and PHY]
D -->|interrupt| C
C -->|completion or bus event| E[Core event path]
E --> A
The driver reports each accepted transfer through the event helper. It does not call a class driver directly from the interrupt handler. Device setup events carry the setup request so the device core can manage control-transfer order.
References: src/tusb.c• L130-145 src/…/hcd.h• L37-71 src/…/dcd.h• L28-76 src/…/usbh.c• L748-864 src/…/usbd.c• L698-849
Host and device contracts
The HCD and DCD contracts both submit asynchronous endpoint work and report completion. Their port roles differ. An HCD manages attachment and bus control. A DCD manages the target's connection and device state.
Host controller contract
An HCD gives the host core control of a root port and its endpoint transfers. It initializes host mode, reports connection and speed, performs reset, opens device endpoints, sends setup packets, submits or aborts transfers, clears stalls, and closes endpoints.
Host requests complete later through HCD_EVENT_XFER_COMPLETE. Attach and removal events start or stop enumeration. The host core then advances control stages or sends non-control completion results to the owning class driver.
| HCD area | Contract result |
|---|---|
| Controller | Host mode is ready and its interrupt can run. |
| Root port | Connection, reset completion, and speed are observable. |
| Endpoint | The endpoint address and type map to retained controller state. |
| Transfer | Acceptance is immediate. Completion reports one result and length. |
| Recovery | Abort, close, and clear-stall leave later work in a known state. |
References: src/…/hcd.h• L91-158 src/…/usbh.c• L496-579 src/…/usbh.c• L1169-1323
Device controller contract
A DCD gives the device core control of the USB connection and device endpoints. It initializes device mode, controls the pull-up, handles remote wakeup and interrupts, applies the device address, opens and closes endpoints, submits transfers, and changes endpoint halt state.
It also converts bus signals into device events. These include reset with negotiated speed, setup packets, transfer completion, suspend, resume, unplug, and optional start-of-frame events. The device address and control status order belong to the DCD because controllers differ in when they accept the new address.
References: src/…/dcd.h• L23-76 src/…/dcd.h• L97-188 src/…/usbd.c• L992-1043 src/…/dcd_samg.c• L152-223
Transfer memory contract
DMA-capable controllers need buffers that the hardware can access with the required alignment and cache state. The controller and board configuration must agree on ownership before and after each DMA operation.
Cache clean and invalidate actions are part of the role contract where the target needs them. A mismatch can look like a USB protocol fault because the controller may read old data or the CPU may read stale data after a transfer. Shared controller descriptors also need a memory location and ordering rule that both sides can use safely.
References: src/tusb_option.h• L567-585 src/tusb_option.h• L616-635 src/…/hcd.h• L73-87 src/…/dcd.h• L79-95 src/…/dcd_dwc2.c• L135-148 src/…/hcd_dwc2.c• L347-362
Controller families
The portable layer groups drivers by shared USB IP behavior. Common code owns the role contract and transaction model. Vendor or MCU-family code owns register, clock, PHY, interrupt, and memory differences.
Shared IP families
An IP-family driver can provide an HCD, a DCD, or both. The family code exposes the common role contract. A target adaptation describes each controller instance and its limits.
Examples include:
- DWC2, which supports host and device roles with vendor-specific controller descriptions and low-level hooks.
- RUSB2, which supports host and device roles through finite pipes and FIFO resources.
- ChipIdea high speed, which uses host channels for host behavior and queue descriptors for device behavior.
- FSDEV and MUSB, which adapt register-based full-speed or dual-role controllers.
- WCH USBFS and WCH USBHS, which provide related full-speed and high-speed paths with different endpoint and transaction rules.
- PIO-USB, which provides a software host while still following the HCD contract.
References: src/…/dwc2_common.c• L18-66 src/…/hcd_dwc2.c• L8-21 src/…/dcd_dwc2.c• L11-29 src/…/rusb2_common.h• L12-39 src/…/dcd_stm32_fsdev.c• L75-81 src/…/dcd_wch_usbhs_v1.c• L9-16 src/…/hcd_pio_usb.c• L33-45
Native device paths
Several MCU families use native device paths because their endpoint and buffer models do not match a shared IP family. SAMG exposes simplex endpoint resources and applies the device address after the status stage. NUC120 and NUC121 use separate hardware endpoints for the two directions of control endpoint zero. MSP430 uses dedicated endpoint memory and explicit NAK control.
These paths still present the same DCD contract. Their differences stay visible as endpoint limits, supported transfer types, buffer rules, and bus-event behavior.
References: src/…/dcd_samg.c• L21-35 src/…/dcd_samg.c• L226-251 src/…/dcd_nuc120.c• L9-16 src/…/dcd_nuc121.c• L9-16 src/…/dcd_msp430x5xx.c• L245-314
Native host paths
SAMD host support maps attached-device endpoints to a limited pool of hardware pipes. PIO-USB maps logical TinyUSB ports to software host instances and reports attach, removal, stall, and transfer results through HCD events.
The two paths show why a host driver must expose resource and role limits rather than hide them. SAMD reuses a pipe for a device endpoint and advances multi-packet work from pipe status. PIO-USB parks its ports when the host stack stops and makes a later initialization look like a new attachment.
References: src/…/hcd_samd.c• L39-49 src/…/hcd_samd.c• L53-131 src/…/hcd_samd.c• L219-261 src/…/hcd_pio_usb.c• L36-75 src/…/hcd_pio_usb.c• L87-168 src/…/hcd_pio_usb.c• L238-293
DWC2 role paths
DWC2 is a shared controller family with separate host and device drivers. The device path sizes shared receive and transmit FIFO resources from the active endpoint shape. The host path maps opened endpoints to a limited channel pool and schedules periodic work by frame or microframe.
Both roles can use DMA or slave transfers. The device path handles setup buffering, address status, FIFO limits, and cache ownership. The host path handles retries, low-speed timing, disconnect cleanup, and hub split scheduling when the selected build supports it.
References: src/…/dcd_dwc2.c• L318-400 src/…/dcd_dwc2.c• L619-681 src/…/hcd_dwc2.c• L364-377 src/…/hcd_dwc2.c• L626-727 src/…/hcd_dwc2.c• L796-859
Vendor adaptations
Vendor adaptations describe facts that change between chips using the same IP. They select controller instances and provide the hooks needed to start clocks, configure the PHY, control role interrupts, manage wakeup timing, and maintain DMA memory visibility.
The adaptation must not change the meaning of the HCD or DCD contract. If hardware lacks a feature, the driver must reject or limit that feature before the core schedules it. A controller description must stay aligned with the actual port, interrupt, endpoint, FIFO, channel, speed, and memory resources.
References: src/…/dwc2_common.h• L22-56 src/…/dwc2_type.h• L23-31 src/…/dcd_dwc2.c• L619-681 src/…/dcd_wch_usbhs_v1.c• L154-199
Template drivers
The template HCD and DCD show the required driver surface and unsupported-operation behavior. They are a contract checklist, not a fallback controller.
Start with the closest IP family only after comparing reset behavior, endpoint buffering, DMA rules, and interrupt timing. Similar register names do not prove that two controllers share the same USB behavior.
References: src/…/hcd_template.c• L10-28 src/…/hcd_template.c• L52-126 src/…/dcd_template.c• L10-27 src/…/dcd_template.c• L65-126
Controller support workflow
Adding or maintaining controller support moves from hardware facts to a selected port and then to a proven HCD or DCD contract. The work is complete only when the target initializes the intended role and survives normal USB recovery.
Hardware capability map
Record the controller's role support, speed, endpoint limits, endpoint direction rules, FIFO or descriptor model, PHY, DMA access, cache behavior, reset behavior, and interrupt sources. For a shared IP family, record the per-instance resources that the common driver cannot discover safely.
Then connect MCU metadata to the selected family and capability values. Keep board clocks and pins in board support. Keep USB transaction behavior in the controller driver. Keep vendor adaptation code limited to hardware rules that differ between the shared family.
Unsupported endpoint shapes should fail as capability limits before publication. Missing bus topology or a transfer that violates an already-open endpoint's rules indicates caller or stack state and must not be hidden as a normal device rejection.
References: src/…/tusb_mcu.h• L30-35 src/…/tusb_mcu.h• L137-177 src/…/dwc2_type.h• L23-31 src/…/dcd_dwc2.c• L318-400 src/…/hcd_dwc2.c• L364-377
Contract implementation
Implement the smallest role contract that the hardware supports. Host mode must cover root-port status, reset, speed, endpoint open, setup, transfers, abort, close, and stall recovery. Device mode must cover initialization, connection, reset and setup events, address status, endpoint open, transfers, stall recovery, and unplug behavior.
Keep asynchronous state in controller-owned storage. Use event helpers to return results to the core. Preserve transfer ownership when a reset, newer setup, stall, abort, or endpoint reconfiguration makes old hardware state invalid.
References: src/…/hcd.h• L118-158 src/…/dcd.h• L117-188 src/…/hcd_template.c• L88-126 src/…/dcd_template.c• L69-126 src/…/dcd_dwc2.c• L836-915
Core integration
Configuration and board targets select the role and logical port. Host mode uses the host configuration and root-port mode. Device mode uses the device configuration and root-port mode. Dual-role or multi-port targets must map each rhport to one clear role and hardware instance.
The core calls the controller during root-port initialization. The common interrupt entry point then dispatches to the selected HCD or DCD. A mismatch between configuration, role, port, and hardware instance prevents initialization or sends events to the wrong core.
References: src/tusb_option.h• L477-531 src/tusb.c• L93-145 src/…/usbh.c• L496-578 src/…/usbd.c• L526-589
Validation path
Validate the controller in layers so each failure has a clear owner:
- Confirm that MCU metadata selects the intended IP family, role, port, and speed.
- Confirm initialization, interrupt delivery, attach or pull-up behavior, and bus reset.
- Confirm control transfers, including setup, data, status, and address stages.
- Confirm that a newer setup, reset, disconnect, stall, or abort cannot let stale endpoint work affect a new transfer.
- Confirm every claimed bulk, interrupt, and isochronous endpoint mode within its resource limits. Include low-speed devices behind hubs for host paths that support them.
- Confirm FIFO or descriptor reuse, DMA buffers, cache actions, alignment, and memory sections.
- Confirm disconnect, suspend and resume, reset during transfer, stall recovery, endpoint close, and re-enumeration.
Use a host example to exercise attached-device enumeration and a device example to exercise descriptor configuration and class transfers. Read the host and device core pages for the state machines that consume controller events.
References: src/…/usbh.c• L793-849 src/…/usbh.c• L1071-1163 src/…/usbd.c• L722-849 src/…/usbd.c• L922-979 src/…/dcd_dwc2.c• L1283-1321 src/…/hcd_dwc2.c• L626-727 src/…/hcd_pio_usb.c• L256-293 src/…/dcd_stm32_fsdev.c• L375-449 src/…/dcd_wch_usbhs_v1.c• L403-504
Change checklist
When these files or sections are changed, remember to consider these:
- When changing src/…/tusb_mcu.h, check the IP-family guard, controller count,
rhportmapping, endpoint limit, speed, role support, and cache defaults together. - When changing src/…/hcd.h or src/…/dcd.h, check every family driver and both event consumers because the function results and event shape are shared contracts.
- When changing a vendor adaptation, check register base, IRQ, endpoint and FIFO limits, PHY and clock hooks, role controls, wakeup timing, and cache helpers as one target description.
- When changing endpoint or FIFO limits, check descriptor validation, controller storage, allocation rules, and the hardware limit together.
- When changing DMA or cache behavior, check buffer placement, alignment, ownership, and the matching clean or invalidate path for the active role.
- When changing reset, address, setup, disconnect, or stall handling, check transfer retirement and endpoint re-arming so stale work cannot affect the next request.
- When changing DWC2 FIFO allocation, check endpoint count, implemented IN FIFOs, receive FIFO growth, DMA or slave mode, and isochronous allocation together.
- When changing DWC2 setup-DMA protection, check guard alignment, rewind detection, interrupt masking, deferred recovery, unplug notification, and controller re-initialization together.
- When changing DWC2 host scheduling, check periodic frame selection, low-speed preamble spacing, split transactions, channel abort or close, disconnect cleanup, and DMA or slave mode together.
- When changing SAMD pipe allocation or PIO-USB port mapping, check endpoint ownership, port offsets, attach and removal events, reset state, and transfer completion together.
- When changing native device endpoint memory, check control endpoint direction, packet buffer capacity, address timing, data toggles, and reset allocation together.
- When changing FSDEV packet memory or endpoint handling, check PMA reset, alignment, endpoint-direction pairing, isochronous buffering, interrupt completion order, and endpoint-0 control protection together.
- When changing WCH USBHS v1 endpoint handling, check packet toggles, reset-speed reporting, transfer completion flags, setup handling, and DMA buffer ownership together.
- When adding a role or port, check configuration, board-selected
rhport, interrupt entry, controller instance mapping, and claimed speed as one contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| MCU metadata selects the wrong IP family, role, or port | The build can bind the wrong register model or send events to the wrong core. | Keep capability metadata, configuration, rhport, and controller mapping aligned. |
| A controller accepts an endpoint beyond its FIFO, pipe, channel, or packet-memory resources | Endpoint activation or a later transfer can fail in a way the core cannot repair. | Reject unsupported endpoint shapes before publishing them. |
| A newer setup, reset, stall, or unplug leaves old endpoint work armed | A stale completion or DMA action can affect a new control or data transfer. | Retire old transfer state before re-arming the endpoint. |
| An accepted transfer emits no completion event | The core keeps the endpoint busy and the USB operation stops. | Emit one completion event for every accepted transfer. |
| Device address or control status ordering is wrong | Enumeration fails after SET_ADDRESS even when earlier requests work. | Apply the address in the controller's required status-stage order. |
| DMA buffer placement or cache state is wrong | The controller reads old data or the CPU reads stale data. | Use the target's required memory section, alignment, ownership, and cache helpers. |
| DWC2 setup DMA rewinds into protected memory | Later setup traffic can corrupt memory outside the setup area. | Detect the rewind, stop traffic, report unplug, and restart in task context. |
| DWC2 host disconnect cleanup leaves a channel or parked retry active | A later attach can reuse state from the removed device. | Retire channels and parked work before reporting removal. |
| A low-speed preamble or hub split runs outside its allowed bus window | The host can hang, miss the transaction, or drop the root port. | Enforce frame and microframe timing before enabling the transaction. |
| PIO-USB maps a logical root port to the wrong software port | Attach, removal, or transfer events can be sent for a different bus. | Keep the rhport offset and software-host instance mapping together. |
| Native simplex endpoint resources are treated as bidirectional | Control or data traffic can use the wrong hardware endpoint. | Preserve the controller's direction and endpoint-count limits in endpoint admission. |
| FSDEV packet-memory allocation survives reset or configuration close | New endpoints can reuse stale addresses or exceed packet memory. | Reset allocation state when the controller resets or endpoints close. |
| WCH USBHS v1 loses a packet toggle or clears a completion too late | The host can discard a packet or the next transfer can use stale status. | Track the armed toggle and clear the matching interrupt state before reuse. |
Key files
| File | Why to read it |
|---|---|
| src/…/hcd.h | Defines the host controller contract and event shape. |
| src/…/dcd.h | Defines the device controller contract and event shape. |
| src/…/tusb_mcu.h | Maps MCU families to USB IP, speed, endpoint, role, and memory traits. |
| src/tusb_option.h | Selects host or device mode and controller-related configuration defaults. |
| src/tusb.c | Connects root-port roles and interrupts to the host or device core. |
| src/…/usbh.c | Shows how host events drive enumeration, control transfers, and class callbacks. |
| src/…/usbd.c | Shows how device events drive reset, setup, control, and class transfers. |
| src/…/hcd_template.c | Lists the host driver surface for a new implementation. |
| src/…/dcd_template.c | Lists the device driver surface for a new implementation. |
| src/…/dwc2_common.h | Defines the DWC2-to-vendor adaptation boundary. |
| src/…/dwc2_type.h | Defines per-controller DWC2 resources. |
| src/…/dcd_dwc2.c | Shows DWC2 device resource allocation, transfer modes, setup handling, and recovery. |
| src/…/hcd_dwc2.c | Shows DWC2 host channels, timing, retries, splits, and disconnect cleanup. |
| src/…/hcd_samd.c | Shows a native host path with a finite pipe pool and asynchronous completion. |
| src/…/hcd_pio_usb.c | Shows software host-port mapping and HCD event delivery. |
| src/…/dcd_stm32_fsdev.c | Shows full-speed device packet-memory allocation, endpoint recovery, and control-event ordering. |
| src/…/dcd_samg.c | Shows a native device path with simplex endpoints and delayed address application. |
| src/…/dcd_nuc120.c | Shows a native device path with separate control directions and fixed USB RAM. |
| src/…/dcd_nuc121.c | Shows a related Nuvoton device path with variant memory limits. |
| src/…/dcd_msp430x5xx.c | Shows device interrupt masking, endpoint memory, and explicit NAK control. |
| src/…/dcd_wch_usbhs_v1.c | Shows high-speed device endpoint control, toggles, reset-speed reporting, and DMA transfers. |