尝试编写一个简单的driver,作为学习kernel的敲门砖。
source code(hello_world.c):
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
static int word_count_init(void)
{
printk(KERN_ALERT "word_count_init_sucess \n");
return 0;
}
static void word_count_exit(void)
{
printk(KERN_ALERT "word_count_init_exit_sucess \n");
}
//注册初始化linux驱动的函数
module_init(word_count_init);
//注册退出linux驱动的函数
module_exit(word_count_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mstar jacky.zhu<jacky.zhu@mstarsemi.com>");
obj-m := hello_world.o
KDIR = /home/jacky.zhu/marshmallow-maserati-rel/vendor/mstar/kernel/linaro
ifneq ($(KDIR), $(wildcard $(KDIR) ))
$(info KDIR not exist,you should appoint proper KDIR value in Makefile.)
endif
all:
make -C $(KDIR)/ SUBDIRS=$(PWD) modules
clean:
make -C $(KDIR)/ SUBDIRS=$(PWD) clean
printk(KERN_ALERT "word_count_init_sucess \n");
在kernel中,打印debug信息,需要使用printk,这个函数类似C预言中的print。
其中KERN_ALERT 表示高优先级,加上此高优先级后,可以直接在控制台查看此打印消息。
static int word_count_init(void)
{
printk(KERN_ALERT "word_count_init_sucess \n");
return 0;
}
static void word_count_exit(void)
{
printk(KERN_ALERT "word_count_init_exit_sucess \n");
}
//注册初始化linux驱动的函数
module_init(word_count_init);
//注册退出linux驱动的函数
module_exit(word_count_exit);
{
printk(KERN_ALERT "word_count_init_sucess \n");
return 0;
}
static void word_count_exit(void)
{
printk(KERN_ALERT "word_count_init_exit_sucess \n");
}
//注册初始化linux驱动的函数
module_init(word_count_init);
//注册退出linux驱动的函数
module_exit(word_count_exit);
在module_init和module_exit时,各对应一个函数,用于做初始化和退出善后逻辑。
在Makefile中,obj-m := hello_world.o -m表示此driver是一个单独的module,-y表示build_in,内置到kernel
此例已module的方式编译。
编译方法:
jacky.zhu@dell-PowerEdge-T420:~/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world$
make
make -C /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/ SUBDIRS=/home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world modules
make[1]: Entering directory `/home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40'
CC [M] /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.mod.o
LD [M] /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.ko
make -C /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/ SUBDIRS=/home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world modules
make[1]: Entering directory `/home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40'
CC [M] /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.mod.o
LD [M] /home/jacky.zhu/lmr1-hisense8488/vendor/mstar/kernel/3.10.40_lollipop_release/3.10.40/drivers/hello_world/hello_world.ko
直接make即可生成hello_world.ko
驱动加载和卸载:
shell@coconut:/system/lib/modules #
insmod hello_world.ko
[ 128.094580] word_count_init_sucess
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules # lsmod
hello_world 895 0 - Live 0x0000000000000000 (O)
bnep 10655 0 - Live 0x0000000000000000
rfcomm 31700 0 - Live 0x0000000000000000
hidp 13608 0 - Live 0x0000000000000000
bluetooth 200643 5 bnep,rfcomm,hidp, Live 0x0000000000000000
uhid 5748 0 - Live 0x0000000000000000
rtkm 2071 0 - Live 0x0000000000000000 (O)
mtprealloc 7193 0 - Live 0x0000000000000000 (PO)
btusb 52796 0 - Live 0x0000000000000000 (O)
mali 407015 20 - Live 0x0000000000000000 (O)
shell@coconut:/system/lib/modules # rmmod hello_world.ko
[ 144.243487] word_count_init_exit_sucess
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules #
[ 128.094580] word_count_init_sucess
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules # lsmod
hello_world 895 0 - Live 0x0000000000000000 (O)
bnep 10655 0 - Live 0x0000000000000000
rfcomm 31700 0 - Live 0x0000000000000000
hidp 13608 0 - Live 0x0000000000000000
bluetooth 200643 5 bnep,rfcomm,hidp, Live 0x0000000000000000
uhid 5748 0 - Live 0x0000000000000000
rtkm 2071 0 - Live 0x0000000000000000 (O)
mtprealloc 7193 0 - Live 0x0000000000000000 (PO)
btusb 52796 0 - Live 0x0000000000000000 (O)
mali 407015 20 - Live 0x0000000000000000 (O)
shell@coconut:/system/lib/modules # rmmod hello_world.ko
[ 144.243487] word_count_init_exit_sucess
shell@coconut:/system/lib/modules #
shell@coconut:/system/lib/modules #
可以看到,在driver加载和卸载时加的打印信息在串口可以直接看到。
PS:
printk默认输出在控制台是看不到的,需要使用如下方式查看:
echo 8 > /proc/sys/kernel/printk
or
dmesg