USB 的 descriptor 是 host 識別 device 的資料. 當一個 USB 的設備插入 USB port 時, USB host 會主動詢問 USB 設備的相關資料, 以載入相對應的驅動程式, 以及供給電源等動作. USB 設備就是透過 USB descriptor 來傳達這些資訊.
USB descriptor 的結構如附圖. 由一個 Device Descriptor, 包含數個 Configuration Descriptor, 每一個 Configurtion Descriptor 又可以包含數個 Interface Descriptor, 每一個 Interface Descriptor 又可以包含數個 Endpoint Descriptor, 字串的部份則放在 String Descriptor 中.
Device Descriptor
USB device 只能有一個 device descriptor, 這個 descriptor 提供設備為主的資訊. 譬如說對應到 USB 的規格的版本, 設備供應商的代號, 產品代號, 等等.Offset | Field | Size | Value | Description | Note |
0 | bLength | 1 | Number | size of the descriptor in bytes | 0x12 |
1 | bDescriptorType | 1 | Constant | DeviceDescriptor (0x01) | 0x01 |
2 | bcdUSB | 2 | BCD | USB Sepcification Number | USB 3.0 0x00, 0x03USB 2.1 0x10, 0x02 |
4 | bDeviceClass | 1 | Class | Class Code: 0x00: each interface specifies it’s own claas code 0xFF: the class code is vendor specified Otherwise: | 0xEF Miscellaneous |
5 | bDeviceSubClass | 1 | SubClass | Assigned by USB ORG | 0x02 |
6 | bDeviceProtocol | 1 | Protocol | Assigned by USB ORG | 0x01 |
7 | bMaxPacketSize | 1 | Number | Maximum Packet Size for Endpoint 0 | 8/16/32/64 |
8 | idVendor | 2 | ID | Assigned by USB ORG | |
10 | idProduct | 2 | ID | Assigned by Manufacturer | |
12 | bcdDevice | 2 | BCD | Device Release Number | |
14 | iManufacturer | 1 | Index | Index of Manufacturer String Descriptor | |
15 | iProduct | 1 | Index | Index of Product String Descriptor | |
16 | iSerialNumber | 1 | Index | Index of Serial Number String Descriptor | |
17 | bNumConfigurations | 1 | Integer | Number of Possible Configurations |
Configuration Descriptor
Configuration Descriptor 指定裝置在這個 configuration 的狀態下的電力設定及所需要的電力供應. 通常一個設備只會用到一個 configuration, 但是如果設備可以支援自我供電以及 USB 供電兩種方式的時候, 可以有兩個以上的 configuration, 以供 host 選擇, 但是同一時間只能有一個作用中的 configurtion.舉例來說, 可以有一個高功率需求的 bus 供電 configuration, 以及一個自我供電的 configuration. 如果 USB device 被插入一個有電源供應的系統, 驅動程式可以選擇 bus 供電. 如果是被插入沒有電源供給的系統 (以系統的電池供電), 則可以選擇設備自我供電.
Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | 0x09 |
1 | bDescriptorType | 1 | Constant | Configuration Descriptor | 0x02 |
2 | wTotalLength | 2 | Number | Total Length of Data returned | include all sub desceiptors |
4 | bNumInterfaces | 1 | Number | Number of Interfaces | number of interface in this configuration |
5 | bConfigurationValue | 1 | Number | Value to use as an argument to select this configuration | Configuration value to be selected |
6 | iConfiguration | 1 | Index | Index of String Descriptor Describing this configuration | |
7 | bmAttributes | 1 | Bitmap | D7: Bus Powered D6: Self Powered D5: Remote Wakeup D4..D0 reserved (0) | |
8 | bMaxPower | 1 | mA | Maximum Power Consumption |
Interface Descriptor
interface descriptor 可以看成是一組 endpoints 所組成的功能群組, 以達成特定功能. 例如, 一個多功能事務機, 可以用一個 interface descriptor 來描述 fax 功能的 endpoints, 然後用另一個 interface descriptor 來描述 scanner, 再一個來描述 printer. 和 configuration descriptor 不同的是, interface descriptor 沒有在同一時間內只能有一個在運作中的限制. USB 設備可以有一個或更多的 interface 在運作中.interface descriptors 有一個 bInterfaceNumber 的欄位, 用以指定這一個 interface. 另外, 還有一個 bAlternateSetting 欄位, 這可以讓 interface 達成動態變更的效果.
假設, 我們有一個設備, 它有兩個 interface, interface 1, interface 2. interface 1 的 bInterfaceNumber 設成 0, 表示它是第一個 interface descriptor, 並且 bAlternativeSetting 設成 0. interface 2 的 bInterfaceNumber 設成 1, 表示它是第二個 interface descriptor, 並且 bAlternativeSetting 設成 0. 當我們可以再加入一個 interface descriptor, 並且把 bInterfaceNumber 設成 1, 表示它也是第二個 interface descriptor, 但是這次把 bAlternativeSetting 設定為 1, 表示這一個 interface descriptor 是另一個 interface descriptor 的替代.
Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | 0x09 |
1 | bDescriptorType | 1 | Constant | Interface Descriptor | 0x04 |
2 | bInterefaceNumber | 1 | Number | Number of Interface | zero based interface descriptor |
3 | bAlternateSetting | 1 | Number | Value used to select alternative setting | 0: Not alternative 1: alternative |
4 | bNumEndpoints | 1 | Number | Number of Endpoints used for this interface | |
5 | bInterfaceClass | 1 | Class | Assigned by USB ORG | |
6 | bInterfaceSubClass | 1 | SubClass | Assigned by USB ORG | |
7 | bInterfaceProtocol | 1 | Protocol | Protocol Code | |
8 | iInterface | 1 | Index | Index of String Descriptor |
Endpoint Descriptor
每一個 endpoint descriptor 都是用來指定傳輸的型態, 方向, 輪詢間隔, 最大的封包大小. Endpoint 0, 是預設的控制 endpoint, 只會是 control point, 所以不需要 descriptor.Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | 0x07 |
1 | bDescriptorType | 1 | Constant | End Descriptor | 0x05 |
2 | bEndpointAddress | 1 | Endpoint | Endpoint Address 0b..3b Endpoint NUmber 4b..6b Reserved ( Zero) 7b Direction 0 : Out Endpoint 1 : In Endpoint | zero based interface descriptor |
3 | bmAttributes | 1 | Bitmap | Bits 0..1 Transfer Type 00 = Control 01 = Isochronous 10 = Bulk 11 = Interrupt Bits 5..2 reserved if not Isochronous. Bits 3..2 Synchronization type 00 = No Synchronization 01 = Asynchronous 10 = Adaptive 11 = Synchronous Bits 5..4 Usage type 00 = Data endpoint 01 = Feedback endpoint 10 = Implicit feedback Data endpoint 11 = Reserved | 0: Not alternative 1: alternative |
4 | wMaxPacketSize | 2 | Number | Maximum Packet Size . | |
6 | bInterval | 1 | Number | Polling interval in frame count. Bulk & Control Endpoint ignored. Iso must equal 1 Interrupt 1..255 |
String Descriptor
string descriptor 提供可讀的資訊. 如果不提供這一個 string descriptor, string index 需要設成 0. string 以 Unicode 編碼, 並且提供多國語言的支援.String Descriptor Zero, 是用來描述所支持的語言 :
Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | |
1 | bDescriptorType | 1 | Constant | String Descriptor | 0x03 |
2 | wLANGID[0] | 2 | Number | Supported Language Code Zero | ex 0x0409 (English, USA) |
wLANGID[1] | 2 | Number | Supported Language Code One | ex 0x0C09 (English, Austria) | |
4 | wLANGID[2] | 2 | Number | Supported Language Code X | ex 0x0407 (German – Standard) |
Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | |
1 | bDescriptorType | 1 | Constant | String Descriptor | 0x03 |
2 | bString | n | Unicode | Supported Language Code Zero | Unicode Encoded String |
Device Qualifier
device_qualifier descriptor 描述關於高速設備在不同速度下的訊息改變. 例如, 如果設備正在 full-speed 下運作, device_qualifier 傳回它在 high-speed 下運作的資訊. 反之亦然.Offset | Field | Size | Value | Description | |
0 | bLength | 1 | Number | Size of Descriptor in bytes | |
1 | bDescriptorType | 1 | Constant | Device Qualifier Type | |
2 | bcdUSB | 2 | BCD | USB spec version number. | |
4 | bDeviceClass | 1 | Class | Class Code | |
5 | bDeviceSubClass | 1 | SubClass | SubClass Code | |
6 | bDeviceProtocol | 1 | Protocol | Protocol Code | |
7 | bMaxPacketSize0 | 1 | Number | Maximum packet size for other speed | |
8 | bNumConfigurations | 1 | Number | Number of Other-speed configurations | |
9 | bReserved | 1 | Zero | Reserved for future use, must be zero |
沒有留言:
張貼留言
請提供您寶貴的意見