📄 linux设备驱动程序学习(13)-linux设备模型(总线、设备、驱动程序和类) - linux设备驱动程序 - tekkaman ninja.htm
字号:
style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">return</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">!</SPAN><SPAN
style="COLOR: rgb(255,0,0)">strncmp</SPAN><SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dev<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>bus_id<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> driver<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>name<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(255,0,0)">strlen</SPAN><SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>driver<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>name<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">}</SPAN><SPAN
style="COLOR: rgb(255,153,0)">/*仅简单比较驱动和设备的名字*/</SPAN><BR><SPAN
style="COLOR: rgb(255,153,0)">/*当涉及实际硬件时, match
函数常常对设备提供的硬件 ID 和驱动所支持的 ID
做比较*/</SPAN><BR><BR><SPAN
style="COLOR: rgb(0,0,255)">static</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
ldd_uevent<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN> device
<SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>dev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">char</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>envp<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
num_envp<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">char</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>buffer<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
buffer_size<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> envp<SPAN
style="COLOR: rgb(0,0,204)">[</SPAN>0<SPAN
style="COLOR: rgb(0,0,204)">]</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> buffer<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">if</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>snprintf<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>buffer<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN>
buffer_size<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(255,0,255)">"LDDBUS_VERSION=%s"</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN><BR> Version<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">></SPAN><SPAN
style="COLOR: rgb(0,0,204)">=</SPAN>
buffer_size<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">return</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">-</SPAN>ENOMEM<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> envp<SPAN
style="COLOR: rgb(0,0,204)">[</SPAN>1<SPAN
style="COLOR: rgb(0,0,204)">]</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> <SPAN
style="COLOR: rgb(255,0,0)">NULL</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">return</SPAN> 0<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">}</SPAN><SPAN
style="COLOR: rgb(255,153,0)">/*在环境变量中加入 lddbus
源码的当前版本号*/</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P><FONT color=#0000ff
size=3><STRONG>对设备和驱动的迭代</STRONG></FONT></P>
<P><FONT color=#000000>若要编写总线层代码,
可能不得不对所有已经注册到总线的设备或驱动进行一些操作,这可能需要仔细研究嵌入到 bus_type
结构中的其他数据结构, 但最好使用内核提供的辅助函数:
<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: rgb(0,0,0)"><FONT face=新宋体><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
bus_for_each_dev<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
bus_type <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>bus<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN> device
<SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>start<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">void</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>data<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>fn<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN> device
<SPAN style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">void</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
bus_for_each_drv<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
bus_type <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>bus<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
device_driver <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>start<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">void</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>data<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>fn<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
device_driver <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">void</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><BR><SPAN
style="COLOR: rgb(255,153,0)">/*这两个函数迭代总线上的每个设备或驱动程序,
将关联的 device 或 device_driver 传递给 fn, 同时传递 data
值。若 start 为 NULL, 则从第一个设备开始; 否则从 start
之后的第一个设备开始。若 fn 返回非零值, 迭代停止并且那个值从
bus_for_each_dev 或bus_for_each_drv
返回。*/</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></FONT></P>
<P><STRONG><FONT color=#0000ff
size=3>总线属性</FONT></STRONG></P>
<P>几乎 Linux 设备模型中的每一层都提供添加属性的函数,
总线层也不例外。bus_attribute 类型定义在<FONT color=#0000ff>
<linux/device.h></FONT> 如下:
<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: rgb(0,0,0)"><FONT face=新宋体><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
bus_attribute <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
attribute attr<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> ssize_t
<SPAN style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>show<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
bus_type <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">char</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN> buf<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> ssize_t
<SPAN styl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -