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 #include "kdb_private.h"
18 /* Keyboard Controller Registers on normal PCs. */
20 #define KBD_STATUS_REG 0x64 /* Status register (R) */
21 #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
23 /* Status Register Bits */
25 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
26 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
28 static int kbd_exists;
29 static int kbd_last_ret;
32 * Check if the keyboard controller has a keypress for us.
33 * Some parts (Enter Release, LED change) are still blocking polled here,
34 * but hopefully they are all short.
36 int kdb_get_kbd_char(void)
38 int scancode, scanstatus;
39 static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */
40 static int shift_key; /* Shift next keypress */
44 if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
45 (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
51 if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
57 scancode = inb(KBD_DATA_REG);
58 scanstatus = inb(KBD_STATUS_REG);
61 * Ignore mouse events.
63 if (scanstatus & KBD_STAT_MOUSE_OBF)
67 * Ignore release, trigger on make
68 * (except for shift keys, where we want to
69 * keep the shift state so long as the key is
73 if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
75 * Next key may use shift table
77 if ((scancode & 0x80) == 0)
84 if ((scancode&0x7f) == 0x1d) {
88 if ((scancode & 0x80) == 0)
95 if ((scancode & 0x80) != 0) {
107 if (scancode == 0x3a) {
119 if (scancode == 0x0e) {
132 case 0x47: /* Home */
136 case 0x4B: /* Left */
140 case 0x50: /* Down */
142 case 0x4D: /* Right */
146 if (scancode == 0xe0)
150 * For Japanese 86/106 keyboards
151 * See comment in drivers/char/pc_keyb.c.
154 if (scancode == 0x73)
156 else if (scancode == 0x7d)
159 if (!shift_lock && !shift_key && !ctrl_key) {
160 keychar = plain_map[scancode];
161 } else if ((shift_lock || shift_key) && key_maps[1]) {
162 keychar = key_maps[1][scancode];
163 } else if (ctrl_key && key_maps[4]) {
164 keychar = key_maps[4][scancode];
167 kdb_printf("Unknown state/scancode (%d)\n", scancode);
172 switch (KTYP(keychar)) {
175 if (isprint(keychar))
176 break; /* printable characters */
179 if (keychar == K_ENTER)
183 return -1; /* ignore unprintables */
186 if (scancode == 0x1c) {
191 return keychar & 0xff;
193 EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
196 * Best effort cleanup of ENTER break codes on leaving KDB. Called on
197 * exiting KDB, when we know we processed an ENTER or KP ENTER scan
200 void kdb_kbd_cleanup_state(void)
202 int scancode, scanstatus;
205 * Nothing to clean up, since either
206 * ENTER was never pressed, or has already
214 * Enter key. Need to absorb the break code here, lest it gets
215 * leaked out if we exit KDB as the result of processing 'g'.
217 * This has several interesting implications:
218 * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
219 * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
220 * only get a break code at the end of the repeated
221 * sequence. This means we can't propagate the repeated key
222 * press, and must swallow it away.
223 * + Need to handle possible PS/2 mouse input.
224 * + Need to handle mashed keys.
228 while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
232 * Fetch the scancode.
234 scancode = inb(KBD_DATA_REG);
235 scanstatus = inb(KBD_STATUS_REG);
240 if (scanstatus & KBD_STAT_MOUSE_OBF)
244 * If we see 0xe0, this is either a break code for KP
245 * ENTER, or a repeat make for KP ENTER. Either way,
246 * since the second byte is equivalent to an ENTER,
247 * skip the 0xe0 and try again.
249 * If we see 0x1c, this must be a repeat ENTER or KP
250 * ENTER (and we swallowed 0xe0 before). Try again.
252 * We can also see make and break codes for other keys
253 * mashed before or after pressing ENTER. Thus, if we
254 * see anything other than 0x9c, we have to try again.
256 * Note, if you held some key as ENTER was depressed,
257 * that break code would get leaked out.
259 if (scancode != 0x9c)