Ⅰ 如何编写驱动程序
代码:
#include<linux/mole.h>
#include<linux/kernel.h>
#include<asm/io.h>
#include<linux/miscdevice.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
//流水灯代码
#define GPM4CON 0x110002e0
#define GPM4DAT 0x110002e4
static unsigned long*ledcon=NULL;
static unsigned long*leddat=NULL;
//自定义write文件操作(不自定义的话,内核有默认的一套文件操作函数)
static ssize_t test_write(struct file*filp,const char __user*buff,size_t count,loff_t*offset)
{
int value=0;
int ret=0;
ret=_from_user(&value,buff,4);
//底层驱动只定义基本操作动作,不定义功能
if(value==1)
{
*leddat|=0x0f;
*leddat&=0xfe;
}
if(value==2)
{
*leddat|=0x0f;
*leddat&=0xfd;
}
if(value==3)
{
*leddat|=0x0f;
*leddat&=0xfb;
}
if(value==4)
{
*leddat|=0x0f;
*leddat&=0xf7;
}
return 0;
}
//文件操作结构体初始化
static struct file_operations g_tfops={
.owner=THIS_MODULE,
.write=test_write,
};
//杂设备信息结构体初始化
static struct miscdevice g_tmisc={
.minor=MISC_DYNAMIC_MINOR,
.name="test_led",
.fops=&g_tfops,
};
//驱动入口函数杂设备初始化
static int __init test_misc_init(void)
{
//IO地址空间映射到内核的虚拟地址空间
ledcon=ioremap(GPM4CON,4);
leddat=ioremap(GPM4DAT,4);
//初始化led
*ledcon&=0xffff0000;
*ledcon|=0x00001111;
*leddat|=0x0f;
//杂设备注册函数
misc_register(&g_tmisc);
return 0;
}
//驱动出口函数
static void __exit test_misc_exit(void)
{
//释放地址映射
iounmap(ledcon);
iounmap(leddat);
}
//指定模块的出入口函数
mole_init(test_misc_init);
mole_exit(test_misc_exit);
MODULE_LICENSE("GPL");
include用法:
#include命令预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置;可以标识出只有在特定条件下才会被编译的某一段程序代码;可以定义类似标识符功能的宏,在编译时,预处理器会用别的文本取代该宏。
插入头文件的内容
#include命令告诉预处理器将指定头文件的内容插入到预处理器命令的相应位置。有两种方式可以指定插入头文件:
1、#include<文件名>
2、#include"文件名"
如果需要包含标准库头文件或者实现版本所提供的头文件,应该使用第一种格式。如下例所示:
#include<math.h>//一些数学函数的原型,以及相关的类型和宏
如果需要包含针对程序所开发的源文件,则应该使用第二种格式。
采用#include命令所插入的文件,通常文件扩展名是.h,文件包括函数原型、宏定义和类型定义。只要使用#include命令,这些定义就可被任何源文件使用。如下例所示:
#include"myproject.h"//用在当前项目中的函数原型、类型定义和宏
你可以在#include命令中使用宏。如果使用宏,该宏的取代结果必须确保生成正确的#include命令。例1展示了这样的#include命令。
【例1】在#include命令中的宏
#ifdef _DEBUG_
#define MY_HEADER"myProject_dbg.h"
#else
#define MY_HEADER"myProject.h"
#endif
#include MY_HEADER
当上述程序代码进入预处理时,如果_DEBUG_宏已被定义,那么预处理器会插入myProject_dbg.h的内容;如果还没定义,则插入myProject.h的内容。
Ⅱ vs2010如何编译驱动
1、安装VS2010,安装WDK 7.0(DDK);
2、新建VC++->Empty Project
3、打开Configuration Manager 并新建一个名称为“ dirver ”的Solution Configuration 并将“dirver” 设为Active Solution Configuration .
4、打开View-> property Manager。
5、在"dirver" solution configuration 上点击右键,选择Add new property Sheet。取名为“dirverProperty”. 并对他进下以下设置。
5.1. C\C++ - General - Debug Information Format = Program Database (/Zi)
5.2. C\C++ - Preprocessor - Preprocessor Definitions = _X86_ [add also DBG for Debug config]
【WIN32;_DEBUG;_X86_;i386;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;NT_UP;SRVDBG;DBG;_IDWBUILD;_WIN32_WINNT=0x0400;% (PreprocessorDefinitions)】
5.3. C\C++ - Code Generation - Enable C++ Exceptions = No
5.4. C\C++ - Code Generation - Basic Runtime Checks = Default
5.5. C\C++ - Code Generation - Buffer Security Check = No (/GS-)
5.6. C\C++ - Advanced - Calling Convention = __stdcall (/Gz)
5.7. C\C++ - Advanced - Compile As = Compile as C Code (/TC) [if you are going to use plain C]
5.8. Linker - General - Output File = $(OutDir)\$(ProjectName).sys
5.9. Linker - General - Enable Incremental Linking = Default
5.10. Linker - Input - Additional Dependencies = ntoskrnl.lib hal.lib $(NOINHERIT) [add here needed libs here e.g. ntoskrnl.lib hal.lib]
【不知道上面是不是笔误,应该为:ntoskrnl.lib;hal.lib;%(AdditionalDependencies)】
5.11. Linker - Input - Ignore All Default Libraries = Yes (/NODEFAULTLIB)
5.12. Linker - Manifest File - Generate Manifest = No
5.13. Linker - System - SubSystem = Native (/SUBSYSTEM:NATIVE)
5.14. Linker - System - Driver = Driver (/DRIVER)
5.15. Linker - Advanced - Entry Point = DriverEntry
5.16. Linker - Advanced - Base Address = 0x10000
5.17. Linker - Advanced - Randomized Base Address = Disable (/DYNAMICBASE:NO)
【这个也是错误的:应该置空】
5.18. Linker - Advanced - Data Execution Prevention (DEP) = Disable (/NXCOMPAT:NO)
【这个也是错误的:应该置空】
6. Config VC++ Directories
6.1 Open Open up property manager by clicking on Menu View->Property Manager.
6.2 Expand the project node and then the Configuration|Platform nodes, you will see "Microsoft.cpp.<Platform>.users" file for each Configuration|Platform. These are the files
for the global settings, similar to the old tools/Options/VC++ Directories.
6.3 Multi-Select "Microsoft.cpp.<Platform>.users", right click and bring up the property page window
6.4 In the property page window, click on "VC++ Directories" (for example) in the left pane, add new paths for the directories such as "Include Directories". separated by
semicolons
(eg:Include Directories config As:
$(ddkroot)\INC
$(ddkroot)\INC\WNET
$(ddkroot)\INC\DDK\WNET
Library Directories config As:
$(ddkroot)\LIB\WNET\I386
)
6.5 Make sure to save the settings before shutting down Visual Studio.
6.6 Re-launch Visual Studio and the new settings will be in effect.
6.7 Note: If you would like to only change the settings for one project, you can right click on the project and bring up the property page. Change the settings for “VC++
Directories”, these settings will be persisted to the project file.
七. OK. Have done. Now you can test it with simple code, e.g.:
#include "ntddk.h"
NTSTATUS
DriverEntry(PDRIVER_OBJECT DriverObject,PUNICODE_STRING RegistryPath)
{
return STATUS_UNSUCCESSFUL;
}
特别说明:
1.
Visual Studio 2010 在智能设备开发方面只支持Windows Phone OS 7.0。如果你要为Windows CE 5.0和Windows Mobile 6.5开发应用程序,请安装Visual Studio 2008。
2.
做驱动开发时,SDK的版本要和WDK的版本一致,即Win7 WDK要配Win7 SDK,否则会出现编译错误。VS2010里集成了Windows SDK 7.0A。
3.
如果出现类似如下编译错误,解决方法是:拷贝C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\sal.h,然后覆盖掉C:\WinDDK\7600.16385.1\inc\api\sal.h。
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C2143: syntax error : missing ')' before 'const'
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C2143: syntax error : missing '{' before 'const'
.......
Ⅲ 如何编译一个linux下的驱动模块
这是一个简单而完整的实例,对于理解Linux下的驱动模块是非常有帮助的。
1.源码如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
进入目录:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然后拷入上面书上的源码。
2.编译代码:
1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I选项指定内河源码,也就是内核源码树路径。编译结果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在网上查询有网友提示没有引入kernel.h
解决:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次编译仍然报KERN_ALERT没有声明
修改编译条件-I,再次编译:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接着我尝试在2.6内核的虚拟机上进行编译
编译过程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.执行代码,加载驱动模块:
2.4内核加载模块:
insmod ./hello.o
但是此时并没有输出printk打印的信息。但是可以在/var/log/messages 中看到打印的信息,这是由于KERN_ALERT优先级不够高。这里
需要修改为:KERN_EMERG。再次编译,加载模块即可以看到结果
2.6内核加载模块:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能会出现insmod命令找不到的错误,这可能有下面几个原因:
<1> 你的系统没有安装mole-init-tools工具,关于此问题,只需安装即可,但是一般装完系统是有这个命令的。
<2> 环境变量没有添加导致不能使用该命令。使用echo $PATH即可查看PATH环境变量,发现没有/sbin这个路径,所以你当然不能使用insmod这个命令了。解决的方法很简单,只需在命令行输入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin这个目录下,你可以使用whereis insmod查看)。
<3> insmod这个命令需要在root权限下才能使用。
加载完成后你可以输入lsmod查看hello这个模块哦。
4.卸载驱动模块:rmmod hello.
加载模块后就可在屏幕上看到如下信息:Hello world enter.
卸载时就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多个文件,则按下列方式编写Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
Ⅳ 如何编译一个linux下的驱动模块
按照《linux设备驱动开发详解》一书中的步骤实现经典例子"hello,world!"的例子。
具体步骤如下:
=============================================
1.源码如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
进入目录:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然后拷入上面书上的源码。
2.编译代码:
1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I选项指定内河源码,也就是内核源码树路径。编译结果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在网上查询有网友提示没有引入kernel.h
解决:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次编译仍然报KERN_ALERT没有声明
修改编译条件-I,再次编译:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接着我尝试在2.6内核的虚拟机上进行编译
编译过程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
Ⅳ 如何编译驱动程序
驱动的编译和上层应用程序的编译完全不同,作为初学者应该先了解一下,即使你还不懂得怎么写驱动程序。
首先安装DDK,然后随便找一个例子来测试。在菜单中找到BUILD环境菜单执行,不同的系统要使用不同的BUILD环境。会打开一个DOS窗口,这时CD到那个例子程序,输入 build –cZ回车就可以了。 驱动程序都是用一个由DDK提供的叫build.exe的工具编译的。此程序以一个名为SOURCES的文件作为输入,该文件中包含目标可执行文件的名称、类型和要创建的可执行文件的路径,注意这个文件没有后缀名。
SOURCES的文件格式:
TARGETNAME=drivername ,
- 本参数用于指定生成的设备驱动程序名称(不需后缀名),所产生的文件
- 为drivername.sys.
TARGETPATH=./lib
- 本参数用于指定生成的设备驱动程序所存放的路径. 一般采用./lib.
TARGETTYPE=DRIVER
- build能够生成许多不同的目标对象,设备驱动程序一般选用 DRIVER.
INCLUDES=path1;path2;...
- 本参数是可选的, 用于指定其他的#include文件的搜索路径.
TARGETLIBS=lib1;lib2;...
- 本参数是可选的, 用于指定其他的lib库文件的搜索路径.
SOURCES=file1.c file2.c ...
- 本参数用于指定需被编译的全部源文件名称, 后缀名不能省略,文件名之间用空格分开.
SOURCES文件是必需的,如果没有它则表示没有任何源文件需要编译。
如果要换行可以用 ‘/’ 符号,表示对上一行的继续。
也可以创建DIRS文件,DIRS文件用于指定在当前目录下必须创建的子目录。
DIRS文件格式:
DIRS文件的内容由一系列用空格分开的目录名组成
DIRS = /
subdir1 /
subdir2 /
subdir3
DIRS文件是可选的。
有的时候,会提示找不到依赖的文件(.h,.lib 之类),其实设置好 source 文件的
INCLUDES和TARGETLIBS就可以,我第一次编译时就碰到这个问题,和VC环境区别较大,但习惯就好。
Ⅵ linux下编译驱动一定要本机有的设备才能编译么
r8101_n.c: In function `rtl8101_init_one':
r8101_n.c:2832: `RTL_NET_POLL_CONTROLLER' undeclared (first use in this function)
r8101_n.c:2832: (Each undeclared identifier is reported only once
r8101_n.c:2832: for each function it appears in.)
r8101_n.c 的 2832 行:`RTL_NET_POLL_CONTROLLER' 未定义(在这个函数里第一次使用)
驱动必须和内核版本对应,而且必须安装目标内核的开发部分数据。
Linux 经常变更内核代码接口,有的时候就会取消某些变量,或者增加新的变量。所以必须对应版本。
Ⅶ 如何编译一个linux下的驱动模块
linux下编译运行驱动
嵌入式linux下设备驱动的运行和linux x86 pc下运行设备驱动是类似的,由于手头没有嵌入式linux设备,先在vmware上的linux上学习驱动开发。
按照如下方法就可以成功编译出hello world模块驱动。
1、首先确定本机linux版本
怎么查看Linux的内核kernel版本?
'uname'是Linux/unix系统中用来查看系统信息的命令,适用于所有Linux发行版。配合使用'uname'参数可以查看当前服务器内核运行的各个状态。
#uname -a
Linux whh 3.5.0-19-generic #30-Ubuntu SMPTue Nov 13 17:49:53 UTC 2012 i686 i686 i686 GNU/Linux
只打印内核版本,以及主要和次要版本:
#uname -r
3.5.0-19-generic
要打印系统的体系架构类型,即的机器是32位还是64位,使用:
#uname -p
i686
/proc/version 文件也包含系统内核信息:
# cat /proc/version
Linux version 3.5.0-19-generic(buildd@aatxe) (gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) ) #30-UbuntuSMP Tue Nov 13 17:49:53 UTC 2012
发现自己的机器linux版本是:3.5.0-19-generic
2、下载机器内核对应linux源码
到下面网站可以下载各个版本linux源码https://www.kernel.org/
如我的机器3.5.0版本源码下载地址为:https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2
下载完后,找一个路径解压,如我解压到/linux-3.5/
然后很重要的一步是:执行命令uname -r,可以看到Ubuntu的版本信息是3.5.0-19-generic
。进入linux源码目录,编辑Makefile,将EXTRAVERSION = 修改为EXTRAVERSION= -19-generic。
这些都是要配置源码的版本号与系统版本号,如果源码版本号和系统版本号不一致,在加载模块的时候会出现如下错误:insmod: error inserting 'hello.ko': -1 Invalid mole format。
原因很明确:编译时用的hello.ko的kenerl 不是我的pc的kenerl版本。
执行命令cp /boot/config-3.5.0-19-generic ./config,覆盖原有配置文件。
进入linux源码目录,执行make menuconfig配置内核,执行make编译内核。
3、写一个最简单的linux驱动代码hello.c
/*======================================================================
Asimple kernel mole: "hello world"
======================================================================*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("zeroboundaryBSD/GPL");
static int hello_init(void)
{
printk(KERN_INFO"Hello World enter\n");
return0;
}
static void hello_exit(void)
{
printk(KERN_INFO"Hello World exit\n ");
}
mole_init(hello_init);
mole_exit(hello_exit);
MODULE_AUTHOR("zeroboundary");
MODULE_DESCRIPTION("A simple HelloWorld Mole");
MODULE_ALIAS("a simplestmole");
4、写一个Makefile对源码进行编译
KERN_DIR = /linux-3.5
all:
make-C $(KERN_DIR) M=`pwd` moles
clean:
make-C $(KERN_DIR) M=`pwd` clean
obj-m += hello.o
5、模块加载卸载测试
insmod hello.ko
rmmod hello.ko
然后dmesg|tail就可以看见结果了
最后,再次编译驱动程序hello.c得到hello.ko。执行insmod ./hello.ko,即可正确insert模块。
使用insmod hello.ko 将该Mole加入内核中。在这里需要注意的是要用 su 命令切换到root用户,否则会显示如下的错误:insmod: error inserting 'hello.ko': -1 Operation not permitted
内核模块版本信息的命令为modinfo hello.ko
通过lsmod命令可以查看驱动是否成功加载到内核中
通过insmod命令加载刚编译成功的time.ko模块后,似乎系统没有反应,也没看到打印信息。而事实上,内核模块的打印信息一般不会打印在终端上。驱动的打印都在内核日志中,我们可以使用dmesg命令查看内核日志信息。dmesg|tail
可能还会遇到这种问题insmod: error inserting 'hello.ko': -1 Invalid mole format
用dmesg|tail查看内核日志详细错误
disagrees about version of symbolmole_layout,详细看这里。
http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmoles/index.html
在X86上我的办法是:
make -C/usr/src/linux-headers-3.5.0-19-generic SUBDIRS=$PWD moles
Ⅷ linux下怎么编译安装驱动
linux 编译安装驱动有两种,动态加载与静态加载 动态加载 一,编译,在指点内核树下编译,生成o文件或ko文件 二,将生成的o或ko文件拷到相应目录,一般是/lib/mole/kernel下面 三,用insmod命令加载,用rmmod命令卸载 静态加载 静态加载主linux下怎么编译安装驱动?
Ⅸ 怎么编译目标机linux设备驱动
在宿主机上安装开发工具和下载linux源码(要求版本号和目标机上的linux内核版本一致)。开发工具主要有gcc、gdb、make等,这些工具在redhat或fc中默认就安装了,在debian或Ubuntu中可以通过下面这个命令安装:
apt-get install build-essential
linux源码可以通过以下几种途径获得:
将源码解压到/usr/src/目录后,进入linux-source-(版本号)目录中执行下面几个命令:
make oldconfig
make prepare
make scripts
直接去www.kernel.org下载
通过包管理工具下载源码,在debian和Ubuntu中可以通过下面这个命令下载,
apt-get install linux-source-(版本号) ,下载后的文件在/usr/src目录中,解压到该目录即可
编写Linux驱动程序,以一个最简单的hello.c为例,hello.c的内容如下:
#include "linux/init.h"
#include "linux/mole.h"
static int hello_init(void)
{
printk(KERN_ALERT "Hello World linux_driver_mole\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbey linux_driver_mole\n");
}
mole_init(hello_init);
mole_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lpj");
写Makefile文件,一个示例如下,里面各项参数根据实际情况更改:
#sample driver mole
obj-m := hello.o
KDIR = /usr/src/linux-source-2.6.24/
all:
$(MAKE) -C $(KDIR) M=$(PWD)
.PHONY:clean
clean:
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions
编译,在hello.c和Makefile所在目录下执行 make 即可,编译后在当前目录生成hello.ko文件
加载并测试:加载使用insmod或modprobe命令来实现,如在当前路径执行如下代码:
insmod hello.ko 或 modprobe hello
注意,如果在虚拟终端加载内核的话,将看不到内核打印信息,因为内核打印信息不会输出到虚拟终端,而是输出到/proc/kmsg文件中,所以可以通过以下方式查看内核信息:
cat /proc/kmsg 会一直打印,需要Ctrl-C手动终止
dmesg 或 dmesg | tail -N ,N为一数字,表示显示最后N行
卸载:使用rmmod命令卸载驱动模块,如 rmmod hello
Ⅹ 如何把自己的驱动编译进内核或模块
我们知道若要给Linux内核添加模块(驱动)有如下两种方式:
(1)动态方式:采用insmod命令来给运行中的linux加载模块。
(2)静态方式:修改linux的配置菜单,添加模块相关文件到源码对应目录,然后把模块直接编译进内核。
对于动态方式,比较简单,下面我们介绍如何采用静态的方式把模块添加到内核。
最终到达的效果是:在内核的配置菜单中可以配置我们添加的模块,并可以对我们添加的模块进行编译。
一. 内核的配置系统组成
首先我们要了解Linux 2.6内核的配置系统的原理,比如我们在源码下运行“make menuconfig ”为神马会出现一个图形配置菜单,配置了这个菜单后又是如何改变了内核的编译策略滴。
内核的配置系统一般由以下几部分组成:
(1)Makefile:分布在Linux内核源代码中的Makefile,定义Linux内核的编译规则。
(2)配置文件(Kconfig):给用户提供配置选项,修改该文件来改变配置菜单选项。
(3)配置工具:包括配置命令解释器(对配置脚本中使用的配置命令进行解释),配置用户界面(提供字符界面和图形界面)。这些配置工具都是使用脚本语言编写的,如Tcl/TK、Perl等。
其原理可以简述如下:这里有两条主线,一条为配置线索,一条为编译线索。配置工具根据kconfig配置脚本产生配置菜单,然后根据配置菜单的配置情况生成顶层目录下的.config,在.config里定义了配置选择的配置宏定义,如下所示:
如上所示,这里定义的这些配置宏变量会在Makefile里出现,如下所示:
然后make 工具根据Makefile里这些宏的赋值情况来指导编译。所以理论上,我们可以直接修改.config和Makefile来添加模块,但这样很麻烦,也容易出错,下面我们将会看到,实际上我们有两种方法来很容易的实现。
二. 如何添加模块到内核
实际上,我们需要做的工作可简述如下:
(1)将编写的模块或驱动源代码(比如是XXOO)复制到Linux内核源代码的相应目录。
(2)在该目录下的Kconfig文件中依葫芦画瓢的添加XXOO配置选项。
(3)在该目录的Makefile文件中依葫芦画瓢的添加XXOO编译选项。
可以看到,我们奉行的原则是“依葫芦画瓢”,主要是添加。
一般的按照上面方式又可出现两种情况,一种为给XXOO驱动添加我们自己的目录,一种是不添加目录。两种情况的处理方式有点儿不一样哦。
三. 不加自己目录的情况
(1)把我们的驱动源文件(xxoo.c)放到对应目录下,具体放到哪里需要根据驱动的类型和特点。这里假设我们放到./driver/char下。
(2)然后我们修改./driver/char下的Kconfig文件,依葫芦添加即可,如下所示:
注意这里的LT_XXOO这个名字可以随便写,但需要保持这个格式,他并不需要跟驱动源文件保持一致,但最好保持一致,等下我们在修改Makefile时会用到这个名字,他将会变成CONFIG_LT_XXOO,那个名字必须与这个名字对应。如上所示,tristate定义了这个配置选项的可选项有几个,help定义了这个配置选项的帮助信息,具体更多的规则这里不讲了。
(3)然后我们修改./driver/char下的Makefile文件,如下所示:
这里我们可以看到,前面Kconfig里出现的LT_XXOO,在这里我们就需要使用到CONFIG_XXOO,实际上逻辑是酱汁滴:在Kconfig里定义了LT_XXOO,然后配置完成后,在顶层的.config里会产生CONFIG_XXOO,然后这里我们使用这个变量。
到这里第一种情况下的添加方式就完成了。
四. 添加自己目录的情况
(1)在源码的对应目录下建立自己的目录(xxoo),这里假设为/drivers/char/xxoo 。
(2) 把驱动源码放到新建的xxoo目录下,并在此目录下新建Kconfig和Makefile文件。然后给新建的Kconfig和Makefile添加内容。
Kconfig下添加的内容如下:
这个格式跟之前在Kconfig里添加选项类似。
Makefile里写入的内容就更少了:
添加这一句就可以了。
(3)第三也不复杂,还是依葫芦画瓢就可以了。
我们在/drivers/char目录下添加了xxoo目录,我们总得在这个配置系统里进行登记吧,哈哈,不然配置系统怎么找到们呢。由于整个配置系统是递归调用滴,所以我们需要在xxoo的父目录也即char目录的Kconfig和Makefile文件里进行登记。具体如下:
a). 在drivers/char/Kconfig中加入:source “drivers/char/xxoo/Kconfig”
b). 在drivers/char/Makefile中加入:obj-$(CONFIG_LT_XXOO) += xxoo/
添加过程依葫芦画瓢就可以了,灰常滴简单。