Notes on USB
Universal Serial Bus
A General Overview…
- A specification developed by Compaq, Intel, Microsoft, NEC, HP, Lucent, and Philips (USB Implementers Forum).
- Goals:
- replace serial, parallel, ps/2, joystick, midi; all legacy ports.
- no user knowledge necessary to install device.
- correct driver software automatically used
- Speeds
- low: 1.5 Mbit/s
- full: 12 Mbit/s
- high: 480 Mbit/s (USB v2)
Architecture
- “tiered star topology” - single host controller, maximum of 127 connected devices connected to the host.
- 7-bit address field, address 0 reserved
- 6 tiers, max (hubs connected to hubs)
- 5m max cable length
Host Controller
- initiates communication with devices
- communication occurs from device to host, not device to device
- device cannot initiate communication, unless put into a ‘suspend’ state by the host.
- host controller interfaces:
- OHCI - Open Host Controller Interface (hardware-oriented controller)
- UHCI - Universal HCI (Intel’s software-oriented controller USB v1/1.1)
- EHCI - Extended HCI (single standard for OHCI and UHCI)
On-The-Go
- device as a USB host controller.
Electrical Specification
- shielded cable w/ 4 wires
- VBUS (+5V)
- D+
- D-
- GND
- bus-powered device
- draws power from the host
- 100mA max
- 500mA max, if permitted by the host
- 0.5mA max, if suspended
- 2.5mA max, if suspended + remote wakeup enabled
Data Flow
- only one device can talk at a time (shared bus)
- data flows from the host to all devices connected to a hub, but is ignored by only the device with the correct address
- intermediate hubs act as repeaters
Setting Speed
- 1.5Kohm pull-up resistor between _ and 3.3V:
- D+: High Speed Device
- D-: Low Speed Device
- Host observes which line is pulled high and sets speed accordingly.
Line States (D+ and D-)
- Detached: both lines are low.
- Attached/Idle: D+ or D- goes high, other is low. Host observes a device has been plugged in.
- NRZI encoding
Packets
- LSB first
- SYNC bits - DATA bits - EOP (End Of Packet)
- Types:
- token
- data
- handshake
- special
- Transaction: consists of three packets (token, data, handshake)
Data Flow
- Control Tx - used for command or status operations
- Bulk Tx - consume maximum bandwidth, not time-critical
- Interrupt Tx - device requesting action from host
- Isochronous Tx - used to carry time-critical data (eg- video, audio)
Protocol
Start up
- Device is plugged in
- Host detects device via pull-up resistor (high/low speed device)
- Host signals device to reset
- Device resets and then responds to Address 0
- Host queries device for maximum packet size via
Get Descriptor
command - Host resets device
- Host sends
Set Address
command - Host queries device for descriptors (device, configuration, string)
- Host loads device driver
- Device is ready for use
Configurations, Interfaces, Endpoints
These define the features of a device, as well as how to interact with it.
- One configuration loaded at a time, first config is loaded by default
- One or more interfaces, each has a number of endpoints
- Endpoint is a source or sink for data
- up to 16 endpoints on high-speed devices
- up to 3 endpoints on low-speed devices
Example: VOIP Phone
- one audio interface, two audio endpoints
- hid interface, single input endpoint for keypad
Device Identification
A device can be uniquely identified via its Vendor ID and Product ID.
libusb
“An open source library that allows you to communicate with USB devices from userspace.”
Facilitates communication between client software and device endpoints via pipes.
Pipes
- message - used for control transfers
- stream - used for interrupt, isochronous, and bulk transfers
Synchronous and Asynchronous I/O
Logical Representation
Host reading data from an endpoint:
- Host sends request for data
- Host receives incoming data
Host writing data from an endpoint:
- Host sends data to device endpoint
- Host receives acknowledgment
There may be an indefinite delay between these two steps.
Synchronous (blocking I/O) - Perform a USB transfer with via a single function call. Both steps are combined. Asynchronous (non-blocking I/O) - Separate logical steps. Callback function handles step 2.
Devices and Interfaces
- Interact with a device through a
libusb_device
object - one
libusb_dev_handle
per interface on a device
Working with a Device via libusb
libusb_init
creates a sessionlibusb_get_device_list
enumerates all connected devices- loop through devices
- open a device with
libusb_open
orlibusb_open_device_with_vid_pid
- clear device list by calling
libusb_free_device_list
- call
libusb_claim_interface
- perform desired I/O
- release device via
libusb_release_interface
- close device via
libusb_close
- close session via
libusb_exit
Detach from Kernel
If the OS has connected to the device, you cannot claim it.
libusb_detach_kernel_driver
libusb_kernel_driver_active
=> 1/0
List USB Devices in OS X
system_profiler SPUSBDataType
Ruby and libusb
Ruby libusb implementation.
brew install libusb
gem install libusb # or add to Gemfile
Tx Types Supported
Called on DevHandle
instances.
control_transfer
bulk_transfer
interrupt_transfer
HID API
Look into this.
References
- USB Made Simple
- USB - Wikipedia
- USB 3.1 Specification
- libusb - C library for interfacing with USB devices
- libusb wiki
- Intro to libusb