📄 basic_block_drivers.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="#kernel_2.6_block_device_drivers" class="toc">Kernel 2.6 Block Device Drivers</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#define_a_device_structure" class="toc">Define a device Structure</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#get_a_major_number" class="toc">Get a Major Number</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#define_the_sbd_ops_table" class="toc">Define the sbd_ops table</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#set_up_a_gen_disk" class="toc">Set up a Gen Disk</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#request_queue" class="toc">Request Queue</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#data_transfer" class="toc">Data Transfer</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#request_function" class="toc">Request Function</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#basic_ioctl_function" class="toc">Basic IOCTL function</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#add_the_new_device" class="toc">Add the new Device</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#clean_up" class="toc">Clean Up</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="kernel_2.6_block_device_drivers" id="kernel_2.6_block_device_drivers">Kernel 2.6 Block Device Drivers</a></h2><div class="level2"><p> The basic block device driver has undergone major changes.</p><p>The 2.6 kernel has a much simpler and cleaner interface</p><p>Here is a brief list of the new features </p><ul><li class="level1"><div class="li"> The awkward block layout definitions have been replaced by a dynamic</div></li></ul><p>generic disk subsystem.</p><ul><li class="level1"><div class="li"> The block requests have been reworked</div></li><li class="level1"><div class="li"> The Buffer Head structure has been replaced by the more elegant BIO</div></li></ul><p>system.</p><ul><li class="level1"><div class="li"> Better support for large devices</div></li><li class="level1"><div class="li"> Each block driver now has its own lock</div></li></ul><p>The steps to create a 2.6 Block Device Driver are : </p><ul><li class="level1"><div class="li"> define a device structure</div></li><li class="level1"><div class="li"> specify or get a major number</div></li><li class="level1"><div class="li"> register a name with the major number ( no fops )</div></li><li class="level1"><div class="li"> allocate and set up a gen disk system ( here is the fops table )</div></li><li class="level1"><div class="li"> define a queue ( add to the gen disk system )</div></li><li class="level1"><div class="li"> create a request service function to handle reads / writes</div></li><li class="level1"><div class="li"> create a minimal ioctl call to allow geometry information to be exposed</div></li><li class="level1"><div class="li"> use “add_disk” to start the system processing the device</div></li></ul><p> An example of each step is shown</p></div><!-- SECTION [1-1034] --><h3><a name="define_a_device_structure" id="define_a_device_structure">Define a device Structure</a></h3><div class="level3"><p> Define the structure and initialize it.</p><pre class="code c"><span class="kw4">int</span> num_sects = <span class="nu0">1024</span>;<span class="kw4">int</span> sect_size = <span class="nu0">512</span>; <span class="kw4">static</span> <span class="kw4">struct</span> simple_device <span class="br0">{</span> <span class="kw4">unsigned</span> <span class="kw4">long</span> size; <span class="co1">//device size in bytes</span> spinlock_t lock; <span class="co1">//mutex lock</span> <span class="kw4">char</span> * data; <span class="co1">//device data area</span> <span class="kw4">struct</span> gendisk *gendisk; <span class="co1">//gendisk structure</span><span class="br0">}</span> sbd_device; sbd_device.<span class="me1">size</span> = num_sects * sect_size; spin_lock_init<span class="br0">(</span>&sbd_device.<span class="me1">lock</span><span class="br0">)</span>; sbd_device.<span class="me1">data</span> = vmalloc<span class="br0">(</span>num_sects * sect_size<span class="br0">)</span>; <span class="co1">// Add check for NULL here return -ENOMEM if failed</span></pre></div><!-- SECTION [1035-1615] --><h3><a name="get_a_major_number" id="get_a_major_number">Get a Major Number</a></h3><div class="level3"><pre class="code"> #define DEVICE_NAME "sbd" int maj = register_blkdev(0,DEVICE_NAME); // error if <=0</pre></div><!-- SECTION [1616-1758] --><h3><a name="define_the_sbd_ops_table" id="define_the_sbd_ops_table">Define the sbd_ops table</a></h3><div class="level3"><pre class="code c"> <span class="co1">// prototype for the ioctl function</span><span class="kw4">int</span> sbd_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="kw4">static</span> <span class="kw4">struct</span> block_device_operations sbd_ops = <span class="br0">{</span> .<span class="me1">owner</span> = THIS_MODULE, .<span class="me1">ioctl</span> = snd_ioctl<span class="br0">}</span>; </pre></div><!-- SECTION [1759-2077] --><h3><a name="set_up_a_gen_disk" id="set_up_a_gen_disk">Set up a Gen Disk</a></h3><div class="level3"><p> Get a Gen Disk system and set it up using the sbd_ops just defined</p><pre class="code c"> <span class="kw4">int</span> num_parts = <span class="nu0">17</span>; <span class="co1">// we will get 16 partitions here</span> sbd_device.<span class="me1">gendisk</span> = alloc_disk<span class="br0">(</span>num_parts<span class="br0">)</span>; <span class="co1">// Check for NULL and terminate if required</span> sbd_device.<span class="me1">gendisk</span>-&gt;major = maj; sbd_device.<span class="me1">gendisk</span>-&gt;first_minor = <span class="nu0">0</span>; sbd_device.<span class="me1">gendisk</span>-&gt;fops = &sbd_ops; sbd_device.<span class="me1">gendisk</span>-&gt;private_data = &sbd_device;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -