mirror of
https://github.com/Hizenberg469/Driver-tutorial.git
synced 2026-04-20 00:42:25 +03:00
Character driver finished
This commit is contained in:
10
LKM/Makefile
Normal file
10
LKM/Makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
obj-m += hello.o
|
||||
obj-m += hello_func.o
|
||||
obj-m += depmod.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
|
||||
|
||||
23
LKM/depmod.c
Normal file
23
LKM/depmod.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <linux/module.h> // included for all kernel module
|
||||
#include <linux/kernel.h> // included for KERN_INFO
|
||||
#include <linux/init.h> // included for __init and __exit macros
|
||||
|
||||
|
||||
extern void func(void);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Junet Hossain");
|
||||
MODULE_DESCRIPTION("A Simple Hello World module");
|
||||
|
||||
static int __init depmod_init(void){
|
||||
printk(KERN_INFO "In depmod_init calling exported symbol func\n");
|
||||
func();
|
||||
return 0; // Non-zero return means that the module couldn't be loaded.
|
||||
}
|
||||
|
||||
static void __exit depmod_cleanup(void){
|
||||
printk(KERN_INFO "Cleaning up dep module.\n");
|
||||
}
|
||||
|
||||
module_init(depmod_init);
|
||||
module_exit(depmod_cleanup);
|
||||
43
LKM/hello.c
Normal file
43
LKM/hello.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <linux/module.h> // included for all kernel module
|
||||
#include <linux/kernel.h> // included for KERN_INFO
|
||||
#include <linux/init.h> // included for __init and __exit macros
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Junet Hossain");
|
||||
MODULE_DESCRIPTION("A Simple Hello World module");
|
||||
|
||||
static int __init hello_init(void){
|
||||
printk(KERN_INFO "Hello world!\n");
|
||||
return 0; // Non-zero return means that the module couldn't be loaded.
|
||||
}
|
||||
|
||||
static void __exit hello_cleanup(void){
|
||||
printk(KERN_INFO "Cleaning up hello module.\n");
|
||||
}
|
||||
|
||||
module_init(hello_init);
|
||||
module_exit(hello_cleanup);
|
||||
|
||||
/**
|
||||
* Use these macros for efficient and optimized kernel modules.
|
||||
*
|
||||
* __init: It's a macro which is used for marking
|
||||
* the function which is executed when the
|
||||
* kernel is loaded. After execution, its memory
|
||||
* is freed (for built-in modules). For module
|
||||
* compiled as a loadable kernel module (LKM),
|
||||
* __init has no effect (because the memory
|
||||
* is allocated dynamically). If not used for built
|
||||
* -in kernel, the memory for the init function
|
||||
* remains allocated even after module initializaiton
|
||||
* (wasted memory)
|
||||
*
|
||||
* __exit: It's a macro which is used to mark the function
|
||||
* which is executed when the module is unloaded
|
||||
* from the kernel. It is ignored for built-in kernel modules
|
||||
* (since they can't be removed at runtime). For
|
||||
* dynamically loaded modules, it ensures proper
|
||||
* cleanup. If __exit is not used, the function will
|
||||
* still run when removing the module, but marking
|
||||
* it properly makes the code cleaner and optimized.
|
||||
*/
|
||||
41
LKM/hello_func.c
Normal file
41
LKM/hello_func.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <linux/module.h> // included for all kernel module
|
||||
#include <linux/kernel.h> // included for KERN_INFO
|
||||
#include <linux/init.h> // included for __init and __exit macros
|
||||
|
||||
|
||||
void func(void);
|
||||
EXPORT_SYMBOL_GPL(func);
|
||||
|
||||
/**
|
||||
* EXPORT_SYMBOL_GPL:
|
||||
* Since, in user space to use variable/function of one
|
||||
* file to another file we use extern or header file. But
|
||||
* for kernel space such thing is not allowed. But to use
|
||||
* variable or function in another module/file we use
|
||||
* EXPORT_SYMBOL_GPL(<fun or var name>) to export the
|
||||
* function or variable to global scope for all the modules.
|
||||
*/
|
||||
|
||||
int val = 300;
|
||||
|
||||
void func(){
|
||||
|
||||
printk(KERN_INFO"func invoked\n ");
|
||||
printk(KERN_INFO" val = %d \n", val);
|
||||
}
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Junet Hossain");
|
||||
MODULE_DESCRIPTION("A Simple Hello World module");
|
||||
|
||||
static int __init hello_init(void){
|
||||
printk(KERN_INFO "Init Hello func module\n");
|
||||
return 0; // Non-zero return means that the module couldn't be loaded.
|
||||
}
|
||||
|
||||
static void __exit hello_cleanup(void){
|
||||
printk(KERN_INFO "Cleaning up hello func module.\n");
|
||||
}
|
||||
|
||||
module_init(hello_init);
|
||||
module_exit(hello_cleanup);
|
||||
Reference in New Issue
Block a user