`
cloudtech
  • 浏览: 4617320 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Linux 2.6以上版本驱动程序如何开发

 
阅读更多

Linux内核版本若高于2.4,其驱动程序在高版本上移植开发,会有很多不同。

在2.4版本里面,可以简单的利用GCC生成。O文件,然后用insmod加载到内核中,但是在2.6版本上会不可以,会报

格式不正确的错误,如何解决这个问题,由于版本差异,在2.6版本上运行驱动程序是以.ko格式存在的

下面以那个经典的例子:

先在/home里创建一个新目录hello,里面有二个文件

一个是源文件:

//#ifndef __KERNEL__
//#define __KERNEL__
//#endif
//#ifndef _MODULE_
//#define _MODULE_
//#endif

#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/init.h>
#include<linux/config.h>

int init_module(void){
printk("<1>Hello World");
return 0;
}

void cleanup_module(void){
printk("<1>clean up module");
}

//module_init(init_module);
//module_exit(cleanup_module);

另一个是Makefile

obj-m:=hello.o
KDIR:=/lib/modules/$(shell uname -r)/build
SRCPWD:=$(shell pwd)
all:
make -C $(KDIR) M=$(SRCPWD) modules
clean:
rm -rf hello.o
~

然后运行:make,

会发现在当前目录下生成很多文件,其中有一个是hello.ko

就可以,当然在挂接时要用hello.ko

如下所示:insmod ./hello.ko

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics