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;
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)
102 if (scancode == 0x3a) {
114 if (scancode == 0x0e) {
127 case 0x47: /* Home */
131 case 0x4B: /* Left */
135 case 0x50: /* Down */
137 case 0x4D: /* Right */
141 if (scancode == 0xe0)
145 * For Japanese 86/106 keyboards
146 * See comment in drivers/char/pc_keyb.c.
149 if (scancode == 0x73)
151 else if (scancode == 0x7d)
154 if (!shift_lock && !shift_key && !ctrl_key) {
155 keychar = plain_map[scancode];
156 } else if ((shift_lock || shift_key) && key_maps[1]) {
157 keychar = key_maps[1][scancode];
158 } else if (ctrl_key && key_maps[4]) {
159 keychar = key_maps[4][scancode];
162 kdb_printf("Unknown state/scancode (%d)\n", scancode);
167 switch (KTYP(keychar)) {
170 if (isprint(keychar))
171 break; /* printable characters */
174 if (keychar == K_ENTER)
178 return -1; /* ignore unprintables */
181 if ((scancode & 0x7f) == 0x1c) {
183 * enter key. All done. Absorb the release scancode.
185 while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
191 scancode = inb(KBD_DATA_REG);
192 scanstatus = inb(KBD_STATUS_REG);
194 while (scanstatus & KBD_STAT_MOUSE_OBF) {
195 scancode = inb(KBD_DATA_REG);
196 scanstatus = inb(KBD_STATUS_REG);
199 if (scancode != 0x9c) {
201 * Wasn't an enter-release, why not?
203 kdb_printf("kdb: expected enter got 0x%x status 0x%x\n",
204 scancode, scanstatus);
210 return keychar & 0xff;
212 EXPORT_SYMBOL_GPL(kdb_get_kbd_char);