|
设备驱动程序试水,后续也会到版上请教些问题,希望大虾们多多指教啊。
1. 准备内核环境
在内核源代码文件夹(/user/src/linux-2.6*)下执行:
make oldconfig #配置内核支持动态加载模块
make prepare
make scripts
2. 编写hello模块(hello.c)
#include "linux/init.h"
#include "linux/module.h"
static int __init hello_init(void)
{
printk(KERN_ALERT "hello\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "bye.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("LGPL");
MODULE_DESCRIPTION("a hello module for test");
3. 编写Makefile
obj-m := hello.o
KERN_DIR='/usr/src/linux-2.6.16.60-0.54.5'
all:
$(MAKE) -C $(KERN_DIR) M=`pwd`
clean:
rm -f *.o *.ko
4. 编译和装卸
make
insmod hello.ko
lsmod | grep hello
rmmod hello.ko
注:如果终端不打印结果,可以通过 tail /var/log/messages查看结果 |
|