source: mainline/arch/ia32/src/drivers/i8042.c@ 95d191c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95d191c was 95d191c, checked in by Jakub Vana <jakub.vana@…>, 19 years ago

i8042 bug fix…

  • Property mode set to 100644
File size: 10.2 KB
Line 
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>
36#include <synch/spinlock.h>
37#include <typedefs.h>
38#include <console/chardev.h>
39#include <console/console.h>
40#include <macros.h>
41#include <interrupt.h>
42
43/**
44 * i8042 processor driver.
45 * It takes care of low-level keyboard functions.
46 */
47
48#define i8042_DATA 0x60
49#define i8042_STATUS 0x64
50
51
52/** Keyboard commands. */
53#define KBD_ENABLE 0xf4
54#define KBD_DISABLE 0xf5
55#define KBD_ACK 0xfa
56
57/*
58 60 Write 8042 Command Byte: next data byte written to port 60h is
59 placed in 8042 command register.Format:
60
61 |7|6|5|4|3|2|1|0|8042 Command Byte
62 | | | | | | | `---- 1=enable output register full interrupt
63 | | | | | | `----- should be 0
64 | | | | | `------ 1=set status register system, 0=clear
65 | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
66 | | | `-------- disable keyboard I/O by driving clock line low
67 | | `--------- disable auxiliary device, drives clock line low
68 | `---------- IBM scancode translation 0=AT, 1=PC/XT
69 `----------- reserved, should be 0
70*/
71
72#define i8042_SET_COMMAND 0x60
73#define i8042_COMMAND 0x49
74#define i8042_WAIT_MASK 0x02
75
76
77#define SPECIAL '?'
78#define KEY_RELEASE 0x80
79
80static void key_released(__u8 sc);
81static void key_pressed(__u8 sc);
82
83#define PRESSED_SHIFT (1<<0)
84#define PRESSED_CAPSLOCK (1<<1)
85#define LOCKED_CAPSLOCK (1<<0)
86
87SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */
88static volatile int keyflags; /**< Tracking of multiple keypresses. */
89static volatile int lockflags; /**< Tracking of multiple keys lockings. */
90
91static void i8042_suspend(chardev_t *);
92static void i8042_resume(chardev_t *);
93
94static chardev_t kbrd;
95static chardev_operations_t ops = {
96 .suspend = i8042_suspend,
97 .resume = i8042_resume
98};
99
100/** Primary meaning of scancodes. */
101static char sc_primary_map[] = {
102 SPECIAL, /* 0x00 */
103 SPECIAL, /* 0x01 - Esc */
104 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
105 '\b', /* 0x0e - Backspace */
106 '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
107 SPECIAL, /* 0x1d - LCtrl */
108 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
109 '`',
110 SPECIAL, /* 0x2a - LShift */
111 '\\',
112 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
113 SPECIAL, /* 0x36 - RShift */
114 '*',
115 SPECIAL, /* 0x38 - LAlt */
116 ' ',
117 SPECIAL, /* 0x3a - CapsLock */
118 SPECIAL, /* 0x3b - F1 */
119 SPECIAL, /* 0x3c - F2 */
120 SPECIAL, /* 0x3d - F3 */
121 SPECIAL, /* 0x3e - F4 */
122 SPECIAL, /* 0x3f - F5 */
123 SPECIAL, /* 0x40 - F6 */
124 SPECIAL, /* 0x41 - F7 */
125 SPECIAL, /* 0x42 - F8 */
126 SPECIAL, /* 0x43 - F9 */
127 SPECIAL, /* 0x44 - F10 */
128 SPECIAL, /* 0x45 - NumLock */
129 SPECIAL, /* 0x46 - ScrollLock */
130 '7', '8', '9', '-',
131 '4', '5', '6', '+',
132 '1', '2', '3',
133 '0', '.',
134 SPECIAL, /* 0x54 - Alt-SysRq */
135 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
136 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
137 SPECIAL, /* 0x57 - F11 */
138 SPECIAL, /* 0x58 - F12 */
139 SPECIAL, /* 0x59 */
140 SPECIAL, /* 0x5a */
141 SPECIAL, /* 0x5b */
142 SPECIAL, /* 0x5c */
143 SPECIAL, /* 0x5d */
144 SPECIAL, /* 0x5e */
145 SPECIAL, /* 0x5f */
146 SPECIAL, /* 0x60 */
147 SPECIAL, /* 0x61 */
148 SPECIAL, /* 0x62 */
149 SPECIAL, /* 0x63 */
150 SPECIAL, /* 0x64 */
151 SPECIAL, /* 0x65 */
152 SPECIAL, /* 0x66 */
153 SPECIAL, /* 0x67 */
154 SPECIAL, /* 0x68 */
155 SPECIAL, /* 0x69 */
156 SPECIAL, /* 0x6a */
157 SPECIAL, /* 0x6b */
158 SPECIAL, /* 0x6c */
159 SPECIAL, /* 0x6d */
160 SPECIAL, /* 0x6e */
161 SPECIAL, /* 0x6f */
162 SPECIAL, /* 0x70 */
163 SPECIAL, /* 0x71 */
164 SPECIAL, /* 0x72 */
165 SPECIAL, /* 0x73 */
166 SPECIAL, /* 0x74 */
167 SPECIAL, /* 0x75 */
168 SPECIAL, /* 0x76 */
169 SPECIAL, /* 0x77 */
170 SPECIAL, /* 0x78 */
171 SPECIAL, /* 0x79 */
172 SPECIAL, /* 0x7a */
173 SPECIAL, /* 0x7b */
174 SPECIAL, /* 0x7c */
175 SPECIAL, /* 0x7d */
176 SPECIAL, /* 0x7e */
177 SPECIAL, /* 0x7f */
178};
179
180/** Secondary meaning of scancodes. */
181static char sc_secondary_map[] = {
182 SPECIAL, /* 0x00 */
183 SPECIAL, /* 0x01 - Esc */
184 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
185 SPECIAL, /* 0x0e - Backspace */
186 '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
187 SPECIAL, /* 0x1d - LCtrl */
188 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
189 '~',
190 SPECIAL, /* 0x2a - LShift */
191 '|',
192 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
193 SPECIAL, /* 0x36 - RShift */
194 '*',
195 SPECIAL, /* 0x38 - LAlt */
196 ' ',
197 SPECIAL, /* 0x3a - CapsLock */
198 SPECIAL, /* 0x3b - F1 */
199 SPECIAL, /* 0x3c - F2 */
200 SPECIAL, /* 0x3d - F3 */
201 SPECIAL, /* 0x3e - F4 */
202 SPECIAL, /* 0x3f - F5 */
203 SPECIAL, /* 0x40 - F6 */
204 SPECIAL, /* 0x41 - F7 */
205 SPECIAL, /* 0x42 - F8 */
206 SPECIAL, /* 0x43 - F9 */
207 SPECIAL, /* 0x44 - F10 */
208 SPECIAL, /* 0x45 - NumLock */
209 SPECIAL, /* 0x46 - ScrollLock */
210 '7', '8', '9', '-',
211 '4', '5', '6', '+',
212 '1', '2', '3',
213 '0', '.',
214 SPECIAL, /* 0x54 - Alt-SysRq */
215 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
216 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
217 SPECIAL, /* 0x57 - F11 */
218 SPECIAL, /* 0x58 - F12 */
219 SPECIAL, /* 0x59 */
220 SPECIAL, /* 0x5a */
221 SPECIAL, /* 0x5b */
222 SPECIAL, /* 0x5c */
223 SPECIAL, /* 0x5d */
224 SPECIAL, /* 0x5e */
225 SPECIAL, /* 0x5f */
226 SPECIAL, /* 0x60 */
227 SPECIAL, /* 0x61 */
228 SPECIAL, /* 0x62 */
229 SPECIAL, /* 0x63 */
230 SPECIAL, /* 0x64 */
231 SPECIAL, /* 0x65 */
232 SPECIAL, /* 0x66 */
233 SPECIAL, /* 0x67 */
234 SPECIAL, /* 0x68 */
235 SPECIAL, /* 0x69 */
236 SPECIAL, /* 0x6a */
237 SPECIAL, /* 0x6b */
238 SPECIAL, /* 0x6c */
239 SPECIAL, /* 0x6d */
240 SPECIAL, /* 0x6e */
241 SPECIAL, /* 0x6f */
242 SPECIAL, /* 0x70 */
243 SPECIAL, /* 0x71 */
244 SPECIAL, /* 0x72 */
245 SPECIAL, /* 0x73 */
246 SPECIAL, /* 0x74 */
247 SPECIAL, /* 0x75 */
248 SPECIAL, /* 0x76 */
249 SPECIAL, /* 0x77 */
250 SPECIAL, /* 0x78 */
251 SPECIAL, /* 0x79 */
252 SPECIAL, /* 0x7a */
253 SPECIAL, /* 0x7b */
254 SPECIAL, /* 0x7c */
255 SPECIAL, /* 0x7d */
256 SPECIAL, /* 0x7e */
257 SPECIAL, /* 0x7f */
258};
259
260static void i8042_interrupt(int n, void *stack);
261
262/** Initialize i8042. */
263void i8042_init(void)
264{
265 exc_register(VECTOR_KBD, "i8042_interrupt", i8042_interrupt);
266 while (inb(i8042_STATUS)&i8042_WAIT_MASK); /*Wait*/
267 outb(i8042_STATUS,i8042_SET_COMMAND);
268 while (inb(i8042_STATUS)&i8042_WAIT_MASK); /*Wait*/
269 outb(i8042_DATA,i8042_COMMAND);
270
271 trap_virtual_enable_irqs(1<<IRQ_KBD);
272 chardev_initialize("i8042_kbd", &kbrd, &ops);
273 stdin = &kbrd;
274}
275
276/** Process i8042 interrupt.
277 *
278 * @param n Interrupt vector.
279 * @param stack Interrupted stack.
280 */
281void i8042_interrupt(int n, void *stack)
282{
283 __u8 x;
284
285 trap_virtual_eoi();
286 x = inb(i8042_DATA);
287 if (x & KEY_RELEASE)
288 key_released(x ^ KEY_RELEASE);
289 else
290 key_pressed(x);
291}
292
293/** Process release of key.
294 *
295 * @param sc Scancode of the key being released.
296 */
297void key_released(__u8 sc)
298{
299 spinlock_lock(&keylock);
300 switch (sc) {
301 case SC_LSHIFT:
302 case SC_RSHIFT:
303 keyflags &= ~PRESSED_SHIFT;
304 break;
305 case SC_CAPSLOCK:
306 keyflags &= ~PRESSED_CAPSLOCK;
307 if (lockflags & LOCKED_CAPSLOCK)
308 lockflags &= ~LOCKED_CAPSLOCK;
309 else
310 lockflags |= LOCKED_CAPSLOCK;
311 break;
312 default:
313 break;
314 }
315 spinlock_unlock(&keylock);
316}
317
318/** Process keypress.
319 *
320 * @param sc Scancode of the key being pressed.
321 */
322void key_pressed(__u8 sc)
323{
324 char *map = sc_primary_map;
325 char ascii = sc_primary_map[sc];
326 bool shift, capslock;
327 bool letter = false;
328
329 spinlock_lock(&keylock);
330 switch (sc) {
331 case SC_LSHIFT:
332 case SC_RSHIFT:
333 keyflags |= PRESSED_SHIFT;
334 break;
335 case SC_CAPSLOCK:
336 keyflags |= PRESSED_CAPSLOCK;
337 break;
338 case SC_SPEC_ESCAPE:
339 break;
340 case SC_LEFTARR:
341 chardev_push_character(&kbrd, 0x1b);
342 chardev_push_character(&kbrd, 0x5b);
343 chardev_push_character(&kbrd, 0x44);
344 break;
345 case SC_RIGHTARR:
346 chardev_push_character(&kbrd, 0x1b);
347 chardev_push_character(&kbrd, 0x5b);
348 chardev_push_character(&kbrd, 0x43);
349 break;
350 case SC_UPARR:
351 chardev_push_character(&kbrd, 0x1b);
352 chardev_push_character(&kbrd, 0x5b);
353 chardev_push_character(&kbrd, 0x41);
354 break;
355 case SC_DOWNARR:
356 chardev_push_character(&kbrd, 0x1b);
357 chardev_push_character(&kbrd, 0x5b);
358 chardev_push_character(&kbrd, 0x42);
359 break;
360 case SC_HOME:
361 chardev_push_character(&kbrd, 0x1b);
362 chardev_push_character(&kbrd, 0x4f);
363 chardev_push_character(&kbrd, 0x48);
364 break;
365 case SC_END:
366 chardev_push_character(&kbrd, 0x1b);
367 chardev_push_character(&kbrd, 0x4f);
368 chardev_push_character(&kbrd, 0x46);
369 break;
370 case SC_DELETE:
371 chardev_push_character(&kbrd, 0x1b);
372 chardev_push_character(&kbrd, 0x5b);
373 chardev_push_character(&kbrd, 0x33);
374 chardev_push_character(&kbrd, 0x7e);
375 break;
376 default:
377 letter = is_lower(ascii);
378 capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
379 shift = keyflags & PRESSED_SHIFT;
380 if (letter && capslock)
381 shift = !shift;
382 if (shift)
383 map = sc_secondary_map;
384 chardev_push_character(&kbrd, map[sc]);
385 break;
386 }
387 spinlock_unlock(&keylock);
388}
389
390/* Called from getc(). */
391void i8042_resume(chardev_t *d)
392{
393}
394
395/* Called from getc(). */
396void i8042_suspend(chardev_t *d)
397{
398}
Note: See TracBrowser for help on using the repository browser.