⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux设备驱动程序学习(0)-hello, world!模块 - linux设备驱动程序 - tekkaman ninja.htm

📁 Linux设备驱动程序学习(0)-Hello, world!模块 - Linux设备驱动程序.rar
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                              style="COLOR: rgb(0,0,0)">MODULE_</SPAN></CODE><FONT 
                              size=3>声明习惯上放在文件最后。<BR><BR></FONT><FONT 
                              size=3>(12)初始化和关闭<BR>初始化的实际定义通常如下:<BR><BR></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)">static <SPAN 
                                style="COLOR: rgb(0,0,255)">int</SPAN> <SPAN 
                                style="COLOR: rgb(102,1,153)">_ 
                                _</SPAN></SPAN></CODE><CODE 
                                style="COLOR: rgb(102,1,153)">init</CODE><CODE><SPAN 
                                style="COLOR: rgb(0,0,0)"> 
                                initialization_function<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>void<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR>{<BR><SPAN 
                                style="COLOR: rgb(0,1,255)">/*初始化代码*/</SPAN><BR>}<BR><BR>module_init<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>initialization_function<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><BR><FONT 
                              size=3>清除函数的实际定义通常如下:<BR><BR></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,1,2)">static</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">int</SPAN> <SPAN 
                                style="COLOR: rgb(102,1,153)">_ _exit</SPAN> 
                                cleanup_function<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,1,2)">void</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,1,2)">{</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,1,255)">/*清除代码*/</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,1,2)">}</SPAN><BR><BR>module_exit<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>cleanup_function<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><BR><FONT 
                              size=3>(13) 
                              Linux内核模块的初始化出错处理一般使用“goto”语句。通常情况下很少使用“goto”,但在出错处理是(可能是唯一的情况),它却非常有用。在大二学习C语言时,老师就建议不要使用“goto”,并说很少会用到。在这里也是我碰到的第一个建议使用“goto”的地方。<SPAN 
                              style="COLOR: rgb(0,1,255)">“<SPAN 
                              style="COLOR: rgb(255,1,2)">在追求效率的代码中使用goto语句仍是最好的错误恢复机制。</SPAN>”--</SPAN></FONT><SPAN 
                              style="COLOR: rgb(0,1,255)">《Linux设备驱动程序(第3版)》</SPAN><FONT 
                              size=3>以下是</FONT><FONT 
                              size=3>初始化出错处理的推荐代码示例:<BR><BR></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)">struct</SPAN> 
                                something <SPAN 
                                style="COLOR: rgb(0,0,204)">*</SPAN>item1<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">struct</SPAN> 
                                somethingelse <SPAN 
                                style="COLOR: rgb(0,0,204)">*</SPAN>item2<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">int</SPAN> 
                                stuff_ok<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN></SPAN></CODE></P>
                                <P 
                                style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN 
                                style="COLOR: rgb(0,0,0)"><SPAN 
                                style="COLOR: rgb(0,0,204)"></SPAN><BR>void 
                                my_cleanup<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>void<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR>{<BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>item1<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR></SPAN></CODE></P>
                                <P 
                                style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN 
                                style="COLOR: rgb(0,0,0)">&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; release_thing<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>item1<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>item2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; release_thing2<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>item2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>stuff_ok<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; unregister_stuff<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>&nbsp;&nbsp;&nbsp; 
                                return<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>}<BR><SPAN 
                                style="COLOR: rgb(0,0,255)">int</SPAN> __init 
                                my_init<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>void<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR>{<BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                int</SPAN> err <SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">-</SPAN>ENOMEM<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                item1 <SPAN style="COLOR: rgb(0,0,204)">=</SPAN> 
                                allocate_thing<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>arguments<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                item2 <SPAN style="COLOR: rgb(0,0,204)">=</SPAN> 
                                allocate_thing2<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>arguments2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">!</SPAN>item2 <SPAN 
                                style="COLOR: rgb(0,0,204)">|</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">|</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">!</SPAN>item2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; goto</SPAN> fail<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                err <SPAN style="COLOR: rgb(0,0,204)">=</SPAN> 
                                register_stuff<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>item1<SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN> item2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">!</SPAN>err<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,204)"></SPAN>&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; stuff_ok <SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> 1<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                else</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; goto</SPAN> fail<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                return 0<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN> /<SPAN 
                                style="COLOR: rgb(0,0,204)">*</SPAN> success 
                                <SPAN style="COLOR: rgb(0,0,204)">*</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">/</SPAN><BR></SPAN></CODE></P>
                                <P 
                                style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN 
                                style="COLOR: rgb(0,0,0)">&nbsp;fail<SPAN 
                                style="COLOR: rgb(0,0,204)">:</SPAN><BR>&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; my_cleanup<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>&nbsp;&nbsp;&nbsp; 
                                &nbsp;&nbsp;&nbsp; return err<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>}</SPAN></CODE></P></TD></TR></TBODY></TABLE><BR><FONT 
                              size=4>(14)模块参数:<SPAN 
                              style="COLOR: rgb(0,1,255)">内核允许对驱动程序指定参数,而这些参数可在装载驱动程序模块时改变</SPAN>。<BR><FONT 
                              size=2>以下是我的实验程序:</FONT><BR><BR></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,204)">#</SPAN><SPAN 
                                style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">&lt;</SPAN>linux<SP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -