使枚舉成功的USB設備成爲WINUSB設備

《使枚舉成功的USB設備成爲WINUSB設備》

1.       將設備枚舉爲WINUSB設備

1.1  使設備能獲取字符串描述符

1、  確保devicedescriptor中字符串index不爲0。如圖所示,紅色框中部分不能設置爲0,若設置爲0則Host不會發送獲取字符串的命令。

uint8_tUSBDeviceDesc[USB_LEN_DEV_DESC] =

{

0x12,                       /*bLength: 18bit*/

USB_DESC_TYPE_DEVICE,       /*bDescriptorType: 0x02*/

0x00,                       /*bcdUSB */

0x02,

0x00,                       /*bDeviceClass*/

0x00,                       /*bDeviceSubClass*/

0x00,                       /*bDeviceProtocol*/

USB_MAX_EP0_SIZE,          /*bMaxPacketSize: 64bit*/

0x37,          /*低位idVendor*/

0x05,          /*高位idVendor*/

0x21,          /*低位idVendor*/

0x43,          /*高位idVendor*/

0x00,                       /*bcdDevice rel. 2.00*/

0x02,

USBD_IDX_MFC_STR,           /*Index of manufacturer  string USBD_IDX_MFC_STR*/

USBD_IDX_PRODUCT_STR,       /*Index of product stringUSBD_IDX_PRODUCT_STR*/

USBD_IDX_SERIAL_STR,        /*Index of serial number stringUSBD_IDX_SERIAL_STR*/

USBD_MAX_NUM_CONFIGURATION  /*bNumConfigurations    固定爲1,因爲只有一個配置描述符*/

};

其中:

#define  USBD_IDX_MFC_STR                       0x01

#define  USBD_IDX_PRODUCT_STR                                           0x02

#define  USBD_IDX_SERIAL_STR                     0x03

 

2、  根據索引值發送字符串描述符

1、  判斷獲取的標準命令wValue字段部分低8位。

2、  發送字符串描述符。其格式如下表:

其中bLength=字符串描述符的長度爲原字符串長度*2+2,bDescriptorType固定爲0x03,bString中的內容是unicode編碼(就是在ASCII碼前面加上0x00即可)。

例如:若想要的字符串爲"LSQ",那麼其字符串描述符應爲:

Uint8_tUSBManufactoryString[USB_LEN_MAU_STRING]=

{

USB_LEN_MAU_STRING,         // 這裏是0x08

           0x03,

           0x4C,                                             //L

           0x00,

           0x53,                                              //S

           0x00,

           0x51,                                              //Q

           0x00

}

1.2  處理獲取操作系統字符串描述符的標準命令

當上文提到的字符串描述符正確上傳到主機上時(可以用USB monitor)檢測到。此時Host會發送獲取操作系統字符串描述符(OS String Descriptor)。它包含OS string descriptor和OS feature descriptors。此時按照下文所述步驟操作:

1、主機發送要求獲取索引值爲0XEE的字符串(即OSString Descriptor)的命令。其標準請求命令格式爲:

bmRequestType

bRequest

wValue

wIndex

wLength

Data

1000 0000B

GET_DESCRIPTOR

0x03EE

0x0000

0x12

Returned String

                  其中:

1、  GET_DESCRIPTOR爲0x03。

2、設備發送OS StringDescriptor。

         1、OS String Descriptor格式。例如:

Field

Length (Bytes)

Value

Description

bLength

1

0x12

Length of the descriptor

bDescriptorType

1

0x03

Descriptor type

qwSignature

14

‘MSFT100’

Signature field

bMS_VendorCode

1

Vendor-specific

Vendor code

bPad

1

0x00

Pad field

         其中:

1、1.0版本的OS StringDescriptor的qwSignature字段必須設爲‘MSFT100’(0x4D00 0x5300 0x4600 0x5400 0x3100 0x3000 0x3000)。

2、bMS_VendorCode字段是自定義數字。

                  例如:

                   uint8_tUSBD_OS_StringDesc[USB_LEN_OS_STRING_DESC]=

{

         USB_LEN_OS_STRING_DESC, // 0X12

         USB_DESC_TYPE_DEVICE,      // 0X03

         0x4D,                                             // M

         0x00,

         0x53,                                              // S

         0x00,

         0x46,                                              // F

         0x00,                          

         0x54,                                              // T

         0x00,

         0x31,                                              // 1

         0x00,

         0x30,                                              // 0

         0x00,

         0x30,                                              // 0

         0x00,

         0x01,                                              // 自定義數字即可

         0x00

};

         3、Host發送要求獲取OS FeatureDescriptor的標準命令請求,SETUP包內容爲:

bmRequestType

bRequest

wValue

wIndex

wLength

Data

1100 0000B

GET_MS_DESCRIPTOR

Interface

Feature Index

Length

Returned Descriptor

         其中:

1、  GET_MS_DESCRIPTOR爲OS String Descriptor中的bMS_VendorCode字段。

3、  設備發送OS FeatureDescriptor。

1、  OS Feature Descriptor格式爲:

OS FeatureDescriptor包含兩個部分:

                                    i.             Header Section

Offset

Field

Size

(bytes)

Type

Description

0

dwLength

4

DWORD

The length, in bytes, of the complete extended compat ID descriptor

4

bcdVersion

2

BCD

The descriptor’s version number, in binary coded decimal (BCD) format

6

wIndex

2

WORD

An index that identifies the particular OS feature descriptor

8

bCount

1

BYTE

The number of custom property sections

9

RESERVED

7

BYTEs

Reserved

其中:

1、  若操作系統描述符版本爲1.00,那麼bcdVersion固定爲0x0100。

2、  wIndex設定爲0x04.

3、  bCount表示有幾個Function Section。

                                  ii.             Function Section

Offset

Field

Size

(bytes)

Type

Description

0

bFirstInterfaceNumber

1

BYTE

The interface or function number

1

RESERVED

1

BYTE

Reserved

2

compatibleID

8

BYTEs

The function’s compatible ID

10

subCompatibleID

8

BYTEs

The function’s subcompatible ID

18

RESERVED

6

BYTEs

Reserved

Note: The offsets in this table are given from the beginning of the section. To calculate the offset of a field from the beginning of the entire descriptor, add the length of the header section and the lengths of any function sections that precede this section to the value in the table.

                                     其中:

1、  bFirstInterfaceNumber字段是端口號,這裏我們設爲0。

2、  第一個RESERVED部分爲0x01,第二個RESERVED部分全爲NULLS(即0x00)。

3、  compatibleID字段爲兼容性ID,這裏我們要設爲WINUSB。不夠8字節的時候需要用NULL補齊8字節。

4、  subCompatibleID字段在這裏我們可以設爲8個NULL。

例如:

uint8_tUSB_OS_FEATURE_STRING[USB_COMPID_LENGTH]=

{

         // header 9bit

         USB_COMPID_LENGTH,

         0x00,

         0x00,

         0x00,

         0x00,                           //低位bcdVersion

         0x01,                           //高位bcdVersion

         0x04,                           //index of compat ID descriptors

         0x00,

         0x01,                           //1個function

         // reserved 7bit

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

 

         // function

         // 2bit

         0x00,                           //端點0

         0X01,

         // compatibleID 8bit

         0x57,                           //W

         0x49,                           //I

         0x4E,                           //N

         0X55,                           //U 

         0x53,                           //S  

         0x42,                           //B

         0x00,

         0x00,

         // subCompatibleID 8bit

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         // RESERVED 6bit

         0x00,

         0x00,

         0x00,

         0x00,

         0x00,

         0x00

};

2.       獲取操作系統描述符相關文檔

1、  從這個網址上獲取:【https://msdn.microsoft.com/library/windows/hardware/gg463179

2、  如圖點擊“我接受”後會在右邊彈出“下載”按鈕。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章