📄 basic_open_close_functions.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="the_open_function" id="the_open_function">The Open Function</a></h3><div class="level3"><p> The <strong>Open</strong> device driver function is used to set up the process access to the device. Normally the following functions are performed. </p><ul><li class="level1"><div class="li"> Set up a <strong>private data</strong> structure to save the state of the driver</div></li><li class="level1"><div class="li"> Create any memory areas used by the device drivers</div></li><li class="level1"><div class="li"> Manage any locks and semaphores used to control access to the device</div></li></ul><p> The <strong>Open</strong> function should return <strong>0</strong> to indicate success or a negative <strong>error</strong> code.</p><p>The parameters for the <strong>open</strong> function are as follows </p><ul><li class="level1"><div class="li"> struct inode *inode </div></li><li class="level1"><div class="li"> struct file *file </div></li></ul><pre class="code">The inode refers to the device node found on the file system The file is a structure, created by the kernel, to hold thestate of our use of the device within the device driver</pre><p> A typical <strong>Open</strong> Function is shown here</p><pre class="code c"><span class="kw4">static</span> <span class="kw4">unsigned</span> <span class="kw4">long</span> scmd_is_open <span class="kw4">static</span> <span class="kw4">int</span> scmd_open<span class="br0">(</span><span class="kw4">struct</span> inode *inode, <span class="kw4">struct</span> file *file<span class="br0">)</span><span class="br0">{</span> <span class="co1">// allow only one user</span> <span class="kw1">if</span><span class="br0">(</span>test_and_set_bit<span class="br0">(</span><span class="nu0">0</span>, &scmd_is_open<span class="br0">)</span><span class="br0">)</span> <span class="kw1">return</span> -EBUSY; <span class="co1">// Activate the system</span> scmd_start<span class="br0">(</span><span class="br0">)</span>; <span class="kw1">return</span> nonseekable_open<span class="br0">(</span>inode, file<span class="br0">)</span>;<span class="br0">}</span> </pre><p>Note that <strong>nonseekable_open</strong> open is defined in <strong>fs/open.c</strong> </p><pre class="code c"> <span class="coMULTI">/* * This is used by subsystems that don't want seekable * file descriptors */</span><span class="kw4">int</span> nonseekable_open<span class="br0">(</span><span class="kw4">struct</span> inode *inode, <span class="kw4">struct</span> file *filp<span class="br0">)</span><span class="br0">{</span> filp->f_mode &= ~<span class="br0">(</span>FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE<span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span> </pre><p>This is a typical <strong>close</strong> function. It is sometimes called <strong>release</strong>.</p><pre class="code c"> <span class="kw4">static</span> <span class="kw4">int</span> scmd_release<span class="br0">(</span><span class="kw4">struct</span> inode *inode, <span class="kw4">struct</span> file *file<span class="br0">)</span><span class="br0">{</span> <span class="co1">// release the driver for others</span> clear_bit<span class="br0">(</span><span class="nu0">0</span>, &scmd_is_open<span class="br0">)</span>; <span class="co1">// we are done</span> <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span></pre></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -