Subversion Repositories HelenOS

Rev

Rev 1963 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1963 Rev 1968
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <genarch/i8042/i8042.h>
29
#include <genarch/i8042/i8042.h>
30
#include <arch/drivers/i8042.h>
30
#include <arch/drivers/i8042.h>
31
#include <arch/interrupt.h>
31
#include <arch/interrupt.h>
32
#include <cpu.h>
32
#include <cpu.h>
33
#include <arch/asm.h>
33
#include <arch/asm.h>
34
#include <arch.h>
34
#include <arch.h>
35
#include <synch/spinlock.h>
35
#include <synch/spinlock.h>
36
#include <typedefs.h>
36
#include <typedefs.h>
37
#include <console/chardev.h>
37
#include <console/chardev.h>
38
#include <console/console.h>
38
#include <console/console.h>
39
#include <macros.h>
39
#include <macros.h>
40
#include <interrupt.h>
40
#include <interrupt.h>
41
 
41
 
42
/**
42
/**
43
 * i8042 processor driver.
43
 * i8042 processor driver.
44
 * It takes care of low-level keyboard functions.
44
 * It takes care of low-level keyboard functions.
45
 */
45
 */
46
 
46
 
47
/** Keyboard commands. */
47
/** Keyboard commands. */
48
#define KBD_ENABLE  0xf4
48
#define KBD_ENABLE  0xf4
49
#define KBD_DISABLE 0xf5
49
#define KBD_DISABLE 0xf5
50
#define KBD_ACK     0xfa
50
#define KBD_ACK     0xfa
51
 
51
 
52
/*
52
/*
53
 * 60  Write 8042 Command Byte: next data byte written to port 60h is
53
 * 60  Write 8042 Command Byte: next data byte written to port 60h is
54
 *     placed in 8042 command register. Format:
54
 *     placed in 8042 command register. Format:
55
 *
55
 *
56
 *    |7|6|5|4|3|2|1|0|8042 Command Byte
56
 *    |7|6|5|4|3|2|1|0|8042 Command Byte
57
 *     | | | | | | | `---- 1=enable output register full interrupt
57
 *     | | | | | | | `---- 1=enable output register full interrupt
58
 *     | | | | | | `----- should be 0
58
 *     | | | | | | `----- should be 0
59
 *     | | | | | `------ 1=set status register system, 0=clear
59
 *     | | | | | `------ 1=set status register system, 0=clear
60
 *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
60
 *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
61
 *     | | | `-------- disable keyboard I/O by driving clock line low
61
 *     | | | `-------- disable keyboard I/O by driving clock line low
62
 *     | | `--------- disable auxiliary device, drives clock line low
62
 *     | | `--------- disable auxiliary device, drives clock line low
63
 *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
63
 *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
64
 *     `----------- reserved, should be 0
64
 *     `----------- reserved, should be 0
65
 */
65
 */
66
 
66
 
67
#define i8042_SET_COMMAND   0x60
67
#define i8042_SET_COMMAND   0x60
68
#define i8042_COMMAND       0x49
68
#define i8042_COMMAND       0x49
69
 
69
 
70
#define i8042_BUFFER_FULL_MASK  0x01
70
#define i8042_BUFFER_FULL_MASK  0x01
71
#define i8042_WAIT_MASK     0x02
71
#define i8042_WAIT_MASK     0x02
72
 
72
 
73
#define SPECIAL     '?'
73
#define SPECIAL     '?'
74
#define KEY_RELEASE 0x80
74
#define KEY_RELEASE 0x80
75
 
75
 
76
/**
76
/**
77
 * These codes read from i8042 data register are silently ignored.
77
 * These codes read from i8042 data register are silently ignored.
78
 */
78
 */
79
#define IGNORE_CODE 0x7f
79
#define IGNORE_CODE 0x7f
80
 
80
 
81
static void key_released(__u8 sc);
81
static void key_released(__u8 sc);
82
static void key_pressed(__u8 sc);
82
static void key_pressed(__u8 sc);
83
static char key_read(chardev_t *d);
83
static char key_read(chardev_t *d);
84
 
84
 
85
#define PRESSED_SHIFT       (1<<0)
85
#define PRESSED_SHIFT       (1<<0)
86
#define PRESSED_CAPSLOCK    (1<<1)
86
#define PRESSED_CAPSLOCK    (1<<1)
87
#define LOCKED_CAPSLOCK     (1<<0)
87
#define LOCKED_CAPSLOCK     (1<<0)
88
 
88
 
89
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
89
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
90
 
90
 
91
static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE];
91
static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE];
92
 
92
 
93
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
93
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
94
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
94
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
95
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
95
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
96
 
96
 
97
static void i8042_suspend(chardev_t *);
97
static void i8042_suspend(chardev_t *);
98
static void i8042_resume(chardev_t *);
98
static void i8042_resume(chardev_t *);
99
 
99
 
100
static chardev_t kbrd;
100
static chardev_t kbrd;
101
static chardev_operations_t ops = {
101
static chardev_operations_t ops = {
102
    .suspend = i8042_suspend,
102
    .suspend = i8042_suspend,
103
    .resume = i8042_resume,
103
    .resume = i8042_resume,
104
    .read = key_read
104
    .read = key_read
105
};
105
};
106
 
106
 
107
/** Primary meaning of scancodes. */
107
/** Primary meaning of scancodes. */
108
static char sc_primary_map[] = {
108
static char sc_primary_map[] = {
109
    SPECIAL, /* 0x00 */
109
    SPECIAL, /* 0x00 */
110
    SPECIAL, /* 0x01 - Esc */
110
    SPECIAL, /* 0x01 - Esc */
111
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
111
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
112
    '\b', /* 0x0e - Backspace */
112
    '\b', /* 0x0e - Backspace */
113
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
113
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
114
    SPECIAL, /* 0x1d - LCtrl */
114
    SPECIAL, /* 0x1d - LCtrl */
115
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
115
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
116
    '`',
116
    '`',
117
    SPECIAL, /* 0x2a - LShift */
117
    SPECIAL, /* 0x2a - LShift */
118
    '\\',
118
    '\\',
119
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
119
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
120
    SPECIAL, /* 0x36 - RShift */
120
    SPECIAL, /* 0x36 - RShift */
121
    '*',
121
    '*',
122
    SPECIAL, /* 0x38 - LAlt */
122
    SPECIAL, /* 0x38 - LAlt */
123
    ' ',
123
    ' ',
124
    SPECIAL, /* 0x3a - CapsLock */
124
    SPECIAL, /* 0x3a - CapsLock */
125
    SPECIAL, /* 0x3b - F1 */
125
    SPECIAL, /* 0x3b - F1 */
126
    SPECIAL, /* 0x3c - F2 */
126
    SPECIAL, /* 0x3c - F2 */
127
    SPECIAL, /* 0x3d - F3 */
127
    SPECIAL, /* 0x3d - F3 */
128
    SPECIAL, /* 0x3e - F4 */
128
    SPECIAL, /* 0x3e - F4 */
129
    SPECIAL, /* 0x3f - F5 */
129
    SPECIAL, /* 0x3f - F5 */
130
    SPECIAL, /* 0x40 - F6 */
130
    SPECIAL, /* 0x40 - F6 */
131
    SPECIAL, /* 0x41 - F7 */
131
    SPECIAL, /* 0x41 - F7 */
132
    SPECIAL, /* 0x42 - F8 */
132
    SPECIAL, /* 0x42 - F8 */
133
    SPECIAL, /* 0x43 - F9 */
133
    SPECIAL, /* 0x43 - F9 */
134
    SPECIAL, /* 0x44 - F10 */
134
    SPECIAL, /* 0x44 - F10 */
135
    SPECIAL, /* 0x45 - NumLock */
135
    SPECIAL, /* 0x45 - NumLock */
136
    SPECIAL, /* 0x46 - ScrollLock */
136
    SPECIAL, /* 0x46 - ScrollLock */
137
    '7', '8', '9', '-',
137
    '7', '8', '9', '-',
138
    '4', '5', '6', '+',
138
    '4', '5', '6', '+',
139
    '1', '2', '3',
139
    '1', '2', '3',
140
    '0', '.',
140
    '0', '.',
141
    SPECIAL, /* 0x54 - Alt-SysRq */
141
    SPECIAL, /* 0x54 - Alt-SysRq */
142
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
142
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
143
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
143
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
144
    SPECIAL, /* 0x57 - F11 */
144
    SPECIAL, /* 0x57 - F11 */
145
    SPECIAL, /* 0x58 - F12 */
145
    SPECIAL, /* 0x58 - F12 */
146
    SPECIAL, /* 0x59 */
146
    SPECIAL, /* 0x59 */
147
    SPECIAL, /* 0x5a */
147
    SPECIAL, /* 0x5a */
148
    SPECIAL, /* 0x5b */
148
    SPECIAL, /* 0x5b */
149
    SPECIAL, /* 0x5c */
149
    SPECIAL, /* 0x5c */
150
    SPECIAL, /* 0x5d */
150
    SPECIAL, /* 0x5d */
151
    SPECIAL, /* 0x5e */
151
    SPECIAL, /* 0x5e */
152
    SPECIAL, /* 0x5f */
152
    SPECIAL, /* 0x5f */
153
    SPECIAL, /* 0x60 */
153
    SPECIAL, /* 0x60 */
154
    SPECIAL, /* 0x61 */
154
    SPECIAL, /* 0x61 */
155
    SPECIAL, /* 0x62 */
155
    SPECIAL, /* 0x62 */
156
    SPECIAL, /* 0x63 */
156
    SPECIAL, /* 0x63 */
157
    SPECIAL, /* 0x64 */
157
    SPECIAL, /* 0x64 */
158
    SPECIAL, /* 0x65 */
158
    SPECIAL, /* 0x65 */
159
    SPECIAL, /* 0x66 */
159
    SPECIAL, /* 0x66 */
160
    SPECIAL, /* 0x67 */
160
    SPECIAL, /* 0x67 */
161
    SPECIAL, /* 0x68 */
161
    SPECIAL, /* 0x68 */
162
    SPECIAL, /* 0x69 */
162
    SPECIAL, /* 0x69 */
163
    SPECIAL, /* 0x6a */
163
    SPECIAL, /* 0x6a */
164
    SPECIAL, /* 0x6b */
164
    SPECIAL, /* 0x6b */
165
    SPECIAL, /* 0x6c */
165
    SPECIAL, /* 0x6c */
166
    SPECIAL, /* 0x6d */
166
    SPECIAL, /* 0x6d */
167
    SPECIAL, /* 0x6e */
167
    SPECIAL, /* 0x6e */
168
    SPECIAL, /* 0x6f */
168
    SPECIAL, /* 0x6f */
169
    SPECIAL, /* 0x70 */
169
    SPECIAL, /* 0x70 */
170
    SPECIAL, /* 0x71 */
170
    SPECIAL, /* 0x71 */
171
    SPECIAL, /* 0x72 */
171
    SPECIAL, /* 0x72 */
172
    SPECIAL, /* 0x73 */
172
    SPECIAL, /* 0x73 */
173
    SPECIAL, /* 0x74 */
173
    SPECIAL, /* 0x74 */
174
    SPECIAL, /* 0x75 */
174
    SPECIAL, /* 0x75 */
175
    SPECIAL, /* 0x76 */
175
    SPECIAL, /* 0x76 */
176
    SPECIAL, /* 0x77 */
176
    SPECIAL, /* 0x77 */
177
    SPECIAL, /* 0x78 */
177
    SPECIAL, /* 0x78 */
178
    SPECIAL, /* 0x79 */
178
    SPECIAL, /* 0x79 */
179
    SPECIAL, /* 0x7a */
179
    SPECIAL, /* 0x7a */
180
    SPECIAL, /* 0x7b */
180
    SPECIAL, /* 0x7b */
181
    SPECIAL, /* 0x7c */
181
    SPECIAL, /* 0x7c */
182
    SPECIAL, /* 0x7d */
182
    SPECIAL, /* 0x7d */
183
    SPECIAL, /* 0x7e */
183
    SPECIAL, /* 0x7e */
184
    SPECIAL, /* 0x7f */
184
    SPECIAL, /* 0x7f */
185
};
185
};
186
 
186
 
187
/** Secondary meaning of scancodes. */
187
/** Secondary meaning of scancodes. */
188
static char sc_secondary_map[] = {
188
static char sc_secondary_map[] = {
189
    SPECIAL, /* 0x00 */
189
    SPECIAL, /* 0x00 */
190
    SPECIAL, /* 0x01 - Esc */
190
    SPECIAL, /* 0x01 - Esc */
191
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
191
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
192
    SPECIAL, /* 0x0e - Backspace */
192
    SPECIAL, /* 0x0e - Backspace */
193
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
193
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
194
    SPECIAL, /* 0x1d - LCtrl */
194
    SPECIAL, /* 0x1d - LCtrl */
195
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
195
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
196
    '~',
196
    '~',
197
    SPECIAL, /* 0x2a - LShift */
197
    SPECIAL, /* 0x2a - LShift */
198
    '|',
198
    '|',
199
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
199
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
200
    SPECIAL, /* 0x36 - RShift */
200
    SPECIAL, /* 0x36 - RShift */
201
    '*',
201
    '*',
202
    SPECIAL, /* 0x38 - LAlt */
202
    SPECIAL, /* 0x38 - LAlt */
203
    ' ',
203
    ' ',
204
    SPECIAL, /* 0x3a - CapsLock */
204
    SPECIAL, /* 0x3a - CapsLock */
205
    SPECIAL, /* 0x3b - F1 */
205
    SPECIAL, /* 0x3b - F1 */
206
    SPECIAL, /* 0x3c - F2 */
206
    SPECIAL, /* 0x3c - F2 */
207
    SPECIAL, /* 0x3d - F3 */
207
    SPECIAL, /* 0x3d - F3 */
208
    SPECIAL, /* 0x3e - F4 */
208
    SPECIAL, /* 0x3e - F4 */
209
    SPECIAL, /* 0x3f - F5 */
209
    SPECIAL, /* 0x3f - F5 */
210
    SPECIAL, /* 0x40 - F6 */
210
    SPECIAL, /* 0x40 - F6 */
211
    SPECIAL, /* 0x41 - F7 */
211
    SPECIAL, /* 0x41 - F7 */
212
    SPECIAL, /* 0x42 - F8 */
212
    SPECIAL, /* 0x42 - F8 */
213
    SPECIAL, /* 0x43 - F9 */
213
    SPECIAL, /* 0x43 - F9 */
214
    SPECIAL, /* 0x44 - F10 */
214
    SPECIAL, /* 0x44 - F10 */
215
    SPECIAL, /* 0x45 - NumLock */
215
    SPECIAL, /* 0x45 - NumLock */
216
    SPECIAL, /* 0x46 - ScrollLock */
216
    SPECIAL, /* 0x46 - ScrollLock */
217
    '7', '8', '9', '-',
217
    '7', '8', '9', '-',
218
    '4', '5', '6', '+',
218
    '4', '5', '6', '+',
219
    '1', '2', '3',
219
    '1', '2', '3',
220
    '0', '.',
220
    '0', '.',
221
    SPECIAL, /* 0x54 - Alt-SysRq */
221
    SPECIAL, /* 0x54 - Alt-SysRq */
222
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
222
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
223
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
223
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
224
    SPECIAL, /* 0x57 - F11 */
224
    SPECIAL, /* 0x57 - F11 */
225
    SPECIAL, /* 0x58 - F12 */
225
    SPECIAL, /* 0x58 - F12 */
226
    SPECIAL, /* 0x59 */
226
    SPECIAL, /* 0x59 */
227
    SPECIAL, /* 0x5a */
227
    SPECIAL, /* 0x5a */
228
    SPECIAL, /* 0x5b */
228
    SPECIAL, /* 0x5b */
229
    SPECIAL, /* 0x5c */
229
    SPECIAL, /* 0x5c */
230
    SPECIAL, /* 0x5d */
230
    SPECIAL, /* 0x5d */
231
    SPECIAL, /* 0x5e */
231
    SPECIAL, /* 0x5e */
232
    SPECIAL, /* 0x5f */
232
    SPECIAL, /* 0x5f */
233
    SPECIAL, /* 0x60 */
233
    SPECIAL, /* 0x60 */
234
    SPECIAL, /* 0x61 */
234
    SPECIAL, /* 0x61 */
235
    SPECIAL, /* 0x62 */
235
    SPECIAL, /* 0x62 */
236
    SPECIAL, /* 0x63 */
236
    SPECIAL, /* 0x63 */
237
    SPECIAL, /* 0x64 */
237
    SPECIAL, /* 0x64 */
238
    SPECIAL, /* 0x65 */
238
    SPECIAL, /* 0x65 */
239
    SPECIAL, /* 0x66 */
239
    SPECIAL, /* 0x66 */
240
    SPECIAL, /* 0x67 */
240
    SPECIAL, /* 0x67 */
241
    SPECIAL, /* 0x68 */
241
    SPECIAL, /* 0x68 */
242
    SPECIAL, /* 0x69 */
242
    SPECIAL, /* 0x69 */
243
    SPECIAL, /* 0x6a */
243
    SPECIAL, /* 0x6a */
244
    SPECIAL, /* 0x6b */
244
    SPECIAL, /* 0x6b */
245
    SPECIAL, /* 0x6c */
245
    SPECIAL, /* 0x6c */
246
    SPECIAL, /* 0x6d */
246
    SPECIAL, /* 0x6d */
247
    SPECIAL, /* 0x6e */
247
    SPECIAL, /* 0x6e */
248
    SPECIAL, /* 0x6f */
248
    SPECIAL, /* 0x6f */
249
    SPECIAL, /* 0x70 */
249
    SPECIAL, /* 0x70 */
250
    SPECIAL, /* 0x71 */
250
    SPECIAL, /* 0x71 */
251
    SPECIAL, /* 0x72 */
251
    SPECIAL, /* 0x72 */
252
    SPECIAL, /* 0x73 */
252
    SPECIAL, /* 0x73 */
253
    SPECIAL, /* 0x74 */
253
    SPECIAL, /* 0x74 */
254
    SPECIAL, /* 0x75 */
254
    SPECIAL, /* 0x75 */
255
    SPECIAL, /* 0x76 */
255
    SPECIAL, /* 0x76 */
256
    SPECIAL, /* 0x77 */
256
    SPECIAL, /* 0x77 */
257
    SPECIAL, /* 0x78 */
257
    SPECIAL, /* 0x78 */
258
    SPECIAL, /* 0x79 */
258
    SPECIAL, /* 0x79 */
259
    SPECIAL, /* 0x7a */
259
    SPECIAL, /* 0x7a */
260
    SPECIAL, /* 0x7b */
260
    SPECIAL, /* 0x7b */
261
    SPECIAL, /* 0x7c */
261
    SPECIAL, /* 0x7c */
262
    SPECIAL, /* 0x7d */
262
    SPECIAL, /* 0x7d */
263
    SPECIAL, /* 0x7e */
263
    SPECIAL, /* 0x7e */
264
    SPECIAL, /* 0x7f */
264
    SPECIAL, /* 0x7f */
265
};
265
};
266
 
266
 
267
static void i8042_interrupt(int n, istate_t *istate);
267
static void i8042_interrupt(int n, istate_t *istate);
268
static void i8042_wait(void);
268
static void i8042_wait(void);
269
 
269
 
270
/** Initialize i8042. */
270
/** Initialize i8042. */
271
void i8042_init(void)
271
void i8042_init(void)
272
{
272
{
273
    int i;
273
    int i;
274
 
274
 
275
    exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
275
    exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
276
    i8042_wait();
276
    i8042_wait();
277
    i8042_command_write(i8042_SET_COMMAND);
277
    i8042_command_write(i8042_SET_COMMAND);
278
    i8042_wait();
278
    i8042_wait();
279
    i8042_data_write(i8042_COMMAND);
279
    i8042_data_write(i8042_COMMAND);
280
    i8042_wait();
280
    i8042_wait();
281
 
281
 
282
    trap_virtual_enable_irqs(1<<IRQ_KBD);
282
    trap_virtual_enable_irqs(1<<IRQ_KBD);
283
    chardev_initialize("i8042_kbd", &kbrd, &ops);
283
    chardev_initialize("i8042_kbd", &kbrd, &ops);
284
    stdin = &kbrd;
284
    stdin = &kbrd;
285
 
285
 
286
    /*
286
    /*
287
     * Clear input buffer.
287
     * Clear input buffer.
288
     * Number of iterations is limited to prevent infinite looping.
288
     * Number of iterations is limited to prevent infinite looping.
289
     */
289
     */
290
    for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
290
    for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
291
        i8042_data_read();
291
        i8042_data_read();
292
    }  
292
    }  
293
}
293
}
294
 
294
 
295
/** Process i8042 interrupt.
295
/** Process i8042 interrupt.
296
 *
296
 *
297
 * @param n Interrupt vector.
297
 * @param n Interrupt vector.
298
 * @param stack Interrupted stack.
298
 * @param stack Interrupted stack.
299
 */
299
 */
300
void i8042_interrupt(int n, istate_t *istate)
300
void i8042_interrupt(int n, istate_t *istate)
301
{
301
{
302
    __u8 x;
302
    __u8 x;
303
 
303
 
304
    trap_virtual_eoi();
304
    trap_virtual_eoi();
305
    x = i8042_data_read();
305
    x = i8042_data_read();
306
    if (x & KEY_RELEASE)
306
    if (x & KEY_RELEASE)
307
        key_released(x ^ KEY_RELEASE);
307
        key_released(x ^ KEY_RELEASE);
308
    else
308
    else
309
        key_pressed(x);
309
        key_pressed(x);
310
}
310
}
311
 
311
 
312
/** Wait until the controller reads its data. */
312
/** Wait until the controller reads its data. */
313
void i8042_wait(void) {
313
void i8042_wait(void) {
314
    while (i8042_status_read() & i8042_WAIT_MASK) {
314
    while (i8042_status_read() & i8042_WAIT_MASK) {
315
        /* wait */
315
        /* wait */
316
    }
316
    }
317
}
317
}
318
 
318
 
319
/** Process release of key.
319
/** Process release of key.
320
 *
320
 *
321
 * @param sc Scancode of the key being released.
321
 * @param sc Scancode of the key being released.
322
 */
322
 */
323
void key_released(__u8 sc)
323
void key_released(__u8 sc)
324
{
324
{
325
    spinlock_lock(&keylock);
325
    spinlock_lock(&keylock);
326
    switch (sc) {
326
    switch (sc) {
327
        case SC_LSHIFT:
327
        case SC_LSHIFT:
328
        case SC_RSHIFT:
328
        case SC_RSHIFT:
329
        keyflags &= ~PRESSED_SHIFT;
329
        keyflags &= ~PRESSED_SHIFT;
330
        break;
330
        break;
331
        case SC_CAPSLOCK:
331
        case SC_CAPSLOCK:
332
        keyflags &= ~PRESSED_CAPSLOCK;
332
        keyflags &= ~PRESSED_CAPSLOCK;
333
        if (lockflags & LOCKED_CAPSLOCK)
333
        if (lockflags & LOCKED_CAPSLOCK)
334
            lockflags &= ~LOCKED_CAPSLOCK;
334
            lockflags &= ~LOCKED_CAPSLOCK;
335
        else
335
        else
336
            lockflags |= LOCKED_CAPSLOCK;
336
            lockflags |= LOCKED_CAPSLOCK;
337
        break;
337
        break;
338
        default:
338
        default:
339
        break;
339
        break;
340
    }
340
    }
341
    spinlock_unlock(&keylock);
341
    spinlock_unlock(&keylock);
342
}
342
}
343
 
343
 
344
/** Process keypress.
344
/** Process keypress.
345
 *
345
 *
346
 * @param sc Scancode of the key being pressed.
346
 * @param sc Scancode of the key being pressed.
347
 */
347
 */
348
void key_pressed(__u8 sc)
348
void key_pressed(__u8 sc)
349
{
349
{
350
    char *map = sc_primary_map;
350
    char *map = sc_primary_map;
351
    char ascii = sc_primary_map[sc];
351
    char ascii = sc_primary_map[sc];
352
    bool shift, capslock;
352
    bool shift, capslock;
353
    bool letter = false;
353
    bool letter = false;
354
 
354
 
355
    spinlock_lock(&keylock);
355
    spinlock_lock(&keylock);
356
    switch (sc) {
356
    switch (sc) {
357
    case SC_LSHIFT:
357
    case SC_LSHIFT:
358
    case SC_RSHIFT:
358
    case SC_RSHIFT:
359
            keyflags |= PRESSED_SHIFT;
359
            keyflags |= PRESSED_SHIFT;
360
        break;
360
        break;
361
    case SC_CAPSLOCK:
361
    case SC_CAPSLOCK:
362
        keyflags |= PRESSED_CAPSLOCK;
362
        keyflags |= PRESSED_CAPSLOCK;
363
        break;
363
        break;
364
    case SC_SPEC_ESCAPE:
364
    case SC_SPEC_ESCAPE:
365
        break;
365
        break;
366
    case SC_LEFTARR:
366
    case SC_LEFTARR:
367
        chardev_push_character(&kbrd, 0x1b);
367
        chardev_push_character(&kbrd, 0x1b);
368
        chardev_push_character(&kbrd, 0x5b);
368
        chardev_push_character(&kbrd, 0x5b);
369
        chardev_push_character(&kbrd, 0x44);
369
        chardev_push_character(&kbrd, 0x44);
370
        break;
370
        break;
371
    case SC_RIGHTARR:
371
    case SC_RIGHTARR:
372
        chardev_push_character(&kbrd, 0x1b);
372
        chardev_push_character(&kbrd, 0x1b);
373
        chardev_push_character(&kbrd, 0x5b);
373
        chardev_push_character(&kbrd, 0x5b);
374
        chardev_push_character(&kbrd, 0x43);
374
        chardev_push_character(&kbrd, 0x43);
375
        break;
375
        break;
376
    case SC_UPARR:
376
    case SC_UPARR:
377
        chardev_push_character(&kbrd, 0x1b);
377
        chardev_push_character(&kbrd, 0x1b);
378
        chardev_push_character(&kbrd, 0x5b);
378
        chardev_push_character(&kbrd, 0x5b);
379
        chardev_push_character(&kbrd, 0x41);
379
        chardev_push_character(&kbrd, 0x41);
380
        break;
380
        break;
381
    case SC_DOWNARR:
381
    case SC_DOWNARR:
382
        chardev_push_character(&kbrd, 0x1b);
382
        chardev_push_character(&kbrd, 0x1b);
383
        chardev_push_character(&kbrd, 0x5b);
383
        chardev_push_character(&kbrd, 0x5b);
384
        chardev_push_character(&kbrd, 0x42);
384
        chardev_push_character(&kbrd, 0x42);
385
        break;
385
        break;
386
    case SC_HOME:
386
    case SC_HOME:
387
        chardev_push_character(&kbrd, 0x1b);
387
        chardev_push_character(&kbrd, 0x1b);
388
        chardev_push_character(&kbrd, 0x4f);
388
        chardev_push_character(&kbrd, 0x4f);
389
        chardev_push_character(&kbrd, 0x48);
389
        chardev_push_character(&kbrd, 0x48);
390
        break;
390
        break;
391
    case SC_END:
391
    case SC_END:
392
        chardev_push_character(&kbrd, 0x1b);
392
        chardev_push_character(&kbrd, 0x1b);
393
        chardev_push_character(&kbrd, 0x4f);
393
        chardev_push_character(&kbrd, 0x4f);
394
        chardev_push_character(&kbrd, 0x46);
394
        chardev_push_character(&kbrd, 0x46);
395
        break;
395
        break;
396
    case SC_DELETE:
396
    case SC_DELETE:
397
        chardev_push_character(&kbrd, 0x1b);
397
        chardev_push_character(&kbrd, 0x1b);
398
        chardev_push_character(&kbrd, 0x5b);
398
        chardev_push_character(&kbrd, 0x5b);
399
        chardev_push_character(&kbrd, 0x33);
399
        chardev_push_character(&kbrd, 0x33);
400
        chardev_push_character(&kbrd, 0x7e);
400
        chardev_push_character(&kbrd, 0x7e);
401
        break;
401
        break;
402
    default:
402
    default:
403
            letter = is_lower(ascii);
403
            letter = is_lower(ascii);
404
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
404
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
405
        shift = keyflags & PRESSED_SHIFT;
405
        shift = keyflags & PRESSED_SHIFT;
406
        if (letter && capslock)
406
        if (letter && capslock)
407
            shift = !shift;
407
            shift = !shift;
408
        if (shift)
408
        if (shift)
409
            map = sc_secondary_map;
409
            map = sc_secondary_map;
410
        chardev_push_character(&kbrd, map[sc]);
410
        chardev_push_character(&kbrd, map[sc]);
411
        break;
411
        break;
412
    }
412
    }
413
    spinlock_unlock(&keylock);
413
    spinlock_unlock(&keylock);
414
}
414
}
415
 
415
 
416
/* Called from getc(). */
416
/* Called from getc(). */
417
void i8042_resume(chardev_t *d)
417
void i8042_resume(chardev_t *d)
418
{
418
{
419
}
419
}
420
 
420
 
421
/* Called from getc(). */
421
/* Called from getc(). */
422
void i8042_suspend(chardev_t *d)
422
void i8042_suspend(chardev_t *d)
423
{
423
{
424
}
424
}
425
 
425
 
426
static __u8 active_read_buff_read(void)
426
static __u8 active_read_buff_read(void)
427
{
427
{
428
    static int i=0;
428
    static int i=0;
429
    i &= (ACTIVE_READ_BUFF_SIZE-1);
429
    i &= (ACTIVE_READ_BUFF_SIZE-1);
430
    if(!active_read_buff[i]) {
430
    if(!active_read_buff[i]) {
431
        return 0;
431
        return 0;
432
    }
432
    }
433
    return active_read_buff[i++];
433
    return active_read_buff[i++];
434
}
434
}
435
 
435
 
436
static void active_read_buff_write(__u8 ch)
436
static void active_read_buff_write(__u8 ch)
437
{
437
{
438
    static int i=0;
438
    static int i=0;
439
    active_read_buff[i] = ch;
439
    active_read_buff[i] = ch;
440
    i++;
440
    i++;
441
    i &= (ACTIVE_READ_BUFF_SIZE-1);
441
    i &= (ACTIVE_READ_BUFF_SIZE-1);
442
    active_read_buff[i]=0;
442
    active_read_buff[i]=0;
443
}
443
}
444
 
444
 
445
 
445
 
446
static void active_read_key_pressed(__u8 sc)
446
static void active_read_key_pressed(__u8 sc)
447
{
447
{
448
    char *map = sc_primary_map;
448
    char *map = sc_primary_map;
449
    char ascii = sc_primary_map[sc];
449
    char ascii = sc_primary_map[sc];
450
    bool shift, capslock;
450
    bool shift, capslock;
451
    bool letter = false;
451
    bool letter = false;
452
 
452
 
453
    /*spinlock_lock(&keylock);*/
453
    /*spinlock_lock(&keylock);*/
454
    switch (sc) {
454
    switch (sc) {
455
    case SC_LSHIFT:
455
    case SC_LSHIFT:
456
    case SC_RSHIFT:
456
    case SC_RSHIFT:
457
            keyflags |= PRESSED_SHIFT;
457
            keyflags |= PRESSED_SHIFT;
458
        break;
458
        break;
459
    case SC_CAPSLOCK:
459
    case SC_CAPSLOCK:
460
        keyflags |= PRESSED_CAPSLOCK;
460
        keyflags |= PRESSED_CAPSLOCK;
461
        break;
461
        break;
462
    case SC_SPEC_ESCAPE:
462
    case SC_SPEC_ESCAPE:
463
        break;
463
        break;
464
    case SC_LEFTARR:
464
    case SC_LEFTARR:
465
        active_read_buff_write(0x1b);
465
        active_read_buff_write(0x1b);
466
        active_read_buff_write(0x5b);
466
        active_read_buff_write(0x5b);
467
        active_read_buff_write(0x44);
467
        active_read_buff_write(0x44);
468
        break;
468
        break;
469
    case SC_RIGHTARR:
469
    case SC_RIGHTARR:
470
        active_read_buff_write(0x1b);
470
        active_read_buff_write(0x1b);
471
        active_read_buff_write(0x5b);
471
        active_read_buff_write(0x5b);
472
        active_read_buff_write(0x43);
472
        active_read_buff_write(0x43);
473
        break;
473
        break;
474
    case SC_UPARR:
474
    case SC_UPARR:
475
        active_read_buff_write(0x1b);
475
        active_read_buff_write(0x1b);
476
        active_read_buff_write(0x5b);
476
        active_read_buff_write(0x5b);
477
        active_read_buff_write(0x41);
477
        active_read_buff_write(0x41);
478
        break;
478
        break;
479
    case SC_DOWNARR:
479
    case SC_DOWNARR:
480
        active_read_buff_write(0x1b);
480
        active_read_buff_write(0x1b);
481
        active_read_buff_write(0x5b);
481
        active_read_buff_write(0x5b);
482
        active_read_buff_write(0x42);
482
        active_read_buff_write(0x42);
483
        break;
483
        break;
484
    case SC_HOME:
484
    case SC_HOME:
485
        active_read_buff_write(0x1b);
485
        active_read_buff_write(0x1b);
486
        active_read_buff_write(0x4f);
486
        active_read_buff_write(0x4f);
487
        active_read_buff_write(0x48);
487
        active_read_buff_write(0x48);
488
        break;
488
        break;
489
    case SC_END:
489
    case SC_END:
490
        active_read_buff_write(0x1b);
490
        active_read_buff_write(0x1b);
491
        active_read_buff_write(0x4f);
491
        active_read_buff_write(0x4f);
492
        active_read_buff_write(0x46);
492
        active_read_buff_write(0x46);
493
        break;
493
        break;
494
    case SC_DELETE:
494
    case SC_DELETE:
495
        active_read_buff_write(0x1b);
495
        active_read_buff_write(0x1b);
496
        active_read_buff_write(0x5b);
496
        active_read_buff_write(0x5b);
497
        active_read_buff_write(0x33);
497
        active_read_buff_write(0x33);
498
        active_read_buff_write(0x7e);
498
        active_read_buff_write(0x7e);
499
        break;
499
        break;
500
    default:
500
    default:
501
            letter = is_lower(ascii);
501
            letter = is_lower(ascii);
502
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
502
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
503
        shift = keyflags & PRESSED_SHIFT;
503
        shift = keyflags & PRESSED_SHIFT;
504
        if (letter && capslock)
504
        if (letter && capslock)
505
            shift = !shift;
505
            shift = !shift;
506
        if (shift)
506
        if (shift)
507
            map = sc_secondary_map;
507
            map = sc_secondary_map;
508
        active_read_buff_write(map[sc]);
508
        active_read_buff_write(map[sc]);
509
        break;
509
        break;
510
    }
510
    }
511
    /*spinlock_unlock(&keylock);*/
511
    /*spinlock_unlock(&keylock);*/
512
 
512
 
513
}
513
}
514
 
514
 
515
static char key_read(chardev_t *d)
515
static char key_read(chardev_t *d)
516
{
516
{
517
    char ch;   
517
    char ch;   
518
 
518
 
519
    while(!(ch = active_read_buff_read())) {
519
    while(!(ch = active_read_buff_read())) {
520
        __u8 x;
520
        __u8 x;
521
        while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK)))
521
        while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK)))
522
            ;
522
            ;
523
        x = i8042_data_read();
523
        x = i8042_data_read();
524
        if (x != IGNORE_CODE) {
524
        if (x != IGNORE_CODE) {
525
            if (x & KEY_RELEASE)
525
            if (x & KEY_RELEASE)
526
                key_released(x ^ KEY_RELEASE);
526
                key_released(x ^ KEY_RELEASE);
527
            else
527
            else
528
                active_read_key_pressed(x);
528
                active_read_key_pressed(x);
529
        }
529
        }
530
    }
530
    }
531
    return ch;
531
    return ch;
532
}
532
}
533
 
533
 
534
/** Poll for key press and release events.
534
/** Poll for key press and release events.
535
 *
535
 *
536
 * This function can be used to implement keyboard polling.
536
 * This function can be used to implement keyboard polling.
537
 */
537
 */
538
void i8042_poll(void)
538
void i8042_poll(void)
539
{
539
{
540
    __u8 x;
540
    __u8 x;
541
 
541
 
542
    while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
542
    while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
543
        x = i8042_data_read();
543
        x = i8042_data_read();
544
        if (x != IGNORE_CODE) {
544
        if (x != IGNORE_CODE) {
545
            if (x & KEY_RELEASE)
545
            if (x & KEY_RELEASE)
546
                key_released(x ^ KEY_RELEASE);
546
                key_released(x ^ KEY_RELEASE);
547
            else
547
            else
548
                key_pressed(x);
548
                key_pressed(x);
549
        }
549
        }
550
    }
550
    }
551
}
551
}
552
 
552