嵌入式Linux最简单驱动模块编译与剖析

2019-07-12 14:45发布

# 最简单的内核模块: #include #include
static int my_driver_init(void) { printk("hello "); return 0; }
static my_driver_exit(void) { printk("byebye "); }
module_init(my_driver_init); module_exit(my_driver)_exit);
MODULE_AUTHOR("hanshanjie"); MODULE_LICENSE("GPL");
其中,module_init()表名模块的入口,该函数在模块加载时候被调用,即insmod,modprobe时候被调用。该函数指定的函数,试分析一个驱动最先关注的,类似于main函数。
Makefile编写: CROSS_COMPILE ?= CC = $(CROSS_COMPILE)gcc obj-m := hello.o
KERNELDIR =
PWD := $(shell pwd)
deafult: $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE= modules clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE= clean
rm -f *.ko *.mod *.symvers *~*.order

实际编译时候: make -C /home/parallels/work31017/kernel_source/3.10.17-r0/ M=/home/parallels/work31017/helloko/ ARCH=arm CROSS_COMPILE=/opt/poky/1.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi- modules
过程: make: Entering directory `/home/parallels/work31017/kernel_source/3.10.17-r0' CC [M] /home/parallels/work31017/helloko/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/parallels/work31017/helloko/hello.mod.o LD [M] /home/parallels/work31017/helloko/hello.ko make: Leaving directory `/home/parallels/work31017/kernel_source/3.10.17-r0'生成: hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symver
安装驱动:insmod 显示hello
卸载驱动 rmmod 显示byebye