Two ways to add driver modules to the kernel

Two ways to add driver modules to the kernel

1. Place it in the kernel source tree

Summary of steps:

  1. new folder
  2. Write Makefile, write Kconfig
  3. Modify the upper Kconfig
  4. Execute make menuconfig
  5. Execute make zImage or make modules
1.1 Put the source code into the folder

For example: add a key character device module

  • Create a new folder mykey in the drivers/char directory under the kernel directory

  • Add a line to the Makefile under drivers/char/

    obj-m += mykey/
    这行指令告诉模块构建系统,在编译模块时需要进入 mykey/ 子目录中
        
    有可能的选择是,驱动程序的编译取决于一个特殊的配置选项,例如 CONFIG_MYKEY ,则指令要替换成
    obj-$(CONFIG_MYKEY) += mykey/
    
  • Finally, add a Makefile in the drivers/char/mykey/ directory, which requires the following line of instructions

    obj-m += mykey.o
    表示进入 mykey/ 子目录后,要将 mykey.c 编译成 mykey.o,但最终会生成 mykey.ko 文件
    
    如果加了编译选项,则指令如下
    obj-$(CONFIG_MYKEY) += mykey.o
    
  • In addition, if your keystroke program needs to use other source files later, you can modify the Makefile in the mykey/ directory as follows

    obj-$(CONFIG_MYKEY) += mykey.o
    mykey-objs := mykey_main.o other.o
    表示 mykey_main.c 和 other.c 会一起被编译和链接进 mykey.ko 文件
    
1.2 Place the source code in the general directory of the device

If there are only one or two source files, you can choose not to create a new folder, directly put mykey.c into the drivers/char/ directory, and add the following instructions to the Makefile under the drivers/char/ directory.

obj-m += mykey.o
    
如果配置了编译选项,则指令为
obj-$(CONFIG_MYKEY) += mykey.o
    
如果有多个源文件,则指令为
obj-$(CONFIG_MYKEY) += mykey.o
mykey-objs := mykey_main.o other.o

That is, all the contents of the original drivers/char/mykey/Makefile are written to drivers/char/Makefile.

1.3 Management configuration options
  • Create a new Kconfig file in the drivers/char/mykey/ directory (if there is no independent directory, add it directly to drivers/char/Kconfig)

  • Add the following content (example)

    config MYKEY
    	tristate "This is config of mykey"
    	default n
    	help 
    	  If you chose Y, support this key function, 
    	  this key will be compiledinto the kernel.
    	  you can also chose M, then this driver will 
    	  be built as a module named mykey.ko.
    
  1. The first line defines the configuration target represented by this option (note that the CONFIG_ prefix does not need to be written)

  2. The second line declares that the option type is "tristate", which has three options NYM. Y means it can be compiled into the kernel, M means it is compiled as a module, and N means no operation.

  3. The third line is the default option type

  4. help means providing help documentation for this option

  5. In addition to the above options, there are other options, such as the depends directive, which specifies the dependency options of this option, indicating that the dependency options must be set before this option can be selected.

    depends on MENU_KEY

  6. Add in drivers/char/Kconfig file

    source “drivers/char/mykey/Kconfig”

The result after executing make menuconfig is as follows
Insert image description here

If the content of Kconfig is changed to this (with menu)

menu "Config my key"

config MYKEY    
	tristate "This is config of mykey"    
	default n    
	help       
		If you chose Y, support this key function,       
		this key will be compiledinto the kernel.      
		you can also chose M, then this driver will       
		be built as a module named mykey.ko.

endmenu

Make menuconfig to find MYKEY (you can see an extra menu)
Insert image description here

Press "h" to enter help
Insert image description here
make menuconfig. Before taking a look at the .config file in the kernel root directory, CONFIG_MYKEY has not been defined yet.

Insert image description here
After we use make menuconfig to select the CONFIG_MYKEY option as M, take a look at the .config file in the kernel directory.
Insert image description here

Don't forget to add content in drivers/char/mykey/Makefile

obj-$(CONFIG_MYKEY) += mykey.o

After making modules, observe the files in the drivers/char/mykey/ directory. There are many more files, including key.ko
Insert image description here

If you want to compile it into the kernel, select Y for the CONFIG_MYKEY option, and then execute make zImage. You will find that the size of the generated zImage image has increased, but key.ko will not be generated under drivers/char/mykey/, but the key will be generated. o

PS: obj-y (corresponding to make zImage)) refers to compiling the module into the kernel (zImage)

​obj -m (corresponding to make modules) refers to compiling modules to generate ko files


2. Place it outside the kernel code

If we hope that the module we write can be dynamically loaded into the kernel, even if we use the insmod and modprobe commands, we cannot put the module code directly into the kernel source tree.

Just create a new Makefile in the module directory you wrote. Since it is outside the kernel source code, we need to tell the make tool how to find the kernel source code file and the basic Makefile.

Why do we need to find the kernel source code and basic Makefile?

Answer: Because the driver code uses functions, header files, etc. provided by the kernel, compilation into the final ko file must also follow the compilation method of the kernel.

Makefile must contain the following instructions

make -C 内核所在路径 M=$PWD modules
    
obj-m += mykey.o

The general Makefile is written as follows:

KERN_DIR = /home/内核路径

all:
	make -C $(KERN_DIR) M=`pwd` modules

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean

obj-m += mykey.o

After executing make, generate the mykey.ko file and insert it into the kernel using the dynamic loading command.

insmod mykey.ko

Guess you like

Origin blog.csdn.net/HuangChen666/article/details/133576722