📄 linux设备驱动程序学习(14)-linux设备模型(各环节的整合) - linux设备驱动程序 - tekkaman ninja.htm
字号:
size=4>Linux设备驱动程序学习(14)</FONT></H1>
<H1 align=center><FONT color=#0000ff
size=4>-Linux设备模型(各环节的整合)</FONT></H1>
<DIV> </DIV>
<DIV>通过一个设备在内核中生命周期的各个阶段,可以更好地理解Linux设备模型。我将通过分析lddbus和sculld的源码来了解Linux设备模型中各环节的整合。《LDD3》中的(PCI总线)各环节的整合这部分内容作为参考资料,因为嵌入式Linux比较少用到PCI总线。<FONT
color=#ff0000>看这部分内容一定要先熟悉一下 lddbus 和 sculld
的源码。</FONT>
<HR id=null>
</DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff
size=3><STRONG>一、lddbus模块:添加总线、导出总线设备和设备驱动的注册函数。</STRONG></FONT></DIV>
<DIV>lddbus子系统声明了一个bus_type结构,称为ldd_bus_type
。源码是在编译时初始化了这个结构体,源码:</DIV>
<DIV></DIV>
<DIV></DIV>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><SPAN
style="COLOR: #ff9900">/*<BR> * And the bus
type.<BR> */</SPAN><BR><SPAN
style="COLOR: #0000ff">struct</SPAN> bus_type
ldd_bus_type <SPAN
style="COLOR: #0000cc">=</SPAN> <SPAN
style="COLOR: #0000cc">{</SPAN><BR> <SPAN
style="COLOR: #0000cc">.</SPAN>name <SPAN
style="COLOR: #0000cc">=</SPAN> <SPAN
style="COLOR: #ff00ff">"ldd"</SPAN><SPAN
style="COLOR: #0000cc">,</SPAN><BR> <SPAN
style="COLOR: #0000cc">.</SPAN>match <SPAN
style="COLOR: #0000cc">=</SPAN> ldd_match<SPAN
style="COLOR: #0000cc">,</SPAN><BR> <SPAN
style="COLOR: #0000cc"><FONT
color=#ff0000>.uevent =
ldd_uevent</FONT></SPAN><SPAN
style="COLOR: #0000cc">,</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P>在将lddbus子系统装载到内核和从内核卸载的源码如下:
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">static</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> __init
ldd_bus_init<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><BR><SPAN
style="COLOR: #0000cc">{</SPAN><BR> <SPAN
style="COLOR: #0000ff">int</SPAN> ret<SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><SPAN
style="COLOR: #0000cc"></SPAN><BR><FONT
face=新宋体> ret <SPAN
style="COLOR: #0000cc">=</SPAN>
bus_register<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>ldd_bus_type<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">; <FONT
color=#ff9900>/*注册总线,在调用这个函数之后<FONT
color=#ff9900>ldd_bus_type
结构体将向内核注册,在/sys/bus中出现ldd文件夹,其中包含两个目录:devices
和 drivers
</FONT>*/</FONT></SPAN><BR> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN
style="COLOR: #0000cc">(</SPAN>ret<SPAN
style="COLOR: #0000cc">)</SPAN><BR> <SPAN
style="COLOR: #0000ff">return</SPAN> ret<SPAN
style="COLOR: #0000cc">;</SPAN><BR> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN
style="COLOR: #0000cc">(</SPAN>bus_create_file<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>ldd_bus_type<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000cc">&</SPAN>bus_attr_version<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">) <FONT
color=#ff9900>/*添加总线属性,将在/sys/bus/ldd目录中出现<FONT
color=#ff9900>version属性文件</FONT>*/</FONT></SPAN><BR> printk<SPAN
style="COLOR: #0000cc">(</SPAN>KERN_NOTICE <SPAN
style="COLOR: #ff00ff">"Unable to create version
attribute ! \n"</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> ret
<SPAN style="COLOR: #0000cc">=</SPAN>
device_register<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>ldd_bus<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;<FONT
color=#ff9900>/*将总线作为设备注册。因为总线也可以是一个设备,比如在S3C2440中SPI总线控制器相对于ARM920T核心来说,其实就是一个外设。调用此函数后,就会在/sys/devices中出现ldd0目录*/</FONT></SPAN><BR> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN
style="COLOR: #0000cc">(</SPAN>ret<SPAN
style="COLOR: #0000cc">)</SPAN><BR> printk<SPAN
style="COLOR: #0000cc">(</SPAN>KERN_NOTICE <SPAN
style="COLOR: #ff00ff">"Unable to register ldd0
! \n"</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> <BR> printk<SPAN
style="COLOR: #0000cc">(</SPAN>KERN_NOTICE <SPAN
style="COLOR: #ff00ff">"Mount lddbus ok !\nBus
device is ldd0 !\n<FONT color=#ff0000>You can
see me in sys/module/ , sys/devices/ and
sys/bus/ !</FONT> \n"</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> <BR> <SPAN
style="COLOR: #0000ff">return</SPAN> ret<SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><BR><BR><SPAN
style="COLOR: #0000ff">static</SPAN> <SPAN
style="COLOR: #0000ff">void</SPAN>
ldd_bus_exit<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><BR><SPAN
style="COLOR: #0000cc">{</SPAN><BR> device_unregister<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>ldd_bus<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> bus_unregister<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>ldd_bus_type<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><BR><BR>module_init<SPAN
style="COLOR: #0000cc">(</SPAN>ldd_bus_init<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR>module_exit<SPAN
style="COLOR: #0000cc">(</SPAN>ldd_bus_exit<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P>
lddbus模块的主要部分就是这些,很简单。因为这只不过是一个虚拟的总线,没有实际的驱动。模块还导出了加载总线设备和总线驱动时需要用到的注册和注销函数。对于实际的总线,应该还要导出总线的读写例程。</P>
<P>
将总线设备和驱动注册函数放在lddbus模块,并导出给其他的总线驱动程序使用,是因为注册总线设备和驱动需要总线结构体的信息,而且这些注册函数对于所有总线设备和驱动都一样。只要这个总线驱动一加载,其他的总线驱动程序就可以通过调用这些函数注册总线设备和驱动,方便了总线设备驱动的作者,减少了代码的冗余。</P>
<P> 这些注册函数内部调用<FONT
color=#ff0000>driver_register、device_register 和
driver_unregister、device_unregister</FONT> 这些函数。
<HR id=null>
<P></P>
<DIV></DIV>
<DIV><STRONG><FONT color=#0000ff
size=3>二、sculld模块:在scull的基础上添加设备和驱动注册和注销函数。</FONT></STRONG></DIV>
<DIV><FONT size=2>
sculld模块基本和scull模块实现的功能一致,我参考《LDD3》提供的sculld,将以前实验过的功能较全的scull进行修改。<FONT
color=#ff0000>主要</FONT>的修改如下(其他还有些小改动):</FONT></DIV>
<DIV><STRONG><FONT color=#0000ff
size=3></FONT></STRONG> </DIV>
<TABLE
style="WIDTH: 611px; BORDER-COLLAPSE: collapse; HEIGHT: 812px"
borderColor=#999999 cellSpacing=0 cellPadding=0
width=611 bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><SPAN
style="COLOR: #ff9900">//*******在源码的声明阶段添加如下代码,以增加设备和驱动的结构体*****</SPAN><BR><SPAN
style="COLOR: #0000ff">struct</SPAN> sculld_dev
<SPAN
style="COLOR: #0000cc">*</SPAN>sculld_devices<SPAN
style="COLOR: #0000cc">;</SPAN> <SPAN
style="COLOR: #ff9900">/* allocated in
scull_init_module */</SPAN></SPAN></CODE></P>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><SPAN
style="COLOR: #ff9900"></SPAN><BR><SPAN
style="COLOR: #ff9900">/* Device model stuff
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -