Unit USB

From Ultibo.org
Revision as of 06:23, 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

[Expand]
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


[Expand]
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.


[Expand]
function USBStop:LongWord;
Description: To be documented


[Expand]
procedure USBAsyncStart(Host:PUSBHost);
Description: To be documented


USB device, driver and host functions

[Expand]
function USBDeviceGetAddress(Device:PUSBDevice):Byte;
Description: Get the bus address for the specified device


[Expand]
function USBDeviceSetAddress(Device:PUSBDevice; Address:Byte):LongWord;
Description: Set the bus address for the specified device


[Expand]
function USBDeviceCreateDeviceDescriptor(Device:PUSBDevice; Length:Word):LongWord;
Description: Allocate a device descriptor for the specified device


[Expand]
function USBDeviceCreateConfigurations(Device:PUSBDevice):LongWord;
Description: Allocate the available configurations for this device


[Expand]
function USBDeviceReadConfigurations(Device:PUSBDevice):LongWord;
Description: Read and parse the available configurations for this device


[Expand]
function USBDeviceCreateConfiguration(Device:PUSBDevice; Index:Byte; Size:Word):LongWord;
Description: Allocate the specified configuration for this device


[Expand]
function USBDeviceReadConfiguration(Device:PUSBDevice; Index:Byte):LongWord;
Description: Read and parse the specified configuration for this device


[Expand]
function USBDeviceReadStringDescriptor(Device:PUSBDevice; Index:Byte):String;
Description: To be documented


[Expand]
function USBDeviceReadStringDescriptorW(Device:PUSBDevice; Index:Byte):UnicodeString;
Description: To be documented


[Expand]
function USBDeviceGetConfiguration(Device:PUSBDevice; var ConfigurationValue:Byte):LongWord;
Description: Get the current configuration for the specified device


[Expand]
function USBDeviceSetConfiguration(Device:PUSBDevice; ConfigurationValue:Byte):LongWord;
Description: Set the configuration for the specified device


[Expand]
function USBDeviceFindConfigurationByValue(Device:PUSBDevice; ConfigurationValue:Byte):PUSBConfiguration;
Description: Find the configuration represented by configuration value for the specified device


[Expand]
function USBDeviceGetInterface(Device:PUSBDevice; Index:Byte; var AlternateSetting:Byte):LongWord;
Description: Get the interface alternate setting for the specified device


[Expand]
function USBDeviceSetInterface(Device:PUSBDevice; Index,AlternateSetting:Byte):LongWord;
Description: Set the interface alternate setting for the specified device


[Expand]
function USBDeviceSetFeature(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Feature,Index:Word):LongWord;
Description: Enable a feature on the specified endpoint on the specified device


[Expand]
function USBDeviceClearFeature(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Feature:Word):LongWord;
Description: Disable a feature on the specified endpoint on the specified device


[Expand]
function USBDeviceSetState(Device:PUSBDevice; State:LongWord):LongWord;
Description: Set the state of the specified device and send a notification


[Expand]
function USBDeviceSetStatus(Device:PUSBDevice; Status:LongWord):LongWord;
Description: Set the status of the specified device and send a notification


[Expand]
function USBDeviceBind(Device:PUSBDevice):LongWord;
Description: Attempt to bind a device to one of the registers drivers


[Expand]
function USBDeviceAttach(Device:PUSBDevice):LongWord;
Description: Configure and initialize a newly attached USB device


[Expand]
function USBDeviceDetach(Device:PUSBDevice):LongWord;
Description: Shutdown and detach a USB device


[Expand]
function USBDeviceAllocate(Host:PUSBHost; Parent:PUSBDevice):PUSBDevice;
Description: Create and Register a new Device entry in the Device table}


[Expand]
function USBDeviceFind(USBId:LongWord):PUSBDevice;
Description: To be documented


[Expand]
function USBDeviceFindByName(const Name:String):PUSBDevice; inline;
Description: To be documented


[Expand]
function USBDeviceFindByDescription(const Description:String):PUSBDevice; inline;
Description: To be documented


[Expand]
function USBDeviceEnumerate(Callback:TUSBDeviceEnumerate; Data:Pointer):LongWord;
Description: To be documented


[Expand]
function USBDeviceNotification(Device:PUSBDevice; Callback:TUSBDeviceNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: To be documented


[Expand]
function USBDriverCreate:PUSBDriver;
Description: Create a new Driver entry


[Expand]
function USBDriverCreateEx(Size:LongWord):PUSBDriver;
Description: Create a new Driver entry}


[Expand]
function USBDriverDestroy(Driver:PUSBDriver):LongWord;
Description: Destroy an existing Driver entry


[Expand]
function USBDriverRegister(Driver:PUSBDriver):LongWord;
Description: Register a new Driver in the Driver table


[Expand]
function USBDriverFind(DriverId:LongWord):PUSBDriver;
Description: To be documented


[Expand]
function USBDriverFindByName(const Name:String):PUSBDriver; inline;
Description: To be documented


[Expand]
function USBDriverEnumerate(Callback:TUSBDriverEnumerate; Data:Pointer):LongWord;
Description: To be documented


[Expand]
function USBHostSetState(Host:PUSBHost; State:LongWord):LongWord;
Description: Set the state of the specified host and send a notification}


[Expand]
function USBHostCreate:PUSBHost;
Description: Create a new Host entry


[Expand]
function USBHostCreateEx(Size:LongWord):PUSBHost;
Description: Create a new Host entry


[Expand]
function USBHostDestroy(Host:PUSBHost):LongWord;
Description: Destroy an existing Host entry


[Expand]
function USBHostRegister(Host:PUSBHost):LongWord;
Description: Register a new Host in the Host table


[Expand]
function USBHostFind(HostId:LongWord):PUSBHost;
Description: To be documented


[Expand]
function USBHostFindByName(const Name:String):PUSBHost; inline;
Description: To be documented


[Expand]
function USBHostFindByDescription(const Description:String):PUSBHost; inline;
Description: To be documented


[Expand]
function USBHostEnumerate(Callback:TUSBHostEnumerate; Data:Pointer):LongWord;
Description: To be documented


[Expand]
function USBHostNotification(Host:PUSBHost; Callback:TUSBHostNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: To be documented


[Expand]
function USBBufferAllocate(Device:PUSBDevice; Size:LongWord):Pointer; inline;
Description: Allocate a data buffer for a USB request


[Expand]
function USBBufferAllocateEx(Device:PUSBDevice; Size:LongWord; var Flags:LongWord):Pointer;
Description: Allocate a data buffer for a USB request


[Expand]
function USBBufferValidate(Device:PUSBDevice; Buffer:Pointer; Size:LongWord; var Flags:LongWord):LongWord;
Description: Validate a data buffer for a USB request against the USB host requirements


[Expand]
function USBRequestAllocate(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Callback:TUSBRequestCompleted; Size:LongWord; DriverData:Pointer):PUSBRequest; inline;
Description: Allocate a new USB request


[Expand]
function USBRequestAllocateEx(Device:PUSBDevice; Endpoint:PUSBEndpointDescriptor; Callback:TUSBRequestCompleted; var Data:Pointer; Size:LongWord; DriverData:Pointer):PUSBRequest;
Description: Allocate a new USB request


[Expand]
function USBRequestRelease(Request:PUSBRequest):LongWord;
Description: Release and destroy a USB request


[Expand]