HelloWorld Driver
来源:互联网 发布:ios更新系统 数据丢失 编辑:程序博客网 时间:2024/04/23 16:23
尝试编写一个简单的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.oKDIR = /home/jacky.zhu/marshmallow-maserati-rel/vendor/mstar/kernel/linaroifneq ($(KDIR), $(wildcard $(KDIR) )) $(info KDIR not exist,you should appoint proper KDIR value in Makefile.)endifall:make -C $(KDIR)/ SUBDIRS=$(PWD) modulesclean: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
0 0
- HelloWorld Driver
- OpenRisc-11-编写ipcore 的linux driver,然后run helloworld
- OpenRisc-11-编写ipcore 的linux driver,然后run helloworld
- Driver
- driver
- helloworld
- HelloWorld
- helloworld
- helloworld
- HelloWorld
- HelloWorld
- HelloWorld
- HelloWorld
- HelloWorld
- HelloWorld
- helloWorld!
- helloworld!
- HelloWorld
- Ultra-Pull-To-Refresh动态添加View不能正常显示BUG解决方法
- js时间段列表生成方法
- Android自定义捕获Application全局异常
- hdu 1281 棋盘游戏 二分图匹配 匈牙利算法 暴力
- JAVA中的阻塞队列
- HelloWorld Driver
- 欢迎使用CSDN-markdown编辑器
- Apache OFBiz 学习笔记 之 服务引擎 二
- javascript中的||操作符与&&操作符
- 彻底解决数据库去中心化问题
- “睡城”通州的逆袭:房价大涨40% 限制人口落户
- String详解(一):如何理解String
- php 将中文字符串转成汉语拼音
- C语言pthread_cond_wait与pthread_cond_signal的使用