-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
104 lines (79 loc) · 2.46 KB
/
test.c
File metadata and controls
104 lines (79 loc) · 2.46 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include "test.h"
unsigned long (*func)(unsigned long) = NULL;
unsigned long arg = 0;
unsigned long res = 0;
static long test_ioctl(struct file* file, unsigned int cmd, unsigned long argp) {
printk(KERN_INFO "test.c: test_ioctl(file: %lx, cmd: %x, argp: %lx)\n",
file, cmd, argp);
switch (cmd)
{
case IOCTL_DO:
printk(KERN_INFO "test.c: function: %lx, arg: %lx\n",
func, arg);
res = func(arg);
break;
case IOCTL_SET_FUNCTION:
printk(KERN_INFO "test.c: function = %lx\n",
argp);
func = argp;
break;
case IOCTL_SET_ARGUMENT:
printk(KERN_INFO "test.c: arg = %lx\n",
argp);
arg = argp;
break;
case IOCTL_GET_RESPONSE:
printk(KERN_INFO "test.c: res: %lx",
res);
copy_to_user(__user (unsigned long*)argp, &res, sizeof(unsigned long));
break;
default:
break;
}
return 0;
}
static int test_open(struct inode* inode, struct file* file) {
printk(KERN_INFO "test.c: test_open(inode: %lx, file: %lx)\n",
inode, file);
return 0;
}
static int test_close(struct inode* inode, struct file* file) {
printk(KERN_INFO "test.c: test_close(inode: %lx, file: %lx)\n",
inode, file);
return 0;
}
static int test_uevent(struct device *dev, struct kobj_uevent_env *env)
{
add_uevent_var(env, "DEVMODE=%#o", 0666);
return 0;
}
static struct file_operations fops = {
.open = test_open,
.release = test_close,
.unlocked_ioctl = test_ioctl
};
static dev_t dev_id;
static struct class* class;
static int __init module_initialize(void) {
printk(KERN_INFO "test.c: module_initialize(void)\n");
dev_id = register_chrdev(0, DEVICE_NAME, &fops);
class = class_create(THIS_MODULE, DEVICE_NAME);
class->dev_uevent = test_uevent;
device_create(class, NULL, MKDEV(dev_id, 0), NULL, DEVICE_NAME);
return 0;
}
static void __exit module_cleanup(void) {
printk(KERN_INFO "test.c: module_cleanup(void)\n");
device_destroy(class, MKDEV(dev_id, 0));
class_destroy(class);
unregister_chrdev(dev_id, DEVICE_NAME);
return;
}
module_init(module_initialize);
module_exit(module_cleanup);
MODULE_LICENSE("GPL");