Hello World Linux Device Driver Program

Let's in this post write a hello world Linux device driver

Create a hello.c file with the following content in a new folder


#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int test_hello_init(void)
{
printk(KERN_INFO"%s: In init\n", __func__);
return 0;
}
static void test_hello_exit(void)
{
printk(KERN_INFO"%s: In exit\n", __func__);
}
module_init(test_hello_init);
module_exit(test_hello_exit);
view raw hello.c hosted with ❤ by GitHub
Create a Makefile with the following content in the same folder

obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
view raw Makefile hosted with ❤ by GitHub
Command to create a module: make


Command to load module: insmod hello.ko

When you run this command, function specified in module_init will be executed, to check run "dmesg"

Command to remove module: rmmod hello

When you run this command, function specified in module_exit will be executed, verify it by running "dmesg"


Output:



Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto