HelloWorld Driver

尝试编写一个简单的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>");


Makefile:

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);

在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即可生成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 #

可以看到,在driver加载和卸载时加的打印信息在串口可以直接看到。

PS:
printk默认输出在控制台是看不到的,需要使用如下方式查看:
echo 8 > /proc/sys/kernel/printk    
or
dmesg



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值