📄 linux设备驱动程序学习(1)-字符设备驱动程序 - linux设备驱动程序 - tekkaman ninja.htm
字号:
quantum<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* the current
quantum size
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> qset<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* the current
array size
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">long</SPAN>
size<SPAN style="COLOR: rgb(0,0,204)">;</SPAN>
<SPAN style="COLOR: rgb(255,153,0)">/* amount of
data stored here
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
access_key<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* used by
sculluid and scullpriv
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN>
semaphore sem<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* mutual
exclusion semaphore
*/</SPAN><BR> <SPAN
style="COLOR: rgb(0,0,255)">struct</SPAN> cdev
cdev<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN>
<SPAN style="COLOR: rgb(255,153,0)">/* Char
device
structure */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">}</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<P><FONT color=#0000ff size=3><FONT
color=#000066>scull驱动程序引入了两个Linux内核中用于内存管理的核心函数,</FONT><FONT
color=#000102>它们的定义都在</FONT><FONT size=3><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)"><linux/slab.h>:
<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(0,0,204)"><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)"><CODE><SPAN
style="COLOR: rgb(0,0,0)"><FONT face=新宋体><FONT
size=3><SPAN
style="COLOR: rgb(0,0,255)">void</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>kmalloc<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(255,0,0)">size_t</SPAN>
size<SPAN style="COLOR: rgb(0,0,204)">,</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
flags<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">void</SPAN>
kfree<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>ptr<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN></FONT></FONT></SPAN></CODE></SPAN></SPAN></SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE></SPAN></SPAN></FONT></FONT></P>
<P><FONT color=#0000ff
size=3>以下是scull模块中的一个释放整个数据区的函数(类似清零),将在scull以写方式打开和scull_cleanup_module中被调用:</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(0,0,255)">int</SPAN>
scull_trim<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><BR><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> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>dptr<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">
int</SPAN> qset <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>qset<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* <FONT
face=新宋体>量子集中量子的个数</FONT>*/</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">
int</SPAN> i<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">
for</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dptr <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>data<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> dptr<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> dptr <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> next<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/*
循环scull_set个数次,直到dptr为NULL为止。*/</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">
if</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dptr<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> <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">
for</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>i <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN> 0<SPAN
style="COLOR: rgb(0,0,204)">;</SPAN> i <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN>
qset<SPAN style="COLOR: rgb(0,0,204)">;</SPAN>
i<SPAN style="COLOR: rgb(0,0,204)">+</SPAN><SPAN
style="COLOR: rgb(0,0,204)">+</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)<SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
循环一个量子集中量子的个数次*/</FONT></SPAN><BR></SPAN>
kfree<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dptr<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>i<SPAN
style="COLOR: rgb(0,0,204)">]</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;<SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
释放其中一个量子的空间*/</FONT></SPAN><BR></SPAN><BR>
kfree<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dptr<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><SPAN
style="COLOR: rgb(0,0,204)">;<SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
释放当前的scull_set的量子集的空间*/</FONT></SPAN></SPAN><BR>
dptr<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> <SPAN
style="COLOR: rgb(255,0,0)">NULL</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;<SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
释放一个scull_set中的<SPAN
style="COLOR: rgb(0,0,255)">void</SPAN><FONT
color=#000000> </FONT><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><FONT
color=#000000>data</FONT><FONT
color=#ff9933>指针</FONT>*/</FONT></SPAN></SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">
}</SPAN><BR> next
<SPAN style="COLOR: rgb(0,0,204)">=</SPAN>
dptr<SPAN
style="COLOR: rgb(0,0,204)">-</SPAN><SPAN
style="COLOR: rgb(0,0,204)">></SPAN>next<SPAN
style="COLOR: rgb(0,0,204)">; <SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
准备下个scull_set的指针*/</FONT></SPAN></SPAN><BR>
kfree<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dptr<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;<SPAN
style="COLOR: rgb(255,153,0)"><FONT face=新宋体>/*
释放当前的scull_set*/</FONT></S
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -