libusb數據結構說明

  1. 設備描述符結構
struct libusb_device_descriptor{
u_int8_t blength;
u_int8_t bDescriptorType;
u_int16_t bcdUSB;
u_int8_t  bDeviceClass;
u_int8_t  bDeviceSubClass;
u_int8_t  bDeviceProtocol;
u_int8_t  bMaxPacketSize0;
u_int16_t idVendor;
u_int16_t idProduct;
u_int16_t bcdDevice;
u_int8_t  iManufacturer;
u_int8_t  iProduct;
u_int8_t  iSerialNumber;
u_int8_t  bNumConfigurations;
};
bLength : 描述符大小.固定爲0x12.
bDescriptorType : 設備描述符類型.固定爲0x01 LIBUSB_DT_DEVICE.
bcdUSB : USB 規範的版本號.表示了本設備能適用於那種協議,如2.0=0200,1.1=0110等.
bDeviceClass : 類型代碼(由USB指定)。當它的值是0時,表示所有接口在配置描述符裏,並且所有接口是獨立的。當它的值是1到FEH時,表示不同的接口關聯的。當它的值是0xFF時,它是廠商自己定義的.
bDeviceSubClass : 子類型代碼(由USB分配).如果bDeviceClass值是0,一定要設置爲0.其它情況就跟據USB-IF組織定義的編碼.
bDeviceProtocol : 協議代碼(由USB分配).如果使用USB-IF組織定義的協議,就需要設置這裏的值,否則直接設置爲0。如果廠商自己定義的可以設置爲0xFF.
bMaxPacketSize0 : 端點0最大分組大小(只有8,16,32,64有效).
idVendor : 供應商ID(由USB分配).
idProduct : 產品ID(由廠商分配).由供應商ID和產品ID,就可以讓操作系統加載不同的驅動程序.
bcdDevice : 設備出產編碼.由廠家自行設置.
iManufacturer : 廠商描述符字符串索引.索引到對應的字符串描述符. 爲0則表示沒有.
iProduct : :產品描述符字符串索引.同上.
iSerialNumber : 設備序列號字符串索引.同上.
bNumConfigurations : 可能的配置數.指配置字符串的個數
USB class CODE 詳細說明如下:
Class code             用途           詳細說明
00h                    Device         表示CLASS的信息在接口的描述符中定義
01h                    Interface      Audio   音頻設備
02h                    Both           Communications and CDC Control通信設備
03h                    Interface      HID (Human Interface Device) 人機接口設備
05h                    Interface      Physical 物理設備
06h                    Interface      Image 圖像設備
07h                    Interface      Printer 打印機
08h                    Interface      Mass Storage 大容量存儲
09h                    Device         Hub
0Ah                    Interface      CDC-Data 通信設備
0Bh                    Interface      Smart Card 智能卡
0Dh                    Interface      Content Security 內容安全設備
0Eh                    Interface      Video 視頻設備 
0Fh                    Interface      Personal Healthcare  個人健康設備
10h   Interface      Audio/Video Devices 音頻/視頻設備
DCh                    Both           Diagnostic Device 診斷設備
E0h                    Interface      Wireless Controller 無線控制器(藍牙設備等)
EFh                    Both           Miscellaneous 雜項(ActiveSync,PalmSync,各種協會等)
FEh                    Interface      Application Specific 應用專有規範(固件升級,紅外,USB測試與測量等)
FFh                    Both           Vendor Specific 供應商自定義規範
注:各類設備的class subclass protocol的詳細定義可以參考http://www.usb.org/developers/defined_class

2. 配置描述符

struct libusb_config_descriptor{
u_int8_t blength;
u_int8_t bDescriptorType;
u_int16_t wTotalLength;
u_int8_t bNumInterfaces;
u_int8_t bConfigurationValue;
u_int8_t iConfiguration;
u_int_t bmAttributes;
 u_int8_t MaxPower;

const struct libusb_interface *interface;

const unsigned char *extra;
int extralen;
};

bLength:描述符大小,固定爲0x09.
bDescriptorType:配置描述符類型,固定爲0x02 LIBUSB_DT_CONFIG.
wTotalLength:返回整個數據的長度,指此配置返回的配置描述符,接口描述符以及端點描述符的全部大小.
bNumInterfaces:配置所支持的接口數,指改配置配備的接口數量,也表示該配置下接口描述符數量.
bConfigurationValue:作爲Set Configuration的一個參數選擇配置值.
iConfiguration:用於描述該配置字符串描述符的索引
bmAttributes:供電模式選擇,Bit4-0保留,D7:總線供電,D6:自供電,D5:遠程喚醒。
MaxPower:總線供電的USB設備的最大消耗電流,以2mA爲單位。

struct libusb_interface_descriptor {
u_int8_t  bLength;
u_int8_t  bDescriptorType;
u_int8_t  bInterfaceNumber;
u_int8_t  bAlternateSetting;
u_int8_t  bNumEndpoints;
u_int8_t  bInterfaceClass;
u_int8_t  bInterfaceSubClass;
u_int8_t  bInterfaceProtocol;
u_int8_t  iInterface;

const struct libusb_endpoint_descriptor *endpoint;


const unsigned char *extra;
int extralen;
};
bLength : 描述符大小.固定爲0x09.
bDescriptorType : 接口描述符類型.固定爲0x04 LIBUSB_DT_INTERFACE.
bInterfaceNumber: 該接口的編號.
bAlternateSetting : 用於爲上一個字段選擇可供替換的位置.即備用的接口描述符標號.
bNumEndpoint : 使用的端點數目.端點0除外.
bInterfaceClass : 類型代碼(由USB分配).
bInterfaceSunClass : 子類型代碼(由USB分配).
bInterfaceProtocol : 協議代碼(由USB分配).
iInterface : 字符串描述符的索引
 3. 接口描述符
     接口描述符說明了接口所提供的配置,一個配置所擁有的接口數量通過配置描述符的bNumInterfaces決定


 struct libusb_endpoint_descriptor {
u_int8_t  bLength;
u_int8_t  bDescriptorType;
u_int8_t  bEndpointAddress;
u_int8_t  bmAttributes;
u_int16_t wMaxPacketSize;
u_int8_t  bInterval;
u_int8_t  bRefresh;
u_int8_t  bSynchAddress;


unsigned char *extra;
/* Extra descriptors */
int extra_length;
};

bLength : 描述符大小.固定爲0x07.
bDescriptorType : 接口描述符類型.固定爲0x05 LIBUSB_DT_ENDPOINT.
bEndpointAddress : USB設備的端點地址.Bit7,方向,對於控制端點可以忽略,1/0:IN/OUT.Bit6-4,保留.BIt3-0:端點號.
bmAttributes : 端點屬性.Bit7-2,保留.BIt1-0:00控制,01同步,02批量,03中斷.
wMaxPacketSize : 本端點接收或發送的最大信息包大小.
bInterval : 輪詢數據傳送端點的時間間隔.對於批量傳送和控制傳送的端點忽略.對於同步傳送的端點,必須爲1,對於中斷傳送的端點,範圍爲1-255.

Endpoint 按屬性分有四種類型:
1)控制端點(Control Endpoint):
通常用於 配置設備、獲取設備信息、發送命令到設備、獲取設備的狀態報告等,即傳輸控制信息。
2)中斷端點(Interrupt Endpoint):
當 USB 主機要求設備傳輸數據時,中斷端點就以一個固定的速率來傳送少量的數據。(USB鍵鼠即是此方式)
3)批量端點(Bulk Endpoint):
傳輸大批量數據,確保不丟失數據,但不保證特定時間內完成。(打印機、網絡設備多用此方式)
4)同步端點(Isochronous Endpoint):
傳輸大批量數據,不保證數據是否到達,對傳送延遲非常敏感,可保持一個恆定速率收發實時信息。(音視頻設備多用此方式)

發佈了19 篇原創文章 · 獲贊 30 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章