📄 linux设备驱动之usb主机控制器驱动分析-(8)_linux技术文章_linux_操作系统8.htm
字号:
udev->manufacturer = kstrdup("n/a (unauthorized)",
GFP_KERNEL);<BR> udev->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL);<BR>
}<BR> else
{<BR>
//usb_cache_string:会将其关的字串存进一个缓冲,用户空间如果要取设备信息的话<BR>
//只要从缓存区取就可以了<BR> /* read the
standard strings and cache them if present
*/<BR> udev->product =
usb_cache_string(udev,
udev->descriptor.iProduct);<BR>
udev->manufacturer =
usb_cache_string(udev,<BR>
udev->descriptor.iManufacturer);<BR>
udev->serial = usb_cache_string(udev,
udev->descriptor.iSerialNumber);<BR>
}<BR> //OTG:
On-The-GO.表示设备有主机控制器的功能<BR> err =
usb_configure_device_otg(udev);<BR>fail:<BR> return
err;<BR>}<BR>首先解释一下CONFIG_USB_OTG的配置选项.一般来说,系统中只能有一个主机控制器.但有时候设备也可以带一个host
control的功能.举个例子,数码相机.它接在PC上,做为一般的USB设备使用.它也可以连接在打印上直接打印,这时就会做会一个HC使用.<BR>关于OTG的选择编译代码,这里不做深入研究,忽略掉.<BR>其次要解释的是关于usb_cache_string()的操作.这个函数在取字串的时候还会将字符信息保存到一个缓存区.这样一些读USB信息的工具,就只要从指定的缓存区里取值就可以了.<BR>重点放在usb_get_configuration()函数上.这个函数很烦锁.在分析之前.先来了解一下相关的部份.<BR>USB设备有时候会用做多种用途.比如上面的一个例如.数码相机中的USB,可以用做视频存储,也可以当做U盘来使用.那做为驱动程序.它必须要知道设备有多少种功能.在USB2.0
spec中,用配置表示功能.也就是说,对于上在的例子来说,数码相机的USB设备至少应该有两个配置,一个是视频存储的配置,另外的是U盘的配置.由驱动程序来决定应用哪种配置来使用对应的功能.<BR>接口是USB提供的单元组件.因此,有可能一个配置要使用到多个接口,也有可能一个接口也被多个配置使用的情况,不过不使用接口的配置是不存在的.<BR>根据USB的spec有关设备的检测过程中描述,USB控制器先取得设备描述符,这个描述符里包含了配置的个数.然后再以长度9做为参数去取设备配置描述符头部,这个描述符里包含了描述符的实际长际.最后再以实际长度做参数去取完整的配置描述符.取得的配置描述符不仅包含配置描述符信息,还包括了接口信息和接口所使用的端口信息.<BR>将代码中的有关数据结构如下所示 :<BR><IMG
src="linux设备驱动之USB主机控制器驱动分析-(8)_Linux技术文章_Linux_操作系统8.files/080909094714.jpg"
width=500 onload="javascript:if(this.width>500)this.width=500;"
border=0><BR> <BR>大概说一下,usb_dev中的config数组对应每一项配置.config数组的数据结构为struct
usb_host_config.这个数据结构中又包含Inft_cache[
]数组,这个数组用来表示存放接口信息.由于一个接口可能属于同一配置的不同设置,用接口描述符的bAlternateSetting字段来区别接口所属的接口描述符.所以在inft_cache[]对应的usb_host_cache中又有一个扩展项来存放每一个接口描述符.<BR> <BR>以注释的方式列出usb_get_configuration(),就不做详细分析了,结合上面的说明和代码中的注释来分析这段代码应该没什么问题了.如下:<BR>int
usb_get_configuration(struct usb_device *dev)<BR>{<BR>
struct device *ddev = &dev->dev;<BR> int ncfg =
dev->descriptor.bNumConfigurations;<BR> int result =
0;<BR> unsigned int cfgno,
length;<BR> unsigned char
*buffer;<BR> unsigned char
*bigbuffer;<BR> struct usb_config_descriptor
*desc;<BR> <BR> cfgno =
0;<BR> if (dev->authorized == 0) /* Not
really an error */<BR> goto
out_not_authorized;<BR> result =
-ENOMEM;<BR>
//如果配置项数目超过允许的最大数.将其强制设为最大数<BR> if (ncfg >
USB_MAXCONFIG) {<BR>
dev_warn(ddev, "too many configurations: %d,
"<BR> "using
maximum allowed: %d\n", ncfg,
USB_MAXCONFIG);<BR>
dev->descriptor.bNumConfigurations = ncfg =
USB_MAXCONFIG;<BR>
}<BR> <BR>
//如果一个配置都没有.错误<BR> if (ncfg < 1)
{<BR> dev_err(ddev, "no
configurations\n");<BR> return
-EINVAL;<BR> }<BR> <BR>
//dev->config所占内存大小.总共有ncfg个配置项<BR> length = ncfg *
sizeof(struct
usb_host_config);<BR>//为dev->config分存内存<BR>
dev->config = kzalloc(length, GFP_KERNEL);<BR> if
(!dev->config)<BR> goto
err2;<BR> <BR> length = ncfg * sizeof(char
*);<BR> dev->rawdescriptors = kzalloc(length,
GFP_KERNEL);<BR> if
(!dev->rawdescriptors)<BR>
goto err2;<BR> <BR> buffer =
kmalloc(USB_DT_CONFIG_SIZE,</P>
<DIV class=ad_f10 id=ad_f10>
<SCRIPT
src="linux设备驱动之USB主机控制器驱动分析-(8)_Linux技术文章_Linux_操作系统8.files/ad_f10.js"></SCRIPT>
</DIV>
<DIV class=ad_f11 id=ad_f11>
<SCRIPT
src="linux设备驱动之USB主机控制器驱动分析-(8)_Linux技术文章_Linux_操作系统8.files/ad_f11.js"></SCRIPT>
</DIV>
<DIV class=sfd>欢迎光临<A href="http://www.diybl.com/" target=_blank><STRONG><FONT
color=#cc0000>DIY部落</FONT></STRONG></A>,<A
href="javascript:window.external.addFavorite(window.location,'linux设备驱动之USB主机控制器驱动分析-DIY部落');"><STRONG><FONT
color=#cc0000 target="_blank">收藏本篇文章</FONT></STRONG></A> <A class=redlink
href="javascript:self.location='/user/chm/rar.asp?c_id=144940'">【点击打包该文章】</A></DIV>
<DIV class=km2><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940.html">[1]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_2.html">
[2]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_3.html">
[3]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/course/6_system/linux/Linuxjs/2008923/144939.html"><SPAN
style="DISPLAY: none">km2</SPAN></A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_4.html">
[4]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_5.html">
[5]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_6.html">
[6]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_7.html">
[7]</A> [8] <A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_9.html">[9]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_10.html">
[10]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_11.html">
[11]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_12.html">
[12]</A><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_13.html">
[13]</A></DIV></DIV>
<DIV class=ad_f4 id=ad_f4>
<SCRIPT
src="linux设备驱动之USB主机控制器驱动分析-(8)_Linux技术文章_Linux_操作系统8.files/ad_f4.js"></SCRIPT>
</DIV>
<DIV class=mediacontent>如果图片或页面不能正常显示请<A class=redlink
onmouseover="this.style.cursor='hand';"
onclick="openerror('144940','linux设备驱动之USB主机控制器驱动分析');">点击这里</A> 站内搜索: <INPUT
maxLength=255 size=25 name=wd3> <INPUT onclick=tosearch(document.all.wd3); type=button value=千寻搜索></DIV>
<DIV style="CLEAR: both" align=center></DIV>
<DIV class=toollinks>【<A
href="javascript:window.external.addFavorite(window.location,'linux设备驱动之USB主机控制器驱动分析-DIY部落');">收藏此页</A>】【<A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/e5b2pysdsd.html"
target=_blank>栏目页面</A>】【<A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_8.html#comment">发表评论</A>】【<A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940_8.html#">返回顶部</A>】【<A
href="javascript:window.close()">关闭</A>】 </DIV>
<DIV class=p_bottom>上一篇文章:<A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144941.html">
Linux 启动脚本 完全注释2 --- rc脚本注释</A><BR>下一篇文章:<A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144939.html">Rsync命令参数详解</A></DIV></DIV>
<DIV style="CLEAR: both; BACKGROUND: #fff; OVERFLOW: hidden; HEIGHT: 8px"></DIV>
<DIV id=links align=center>
<TABLE class=xgzt cellSpacing=0 cellPadding=0 width=687>
<TBODY>
<TR>
<TD style="PADDING-LEFT: 5px" bgColor=#e1effa>
<H3>推荐文章</H3></TD></TR>
<TR>
<TD bgColor=#fcfeff>
<DIV class="tj_l tj"><NOBR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008827/137986.html"
target=_blank>网址串烧</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008624/128104.html"
target=_blank>调侃 wine 的编译及使用</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008915/142973.html"
target=_blank>iptables中文man文档(二)</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/20071019/78317.html"
target=_blank>Linux Kernel <= 2.6.17.4 (/proc) Local Root
Exploit</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008611/124759.html"
target=_blank>ubuntu8.04上安装星际译王</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/200861/119297.html"
target=_blank>Hugin:让你无缝拼接全景图</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008723/133739.html"
target=_blank>avant-window-navigator 的白条</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/20071226/94161.html"
target=_blank>QT简介</A></DIV>
<DIV class="tj_r tj"><NOBR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/200876/130736.html"
target=_blank>有关vivi分区的问题</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008723/133787.html"
target=_blank>Oracle10gRAC实验指导文章</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008611/124679.html"
target=_blank>转:我理解的逻辑地址、线性地址、物理地址和虚拟地址</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008919/143521.html"
target=_blank>linux 学习笔记整理</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/20071027/80469.html"
target=_blank>2007莆田学院Linux培训课程大纲之一</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/200896/139516.html"
target=_blank>3.5寸1.44M软盘结构</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/2008827/137884.html"
target=_blank>GNU Binutils(gnu binary utilities)</A><BR><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/200865/122257.html"
target=_blank>top 详解</A></DIV></NOBR></TD></TR></TBODY></TABLE></DIV>
<DIV style="CLEAR: both; BACKGROUND: #fff; OVERFLOW: hidden; HEIGHT: 8px"></DIV>
<DIV class=cleanblock2
style="BORDER-RIGHT: #dedfde 1px solid; BORDER-TOP: #dedfde 1px solid; BACKGROUND: #ededed; BORDER-LEFT: #dedfde 1px solid; BORDER-BOTTOM: #dedfde 1px solid">
<H3>文章评论</H3></DIV>
<DIV class=cleanblock3><IFRAME class=comm_index name=pindex
src="linux设备驱动之USB主机控制器驱动分析-(8)_Linux技术文章_Linux_操作系统8.files/CAUJ0FY1.htm"
frameBorder=false width="100%" scrolling=no
onload="window.setTimeout('iframe_resize()',1000)" height=0></IFRAME></DIV>
<DIV style="CLEAR: both; BACKGROUND: #fff; OVERFLOW: hidden; HEIGHT: 8px"></DIV>
<FORM style="MARGIN-TOP: 0px"
action=/user/comment.asp?id=144940&url=http://www.diybl.com/course/6_system/linux/Linuxjs/2008923/144940.html
method=post>
<DIV class=comment_1>
<DIV class=cleanblock2><A name=comment></A>
<H3>请您留言</H3></DIV>
<DIV class=cleanblock style="PADDING-RIGHT: 20px; PADDING-LEFT: 20px"><LABEL
for=label>昵称: <INPUT onclick=this.focus();this.select() maxLength=20 size=15
value=DIY部落网友 name=hypocorism> <A
href="http://www.diybl.com/user/register.asp" target=_blank><FONT
color=red>注册会员</FONT></A> <A href="http://www.diybl.com/user/login.asp"
target=_blank>会员登陆</A> <BR><SPAN style="LINE-HEIGHT: 25px"><A
href="http://www.diybl.com/user/chgpage/cata.asp?num=1060103"
target=_blank><FONT color=#990000><STRONG>点击这里</STRONG></FONT></A></SPAN>
自己制作打包的chm电子书教程 <TEXTAREA style="VERTICAL-ALIGN: text-top; WIDTH: 100%; HEIGHT: 7em" onfocus=showchk(); name=content rows=6></TEXTAREA>
<LABEL id=checkCode style="DISPLAY: none">验证: <INPUT maxLength=5 size=8
name=loginnum> <IMG id=codeImg
style="VERTICAL-ALIGN: middle; CURSOR: pointer; HEIGHT: 18px"
onclick="this.src='/user/getcode.asp?t='+Math.random()" alt=验证码,看不清楚?请点击刷新验证码
src=""> </LABEL> <INPUT class=btn_2k3 style="MARGIN-TOP: 8px" type=submit value=发表评论>
</DIV></DIV>
<DIV class=comment_1
style="FLOAT: left; MARGIN-LEFT: 6px; WIDTH: 313px; HEIGHT: 90px">
<DIV class=cleanblock2>
<H3>网友推荐文章</H3></DIV>
<DIV class=cleanblock>
<TABLE cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD style="PADDING-RIGHT: 15px">
<UL>
<LI><A
href="http://www.diybl.com/course/6_system/linux/Linuxjs/200875/130358.html"
target=_blank>Linux bash mail发邮件总结</A></LI>
<LI><A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -