step
Adding modules to the kernel
- Write Driver
- Add drivers to the linux source directory, typically inside / Drivers
- Add the project compilation option corresponding to the driver in the Kconfig file where the directory is located
- Add compilation statements for new drivers to the Makefile file file in the directory
Test Dynamic Add Module
- Files to be written are test.c add_sub.h add_sub.c and Makefile under test.c and add_sub.c
//test.c /************************************************************************* > File Name: test.c > Author: houwenzhi > Mail: houwenzhi@163.com > Created Time: 2020 Friday, March 13, 2003, 10:41 min 16 seconds ************************************************************************/ #include <linux/init.h> #include <linux/module.h> #include "add_sub.h" #include <linux/kernel.h> static long a = 1; static long b = 1; static int AddOrSub = 1; static int test_init(void) { long result = 0; printk("test initi\r\n"); if(1 == AddOrSub) { result = add_integer(a,b); } else { result = sub_integer(a,b); } printk("The %s result is %ld",AddOrSub==1?"Add":"Sub",result); return 0; } static void test_exit(void) { printk("test exit\r\n"); } module_param(a,long,S_IRUGO); module_param(b,long,S_IRUGO); module_param(AddOrSub,int,S_IRUGO); module_init(test_init); module_exit(test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("HouWenzhi");
#Makefile obj-m :=test.o KERNELDIR ?= /home/user/source/linux-2.6.30 PWD := $(shell pwd) SYMBOL_INC = $(obj)/../$(SYMBOL_INC) KBUILD_EXTRA_SYMBOLS=$(obj)/../print/Module.symvers modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install clean: rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions .PHONY:modules modules_install clean
//add_sub.c /************************************************************************* > File Name: add_sub.c > Author: houwenzhi > Mail: houwenzhi@163.com > Created Time: 2020 Friday, March 13, 2003, 11:24 min 59 seconds ************************************************************************/ #include <linux/module.h> #include <linux/init.h> #include "../add_sub.h" long add_integer(int a,int b) { return a + b; } long sub_integer(int a,int b) { return a - b; } EXPORT_SYMBOL(add_integer); EXPORT_SYMBOL(sub_integer); MODULE_LICENSE("GPL");
#Makefile ifeq ($(KERNELRELEASE),) KERNELDIR ?= /home/user/source/linux-2.6.30 PWD := $(shell pwd) PRINT_INC = $(PWD)/../include EXTRA_CFLAGS += -I $(PRINT_INC) modules: $(MAKE) -I $(PRINT_INC) -C $(KERNELDIR) M=$(PWD) modules modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install clean: rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions .PHONY:modules modules_install clean else obj-m :=add_sub.o endif
/************************************************************************* > File Name: add_sub.h > Author: houwenzhi > Mail: houwenzhi@163.com > Created Time: 2020 Friday, March 13, 2003, 11:19 min 15 seconds ************************************************************************/ #ifndef _ADD_SUB_H_ #define _ADD_SUB_H_ long add_integer(int a,int b); long sub_integer(int a,int b); #endif
- The example above describes the communication between modules through two modules, which add_subt provides two export functions, add_integer(), and sub_integer(), that add and subtract two numbers, respectively.The module test calls two methods of the module's add_subto complete an addition or subtraction operation.
- Test module
Before loading the test module, the add_submodule needs to be loaded before the test module can access the export functions of the add_submodule body
insmod add_sub.ko
insmod test.ko a=3 b=2 AddOrSub=2- This is a dynamic loading method
Add modules to the kernel
- First, the directory of the kernel module is in the / dervers directory, under which a new directory is created, add_sub_Config
Include in Catalog
$:cd add_sub_Config $:tree . ├── add_sub.c ├── add_sub.h ├── Kconfig ├── Makefile └── test.c
- Modify Kconfig
menu "ADD_SUB" comment "ADD_SUB" config CONFIG_ADD_SUB boolean "ADD_SUB support" default y config CONFIG_TEST tristate "ADD_SUB test support" depends on CONFIG_ADD_SUB default y endmenu
- Modify Makefile
obj-$(CONFIG_ADD_SUB) +=add_sub.o obj-$(CONFIG_TEST) +=test.o
3. Modify the Kconfig of the upper directory, add
source "drivers/add_sub_Kconfig/Kconfig"
- Modify the Makefile in the upper directory, add
obj-$(ADD_SUB) += add_sub_Kconfig/
Configuring the add_sub module
$:make menuconfig
The Add_Sub module is theoretically found in the Device Drivers directory, but there is no such module. After repeated experimentation, no results are found and recorded