Unit USB
Return to Unit Reference
Description
USB
This unit implements the USB core software that does not depend on the specific host controller hardware and is not specific to any single USB device or platform.
Features and limitations:
- This driver is written to be compatible with USB 2.0. USB 3.0 devices work correctly when operating in USB 2.0 compatible mode, actual support for USB 3.0 host controllers and native super speed modes will require some modification to this driver.
- Not all USB transfer types and speeds are supported yet. This depends on the Host Controller Driver, see USBRequestSubmit.
- This driver does not attempt to do any intelligent power management, bandwidth allocation, or transfer scheduling. Devices are always set to their first listed configuration regardless of power requirements. Requests are simply passed to the Host Controller Driver in the order submitted, the Host Controller Driver is responsible for doing any more intelligent scheduling if desired.
- This driver does not support multiple configurations per USB device. If a device happens to have multiple configurations, the first one will be assigned (see devices below for more information).
- By design, it is possible to implement a host controller driver for different host controller hardware without changing any of this code, as long as the host controller driver provides the functions declared in TUSBHost (Start, Stop, Reset, Submit and Cancel).
- By design, this driver has a hard-coded dependency on the USB hub driver because USB is useless without hubs.
To initialize this core USB driver, USBInit must be called by the system startup code. See that function for details.
If the flag USB_AUTOSTART is not set then USBStart must be called to start the USB core. See that function for details.
The other functions exported by this core USB driver are mostly intended to be used by USB device drivers.
Debugging messages in this driver can be enabled by changing the value of USB_DEFAULT_LOG_LEVEL and by enabling the USB_DEBUG define in GlobalDefines.
Be careful when enabling USB debugging as the USB core can generate a lot of messages due to constant polling of interrupt endpoints on devices such as hubs, keyboards and mice.
USB Device
USB devices are the generic implementation of anything that can be connected to the USB bus. All USB devices require a driver to implement the specific behaviour of the device but at the generic level each device will have:
- One or more configurations available. The USB core will first offer the device to drivers without a configuration being selected, the driver may choose a specific configuration if required otherwise the core will simply select the first available configuration.
- One or more interfaces available. On devices with multiple interfaces, the USB core supports different drivers binding to different interfaces. This way combination devices are supported.
- One or more endpoints available. All devices must have a control endpoint and most devices will have one or more bulk, interrupt or isochronous endpoints as well. The USB core will only communicate with the control endpoint to perform generic operations like reading the desriptors and assigning a device addresss.
Drivers are expected to understand which endpoints they need to communicate with to provide the device specific functionality.
Devices are considered dynamic by the USB core and can be connected or disconnected at any time. The hub driver is responsible for attaching and detaching devices in response to hub status change events and for binding or unbinding drivers as devices are added or removed.
USB Host
USB hosts implement the hardware level interface that supports sending USB requests to the hardware and receiving responses. All handling of interrupts, DMA, transaction sequence, errors and resubmitting/retrying requests is done by the USB host driver. All host drivers must implement the functions defined in TUSBHost (Start, Stop, Reset, Submit and Cancel) and must register themselves with the USB core by calling USBHostRegister. When the USB core is started each registered host driver will be called via the Start function so it can initialize itself and allocate locks, buffers and other resources required to interact with the hardware.
A host driver can also be registered after the USB core has been started and it will be given the opportunity to start itself immediately. In this way USB hosts can potentially be hot pluggable.
Each host driver must also implement a root hub which respresents the port or ports that are directly connected to the controller hardware. In many cases this will not be a real hub but will be simulated in the host driver so that the hub driver can interact with it as though it was a standard hub device.
Constants
To be documented
Type definitions
To be documented
Public variables
To be documented
Function declarations
Initialization functions
procedure USBInit;
Note | To be documented |
---|
function USBStart:LongWord;
Note | To be documented |
---|
function USBStop:LongWord;
Note | To be documented |
---|
procedure USBAsyncStart(Host:PUSBHost);
Note | To be documented |
---|
USB device, driver and host functions
function USBDeviceGetAddress(Device:PUSBDevice):Byte;
Device | The USB device to get the address for |
---|---|
Return | Device address or 0 on failure |
function USBDeviceSetAddress(Device:PUSBDevice; Address:Byte):LongWord;
Device | The USB device to set the address for |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceCreateDeviceDescriptor(Device:PUSBDevice; Length:Word):LongWord;
Device | The USB device to create the device descriptor for |
---|---|
Length | The length of the descriptor to create |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceCreateConfigurations(Device:PUSBDevice):LongWord;
Device | The USB device to create the configurations for |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceReadConfigurations(Device:PUSBDevice):LongWord;
Device | The USB device to read the configurations for |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceCreateConfiguration(Device:PUSBDevice; Index:Byte; Size:Word):LongWord;
Device | The USB device to create the configuration for |
---|---|
Index | The index of the configuration to create |
Size | The size of the configuration descriptor to create |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceReadConfiguration(Device:PUSBDevice; Index:Byte):LongWord;
Device | The USB device to read the configuration for |
---|---|
Index | The index of the configuration to read |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceReadStringDescriptor(Device:PUSBDevice; Index:Byte):String;
Note | To be documented |
---|
function USBDeviceReadStringDescriptorW(Device:PUSBDevice; Index:Byte):UnicodeString;
Note | To be documented |
---|
function USBDeviceGetConfiguration(Device:PUSBDevice; var ConfigurationValue:Byte):LongWord;
Device | The USB device to get the current configuration for |
---|---|
ConfigurationValue | The current configuration (As per bConfigurationValue in the configuration descriptor) |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceSetConfiguration(Device:PUSBDevice; ConfigurationValue:Byte):LongWord;
Device | The USB device to set the configuration for |
---|---|
ConfigurationValue | The configuration to set (As per bConfigurationValue in the configuration descriptor) |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceFindConfigurationByValue(Device:PUSBDevice; ConfigurationValue:Byte):PUSBConfiguration;
Device | The USB device to find the configuration for |
---|---|
ConfigurationValue | The configuration value to find (As per bConfigurationValue in the configuration descriptor) |
Return | USB Configuration if completed or nil on failure |
function USBDeviceGetInterface(Device:PUSBDevice; Index:Byte; var AlternateSetting:Byte):LongWord;
Device | The USB device to get the interface alternate setting for |
---|---|
Index | The index of the interface to get (As per bInterfaceNumber in the interface descriptor) |
AlternateSetting | The current alternate setting of the specified interface (As per bAlternateSetting in the interface descriptor) |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceSetInterface(Device:PUSBDevice; Index,AlternateSetting:Byte):LongWord;
Device | The USB device to set the interface alternate setting for |
---|---|
Index | The index of the interface to set (As per bInterfaceNumber in the interface descriptor) |
AlternateSetting | The alternate setting to set on the specified interface (As per bAlternateSetting in the interface descriptor) |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceSetFeature(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Feature,Index:Word):LongWord;
Device | The USB device to enable the feature for |
---|---|
Endpoint | The endpoint to enable the feature on |
Feature | The feature to enable |
Index | To be documented |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceClearFeature(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Feature:Word):LongWord;
Device | The USB device to disable the feature for |
---|---|
Endpoint | The endpoint to disable the feature on |
Feature | The feature to disable |
Index | To be documented |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceSetState(Device:PUSBDevice; State:LongWord):LongWord;
Device | The USB device to set the state for |
---|---|
Status | The new state to set and notify |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceSetStatus(Device:PUSBDevice; Status:LongWord):LongWord;
Device | The USB device to set the status for |
---|---|
Status | The new status to set and notify |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceBind(Device:PUSBDevice):LongWord;
Device | The device to attempt to bind a driver to |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceAttach(Device:PUSBDevice):LongWord;
Device | New USB device to configure and initialize |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceDetach(Device:PUSBDevice):LongWord;
Device | The USB device to shutdown and detach |
---|---|
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBDeviceAllocate(Host:PUSBHost; Parent:PUSBDevice):PUSBDevice;
Host | The Host this device is attached to |
---|---|
Parent | The Parent device (Hub) this device is attached to (nil if this device is a root hub) |
Return | Pointer to new Device entry or nil if device could not be created |
function USBDeviceFind(USBId:LongWord):PUSBDevice;
Note | To be documented |
---|
function USBDeviceFindByName(const Name:String):PUSBDevice; inline;
Note | To be documented |
---|
function USBDeviceFindByDescription(const Description:String):PUSBDevice; inline;
Note | To be documented |
---|
function USBDeviceEnumerate(Callback:TUSBDeviceEnumerate; Data:Pointer):LongWord;
Note | To be documented |
---|
function USBDeviceNotification(Device:PUSBDevice; Callback:TUSBDeviceNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Note | To be documented |
---|
function USBDriverCreate:PUSBDriver;
Return | Pointer to new Driver entry or nil if driver could not be created |
---|
function USBDriverCreateEx(Size:LongWord):PUSBDriver;
Size | Size in bytes to allocate for new driver (Including the driver entry) |
---|---|
Return | Pointer to new Driver entry or nil if driver could not be created |
function USBDriverDestroy(Driver:PUSBDriver):LongWord;
Note | To be documented |
---|
function USBDriverRegister(Driver:PUSBDriver):LongWord;
Note | To be documented |
---|
function USBDriverFind(DriverId:LongWord):PUSBDriver;
Note | To be documented |
---|
function USBDriverFindByName(const Name:String):PUSBDriver; inline;
Note | To be documented |
---|
function USBDriverEnumerate(Callback:TUSBDriverEnumerate; Data:Pointer):LongWord;
Note | To be documented |
---|
function USBHostSetState(Host:PUSBHost; State:LongWord):LongWord;
Host | The USB host to set the state for |
---|---|
State | The new state to set and notify |
Return | USB_STATUS_SUCCESS if completed or another error code on failure |
function USBHostCreate:PUSBHost;
Return | Pointer to new Host entry or nil if host could not be created |
---|
Return to Unit Reference