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

📄 basic_char_driver.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>  <title></title>  <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />  <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />  <link rel="stylesheet" media="print" type="text/css" href="./print.css" />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><h3><a name="a_basic_char_device_driver" id="a_basic_char_device_driver">A Basic Char Device Driver</a></h3><div class="level3"><p>The following code represents a most basic device driver. This code can be compiled to run on the host or the target systems. It is broken up into sections to help understand it.</p><p>Includes and defines:</p><pre class="code c">  <span class="co1">//                                                                 </span>  <span class="co1">// Basic Character mode driver                                     </span>  <span class="co1">//                                                                 </span>                                                                       <span class="co2">#include &lt;linux/module.h&gt;                                          </span>  <span class="co2">#include &lt;linux/kernel.h&gt;                                          </span>  <span class="co2">#include &lt;linux/init.h&gt;                                            </span>  <span class="co2">#include &lt;linux/fs.h&gt;                                              </span>                                                                       <span class="co2">#define SCMD_DEV &quot;scmd_basic&quot;                                      </span>                                                                       <span class="kw4">static</span> <span class="kw4">int</span> scmd_major=<span class="nu0">250</span>;                                         &nbsp;  <span class="coMULTI">/*                                                                    * for dynamic major allocation                                       static int scmd_major=0;                                             */</span>                                                                &nbsp;</pre><p>The <strong>fops</strong> table</p><pre class="code c">&nbsp;  <span class="kw4">struct</span> file_operations scmd_driver_fops = <span class="br0">&#123;</span>                            <span class="kw2">NULL</span>,                 <span class="coMULTI">/* nothing more, fill with NULLs */</span>          <span class="br0">&#125;</span>;                                                                 &nbsp;</pre><p>This is the code that registers the module  as a device driver:</p><pre class="code c">  <span class="kw4">static</span> <span class="kw4">int</span> __init scmd_init<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span><span class="br0">&#123;</span>                                       <span class="kw4">int</span> ret;                                                       &nbsp;      <span class="co1">//SET_MODULE_OWNER(&amp;scmd_driver_fops); //not now in 2.6 kernels</span>                                                                           ret = register_chrdev<span class="br0">&#40;</span>scmd_major, SCMD_DEV,                                                          &amp;scmd_driver_fops<span class="br0">&#41;</span>;            &nbsp;      printk<span class="br0">&#40;</span><span class="st0">" Driver scmd_basic registered .. ret %d <span class="es0">\n</span>"</span>,ret<span class="br0">&#41;</span>;            <span class="kw1">if</span> <span class="br0">&#40;</span> ret &gt; <span class="nu0">0</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>                                                         scmd_major = ret;                                                 ret = <span class="nu0">0</span>;                                                                <span class="br0">&#125;</span>                                                                      <span class="kw1">return</span> ret;                                                      <span class="br0">&#125;</span></pre><p>Here is the cleanup code to remove the device driver:</p><pre class="code c">                                                                       <span class="kw4">static</span> <span class="kw4">void</span> __exit scmd_exit<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>                                     <span class="kw4">int</span> ret;                                                             ret = unregister_chrdev <span class="br0">&#40;</span>scmd_major, SCMD_DEV<span class="br0">&#41;</span>;                      printk<span class="br0">&#40;</span><span class="st0">" Driver scmd_basic unregistered .. ret %d <span class="es0">\n</span>"</span>,ret<span class="br0">&#41;</span>;      <span class="br0">&#125;</span></pre><p> This is the way the module entry and exit fuinctions are defined:</p><pre class="code c">                                                                       module_init<span class="br0">&#40;</span>scmd_init<span class="br0">&#41;</span>;                                              module_exit<span class="br0">&#40;</span>scmd_exit<span class="br0">&#41;</span>;                                            &nbsp;</pre><p> Although this device driver does not have any actual <strong>Device Driver</strong> code it does perform the  proper kernel functions to get to the point where the driver code can be added. </p></div></body></html>

⌨️ 快捷键说明

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