參考書:《linux device drivers》、《usb 2.0規範》 《usb3.1規範》《usb白皮書》linux
以linux爲例來講明usb系統。less
先看一下usb藍牙適配器的詳細信息:spa
$ lsusb | grep Cambridge Bus 001 Device 006: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode) $ sudo lsusb -s 001:006 -v
[sudo] password for host:
Bus 001 Device 006: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 224 Wireless
bDeviceSubClass 1 Radio Frequency
bDeviceProtocol 1 Bluetooth
bMaxPacketSize0 64
idVendor 0x0a12 Cambridge Silicon Radio, Ltd
idProduct 0x0001 Bluetooth Dongle (HCI mode)
bcdDevice 88.91
iManufacturer 0
iProduct 2 CSR8510 A10
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 177
bNumInterfaces 2
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xe0
Self Powered
Remote Wakeup
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 3
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0010 1x 16 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0000 1x 0 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0000 1x 0 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 1
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0009 1x 9 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0009 1x 9 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 2
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0011 1x 17 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0011 1x 17 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 3
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0019 1x 25 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0019 1x 25 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 4
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0021 1x 33 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0021 1x 33 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 5
bNumEndpoints 2
bInterfaceClass 224 Wireless
bInterfaceSubClass 1 Radio Frequency
bInterfaceProtocol 1 Bluetooth
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x03 EP 3 OUT
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0031 1x 49 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0031 1x 49 bytes
bInterval 1
Device Status: 0x0001
Self Powered
根據《linux device drivers》中的說明,一個設備能夠有一個或者多個configuration(一般只有一個),code
一個configuration能夠有一個或者多個interface,一個interface中能夠有一個或者多個endpoint.blog
上面的藍牙適配器有1個configuration,這個configuration有7個interface,每一個interface有2個或者3個endpoint.接口
在linux驅動中usb設備驅動與interface相對應。ip
usb之間最基本的通訊是經過endpoint完成的,endpoint之間只能向一個方向發送數據(主機到設備(即OUT endpoint)或者設備到主機(即IN endpoint))。ci
endpoint傳輸數據能夠是下面四種類型之一:get
(1) control endpoint: 用於存取usb設備的不通部分,能夠配置設備、向設備發命令或者獲取設備狀態信息。it
(2)interrupt endpoint: 主機向設備請求數據量較少時使用,例如鍵盤和鼠標
(3)bulk endpoint:用於大量數據傳送。 若是傳送數據過大,則可能分紅多個塊傳送
(4)isochronous endpoint:能夠傳送大量數據,一般用於能處理數據丟失以及固定速率傳送數據的設備。
usb數據傳輸實際是經過packet來進行傳輸的,每一個packet由SYNC(1個字節)、PID(1個字節)、CONTENT(不一樣PID可能有不一樣長度)和CRC(不一樣PID可能有不一樣長度)
詳細的packet定義能夠查看usb規範。
interface用於處理一種類型的usb鏈接,例如鼠標或者鍵盤等。前面的藍牙適配器有多個interface,能夠用來支持多個藍牙設備。
interface還可能有alternate setting, 能夠用來選擇不一樣的interface參數值。
查看pci設備信息:
$ lspci 00:00.0 Host bridge: Intel Corporation 82G33/G31/P35/P31 Express DRAM Controller (rev 10) 00:02.0 VGA compatible controller: Intel Corporation 82G33/G31 Express Integrated Graphics Controller (rev 10) 00:1b.0 Audio device: Intel Corporation NM10/ICH7 Family High Definition Audio Controller (rev 01) 00:1c.0 PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port 1 (rev 01) 00:1c.1 PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port 2 (rev 01) 00:1c.2 PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port 3 (rev 01) 00:1c.3 PCI bridge: Intel Corporation NM10/ICH7 Family PCI Express Port 4 (rev 01) 00:1d.0 USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller #1 (rev 01) 00:1d.1 USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller #2 (rev 01) 00:1d.2 USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller #3 (rev 01) 00:1d.3 USB controller: Intel Corporation NM10/ICH7 Family USB UHCI Controller #4 (rev 01) 00:1d.7 USB controller: Intel Corporation NM10/ICH7 Family USB2 EHCI Controller (rev 01) 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev e1) 00:1f.0 ISA bridge: Intel Corporation 82801GB/GR (ICH7 Family) LPC Interface Bridge (rev 01) 00:1f.1 IDE interface: Intel Corporation 82801G (ICH7 Family) IDE Controller (rev 01) 00:1f.2 IDE interface: Intel Corporation NM10/ICH7 Family SATA Controller [IDE mode] (rev 01) 00:1f.3 SMBus: Intel Corporation NM10/ICH7 Family SMBus Controller (rev 01) 04:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller (rev 02)
安裝tree:
sudo apt-get install tree
查看sysfs中全部usb設備文件:
$ tree /sys/bus/usb/devices/ /sys/bus/usb/devices/ ├── 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-0:1.0 ├── 1-1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1 ├── 1-1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0 ├── 1-1.2 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2 ├── 1-1.2:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.0 ├── 1-1.2:1.1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.1 ├── 1-1.3 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.3 ├── 1-1.3:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.3/1-1.3:1.0 ├── 1-1.3:1.1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.3/1-1.3:1.1 ├── 1-4 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-4 ├── 1-4:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0 ├── 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-0:1.0 ├── 2-2 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-2 ├── 2-2:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0 ├── 3-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.1/usb3/3-0:1.0 ├── 3-1 -> ../../../devices/pci0000:00/0000:00:1d.1/usb3/3-1 ├── 3-1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0 ├── 4-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.2/usb4/4-0:1.0 ├── 5-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.3/usb5/5-0:1.0 ├── usb1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1 ├── usb2 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2 ├── usb3 -> ../../../devices/pci0000:00/0000:00:1d.1/usb3 ├── usb4 -> ../../../devices/pci0000:00/0000:00:1d.2/usb4 └── usb5 -> ../../../devices/pci0000:00/0000:00:1d.3/usb5
本機藍牙適配器鏈接到usb集線器,usb集線器鏈接到pc的一個usb口上。而且usb集線器還鏈接了其餘的一些設備。
從上面的輸出能夠推測,1-1開頭部分應該是usb集線器上鍊接的設備。
/sys/devices/pci0000\:00/0000\:00\:1d.7/usb1應該對應host controller設備,1-1對應struct usb_device.
1-1.2表示藍牙適配器所在hub口,1-1.2:1.0和1-1.2:1.1可能分別表示藍牙適配器的接口。
查看這兩個接口的endpoint:
$ ls -d /sys/devices/pci0000\:00/0000\:00\:1d.7/usb1/1-1/1-1.2/1-1.2\:1.0/ep* /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.0/ep_02 /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.0/ep_82 /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.0/ep_81
$ ls -d /sys/devices/pci0000\:00/0000\:00\:1d.7/usb1/1-1/1-1.2/1-1.2\:1.1/ep*
/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.1/ep_03 /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1.2/1-1.2:1.1/ep_83
1-1.2:1.0下有三個endpoint,對應與lsusb輸出信息中的第一個interface中的3個endpoint.
1-1.2:1.1下有兩個endpoint,對應於lsusb輸出信息中的其餘interface中的2個endpoint。
可是lsusb中總共有7個interface,最後6個interface只用一個interface就能表示,彷佛看起來很奇怪。
仔細看上面的輸出信息能夠知道,最後6個interface確實是一個interface,只是使用了alterate setting,其bAlternateSetting的值分別是0-5,其它組成部分的值如出一轍。