helloworld模块:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$vim hello.c
1 /*======================================================================
2 A simple kernel module: "hello world"
3
4 The initial developer of the original code is Baohua Song
5 <author@linuxdriver.cn>. All Rights Reserved.
6 ======================================================================*/
7 #include <linux/init.h>
8 #include <linux/module.h>
9 MODULE_LICENSE("Dual BSD/GPL");
10 static int hello_init(void)
11 {
12 printk(KERN_INFO " Hello World enter\n");
13 return 0;
14 }
15
16 static void hello_exit(void)
17 {
18 printk(KERN_INFO " Hello World exit\n ");
19 }
20
21 module_init(hello_init);
22 module_exit(hello_exit);
23
24 MODULE_AUTHOR("Song Baohua");
25 MODULE_DESCRIPTION("A simple Hello World Module");
26 MODULE_ALIAS("a simplest module");
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$vim Makefile
1 obj-m := hello.o
2
3 clean:
4 rm -rf *.ko *.mod.c *.o *.order *.symvers
编译模块:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$make -C /usr/src/linux-headers-2.6.35-22-generic/ M=$(pwd) modules
make:进入目录'/usr/src/linux-headers-2.6.35-22-generic'
CC [M] /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.mod.o
LD [M] /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.ko
make:离开目录“/usr/src/linux-headers-2.6.35-22-generic”
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symvers
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$sudo insmod hello.ko
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$tail -n 2 /var/log/messages
Jan 24 11:13:15 xxha-OptiPlex-780 kernel: [ 6909.919001] book num:5000
Jan 24 13:38:43 xxha-OptiPlex-780 kernel: [15638.098334] Hello World enter
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$modinfo hello.ko //查询模块的信息。
filename: hello.ko
alias: a simplest module
description: A simple Hello World Module
author: Song Baohua
license: Dual BSD/GPL
srcversion: AF31C8A811E72E11F852B3E
depends:
vermagic: 2.6.35.4 SMP mod_unload modversions
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$sudo rmmod hello.ko
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$tail -n 2 /var/log/messages
Jan 24 13:38:43 xxha-OptiPlex-780 kernel: [15638.098334] Hello World enter
Jan 24 13:40:34 xxha-OptiPlex-780 kernel: [15748.893361] Hello World exit
这就是helloworld.ko, 超简单。