Showing posts with label Building and Running modules. Show all posts
Showing posts with label Building and Running modules. Show all posts

Tuesday, June 10, 2014

How to add your linux driver module in a kernel

Use below steps to add your driver module in linux kernel. by adding your driver as a module you can load/unload it at your convenience and will not be a part of your kernel image. I have used hello driver to explain it.
Steps to follow
===========
1). Create your module directory in /kernel/drivers
Eg. mkdir hellodriver
2).  Create your file inside /kernel/drivers/hellodriver/  and add below functions and save it.
Write your driver init and exit function in hello.c file.
————————————————–
#include
#include
static int __init hello_module_init(void)
{
printk (“hello test app module init”);
return 0;
}
static int __exit hello_module_cleanup(void)
{
printk(“hello test app module cleanup “);
return 0;
}
module_init(hello_module_init);
module_exit(hello_module_cleanup);
MODULE_LICENSE(“GPL”);
—————————————————————————————-
3).  Create empty Kconfig file and Makefile in /kernel/drivers/hellodriver/
4). Add below entries in Kconfig
config HELLOTEST_APP
tristate “HelloTest App”
depends on ARM
default m
help
hellotest app
5). Add below entries in Makefile
obj-$(CONFIG_HELLOTEST_APP) +=hello.o
6). Modify the /kernel/drivers Kconfig and Makefile to support your module
7). Add below entry in /kernel/drivers/Kconfig file
source “drivers/hellodriver/Kconfig”
8). Add below entry in /kernel/drivers/Makefile file
obj-$(CONFIG_HELLOTEST_APP) +=hellodriver/
9).  Now go to kernel directory and give
make menuconfig ARCH=arm
Verify that your driver module entry is visible under Device Drivers  —>
For module entry it shows HelloTest App
Now recompile the kernel with your requirement  and give
sudo make ARCH=arm CROSS_COMPILE=your toolchain path-
10). Check the hello.o and hello.ko files are generated at /kernel/drivers/hellodriver/
If you want to make your module as a part of kernel image then you only need to change <*> HelloTest App the option in menuconfig and  recompile the kernel.
If you don’t want to make your module as a part of kernel image then you only need to change <> HelloTest App the option in menuconfig and  recompile the kernel.
This is a very simple example of adding a module in a kernel.
How to Load/Unload  module/s from the user space:
In user space, you can load the module as root by typing the following into the command line. insmod load the module into kernel space.
# insmod hello.ko

To see the module loaded you can do following:

# lsmod

To remove your module from the kernel space you can do followig:

#rmmod hello.ko