Universal Serial Bus
A General Overview…
- A specification developed by Compaq, Intel, Microsoft, NEC, HP, Lucent, and Philips (USB Implementers Forum).
- replace serial, parallel, ps/2, joystick, midi; all legacy ports.
- no user knowledge necessary to install device.
- correct driver software automatically used
- low: 1.5 Mbit/s
- full: 12 Mbit/s
- high: 480 Mbit/s (USB v2)
- “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
- 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)
- device as a USB host controller.
- shielded cable w/ 4 wires
- VBUS (+5V)
- 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
- 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
- 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
- LSB first
- SYNC bits - DATA bits - EOP (End Of Packet)
- Transaction: consists of three packets (token, data, handshake)
- 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)
- 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
- Host resets device
- Host sends
- 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
A device can be uniquely identified via its Vendor ID and Product ID.
“An open source library that allows you to communicate with USB devices from userspace.”
Facilitates communication between client software and device endpoints via pipes.
- message - used for control transfers
- stream - used for interrupt, isochronous, and bulk transfers
Synchronous and Asynchronous I/O
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_dev_handleper interface on a device
Working with a Device via libusb
libusb_initcreates a session
libusb_get_device_listenumerates all connected devices
- loop through devices
- open a device with
- clear device list by calling
- perform desired I/O
- release device via
- close device via
- close session via
Detach from Kernel
If the OS has connected to the device, you cannot claim it.
List USB Devices in OS X
Ruby and libusb
Ruby libusb implementation.
brew install libusb gem install libusb # or add to Gemfile
Tx Types Supported
Look into this.