1. [代码]helloworld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <linux/module.h>//与module相关的信息
#include <linux/kernel.h>
#include <linux/init.h> //与init相关的函数
static
int
__init hellokernel_init(
void
)
{
printk(KERN_INFO
"Hello kernel!\n"
);
return
0;
}
static
void
__exit hellokernel_exit(
void
)
{
printk(KERN_INFO
"Exit kernel!\n"
);
}
module_init(hellokernel_init);
module_exit(hellokernel_exit);
MODULE_LICENSE(
"GPL"
);
MODULE_AUTHOR(
"xxxx"
);
|
2. [代码]Makefile
1
2
3
4
5
6
7
8
9
|
obj-m := helloworld.o
PWD := $(shell pwd)
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
rm -rf *.o *~ core .*.cmd *.mod.c ./tmp_version
|
3. [代码]执行与运行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
3)执行make
编译成功之后会生成相应有ko文件,也就是我们想要的驱动了
4)驱动程序的相关操作
a)查看ko模块的信息 modinfo helloworld.ko
b)插入模块 insmod helloworld.ko
c)卸载模块 rmmod helloworld
d)还有一个modprobe功能,以后介绍!
5)查看驱动的打印信息
使用dmesg可以查看在驱动的相关打印信息!
现在有例子是会有如下的打印内容:
---------------------log start----------------------------
[27520.195551] Exit kernel!
[27948.531569] Hello kernel!
---------------------log end----------------------------
|