?? linux設(shè)備驅(qū)動程序?qū)W習(xí)(1)-字符設(shè)備驅(qū)動程序 - linux設(shè)備驅(qū)動程序 - tekkaman ninja.htm
字號:
style="FONT-WEIGHT: normal">主設(shè)備號表示設(shè)備對應(yīng)的驅(qū)動程序;次設(shè)備號由內(nèi)核使用,用于正確確定設(shè)備文件所指的設(shè)備。</SPAN><BR
style="FONT-WEIGHT: normal"><SPAN
style="FONT-WEIGHT: normal">內(nèi)核用dev_t類型(<SPAN
style="COLOR: rgb(0,1,255)"><linux/types.h></SPAN>)來保存設(shè)備編號,dev_t是一個32位的數(shù),12位表示主設(shè)備號,20為表示次設(shè)備號。</SPAN><BR
style="FONT-WEIGHT: normal"><SPAN
style="FONT-WEIGHT: normal">在實(shí)際使用中,是通過<SPAN
style="COLOR: rgb(0,1,255)"><linux/kdev_t.h></SPAN>中定義的宏來轉(zhuǎn)換格式。</SPAN><BR></SPAN>
<TABLE style="WIDTH: 447px; HEIGHT: 21px"
cellSpacing=1 cellPadding=1 border=1>
<TBODY>
<TR>
<TD> (dev_t)-->主設(shè)備號、次設(shè)備號</TD>
<TD> MAJOR(dev_t dev)<BR> MINOR(dev_t
dev)<BR></TD></TR>
<TR>
<TD> 主設(shè)備號、次設(shè)備號-->(dev_t)</TD>
<TD> MKDEV(int major,int
minor) </TD></TR></TBODY></TABLE><BR
style="COLOR: rgb(0,1,2)"><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)">建立一個字符設(shè)備之前,驅(qū)動程序首先要做的事情就是獲得設(shè)備編號。其這主要函數(shù)在<SPAN
style="COLOR: rgb(0,1,255)"><linux/fs.h></SPAN>中聲明:</SPAN><BR></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(0,0,255)">int</SPAN>
register_chrdev_region<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dev_t
first<SPAN style="COLOR: rgb(0,0,204)">,</SPAN>
<SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
<SPAN
style="COLOR: rgb(255,0,0)">count</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">char</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)">;
//指定設(shè)備編號</SPAN><BR><BR><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN>
alloc_chrdev_region<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dev_t <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN>dev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
firstminor<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN><BR><SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
<SPAN
style="COLOR: rgb(255,0,0)">count</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>name<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;
//動態(tài)生成設(shè)備編號</SPAN><BR><BR><SPAN
style="COLOR: rgb(0,0,255)">void</SPAN>
unregister_chrdev_region<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dev_t
first<SPAN style="COLOR: rgb(0,0,204)">,</SPAN>
<SPAN
style="COLOR: rgb(0,0,255)">unsigned</SPAN>
<SPAN style="COLOR: rgb(0,0,255)">int</SPAN>
<SPAN
style="COLOR: rgb(255,0,0)">count</SPAN><SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;
//釋放設(shè)備編號<BR></SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><FONT
size=4><BR style="COLOR: rgb(255,1,2)"><SPAN
style="COLOR: rgb(255,1,2)">分配之設(shè)備號的最佳方式是:默認(rèn)采用動態(tài)分配,同時(shí)保留在加載甚至是編譯時(shí)指定主設(shè)備號的余地。<BR><BR><FONT
size=2><SPAN
style="COLOR: rgb(0,1,2)">以下是在scull.c中用來獲取主設(shè)備好的代碼:<BR><BR></SPAN></FONT></SPAN></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)"><SPAN
style="COLOR: rgb(0,0,255)">if</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>scull_major<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>
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><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR>
result <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN>
register_chrdev_region<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN>dev<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN>
scull_nr_devs<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(255,0,255)">"scull"</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(0,0,255)">else</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">{</SPAN><BR>
result <SPAN
style="COLOR: rgb(0,0,204)">=</SPAN>
alloc_chrdev_region<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>
scull_minor<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN>
scull_nr_devs<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN><SPAN
style="COLOR: rgb(0,0,255)"></SPAN><SPAN
style="COLOR: rgb(255,1,255)">"scull"</SPAN><SPAN
style="COLOR: rgb(0,1,255)">);</SPAN><BR>
scull_major = MAJOR<SPAN
style="COLOR: rgb(0,1,255)">(</SPAN>dev<SPAN
style="COLOR: rgb(0,1,255)">);</SPAN><BR><SPAN
style="COLOR: rgb(0,1,255)">}</SPAN><BR><SPAN
style="COLOR: rgb(0,1,255)">if (</SPAN>result
< 0<SPAN style="COLOR: rgb(0,1,255)">)
{</SPAN><BR> printk<SPAN
style="COLOR: rgb(0,1,255)">(</SPAN>KERN_WARNING
<SPAN style="COLOR: rgb(255,1,255)">"scull:
can't get major %d\n"</SPAN>, scull_major<SPAN
style="COLOR: rgb(0,1,255)">);</SPAN><BR>
return result<SPAN
style="COLOR: rgb(0,1,255)">;</SPAN><BR
style="COLOR: rgb(0,1,255)"><SPAN
style="COLOR: rgb(0,1,255)">}</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)"><BR></SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><FONT
size=3></FONT>
<P><FONT size=3><BR
style="COLOR: rgb(255,1,2)"><SPAN
style="COLOR: rgb(255,1,2)">在這部分中,比較重要的是在用函數(shù)獲取設(shè)備編號后,其中的參數(shù)name是和該編號范圍關(guān)聯(lián)的設(shè)備名稱,它將出現(xiàn)在/proc/devices和sysfs中。<BR><BR
style="COLOR: rgb(255,1,2)"><SPAN
style="COLOR: rgb(255,1,2)">看到這里,就可以理解為什么mdev和udev可以動態(tài)、自動地生成當(dāng)前系統(tǒng)需要的設(shè)備文件。</SPAN></SPAN><SPAN
class=search_hit
style="COLOR: rgb(255,1,2)">udev</SPAN><SPAN
style="COLOR: rgb(255,1,2)">就是通過讀取</SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)"
size=3>sysfs</FONT><FONT size=3><SPAN
style="COLOR: rgb(255,1,2)">下的信息來識別硬件設(shè)備的.<BR><SPAN
style="COLOR: rgb(0,1,255)">(請看《</SPAN></SPAN></FONT><FONT
style="FONT-SIZE: 14pt; COLOR: rgb(0,1,255)"
color=#02368d size=3>理解和認(rèn)識udev</FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,255)">》</SPAN><BR
style="COLOR: rgb(0,1,255)"><SPAN
style="COLOR: rgb(0,1,255)">URL:http://blog.chinaunix.net/u/6541/showart_396425.html)</SPAN><BR></FONT></P>
<P></P>
<HR style="WIDTH: 100%; HEIGHT: 2px">
<P></P>
<P><FONT style="COLOR: rgb(0,1,255)"
size=4>二、一些重要的數(shù)據(jù)結(jié)構(gòu)</FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><BR><SPAN
style="COLOR: rgb(0,1,2)">大部分基本的驅(qū)動程序操作涉及及到三個重要的內(nèi)核數(shù)據(jù)結(jié)構(gòu),分別是file_operations、file和inode,它們的定義都在</SPAN></FONT><FONT
size=3><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)"><linux/fs.h><SPAN
style="COLOR: rgb(0,1,2)">。<BR></SPAN></SPAN></SPAN></FONT><FONT
style="COLOR: rgb(0,1,255)" size=4><BR></FONT></P>
<P></P>
<HR style="WIDTH: 100%; HEIGHT: 2px">
<P></P>
<P><FONT style="COLOR: rgb(0,1,255)"
size=4>三、字符設(shè)備的注冊</FONT><BR><FONT size=3><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"><SPAN
style="COLOR: rgb(0,1,255)"><SPAN
style="COLOR: rgb(0,1,2)"><BR></SPAN></SPAN></SPAN></FONT><FONT
style="COLOR: rgb(255,1,2)" size=3><SPAN
style="COLOR: rgb(0,1,2)">內(nèi)核內(nèi)部使用<SPAN
style="COLOR: rgb(0,1,255)">struct
cdev</SPAN>結(jié)構(gòu)來表示字符設(shè)備。在內(nèi)核調(diào)用設(shè)備的操作之前,必須分配并注冊一個或多個</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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -