USB interface
by weller
USB overview
A USB system is a unidirectional communications hierarchy consisting of a single USB HOST and anywhere from 1 to 127 USB Devices. If more than one device is present, one or more USB Hubs must be used. When used, a Hub will be considered one of the 127 devices. All components of a USB system connect to each other using one of the USB specified cables and connectors.
Low, Full, and High-Speed USB signals are transmitted using differential signaling on a twisted-pair data cable ( D+ and D-). In addition to the twisted pair, USB cables also provide a 5 V reference from the Host (VBUS) and a ground reference. For USB3.x SuperSpeed and SuperSpeed Plus, additional high-speed differential data lines are added to D+ and D-. The added lines provide for full-duplex communications to occur in SuperSpeed and SuperSpeed Plus systems.
USB speed
USB spec | Speed | Max bandwidth |
---|---|---|
1.0 | Low Speed | 1.5 Mbit/s |
1.0 | Full Speed | 12 Mbit/s |
2.0 | High Speed | 480 Mbit/s |
3.0 | Super Speed | 5.0Gbit/s |
3.1 | Super Speed plus | 10 Gbit/s |
USB controllers
USB controller is a subsystem which compliant with USB-IF specification inside the SoC. Normally the SoC vendor purchase the USB controller IP from another company. The most famous are : Synopsys DWC3 and Chipidea.\
USB endpoints
Endpoints can be described as data sources or sinks and exists in USB controller only. The data stored at an endpoint may either be received from or waiting for being sent to the USB Host. An endpoint can be configured to support four transfer types defined in the USB specification (Control Transfers, Interrupt Transfers, Isochronous Transfers, and Bulk Transfers). An endpoint acts as a kind of buffer. A USB Host’s client may send data to Endpoint 1 for example. Coming from the USB Host, the data will be sent to the OUT Endpoint 1. The program on the USB device side will then read the data as soon as it is ready to do so. Returning data has to be written to the IN Endpoint 1, as the program cannot access the USB bus freely (the USB bus being controlled by the USB Host). The data in IN Endpoint 1 stays there until the host sends an IN packet to Endpoint 1 requesting the data.
USB data transfer type
The USB Specification defines four different types of data transfers:
Control Transfers
The USB host sends commands and queries to a USB device using a control transfer. The control transfer uses endpoint 0 (EP0) while the USB device is being enumerated. Therefore, it is mandatory that all USB devices support EP0 regardless of the supported speed. The maximum size for a control packet is 8 bytes for low-speed devices; 64 bytes for full-speed devices; and 16, 32 or 64 bytes for high-speed devices.
Interrupt Transfers
Interrupt transfers enable a USB device to request a polling rate from the USB host during the enumeration process. The maximum polling rate for a full-speed device is once per millisecond and once per every 10 milliseconds on low-speed devices. The maximum data payload size for a low-speed USB device is 8 bytes with a maximum throughput of 800 bps. For a full-speed USB device, the maximum payload size is 64 bytes with a maximum throughput of 64 Kbps. Interrupt transfers are acknowledged so they guarantee the delivery of a packet. If a packet fails to arrive, the transfer is tried again.
Bulk Transfers
Bulk transfers are a way for devices to transfer large amounts of data. Because they have the lowest priority when being scheduled on the bus, they do not guarantee a timely delivery. After all other transfers are complete, the remaining bandwidth is given to bulk transfers. Just like interrupt transfers, bulk transfers are acknowledged to guarantee their delivery. Bulk transfers are only supported by full-speed and high-speed devices. For full-speed USB device endpoints, the maximum packet size can either be 8, 16, 32 or 64 bytes long. For high-speed USB device endpoints, the maximum packet size can be up to 512 bytes long.
Isochronous Transfers
A full-speed isochronous transaction can send 1023 bytes per frame at a maximum transfer rate of 1023 Kbps. While isochronous transfers offer a guaranteed data transfer rate, their delivery is not acknowledged, and some packets may not arrive. Audio/video streaming is a typical application for isochronous transfers because it is more important to keep the video and audio up to date at the expense of dropping packets.
USB device class
USB defines class code information that is used to identify a device’s functionality and to nominally load a device driver based on that functionality. The information is contained in three bytes with the names Base Class, SubClass, and Protocol.
There are two places on a device where class code information can be placed. One is in the Device Descriptor, and the other is in the Interface Descriptors.
Enumeration
USB Enumeration is the process of detecting, identifying and loading drivers for a USB device. It involves a mixture of hardware techniques for detecting something is present and software to identify what has been connected.
Detecting a Device
A USB interface consists of 4 wires. Power, Ground, Data Plus D+ and D-.
When a USB device is plugged there is a change on these USB data lines. This change that the USB host uses to detect a device has been connected.
Determining the Device Speed
A low-speed device places 5 V on D-, high and full-speed devices assert 5 V on D+.
If the attached Device is a High-Speed device, a “chirp” will be returned and the High-Speed detection process will be completed.
Determining What Device is Attached
Once the USB host has established a USB device is connected, and at what speed it should communicate, then the host will reset the USB device and attempt to read the descriptors to identify the USB device using a default address. The USB host will send a Get_Device_Descriptor command and then receive a packet of bytes with the descriptor length and the actual descriptor.
Once the speed has been settled, the Host reads the Device descriptor and assigns an address. Each USB device is assigned a unique address by the host after attachment or after reset.
Determining the Device Configuration
A USB device must be configured before its function(s) may be used. The host is responsible for configuring a USB device. The host typically requests configuration information from the USB device to determine the device’s capabilities.
The Configuration Descriptor is of a fixed length 9 bytes and is defined as type 4. This provides device specific information such as the number of interfaces supported by the device and maximum power the device is expected to consume.
The most commonly used descriptors include:
- Device Descriptor
- Configuration Descriptor
- Interface Descriptor
- Endpoint Descriptor
- String Descriptor
Determining the Device Interface
Within a single configuration, a device may support multiple interfaces. An interface is a related set of endpoints that present a single feature or function of the device to the host.
The Interface Descriptor is of a fixed length 9 bytes and is defined as type 4. This provides information such as the number of endpoints to be used.
Loading the Driver
When the USB device has been fully identified by the USB host, then the host needs a driver to control the USB device. Matching the USB device to the driver is usually done by matching up the VID and PID.
QNX USB driver
QCOM USB driver is launched by the command :
io-usb-otg -o dcd-usbrndis-xxx-dwc3 ioport=0xa600000,irq=165,soc=8155,maxspeed=high, -dqcom-xhci ioport=0xa600000,irq=165,nousb3,soc=8155,memory=/memory/ram/dma -n /dev/usb1/io-usb-otg
io-usb-otg is the driver server. The detail of io-usb-otg options description can be found here.
Let’s see what kind of information we can get from the USB driver:
The USB device is allocated with address 1. Device class is defined in interface class. Device class is 0x08 which is Mass Storage device.
There are 3 endpoints are used. EDP0 for control, EDP1 for input/read. EDP2 for output/write.
Some other information also list there, such as vendor ID and product ID which is part of device Descriptor.
# usb -vvvvvvvvvv
USB 1 (XHCI) v1.10, v1.01 DDK, v2.00 HCD, DLL: Active
Control, Interrupt, Bulk(SG), Isoch(Stream), High Speed, Super Speed Plus, DMA:32-bit
Device Address : 1
Upstream Host Controller : 1
Upstream Device Address : 0
Upstream Port : 1
Upstream Port Speed : High
Vendor : 0x0951 (Kingston)
Product : 0x1666 (DataTraveler 3.0)
Device Release : r2.00
USB Spec Release : v2.10
Serial Number : E0D55E6C711616C1194203A9
Class : 0x00 (Independent per interface)
Max PacketSize0 : 64
Languages : 0x0409 (English)
Current Frame : 13290 (1024 bytes)
Binary Device Object Store :
2.0 Extention Cap :
Attributes : LPM
SuperSpeed Cap :
Attributes :
Speeds Supported : FULL, HI, SUPER
U1 Device Exit Latency : 10us
U2 Device Exit Latency : 255us
Configurations : 1
Configuration : 1
Attributes : 0x80 (Bus-powered)
Max Power : 100 mA
Interfaces : 1
Interface : 0 / 0
Class : 0x08 (Mass Storage)
Subclass : 0x06 (SCSI)
Protocol : 0x50
Endpoints : Control + 2
Endpoint : 0
Attributes : Control
Max Packet Size: 1x 64 bytes
Endpoint : 1
Attributes : Bulk/IN
Max Packet Size: 1x 512 bytes
Endpoint : 2
Attributes : Bulk/OUT
Max Packet Size: 1x 512 bytes
So, we can see the layer structure between the descriptors like this:
USB launcher and hotplug support
To support the USB device hot plug. QNX USB launcher is processing the PPS event from USB driver and execute script to do usb device enabling or disabling.
usblauncher_otg -S 1 -c /etc/usblauncher/usb1.lua -M /etc/usblauncher/rules.mnt -m /pps/qc -n /dev/usb1/io-usb-otg -E -e -0
From the configuration file : usb1.lua. We can see how the the QNX USB hotplug to be support for different kind of USB devices, such as USB ethernet, USB MASS storage device and so on.
USB handling in QNX for StR
USB schematic and VBUS
From the schematic you can see the USB standard interface. But in real application, there are some thing need to know.
OC pin :
Over current detection is done by this SoC input GPIO to protect the device in the case of over current.
PER_EN :
Power control is a GPIO for USB driver to control the VBUS power. VBUS power can’t be directly supplied by the SoC USB controller. SoC can’t supply big current for external USB devices. So USB device VBUS power will be provide by other circuit by a GPIO control.
devb-umass
A typical command of devb-umass to launch the driver for USB mass-storage devices.
devb-umass disk name=umass, blk cache=32m,vnode=384,auto=none,delwri=5:2,rmvto=none,noatime,memory=/memory/ram/sysram,verbose=r,noexec, umass path=/dev/usb1/io-usb-otg,priority=21,mpoll=100,cam user=0:0,,vid=0x424,did=0x4041,busno=0x1 ,devno=0x2,iface=0,ign_remove