LDD 2 How to write kernel module and compile it - limingth/LASO GitHub Wiki
get led driver from git
https://github.com/limingth/ARM-Lessons/tree/master/CortexA8-s5pv210-20120901/tiny210/drivers/char
know more about Kernel Module
http://hi.baidu.com/_tobsp/item/fbce401590b2df0ad1d66d4a
2.4 vs 2.6 kernel module
http://blog.csdn.net/pottichu/article/details/1892203
http://hi.baidu.com/wenes/blog/item/ddc167c7d76782d3d100601c.html
write hello.c kernel module
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world");
}
module_init(hello_init);
module_exit(hello_exit);
write a Makefile to compile
2.6 内核中编译外部模块的 Makefile
代码:
引用
01 obj-m := hello.o
02
03 KDIR := /lib/modules/$(shell uname -r)/build
04 PWD := $(shell pwd)
05
06 default:
07 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
08
09 clean:
10 rm -rf *.ko
11 rm -rf *.mod.*
12 rm -rf .*.cmd
13 rm -rf *.o
第 1 行
定义生成模块的名称。没有特殊约定时,hello.c 将会成为编译成 hello.c 的源代码文件。
第 3 行
指定内核源代码的位置
第 4 行
指定编译对象模块源代码所在位置的当前目录。
第 7 行
指定编译模块的命令。
第 8 行
利用编译结果清除所有的生成文件。
编译后,会生成许多文件,2.6 内核中生成的模块实际名称为 hello.ko , 也可以使用 hello.o 。
compile and test it
make
make V=1 (verbose)
insmod hello.ko
rmmod hello