The kernel has many abstraction layers and levels of indirection and because its build system allows for creating highly customized kernel binary images.
Please read the "Kbuild: the Linux Kernel Build System" carefully, you will understand how this system works Kbuild Linux Kernel Build System
Make a "helloworld" directory under drivers
user@workstation:~/linux$ mkdir drivers/helloworldCreate helloworld.c file under our helloworld directory and add the C code below, this is a simple Hello World Kernel Module
user@workstation:~/linux$ nano drivers/helloworld/helloworld.c#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int module_init_function(void)
{
printk(KERN_INFO "Module? Hello!\n");
return 0;
}
static void module_exit_function(void)
{
printk(KERN_INFO "Module? Bye!\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xe1gyq");
MODULE_DESCRIPTION("My First Linux Kernel Module");
module_init(module_init_function);
module_exit(module_exit_function);Create the Kconfig file under helloworld directory and add the code below, make sure indentation is correct
user@workstation:~/linux$ nano drivers/helloworld/Kconfig
menu "Hello Module Kernel Support"
config HELLO_WORLD
tristate "Hello Module Driver"
depends on X86
help
Select this option to run a Hello World Module!
endmenuCreate the Makefile under helloworld directory and add the code below
user@workstation:~/linux$ nano drivers/helloworld/Makefile
obj-$(CONFIG_HELLO_WORLD) += helloworld.oSeeing Hello World Directory
Modify Kconfig under drivers directory and add the line with helloworld
user@workstation:~/linux$ nano drivers/Kconfig
menu "Device Drivers"
source "drivers/helloworld/Kconfig"
source "drivers/amba/Kconfig"Compiling Hello World Directory
Modify Makefile under drivers directory and add the line with CONFIG_HELLO_WORLD
user@workstation:~/linux$ nano drivers/Makefile
#
# Makefile for the Linux kernel device drivers.
#
# 15 Sep 2000, Christoph Hellwig <hch@infradead.org>
# Rewritten to use lists instead of if-statements.
#
obj-$(CONFIG_HELLO_WORLD) += helloworld/
obj-y += irqchip/
obj-y += bus/
...user@workstation:~/linux$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: drivers/Kconfig
modified: drivers/Makefile
Untracked files:
(use "git add <file>..." to include in what will be committed)
drivers/helloworld/
no changes added to commit (use "git add" and/or "git commit -a")
user@workstation:~/linux$ We are ready to view our Hello World Module under menuconfig
user@workstation:~/linux$ make menuconfigGo to its location under
-> Device Drivers
-> Hello Module Kernel Support
Understand the menu options seen below including their fast paths (one letter invocation)
<Select> < Exit > < Help > < Save > < Load >Get help for the Hello Module Kernel Support using Help function, you should see this
CONFIG_HELLO_WORLD:
Select this option to run a Hello World Module!
Symbol: HELLO_WORLD [=n]
Type : tristate
Prompt: Hello Module Driver
Location:
-> Device Drivers
-> Hello Module Kernel Support
Defined at drivers/helloworld/Kconfig:3
Depends on: X86 [=y]Understand about the following options from Kconfig by googling or looking
at other Kconfigs
- default
- tristate
- Depends on
Take a look at the default building state for our Hello World Module and
modify Kconfig so you can have it built as default
Symbol: HELLO_WORLD [=n]