📄 a_simple_module_example.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="level1"><div class="li"><span class="li"><a href="#module_example" class="toc">Module Example</a></span></div></li><li class="level1"><div class="li"><span class="li"><a href="#sample_module.c" class="toc">sample_module.c</a></span></div></li><li class="level1"><div class="li"><span class="li"><a href="#makefile" class="toc">Makefile</a></span></div></li><li class="level1"><div class="li"><span class="li"><a href="#building_a_kernel_which_accepts_modules" class="toc">Building a Kernel which accepts modules</a></span></div></li><li class="level1"><div class="li"><span class="li"><a href="#building_the_module" class="toc">Building the Module</a></span></div></li><li class="level1"><div class="li"><span class="li"><a href="#using_the_module" class="toc">Using the Module</a></span></div></li></ul></div></div><h1><a name="module_example" id="module_example">Module Example</a></h1><div class="level1"><p>This example shows how to create and build a module outside of the kernel tree, and install and remove it. </p><p>While it is always best to keep the module as part of the kernel source tree (in the same source directory), so it can pick up the correct header files properly, it is not necessary. This example assumes that the kernel is installed at <code>/home/uClinux-dist</code> and the example module files are created in the <code>/home/module-example</code> directory. If your installation is in different paths, you will need to change the command line build options.</p></div><!-- SECTION [1-580] --><h1><a name="sample_module.c" id="sample_module.c">sample_module.c</a></h1><div class="level1"><p>The first file which must be created is the module itself.</p><pre class="code c"><span class="co2">#include <linux/config.h></span><span class="co2">#include <linux/init.h></span><span class="co2">#include <linux/module.h></span><span class="co2">#include <linux/kernel.h></span><span class="co2">#include <linux/device.h></span><span class="co2">#include <linux/platform_device.h></span> MODULE_LICENSE<span class="br0">(</span><span class="st0">"GPL"</span><span class="br0">)</span>; <span class="kw4">static</span> <span class="kw4">int</span> sample_module_drv_probe<span class="br0">(</span><span class="kw4">struct</span> device *dev<span class="br0">)</span><span class="br0">{</span> printk<span class="br0">(</span>KERN_DEBUG <span class="st0">"probe sample module device<span class="es0">\n</span>"</span><span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span> <span class="kw4">static</span> <span class="kw4">int</span> sample_module_drv_remove<span class="br0">(</span><span class="kw4">struct</span> device *dev<span class="br0">)</span><span class="br0">{</span> printk<span class="br0">(</span>KERN_DEBUG <span class="st0">"remove sample module device<span class="es0">\n</span>"</span><span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span> <span class="kw4">static</span> <span class="kw4">struct</span> device_driver sample_module_driver = <span class="br0">{</span> .<span class="me1">name</span> = <span class="st0">"sample_module"</span>, .<span class="me1">bus</span> = &platform_bus_type, .<span class="me1">probe</span> = sample_module_drv_probe, .<span class="me1">remove</span> = sample_module_drv_remove,<span class="br0">}</span>; <span class="kw4">static</span> <span class="kw4">int</span> __init sample_module_init<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="br0">{</span> printk<span class="br0">(</span>KERN_DEBUG <span class="st0">"init sample module device<span class="es0">\n</span>"</span><span class="br0">)</span>; <span class="kw1">return</span> driver_register<span class="br0">(</span>&sample_module_driver<span class="br0">)</span>;<span class="br0">}</span> <span class="kw4">static</span> <span class="kw4">void</span> __exit sample_module_cleanup<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="br0">{</span> printk<span class="br0">(</span>KERN_DEBUG <span class="st0">"cleanup sample module device<span class="es0">\n</span>"</span><span class="br0">)</span>; driver_unregister<span class="br0">(</span>&sample_module_driver<span class="br0">)</span>;<span class="br0">}</span> module_init<span class="br0">(</span>sample_module_init<span class="br0">)</span>;module_exit<span class="br0">(</span>sample_module_cleanup<span class="br0">)</span>;</pre></div><!-- SECTION [581-1748] --><h1><a name="makefile" id="makefile">Makefile</a></h1><div class="level1"><p>The 2nd file which must be created is the Makefile for the module.</p><pre class="code">obj-m += sample_module.o</pre><p>If there were multiple c files (<code>file1.c</code> and <code>file2.c</code>) which needed to be linked for the module to build properly, the <code>Makefile</code> would look something like: </p><pre class="code">obj-m := sample_module.omodule-objs := file1.o file2.o</pre></div><!-- SECTION [1749-2117] --><h1><a name="building_a_kernel_which_accepts_modules" id="building_a_kernel_which_accepts_modules">Building a Kernel which accepts modules</a></h1><div class="level1"><p>Since the kernel and module share resources, the kernel must be set up properly to install and remove modules.</p><p>Make sure the following settings are enabled: </p><pre class="code">Linux Kernel Configuration Loadable module support [*] Enable loadable module support [*] Module unloadingApplication Main Menu BusyBox [*] insmod [*] insmod: lsmod [*] insmod: modprobe [*] insmod: rmmod [*] insmod: 2.6 and above kernel modules [*] insmod: Model version checks [*] insmod: load in kernel memory</pre><p>and then what ever other kernel/application options you may want to set for your build and platform. When building a kernel with loadable module support, but no loadable modules, you may get warnings like: </p><pre class="code">BFD: /home/uClinux-dist/images/linux: warning: Empty loadable segment detected, is this intentional ?</pre><p> This is normal.</p></div><!-- SECTION [2118-3040] --><h1><a name="building_the_module" id="building_the_module">Building the Module</a></h1><div class="level1"><p>To build the module, on the host do: (you will need to change the paths, to suit your installation)</p><p></p><pre class="code">rgetz@home:~/uClinux-dist> make -C /home/uClinux-dist/linux-2.6.x SUBDIRS=/home/module_example modulesmake: Entering directory `/home/uClinux-dist/linux-2.6.x' CC [M] /home/module_example/sample_module.o Building modules, stage 2. MODPOST CC /home/module_example/sample_module.mod.o LD [M] /home/module_example/sample_module.komake: Leaving directory `/home/uClinux-dist/linux-2.6.x'</pre><p> This will leave a few new files in the <code>/home/module_example</code> directory </p><pre class="code">rgetz@home:~/uClinux-dist> ls -l /home/module_exampletotal 24-rw-r--r-- 1 rgetz users 25 2005-09-28 19:14 Makefile-rw-r--r-- 1 rgetz users 862 2005-09-28 19:13 sample_module.c-rw-r--r-- 1 rgetz users 2682 2005-09-28 19:18 sample_module.ko-rw-r--r-- 1 rgetz users 454 2005-09-28 19:18 sample_module.mod.c-rw-r--r-- 1 rgetz users 1248 2005-09-28 19:18 sample_module.mod.o-rw-r--r-- 1 rgetz users 1944 2005-09-28 19:18 sample_module.o</pre><ul><li class="level1"><div class="li"> <code>sample_module.o</code> is the object file of the module, this can not be loaded into the kernel.</div></li><li class="level1"><div class="li"> <code>sample_module.ko</code> is the kernel module, it is the file which can be dynamically loaded and unloaded from the kernel.<pre class="code">rgetz@home:~module_example> file sample_module.kosample_module.ko: ELF 32-bit LSB relocatable, version 1 (SYSV), not stripped</pre></div></li><li class="level1"><div class="li"> <code>sample_module.mod.c</code> is generated by kernel Makefile automatically. It defines where to link the module initialization and dependency data.</div></li></ul></div><!-- SECTION [3041-4815] --><h1><a name="using_the_module" id="using_the_module">Using the Module</a></h1><div class="level1"><ol><li class="level1"><div class="li"> After the kernel is booted, make sure you are running a kernel which supports modules, This can be done by:<pre class="code">root:~> lsmodModule Size Used bylsmod: /proc/modules: No such file or directory</pre><p>or by checking the <code>/proc/modules</code> directory</p><pre class="code">root:~> ls /proc/modulesls: /proc/modules: No such file or directory</pre><p>If you are running a kernel which supports loadable modules, you should see something that looks like:</p><pre class="code">root:~> lsmodModule Size Used byroot:~> ls -l /proc/modules-r--r--r-- 1 0 0 0 /proc/modules</pre></div></li><li class="level1"><div class="li"> Load the module to the target, via network (ftp, rcp, nfs, wget) or serial (if you have compiled in zmodem support)</div></li><li class="level1"><div class="li"> Install the module<pre class="code">root:~> insmod ./sample_module.koroot:~> lsmodModule Size Used bysample_module 1396 0 - Live 0x00653000</pre></div></li><li class="level1"><div class="li"> This is only a sample module framework. It does nothing. If you want it to do anything, please add specific code into it.</div></li><li class="level1"><div class="li"> Remove the module<pre class="code">root:~> rmmod sample_module</pre></div></li><li class="level1"><div class="li"> Make sure the module is gone:<pre class="code">root:~> lsmodModule Size Used by</pre></div></li></ol></div><!-- SECTION [4816-] --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -