设备描述符包含有关整个 USB 设备的信息。 This article describes the USB_DEVICE_DESCRIPTOR structure and includes information about how a client driver can send a get-descriptor request to obtain the device descriptor.
每个通用串行总线(USB)设备都必须能够提供包含设备相关信息的单个设备描述符。 The USB_DEVICE_DESCRIPTOR structure describes a device descriptor. Windows 使用该信息来派生各种信息集。 For example, the idVendor and idProduct fields specify vendor and product identifiers, respectively. Windows uses those field values to construct a hardware ID for the device. 查看特定设备的硬件 ID:
- Open Device Manager.
- Right-click on the USB device and select Properties.
- Select the Details tab in the properties dialog box.
- Drop down the Property list.
- Select the Hardware Ids property
The values indicate the hardware IDs ("USB\XXX") that Windows generates.
The bcdUSB field of the USB_DEVICE_DESCRIPTOR structure indicates the version of the USB specification to which the device conforms. 例如,0x0200指示设备按 USB 2.0 规范设计。 The bcdDevice value indicates the device-defined revision number.
The USB driver stack uses bcdDevice, along with idVendor and idProduct, to generate hardware and compatible IDs for the device. You can view those identifiers in Device Manager. 设备描述符还指示设备支持的配置总数。
当设备以高速容量连接到主计算机时,设备可能会报告其设备描述符中的不同信息,而不是当设备以全速容量连接时。 设备不得在连接的生存期内更改设备描述符中包含的信息,包括在电源状态更改期间。
主机通过控制传输获取设备描述符。 在传输中,请求类型为 GET DESCRIPTOR,接收方是设备。 客户端驱动程序可以通过以下两种方式之一启动传输:使用框架 USB 目标设备对象或发送包含请求信息的 URB。
获取设备描述符
只有在创建框架 USB 目标设备对象后,Windows 驱动程序框架(WDF)客户端驱动程序才能获取设备描述符。
A Kernel-Mode Driver Framework (KMDF) driver must obtain a WDFUSBDEVICE handle to the USB target device object by calling WdfUsbTargetDeviceCreate. Typically, a client driver calls WdfUsbTargetDeviceCreate in the driver's EvtDevicePrepareHardware callback implementation. After that, the client driver must call the WdfUsbTargetDeviceGetDeviceDescriptor method. After the call completes, the device descriptor is received in the caller-allocated USB_DEVICE_DESCRIPTOR structure.
A User-Mode Driver Framework (UMDF) driver must query the framework device object for an IWDFUsbTargetDevice pointer and then call the IWDFUsbTargetDevice::RetrieveDescriptor method and specify USB_DEVICE_DESCRIPTOR_TYPE as the descriptor type.
主机还可以通过发送 URB 来获取设备描述符。 此方法仅适用于内核模式驱动程序。 但是,除非驱动程序基于 Windows 驱动程序模型(WDM),否则客户端驱动程序永远不必为此类请求发送 URB。 Such a driver must allocate an URB structure and then call the UsbBuildGetDescriptorRequest macro to specify format the URB for the request. 然后,驱动程序可以通过将 URB 提交到 USB 驱动程序堆栈来发送请求。 有关详细信息,请参阅 如何提交 URB。
此代码示例演示 UsbBuildGetDescriptorRequest 调用,该调用使用相应的 URB 设置 pURB 指向的缓冲区的格式:
UsbBuildGetDescriptorRequest(
pURB, // Points to the URB to be formatted
sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_DEVICE_DESCRIPTOR_TYPE,
0, // Not used for device descriptors
0, // Not used for device descriptors
pDescriptor, // Points to a USB_DEVICE_DESCRIPTOR structure
NULL,
sizeof(USB_DEVICE_DESCRIPTOR),
NULL
);
示例设备描述符
此示例显示了 USB 网络摄像头设备的设备描述符(请参阅 USB 设备布局),通过使用 USBView 应用程序获取:
Device Descriptor:
bcdUSB: 0x0200
bDeviceClass: 0xEF
bDeviceSubClass: 0x02
bDeviceProtocol: 0x01
bMaxPacketSize0: 0x40 (64)
idVendor: 0x045E (Microsoft Corporation)
idProduct: 0x0728
bcdDevice: 0x0100
iManufacturer: 0x01
0x0409: "Microsoft"
iProduct: 0x02
0x0409: "Microsoft LifeCam VX-5000"
0x0409: "Microsoft LifeCam VX-5000"
iSerialNumber: 0x00
bNumConfigurations: 0x01
在前面的示例中,根据 USB 规范版本 2.0 开发设备。 Note the bDeviceClass, bDeviceSubClass, and bDeviceProtocol values. 这些值指示设备包含一个或多个 USB 接口关联描述符,这些描述符可用于对每个函数的多个接口进行分组。 有关详细信息,请参阅 USB 接口关联描述符。
Next, see the value of bMaxPacketSize0. 此值指示默认终结点的最大数据包大小。 此示例设备可以通过其默认终结点传输多达 64 字节的数据。
通常,若要配置设备,客户端驱动程序会在获取设备描述符后获取有关设备中支持的配置的信息。 To determine the number of configurations that the device supports, inspect the bNumConfigurations member of the returned structure. 此设备支持一个配置。 若要获取有关 USB 配置的信息,驱动程序必须获取 USB 配置描述符。