Home | Projects | Notes > Linux Device Drivers > Linux Kernel Module Build - In-Tree Module
Add the LKM inside the Linux kernel source tree and let the Linux build system build it.
To list your kernel module selection in kernel menuconfig, create and use a Kconfig
file.
Create a directory in linux_bbb_4.14/drivers/char/my_c_dev/
Copy main.c
Create Kconfig
file and add the following entries:
xxxxxxxxxx
51menu "Kyungjae's custom modules"
2 config CUSTOM_HELLOWORLD
3 tristate "helloworld module support"
4 default n
5endmenu
CUSTOM_HELLOWORLD
is an identifier by which the kernel identifies your custom module.User custom module can be selected or unselected. If selected, it can be either static module or dynamic module.
Each kernel module has 3 states;
y
,m
,n
. You can specify it by using thetristate
anddefault
keyword.
n
- Unselected by default
y
- Select by default
Add the local Kconfig
entry to upper-level Kconfig
:
Go one level up (i.e., linux_bbb_4.14/drivers/char/
), open Kconfig
and add the following line at the end:
xxxxxxxxxx
11source "drivers/char/my_c_dev/Kconfig"
Create a local Makefile in linux_bbb_4.14/drivers/char/my_c_dev/
.
Add obj-<config_item> += <module>.o
to the local Makefile
config_item
- The custom module identifier (e.g.,CUSTOM_HELLOWORLD
)
xxxxxxxxxx
11obj-$(CONFIG_CUSTOM_HELLOWORLD) += main.o
Since the state of this module will be selected via menu, we cannot specify it at the time of writing the Makefile.
$(CONFIG_CUSTOM_HELLOWORLD)
will be replaced by the selected state.
Add the local level Makefile to higher level Makefile:
Go one level up (i.e., linux_bbb_4.14/drivers/char/
), open the Makefile and add the following line at the end:
xxxxxxxxxx
11obj-y += my_c_dev/
This is how you direct the higher-level Makefile to run another Makefile.
-y
since you wantmy_c_dev
directory always selected.
Run the Kernel Configuration:
xxxxxxxxxx
11make ARCH=arm menuconfig
Select M
for helloworld module support
and exit saving your new configuration!
Open the updated linux_bbb_4.14/.config
file and search for CONFIG_CUSTOM_HELLOWORLD
.
If it is there, it means that your custom module is now part of the kernel source tree.
Build the kernel modules!
In the Linux kernel source directory linux_bbb_4.14/
run:
xxxxxxxxxx
11make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules -j4
You'll be able to see
main.ko
generated.
Check the module info and see if the built module is marked as "intree":
Go to linux_bbb_4.14/drivers/char/my_c_dev
and run:
xxxxxxxxxx
11modinfo main.ko
Then, you'll see the intree:
field is marked Y
.
[!] Reference: https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt