Sie sind auf Seite 1von 108

專題簡報

題目 : USB 驅動程式
指導老師

 李志賢 組長
 黃馨瑧 老師
 孫文駿 老師
 潘世恆 老師
組員名單
 組長 : 96951130 張銘峻
 組員 : 96951127 葉明華
96951131 王品哲
96951132 吳維文
96951135 劉宗漢
緣起
 USB 技術發起於 1995 ,用於整合大多數裝置的連接
問題

 現今大多數裝置接透過 USB 與主機連繫,此一技術幾


乎已成主流

 希望透對 USB 驅動程式實作,達到實踐課程中所學習


到的知識
目標
在一裝置與主機連繫中,可分成 :

 Host: 了解 Host 端的驅動程式,對上


(O.S)
對下 (Device) 的運作
 Device: 微處理器如何實現 USB 的通訊
協 定及對硬體的控制
USB 簡介 (1) 系統構成
 主機與裝備: Host 、 device

 物理構成:裝置插入 USB 插槽

 邏輯構成:不同的 USB 元件所擔當的角色和


責任,以及從主機和裝置的角度出發 USB 匯
流排所呈現的結構
USB 簡介 (2) 傳輸類型
 控制傳輸( control )
 中斷傳輸( interrupt )
 巨量傳輸( bulk )
 等時傳輸( isochronous )
驅動程式示意圖
Host device deriver

USB Host deriver

USB client deriver

Device
USB 驅動程式的定位
USER

VFS Block Net Char TTY


layer layer layer layer layer

核心
USB 驅動程式

USB Core

USB 主控器

硬體
基本流程
主機 實體裝置

應用軟體 功能 功能層

USB 系統軟體 USB 邏輯裝置 USB 裝置


USB 主機控制器 USB 匯流排介面 USB 匯流排層

邏輯通訊流
實際通訊流
USB 各邏輯單位間之關係

 Endpoint: 基本的 USB 通訊透過所謂的端點來進



 Interface:USB 端點的組合。一個 USB 介面只處
理 USB 邏輯連結的其中一種形式 , 如鍵盤、滑鼠
 Configuration:USB 介面的運作狀態。一個 USB
裝置可以有多種模式 , 但在一個時間點 , 只能有一
種模式。
USB 各邏輯單位間之關係
 裝置 (device) 可含有一或多種模式
(configurations) 。

 模式通常包含一或多個介面 (interfaces) 。

 介面通常具有一或多組設定值 (settings) 。

 介面可不含任何端點 (endpoints) ,也可包含多個端


點。
描述元樹狀圖

裝置

配置 配置 配置

介面 介面 介面

端點 端點 端點 端點 端點 端點
軟體需求
 Linux kernel 2.6

 USB Host Device Driver

 Host Controller Driver

 ELAN EM78 Assembly

 ELAN WICE
硬體需求
 Host : PC

 模擬 USB 裝置: USB


chip(EM78M611 義隆 ) , 麵包板
( 檢驗訊號 )
硬體 示意圖
USB 接頭與纜線

D+ D-

VDD 資料線 GND

電源線 地線
USB 接頭與纜線
USB 的電源管理

 供電方式:
 Root port hubs
 Bus–powered hub
 Self-powered hubs
訊號處理

 差動分析:
 差動 1: (D+) - (D-) >200mV 且 (D+)
> Vih
 差動 0: (D-) - (D+) >200mV 且 (D-)
>Vih
 編碼方式 :NRZI(Non Return to Zero
Invert)
USB- 通信協定

USB 的通訊協定基本單位 , 是最下層的資料欄


由數個不同的資料欄形成一個封包


由各種不同的封包 , 組成一個資料交易


由一個或一串序列交易 , 構成傳輸

所以在 1ms 中 , 可能含了數個 Device 提出的資


料交易
通信協定 - 資料欄位的格式

 SYNC  DATA
 PID  CRC
 ADDR
 ENDP
 FRAME Number
通信協定 - 資料欄位的格式

同步欄位 (SYNC): 所有封包的開端 , 由 8bit 組成 , 固
定為 00000001


封包辨識元欄位 (PID) :接在同步欄位之後 , 辨識此為
何種封包 , 由 8bit 組成。其中有 4bit 的辨識元 , 和互
為補數的檢查欄


位址欄位 (ADDR) :由 7 個 bit 組成 , 扣除位址 00h
是預備裝置 , 最多可有 127 個裝置


端點欄位 (ENDP): 由 4 個 bit 組成 , 可有 16 個端點做
定址 , 所有 Device 都需要支援端點 0 為控制端點
通信協定 - 資料欄位的格式

訊框號碼欄位 (FRAME NUMBER) :主機以一個
基數遞加 , 直到 7FFH 為止。存於訊框開始的時候
SOF 封包

資料欄位 (DATA) :存於 DATA 封包中 , 在不同的
傳輸形態 , 有不同大小 , 從 0 到 1023 個 byte, 且
為低位元優先 (LBS)

循環重覆核對欄位 (CRC) :用於保護標籤封包合
資料封包的所有非 PID 欄位之資料。而資料封包使
用 CRC16 ,其餘皆是 CRC5
通信協定 - 封包格式

 Token
 SOF
 DATA
 Handshake
 Spcial
通信協定 - 封包格式

標籤封包 : 用於執行通訊協定之前的前導
動作

SYNC PID ADDR ENPD CRC


其中之 PID 封包可分成 :
IN 、 OUT 、 SETUP 來決定此交易是何

通信協定 - 封包格式


SOF 封包 : 屬於標籤封包之一種, PID
獨立出來。目標裝置利用 SOF 來辨別訊
框起點 ( 但是此封包不支援低速 )
SYNC PID Farme numer CRC


資料封包 :PID 分為 DATA0 和 DATA1,
利用彼此交換確保資料傳輸與裝置維持同

SYNC PID DATA CRC
通信協定 - 封包格式


交握封包 :PID 裡含有
ACK 、 NAK 、 STALL 。目的在於回報
交易狀態 , 包含資料接收成功、命令認 `
可或駁回 , 流程控制或暫停
SYNC PID

特殊封包 :PID 為 PRE, 僅用於從全速將為


低速使用
SYNC PID
通訊協定 - 封包格式 (for 2.0)
通訊協定 - 資料交易
(transaction)

資料交易可分成三種


設定式交易 (Setup transaction)


寫入式交易 (In transaction)


讀出式交易 (Out transaction)
通訊協定 - 資料傳輸

SETUP
標籤

控制型傳輸交易:

DATA0
資料

ACK
交握

閒置
主機 裝置
通訊協定 - 資料傳輸
 巨量型傳輸交易
IN OUT
標籤

DATA0/DATA1 NAK STALL DATA0/DATA1

資料 閒置

ACK ACK NAK STALL


交握

主機 裝置 閒置
通訊協定 - 資料交易

中斷型傳輸交易:
IN OUT

標籤

DATA0/DATA1 NAK STALL DATA0/DATA1

資料

ACK ACK NAK SATLL


交握

主機 裝置 閒置
通訊協定 - 資料交易

等時型傳輸交易
IN OUT
標籤

DATA0 DATA0 資料

閒置

主機 裝置
USB 匯流排列舉

 裝置插入
 通電狀態
 預設狀態
 位址狀態
 結構確定狀態
 ( 休眠狀態 )
Client 端

 EM78M611E Chip set

 Simple Test Case


EM78M611 簡介
EM78M611 簡介

 內建 8bit 微處理器 , 有 8 個中斷源及 8


階堆疊
 8位元時間處理器 , 可產生溢位中斷
 內部記憶體 :144byte 的
SRAM 、 64byte 的程式記憶體、 4byte
的 EEROM
Pin Description
USB- 引擎規格

 相容於 USB1.1 之規範


 支援 USB 低速裝置
 可取得與恢復 USB Clock 和 DATA
 提供 Bit Stripping 和 Bit Stuffing 功能
 資料同步位元與維護
 支援一個裝置位址 , 提供一個結構 , 以及在
EM78M611 提供一個介面 , 兩個端點 , 除了端點為
控制傳輸端點 , 端點 1/2 做為中斷型傳輸端點
EM78M611 主要特徵與功能

 可提供多次燒錄 ,8Bit 的 RISC 架構及 USB 微處理



 主要應用範圍在低速裝置 , 如鍵盤
 支援 HID 人機介面裝置類別
 自動偵測主機連接端是 PS/2 或是 USB PORT
工作暫存器 (1)

 Operational  R3: Status register


Registers:  R4: RAM Select
 R0: Indirect register (RSR)
addressing register  R5~R9: Port5 ~
 R1: Time/Clock Port9
Counter
 R2: Program
Counter & Stack
工作暫存器 (2)

 RA: EEPROM Control register


 RB: RF Control register
 RC: USB Application status register
 RD: FIFO address register
 RE: FIFO data register
 RF: Interrupt Status register
 R10~R1F,R20~R3F: General Purpose
register
Control Register

 A: Accumulater  IOCC: Port9 LED


 CONT: Control driving ability
register  IOCD: Port9 pull-
 IOC5~IOC9: I/O Port high control register
control register  IOCE: pull-high
 IOCA: PS/2 USB control &
control register WDTcontrol register
 IOCB: Port9 Wake-  IOCF: Interrupt Mask
up Control register
Extra Registers

 ERA: AD Controller/AD Selection Pin


 ERB: AD Input DATA (LSB)
 ERC: AD Input DATA (MSB)
 ERD: AD Conversion Rate
 ERE: PWM Control
省電模式

 Power Down Mode:

 Dual Clock Mode


Reference
 EM78M611E Specification
 WICE User Manual
ELAN Assembly Language

56 instructions
Example Study

 EM78M611_USB
Analysis of the code (1)
( Registers setup →(1)JMP INITIAL
→(2)WAIT_USB_IN → (3)USB_MODE
→(4)CALL TRIM_PAGE0 →
(5)USB_INITIAL → (6)MAIN_LOOP

Inside MAIN_LOOP(), call EXE_USB to


setup and report USB descriptors
to the host.
Analysis of the code (2)

 This file is for keyboard simulation.


The control of the program will go
back to the main loop after the
initialization. It will wait for the
interrupt to occur and report it
back to the host.
WICE Operational
Procedure

 Running WICE
W-ICE 操作流程 (1)
 點選“ WICE” 程式
W-ICE 操作流程 (2)
W-ICE 操作流程 (3)
W-ICE 操作流程 (4)
W-ICE 操作流程 (5)
W-ICE 操作流程 (6)
W-ICE 操作流程 (7)
W-ICE 操作流程 (8)
W-ICE 操作流程 (9)
W-ICE 操作流程 (10)
W-ICE 操作流程 (11)
W-ICE 操作流程 (12)
USB-Host 端 (1)

 USBD(USB Device Driver layer )


 HCD(Host Controller Driver layer)
USB-Host 端 (2)
Host-End Chipset
Introduction
Mobile Intel 945 Express Chipset
Family
for Intel Centrino Duo Technology

Intel I/O Controller Hub 7 (ICH7)


Family
Intel 945 Express Chipset
ICH7 Chipset
ICH7 (1)
USB host interface with support

for 8 USB ports


4 UHCI host controllers;
1 EHCI high-speed USB 2.0 Host
controller
ICH7 USB Ports Connection
ICH7 (2)
6 logical devices for PCI functions :

B0:D29 is for USB

Bus 0:Device 29:Function 0 USB UHCI Controller


#1
Bus 0:Device 29:Function 1 USB UHCI Controller
#2
Bus 0:Device 29:Function 2 USB UHCI Controller
#3
Bus 0:Device 29:Function 3 USB UHCI Controller
ICH7 (3)
Functions :
Chapter 5.19, Page 200
Registers :
Chapter 11, Page 469
UHCI :
UHCI.pdf
Linux USB Driver
USB core driver:
Implements the USB bus specification.
Architecture independent kernel sub-system.

USB Host drivers:


Different drivers for each USB control
hardware.
Architecture and platform dependent.
Driver Implementation
 Registration : for drivers
 Auto-detection : for device plug-in
 Probe : for device set-up
 Data Transfer : Open , Read/Write
 Sleep & Withdraw : wake-up &
Reset
 Exit : un-register driver
Important Abbreviation
Such as .....
PM : Power Management
RH : Root Hub
QH : Queue Header
TD : Transfer Descriptors
FSBR: Full-Speed Bandwidth
Reclamation
The rest of them are inside of
Important Data Structures
• File Operations Structure
– In order for a driver to turn a module into a device driver module, it
needs to implement the file_operations structure (defined in
<linux/fs.h>)
– It defines a set of functions that the VFS calls when an application
accesses a device node (device file)
• Each VFS file object representing a char device file contains a pointer
to this structure
– These operations are in charge of implementing the system calls
and are therefore named open, read, and so on.
– Each member of the structure must point to the function in the
driver that implements a specific operation (or be left NULL for
unsupported operations)
– See chapter 3 in “Linux Device Drivers, J. Corbet, A. Rubini, G.
Kroah-Hartman, Third Edition, February 2005” for a complete list of
members
Important Files
/drivers/base/bus.c , class.c
/drivers/usb/usb-skeleton.c
/drivers/usb/core/usb.c , hub.c ,
hcd.c ,
urb.c , message.c , config.c , file.c ,
buffer.c , sysfs.c
/drivers/usb/host/uhci-hub.c ,
uhci-hcd.c
USB Core : Overview
Setting up and releasing the
devices, descriptors, interfaces,
and status etc...

Especially, it becomes a medium


between the OS and the low-level
softwares.
描述元樹狀圖

裝置

配置 配置 配置

介面 介面 介面

端點 端點 端點 端點 端點 端點
USB Core : Analysis
/drivers/usb/core/usb.c
This file controls the resources and
manages the whole operation.
/drivers/usb/core/hub.c
This file acts like a gate keeper.
/drivers/usb/core/hcd.c
This file talks to controllers.
USB Core : usb.c
Include “usb.h”,”hub.h”
,<linux/usb.h>

This file controls the resources and


manages the whole operation.
Such as setting up the logical
connection, descriptors and
assigning different tasks to different
subroutines according to the
Important Structures
struct usb_interface
struct usb_host_interface

struct usb_device
Analysis of Files (1)
__find_interface()
usb_release_dev() : release it

when not using anymore.


ksuspend_usb_init(): to the

workqueue
ksuspend_usb_cleanup():

usb_alloc_dev()
USB-Core : devio.c

• devio.c -- User space


communication with
USB devices
•connected()
•usbdev_lseek()
•usbdev_read()
• *alloc_async()
USB-Core : devio.c

 free_asybc()
 async_newpending()
 async_removepending()
 *async_getpending()
 snoop_urb()

async_completed()
 destory_asnyc()
USB-Core : devio.c

 destory_all_async()
 driver_probe()
 driver_disconnect()
 claimintf()
 releaseintf()
 findintfep()
USB-Core : devio.c

 check_ctrlrecip()
 __match_minor()
 *usbdev_lookup_by_minor()
file operations

 usb_open()
 usbdev_release()
 proc_control()
 proc_bulk()
 proc_resetup()
 proc_getdriver()
file operations

 proc_setconfig()
 proc_setinft()
 proc_do_submit()
 proc_submit()
 proc_umlinkurb()
 processcompl()
file operations

 proc_reaprub()
 proc_reapirbnonblock()
 proc_disconectsignal()
 proc_climitinterface()
 realseinterface()
 proc_ioctl()
file operations

 proc_ioctl_defualt()
 usbdev_ioctl()
 usbdev_poll()

 usb_devio_init()
 cleanup()
USB-Endpoint.c

 show_ep_wMaxPacketSize()
 show_ep_type()
 show_ep_interval()
 show_ep_direction()
 usb_endpoint_major_init()
 usb_endpoint_major_clean()
USB-Core : Endpoint.c

 endpoint_free_minor()
 init_endpoint_class()
 release_endpoint_class()
 destory_endpoint_class()
 usb_create_ep_files()
 usb_remore_ep_file()
USB-Core : buffer.c

 hcd_buffer_create()
 hcd_buffer_destroy()
 hcd_buffer_alloc
 hcd_buffer_free()
Host Controller Driver

 UHCI(Universal Host Controller Interface)

 OHCI(Open Host Controller Interface)

 EHCI(Enhanced Host Controller Interface)


for USB 2.0
UHCI Diagram
UHCI : Important
Structures and Defines (1)
 Command Register
 Status Register
 Interrupt Enable Register
 USB port status and control
Registers
 Legacy support Register
 OC and OCC from Intel 430TX and
Later (USB 2.0)
UHCI : Important
Structures and Defines (2)
 struct uhci_qh
 struct uhci_td
 enum uhci_rh_state
 struct uhci_hcd (full uhci controller)
 struct urb_priv (private per-URB
data)
Analysis of UHCI Files (1)
/drivers/usb/host/uhci-hcd.c
uhci_init(): initialization
uhci_start(): setup the skeleton
uhci_stop(): stop running uhci
uhci_shutdown(): make sure it’s
down
uhci_irq(): interrupt handling
[There are several important
Analysis of UHCI Files (2)
uhci_frame_skel_link(): something
with QH, ffs(find first bit set)
finish_reset(): finish up a HC reset
uhci_hcd_died(): last rites for a
defunct/nonfunctional controller
check_and_reset_hc(): check up HC
configure_hc(): store HC registers
Analysis of UHCI Files (3)
/drivers/usb/host/uhci-hub.c
Setting up registers, checking the
status and running of the root hub.
any_ports_active():
get_hub_status_data():
uhci_finish_suspend():
uhci_check_ports():
uhci_hub_control():
Analysis of UHCI Files (4)
/drivers/usb/host/uhci-q.c
Many subroutines are here to control
interrupts, set-up fsbr, td, urb, qh,
and data transfer types, etc ...

/drivers/usb/host/uhci-debug.c
This is for debugging efforts.
謝謝各位

Das könnte Ihnen auch gefallen