forked from amitsagargowda/LinuxKernelDevelopment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback1.c
More file actions
90 lines (71 loc) · 3.05 KB
/
callback1.c
File metadata and controls
90 lines (71 loc) · 3.05 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h> /* Needed to use module_param */
int intVal = 0; /* Integer param */
char *charPVal = "HELLO"; /* Char Pointer */
int intArray[4] = {-1, -2} ; /* Integer array passing */
int intValChgNotify = 9; /* To show how we can get notification */
int count_of_array_elements ;
int i = 0;
MODULE_AUTHOR("Abha Patidar");
MODULE_DESCRIPTION("Parameter Passing");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.1.1");
MODULE_PARM_DESC(intVal, "Integer Variable");
MODULE_PARM_DESC(charPVal, "Character Pointer Variable");
MODULE_PARM_DESC(intArray, "Integer Array Variable");
MODULE_PARM_DESC(intValChgNotify, "Integer Variable to see call back getter/setter functions");
int setNewValue (const char *newValue, const struct kernel_param *kp)
{
int res = param_set_int(newValue, kp);
if (res == 0){
printk(KERN_INFO "Setter method called \n");
printk(KERN_INFO "New value is = %d \n", intValChgNotify);
return 0;
}
return -1;
}
int getCurrentValue (char *valueBuffer, const struct kernel_param *kp)
{
int res;
printk(KERN_INFO "Getter method called \n");
res = param_get_int(valueBuffer, kp);
if (res >= 0){
printk(KERN_INFO "Getter method called \n");
printk(KERN_INFO "Current value is = %d \n", intValChgNotify);
return res;
}
printk(KERN_INFO "Error getting value \n" );
return -1;
}
static struct kernel_param_ops kParamOps =
{
.set = setNewValue , // Use our setter. This is called when value is set from outside
.get = getCurrentValue ,
//.get = param_get_int, // getCurrentValue , // This is standard getter = param_get_int
};
module_param(intVal, int , S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
module_param(charPVal, charp , S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
module_param_array(intArray, int, &count_of_array_elements, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
module_param_cb(intValChgNotify, &kParamOps, &intValChgNotify, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
static int __init startUpModule(void)
{
printk(KERN_INFO "== Trying to insert module into kernel memory ... \n");
printk(KERN_INFO "Value of Integer intVal is %d \n", intVal);
printk(KERN_INFO "Value of Character Pointer charPval is %s \n", charPVal);
printk(KERN_INFO "Value of Integer to be notified param is %d \n", intValChgNotify);
for (i = 0 ; i < sizeof(intArray)/sizeof(int) ; i++)
{
printk(KERN_INFO "Value of element number %d = %d \n", i, intArray[i]);
}
printk(KERN_INFO "Number of array elements got %d \n", count_of_array_elements);
printk(KERN_INFO "== Module inserted into kernel memory\n");
return 0;
}
static void __exit exitModule(void)
{
printk(KERN_INFO "Kernel Module removed successfully\n");
}
module_init(startUpModule);
module_exit(exitModule);