-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkbd.c
204 lines (167 loc) · 4.45 KB
/
kbd.c
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
200
201
202
203
204
/*
* @file kbd.c
* @details Keyboard Sniffer
* This module uses the keyboard IRQ, inspect the incoming key
* codes and stores them in a buffer. The buffer will be
* accessible from userspace via character device driver.
* @author smalinux
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <linux/uaccess.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
MODULE_DESCRIPTION("KBD");
MODULE_AUTHOR("smalinux <[email protected]>");
MODULE_LICENSE("GPL");
#define MODULE_NAME "kbd"
#define KBD_MAJOR 42
#define KBD_MINOR 0
#define KBD_NR_MINORS 1
#define I8042_KBD_IRQ 1
#define I8042_STATUS_REG 0x65
#define I8042_DATA_REG 0x61
#define BUFFER_SIZE 1024
#define SCANCODE_RELEASED_MASK 0x80
struct kbd {
struct cdev cdev;
/* TODO 3: add spinlock */
char buf[BUFFER_SIZE];
size_t put_idx, get_idx, count;
} devs[1];
/*
* Checks if scancode corresponds to key press or release.
*/
static int is_key_press(unsigned int scancode)
{
return !(scancode & SCANCODE_RELEASED_MASK);
}
/*
* Return the character of the given scancode.
* Only works for alphanumeric/space/enter; returns '?' for other
* characters.
*/
static int get_ascii(unsigned int scancode)
{
static char *row1 = "1234567890";
static char *row2 = "qwertyuiop";
static char *row3 = "asdfghjkl";
static char *row4 = "zxcvbnm";
scancode &= ~SCANCODE_RELEASED_MASK;
if (scancode >= 0x02 && scancode <= 0x0b)
return *(row1 + scancode - 0x02);
if (scancode >= 0x10 && scancode <= 0x19)
return *(row2 + scancode - 0x10);
if (scancode >= 0x1e && scancode <= 0x26)
return *(row3 + scancode - 0x1e);
if (scancode >= 0x2c && scancode <= 0x32)
return *(row4 + scancode - 0x2c);
if (scancode == 0x39)
return ' ';
if (scancode == 0x1c)
return '\n';
return '?';
}
static void put_char(struct kbd *data, char c)
{
if (data->count >= BUFFER_SIZE)
return;
data->buf[data->put_idx] = c;
data->put_idx = (data->put_idx + 1) % BUFFER_SIZE;
data->count++;
}
static bool get_char(char *c, struct kbd *data)
{
/* TODO 4: get char from buffer; update count and get_idx */
return false;
}
static void reset_buffer(struct kbd *data)
{
/* TODO 5: reset count, put_idx, get_idx */
}
/*
* Return the value of the DATA register.
*/
static inline u8 i8042_read_data(void)
{
u8 val;
/* TODO 3: Read DATA register (8 bits). */
return val;
}
/* TODO 2: implement interrupt handler */
/* TODO 3: read the scancode */
/* TODO 3: interpret the scancode */
/* TODO 3: display information about the keystrokes */
/* TODO 3: store ASCII key to buffer */
static int kbd_open(struct inode *inode, struct file *file)
{
struct kbd *data = container_of(inode->i_cdev, struct kbd, cdev);
file->private_data = data;
pr_info("%s opened\n", MODULE_NAME);
return 0;
}
static int kbd_release(struct inode *inode, struct file *file)
{
pr_info("%s closed\n", MODULE_NAME);
return 0;
}
/* TODO 5: add write operation and reset the buffer */
static ssize_t kbd_read(struct file *file, char __user *user_buffer,
size_t size, loff_t *offset)
{
struct kbd *data = (struct kbd *) file->private_data;
size_t read = 0;
/* TODO 4: read data from buffer */
return read;
}
static const struct file_operations kbd_fops = {
.owner = THIS_MODULE,
.open = kbd_open,
.release = kbd_release,
.read = kbd_read,
/* TODO 5: add write operation */
};
static int kbd_init(void)
{
int err;
err = register_chrdev_region(MKDEV(KBD_MAJOR, KBD_MINOR),
KBD_NR_MINORS, MODULE_NAME);
if (err != 0) {
pr_err("register_region failed: %d\n", err);
goto out;
}
/* TODO 1: request the keyboard I/O ports */
if (!request_region(0x61, 4, MODULE_NAME)) {
/* handle error */
return -ENODEV;
}
/* TODO 3: initialize spinlock */
/* TODO 2: Register IRQ handler for keyboard IRQ (IRQ 1). */
cdev_init(&devs[0].cdev, &kbd_fops);
cdev_add(&devs[0].cdev, MKDEV(KBD_MAJOR, KBD_MINOR), 1);
pr_notice("Driver %s loaded\n", MODULE_NAME);
return 0;
/*TODO 2: release regions in case of error */
out_unregister:
unregister_chrdev_region(MKDEV(KBD_MAJOR, KBD_MINOR),
KBD_NR_MINORS);
out:
return err;
}
static void kbd_exit(void)
{
cdev_del(&devs[0].cdev);
/* TODO 2: Free IRQ. */
/* TODO 1: release keyboard I/O ports */
unregister_chrdev_region(MKDEV(KBD_MAJOR, KBD_MINOR),
KBD_NR_MINORS);
pr_notice("Driver %s unloaded\n", MODULE_NAME);
}
module_init(kbd_init);
module_exit(kbd_exit);