Yuhang Zheng

第三节、把驱动编译进内核

N 人看过

本节用于介绍如何把驱动编译进内核

一个常见的Kconfig,位置为drivers/leds/Kconfig

1
2
3
4
5
6
7
source "drivers/leds/trigger/Kconfig"
config LEDS_COBALT_RAQ
bool "LED Support for the Cobalt Raq series"
depends on LEDS_CLASS=y && MIPS_COBALT
select LEDS_TRIGGERS
help
This option enables support for the Cobalt Raq series LEDs.

source “drivers/leds/trigger/Kconfig”

他会包含drivers/leds/trigger/Kconfig这个路径下的驱动文件,方便我们对菜单进行管理

config LEDS_COBALT_RAQ

配置选项的名字,也就是在config文件里面的名字,CONFIG_LEDS_COBALT_RAQ

驱动可选的编译状态

  • bool 编译进内核,不编译
  • tristate 编译成模块,编译进内核,不编译

依赖关系

  • depends on A depends on B,表示只有在选择B的时候才可以选择A
  • select A select B,反向依赖,A选项被选中时,B选项会自动被选中

帮助信息

  • help

假设我们创建一个字符设备驱动,新建目录 drivers/char/hello

然后在驱动目录添加文件

1
2
zyh@1ffe9f7b637e:~/OK10xx-linux-kernel/drivers/char/hello$ ls
helloworld.c Kconfig Makefile

Kconfig:

1
2
3
4
config HELLO
tristate "hello world"
help
hello hello

Makefile:

1
obj-y += helloworld.o

修改上一级目录,也就是drivers/char目录下的文件

Kconfig内添加:

1
source "drivers/char/hello/Kconfig"

Makefile内添加:

1
obj-$(CONFIG_HELLO)             += hello/