Introducing a new way to review your PRs.

Read the announcement

hathach / tinyusb

An open source cross-platform USB stack for embedded system

View on GitHub

18 documents · Updated Nov 23, 2026

docs / class-specialized-peripherals

Specialized peripheral classes

These class drivers connect USB hosts to devices that need more than a serial link, human input, or common media transfer. They cover controller traffic, firmware update, music messages, object exchange, printing, test instruments, custom device protocols, and video frames. Most are device-side drivers; MIDI is the main area with both device and host roles. For the wider class map, see USB class drivers, serial and human interface classes, and media and connectivity classes.

Protocol roles

The drivers expose two main integration models. A structured class owns the USB message flow and asks the application for protocol data. A stream class moves bytes or frame data and lets the application define the content.

ClassSupported role in this areaApplication boundary
Bluetooth HCIUSB device acting as a Bluetooth primary controllerHCI command, ACL data, and controller event callbacks
DFUUSB device for runtime detach and firmware transferFlash, verify, partition, and reboot callbacks
MIDI 1.0USB device and USB hostMIDI packet or byte-stream I/O with mount and transfer callbacks
MIDI 2.0USB device and USB hostUMP I/O, alternate-setting choice, and protocol discovery
MTPUSB deviceSession, storage, object, and transfer callbacks
PrinterUSB devicePrint byte streams and printer status callbacks
USBTMCUSB device for test and measurement controlCommand, response, abort, clear, and notification callbacks
VendorUSB device for an application-defined protocolBuffered or direct endpoint I/O and optional interrupt or isochronous I/O
VideoUSB device that streams frames to a hostFormat negotiation and frame payload callbacks

The application normally does not handle USB descriptors or endpoint completion itself. It declares the class function, supplies class-specific descriptors, and implements the callbacks that carry product data or state. The driver then maps host requests and endpoint transfers to those callbacks.

Controller and firmware flows

Bluetooth HCI

The Bluetooth HCI driver makes a USB device look like a Bluetooth primary controller. The host sends HCI commands through the control path and ACL data through a bulk OUT endpoint. The controller returns HCI events through an interrupt IN endpoint and ACL data through a bulk IN endpoint. Voice endpoints may be described for alternate settings, but the current driver does not use them.

The application boundary is a set of packet callbacks. The application consumes received commands and ACL packets. It gives the driver event and ACL buffers to send, then keeps those buffers valid until the matching sent callback runs. This lets an external Bluetooth controller or stack own the Bluetooth state while the USB class only carries HCI traffic.

References: src/…/bth_device.h• L47-83 src/…/bth_device.c• L110-181 src/…/bth_device.c• L219-282

DFU runtime and download

The Device Firmware Upgrade (DFU) class supports two related device states. Runtime mode exposes a small control interface that accepts a detach request and reports the application-idle state. The application uses the detach callback to reboot into DFU mode. DFU mode then exposes download, upload, status, state, abort, and manifest operations.

The application owns the non-USB firmware work. A download callback receives each image block and a manifest callback finishes the image. The application reports completion with tud_dfu_finish_flashing(). Alternate settings identify separate targets such as flash or EEPROM. Upload callbacks provide image data.

The transfer is asynchronous from the USB class point of view. The driver reports a busy state and a poll delay while the application erases, writes, or checks the target. A successful completion advances the download or manifest state. A failed application result moves the DFU state to an error state, which gives the host a clear recovery point.

References: src/…/dfu.h• L22-84 src/…/dfu_device.h• L30-65 src/…/dfu_device.c• L161-220 src/…/dfu_device.c• L234-413 src/…/dfu_rt_device.h• L19-31 src/…/dfu_rt_device.c• L47-118

Music message flows

MIDI 1.0

The MIDI 1.0 driver carries musical messages over USB bulk endpoints. A USB event packet contains a virtual cable number and the MIDI message. The application can use packet I/O when it wants USB framing, or stream I/O when it wants ordinary MIDI status and data bytes. Multiple virtual cables let one USB interface represent several logical MIDI connections.

On a device, received data raises tud_midi_rx_cb(), and the application reads or writes packets or stream bytes. On a host, enumeration reports the MIDI descriptor and cable counts through callbacks. The host then reports received data and transmit space through transfer callbacks. Packet APIs preserve USB-MIDI event packets. Stream APIs convert between those packets and MIDI bytes while keeping partial messages and SysEx state across calls. Host stream reads also group data by cable and continue while the caller has room for another complete MIDI message.

The host implementation has a narrow device scope. It supports one MIDI device with at most one bulk IN and one bulk OUT endpoint. Each endpoint can carry up to 16 virtual cables. Devices with only one direction are allowed.

References: src/…/midi.h• L20-59 src/…/midi_device.h• L38-77 src/…/midi_device.c• L68-149 src/…/midi_host.h• L42-61 src/…/midi_host.c• L143-170 src/…/midi_host.c• L320-435 src/…/midi_host.c• L441-649

MIDI 2.0

The MIDI 2.0 driver adds Universal MIDI Packets (UMP) while keeping a MIDI 1.0 compatibility path. On a device, alternate setting 0 carries MIDI 1.0 event packets and alternate setting 1 carries UMP words. The device exposes Group Terminal Block data that describes its function-block layout and can answer UMP stream discovery and protocol configuration requests.

MIDI 2.0 discovery gives an application a way to describe endpoint identity, names, function blocks, direction, and group spans. Built-in replies provide the standard discovery data. Application callbacks can override stream messages or supply identity, names, and topology. The device reports the active alternate setting, protocol, and negotiation state to its application.

The host reads both alternate settings during enumeration. When the descriptor identifies MIDI 2.0, it tries to activate alternate setting 1 and reports the active protocol and group count through descriptor and mount callbacks. If endpoint changes or SET_INTERFACE fail, it keeps alternate setting 0 as the MIDI 1.0 path. Host applications then use UMP read and write APIs; the host driver keeps each UMP message whole when it builds a USB transfer, but it does not perform the device-side discovery exchange.

Applications must use packet APIs with alternate setting 0 and UMP APIs with alternate setting 1. Reads return complete messages. Writes accept only complete UMP messages, so an incomplete final message remains unsent until the rest of it is supplied.

The following flow shows how a MIDI 2.0 host selects a protocol and exposes UMP traffic to the application.

sequenceDiagram
    participant H as USB host
    participant D as MIDI 2.0 host driver
    participant A as Application
    H->>D: Parse MIDI 1.0 and MIDI 2.0 settings
    D->>H: Select MIDI 2.0 alternate setting
    H-->>D: Setting result
    D->>A: Descriptor and mount callbacks
    A->>D: Queue complete UMP messages
    D->>H: UMP bulk transfer
    H->>D: UMP bulk transfer
    D->>A: Receive callback
    A->>D: Read UMP words

References: src/…/midi2_device.h• L61-85 src/…/midi2_device.h• L121-221 src/…/midi2_device.c• L204-245 src/…/midi2_device.c• L582-729 src/…/midi2_device.c• L840-1072 src/…/midi2_host.h• L23-72 src/…/midi2_host.c• L225-287 src/…/midi2_host.c• L485-570 src/…/midi2_host.c• L599-892

Object and stream flows

MTP

The Media Transfer Protocol (MTP) driver presents a device as a set of storages and objects. A host opens a session, discovers storage and object handles, reads or writes object data, and closes the session. Objects can represent files, images, audio, video, or other media records. An interrupt endpoint carries device events such as object or storage changes.

The class driver owns the command, data, and response phases. The application supplies the product model behind those phases. It maps operation codes to actions, fills response containers, starts data sends or receives, and sends events. Data callbacks let the application process multi-packet transfers and report completion before it sends the transaction response. Control callbacks handle cancel, reset, device status, extended event data, and vendor requests. The example application shows the intended boundary by mapping MTP operations to a small hierarchical file store.

MTP therefore is not a mass-storage view of a disk. The host sees named objects and their properties through a session. The application decides which objects exist, which properties are valid, and whether the store permits reads, writes, or deletes.

References: src/…/mtp.h• L24-58 src/…/mtp.h• L427-545 src/…/mtp_device.h• L21-46 src/…/mtp_device.h• L79-145 src/…/mtp_device.c• L32-67 src/…/mtp_device.c• L284-384 src/…/mtp_device.c• L402-536 src/…/mtp_device.c• L539-777 examples/…/mtp_fs_example.c• L134-172 examples/…/mtp_fs_example.c• L314-397

Printer

The printer class provides a byte stream for print data and a small control model for printer identity and state. The host sends print bytes to the device over bulk OUT. The device can return bytes over bulk IN when the product needs a reverse channel. The application can report an IEEE 1284 device ID, port status, and soft-reset behavior.

The application reads received bytes from a stream or responds to the receive callback. It writes device-to-host data through the transmit API and flushes it when needed. Control requests are handled through callbacks, so the application owns printer state such as online status, paper state, and device-specific reset work.

References: src/…/printer.h• L17-35 src/…/printer_device.h• L29-51 src/…/printer_device.h• L93-114 src/…/printer_device.c• L92-135 src/…/printer_device.c• L230-315

USBTMC

The USB Test and Measurement Class (USBTMC) driver gives a host a command-and-response link to a test instrument. It carries commands and replies over bulk endpoints and uses tags to match each transfer. An optional interrupt IN endpoint carries notifications. The USB488 extension adds trigger and status-byte support when the application advertises those capabilities; local-control requests such as REN, GTL, and LLO are not supported.

The application owns the instrument behavior while the class driver owns the USB message flow. It starts the bus read when the interface opens and after each completed command or recovery step. A bulk OUT callback receives command data in pieces. A bulk IN request callback reports the requested reply size and termination rules. The application then supplies reply data and keeps that data valid until the completion callback. Vendor-specific message IDs use the same command and response boundary with a different message ID.

USBTMC recovery is part of the application contract. Abort and clear requests can stop a transfer and leave the class in a pending state. The application reports when each action is complete. The driver then returns to accepting commands. The class handles one command or response direction at a time, so the application must treat bus-read arming and recovery callbacks as ordered hand-off points.

References: src/…/usbtmc.h• L15-35 src/…/usbtmc.h• L149-195 src/…/usbtmc_device.h• L24-90 src/…/usbtmc_device.c• L217-281 src/…/usbtmc_device.c• L304-407 src/…/usbtmc_device.c• L420-650 src/…/usbtmc_device.c• L722-1100

Custom and video flows

Vendor

The vendor class is a transport for a product-defined USB protocol. It does not assign meaning to the payload. The application chooses the message format and can use bulk, interrupt, or isochronous endpoints. A vendor interface can therefore carry a normal data stream, control-like events, or time-sensitive data.

The application can select buffered FIFO I/O or direct transfers. Buffered mode gives stream read and write calls. Direct mode gives the application ownership of transfer timing and buffers. Optional interrupt and isochronous endpoints use explicit arm and send calls. Alternate settings can switch endpoint sets, which is useful when a product has idle and active modes or needs to add time-sensitive isochronous traffic. The vendor control callback is the escape point for product-specific control requests.

References: src/…/vendor_device.h• L36-123 src/…/vendor_device.h• L126-207 src/…/vendor_device.h• L304-337 src/…/vendor_device.c• L168-223 src/…/vendor_device.c• L225-357 src/…/vendor_device.c• L494-689 src/…/vendor_device.c• L692-803 src/…/vendor_device.c• L891-991

Video

The video class makes a device appear as a USB video source. A USB video control interface describes the camera or other source. One or more video streaming interfaces describe formats, frame sizes, frame intervals, and the streaming endpoint. The host uses probe and commit requests to choose a stream format before frame traffic starts.

The application accepts or rejects the committed stream parameters through a callback. After commit, it submits one frame buffer with tud_video_n_frame_xfer() or generates each payload through tud_video_prepare_payload_cb(). The driver adds video payload headers, splits the frame across transfers, marks the end of the frame, and reports completion or abort. After the driver accepts a frame buffer, the application must keep it valid until the complete or abort callback releases it.

This is a device-to-host frame model. The application descriptor decides which formats and frame choices the host can request. The stream can use bulk or isochronous transfers. Opening the device can fail when an isochronous endpoint needs more endpoint or packet memory than the MCU provides. Lower the resolution, frame rate, or format size, or reduce the number of video streaming interfaces to fit the port.

The following flow shows the normal video life cycle, including the application-owned frame buffer until the driver completes or aborts it.

flowchart LR
    H[Host] -->|Probe and commit| V[Video class driver]
    V -->|Commit callback| A[Application]
    A -->|Frame buffer or payload callback| V
    V -->|Payload packets| H
    V -->|Complete or abort callback| A

References: src/…/video.h• L64-165 src/…/video.h• L194-524 src/…/video.h• L528-680 src/…/video_device.c• L205-257 src/…/video_device.c• L883-993 src/…/video_device.c• L1219-1367 src/…/video_device.c• L1388-1485 src/…/video_device.c• L1612-1701

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep each class's descriptor shape aligned with the endpoint and callback contract. This is especially important for Bluetooth HCI alternate settings, MIDI 2.0 alternate settings, MTP's three-endpoint layout, and video control-to-streaming links.
  • For DFU, keep the functional descriptor's transfer size, the transfer buffer, and the application's flash block size within the same contract. Keep runtime detach and status behavior aligned with update-tool expectations.
  • For MIDI 2.0, update Group Terminal Block topology, alternate-setting detection, active protocol state, host fallback, UMP transfer boundaries, and discovery response state together.
  • For MIDI 1.0 and MIDI 2.0 host reads, keep applications draining the receive FIFO from each receive callback so pending data does not stop later bulk IN transfers. Keep stream reads aligned with cable and message boundaries.
  • For MTP, keep the advertised operation, event, property, and format lists aligned with the callbacks that handle those codes. Keep command, data, response, and recovery phases aligned.
  • For USBTMC, keep capability replies aligned with USB488 trigger and status-byte support, the optional interrupt endpoint, term-character handling, and the supported abort and clear callbacks. Keep every point that re-arms the bus read aligned with the command and recovery state transitions.
  • For vendor alternate settings, keep every reused endpoint address's type and packet size stable across settings. Keep isochronous endpoints out of the default setting.
  • For video, keep format and frame descriptors aligned with probe and commit validation and with the payload size accepted by the application. Keep accepted frame buffers valid until either the complete or abort callback. Also keep isochronous endpoint sizes within the port's endpoint and packet-memory limits.

Important failure modes

TriggerConsequenceGuard
DFU application does not call tud_dfu_finish_flashing()The host sees a busy DFU state and cannot finish the download or manifest stepComplete every asynchronous flash or verify operation with a success or error status
DFU runtime does not reboot after the detach callbackThe host remains in runtime mode and cannot continue the firmware updateUse the runtime detach callback to enter the DFU image after the request is acknowledged
Bluetooth event or ACL buffer is reused before its sent callbackThe controller can send changed or invalid packet dataKeep each transmit buffer valid until the matching completion callback
MIDI host device exposes more than one bulk endpoint in one directionThe MIDI interface can fail to enumerateUse one bulk IN and one bulk OUT endpoint within the host driver's stated limits
MIDI host application leaves data in the receive FIFOLater bulk IN transfers can stop landingRead until the stream or UMP API returns no more data
MIDI 2.0 host cannot activate alternate setting 1The device is used through the MIDI 1.0 compatibility settingCheck the active protocol and alternate setting reported by the mount callback
MIDI 2.0 host write contains an incomplete UMP messageThe incomplete message remains queued and is not sent as a valid USB transferQueue whole UMP messages and retry the unsent words after more FIFO space is available
MTP application does not start the next data or response phaseThe transaction stops in command or data stateCall the matching MTP send, receive, or response API from the operation callback
USBTMC application does not re-arm the bus read after an open, completed command, or successful recoveryThe instrument accepts one command and then stops receivingCall tud_usbtmc_start_bus_read() at each required callback boundary
USBTMC application changes reply data before the completion callbackThe host can receive changed instrument dataKeep the reply buffer unchanged until tud_usbtmc_msgBulkIn_complete_cb() runs
Vendor interrupt or isochronous OUT callback does not arm the next transferNo later data reaches the applicationRe-arm the endpoint after consuming the current packet
Video application sends a frame before probe and commit completeThe driver rejects the transfer or has no valid payload sizeWait for the committed stream state before calling the frame transfer API
Video application reuses an accepted frame buffer before a complete or abort callbackThe driver can read changed data while sending the frameKeep the buffer valid until the callback releases ownership
Video isochronous endpoint allocation exceeds port resourcesThe video device fails to open on that portLower the resolution, frame rate, format size, or number of streaming interfaces
A descriptor advertises a class feature without its application callbackThe request stalls or the class reports an unsupported resultTreat descriptors and callbacks as one public contract

Key files

FileWhy to read it
src/…/bth_device.hDefines the Bluetooth HCI packet and callback boundary
src/…/dfu_device.h and src/…/dfu_rt_device.hDefine firmware transfer and runtime detach integration
src/…/midi_device.h and src/…/midi_host.hDefine MIDI 1.0 packet, stream, mount, and transfer APIs
src/…/midi_host.cShows host stream conversion, FIFO draining, and bounded read behavior
src/…/midi2_device.h and src/…/midi2_host.hDefine MIDI 2.0 UMP, alternate-setting, and discovery APIs
src/…/midi2_host.cShows host alternate-setting selection, UMP transfer boundaries, and mount callbacks
src/…/midi2_device.cShows MIDI 2.0 protocol negotiation and discovery behavior
src/…/mtp_device.hDefines the MTP session, object transfer, and control callbacks
examples/…/mtp_fs_example.cShows how an application maps MTP operations to a storage model
src/…/printer_device.hDefines print stream and printer control integration
src/…/usbtmc.h and src/…/usbtmc_device.hDefine USBTMC message roles, capabilities, command flow, and application callbacks
src/…/usbtmc_device.cShows USBTMC command, response, notification, and recovery flow
src/…/vendor_device.hDefines the generic transport modes and optional endpoint APIs
src/…/video_device.hDefines stream negotiation, frame transfer, payload generation, and frame ownership callbacks
src/…/video.hDefines video formats, controls, descriptors, and payload headers