📄 ioctl_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><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="clear"><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#device_driver_ioctl_interface" class="toc">Device Driver IOCTL interface</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#ioctl_user_space" class="toc">IOCTL User Space</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#ioctl_-_kernel_space" class="toc">IOCTL - Kernel Space</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#ioctl_commands" class="toc">IOCTL Commands</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#handling_user_space_data" class="toc">Handling User Space Data</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#ioctl_example_code" class="toc">IOCTL Example code</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#ioctl_addition_to_driver" class="toc">IOCTL Addition to Driver</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#ioctl_fops_table" class="toc">IOCTL Fops Table</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#user_code" class="toc">User code</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="device_driver_ioctl_interface" id="device_driver_ioctl_interface">Device Driver IOCTL interface</a></h2><div class="level2"><p> These interfaces are used to provide access into the device kernel space. Normally setup and performance data is managed with ioctl functions.</p><p>The IOCTL interface provides a wide range of kernel interface options. It is used to control characteristics of the driver (and in fact any other global kernel space variables).</p><p>The basic use of an IOCTL call involved some kind of a command switch with an associated optional data item. There is a convention applied to the way the command switch is formulated.</p><p>Currently all IOCTL calls are serialized using the Big Kernel Lock. This means only one IOCTL can be in progress at one time. More of an issue with multi CPU systems.</p></div><!-- SECTION [1-718] --><h3><a name="ioctl_user_space" id="ioctl_user_space">IOCTL User Space</a></h3><div class="level3"><p> The user space interface is: int ioctl(int fd , int cmd , var * data ) </p><p>An example:</p><pre class="code c"> ioctl<span class="br0">(</span>fd, TCSETA, tp<span class="br0">)</span>;</pre></div><!-- SECTION [719-898] --><h3><a name="ioctl_-_kernel_space" id="ioctl_-_kernel_space">IOCTL - Kernel Space</a></h3><div class="level3"><p> In Kernel space the most common way to implement an IOCTL function is to use a large switch statement.</p><p>The kernel function servicing ioctl’s is called <strong>sys_ioctl</strong> in the file <strong>linux-2.6.x/fs/ioctl.c</strong>.</p><p>Question What do the lock_kernel() / unlock_kernel() functions do ?</p><pre class="code c"> <span class="kw4">static</span> <span class="kw4">int</span> tty_ioctl<span class="br0">(</span><span class="kw4">struct</span> inode * inode, <span class="kw4">struct</span> file * file, <span class="kw4">unsigned</span> <span class="kw4">int</span> cmd, <span class="kw4">unsigned</span> <span class="kw4">long</span> arg<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">switch</span> <span class="br0">(</span>cmd<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">case</span> TIOCSTI: <span class="kw1">return</span> tiocsti<span class="br0">(</span>tty, p<span class="br0">)</span>; <span class="kw1">case</span> TIOCGWINSZ: <span class="kw1">return</span> tiocgwinsz<span class="br0">(</span>tty, p<span class="br0">)</span>; <span class="kw1">case</span> TIOCSWINSZ: <span class="kw1">return</span> tiocswinsz<span class="br0">(</span>tty, real_tty, p<span class="br0">)</span>; <span class="br0">}</span> <span class="br0">}</span></pre></div><!-- SECTION [899-2072] --><h3><a name="ioctl_commands" id="ioctl_commands">IOCTL Commands</a></h3><div class="level3"><p> The commands used to trigger the various functions must be well defined and understood by both the kernel code and the user application. These definitions are normally held in an include file shared with user space.</p></div><!-- SECTION [2073-2314] --><h3><a name="handling_user_space_data" id="handling_user_space_data">Handling User Space Data</a></h3><div class="level3"><p> The user space data address must be copied into kernel space. The <strong>copy_to_user</strong> and <strong>copy_from_user</strong> macros can be used.</p></div><!-- SECTION [2315-2476] --><h3><a name="ioctl_example_code" id="ioctl_example_code">IOCTL Example code</a></h3><div class="level3"><p> This is a code example for implementing an IOCTL interface. The DATA array for a device will be written, read and exchanged.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -