Linux Kernel Driver Program: 2_Pass_arguments_to_module
neelkanth_surekha#cat hello_world_module.c
/*************************************************************************
* How to execute insert this driver
* sudo insmod hello_world_module.ko value_test=14 name_test="Neel_driver" arr_value_test=100,102,104,106
*************************************************************************/
/************************************************************************
* Location of this Kernel module "hello_world_module"
* sys/module/hello_world_module/parameters/cb_value_test
************************************************************************/
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>
int value_test, arr_value_test[4];
char *name_test;
int cb_value_test = 0;
/**************************************************************
* This macro used to initialize the arguments.
* This macro is analogous to command line argument for
* main () function in user space. In the below, these variables
* may be initialized to any number, but the value can be changed
* dynamically while loading the kernel module as they are
* passed as arguments to kernel module while loading.
**************************************************************/
module_param(value_test, int, S_IRUSR|S_IWUSR); //integer value
module_param(name_test, charp, S_IRUSR|S_IWUSR); //String
module_param_array(arr_value_test, int, NULL, S_IRUSR|S_IWUSR); //Array of integers
/***********************************************************************************
* module_param_cb: This macro used to register the callback whenever the argument
* (parameter) got changed.
***********************************************************************************/
int notify_param(const char *val, const struct kernel_param *kp)
{
int res = param_set_int(val, kp); // Use helper for write variable
if(res==0) {
printk(KERN_INFO "Call back function called...\n");
printk(KERN_INFO "New value of cb_value_test = %d\n", cb_value_test);
return 0;
}
return -1;
}
const struct kernel_param_ops my_param_ops =
{
.set = ¬ify_param, // Use our setter ...
.get = ¶m_get_int, // .. and standard getter
};
module_param_cb(cb_value_test, &my_param_ops, &cb_value_test, S_IRUGO|S_IWUSR );
static int __init hello_world_init(void)
{
int i;
printk(KERN_INFO "Value_test = %d \n", value_test);
printk(KERN_INFO "cb_value_test = %d \n", cb_value_test);
printk(KERN_INFO "Name_test = %s \n", name_test);
for (i = 0; i < (sizeof arr_value_test / sizeof (int)); i++) {
printk(KERN_INFO "Arr_value[%d] = %d\n", i, arr_value_test[i]);
}
printk(KERN_INFO "Kernel Module Inserted Successfully...\n");
return 0;
}
void __exit hello_world_exit(void)
{
printk(KERN_INFO "Kernel Module Removed Successfully...\n");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Neelkanth Reddy <www.neelkanth.13@gmail.com>");
MODULE_DESCRIPTION("A simple hello world driver");
MODULE_VERSION("0:1.1");
neelkanth_surekha#cat Makefile
obj-m += hello_world_module.o
KDIR = /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
neelkanth_surekha#
neelkanth_surekha#cat Readme
*******************************************************************************
execute command
*******************************************************************************
neelkanth_surekha#sudo insmod hello_world_module.ko value_test=14 name_test="Neel_driver" arr_value_test=100,102,104,106
*******************************************************
dmesg output
*******************************************************
neelkanth_surekha#dmesg | tail -f
[51857.540408] Kernel Module Inserted Successfully...
[52158.948686] Kernel Module Removed Successfully...
[52162.692584] Value_test = 14
[52162.692587] cb_value_test = 0
[52162.692588] Name_test = Neel_driver
[52162.692590] Arr_value[0] = 100
[52162.692591] Arr_value[1] = 102
[52162.692593] Arr_value[2] = 104
[52162.692594] Arr_value[3] = 106
[52162.692595] Kernel Module Inserted Successfully...
******************************************************************************
update the file "cb_value_test " with new value :
******************************************************************************
neelkanth_surekha#cat /sys/module/hello_world_module/parameters/cb_value_test
0 -------> Initial Value
neelkanth_surekha#ls -lrt /sys/module/hello_world_module/parameters/cb_value_test
-rw-r--r-- 1 root root 4096 Jan 22 01:20 /sys/module/hello_world_module/parameters/cb_value_test
neelkanth_surekha#sudo chmod 777 /sys/module/hello_world_module/parameters/cb_value_test
[sudo] password for neelkanth_surekha:
neelkanth_surekha#echo 13 > /sys/module/hello_world_module/parameters/cb_value_test
*******************************************************
dmesg output after updating the file with new value
*******************************************************
neelkanth_surekha#dmesg | tail -f
[52162.692584] Value_test = 14
[52162.692587] cb_value_test = 0
[52162.692588] Name_test = Neel_driver
[52162.692590] Arr_value[0] = 100
[52162.692591] Arr_value[1] = 102
[52162.692593] Arr_value[2] = 104
[52162.692594] Arr_value[3] = 106
[52162.692595] Kernel Module Inserted Successfully...
[52366.983060] Call back function called...
[52366.983064] New value of cb_value_test = 13
x