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
?
?
Testing Strategies
Testing Strategies
?
?
Issues to consider
Issues to consider
?
?
Conclusions
Conclusions
2
3
USB Overview
USB Overview
?
?
Hardware Overview
Hardware Overview
–
–
Topology
Topology
?
?
Protocol Characteristics
Protocol Characteristics
–
–
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
Phone
Monitor
PC
PC
Monitor
Speaker
Pen Mouse
Mic
Phone
PC
HUB
HUB
HOST/HUB
Kbd
?
?
Topology
Topology
–
–
Tiered Star
Tiered Star
(Distributes Connectivity Points)
(Distributes Connectivity Points)
–
–
127 logical connections
127 logical connections
(up to 5 meters per segment)
(up to 5 meters per segment)
–
–
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
–
–
Dynamic insertion-removal
Dynamic insertion-removal
–
–
Autoconfiguration
Autoconfiguration
on change
on change
?
?
Physical Layer
Physical Layer
–
–
2-wire differential signaling, NRZI
2-wire differential signaling, NRZI
coded with bit stuffing
coded with bit stuffing
–
–
4 pin connector, 4 wire cable
4 pin connector, 4 wire cable
–
–
Supply
Supply
Sourcing
Sourcing
+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
Handshake to acknowledge data transfer and flow control
–
–
Very low raw physical bit error rate ( <10
Very low raw physical bit error rate ( <10
-10
-10
)
)
–
–
CRC protection plus hardware retry option
CRC protection plus hardware retry option
–
–
Data Toggle Sequence bits
Data Toggle Sequence bits
?
?
Bounded transfer characteristics
Bounded transfer characteristics
–
–
Data transfer bandwidth and latency
Data transfer bandwidth and latency
prenegotiated
prenegotiated
–
–
Flow control for peripheral buffer management
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
–
–
0 - 1023 bytes
0 - 1023 bytes
?
?
Handshake - ACK, NAK, STALL
Handshake - ACK, NAK, STALL
–
–
Report status of data transaction
Report status of data transaction
–
–
Flow control
Flow control
–
–
Stall conditions
Stall conditions
?
?
Special - PRE
Special - PRE
–
–
Enables Hub for low speed communications
Enables Hub for low speed communications
4
7
A Typical USB Transaction Consists
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
Handshake
Handshake
Data
Data
IN
OUT
SET
8
There are Four Types of USB
There are Four Types of USB
Transactions
Transactions
?
?
Isochronous
Isochronous
(Audio, telephony …)
(Audio, telephony …)
–
–
Periodic, Bounded latencies, guaranteed bandwidth
Periodic, Bounded latencies, guaranteed bandwidth
?
?
Interrupt (Mouse, joystick …)
Interrupt (Mouse, joystick …)
–
–
Asynchronous,
Asynchronous,
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
The Basic USB Model has Several
The Basic USB Model has Several
Layers of Abstraction
Layers of Abstraction
Host
Endpoint 0
Endpoint 0
- Required, shared
- Required, shared
- Configuration access
- Configuration access
- Capability control
- Capability control
Pipe, connection abstraction
Pipe, connection abstraction
between two horizontal layers
between two horizontal layers
Data transport mechanism
Data transport mechanism
USB-relevant format of transported data
USB-relevant format of transported data
SIE
SIE
Host
Cntrl
USB Bus Interface
USB Bus
Interface
USB System
manages devices
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
Specific
Function
a collection
of
Interfaces
Pipe Bundle
Pipe Bundle
to an
to an
interface
interface
No USB
No USB
Format
Format
No USB
No USB
Format
Format
Interface
x
Client SW
manages an
interface
USB Wire
Transactions
USB Framed
Data
Device
10
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
–
–
USB Host sends Set Address with non-zero address
USB Host sends Set Address with non-zero address
?
?
Configured state
Configured state
–
–
USB Host sends Set Configuration with non-zero
USB Host sends Set Configuration with non-zero
value
value
?
?
Suspended state
Suspended state
–
–
USB Host stops sending SOF for 3
USB Host stops sending SOF for 3
msec
msec
6
11
The Device State Machine
The Device State Machine
Attached Powered Suspended
Bus Activity
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
Power
Power
Interruption
Interruption
Reset
Reset
Idle
Bus
Device
Deconfigured
12
Enumeration is the Process of Assigning
Enumeration is the Process of Assigning
Addresses and Setting Configurations
Addresses and Setting Configurations
Connect Cable
Connect Cable
Attached
Attached
Status Change
Status Change
Query Change
Query Change
Port Enable
Port Enable
Powered
Powered
Default
Default
Reset Device
Reset Device
Get 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 Value
Configured
Configured
Host
Host
Hub
Hub
Device
Device
7
13
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
?
?
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
?
?
Combination USB Host and USB Peripheral chip
Combination USB Host and USB Peripheral chip
–
–
ScanLogic
ScanLogic
?
?
Combination micro-processor and USB Core
Combination micro-processor and USB Core
–
–
8/16 bit processor Mitsubishi, .etc
8/16 bit processor Mitsubishi, .etc
?
?
Single Chip Solutions
Single Chip Solutions
–
–
Netchip
Netchip
NET1031 Single chip scanner controller.
NET1031 Single chip scanner controller.
Types of USB Controllers
Types of USB Controllers
8
15
?
?
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
?
?
Internal Bus
Internal Bus
?
?
Serial Interface Engine
Serial Interface Engine
?
?
Output Pads
Output Pads
USB Controller Hardware Architecture
USB Controller Hardware Architecture
16
Example of USB Controller
Example of USB Controller
EP0 FIFO
Processor Bus
USB Core Logic
DMA and
FIFO Control
EP3 FIFOEP2 FIFOEP1 FIFOEP0 FIFO
USB Internal Bus
EPx Status
Reg
USB Control
Reg
EPx Ctl Reg
Register
Bank
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
–
–
Class/Vendor Requests as appropriate
Class/Vendor Requests as appropriate
?
?
USB Event Interrupts and status
USB Event Interrupts and status
–
–
Setup, Suspend, Resume, SOF, Reset, Zero Byte
Setup, Suspend, Resume, SOF, Reset, Zero Byte
Packet
Packet
–
–
DMA Complete
DMA Complete
–
–
Transmit/Receive
Transmit/Receive
Ack
Ack
/
/
Nack
Nack
/Error status
/Error status
–
–
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
–
–
detect a USB Reset
detect a USB Reset
–
–
reset USB Controller
reset USB Controller
–
–
Select endpoint as DMA destination
Select endpoint as DMA destination
–
–
Detect enumeration
Detect enumeration
–
–
Read Current Configuration and Interface
Read Current Configuration and Interface
–
–
Stall endpoints
Stall endpoints
Key Features of a USB Controller II
Key Features of a USB Controller II
10
19
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
?
?
Testing Strategies
Testing Strategies
?
?
Issues to consider
Issues to consider
?
?
Conclusions
Conclusions
20
Architecture of an Embedded USB
Architecture of an Embedded USB
Device
Device
Fifo
Fifo
Fifo
Fifo
Hardware
Hardware
Data packets from host
Data packets from host
Application
Application
Driver
Driver
USB Protocol
USB Protocol
API
API
API
API
Logical pipe
Logical pipe
Logical pipe
Logical pipe
11
21
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
?
?
USB Protocol Thread
USB Protocol Thread
–
–
Task which implements USB Protocol
Task which implements USB Protocol
•
•
Control, Bulk,
Control, Bulk,
Isochrnous
Isochrnous
, Interrupt Endpoints
, Interrupt Endpoints
•
•
Attach/
Attach/
Dettach
Dettach
, SOF, Suspend/Resume
, SOF, Suspend/Resume
?
?
Main Thread
Main Thread
–
–
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