﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-edog-随笔分类-Linux设备驱动开发</title><link>http://www.cppblog.com/edog/category/13162.html</link><description>冰冻热狗</description><language>zh-cn</language><lastBuildDate>Fri, 18 Oct 2013 14:34:29 GMT</lastBuildDate><pubDate>Fri, 18 Oct 2013 14:34:29 GMT</pubDate><ttl>60</ttl><item><title>一个简单的Linux驱动程序和Makefile</title><link>http://www.cppblog.com/edog/archive/2013/10/18/203810.html</link><dc:creator>冰冻热狗</dc:creator><author>冰冻热狗</author><pubDate>Fri, 18 Oct 2013 14:18:00 GMT</pubDate><guid>http://www.cppblog.com/edog/archive/2013/10/18/203810.html</guid><wfw:comment>http://www.cppblog.com/edog/comments/203810.html</wfw:comment><comments>http://www.cppblog.com/edog/archive/2013/10/18/203810.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/edog/comments/commentRss/203810.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/edog/services/trackbacks/203810.html</trackback:ping><description><![CDATA[<div><p><strong><span style="color:blue">1</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、源程序</span></strong></p>  <p>//hello.c</p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:gray;">#include&nbsp;&lt;linux/init.h&gt;&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:gray;">#include&nbsp;&lt;linux/module.h&gt;&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">MODULE_LICENSE(</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:blue;">"Dual&nbsp;BSD/GPL"</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">);&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">6.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:#006699;">static</span></strong>&nbsp;<strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:seagreen;">int</span></strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;hello_init(</span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:#006699;">void</span></strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black;border:none windowtext 1.0pt;padding:0cm;background:white;">)&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">7.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">{&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">8.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;&nbsp;&nbsp;printk(KERN_ALERT&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:blue;">"hello&nbsp;module!\n"</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">);&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">9.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:#006699;">return</span></strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black;border:none windowtext 1.0pt;padding:0cm;background:white;">&nbsp;0;&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">10.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">}&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">11.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">12.&nbsp;</span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:#006699;">static</span></strong>&nbsp;<strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#006699;">void</span></strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;hello_exit(</span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:#006699;">void</span></strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black;border:none windowtext 1.0pt;padding:0cm;background:white;">)&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">13.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">{&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">14.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;&nbsp;&nbsp;printk(KERN_ALERT&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:blue;">"bye&nbsp;module!\n"</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">);&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">15.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">}&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">16.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">17.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">module_init(hello_init);&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">18.&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">module_exit(hello_exit);&nbsp;&nbsp;</span></p>  </div>  <p>&nbsp;</p>  <p><strong><span style="color:blue">2</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、驱动程序介绍</span></strong></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">一个</span>linux<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">内核模块主要由如下几个部分组成：</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>1<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">加载函数。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">当通过</span>insmod<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">或</span>modprobe<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">命令加载内核</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">时，</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的加载函数会自动被内核运行，完成本</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的相关初始化工作。</span></p>  <p>&nbsp;</p>  <p>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">加载函数通过</span>module_init()<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">函数向内核注册。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>2<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">卸载函数。</span></p>  <p>&nbsp;</p>  <p>rmmod<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">命令卸载某个模块时，模块的卸载函数会自动被内核执行，完成本模块初始化的相反功能。</span></p>  <p>&nbsp;</p>  <p>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">卸载函数通过</span>module_exit()<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">函数向内核注册。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>3<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">许可声明（必须）</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">许可证</span>license<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">声明描述内核模块的许可权限，如果不声明</span>license<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，模块被加载时，将，收到内核被污染（</span>kernel tainted<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）的警告。</span>linux<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中可接受的</span>license<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">包括&#8220;</span>GPL<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">&#8221;，&#8220;</span>GPL v2<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">&#8221;，&#8220;</span>Dual BSD/GPL<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">&#8221;，&#8220;</span>Dual MPL/GPL<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">&#8221;等。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">多数情况下，内核模块应遵循</span>GPL<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">兼容许可权，</span>2.6<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">内核模块最常见的是以</span><span>MODULE_LICENSE("Dual BSD/GPL"</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）语句声明模块采用</span><span>BSD/GPL </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">双</span>LICENSE<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>4<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）模块参数（可选）</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>5<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）模块到处符号（可选）</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>6<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）模块作者等信息声明（可选），如</span>MODULE_AUTHOR<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（），</span>MODULE_DESCRIPTION<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（），</span>MODULE_ALIAS<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（）等。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">编译得到</span><strong><span style="color:red">hello.ko</span></strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，然后</span><strong><span style="color:red">insmod hello.ko</span></strong><span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">加载模块，</span><strong><span style="color:red">rmmod hello.ko</span></strong> <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">卸载模块。</span></p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p>linux<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">内核的整体结构已经非常庞大，而其包含的组件也非常多，有两种方法把需要的部分都包含在内核中</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">一，把所有功能都编译进内核，但这回导致两个问题，生成的内核会特别打，假如要把现在的内核增加或删除功能，将不得不重新编译整个内核。</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">二，使用模块</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，上述我们写的最简单驱动，就是一个模块</span>module<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，可以随意的增加或删除。</span></p>  <p>&nbsp;</p>  <p><strong><span style="color:blue">3</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、</span><span style="color:blue">Makefile</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">文件</span></strong></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">怎样把</span>hello.c<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">源文件编译成</span>helo.ko<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">内核模块呢，同样使用</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，但这里的</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">与一般的应用程序</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有所不同，驱动</span>Makfile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">要指定内核源代码位置，先看一个简单的驱动</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">：</span></p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">obj-m&nbsp;:=&nbsp;hello.o&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">KERNEL_DIR&nbsp;:=&nbsp;/lib/modules/$(shell&nbsp;uname&nbsp;-r)/build&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">PWD&nbsp;:=&nbsp;$(shell&nbsp;pwd)&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">all:&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;&nbsp;&nbsp;make&nbsp;-C&nbsp;$(KERNEL_DIR)&nbsp;SUBDIRS=$(PWD)&nbsp;modules&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">6.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">clean:&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">7.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;&nbsp;&nbsp;rm&nbsp;*.o&nbsp;*.ko&nbsp;*.mod.c&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">8.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">9.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">.PHONY:clean&nbsp;&nbsp;</span></p>  </div>  <p>KERNEL_DIR<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">为内核源代码</span>build<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">目录，我们知道，内核存放在</span>/usr/src<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中，</span>/lib/modules<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">其实是连接到这个地方，在</span>shell<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中执行</span>uname -r<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会得到正在使用的完整内核版本号，这样就选择了适当的内核源码。</span></p>  <p>PWD<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">为源文件</span>hello.c<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">所在目录。</span></p>  <p>make -C <span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">（大写</span>C<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span> make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会进入</span>KERNEL_DIR<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">目录执行此目录下的</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，然后在返回</span>PWD<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">目录执行自己写的</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。</span></p>  <p><strong><span style="color:blue">4</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、在终端中</span><span style="color:blue">make</span></strong></p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">[root@localhost&nbsp;driver]#&nbsp;make&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">make&nbsp;-C&nbsp;/lib/modules/2.6.9-89.ELsmp/build&nbsp;SUBDIRS=/root/linux/driver&nbsp;modules&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">make[1]:&nbsp;Entering&nbsp;directory&nbsp;`/usr/src/kernels/2.6.9-89.EL-smp-i686'&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;CC&nbsp;[M]&nbsp;&nbsp;/root/linux/driver/hello.o&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;Building&nbsp;modules,&nbsp;stage&nbsp;2.&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">6.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;MODPOST&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">7.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;CC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/root/linux/driver/hello.mod.o&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">8.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;&nbsp;LD&nbsp;[M]&nbsp;&nbsp;/root/linux/driver/hello.ko&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">9.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">make[1]:&nbsp;Leaving&nbsp;directory&nbsp;`/usr/src/kernels/2.6.9-89.EL-smp-i686'</span></p>  </div>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这样</span>hello.ko<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">驱动模块就产生好了。</span></p>  <p>&nbsp;</p>  <p><strong><span style="color:blue">5</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、</span><span style="color:blue">insmod</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">加载</span></strong></p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&nbsp;[root@localhost&nbsp;driver]#&nbsp;insmod&nbsp;hello.ko</span></p>  </div>  <p><strong><span style="color:blue">6</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、</span><span style="color:blue">lsmod</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">查看模块</span></strong></p>  <p>lsmod<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">一下就会看到</span>hello<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模块的存在了，并且在系统的日志</span>/var/log/messages<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中会记录模块的输出，也就是</span></p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">printk(KERN_ALERT&nbsp;</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;; color:blue;">"hello&nbsp;module!\n"</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">);&nbsp;&nbsp;</span></p>  </div>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">输出的</span><span>hello module</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">！</span></p>  <div style="border:none;border-left:solid #6CE26C 2.25pt; padding:0cm 0cm 0cm 0cm;background:white;margin-left:18.0pt;margin-right:0cm">  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">&lt;pre&nbsp;name="code"&nbsp;class="plain"&gt;[root@localhost&nbsp;driver]#tail&nbsp;-1&nbsp;/var/log/messages&nbsp;&nbsp;</span></p>  <p align="left" style="margin-left:18.0pt;text-align:left;text-indent:-18.0pt;line-height:13.5pt; background:white;border:none; padding:0cm;"><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;Courier New&quot;;color:#5C5C5C;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:black; border:none windowtext 1.0pt;padding:0cm; background:white;">Oct&nbsp;13&nbsp;11:27:07&nbsp;localhost&nbsp;kernel:&nbsp;hello&nbsp;module!&nbsp;</span></p>  </div>  <p><strong><span style="color:blue">7</span></strong><strong><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;;color:blue">、</span><span style="color:blue">rmmod helle </span></strong><strong><span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;; color:blue">卸载</span><span style="color:blue">hello.ko</span></strong></p>  <p>&nbsp;</p></div><img src ="http://www.cppblog.com/edog/aggbug/203810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/edog/" target="_blank">冰冻热狗</a> 2013-10-18 22:18 <a href="http://www.cppblog.com/edog/archive/2013/10/18/203810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符设备驱动基本框架</title><link>http://www.cppblog.com/edog/archive/2010/03/02/108725.html</link><dc:creator>冰冻热狗</dc:creator><author>冰冻热狗</author><pubDate>Tue, 02 Mar 2010 06:38:00 GMT</pubDate><guid>http://www.cppblog.com/edog/archive/2010/03/02/108725.html</guid><wfw:comment>http://www.cppblog.com/edog/comments/108725.html</wfw:comment><comments>http://www.cppblog.com/edog/archive/2010/03/02/108725.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/edog/comments/commentRss/108725.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/edog/services/trackbacks/108725.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">linux</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">init.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">linux</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">module.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">asm</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">gpio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">linux</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">moduleparam.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">linux</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">mm.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">asm</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">io.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;DEVICE_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"LEDS"</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;NUM_MAJOR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;240</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>MODULE_LICENSE(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Dual&nbsp;BSD/GPL</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;leds_ioctl(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;inode&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">inode,&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;file&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">file,&nbsp;unsigned&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cmd,&nbsp;unsigned&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;arg)<br><img id=Codehighlighter1_334_347_Open_Image onclick="this.style.display='none'; Codehighlighter1_334_347_Open_Text.style.display='none'; Codehighlighter1_334_347_Closed_Image.style.display='inline'; Codehighlighter1_334_347_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_334_347_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_334_347_Closed_Text.style.display='none'; Codehighlighter1_334_347_Open_Image.style.display='inline'; Codehighlighter1_334_347_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_334_347_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_334_347_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;file_operations&nbsp;fops&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;<br><img id=Codehighlighter1_388_431_Open_Image onclick="this.style.display='none'; Codehighlighter1_388_431_Open_Text.style.display='none'; Codehighlighter1_388_431_Closed_Image.style.display='inline'; Codehighlighter1_388_431_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_388_431_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_388_431_Closed_Text.style.display='none'; Codehighlighter1_388_431_Open_Image.style.display='inline'; Codehighlighter1_388_431_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_388_431_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_388_431_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;owner:&nbsp;THIS_MODULE,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;ioctl:&nbsp;leds_ioctl,<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;leds_init(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)&nbsp;<br><img id=Codehighlighter1_463_537_Open_Image onclick="this.style.display='none'; Codehighlighter1_463_537_Open_Text.style.display='none'; Codehighlighter1_463_537_Closed_Image.style.display='inline'; Codehighlighter1_463_537_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_463_537_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_463_537_Closed_Text.style.display='none'; Codehighlighter1_463_537_Open_Image.style.display='inline'; Codehighlighter1_463_537_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_463_537_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_463_537_Open_Text><span style="COLOR: #000000">{&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;注册设备</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;register_chrdev(NUM_MAJOR,&nbsp;DEVICE_NAME,&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">fops);&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;leds_exit(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)&nbsp;<br><img id=Codehighlighter1_570_624_Open_Image onclick="this.style.display='none'; Codehighlighter1_570_624_Open_Text.style.display='none'; Codehighlighter1_570_624_Closed_Image.style.display='inline'; Codehighlighter1_570_624_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_570_624_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_570_624_Closed_Text.style.display='none'; Codehighlighter1_570_624_Open_Image.style.display='inline'; Codehighlighter1_570_624_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_570_624_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_570_624_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;注销设备</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;unregister_chrdev(NUM_MAJOR,DEVICE_NAME);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>module_init(leds_init);&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>module_exit(leds_exit);&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/edog/aggbug/108725.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/edog/" target="_blank">冰冻热狗</a> 2010-03-02 14:38 <a href="http://www.cppblog.com/edog/archive/2010/03/02/108725.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>