Module Parameters Macros
- module_param()// module_param(name, type, perm);
This macro used to initialize the arguments.module_param takes three parameters: the name of the variable, its type, and a permissions mask to be used for an accompanying sysfs entry.
- module_param_array()//
module_param_array(name,type,num,perm);
Where,name
is the name of your array (and of the parameter),type
is the type of the array elements,num
is an integer variable (optional) otherwise NULL, andperm
is the usual permissions value.
This macro is used to send the array as a argument. Array parameters, where the values are supplied as a comma-separated list.
- module_param_cb()
This macro used to register the callback whenever the argument (parameter) got changed.
There are several types of permissions:
- S_IWUSR
- S_IRUSR
- S_IXUSR
- S_IRGRP
- S_IWGRP
- S_IXGRP
In this S_I is common header.
R = read ,W =write ,X= Execute.
USR =user ,GRP =Group
Using OR ‘|’ (or operation) we can set multiple permissions at a time.
-------------------R = read ,W =write ,X= Execute.
USR =user ,GRP =Group
Using OR ‘|’ (or operation) we can set multiple permissions at a time.
For Example,
I have created one parameter by using
module_param()
.
1
|
module_param(valueETX, int, S_IWUSR|S_IRUSR);
|
This will create the sysfs entry. (
/sys/module/hello_world_module/parameters/valueETX
)
You can change the value of
valueETX
from command line byecho 1 > /sys/module/hello_world_module/parameters/valueETX
This will update the
valueETX
variable. But there is no way to notify your module that “valueETX
” has changed.
By using this
module_param_cb()
macro, we can get notification.
If you want to get notification whenever value got change. we need to register our handler function to its file operation structure.
1
2
3
4
5
|
struct kernel_param_ops {
int (*set)(const char *val, const struct kernel_param *kp);
int (*get)(char *buffer, const struct kernel_param *kp);
void (*free)(void *arg);
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include
#include
#include
#include
int valueETX, arr_valueETX[4];
char *nameETX;
int cb_valueETX = 0;
module_param(valueETX, int, S_IRUSR|S_IWUSR); //integer value
module_param(nameETX, charp, S_IRUSR|S_IWUSR); //String
module_param_array(arr_valueETX, int, NULL, S_IRUSR|S_IWUSR); //Array of integers
/*----------------------Module_param_cb()--------------------------------*/
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_valueETX = %d\n", cb_valueETX);
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_valueETX, &my_param_ops, &cb_valueETX, S_IRUGO|S_IWUSR );
/*-------------------------------------------------------------------------*/
static int __init hello_world_init(void)
{
int i;
printk(KERN_INFO "ValueETX = %d \n", valueETX);
printk(KERN_INFO "cb_valueETX = %d \n", cb_valueETX);
printk(KERN_INFO "NameETX = %s \n", nameETX);
for (i = 0; i < (sizeof arr_valueETX / sizeof (int)); i++) {
printk(KERN_INFO "Arr_value[%d] = %d\n", i, arr_valueETX[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("EmbeTronicX
MODULE_DESCRIPTION("A simple hello world driver");
MODULE_VERSION("1.0");
|
Compiling
This is the code of
Makefile
.
1
2
3
4
5
6
7
8
9
|
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
|
In terminal enter
sudo make
Loading the Driver
sudo insmod hello_world_module.ko valueETX=14 nameETX="EmbeTronicX" arr_valueETX=100,102,104,106
Verify the parameters by using dmesg
Now our module got loaded. now check
dmesg
. In below picture, every value got passed to our device driver.module_param_cb()
is weather calling that handler function or not. For that i need to change the variable in sysfs. Now i’m going to checkecho 13 > /sys/module/hello_world_module/parameters/cb_valueETX
dmesg
and check. Now do
Se
No comments:
Post a Comment