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
Create a Makefile with the following content in the same folder
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:
Create a hello.c file with the following content in a new folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Post a Comment