Unit USB

From Ultibo.org
Revision as of 05:55, 15 September 2016 by Ultibo (Talk | contribs)

Jump to: navigation, search

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;
Description: Performs basic initialization of the USB core driver, after this devices, hosts and drivers can be registered however nothing will work until USBStart is called
Note To be documented


function USBStart:LongWord;
Description: Starts all registered USB hosts, starts the USB hub thread and begins the USB enumeration process. USB enumeration will continue after this function returns as devices are discovered by changes in hub status.
Note To be documented


function USBStop:LongWord;
Description: To be documented
Note To be documented


procedure USBAsyncStart(Host:PUSBHost);
Description: To be documented
Note To be documented


USB device, driver and host functions

function USBDeviceGetAddress(Device:PUSBDevice):Byte;
Description: Get the bus address for the specified device
Device The USB device to get the address for
Return Device address or 0 on failure


function USBDeviceSetAddress(Device:PUSBDevice; Address:Byte):LongWord;
Description: Set the bus address for the specified device
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;
Description: Allocate a device descriptor for the specified device
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;
Description: Allocate the available configurations for this device
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;
Description: Read and parse the available configurations for this device
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;
Description: Allocate the specified configuration for this device
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;
Description: Read and parse the specified configuration for this device
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;
Description: To be documented
Note To be documented


function USBDeviceReadStringDescriptorW(Device:PUSBDevice; Index:Byte):UnicodeString;
Description: To be documented
Note To be documented


function USBDeviceGetConfiguration(Device:PUSBDevice; var ConfigurationValue:Byte):LongWord;
Description: Get the current configuration for the specified device
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;
Description: Set the configuration for the specified device
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;
Description: Find the configuration represented by configuration value for the specified device
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;
Description: Get the interface alternate setting for the specified device
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


Return to Unit Reference