1
1
Design of a USB Device Driver
Design of a USB Device Driver
Joe Flynn
Questra Corporation
(716)381-0260
2
Outline
Outline
?
?
USB Overview
USB Overview
?
?
USB Hardware Controllers
USB Hardware Controllers
?
?
Architecture of an Embedded USB Device
Architecture of an Embedded USB Device
?
?
USB Device Driver Architecture
USB Device Driver Architecture
?
?
Case Study of a USB Device Driver
Case Study of a USB Device Driver
Packet Types
Packet Types
–
–
USB Transactions
USB Transactions
?
?
Enumeration
Enumeration
–
–
Enumeration States
Enumeration States
?
?
Examples of USB devices
Examples of USB devices
4
Hardware Overview
Hardware Overview
Physical Hardware View
Physical Hardware View
Logical Hardware View
Logical Hardware View
Pen
Kbd
Mouse
Mic
Mic
–
–
Up to 6 tiers
Up to 6 tiers
?
?
Bus transactions
Bus transactions
–
–
Speed: 12Mbps aggregate
Speed: 12Mbps aggregate
•
•
1.5Mbps sub-channel
1.5Mbps sub-channel
–
–
Isochronous
Isochronous
and Asynchronous
and Asynchronous
–
–
Media access controlled by host
Media access controlled by host
?
?
Configuration
Configuration
+5V
+5V
Speaker
3
5
The Transaction Protocol is Host Based
The Transaction Protocol is Host Based
?
?
Host based token polling
Host based token polling
–
–
Data from host-to-function and function-to-host
Data from host-to-function and function-to-host
–
–
Host handles most of the protocol complexity
Host handles most of the protocol complexity
–
–
Peripheral design is simple and low-cost
Peripheral design is simple and low-cost
?
?
Robustness
Robustness
–
–
Handshake to acknowledge data transfer and flow control
Flow control for peripheral buffer management
Data Transfer
Data TransferToken
Token
Handshake
Handshake
6
Packet Types
Packet Types
?
?
Token - OUT, IN, SOF, SETUP
Token - OUT, IN, SOF, SETUP
–
–
First packet in any transaction
First packet in any transaction
–
–
Specifies function address, endpoint
Specifies function address, endpoint
–
–
Specifies data direction
Specifies data direction
?
?
Data - DATA0, DATA1
Data - DATA0, DATA1
–
A Typical USB Transaction Consists
of Three Packets
of Three Packets
Idle
Idle
Idle
Idle
NAK STALL
DATA0/
DATA 1
DATA0/
DATA 1
T/O
ACKACK
DATA0/
DATA1
DATA0/
DATA1
ACK STALLNAK T/O
DATA0
ACK
T/O
Function
Function
Host
Host
Token
Token
Idle
Idle
bursty
bursty
, non-periodic, low bandwidth
, non-periodic, low bandwidth
?
?
Bulk (Printer, scanner, digital camera …)
Bulk (Printer, scanner, digital camera …)
–
–
Non-periodic,
Non-periodic,
bursty
bursty
, high bandwidth utilization
, high bandwidth utilization
?
?
Control (Configuration messages …)
Control (Configuration messages …)
–
–
Bursty
Bursty
, host-initiated (bus management,
, host-initiated (bus management,
configuration)
configuration)
5
9
USB Device
a collection
of
endpoints
Data
Data Data Per
Data Per
Endpoint
Endpoint
Default
Default
Pipe to
Pipe to
Endpoint Zero
Endpoint Zero
Unspecified
Unspecified
USB
USB
Framed
Framed
Data
Data
Endpoint
Zero
Buffers
Buffers
Interface
Interface
Specific
Enumeration: Device perspective
Enumeration: Device perspective
?
?
Attached State
Attached State
–
–
Entered by attaching USB Cable
Entered by attaching USB Cable
?
?
Powered State
Powered State
–
–
USB Host Applies power
USB Host Applies power
?
?
Default state
Default state
–
–
USB Host resets bus
USB Host resets bus
?
?
Addressed State
Addressed State
Bus Activity
Idle Bus
Idle Bus
Default
Configured
Address
Suspended
Suspended
Suspended
Bus Activity
Idle Bus
Bus Activity
Bus
Activity
Idle Bus
Address
Assigned
Device
Configured
Hub
Hub
Configured
Configured
Hub Reset
Hub Reset
or
or
Deconfigured
Deconfigured
Reset
Reset Device
Reset DeviceGet Device Descriptor via Default Pipe and Address
Get Device Descriptor via Default Pipe and Address Addr
Addr
.
.
Assigned
Assigned
Assign a Unique Address
Assign a Unique Address Read All Configuration Information
Read All Configuration Information Configure and Assign Configuration Value
Configure and Assign Configuration ValueConfigured
Configured
Host
Host
Testing Strategies
Testing Strategies
?
?
Issues to consider
Issues to consider
?
?
Conclusions
Conclusions
14
?
?
Discrete Components
Discrete Components
–
–
NetChip
NetChip
, Intel, National Semiconductor, Phillips
, Intel, National Semiconductor, Phillips
?
?
USB IP Cores as part of an ASIC
USB IP Cores as part of an ASIC
–
–
Sand, Motorola, Texas Instruments, .etc
Sand, Motorola, Texas Instruments, .etc
?
?
USB Core
USB Core
?
?
Registers for Control and Endpoint Data
Registers for Control and Endpoint Data
Transfer
Transfer
?
?
FIFO Controller
FIFO Controller
–
–
Input and Output
Input and Output
FIFOs
FIFOs
for Control Endpoint
for Control Endpoint
–
–
Input or Output FIFO for other Endpoints
Input or Output FIFO for other Endpoints
?
?
DMA Controller
DMA Controller
?
EP 0
Control
EP 1
Bulk/ISO
IN
EP2
Bulk/Iso
Out
EP3
Interrupt
IN
Serial Interface Engine
Output Pad Enable
USB Pads
9
17
?
?
Implements most USB Requests in hardware
Implements most USB Requests in hardware
–
–
Standard Requests
Standard Requests
•
•
GET_DESCRIPTOR and SET_DESCRIPTOR may be
GET_DESCRIPTOR and SET_DESCRIPTOR may be
implemented in software for versatility
implemented in software for versatility
–
–
FIFO empty/full or at high/low threshold level
FIFO empty/full or at high/low threshold level
?
?
FIFOs
FIFOs
supporting
supporting
–
–
multiple packet depth
multiple packet depth
–
–
Hardware Retry of Packet Transfers on error
Hardware Retry of Packet Transfers on error
Key Features of a USB Controller
Key Features of a USB Controller
18
?
?
Hardware should provide ability to
Hardware should provide ability to
–
–
initiate a Remote Wakeup
initiate a Remote Wakeup
–
?
USB Overview
USB Overview
?
?
USB Hardware Controllers
USB Hardware Controllers
?
?
Architecture of an Embedded USB Device
Architecture of an Embedded USB Device
?
?
USB Device Driver Architecture
USB Device Driver Architecture
?
?
Case Study of a USB Device Driver
Case Study of a USB Device Driver
?
?
Testing Strategies
Testing Strategies
?
?
Issues to consider
Issues to consider
?
?
Conclusions
System Architecture
System Architecture
ISR
Thread
USB Controller
Hardware
USB Protocol
Thread
Main Thread
File System
Thread
22
USB Peripheral Threads
USB Peripheral Threads
?
?
ISR Thread
ISR Thread
–
–
Low Level Interrupt Service routine(s)
Low Level Interrupt Service routine(s)
•
•
USB Controller Interrupt
USB Controller Interrupt
•
•
DMA Controller Interrupt
DMA Controller Interrupt
Thread which executes the product application
Thread which executes the product application
–
–
Calls and is triggered by Callback from USB Driver
Calls and is triggered by Callback from USB Driver
layer
layer
?
?
File System Thread
File System Thread
–
–
Lower Priority File System Thread
Lower Priority File System Thread