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. 

以RTK UVC  iManufacturer Index (得到的index 是 03 )為例子:

先下 80 06 +[:iManufacturer indx'] + 03 09 04 04 00 
會得到 10 03 47 00  (長度得到 0x10)
再次下 80 06 +[:iManufacturer indx'] + 03 09 04 10 00 
會得到 10 03 47 00 65 00 6e 00 6e 00 72 00 69 00 63 00 --> ASC解出 "Generic"


(HWSerNum)可以是一个字符串嗎?


是的,**USB 设备的硬件序列号(HWSerNum)可以是一个字符串**。

这是一个非常常见和标准的做法。下面为您详细解释:

### 1. 技术规范

USB 规范本身定义了设备可以提供一个 `iSerialNumber` 字符串描述符。这个描述符的内容完全由设备制造商决定,可以包含字母、数字、符号等任何可打印的字符,因此它本质上就是一个字符串。

*   **位置**:这个字符串描述符的索引号存储在设备的**设备描述符(Device Descriptor)** 中的 `iSerialNumber` 字段里。
*   **作用**:操作系统(如 Windows, Linux, macOS)在枚举 USB 设备时,会读取这个字符串,并将其作为该设备的一个唯一标识符(或至少是一个区分同型号设备的标识符)。

### 2. 常见格式

制造商通常会使用有意义的字符串作为序列号,格式多种多样:

*   **纯数字串**: `003241ABC982`,`1234567890`
*   **数字和字母混合**: `SN-12345A`,`CAM-2024-ABCDE`
*   **包含特殊字符**(较少见,但可能): `Device_ID:123-456@XYZ`

### 3. 为什么使用字符串?

*   **灵活性**:字符串格式可以容纳更多信息,例如将生产批次、日期、型号代码等编码进去。
*   **可读性**:字符串形式的序列号对人类来说更容易识别和处理,远比一长串原始的二进制数据要直观。
*   **唯一性**:结合厂商ID(VID)和产品ID(PID),字符串序列号可以有效地将同一型号的两个设备区分开来。这正是操作系统用它来生成**唯一设备实例ID** 的原因。

### 4. 在系统中的体现

当你在操作系统中查看USB设备属性时,看到的“序列号”就是这个字符串。

*   **Windows**: 在设备管理器的“详细信息”选项卡中,选择“设备实例路径”或“硬件ID”等属性,就能看到包含序列号的字符串。
    *   示例设备实例路径: `USB\VID_1234&PID_5678\SN_12345ABCDE`
    *   这里的 `SN_12345ABCDE` 就是从设备读取的字符串序列号。

*   **Linux**: 使用 `lsusb -v` 命令可以列出详细的USB设备信息。在输出中找到你的设备,在其设备描述符中会有一行:
    ```
    iSerial                3 000000123456
    ```
    这表示索引号为3的字符串描述符是序列号,其内容是 `000000123456`。你可以继续在输出中查找索引为3的字符串描述符来确认。

*   **macOS**: 可以使用 `system_profiler SPUSBDataType` 命令来查看USB设备的详细信息,其中就包含序列号。

### 5. 重要注意事项

*   **不是强制要求**:USB 规范并不强制要求设备**必须**提供序列号。如果设备的 `iSerialNumber` 字段为0,则表示该设备没有序列号。这种情况下,操作系统通常会使用其他信息(如端口号)来区分设备,可能导致每次插拔后系统识别的设备标识发生变化。
*   **驱动匹配**:在 Windows 中,驱动的安装和匹配严重依赖包括序列号在内的硬件ID。如果一个设备没有序列号,Windows 可能会为它安装在同一个端口上的每个实例都加载一次驱动,而不是将其识别为同一个设备。

### 总结

完全可以,并且**普遍如此**。USB 设备的硬件序列号在设计上就是一个可读的字符串,它提供了比纯二进制数值更大的灵活性和可读性,是操作系统识别和区分设备的关键信息。

沒有留言:

CAN FD