mirror of
https://github.com/Hizenberg469/Linux-kernel-driver-tutorial.git
synced 2026-04-20 00:42:25 +03:00
Hello World for linux Kernel Module
This commit is contained in:
21
01_hello/Makefile
Normal file
21
01_hello/Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
#This variable is used to specify all the object file
|
||||
#which will be loaded and compile as kernel module for
|
||||
#usage. And the compilation for obj file from .c file
|
||||
#is done automatically.
|
||||
|
||||
obj-m += hello.o
|
||||
|
||||
# M variable used here tells the "make" to look in
|
||||
# current path to build bootable kernel module from
|
||||
# .c files in specified current path.
|
||||
|
||||
# modules: It is the target that is used to build the kernel
|
||||
# modules.
|
||||
# clean: It is used to clean the build targets, compile and
|
||||
# generated files which would be created.
|
||||
all:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
58
01_hello/hello.c
Normal file
58
01_hello/hello.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/init.h>
|
||||
|
||||
|
||||
/*
|
||||
* insmod: It's the command to insert the
|
||||
* loadable kernel module to the
|
||||
* kernel.
|
||||
*
|
||||
* rmmod: It's the command to remove the
|
||||
* loadable kernel module from the
|
||||
* kernel.
|
||||
*/
|
||||
|
||||
/*
|
||||
* There are two imp. events for linux driver
|
||||
* The loading of driver on kernel and unloading
|
||||
* of driver from kernel.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Function to use when this is
|
||||
* module is loaded to the kernel.
|
||||
*/
|
||||
int
|
||||
my_init(void){
|
||||
|
||||
|
||||
/*
|
||||
* We don't use printf for linux driver
|
||||
* because printf writes to standard IO
|
||||
* and there is no standard IO for kernel
|
||||
* There is only kernel's log. Kernel's log
|
||||
* is buffer which stores whatever is written
|
||||
* onto that buffer. It can me printed using
|
||||
* command: dmesg
|
||||
*/
|
||||
|
||||
printk("hello - Hello, Kernel!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to execute when module
|
||||
* is unloaded from the kernel
|
||||
*/
|
||||
|
||||
void
|
||||
my_exit(void){
|
||||
|
||||
printk("hello - Goodbye, kernel!\n");
|
||||
}
|
||||
|
||||
module_init(my_init);
|
||||
module_exit(my_exit);
|
||||
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
Reference in New Issue
Block a user