class UsbPeripheral

Defined at line 106 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

This is the main class for the USB peripheral role driver.

It binds against the USB DCI driver device and manages a list of UsbFunction devices,

one for each USB function in the peripheral role configuration.

Public Members

static basic_string_view kDriverName
static basic_string_view kChildNodeName
static const uint8_t kMaxInterfaces
static const uint8_t kMaxStrings
static const uint8_t kMaxStringLength
static const uint8_t kOutEpStart
static const uint8_t kOutEpEnd
static const uint8_t kInEpStart
static const uint8_t kInEpEnd

Public Methods

zx::result<> Start (fdf::DriverContext context)

fdf::DriverBase2 implementation.

Defined at line 169 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t UsbDciEndpointSetStall (uint8_t ep_address)

Defined at line 121 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t UsbDciEndpointClearStall (uint8_t ep_address)

Defined at line 138 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

bool IsPeripheralStopping ()

Defined at line 98 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void UsbPeripheral ()

Defined at line 187 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void ~UsbPeripheral ()

Defined at line 188 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

DeviceState SnapshotState ()

Defined at line 236 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

size_t active_functions_count ()

Defined at line 241 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void IncrementActiveFunctionsLocked ()

Defined at line 246 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void DecrementActiveFunctions (uint64_t config_generation)

Defined at line 248 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void SetState (DeviceState state)

Defined at line 266 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

bool HasPendingSetConfiguration ()

Defined at line 271 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

fdescriptor::UsbMode SnapshotUsbMode ()

Defined at line 276 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void SetUsbMode (fdescriptor::UsbMode mode)

Defined at line 281 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

const usb_device_descriptor_t & device_desc ()

Defined at line 318 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void SetBcdUsbForTesting (uint16_t bcd_usb)

Defined at line 319 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

inspect::Node & inspect_node ()

Defined at line 329 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

const inspect::Inspector & inspector ()

Defined at line 330 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

usb_inspect::DciInspect & dci_inspect ()

Defined at line 332 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void Stop (fdf::StopCompleter completer)

Defined at line 2630 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t UsbDciCancelAll (uint8_t ep_address)

Defined at line 103 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void SetConfiguration (SetConfigurationRequestView request, SetConfigurationCompleter::Sync & completer)

fuchsia_hardware_usb_peripheral::Device protocol implementation.

Defined at line 2446 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void ClearFunctions (ClearFunctionsCompleter::Sync & completer)

Defined at line 2546 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void GetConfiguration (GetConfigurationCompleter::Sync & completer)

Defined at line 2554 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void SetStateChangeListener (SetStateChangeListenerRequestView request, SetStateChangeListenerCompleter::Sync & completer)

Defined at line 2617 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void handle_unknown_method (fidl::UnknownMethodMetadata<fuchsia_hardware_usb_peripheral::Device> metadata, fidl::UnknownMethodCompleter::Sync & completer)

Defined at line 2625 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t SetDeviceDescriptor (DeviceDescriptor desc)

Defined at line 2508 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx::result<uint8_t> ValidateFunction (size_t function_index, void * descriptors, size_t length)

Validates a function and returns the number of interfaces it uses on

success.

Defined at line 380 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t FunctionRegistered ()

Defined at line 529 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t CheckAndStartController ()

Defined at line 551 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t StartController ()

Defined at line 818 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t StopController ()

Defined at line 853 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t FunctionUnregistered (fit::callback<void ()> on_complete)

Handles unregistration of a single USB function (`Deconfigure` or channel closure).

Callback Semantics (`on_complete`, optional):

- Invocation: If non-null, invoked on `dispatcher()` exactly once after remaining functions

have finished unconfiguring and the DCI controller has stopped (or synchronously if the

controller was not active).

Defined at line 755 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void FunctionCleared (size_t function_index, uint64_t config_generation)

Defined at line 1946 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void SetStateLocked (DeviceState state)

Defined at line 887 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx::result<ResourceAllocations> AllocResources (size_t function_index, uint8_t interface_count, std::span<fuchsia_hardware_usb_function::EndpointResource> endpoints, std::span<std::string> strings)

Defined at line 1138 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

bool ValidateEndpoint (size_t function_index, uint8_t ep_address)

Defined at line 1314 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void ReleaseResources (size_t function_index)

Defined at line 2710 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

void ReleaseResourcesLocked (size_t function_index)

Defined at line 2715 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

ResourceAllocations GetResourceAllocations (size_t function_index)

Returns currently allocated resources for the given function.

For testing purposes only.

Defined at line 2750 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

zx_status_t ConnectToEndpoint (uint8_t ep_address, fidl::ServerEnd<fuchsia_hardware_usb_endpoint::Endpoint> ep)

Defined at line 155 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

const fidl::WireSyncClient<fuchsia_hardware_usb_dci::UsbDci> & dci ()

Defined at line 313 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

void OnHostConnectionChanged (bool connected, fit::callback<void ()> on_complete)

Updates host connection status and, on disconnect, drains `UsbFunction::Unconfigure()` across

all functions before transitioning back to `kPeripheralReady`.

Callback Semantics (`on_complete`, optional):

- Invocation: If non-null, invoked on `dispatcher()` exactly once: synchronously on connect

(or if no functions need unconfiguring), or asynchronously after all functions finish

unconfiguring (or clearing) on disconnect.

Defined at line 2324 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.cc

Enumerations

enum class DeviceState : uint8_t
Name Value
kNoConfiguration 0
kWaitForFunctionBind 1
kStarting 2
kPeripheralReady 3
kHostConnected 4
kUnregisteringFunction 5
kClearingFunctions 6
kStoppingDriver 7

The driver uses a formal state machine to manage the lifecycle of configurations

and host connections. This ensures that resources (child nodes, DCI mode) are

handled safely during asynchronous events like host (dis)connects or driver

teardown.

State Machine:

kNoConfiguration:

Initial state. No active configuration. Functions can be added to the staging area.

Transitions:

-> kWaitForFunctionBind: Occurs when a configuration is committed (SetConfiguration()

or SetDefaultConfig()). Child nodes are published.

-> kClearingFunctions: Occurs on ClearFunctions().

-> kStoppingDriver: Occurs on Stop().

kWaitForFunctionBind:

Configuration committed. Child nodes are published. Waiting for function drivers to bind.

Transitions:

-> kStarting: Occurs when all functions have registered.

-> kClearingFunctions: Occurs on ClearFunctions().

-> kStoppingDriver: Occurs on Stop().

kStarting:

All functions have registered. Starting the DCI controller.

Transitions:

-> kPeripheralReady: Occurs when StartController() succeeds.

-> kWaitForFunctionBind: Occurs if StartController() fails.

-> kClearingFunctions: Occurs on ClearFunctions().

-> kStoppingDriver: Occurs on Stop().

kPeripheralReady:

All functions have registered. DCI is active. Ready for a USB host to connect.

Transitions:

-> kHostConnected: Occurs when a USB host connects.

-> kUnregisteringFunction: Occurs if a function is unregistered (`Deconfigure` / unbind).

-> kClearingFunctions: Occurs on ClearFunctions().

-> kStoppingDriver: Occurs on Stop().

kHostConnected:

USB host has performed enumeration and selected a configuration. Data paths are active.

Transitions:

-> kPeripheralReady: Occurs when the host disconnects and all functions finish

unconfiguring.

-> kUnregisteringFunction: Occurs if a function is unregistered (`Deconfigure` / unbind).

-> kClearingFunctions: Occurs on ClearFunctions().

-> kStoppingDriver: Occurs on Stop().

kUnregisteringFunction:

A single function unregistered while the controller was active. Unconfiguring remaining

functions and stopping the controller before returning to `kWaitForFunctionBind`.

Transitions:

-> kWaitForFunctionBind: Once remaining functions finish unconfiguring and DCI stops.

-> kClearingFunctions: Occurs if ClearFunctions() is called during unregistration.

-> kStoppingDriver: Occurs if Stop() is called during unregistration.

kClearingFunctions:

Configuration clear in progress (`ClearFunctions()`). Unconfiguring all functions, stopping

the controller, and removing child nodes.

Transitions:

-> kNoConfiguration: When all functions are cleared.

-> kStoppingDriver: Occurs if Stop() is called while clearing functions.

kStoppingDriver:

Terminal driver shutdown in progress (`Stop()`).

Defined at line 173 of file ../../src/devices/usb/drivers/usb-peripheral/usb-peripheral.h

Records

Friends

class UsbDciInterfaceServer