📄 linux设备驱动程序学习(14)-linux设备模型(各环节的整合) - linux设备驱动程序 - tekkaman ninja.htm
字号:
*/</SPAN><BR><SPAN
style="COLOR: #0000ff">static</SPAN> <SPAN
style="COLOR: #0000ff">struct</SPAN> ldd_driver
sculld_driver <SPAN
style="COLOR: #0000cc">=</SPAN> <SPAN
style="COLOR: #0000cc">{</SPAN><BR> <SPAN
style="COLOR: #0000cc">.</SPAN>version <SPAN
style="COLOR: #0000cc">=</SPAN> <SPAN
style="COLOR: #ff00ff">"$Revision:
1.21-tekkamanninja $"</SPAN><SPAN
style="COLOR: #0000cc">,</SPAN><BR> <SPAN
style="COLOR: #0000cc">.</SPAN>module <SPAN
style="COLOR: #0000cc">=</SPAN> THIS_MODULE<SPAN
style="COLOR: #0000cc">,</SPAN><BR> <SPAN
style="COLOR: #0000cc">.</SPAN>driver <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">"sculld"</SPAN><SPAN
style="COLOR: #0000cc">,</SPAN><BR> <SPAN
style="COLOR: #0000cc">}</SPAN><SPAN
style="COLOR: #0000cc">,</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #ff9900">//**************************************************************</SPAN><BR><BR><SPAN
style="COLOR: #ff9900">//******增加设备注册函数和设备号属性********************************<BR></SPAN><SPAN
style="COLOR: #0000ff">static</SPAN> ssize_t
sculld_show_dev<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> device
<SPAN style="COLOR: #0000cc">*</SPAN>ddev<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">struct</SPAN>
device_attribute <SPAN
style="COLOR: #0000cc">*</SPAN>attr <SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">char</SPAN> <SPAN
style="COLOR: #0000cc">*</SPAN>buf<SPAN
style="COLOR: #0000cc">)</SPAN><BR><SPAN
style="COLOR: #0000cc">{</SPAN><BR> <SPAN
style="COLOR: #0000ff">struct</SPAN> sculld_dev
<SPAN style="COLOR: #0000cc">*</SPAN>dev <SPAN
style="COLOR: #0000cc">=</SPAN> ddev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>driver_data<SPAN
style="COLOR: #0000cc">;</SPAN><BR> <SPAN
style="COLOR: #0000ff">return</SPAN>
print_dev_t<SPAN
style="COLOR: #0000cc">(</SPAN>buf<SPAN
style="COLOR: #0000cc">,</SPAN> dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>cdev<SPAN
style="COLOR: #0000cc">.</SPAN>dev<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><BR><BR><SPAN
style="COLOR: #0000ff">static</SPAN>
DEVICE_ATTR<SPAN
style="COLOR: #0000cc">(</SPAN>dev<SPAN
style="COLOR: #0000cc">,</SPAN> S_IRUGO<SPAN
style="COLOR: #0000cc">,</SPAN>
sculld_show_dev<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #ff0000">NULL</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><BR><SPAN
style="COLOR: #0000ff">static</SPAN> <SPAN
style="COLOR: #0000ff">void</SPAN>
sculld_register_dev<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> sculld_dev
<SPAN style="COLOR: #0000cc">*</SPAN>dev<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> index<SPAN
style="COLOR: #0000cc">)</SPAN><BR><SPAN
style="COLOR: #0000cc">{</SPAN><BR> <SPAN
style="COLOR: #ff0000">sprintf</SPAN><SPAN
style="COLOR: #0000cc">(</SPAN>dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>devname<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #ff00ff">"sculld%d"</SPAN><SPAN
style="COLOR: #0000cc">,</SPAN> index<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>ldev<SPAN
style="COLOR: #0000cc">.</SPAN>name <SPAN
style="COLOR: #0000cc">=</SPAN> dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>devname<SPAN
style="COLOR: #0000cc">;</SPAN><BR> dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>ldev<SPAN
style="COLOR: #0000cc">.</SPAN>driver <SPAN
style="COLOR: #0000cc">=</SPAN> <SPAN
style="COLOR: #0000cc">&</SPAN>sculld_driver<SPAN
style="COLOR: #0000cc">;</SPAN><BR> dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>ldev<SPAN
style="COLOR: #0000cc">.</SPAN>dev<SPAN
style="COLOR: #0000cc">.</SPAN>driver_data <SPAN
style="COLOR: #0000cc">=</SPAN> dev<SPAN
style="COLOR: #0000cc">;</SPAN><BR> register_ldd_device<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>ldev<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN
style="COLOR: #0000cc">(</SPAN>device_create_file<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>dev<SPAN
style="COLOR: #0000cc">-</SPAN><SPAN
style="COLOR: #0000cc">></SPAN>ldev<SPAN
style="COLOR: #0000cc">.</SPAN>dev<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000cc">&</SPAN>dev_attr_dev<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN>
<BR> printk<SPAN
style="COLOR: #0000cc">(</SPAN> <SPAN
style="COLOR: #ff00ff">"Unable to create dev
attribute ! \n"</SPAN><SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000cc">}</SPAN><BR><SPAN
style="COLOR: #ff9900">//*****************************************************************<BR></SPAN><BR><SPAN
style="COLOR: #ff9900">/*还要<FONT
color=#ff0000>在模块的初始化函数和模块清除函数中添加设备和驱动的注册和注销函数</FONT>*/</SPAN><BR> sculld_register_dev<SPAN
style="COLOR: #0000cc">(</SPAN>sculld_devices
<SPAN style="COLOR: #0000cc">+</SPAN> i<SPAN
style="COLOR: #0000cc">,</SPAN> i<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> register_ldd_driver<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>sculld_driver<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> unregister_ldd_device<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>sculld_devices<SPAN
style="COLOR: #0000cc">[</SPAN>i<SPAN
style="COLOR: #0000cc">]</SPAN><SPAN
style="COLOR: #0000cc">.</SPAN>ldev<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> unregister_ldd_driver<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000cc">&</SPAN>sculld_driver<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P>修改后好模块就可以实现向sysfs文件系统导出信息。
<HR id=null>
<P></P>
<P><STRONG><FONT color=#0000ff
size=3>三、分析设备和驱动注册和注销核心函数,了解<FONT
color=#ff0000>一般</FONT>的注册、注销过程。</FONT></STRONG></P>
<P><STRONG><U><FONT
color=#660099>以下也参考了《LDD3》中的PCI驱动分析</FONT></U></STRONG></P>
<P><FONT
color=#0000ff><STRONG>(1)设备的注册</STRONG></FONT></P>
<P>在驱动程序中对设备进行注册的核心函数是:
<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">int</SPAN>
device_register<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> device
<SPAN style="COLOR: #0000cc">*</SPAN>dev<SPAN
style="COLOR: #0000cc">)</SPAN><BR><SPAN
style="COLOR: #0000cc">{</SPAN><BR> device_initialize<SPAN
style="COLOR: #0000cc">(</SPAN>dev<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR> <SPAN
style="COLOR: #0000ff">return</SPAN>
device_add<SPAN
style="COLOR: #0000cc">(</SPAN>dev<SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -