source: mainline/genarch/src/i8042/i8042.c@ cc73a8a1

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

Cleanup of comments.

  • Property mode set to 100644
File size: 13.8 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/** @addtogroup genarch
30 * @{
31 */
32/**
33 * @file
34 * @brief i8042 processor driver.
35 *
36 * It takes care of low-level keyboard functions.
37 */
38
39#include <genarch/i8042/i8042.h>
40#include <arch/drivers/i8042.h>
41#include <arch/interrupt.h>
42#include <cpu.h>
43#include <arch/asm.h>
44#include <arch.h>
45#include <synch/spinlock.h>
46#include <typedefs.h>
47#include <console/chardev.h>
48#include <console/console.h>
49#include <macros.h>
50#include <interrupt.h>
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
75#define i8042_BUFFER_FULL_MASK 0x01
76#define i8042_WAIT_MASK 0x02
77
78#define SPECIAL '?'
79#define KEY_RELEASE 0x80
80
81/**
82 * These codes read from i8042 data register are silently ignored.
83 */
84#define IGNORE_CODE 0x7f
85
86static void key_released(__u8 sc);
87static void key_pressed(__u8 sc);
88static char key_read(chardev_t *d);
89
90#define PRESSED_SHIFT (1<<0)
91#define PRESSED_CAPSLOCK (1<<1)
92#define LOCKED_CAPSLOCK (1<<0)
93
94#define ACTIVE_READ_BUFF_SIZE 16 /* Must be power of 2 */
95
96static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE];
97
98SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */
99static volatile int keyflags; /**< Tracking of multiple keypresses. */
100static volatile int lockflags; /**< Tracking of multiple keys lockings. */
101
102static void i8042_suspend(chardev_t *);
103static void i8042_resume(chardev_t *);
104
105static chardev_t kbrd;
106static chardev_operations_t ops = {
107 .suspend = i8042_suspend,
108 .resume = i8042_resume,
109 .read = key_read
110};
111
112/** Primary meaning of scancodes. */
113static char sc_primary_map[] = {
114 SPECIAL, /* 0x00 */
115 SPECIAL, /* 0x01 - Esc */
116 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
117 '\b', /* 0x0e - Backspace */
118 '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
119 SPECIAL, /* 0x1d - LCtrl */
120 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
121 '`',
122 SPECIAL, /* 0x2a - LShift */
123 '\\',
124 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
125 SPECIAL, /* 0x36 - RShift */
126 '*',
127 SPECIAL, /* 0x38 - LAlt */
128 ' ',
129 SPECIAL, /* 0x3a - CapsLock */
130 SPECIAL, /* 0x3b - F1 */
131 SPECIAL, /* 0x3c - F2 */
132 SPECIAL, /* 0x3d - F3 */
133 SPECIAL, /* 0x3e - F4 */
134 SPECIAL, /* 0x3f - F5 */
135 SPECIAL, /* 0x40 - F6 */
136 SPECIAL, /* 0x41 - F7 */
137 SPECIAL, /* 0x42 - F8 */
138 SPECIAL, /* 0x43 - F9 */
139 SPECIAL, /* 0x44 - F10 */
140 SPECIAL, /* 0x45 - NumLock */
141 SPECIAL, /* 0x46 - ScrollLock */
142 '7', '8', '9', '-',
143 '4', '5', '6', '+',
144 '1', '2', '3',
145 '0', '.',
146 SPECIAL, /* 0x54 - Alt-SysRq */
147 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
148 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
149 SPECIAL, /* 0x57 - F11 */
150 SPECIAL, /* 0x58 - F12 */
151 SPECIAL, /* 0x59 */
152 SPECIAL, /* 0x5a */
153 SPECIAL, /* 0x5b */
154 SPECIAL, /* 0x5c */
155 SPECIAL, /* 0x5d */
156 SPECIAL, /* 0x5e */
157 SPECIAL, /* 0x5f */
158 SPECIAL, /* 0x60 */
159 SPECIAL, /* 0x61 */
160 SPECIAL, /* 0x62 */
161 SPECIAL, /* 0x63 */
162 SPECIAL, /* 0x64 */
163 SPECIAL, /* 0x65 */
164 SPECIAL, /* 0x66 */
165 SPECIAL, /* 0x67 */
166 SPECIAL, /* 0x68 */
167 SPECIAL, /* 0x69 */
168 SPECIAL, /* 0x6a */
169 SPECIAL, /* 0x6b */
170 SPECIAL, /* 0x6c */
171 SPECIAL, /* 0x6d */
172 SPECIAL, /* 0x6e */
173 SPECIAL, /* 0x6f */
174 SPECIAL, /* 0x70 */
175 SPECIAL, /* 0x71 */
176 SPECIAL, /* 0x72 */
177 SPECIAL, /* 0x73 */
178 SPECIAL, /* 0x74 */
179 SPECIAL, /* 0x75 */
180 SPECIAL, /* 0x76 */
181 SPECIAL, /* 0x77 */
182 SPECIAL, /* 0x78 */
183 SPECIAL, /* 0x79 */
184 SPECIAL, /* 0x7a */
185 SPECIAL, /* 0x7b */
186 SPECIAL, /* 0x7c */
187 SPECIAL, /* 0x7d */
188 SPECIAL, /* 0x7e */
189 SPECIAL, /* 0x7f */
190};
191
192/** Secondary meaning of scancodes. */
193static char sc_secondary_map[] = {
194 SPECIAL, /* 0x00 */
195 SPECIAL, /* 0x01 - Esc */
196 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
197 SPECIAL, /* 0x0e - Backspace */
198 '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
199 SPECIAL, /* 0x1d - LCtrl */
200 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
201 '~',
202 SPECIAL, /* 0x2a - LShift */
203 '|',
204 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
205 SPECIAL, /* 0x36 - RShift */
206 '*',
207 SPECIAL, /* 0x38 - LAlt */
208 ' ',
209 SPECIAL, /* 0x3a - CapsLock */
210 SPECIAL, /* 0x3b - F1 */
211 SPECIAL, /* 0x3c - F2 */
212 SPECIAL, /* 0x3d - F3 */
213 SPECIAL, /* 0x3e - F4 */
214 SPECIAL, /* 0x3f - F5 */
215 SPECIAL, /* 0x40 - F6 */
216 SPECIAL, /* 0x41 - F7 */
217 SPECIAL, /* 0x42 - F8 */
218 SPECIAL, /* 0x43 - F9 */
219 SPECIAL, /* 0x44 - F10 */
220 SPECIAL, /* 0x45 - NumLock */
221 SPECIAL, /* 0x46 - ScrollLock */
222 '7', '8', '9', '-',
223 '4', '5', '6', '+',
224 '1', '2', '3',
225 '0', '.',
226 SPECIAL, /* 0x54 - Alt-SysRq */
227 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
228 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
229 SPECIAL, /* 0x57 - F11 */
230 SPECIAL, /* 0x58 - F12 */
231 SPECIAL, /* 0x59 */
232 SPECIAL, /* 0x5a */
233 SPECIAL, /* 0x5b */
234 SPECIAL, /* 0x5c */
235 SPECIAL, /* 0x5d */
236 SPECIAL, /* 0x5e */
237 SPECIAL, /* 0x5f */
238 SPECIAL, /* 0x60 */
239 SPECIAL, /* 0x61 */
240 SPECIAL, /* 0x62 */
241 SPECIAL, /* 0x63 */
242 SPECIAL, /* 0x64 */
243 SPECIAL, /* 0x65 */
244 SPECIAL, /* 0x66 */
245 SPECIAL, /* 0x67 */
246 SPECIAL, /* 0x68 */
247 SPECIAL, /* 0x69 */
248 SPECIAL, /* 0x6a */
249 SPECIAL, /* 0x6b */
250 SPECIAL, /* 0x6c */
251 SPECIAL, /* 0x6d */
252 SPECIAL, /* 0x6e */
253 SPECIAL, /* 0x6f */
254 SPECIAL, /* 0x70 */
255 SPECIAL, /* 0x71 */
256 SPECIAL, /* 0x72 */
257 SPECIAL, /* 0x73 */
258 SPECIAL, /* 0x74 */
259 SPECIAL, /* 0x75 */
260 SPECIAL, /* 0x76 */
261 SPECIAL, /* 0x77 */
262 SPECIAL, /* 0x78 */
263 SPECIAL, /* 0x79 */
264 SPECIAL, /* 0x7a */
265 SPECIAL, /* 0x7b */
266 SPECIAL, /* 0x7c */
267 SPECIAL, /* 0x7d */
268 SPECIAL, /* 0x7e */
269 SPECIAL, /* 0x7f */
270};
271
272static void i8042_interrupt(int n, istate_t *istate);
273static void i8042_wait(void);
274
275static iroutine oldvector;
276/** Initialize keyboard and service interrupts using kernel routine */
277void i8042_grab(void)
278{
279 oldvector = exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
280 i8042_wait();
281 i8042_command_write(i8042_SET_COMMAND);
282 i8042_wait();
283 i8042_data_write(i8042_COMMAND);
284 i8042_wait();
285}
286/** Resume the former interrupt vector */
287void i8042_release(void)
288{
289 if (oldvector)
290 exc_register(VECTOR_KBD, "user_interrupt", oldvector);
291}
292
293/** Initialize i8042. */
294void i8042_init(void)
295{
296 int i;
297
298 i8042_grab();
299 /* Prevent user from accidentaly releasing calling i8042_resume
300 * and disabling keyboard
301 */
302 oldvector = NULL;
303
304 trap_virtual_enable_irqs(1<<IRQ_KBD);
305 chardev_initialize("i8042_kbd", &kbrd, &ops);
306 stdin = &kbrd;
307
308 /*
309 * Clear input buffer.
310 * Number of iterations is limited to prevent infinite looping.
311 */
312 for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
313 i8042_data_read();
314 }
315}
316
317/** Process i8042 interrupt.
318 *
319 * @param n Interrupt vector.
320 * @param istate Interrupted state.
321 */
322void i8042_interrupt(int n, istate_t *istate)
323{
324 __u8 x;
325
326 while ((i8042_status_read() & i8042_BUFFER_FULL_MASK)) {
327 x = i8042_data_read();
328 if (x & KEY_RELEASE)
329 key_released(x ^ KEY_RELEASE);
330 else
331 key_pressed(x);
332 }
333 trap_virtual_eoi();
334}
335
336/** Wait until the controller reads its data. */
337void i8042_wait(void) {
338 while (i8042_status_read() & i8042_WAIT_MASK) {
339 /* wait */
340 }
341}
342
343/** Process release of key.
344 *
345 * @param sc Scancode of the key being released.
346 */
347void key_released(__u8 sc)
348{
349 spinlock_lock(&keylock);
350 switch (sc) {
351 case SC_LSHIFT:
352 case SC_RSHIFT:
353 keyflags &= ~PRESSED_SHIFT;
354 break;
355 case SC_CAPSLOCK:
356 keyflags &= ~PRESSED_CAPSLOCK;
357 if (lockflags & LOCKED_CAPSLOCK)
358 lockflags &= ~LOCKED_CAPSLOCK;
359 else
360 lockflags |= LOCKED_CAPSLOCK;
361 break;
362 default:
363 break;
364 }
365 spinlock_unlock(&keylock);
366}
367
368/** Process keypress.
369 *
370 * @param sc Scancode of the key being pressed.
371 */
372void key_pressed(__u8 sc)
373{
374 char *map = sc_primary_map;
375 char ascii = sc_primary_map[sc];
376 bool shift, capslock;
377 bool letter = false;
378
379 spinlock_lock(&keylock);
380 switch (sc) {
381 case SC_LSHIFT:
382 case SC_RSHIFT:
383 keyflags |= PRESSED_SHIFT;
384 break;
385 case SC_CAPSLOCK:
386 keyflags |= PRESSED_CAPSLOCK;
387 break;
388 case SC_SPEC_ESCAPE:
389 break;
390 case SC_LEFTARR:
391 chardev_push_character(&kbrd, 0x1b);
392 chardev_push_character(&kbrd, 0x5b);
393 chardev_push_character(&kbrd, 0x44);
394 break;
395 case SC_RIGHTARR:
396 chardev_push_character(&kbrd, 0x1b);
397 chardev_push_character(&kbrd, 0x5b);
398 chardev_push_character(&kbrd, 0x43);
399 break;
400 case SC_UPARR:
401 chardev_push_character(&kbrd, 0x1b);
402 chardev_push_character(&kbrd, 0x5b);
403 chardev_push_character(&kbrd, 0x41);
404 break;
405 case SC_DOWNARR:
406 chardev_push_character(&kbrd, 0x1b);
407 chardev_push_character(&kbrd, 0x5b);
408 chardev_push_character(&kbrd, 0x42);
409 break;
410 case SC_HOME:
411 chardev_push_character(&kbrd, 0x1b);
412 chardev_push_character(&kbrd, 0x4f);
413 chardev_push_character(&kbrd, 0x48);
414 break;
415 case SC_END:
416 chardev_push_character(&kbrd, 0x1b);
417 chardev_push_character(&kbrd, 0x4f);
418 chardev_push_character(&kbrd, 0x46);
419 break;
420 case SC_DELETE:
421 chardev_push_character(&kbrd, 0x1b);
422 chardev_push_character(&kbrd, 0x5b);
423 chardev_push_character(&kbrd, 0x33);
424 chardev_push_character(&kbrd, 0x7e);
425 break;
426 default:
427 letter = is_lower(ascii);
428 capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
429 shift = keyflags & PRESSED_SHIFT;
430 if (letter && capslock)
431 shift = !shift;
432 if (shift)
433 map = sc_secondary_map;
434 chardev_push_character(&kbrd, map[sc]);
435 break;
436 }
437 spinlock_unlock(&keylock);
438}
439
440/* Called from getc(). */
441void i8042_resume(chardev_t *d)
442{
443}
444
445/* Called from getc(). */
446void i8042_suspend(chardev_t *d)
447{
448}
449
450static __u8 active_read_buff_read(void)
451{
452 static int i=0;
453 i &= (ACTIVE_READ_BUFF_SIZE-1);
454 if(!active_read_buff[i]) {
455 return 0;
456 }
457 return active_read_buff[i++];
458}
459
460static void active_read_buff_write(__u8 ch)
461{
462 static int i=0;
463 active_read_buff[i] = ch;
464 i++;
465 i &= (ACTIVE_READ_BUFF_SIZE-1);
466 active_read_buff[i]=0;
467}
468
469
470static void active_read_key_pressed(__u8 sc)
471{
472 char *map = sc_primary_map;
473 char ascii = sc_primary_map[sc];
474 bool shift, capslock;
475 bool letter = false;
476
477 /*spinlock_lock(&keylock);*/
478 switch (sc) {
479 case SC_LSHIFT:
480 case SC_RSHIFT:
481 keyflags |= PRESSED_SHIFT;
482 break;
483 case SC_CAPSLOCK:
484 keyflags |= PRESSED_CAPSLOCK;
485 break;
486 case SC_SPEC_ESCAPE:
487 break;
488 case SC_LEFTARR:
489 active_read_buff_write(0x1b);
490 active_read_buff_write(0x5b);
491 active_read_buff_write(0x44);
492 break;
493 case SC_RIGHTARR:
494 active_read_buff_write(0x1b);
495 active_read_buff_write(0x5b);
496 active_read_buff_write(0x43);
497 break;
498 case SC_UPARR:
499 active_read_buff_write(0x1b);
500 active_read_buff_write(0x5b);
501 active_read_buff_write(0x41);
502 break;
503 case SC_DOWNARR:
504 active_read_buff_write(0x1b);
505 active_read_buff_write(0x5b);
506 active_read_buff_write(0x42);
507 break;
508 case SC_HOME:
509 active_read_buff_write(0x1b);
510 active_read_buff_write(0x4f);
511 active_read_buff_write(0x48);
512 break;
513 case SC_END:
514 active_read_buff_write(0x1b);
515 active_read_buff_write(0x4f);
516 active_read_buff_write(0x46);
517 break;
518 case SC_DELETE:
519 active_read_buff_write(0x1b);
520 active_read_buff_write(0x5b);
521 active_read_buff_write(0x33);
522 active_read_buff_write(0x7e);
523 break;
524 default:
525 letter = is_lower(ascii);
526 capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
527 shift = keyflags & PRESSED_SHIFT;
528 if (letter && capslock)
529 shift = !shift;
530 if (shift)
531 map = sc_secondary_map;
532 active_read_buff_write(map[sc]);
533 break;
534 }
535 /*spinlock_unlock(&keylock);*/
536
537}
538
539static char key_read(chardev_t *d)
540{
541 char ch;
542
543 while(!(ch = active_read_buff_read())) {
544 __u8 x;
545 while (!(i8042_status_read() & i8042_BUFFER_FULL_MASK))
546 ;
547 x = i8042_data_read();
548 if (x != IGNORE_CODE) {
549 if (x & KEY_RELEASE)
550 key_released(x ^ KEY_RELEASE);
551 else
552 active_read_key_pressed(x);
553 }
554 }
555 return ch;
556}
557
558/** Poll for key press and release events.
559 *
560 * This function can be used to implement keyboard polling.
561 */
562void i8042_poll(void)
563{
564 __u8 x;
565
566 while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
567 x = i8042_data_read();
568 if (x != IGNORE_CODE) {
569 if (x & KEY_RELEASE)
570 key_released(x ^ KEY_RELEASE);
571 else
572 key_pressed(x);
573 }
574 }
575}
576
577/** @}
578 */
579
Note: See TracBrowser for help on using the repository browser.