Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc

上传人:scccc 文档编号:13867876 上传时间:2022-01-25 格式:DOC 页数:18 大小:335.50KB
返回 下载 相关 举报
Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc_第1页
第1页 / 共18页
Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc_第2页
第2页 / 共18页
Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc_第3页
第3页 / 共18页
Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc_第4页
第4页 / 共18页
Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc》由会员分享,可在线阅读,更多相关《Linux内核USB驱动架构:USB卷五_USB设备枚举过程.doc(18页珍藏版)》请在三一文库上搜索。

1、LK 版本:2.635.32013 年 1 月 14 USB卷五USB设备的枚举过程任务目标:分析整理USB设备枚举过程:本文要点:1、USB设备枚举数据结构:2、USB设备枚举流程;硬件框图:1数据结构:u$b_deT*ice u$b_host_config usb_config_descriptor bLensth bDescriptorTpe wTotalLength bConfigurat)onvalu bNumlntexfices usb_interabce_caclie d*$cnum.altsettingmtf_cacheO何个接 Icache altsettmg 0endpoi

2、nt毎个炯点 Uib_en4omt_descnptor )usb host encomtObLenthbDescnptorTjpebEn4omtAddressbmAttnVutesblnter.al de$c每个接ntruib_li%t3成员说明:stmct usb_device stnict device dev;stnict usb_deice_descriptor desciiptor;stnict usb_host_config *config;stnict usb host config *actconfig;chai- *rawdescnptors;char *product;ch

3、ar manufacturer;char *senal;;/ generic dexice interface / descriptor*/ all of the configs */the active configuration /* raw descriptors for each config /iProduct string, if present / /*iManufactxu er string, if present / /iSenalNumber strmg.if present /4#stinct usb dex ice descriptor _u8 bLength;_u8

4、 bDescnptorType; _lel6bcdUSB;_u8 bDenceClass;_u8 bDeviceSubClass; _u8 bDe viceProtocol;_u8 bMaxPacketSizeO; el6 idendor;_lel6 ldProduct;_u8 bNumConfigiuations;bLength:描述符的长度祁define USB_DT_DEVICE_SIZE1&bDescnptorTpe:值为 USB DT DEMCE: 0x01bcdUSB: USB spec的版本号,如果足2.0版本,值为0x0200.如圧1.1版本.则值、)J0x0110.bDenc

5、eClass 分别指每个设备屈于一个Class-然Class F面又分了DenceSubClass 卜 SubClass. SubClass 下Illi 乂做备种设的不冋的通bDexnceProtocol J 信协议继续细分idendor设备的厂商和ID号.在USB设备与驱动匹配时会用ldProductJ到此成员参数1bNhxPacketSizeO:端点0 次町以处理的杲大字节数。11Q:为什么要放在设备描述符里呢?1A:由于它门己没有一个描述符.而毎个设备又都有这么一个端点1所以这个信息被保存在了设佑描述符里梵中.这个值只能肚1&1632或64这四音之一.高速模式值只能为64.低速模式1只能

6、为8,对于中速.可以为8, 16, 32, 64,单位字节。1bNumConfigiirations :设备肖前速度模式卜支持的配置数域1111包括两份:一份来门USB接口驱动中的module.usbmap文件中另1份在USB外设中由枚举时获収 设备类代码及例子请见下表 哀6、设备的类别(bDeviceClass)W 1 八 E 种J) / 按口描述符中15供类的值hB亍1 11W 丁河I 巾吃 tri w w *T.X aJr1 4t wF wr1 _集钱器类例子:Base Class 09h (Hub)This base class is defined for devices that

7、are USB hubs i USB specification. That specification defines the complet values are reserved These class codes can only be usedBase ClassSubClassProtocolMeaning09hOOhOOhFull speed HubOlhHi speed hub with single TT02hHi speed hub with multiple TTs#define USB_NIAXINTERFACES 32#define USB_MAXIADS(USB_N

8、IAXINTERFACES/2)stiuct usb_host_config struct usb_config_descnptor desc;char *stnng;stnict usb mterface assoc desciiptor *mtfassocUSB_MAXIADS; stnict usb_mterface *interfaceUSB_MAXINTERFACES;stnict usb_mterface_cache *intf_cacheUSB_MAXETERFACES; unsigned chai* *extia;mt extralen;;mterfacel:表示与当前配置关联

9、的所有接口:只有在当前配呂被激活时.数组才能存在。 数组成员足与usb驱动相配对的数据结构:mtf_cache:表示当前配呂的所有接口信息:即使当前配程为处于激活态,数组仍然存 在.其生命周期伴随着usb_deviceII存在主要用丁发现新设备后,对设备进行枚举:stmct usb_config_descnptor _u8 bLengtli;_u8 bDescnptorType; e 16 wTotalLength;u8_u8_u8_u8u8bNumlnterfaces; bConfigurationValue; iCoiifigiiration; bmAttnbutes; bMaxPower

10、;bLength :值为 9define USB_DT_CONFIG_SIZE 9bDescnptorTpe:值为 0x02 define USBDTCONFIG 0x02 wTotalLength:使用GET_DESCRIPTOR请隶从浚备中获得配且描述符信息.返回的数拯 长度,也就於对包括配置描述符、接口描述将.端点描述符等进行统计bConfigurationValue:対尸拥自多个配昼的设备来说,假如想改变正任使用的配呂.町以 使用 SETCONFIGURAnON 请求.bConfigurationXalue 指明 了将 要激活哪不配呂,注意,设备可以有多个配置,但同一时间只能有 一个配

11、置被激活binAttributess衣示配置的某叫特点.bit7必须程lbit6表示self-powered.最大供电500mA b”5表示支持远程唤醒.bit4表示banery powered/ number of alternate settings /* reference coimter / llnteiiace stnng, if present /* Extra descnptors /stmct usb interface caclie unsigned num_altsetting; stnict kref ref;stnict usb_host_mterface altset

12、tingO; ;struct usb_host_interface stnict usb_mterface_descnptor desc; stnict usb_host_endpomt *endpomt; char *struig;unsigned char *extra; mt extralen;stnict usb interface descnptor _u8bLengtli;_u8bDescnptorType;_u8blnterfaceNmnber;_u8bAltemateSetting;_u8bNumEndpoints;_u8blnteiiaceClass;_u8blnteifac

13、eSubClass;_u8blnteifaceProtocol;_u8llnterface;bLength:值为 9,笄define USB_DT_INTERFACE_SIZE9bDescriptorT)pe:值为 0x04 define USB_DT_ZtERFACE 0x04 blnterfaceNumber:接口号.毎个设条可说Ilf多个接口.这个值就是它们的索引值 起始值从0开始。bAltemateSening:接口使用的兄哪个可选设岂.协议规定:默认使用的设捏总为0号 设置blnterfaceClass blnteifaceSubClass blnterfaceProtocol Ji

14、lnterfoce:接口对应的字符串描述符的索引值接口类代码及例子请见卜表7#音频类例子如下:Base Class 10h (Audio/Video Devices)The USB AudioA/ideo (AV) Device Class Definition describes the methods used to communicate with devices or functions embedded in comoosite devices that are used to manipulate audio, video, voice, and all image and sou

15、nd-rela:ed functionality. That specification defines the usable set of SubClass anc Protocol values Values outside of that defined spec are reserved These class codes can only be used n interface Descriptors.Base ClassSubClassProtocolMeaningOlhOOhAudio/Video Device - AVControl interface10b02hOOhAudi

16、o/Video Dewce - AVData Video Streaming interface;03hOOhAudio/Video Device 一 AVData Audio Streaming Interfacestiuct usb_host_endpoint stnic? usb_endpomt_descriptorstnict usb_ss_ep_comp_descnptor ss_ep_comp;stnict list_headvoidstnict ep_denceurb_list;*hcpnv;ep_dev;/ For sysfs info /int enabled; ;stnic

17、t usb_endpoint_descnptor _u8 bLength;_u8 bDescnptorType;_u8 bEndpointAddiess; _u8 bmAttnbutes;lel6 wMaxPacketSize;#u8 bliiteival;u8 bRefresh;u8 bSynchAddiess;bLength: #defiue USB_DT_ENDPOINT_SIZE 7 或祥define USB住NDPOINTJXuDIO_SIZE9 肓而多了两个字节,那是针对音颍设盂扩展的。一 -bDescriptorT)pe: define USB_DT_ENDPOINT 0x05b

18、EndpomtAddress: bil0bH3裘示陌就是端点号,使用OxOf和它相与就可以得 到端点号.起始号为0.另外bil7表示方向.0为输出 1 为输入,输入端点0-15.输出端点0-15bmAttributes: bitO和bit共同Qj传输类型,00表示控制传01表示第时 传输 10农示批虽传输 11表示中断传綸:wMaxPacketSize:端点一次可以处理的报大字节数.010位表示数据包的长I 度.不同的传输类型,有不同的要求.详细请见下表 Q:它与上面的wMaxPacketSizeO有何X别?A: wMaxPacketSizeO表示瑞点0 次可吸处理的最大字节 数.而wMaxP

19、acketSize表示除0号螺点一次可以处理 的最大字节数。blntenal: USB足轮询式总线.这个值表示了主机轮询这个端点的时间间隔。 它只对等时和中断传输有效.单位为 对于全速/高速的等时传输端点它的值范甬为1-16: 对于全速/低速的中断传输端点它的値范甬为1-255: 对于高速的中断传输端点.它的值范闱为1-16:控制传输:(High-speed: 64 bytescomplete赋值为hub_irq().提交了一个中断urb给主机 控制器后,主机控制器会定期询问Hub,每当Hub端II上检测有一个usb设备插入时,就 会进行USB设备的枚举,获取该设备的当前速度和各描述符等。r

20、usb_alloc_dev()I usb_new_device0仃 USB 新设备插入=hub_eTentsO=hub_port_coiinect_changeO= f usb_enumerate_deice()I device_add()usb enumerate device9#static int usb_enumerate_deice(stnict usb device *udev)usb_add_hcd():hcd-authorized_default = hcd-xireless?O : 1: usblloc_dev():if (udev-config = NULL) usb ge

21、t configuration(udev);if (root_hub)dex-autliorized = 1;else de-autliorized = iub_hcd-authorized_default; deerusb = usb_bu$_is_wusb(bus)? 1 : 0;;if (udev-wusb = 1 & udev-authonzed = 0) udev-product = kstidup(Mn/a (unautliorized), GFP KERNEL); udev-manufactxirer = kstrdupCn/a (unauthorized), GFP KERNE

22、L); udev-senal = kstrdup(”n/a (unauthonzed)H, GFP KERNEL); else udev-product = usb_cache_strmg(ude; udev-descnptor.iProduct); ude-manufacturer = usb_cache_stnng(udev, udev-desci*iptor. 1Nlanufactiuer); udev-senal = usb_caclie_stnng(udev, udev-descnptor.iSenalNi.imber);usb_ennmerate_derice_otg(udev);

23、#define USB_NIAXCONFIG8int usb_get_configuration(stnict usb_device *dev)stnict device *ddev = &dev-dev;int ncfg = dev-descnptor.bNnmConfigurations;由 hub_port_init0获取设备描述符中得到 unsigned int cfgno, length; unsigned char *bigbuffer;stnict usb_config_descnptor *desc;cfgno = 0;if (ncfg USBMAXCONFIG) dex-de

24、scnptor.bNumCoiifigurations = ncfg = USBMAXCONFIG;length = ncfg * sizeof(struct usb_host_config);dev-config = kzalloc(length,GFP_KERNEL);length = ncfg sizeoftchai* *);dev-rawdescriptors = kzallocQength, GFP KERNEL);desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);/USB_DT_CONFIG_SIZE=9 for (; cfgiio nc

25、fg; cfgno+) result = iisb_get_descnptor(dev, USB DT CONFIG, cfgno,desc, USB DT CONHG SIZE);/USB DT CONFIG=0x02int usb_get descriptor(stnict usb_deice adex; unsigned char type、 unsigned char index. void *buf mt size)int i;mt result.mein$et(buf, 0. size):for(i = 0;i3;+i) result = usb_control_msg(de; u

26、sb_rcvctr)pipe(dev 0). USB_REQ_GET_DESCRIPTOR. USB_DIR_IN. (type 8)十 index. 0, buf, size,USB_CTRL_GET_TIN 任 OUT);return result;/USB_CTRL GET TIMEOUT 5000 由于二些不守规矩的生成设务厂商的原因,一次请求还不一定能成 功拿到设估描述符,所冇多试几次.这里为3次10if (result descnptor.bNumConfigxuations = cfeiio; break; else if (result vTotalLeugtli),USB_D

27、T_CONFIG_SIZE); bigbufTer = kinalloc(length5 );result = usb_get_descnptor(dev, USB_DT_CONFIG、cfgiio,bigbuffer, length);if (result rawdescriptorscfgno = bigbuffer;usbjarse configuration(dev, cfgno, &dev-configcfgno, bigbuffei; length);result = 0;eiT:kfree(desc);out_not_authonzed: dev-descnptor.bNuniC

28、oiifiguratioiis = cfgno;return result;-个配置包含的所有相关描述符bigbuffer对于每个配置都会进行如下解析:原因:由GET.DESCRIPTOR请求获取的数据是包含一个配置里所有柑关描述符内容的 一堆未拆分的数据,故下面将进行拆分和解析。/USBMAXINTERFACES 32/USBIAXLTSETTING 128/USB_QUIRK_HONOR_BNUMINTERFACES 0x00000020/USbiAXETERFAcEs 32/USBMAXIADS (USE_MAXINTERFACES/2)static int usb_parse_conf

29、iguration(stnict usb_device *dev,mt cfgidx,strnct usbjiost config *config, unsigned cliar buffer, int size) 一 strnct device *ddev = &dev-dev;unsigned chai- *bufier0 = buffer;strnct usb mterface cache mtfc;unsigned char *buffer2;mt size2;strnct usb_descnptor_header *header;u8 mumsUsB_NIA文INTERFACES;u

30、8 naltsUSBlAXINTERFACES;memcpyX&configdesc、buffer, USB_DT_CONFIG_SIZE);if (config-desc.bDescnptorType !=USB DT CONFIG )|config-desc.bLength desc bConfigurationA alue;/ 前配置的配置号buffer += config-desc.bLength;size -= config-desc bLength;nmtf = nintfong = config-desc bNxuiiIiiterfaces;/前配置支持的接I 丨数if (nui

31、tf USB_NIAXINTERFACES) mntf= U 込MAXINTERFACES; 一n = 0;for (buffer? = buffer, size2 = size);size2 0;(buffer? += header-bLength, size2 -= header-bLength) if (size2 v sizeoftstnict usb_descnptor_header) break;header = (struct usb_descnptor_header *) bufier2;if (header-bLengtli size?) | (header-bLength

32、bDescnptorType = USB DT INTERFACE) struct usb_interface_descnptor *d;mt mum;d = (struct usb_interface_desciiptor ) header;if(d-bLengtlibIntei*faceNumber,if (devquirks & USB QUIRK HONOR BNUMINTERFACES) & n = nintfong) continue;if (miun = nintfong) dev_wam(ddev, nconfig %d has ail invalid H Hinterface

33、 muiibe匚 d but max is %dn役c龟no、mum、mntf_ong -1);for (i = 0; 1 n; +i) if (inumsi = mum)break;if(naltsi255)+naltsi; else if(n bDescnptorType = USB_DT_INTERFACE_ASSOCIATION) if (iad_num = USB_MAXIADS) 一dev_wam(ddev, nfouiid more Inteiiace H MAssociation Descnptors H Hthan allocated for m M configuratio

34、n %d cfgiio); else config-intf_associad_mun=(stinct usb_inteiiace_assoc_descnptor*)header; iad_numi; else if (header-bDescriptorType = USB DT DEVICE | header-bDescnptoiType = U SB_DT_CONFIG) devwamCddev, config %d contains an unexpected descriptor of typeOx%X, skippingnH, cfgiio, header-bDescnptorTp

35、e);size = buffer? buffer;config-desc.wTotalLength = q)u_to_le 16(buffer2 - bufferO);configdesc bNumlnteiiaces = nintf = n;for (1 = 0; 1 USB_MAXALTSETTING) naltsi =j = USB_MAXALTSETTING; len = sizeof(*mtfc) + sizeof(stnict usb_host_interface) * j; config-intf_cachei = mtfc = kzalloc(len,GFP_KERNEL);c

36、onfig-extia = buffer;i = find next descriptoi (buffer, size,USB_DT_INTERFACE,USB_DT_INTERFACE, &n);static int find_next_descriptor(un$igned char Fuffiw; int size, int dtl, mt dt2. int *num_skipped) _struct usb_descriptor_header sh.int n = 0;unsigned char *buffer0 = bufier;while (size 0) h = (stnict

37、u$b_descriptor_header *) buffer;if (h-bDe$cnptorlpe = dtl h-bDescnptorTpe = dt2) breakbuffer += li-bLength:size h-bLength;-KU;if (nuni_skipped) *num_skipped = n: retuin buffer bufferO.config-extialen = i; buffer += i; size =i;while (size 0) retval = usb_parse interfaceCddev, cfgiio, config, buffer,

38、size, iliums, nalts); if (retval bLength;size =d-bLength;if(d-bLengtli bIiiterfaceNumber;for (i = 0; i desc.bNmiiIiiterfaces;卄i) if (nnunsi = inum) intfc = con fig-intfcache 1;break;if (fmtfc | mtfc-nuin_altsetting = naltsi)goto skip_to_next_interface_descriptor;/超IB 分配的内存asmun = d-bAltemateSettmg;for (i = 0, alt = &iutfc-altsettingO); i num_altsettmg;(卄i, +alt) if (alt-desc.bAltemateSetting = asnxun) goto skip_to_next_mterface_descnptor;+intfc-mun_altsettmg;memcpy(&alt-de

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1