Your partner for your innovative projects ...                                            

Actualités OSELIS

 

contact

 

French (Fr)English (United Kingdom)

Adding a module to the kernel

E-mail Print PDF

The aim of this paper is to show how to add very easily a module to the kernel source, so if needed we can add our own modules in the future.

A module can be loaded and unloaded on demand, the benefit of a module is that it does'nt reside into the monolithic kernel image.

Every driver from the kernel ca be compiled in this way, or directly beeing part of the kernel image. We will add a very simple driver to the kernel.
Let's add the drivers/misc folder to our kernel source. Then the following file:

drivers/misc/hello_world.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <linux/kernel.h>
#include <linux/module.h>

static int __init hello_world_module_init(void)
{
printk("Hello World, sample module is installed!\n");
return 0;
}

static void __exit hello_world_module_cleanup(void)
{
printk("Good-bye, sample module was removed!\n");
}

module_init(hello_world_module_init);
module_exit(hello_world_module_cleanup);
MODULE_LICENSE("GPL");

The following step is to add the module configuration enabling to activate or deactivate it. Let's add the file drivers/misc/Kconfig with the following content :

1
2
3
4
5
6
7
config HELLO_WORLD_MODULE
tristate "hello world module"
depends on ARM
default m if ARM
help
hello world module.

The 3rd line shows that our driver isn't available when the ARM architecture is active (CONFIG_ARM), and the 4th line shows that when the driver is available, it is by default selected as a module. The next step will tell the kernel to compile the file hello_world.c when HELLO_WORLD_MODULE is activated and configured. Let's add the file drivers/misc/Makefile with the following content:

1
obj-$(CONFIG_HELLO_WORLD_MODULE)+= hello_world.o

Our driver is now added. Let's compile it now. for our example, we start with a pre-configured file (omap3_evm_defconfig).

1
2
3
4
5
6
export CROSS_COMPILE=arm-none-linux-gnueabi-
export ARCH=arm
make clean
make omap3_evm_defconfig
make menuconfig

Activate your module from the interface in the menu Device Drivers —> Misc devices —> hello world module. Compilation launch :

1
2
3
make modules
make modules_install INSTALL_MOD_PATH=$ROOTFS

We see $ROOTFS as the folder including the target file system. To test our module:

1
2
3
4
$ modprobe hello_world
Hello World, sample module is installed !
$ rmmod hello_world
Good-bye, sample module was removed!

© Copyright 2011, Oselis.

White papers

 Adding a module to the kernel : A module can be loaded and unloaded on demand, the benefit of a module is that it does'nt reside into the monolithic kernel image. Read more...


 ARM simulation with qemu and NFS : First step, the development PC station configuration. This phase consists to add and configure a "bridge". Read more...


 Getting started with OMAP3530 : Basic environment setup of Tsunami board, TechNexion PSP installation Read more...


Buildroot on OMAP3530 : In this toturial, the 2011.02 version of builroot was used. Read more...


Open embedded on OMAP3530 : Configuration of Tsunami board. The OE reference tutorial is under... Read more...


DVSDK on OMAP3530 : DVSDK is a development environment done by TI for its platforms, enabling an... Read more...