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>
14 #include <linux/module.h>
17 /* Keyboard Controller Registers on normal PCs. */
19 #define KBD_STATUS_REG 0x64 /* Status register (R) */
20 #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
22 /* Status Register Bits */
24 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
25 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
27 static int kbd_exists
;
28 static int kbd_last_ret
;
31 * Check if the keyboard controller has a keypress for us.
32 * Some parts (Enter Release, LED change) are still blocking polled here,
33 * but hopefully they are all short.
35 int kdb_get_kbd_char(void)
37 int scancode
, scanstatus
;
38 static int shift_lock
; /* CAPS LOCK state (0-off, 1-on) */
39 static int shift_key
; /* Shift next keypress */
43 if (KDB_FLAG(NO_I8042
) || KDB_FLAG(NO_VT_CONSOLE
) ||
44 (inb(KBD_STATUS_REG
) == 0xff && inb(KBD_DATA_REG
) == 0xff)) {
50 if ((inb(KBD_STATUS_REG
) & KBD_STAT_OBF
) == 0)
56 scancode
= inb(KBD_DATA_REG
);
57 scanstatus
= inb(KBD_STATUS_REG
);
60 * Ignore mouse events.
62 if (scanstatus
& KBD_STAT_MOUSE_OBF
)
66 * Ignore release, trigger on make
67 * (except for shift keys, where we want to
68 * keep the shift state so long as the key is
72 if (((scancode
&0x7f) == 0x2a) || ((scancode
&0x7f) == 0x36)) {
74 * Next key may use shift table
76 if ((scancode
& 0x80) == 0)
83 if ((scancode
&0x7f) == 0x1d) {
87 if ((scancode
& 0x80) == 0)
94 if ((scancode
& 0x80) != 0) {
106 if (scancode
== 0x3a) {
118 if (scancode
== 0x0e) {
131 case 0x47: /* Home */
135 case 0x4B: /* Left */
139 case 0x50: /* Down */
141 case 0x4D: /* Right */
145 if (scancode
== 0xe0)
149 * For Japanese 86/106 keyboards
150 * See comment in drivers/char/pc_keyb.c.
153 if (scancode
== 0x73)
155 else if (scancode
== 0x7d)
158 if (!shift_lock
&& !shift_key
&& !ctrl_key
) {
159 keychar
= plain_map
[scancode
];
160 } else if ((shift_lock
|| shift_key
) && key_maps
[1]) {
161 keychar
= key_maps
[1][scancode
];
162 } else if (ctrl_key
&& key_maps
[4]) {
163 keychar
= key_maps
[4][scancode
];
166 kdb_printf("Unknown state/scancode (%d)\n", scancode
);
171 switch (KTYP(keychar
)) {
174 if (isprint(keychar
))
175 break; /* printable characters */
178 if (keychar
== K_ENTER
)
182 return -1; /* ignore unprintables */
185 if (scancode
== 0x1c) {
190 return keychar
& 0xff;
192 EXPORT_SYMBOL_GPL(kdb_get_kbd_char
);
195 * Best effort cleanup of ENTER break codes on leaving KDB. Called on
196 * exiting KDB, when we know we processed an ENTER or KP ENTER scan
199 void kdb_kbd_cleanup_state(void)
201 int scancode
, scanstatus
;
204 * Nothing to clean up, since either
205 * ENTER was never pressed, or has already
213 * Enter key. Need to absorb the break code here, lest it gets
214 * leaked out if we exit KDB as the result of processing 'g'.
216 * This has several interesting implications:
217 * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
218 * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
219 * only get a break code at the end of the repeated
220 * sequence. This means we can't propagate the repeated key
221 * press, and must swallow it away.
222 * + Need to handle possible PS/2 mouse input.
223 * + Need to handle mashed keys.
227 while ((inb(KBD_STATUS_REG
) & KBD_STAT_OBF
) == 0)
231 * Fetch the scancode.
233 scancode
= inb(KBD_DATA_REG
);
234 scanstatus
= inb(KBD_STATUS_REG
);
239 if (scanstatus
& KBD_STAT_MOUSE_OBF
)
243 * If we see 0xe0, this is either a break code for KP
244 * ENTER, or a repeat make for KP ENTER. Either way,
245 * since the second byte is equivalent to an ENTER,
246 * skip the 0xe0 and try again.
248 * If we see 0x1c, this must be a repeat ENTER or KP
249 * ENTER (and we swallowed 0xe0 before). Try again.
251 * We can also see make and break codes for other keys
252 * mashed before or after pressing ENTER. Thus, if we
253 * see anything other than 0x9c, we have to try again.
255 * Note, if you held some key as ENTER was depressed,
256 * that break code would get leaked out.
258 if (scancode
!= 0x9c)