Rev 534 | Rev 575 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <arch/i8042.h> |
||
30 | #include <arch/i8259.h> |
||
31 | #include <arch/interrupt.h> |
||
32 | #include <cpu.h> |
||
33 | #include <arch/asm.h> |
||
34 | #include <arch.h> |
||
35 | #include <print.h> |
||
508 | jermar | 36 | #include <synch/spinlock.h> |
37 | #include <typedefs.h> |
||
511 | jermar | 38 | #include <console/chardev.h> |
39 | #include <console/console.h> |
||
517 | jermar | 40 | #include <macros.h> |
1 | jermar | 41 | |
508 | jermar | 42 | /** |
1 | jermar | 43 | * i8042 processor driver. |
508 | jermar | 44 | * It takes care of low-level keyboard functions. |
1 | jermar | 45 | */ |
46 | |||
512 | jermar | 47 | #define i8042_DATA 0x60 |
48 | #define i8042_STATUS 0x64 |
||
49 | |||
50 | /** Keyboard commands. */ |
||
51 | #define KBD_ENABLE 0xf4 |
||
52 | #define KBD_DISABLE 0xf5 |
||
53 | #define KBD_ACK 0xfa |
||
54 | |||
508 | jermar | 55 | #define SPECIAL '?' |
56 | #define KEY_RELEASE 0x80 |
||
57 | |||
58 | static void key_released(__u8 sc); |
||
59 | static void key_pressed(__u8 sc); |
||
60 | |||
61 | #define PRESSED_SHIFT (1<<0) |
||
62 | #define PRESSED_CAPSLOCK (1<<1) |
||
63 | #define LOCKED_CAPSLOCK (1<<0) |
||
64 | |||
509 | jermar | 65 | static spinlock_t keylock; /**< keylock protects keyflags and lockflags. */ |
508 | jermar | 66 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
509 | jermar | 67 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
508 | jermar | 68 | |
511 | jermar | 69 | static void i8042_suspend(void); |
70 | static void i8042_resume(void); |
||
71 | |||
72 | static chardev_t kbrd; |
||
73 | static chardev_operations_t ops = { |
||
74 | .suspend = i8042_suspend, |
||
75 | .resume = i8042_resume |
||
76 | }; |
||
77 | |||
508 | jermar | 78 | /** Primary meaning of scancodes. */ |
79 | static char sc_primary_map[] = { |
||
80 | SPECIAL, /* 0x00 */ |
||
81 | SPECIAL, /* 0x01 - Esc */ |
||
82 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', |
||
83 | SPECIAL, /* 0x0e - Backspace */ |
||
84 | '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', |
||
85 | SPECIAL, /* 0x1d - LCtrl */ |
||
86 | 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', |
||
87 | '`', |
||
88 | SPECIAL, /* 0x2a - LShift */ |
||
89 | '\\', |
||
90 | 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', |
||
91 | SPECIAL, /* 0x36 - RShift */ |
||
92 | '*', |
||
93 | SPECIAL, /* 0x38 - LAlt */ |
||
94 | ' ', |
||
95 | SPECIAL, /* 0x3a - CapsLock */ |
||
96 | SPECIAL, /* 0x3b - F1 */ |
||
97 | SPECIAL, /* 0x3c - F2 */ |
||
98 | SPECIAL, /* 0x3d - F3 */ |
||
99 | SPECIAL, /* 0x3e - F4 */ |
||
100 | SPECIAL, /* 0x3f - F5 */ |
||
101 | SPECIAL, /* 0x40 - F6 */ |
||
102 | SPECIAL, /* 0x41 - F7 */ |
||
103 | SPECIAL, /* 0x42 - F8 */ |
||
104 | SPECIAL, /* 0x43 - F9 */ |
||
105 | SPECIAL, /* 0x44 - F10 */ |
||
106 | SPECIAL, /* 0x45 - NumLock */ |
||
107 | SPECIAL, /* 0x46 - ScrollLock */ |
||
108 | '7', '8', '9', '-', |
||
109 | '4', '5', '6', '+', |
||
110 | '1', '2', '3', |
||
111 | '0', '.', |
||
112 | SPECIAL, /* 0x54 - Alt-SysRq */ |
||
113 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
||
114 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
||
115 | SPECIAL, /* 0x57 - F11 */ |
||
116 | SPECIAL, /* 0x58 - F12 */ |
||
117 | SPECIAL, /* 0x59 */ |
||
118 | SPECIAL, /* 0x5a */ |
||
119 | SPECIAL, /* 0x5b */ |
||
120 | SPECIAL, /* 0x5c */ |
||
121 | SPECIAL, /* 0x5d */ |
||
122 | SPECIAL, /* 0x5e */ |
||
123 | SPECIAL, /* 0x5f */ |
||
124 | SPECIAL, /* 0x60 */ |
||
125 | SPECIAL, /* 0x61 */ |
||
126 | SPECIAL, /* 0x62 */ |
||
127 | SPECIAL, /* 0x63 */ |
||
128 | SPECIAL, /* 0x64 */ |
||
129 | SPECIAL, /* 0x65 */ |
||
130 | SPECIAL, /* 0x66 */ |
||
131 | SPECIAL, /* 0x67 */ |
||
132 | SPECIAL, /* 0x68 */ |
||
133 | SPECIAL, /* 0x69 */ |
||
134 | SPECIAL, /* 0x6a */ |
||
135 | SPECIAL, /* 0x6b */ |
||
136 | SPECIAL, /* 0x6c */ |
||
137 | SPECIAL, /* 0x6d */ |
||
138 | SPECIAL, /* 0x6e */ |
||
139 | SPECIAL, /* 0x6f */ |
||
140 | SPECIAL, /* 0x70 */ |
||
141 | SPECIAL, /* 0x71 */ |
||
142 | SPECIAL, /* 0x72 */ |
||
143 | SPECIAL, /* 0x73 */ |
||
144 | SPECIAL, /* 0x74 */ |
||
145 | SPECIAL, /* 0x75 */ |
||
146 | SPECIAL, /* 0x76 */ |
||
147 | SPECIAL, /* 0x77 */ |
||
148 | SPECIAL, /* 0x78 */ |
||
149 | SPECIAL, /* 0x79 */ |
||
150 | SPECIAL, /* 0x7a */ |
||
151 | SPECIAL, /* 0x7b */ |
||
152 | SPECIAL, /* 0x7c */ |
||
153 | SPECIAL, /* 0x7d */ |
||
154 | SPECIAL, /* 0x7e */ |
||
155 | SPECIAL, /* 0x7f */ |
||
156 | }; |
||
157 | |||
158 | /** Secondary meaning of scancodes. */ |
||
159 | static char sc_secondary_map[] = { |
||
160 | SPECIAL, /* 0x00 */ |
||
161 | SPECIAL, /* 0x01 - Esc */ |
||
162 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', |
||
163 | SPECIAL, /* 0x0e - Backspace */ |
||
164 | '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', |
||
165 | SPECIAL, /* 0x1d - LCtrl */ |
||
166 | 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', |
||
167 | '~', |
||
168 | SPECIAL, /* 0x2a - LShift */ |
||
169 | '|', |
||
170 | 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', |
||
171 | SPECIAL, /* 0x36 - RShift */ |
||
172 | '*', |
||
173 | SPECIAL, /* 0x38 - LAlt */ |
||
174 | ' ', |
||
175 | SPECIAL, /* 0x3a - CapsLock */ |
||
176 | SPECIAL, /* 0x3b - F1 */ |
||
177 | SPECIAL, /* 0x3c - F2 */ |
||
178 | SPECIAL, /* 0x3d - F3 */ |
||
179 | SPECIAL, /* 0x3e - F4 */ |
||
180 | SPECIAL, /* 0x3f - F5 */ |
||
181 | SPECIAL, /* 0x40 - F6 */ |
||
182 | SPECIAL, /* 0x41 - F7 */ |
||
183 | SPECIAL, /* 0x42 - F8 */ |
||
184 | SPECIAL, /* 0x43 - F9 */ |
||
185 | SPECIAL, /* 0x44 - F10 */ |
||
186 | SPECIAL, /* 0x45 - NumLock */ |
||
187 | SPECIAL, /* 0x46 - ScrollLock */ |
||
188 | '7', '8', '9', '-', |
||
189 | '4', '5', '6', '+', |
||
190 | '1', '2', '3', |
||
191 | '0', '.', |
||
192 | SPECIAL, /* 0x54 - Alt-SysRq */ |
||
193 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
||
194 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
||
195 | SPECIAL, /* 0x57 - F11 */ |
||
196 | SPECIAL, /* 0x58 - F12 */ |
||
197 | SPECIAL, /* 0x59 */ |
||
198 | SPECIAL, /* 0x5a */ |
||
199 | SPECIAL, /* 0x5b */ |
||
200 | SPECIAL, /* 0x5c */ |
||
201 | SPECIAL, /* 0x5d */ |
||
202 | SPECIAL, /* 0x5e */ |
||
203 | SPECIAL, /* 0x5f */ |
||
204 | SPECIAL, /* 0x60 */ |
||
205 | SPECIAL, /* 0x61 */ |
||
206 | SPECIAL, /* 0x62 */ |
||
207 | SPECIAL, /* 0x63 */ |
||
208 | SPECIAL, /* 0x64 */ |
||
209 | SPECIAL, /* 0x65 */ |
||
210 | SPECIAL, /* 0x66 */ |
||
211 | SPECIAL, /* 0x67 */ |
||
212 | SPECIAL, /* 0x68 */ |
||
213 | SPECIAL, /* 0x69 */ |
||
214 | SPECIAL, /* 0x6a */ |
||
215 | SPECIAL, /* 0x6b */ |
||
216 | SPECIAL, /* 0x6c */ |
||
217 | SPECIAL, /* 0x6d */ |
||
218 | SPECIAL, /* 0x6e */ |
||
219 | SPECIAL, /* 0x6f */ |
||
220 | SPECIAL, /* 0x70 */ |
||
221 | SPECIAL, /* 0x71 */ |
||
222 | SPECIAL, /* 0x72 */ |
||
223 | SPECIAL, /* 0x73 */ |
||
224 | SPECIAL, /* 0x74 */ |
||
225 | SPECIAL, /* 0x75 */ |
||
226 | SPECIAL, /* 0x76 */ |
||
227 | SPECIAL, /* 0x77 */ |
||
228 | SPECIAL, /* 0x78 */ |
||
229 | SPECIAL, /* 0x79 */ |
||
230 | SPECIAL, /* 0x7a */ |
||
231 | SPECIAL, /* 0x7b */ |
||
232 | SPECIAL, /* 0x7c */ |
||
233 | SPECIAL, /* 0x7d */ |
||
234 | SPECIAL, /* 0x7e */ |
||
235 | SPECIAL, /* 0x7f */ |
||
236 | }; |
||
237 | |||
238 | /** Initialize i8042. */ |
||
1 | jermar | 239 | void i8042_init(void) |
240 | { |
||
241 | trap_register(VECTOR_KBD, i8042_interrupt); |
||
512 | jermar | 242 | trap_virtual_enable_irqs(1<<IRQ_KBD); |
552 | palkovsky | 243 | spinlock_initialize(&keylock, "i8042_lock"); |
511 | jermar | 244 | chardev_initialize(&kbrd, &ops); |
245 | stdin = &kbrd; |
||
1 | jermar | 246 | } |
247 | |||
508 | jermar | 248 | /** Process i8042 interrupt. |
249 | * |
||
250 | * @param n Interrupt vector. |
||
251 | * @param stack Interrupted stack. |
||
252 | */ |
||
206 | palkovsky | 253 | void i8042_interrupt(__u8 n, __native stack[]) |
1 | jermar | 254 | { |
255 | __u8 x; |
||
256 | |||
257 | trap_virtual_eoi(); |
||
512 | jermar | 258 | x = inb(i8042_DATA); |
508 | jermar | 259 | if (x & KEY_RELEASE) |
260 | key_released(x ^ KEY_RELEASE); |
||
261 | else |
||
262 | key_pressed(x); |
||
1 | jermar | 263 | } |
508 | jermar | 264 | |
265 | /** Process release of key. |
||
266 | * |
||
267 | * @param sc Scancode of the key being released. |
||
268 | */ |
||
269 | void key_released(__u8 sc) |
||
270 | { |
||
271 | spinlock_lock(&keylock); |
||
272 | switch (sc) { |
||
273 | case SC_LSHIFT: |
||
274 | case SC_RSHIFT: |
||
275 | keyflags &= ~PRESSED_SHIFT; |
||
276 | break; |
||
277 | case SC_CAPSLOCK: |
||
278 | keyflags &= ~PRESSED_CAPSLOCK; |
||
279 | if (lockflags & LOCKED_CAPSLOCK) |
||
280 | lockflags &= ~LOCKED_CAPSLOCK; |
||
281 | else |
||
282 | lockflags |= LOCKED_CAPSLOCK; |
||
283 | break; |
||
284 | default: |
||
285 | break; |
||
286 | } |
||
287 | spinlock_unlock(&keylock); |
||
288 | } |
||
289 | |||
290 | /** Process keypress. |
||
291 | * |
||
292 | * @param sc Scancode of the key being pressed. |
||
293 | */ |
||
294 | void key_pressed(__u8 sc) |
||
295 | { |
||
296 | char *map = sc_primary_map; |
||
297 | char ascii = sc_primary_map[sc]; |
||
298 | bool shift, capslock; |
||
299 | bool letter = false; |
||
300 | |||
301 | spinlock_lock(&keylock); |
||
302 | switch (sc) { |
||
303 | case SC_LSHIFT: |
||
304 | case SC_RSHIFT: |
||
305 | keyflags |= PRESSED_SHIFT; |
||
306 | break; |
||
307 | case SC_CAPSLOCK: |
||
308 | keyflags |= PRESSED_CAPSLOCK; |
||
309 | break; |
||
310 | default: |
||
517 | jermar | 311 | letter = is_lower(ascii); |
508 | jermar | 312 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
313 | shift = keyflags & PRESSED_SHIFT; |
||
314 | if (letter && capslock) |
||
315 | shift = !shift; |
||
316 | if (shift) |
||
317 | map = sc_secondary_map; |
||
511 | jermar | 318 | chardev_push_character(&kbrd, map[sc]); |
508 | jermar | 319 | break; |
320 | } |
||
321 | spinlock_unlock(&keylock); |
||
322 | } |
||
511 | jermar | 323 | |
324 | /* Called from getc(). */ |
||
325 | void i8042_resume(void) |
||
326 | { |
||
327 | } |
||
328 | |||
329 | /* Called from getc(). */ |
||
330 | void i8042_suspend(void) |
||
331 | { |
||
332 | } |