linux设备驱动学习(一)——简单的helloworld模块
在内核驱动中新建hello文件夹
1.需要.c文件与Makefile文件
在..../drivers/hello目录下新建xxx.c 和Makefile文件
.c文件样例:
#include <linux/module.h>
#include <linux/kernel.h>
static char *name="likui";
module_param(name,charp,S_IRUGO);
static int count=520;
module_param(count,int,S_IRUGO);
static int __init init_hello_module(void)
{
printk(KERN_INFO"Hello, %s! This is start %d/n",name,count);
return 0;
}
static void __exit exit_hello_module(void)
{
printk(KERN_INFO "Hello, %s! This is exit/n",name);
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(exit_hello_module);
Makefile文件样例:
KVERS = $(shell uname -r)
# Kernel modules
obj-m += helloworld.o
# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0
build: kernel_modules
kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
2.编译与清除
在hello目录下执行make编译,make clean为清除编译文件
make之前
make之后文件
3.模块操作
加载模块:insmod xxx.ko
卸载模块:rmmod xxx
查看模块:lsmod
查看模块输出信息:dmesg -c xxx