2 * Kernel Debugger Architecture Dependent Console I/O handler
4 * This file is subject to the terms and conditions of the GNU General Public
7 * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved.
8 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
11 #include <linux/kdb.h>
12 #include <linux/keyboard.h>
13 #include <linux/ctype.h>
16 /* Keyboard Controller Registers on normal PCs. */
18 #define KBD_STATUS_REG 0x64 /* Status register (R) */
19 #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
21 /* Status Register Bits */
23 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
24 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
26 static int kbd_exists;
27 static int kbd_last_ret;
30 * Check if the keyboard controller has a keypress for us.
31 * Some parts (Enter Release, LED change) are still blocking polled here,
32 * but hopefully they are all short.
34 int kdb_get_kbd_char(void)
36 int scancode, scanstatus;
37 static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */
38 static int shift_key; /* Shift next keypress */
42 if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
43 (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
49 if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
55 scancode = inb(KBD_DATA_REG);
56 scanstatus = inb(KBD_STATUS_REG);
59 * Ignore mouse events.
61 if (scanstatus & KBD_STAT_MOUSE_OBF)
65 * Ignore release, trigger on make
66 * (except for shift keys, where we want to
67 * keep the shift state so long as the key is
71 if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
73 * Next key may use shift table
75 if ((scancode & 0x80) == 0)
82 if ((scancode&0x7f) == 0x1d) {
86 if ((scancode & 0x80) == 0)
93 if ((scancode & 0x80) != 0) {
105 if (scancode == 0x3a) {
117 if (scancode == 0x0e) {
130 case 0x47: /* Home */
134 case 0x4B: /* Left */
138 case 0x50: /* Down */
140 case 0x4D: /* Right */
144 if (scancode == 0xe0)
148 * For Japanese 86/106 keyboards
149 * See comment in drivers/char/pc_keyb.c.
152 if (scancode == 0x73)
154 else if (scancode == 0x7d)
157 if (!shift_lock && !shift_key && !ctrl_key) {
158 keychar = plain_map[scancode];
159 } else if ((shift_lock || shift_key) && key_maps[1]) {
160 keychar = key_maps[1][scancode];
161 } else if (ctrl_key && key_maps[4]) {
162 keychar = key_maps[4][scancode];
165 kdb_printf("Unknown state/scancode (%d)\n", scancode);
170 switch (KTYP(keychar)) {
173 if (isprint(keychar))
174 break; /* printable characters */
177 if (keychar == K_ENTER)
181 return -1; /* ignore unprintables */
184 if (scancode == 0x1c) {
189 return keychar & 0xff;
191 EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
194 * Best effort cleanup of ENTER break codes on leaving KDB. Called on
195 * exiting KDB, when we know we processed an ENTER or KP ENTER scan
198 void kdb_kbd_cleanup_state(void)
200 int scancode, scanstatus;
203 * Nothing to clean up, since either
204 * ENTER was never pressed, or has already
212 * Enter key. Need to absorb the break code here, lest it gets
213 * leaked out if we exit KDB as the result of processing 'g'.
215 * This has several interesting implications:
216 * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
217 * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
218 * only get a break code at the end of the repeated
219 * sequence. This means we can't propagate the repeated key
220 * press, and must swallow it away.
221 * + Need to handle possible PS/2 mouse input.
222 * + Need to handle mashed keys.
226 while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
230 * Fetch the scancode.
232 scancode = inb(KBD_DATA_REG);
233 scanstatus = inb(KBD_STATUS_REG);
238 if (scanstatus & KBD_STAT_MOUSE_OBF)
242 * If we see 0xe0, this is either a break code for KP
243 * ENTER, or a repeat make for KP ENTER. Either way,
244 * since the second byte is equivalent to an ENTER,
245 * skip the 0xe0 and try again.
247 * If we see 0x1c, this must be a repeat ENTER or KP
248 * ENTER (and we swallowed 0xe0 before). Try again.
250 * We can also see make and break codes for other keys
251 * mashed before or after pressing ENTER. Thus, if we
252 * see anything other than 0x9c, we have to try again.
254 * Note, if you held some key as ENTER was depressed,
255 * that break code would get leaked out.
257 if (scancode != 0x9c)