顯示具有 usb 標籤的文章。 顯示所有文章
顯示具有 usb 標籤的文章。 顯示所有文章

2024年11月26日 星期二

USB Device Descriptor

Device Descriptor 

 The USB control transfer command you provided appears to be requesting the Device Descriptor from a USB device. Here's a breakdown of the request:


USB Control Transfer Fields

Command:

CTL 80 06 00 01 00 00 12 00

Field Name Value Explanation
bmRequestType 80 0b10000000: Direction = IN (Device-to-Host), Type = Standard, Recipient = Device.
bRequest 06 GET_DESCRIPTOR: Request to retrieve a descriptor.
wValue 00 01 Descriptor Type = 0x01 (Device Descriptor), Index = 0x00.
wIndex 00 00 Index or language ID. Here, 0x0000 means the default language or no index.
wLength 12 00 Length = 0x0012 (18 bytes), which is the size of a USB Device Descriptor.

Interpretation

This control transfer is a Get Descriptor request to retrieve the Device Descriptor from the USB device. The Device Descriptor provides general information about the USB device, such as vendor ID, product ID, device class, and supported USB version.


Expected Response

The device should return an 18-byte Device Descriptor. The structure of the Device Descriptor is as follows:

Byte Offset Field Size (Bytes) Example Value Description
0 bLength 1 18 (0x12) Descriptor length (18 bytes).
1 bDescriptorType 1 01 Descriptor type (Device).
2-3 bcdUSB 2 0x0200 USB version (e.g., 2.0).
4 bDeviceClass 1 00 Device class (e.g., 0 = per interface).
5 bDeviceSubClass 1 00 Subclass (e.g., 0 = none).
6 bDeviceProtocol 1 00 Protocol (e.g., 0 = none).
7 bMaxPacketSize0 1 64 Max packet size for endpoint 0.
8-9 idVendor 2 0xXXXX Vendor ID assigned by USB-IF.
10-11 idProduct 2 0xXXXX Product ID assigned by the vendor.
12-13 bcdDevice 2 0xYYYY Device release number.
14 iManufacturer 1 01 Index of manufacturer string.
15 iProduct 1 02 Index of product string.
16 iSerialNumber 1 03 Index of serial number string.
17 bNumConfigurations 1 01 Number of configurations.

Command Context

This is often the first request issued by a USB host when enumerating a device. It helps the host identify the device and determine the next steps in initialization (e.g., requesting configuration or string descriptors).

If you'd like further help with parsing the response or issuing this command in a program, let me know!

The bDescriptorType field in USB descriptors identifies the type of descriptor being referenced. It is a single byte in the descriptor that helps the host know what kind of data the descriptor contains.


Common Descriptor Types (bDescriptorType)

Value (Hex) Descriptor Type Description
01 Device Contains general information about the device (e.g., USB version, vendor ID).
02 Configuration Contains details about a specific configuration, including power requirements.
03 String Holds human-readable information such as manufacturer or product name (Unicode).
04 Interface Describes the interfaces supported by the device (e.g., HID, audio, etc.).
05 Endpoint Describes an endpoint's attributes (e.g., direction, type, maximum packet size).
21 HID Contains HID-specific information, such as version and descriptor links.
22 HID Report Contains a detailed structure of input, output, and feature reports for HID devices.
23 HID Physical (Optional) Describes the physical properties of the HID device (e.g., button layout).
29 Hub Contains information about a hub device and its ports.

Where bDescriptorType Appears

  1. In the Request (Get Descriptor):

    • The Descriptor Type is specified in the high byte of the wValue field.
    • For example:
      • wValue = 0x0100: Requesting the Device Descriptor.
      • wValue = 0x2200: Requesting the HID Report Descriptor.
  2. In the Descriptor Itself:

    • Each descriptor includes its own bDescriptorType field, which identifies the descriptor's type.

Example in a Device Descriptor

For a typical Device Descriptor:

Byte Offset Field Value (Hex) Description
0 bLength 12 Descriptor length (18 bytes).
1 bDescriptorType 01 Device Descriptor.

In Summary

The bDescriptorType is a key field that tells the host what kind of descriptor is being processed. The value depends on the context of the descriptor or the type requested. If you need more help identifying or using a specific bDescriptorType, feel free to ask!


bcdUSB

Yes, the bcdUSB field in the Device Descriptor indicates the USB specification version that the device supports. This field is a 16-bit value encoded in Binary-Coded Decimal (BCD) format.


How to Interpret bcdUSB

  • Format: 0xMMmm (BCD)
    • MM: Major version number
    • mm: Minor version number
  • For example:
    • 0x0200 = USB version 2.0
    • 0x0110 = USB version 1.1
    • 0x0300 = USB version 3.0
    • 0x0310 = USB version 3.1
    • 0x0320 = USB version 3.2

Why bcdUSB is Important

  • This field informs the USB host (e.g., computer, hub) about the highest USB specification the device supports.
  • It determines which features and speeds the device can use, such as:
    • USB 1.x: Low-speed (1.5 Mbps) or Full-speed (12 Mbps).
    • USB 2.0: High-speed (480 Mbps).
    • USB 3.x: SuperSpeed (5 Gbps or higher, depending on the sub-version).

Example Scenario

If a USB host queries a device and receives bcdUSB = 0x0200 in the Device Descriptor:

  • The device is compliant with USB 2.0.
  • The host can enable USB 2.0 features like high-speed transfers.

Let me know if you need further details!


bDeviceClass

The bDeviceClass field in the USB Device Descriptor specifies the class of the device. This helps the host system identify the device type and determine which driver to use. The value can indicate a specific class or a generic "classless" device, where the class is determined by the interfaces it contains.


Details of bDeviceClass

  • Size: 1 byte
  • Location: Byte 4 of the Device Descriptor
  • Purpose: Defines the primary functionality or category of the device.

Key Values for bDeviceClass

Value (Hex) Device Class Description
00 Defined by Interface The device does not specify a class at the device level. Class is defined by its interfaces.
01 Audio Audio devices (e.g., speakers, microphones).
02 Communications Communication devices (e.g., modems, network adapters).
03 HID Human Interface Devices (e.g., keyboards, mice, game controllers).
05 Physical Physical devices (e.g., force-feedback devices).
06 Image Imaging devices (e.g., scanners, cameras).
07 Printer Printers.
08 Mass Storage Storage devices (e.g., USB flash drives, external hard drives).
09 Hub USB hubs.
0A CDC-Data Data devices used with the Communications Device Class.
0B Smart Card Smart card readers.
0D Content Security Content protection devices.
0E Video Video devices (e.g., webcams).
0F Personal Healthcare Healthcare devices (e.g., thermometers, blood pressure monitors).
10 Audio/Video Multimedia devices (e.g., media players).
DC Diagnostic Device Diagnostic tools.
E0 Wireless Controller Wireless devices (e.g., Bluetooth controllers).
EF Miscellaneous Devices that do not fit into other classes.
FF Vendor-Specific The device class is vendor-defined, requiring custom drivers.

Special Cases

  1. bDeviceClass = 0x00 (Defined by Interface):

    • The device relies on its interfaces to define functionality.
    • Each interface will have its own bInterfaceClass.
  2. bDeviceClass = 0xFF (Vendor-Specific):

    • The device implements a proprietary protocol.
    • Requires a custom driver provided by the manufacturer.

How bDeviceClass Works with bDeviceSubClass and bDeviceProtocol

  • bDeviceClass: General category of the device.
  • bDeviceSubClass: Subcategory within the class (specific to the bDeviceClass).
  • bDeviceProtocol: Protocol within the subclass (specific to bDeviceClass and bDeviceSubClass).

For example:

  • A Mass Storage device (bDeviceClass = 08) with a subClass of SCSI transparent command set (bDeviceSubClass = 06) and a protocol of Bulk-Only Transport (bDeviceProtocol = 50).

Example: Retrieving bDeviceClass

When you fetch the Device Descriptor, the bDeviceClass field is at offset 4:

Byte Offset Field Value Description
4 bDeviceClass 08 Mass Storage Device.

If you’re working with a USB device and want to parse its descriptors, let me know—I can assist further!


idVendor    ==> VID
idProduct  --> PID

bcdDevice Device revision (e.g., 2.01).

iManufacturer Index of manufacturer string.
iProduct Index of product string. 
iSerialNumber Index of serial number string.
bNumConfigurations Number of configurations. 

USB vendor command

 在 USB 通訊中,Vendor Commands (Vendor-Specific Requests) 是為特定裝置設計的指令,並不屬於 USB 標準規範,而是由廠商自定義的操作方式。這些命令通常用於設備的特殊功能,例如設定裝置參數、更新韌體、或執行自訂功能。

以下是 Vendor Command 的範例與解析:


Vendor Command 格式

Vendor Command 通常透過 Control Transfer 傳送,結構如下:

Field Name Size (Bytes) Description
bmRequestType 1 指定方向、類型(Vendor-Specific),以及接收者(Device/Interface/Endpoint)。
bRequest 1 請求代碼,由廠商自定義,例如 0x010xA0
wValue 2 附加數據,通常用於傳遞參數。
wIndex 2 附加數據,可能代表特定端點或功能。
wLength 2 數據傳輸長度(IN 或 OUT)。
Data Variable 傳輸的實際數據(可選)。

Vendor Command 例子 1: 設置設備參數

假設某裝置支持設定亮度的 Vendor Command:

Field Name Example Value Description
bmRequestType 0x40 Host-to-Device, Vendor-Specific, Device
bRequest 0x01 Vendor Command: Set Brightness
wValue 0x0064 Brightness level (e.g., 100)
wIndex 0x0000 Unused
wLength 0x0000 No data phase

Control Transfer (Setup Stage):

40 01 64 00 00 00 00 00

設備會執行亮度設定為 100。


Vendor Command 例子 2: 韌體版本查詢

假設某設備提供查詢韌體版本的 Vendor Command:

Field Name Example Value Description
bmRequestType 0xC0 Device-to-Host, Vendor-Specific, Device
bRequest 0xA0 Vendor Command: Get Firmware Version
wValue 0x0000 Unused
wIndex 0x0000 Unused
wLength 0x0004 Expect 4 bytes of data

Control Transfer (Setup Stage):

C0 A0 00 00 00 00 04 00

Expected Response (Data Stage):

01 00 02 00

解析:

  • 韌體版本:0x0002.0001

Vendor Command 例子 3: 設備重啟

假設某設備支持重啟的 Vendor Command:

Field Name Example Value Description
bmRequestType 0x40 Host-to-Device, Vendor-Specific, Device
bRequest 0x02 Vendor Command: Reboot Device
wValue 0x0000 Unused
wIndex 0x0000 Unused
wLength 0x0000 No data phase

Control Transfer (Setup Stage):

40 02 00 00 00 00 00 00

設備收到指令後立即重啟。


實作工具

  1. 使用 libusb:

    // Example: Sending a vendor-specific command to set brightness
    uint8_t bmRequestType = 0x40;  // Host-to-Device, Vendor-Specific, Device
    uint8_t bRequest = 0x01;       // Set Brightness Command
    uint16_t wValue = 100;         // Brightness level
    uint16_t wIndex = 0x0000;      // Unused
    uint16_t wLength = 0x0000;     // No data phase
    unsigned char *data = NULL;
    int timeout = 1000;
    
    libusb_control_transfer(handle, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
    
  2. 工具如 USBLyzer 或 Wireshark:

    • 可監控 USB 通訊以反向工程 Vendor Commands。

注意事項

  1. 文檔參考:Vendor Command 的定義通常在裝置的技術文檔中提供。
  2. 安全性:避免對未知裝置執行 Vendor Commands,以免導致不可逆的損壞。
  3. 錯誤處理:對不支援的命令,設備通常會回應 STALL

如果你有特定設備或命令需要協助分析,請提供更多細節!

雲南少數民族武俠小說