Linux 把自己编写的内核驱动,或者IC厂家提供的内核驱动,添加到 menuconfig 可选项 【转】

参考链接:https://www.cnblogs.com/chenfulin5/p/6220753.html

linux kernel 的配置系统由以下三个部分组成。

  Makefile: 分布在Linux 内核源代码中,定义Linux kernel的编译规则。

  配置文件:(kconfig) 给用户提供配置选择的功能。

  配置工具:包括配置命令解析器和配置用户界面。这些配置工具使用的都是脚本语言,如Perl。

  最常使用的,我们一般使用make menuconfig 进行配置我们自己想要的。 这里面我们看到很多可以选择的选项,那么如果我们自己要增加自己的选项该怎么办呢。 网上有很多教程都是在drivers里面添加,我这里讲一种就是直接如果自己想建一个目录,然后添加里面的模块该怎么做。

1.首先在顶层目录建一个目录chentest,然后再建立个c 文件

mkdir chentest
vim chentest.c

2.编写c文件

chentest.c:
#include <linux/init.h>                                                         
#include <linux/module.h>                                                       
                                                                                                                                                            
int __init chen_init(void)                                                      
{                                                                               
    printk("start\n");                                                          
    return 0;                                                                   
}                                                                               
module_init(chen_init);                                                         
                                                                                
void __exit chen_exit(void)                                                     
{                                                                               
    printk("end\n");                                                            
}                                                                               
                                                                                
module_exit(chen_exit);                            

#MODULE_ALIAS ( 模块为人所知的另一个名字 )
# MODULE_DEVICE_TABLE ( 来告知用户空间, 模块支持那些设备 )
                                                                                
MODULE_AUTHOR("chenfl");    #声明谁编写了模块                                                    
MODULE_LICENSE("GPL");      #内核认识的特定许可有, “GPL”( 适用 GNU 通用公共许可的任何版本 ), “GPL v2”( 只适用 GPL 版本 2 )                                                       
MODULE_DESCRIPTION("This is test modules");  #模块功能声明                                   
MODULE_VERSION("V1.0");    #代码修订版本号

3.新建一个Makefile文件

vim Makefile

  Makefile文件添加内容如下:

//定义内核编译规则,配合Kconfig使用
obj-$(CONFIG_CHENTEST) += chen_test.o #这里声明了链接需要添加的驱动,链接到Konfig里的接口名为 CHENTEST
//如果你想直接在kernel里设置为Y,不配置config,可以把这个写为:obj-y  += chen_test.o

4.新建一个Kconfig

//Kconfig文件是配置文件,menuconfig根据该文件生成配置菜单
vim Kconfig

  Kconfig添加内容如下:

Kconfig:    
menu "chentest"   #菜单主入口,在menuconfig显示的名字

config CHEN_TEST  #子菜单入口1
    tristate "This is a test" #子入口显示的模块名
    default y     #默认值为y
    help
      Say Y here if you have any input device (mouse, keyboard, tablet,
      joystick, steering wheel ...) connected to your system and want
      it to be available to applications. This includes standard PS/2
      keyboard and mouse.

      Say N here if you have a headless (no monitor, no keyboard) system.

      More information is available: <file:Documentation/input/input.txt>

      If unsure, say Y.

      To compile this driver as a module, choose M here: the
      module will be called input.

  if y  #当menuconfig选Y的时候显示的内容

  config CONFIG_CHENTEST
      tristate "chentest" #显示的模块名
      help
        Say Y here if you have memoryless force-feedback input device
        such as Logitech WingMan Force 3D, ThrustMaster FireStorm Dual
        Power 2, or similar. You will also need to enable hardware-specific
        driver.

        If unsure, say N.

        To compile this driver as a module, choose M here: the
        module will be called ff-memless.
  endif

endmenu    

# menu---endmenu 之间的菜单入口,都会成为
"chentest" 的子菜单,并且继承其依赖关系

  到了这一步,准备工作差不多做好了,然后你的arm架构的话,需要在arm(64位的话是arm64)/arch/ 里的Kconfig文件添加下面内容·

#在原来sourch附近的地方添加
sourch  "chentest/Kconfig" #source条目用于读取另一个Kconfig文件(格式 “从Kernel源码继续下去直到你自己编写的Kconfig文件的路径/文件名”,例如:”drivers/firmware/Kconfig“)

5.最后回到kernel源码层,执行 make ARCH=arm(64位编译器就arm64) menuconfig 查看是否添加成功

 

注意:如果要添加一些厂家的驱动的话,也可以用这种方法直接添加,就只是把厂家提供的驱动当作自己编写的驱动即可

posted @ 2021-06-22 10:14  白菜没我白  阅读(210)  评论(0编辑  收藏  举报