內(nèi)容原創(chuàng),未經(jīng)本人同意請(qǐng)勿轉(zhuǎn)載。聯(lián)系本人:jianshu_kevin@126.com
PIPE
usb通信的最基本形式是通過(guò)USB設(shè)備里的endpoint,而主機(jī)和endpoit之間的數(shù)據(jù)傳輸就是通過(guò)pipe
通信方式
USB的所有通信都是由host發(fā)起的,總線(xiàn)采用輪詢(xún)方式和設(shè)備進(jìn)行通信
endpoint
端點(diǎn)是有方向的,主機(jī)到從機(jī)成為out端點(diǎn),從機(jī)到主機(jī)成為in端點(diǎn)。從這個(gè)說(shuō)明看出端點(diǎn)是單方向的。(除了0端點(diǎn))
管道通信方式
pipe中的數(shù)據(jù)通信方式有兩種,一種是stream一種是message。message要求進(jìn)出進(jìn)出方向必須要求同一個(gè)管道,默認(rèn)就使用ep0作為message管道
傳輸方式
USB endpiont有四種類(lèi)型,分別對(duì)應(yīng)了不同的數(shù)據(jù)傳輸方式,分別為control transfers控制傳輸、interrupt transfers中斷傳輸、Bluk Data transfers批量傳輸、Isochronous Data Tranfers等時(shí)傳輸
控制傳輸通常用于配置設(shè)備,獲取設(shè)備信息,發(fā)送命令到設(shè)備
邏輯設(shè)備
邏輯設(shè)備就是一系列端點(diǎn)的組合,邏輯設(shè)備與主機(jī)之間的通信發(fā)生在一個(gè)主機(jī)的緩沖區(qū)和設(shè)備的一個(gè)端點(diǎn)之間。
---------------------------
host
---------------------------
| buf | buf | buf |
---------------------------
[] [] []
[] [] []
[] [] []------->pipe
[] [] []
[] [] []
---------------------------
{ |ep| } {| ep| |ep| }-->interface
---------------------------
logic device
---------------------------
interface
一個(gè)邏輯設(shè)備可能包含若干個(gè)接口,每個(gè)接口包含1個(gè)或多個(gè)端點(diǎn)。每個(gè)接口表示一種功能。內(nèi)核里一個(gè)接口對(duì)應(yīng)一個(gè)驅(qū)動(dòng)程序。例如usb揚(yáng)聲器就包含一個(gè)鍵盤(pán)接口和一個(gè)音頻流接口
gadget
class協(xié)議
USB協(xié)議中除了定義一些通用軟硬件電氣特性,還包含各種各樣的class協(xié)議,用來(lái)為不同的功能定義各自的標(biāo)準(zhǔn)接口和具體總線(xiàn)上的數(shù)據(jù)交互內(nèi)容和格式。例如u盤(pán)的Mass storage class、通用數(shù)據(jù)交換CDC class
為何USB都在PCI設(shè)備中
訪(fǎng)問(wèn)設(shè)備文件時(shí)經(jīng)??吹絬sb在pci目錄中,這是因?yàn)橥ǔsb的root hub包含在了pci設(shè)備中。
usbfs
usbfs提供了用戶(hù)空間可以訪(fǎng)問(wèn)usb硬件設(shè)備的接口
設(shè)備描述符、配置描述符、接口描述符、端點(diǎn)描述符
協(xié)議里規(guī)定一個(gè)usb設(shè)備必須支持這四大描述符。字符串描述符是可選的
/* host-side wrapper for one interface setting's parsed descriptors */
struct usb_host_interface {
struct usb_interface_descriptor desc; //接口描述符
int extralen;
unsigned char *extra; /* Extra descriptors */ //其他描述符
/* array of desc.bNumEndpoint endpoints associated with this
* interface setting. these will be in no particular order.
*/
struct usb_host_endpoint *endpoint; //端點(diǎn)描述符
char *string; /* iInterface string, if present */ //用來(lái)保存從設(shè)備上讀取出來(lái)的字符串描述符
};
/* USB_DT_INTERFACE: Interface descriptor */
struct usb_interface_descriptor {
__u8 bLength; //==9
__u8 bDescriptorType; //==4
__u8 bInterfaceNumber;
__u8 bAlternateSetting;
__u8 bNumEndpoints;
__u8 bInterfaceClass;
__u8 bInterfaceSubClass;
__u8 bInterfaceProtocol;
__u8 iInterface; //對(duì)應(yīng)字符串描述符的索引值
} __attribute__ ((packed));
| bDescriptorType | Value |
|---|---|
| DEVICE | 1 |
| CONFIGURATION | 2 |
| STRING | 3 |
| INTERFACE | 4 |
| ENDPOINT | 5 |
| DEVICE_QUALIFIER | 6 |
| OHTER_SPEED_CONFIGURATION | 7 |
| INTERFACE_POWER | 8 |
struct usb_host_endpoint {
struct usb_endpoint_descriptor desc;
struct usb_ss_ep_comp_descriptor ss_ep_comp;
struct list_head urb_list;
void *hcpriv;
/****************提供給sysfs用的
wy@am:/proc$ ls /sys/bus/usb/devices/usb1/ep_00/
bEndpointAddress bInterval bLength bmAttributes direction interval power type uevent wMaxPacketSize
上面這個(gè)文件就是通過(guò)usb_create_ep_files創(chuàng)建的
*/
struct ep_device *ep_dev; /* For sysfs info */
unsigned char *extra; /* Extra descriptors */
int extralen;
int enabled;
};
/* USB_DT_ENDPOINT: Endpoint descriptor */
struct usb_endpoint_descriptor {
__u8 bLength; //==7/9
__u8 bDescriptorType; //==5
__u8 bEndpointAddress; //bit7 表示方向,bit0-3表示端點(diǎn)號(hào)
/*bit1-0 表示傳輸類(lèi)型,
00 控制
01 iosh( 等時(shí))
10 批量
11 中斷
*/
__u8 bmAttributes;
//最大長(zhǎng)度
__le16 wMaxPacketSize;
__u8 bInterval;
///////////////////////////下面這兩個(gè)字段只在audio設(shè)備中有
/* NOTE: these two are _only_ in audio endpoints. */
/* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
__u8 bRefresh;
__u8 bSynchAddress;
} __attribute__ ((packed));
//配置描述符
struct usb_config_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__le16 wTotalLength;
__u8 bNumInterfaces;
__u8 bConfigurationValue; //表明了正在被使用的USB配置,雖然USB設(shè)備可能包含多個(gè)配置,但是同一時(shí)刻只能又一個(gè)配置生效
__u8 iConfiguration; //配置描述符的字符串描述索引值
__u8 bmAttributes;
__u8 bMaxPower;
} __attribute__ ((packed));
設(shè)置
一個(gè)接口包含多個(gè)設(shè)置,每個(gè)設(shè)置對(duì)應(yīng)不同的描述符
class
每個(gè)接口都包含一個(gè)class,class下面有分subclass。usb協(xié)議里為每一種class、subclass、protocol定義了一個(gè)數(shù)值。沒(méi)一個(gè)接口或者設(shè)備都屬于一個(gè)class。
設(shè)備class和接口class有何區(qū)別?
USB設(shè)備號(hào)
單純的USB主設(shè)備號(hào)有兩個(gè)USB_MAJOR,USB_DEVICE_MAJOR。其中USB_DEVICE_MAJOR是為usbfs而生的。USB_MAJOR才是真正的USB設(shè)備主設(shè)備號(hào),但是一般的USB設(shè)備都是模擬成其他的設(shè)備,比如USB鍵盤(pán)、硬盤(pán)等。這類(lèi)設(shè)備和其他子系統(tǒng)關(guān)聯(lián),那么設(shè)備號(hào)就不再使用USB_MARJOR設(shè)備號(hào)了,屬于哪個(gè)子系統(tǒng)就使用該子系統(tǒng)的設(shè)備號(hào)。例如USB硬盤(pán)的主設(shè)備號(hào)是8。這里的USB設(shè)備準(zhǔn)確的說(shuō)應(yīng)該是USB接口。
掛起狀態(tài)
協(xié)議里規(guī)定所有的USB設(shè)備都需要支持掛起,已達(dá)到省電的目的,在設(shè)備指定時(shí)間內(nèi)(3ms)沒(méi)有數(shù)據(jù)傳輸,就進(jìn)入掛起狀態(tài)。當(dāng)收到一個(gè)non-idle信號(hào),就會(huì)被喚醒。