📄 linux设备驱动程序学习(1)-字符设备驱动程序 - linux设备驱动程序 - tekkaman ninja.htm
字号:
cdev</SPAN></SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,2)">。代码应包含</SPAN></FONT><FONT
size=3><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)"><linux/cdev.h></SPAN></SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,2)">,它定义了</SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)">struct
cdev</SPAN></SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,2)">以及与其相关的一些辅助函数。</SPAN></FONT></P>
<P align=center><FONT
style="COLOR: rgb(255,1,2)"><SPAN
style="COLOR: rgb(0,1,2)"><FONT color=#0000ff
size=3><STRONG>注册一个独立的cdev设备的基本过程如下:</STRONG></FONT></SPAN></FONT></P><FONT
style="COLOR: rgb(255,1,2)"></FONT>
<P align=left><FONT
style="COLOR: rgb(255,1,2)"><FONT face=楷体
color=#0000ff size=3>1、为struct cdev
分配空间(如果已经将struct cdev
嵌入到自己的设备的特定结构体中,并分配了空间,这步略过!)</FONT></FONT></P>
<P align=left><FONT
style="COLOR: rgb(255,1,2)"><FONT face=楷体
color=#000000>struct cdev *my_cdev =
cdev_alloc();</FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT face=楷体
color=#0000ff size=3>2、初始化struct cdev
</FONT></FONT></P><FONT
style="COLOR: rgb(255,1,2)"><FONT color=#000000
size=2>void cdev_init(struct cdev *cdev, const
struct file_operations *fops)</FONT> </FONT>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT face=楷体
color=#0000ff
size=3>3、初始化cdev.owner</FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT
color=#000000>cdev.owner =
THIS_MODULE;</FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT
size=3><FONT face=楷体
color=#0000ff>4、cdev设置完成,通知内核struct cdev的信息(<FONT
color=#ff0000>在执行这步之前必须确定你对<FONT size=2>struct
cdev</FONT></FONT><FONT size=3><FONT
color=#ff0000>的以上设置已经完成!</FONT>)</FONT></FONT></FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT
color=#000000>int cdev_add(struct cdev *p, dev_t
dev, unsigned count)</FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT
color=#0000ff size=3>从系统中移除一个字符设备:<FONT
color=#000000 size=2>void cdev_del(struct cdev
*p)</FONT></FONT></FONT></P>
<P><FONT style="COLOR: rgb(255,1,2)"><FONT
size=3><FONT color=#000000></FONT><FONT
color=#0000ff>以下是scull中的初始化代码(之前已经为<FONT
size=2><FONT face=新宋体><FONT color=#000000><FONT
color=#000000><SPAN
style="COLOR: rgb(0,0,255)"><FONT
color=#000000>struct</FONT></SPAN>
</FONT>scull_dev</FONT> <FONT
size=3>分配了空间</FONT></FONT></FONT>):</FONT></FONT></FONT></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: rgb(0,0,0)"><SPAN
style="COLOR: rgb(255,153,0)">/*<BR> * Set
up the char_dev structure for this
device.<BR> */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">static</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">void</SPAN>
scull_setup_cdev<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
scull_dev <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>dev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
index<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> err<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> devno <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> MKDEV<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>scull_major<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> scull_minor
<SPAN style="COLOR: rgb(0,0,204)">+</SPAN>
index<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> <BR> cdev_init<SPAN
style="COLOR: rgb(0,0,204)">(</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>cdev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">&</SPAN>scull_fops<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> dev<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>cdev<SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>owner <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN>
THIS_MODULE<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> dev<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>cdev<SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>ops <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">&</SPAN>scull_fops<SPAN
style="COLOR: rgb(0,0,204)">; </SPAN></SPAN></CODE><CODE><SPAN
style="COLOR: rgb(0,0,0)"><SPAN
style="COLOR: rgb(0,0,204)"> <FONT
color=#ff0000>//这句可以省略,在<FONT face=新宋体><FONT
color=#000000>cdev_init</FONT>中已经做过</FONT></FONT></SPAN><BR> err
<SPAN style="COLOR: rgb(0,0,204)">=</SPAN>
cdev_add <SPAN
style="COLOR: rgb(0,0,204)">(</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>cdev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> devno<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><BR> <SPAN
style="COLOR: rgb(255,153,0)">/* Fail gracefully
if need be <FONT
color=#ff0000>这步值得注意</FONT>*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">if</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>err<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><BR> printk<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>KERN_NOTICE
<SPAN style="COLOR: rgb(255,0,255)">"Error %d
adding scull%d"</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> err<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> index<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></CODE></P></TD></TR></TBODY></TABLE><FONT
color=#0001ff size=4></FONT>
<P></P>
<HR id=null>
<FONT color=#0001ff size=4><FONT
size=4>四、scull模型的内存使用</FONT> </FONT>
<P></P>
<P><FONT color=#0001ff size=4><IMG
src="Linux设备驱动程序学习(1)-字符设备驱动程序 - Linux设备驱动程序 - Tekkaman Ninja.files/071024144610.jpg"></FONT></P>
<P><FONT color=#0001ff size=4><FONT
size=3>以下是scull模型的结构体:</FONT></FONT></P>
<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: rgb(0,0,0)"><SPAN
style="COLOR: rgb(255,153,0)">/*<BR> *
Representation of scull quantum
sets.<BR> */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
scull_qset <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> <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>data<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
scull_qset <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>next<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">}</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><BR><SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
scull_dev <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
scull_qset <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>data<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* Pointer to
first quantum set
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -