xref: /linux/kernel/debug/kdb/kdb_keyboard.c (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kernel Debugger Architecture Dependent Console I/O handler
4  *
5  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
6  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
7  */
8 
9 #include <linux/kdb.h>
10 #include <linux/keyboard.h>
11 #include <linux/ctype.h>
12 #include <linux/io.h>
13 
14 #include "kdb_private.h"
15 
16 /* Keyboard Controller Registers on normal PCs. */
17 
18 #define KBD_STATUS_REG		0x64	/* Status register (R) */
19 #define KBD_DATA_REG		0x60	/* Keyboard data register (R/W) */
20 
21 /* Status Register Bits */
22 
23 #define KBD_STAT_OBF 		0x01	/* Keyboard output buffer full */
24 #define KBD_STAT_MOUSE_OBF	0x20	/* Mouse output buffer full */
25 
26 #define CTRL(c) ((c) - 64)
27 
28 static int kbd_exists;
29 static int kbd_last_ret;
30 
31 /*
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.
35  */
36 int kdb_get_kbd_char(void)
37 {
38 	int scancode, scanstatus;
39 	static int shift_lock;	/* CAPS LOCK state (0-off, 1-on) */
40 	static int shift_key;	/* Shift next keypress */
41 	static int ctrl_key;
42 	u_short keychar;
43 
44 	if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
45 	    (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
46 		kbd_exists = 0;
47 		return -1;
48 	}
49 	kbd_exists = 1;
50 
51 	if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
52 		return -1;
53 
54 	/*
55 	 * Fetch the scancode
56 	 */
57 	scancode = inb(KBD_DATA_REG);
58 	scanstatus = inb(KBD_STATUS_REG);
59 
60 	/*
61 	 * Ignore mouse events.
62 	 */
63 	if (scanstatus & KBD_STAT_MOUSE_OBF)
64 		return -1;
65 
66 	/*
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
70 	 *  held down).
71 	 */
72 
73 	if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
74 		/*
75 		 * Next key may use shift table
76 		 */
77 		if ((scancode & 0x80) == 0)
78 			shift_key = 1;
79 		else
80 			shift_key = 0;
81 		return -1;
82 	}
83 
84 	if ((scancode&0x7f) == 0x1d) {
85 		/*
86 		 * Left ctrl key
87 		 */
88 		if ((scancode & 0x80) == 0)
89 			ctrl_key = 1;
90 		else
91 			ctrl_key = 0;
92 		return -1;
93 	}
94 
95 	if ((scancode & 0x80) != 0) {
96 		if (scancode == 0x9c)
97 			kbd_last_ret = 0;
98 		return -1;
99 	}
100 
101 	scancode &= 0x7f;
102 
103 	/*
104 	 * Translate scancode
105 	 */
106 
107 	if (scancode == 0x3a) {
108 		/*
109 		 * Toggle caps lock
110 		 */
111 		shift_lock ^= 1;
112 
113 #ifdef	KDB_BLINK_LED
114 		kdb_toggleled(0x4);
115 #endif
116 		return -1;
117 	}
118 
119 	if (scancode == 0x0e) {
120 		/*
121 		 * Backspace
122 		 */
123 		return 8;
124 	}
125 
126 	/* Translate special keys to equivalent CTRL control characters */
127 	switch (scancode) {
128 	case 0xF: /* Tab */
129 		return CTRL('I');
130 	case 0x53: /* Del */
131 		return CTRL('D');
132 	case 0x47: /* Home */
133 		return CTRL('A');
134 	case 0x4F: /* End */
135 		return CTRL('E');
136 	case 0x4B: /* Left */
137 		return CTRL('B');
138 	case 0x48: /* Up */
139 		return CTRL('P');
140 	case 0x50: /* Down */
141 		return CTRL('N');
142 	case 0x4D: /* Right */
143 		return CTRL('F');
144 	}
145 
146 	/*
147 	 * For Japanese 86/106 keyboards
148 	 * 	See comment in drivers/char/pc_keyb.c.
149 	 * 	- Masahiro Adegawa
150 	 */
151 	if (scancode == 0x73)
152 		scancode = 0x59;
153 	else if (scancode == 0x7d)
154 		scancode = 0x7c;
155 
156 	if (!shift_lock && !shift_key && !ctrl_key) {
157 		keychar = plain_map[scancode];
158 	} else if ((shift_lock || shift_key) && key_maps[1]) {
159 		keychar = key_maps[1][scancode];
160 	} else if (ctrl_key && key_maps[4]) {
161 		keychar = key_maps[4][scancode];
162 	} else {
163 		keychar = 0x0020;
164 		kdb_printf("Unknown state/scancode (%d)\n", scancode);
165 	}
166 	keychar &= 0x0fff;
167 	if (keychar == '\t')
168 		keychar = ' ';
169 	switch (KTYP(keychar)) {
170 	case KT_LETTER:
171 	case KT_LATIN:
172 		switch (keychar) {
173 		/* non-printable supported control characters */
174 		case CTRL('A'): /* Home */
175 		case CTRL('B'): /* Left */
176 		case CTRL('D'): /* Del */
177 		case CTRL('E'): /* End */
178 		case CTRL('F'): /* Right */
179 		case CTRL('I'): /* Tab */
180 		case CTRL('N'): /* Down */
181 		case CTRL('P'): /* Up */
182 			return keychar;
183 		}
184 
185 		if (isprint(keychar))
186 			break;		/* printable characters */
187 		fallthrough;
188 	case KT_SPEC:
189 		if (keychar == K_ENTER)
190 			break;
191 		fallthrough;
192 	default:
193 		return -1;	/* ignore unprintables */
194 	}
195 
196 	if (scancode == 0x1c) {
197 		kbd_last_ret = 1;
198 		return 13;
199 	}
200 
201 	return keychar & 0xff;
202 }
203 EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
204 
205 /*
206  * Best effort cleanup of ENTER break codes on leaving KDB. Called on
207  * exiting KDB, when we know we processed an ENTER or KP ENTER scan
208  * code.
209  */
210 void kdb_kbd_cleanup_state(void)
211 {
212 	int scancode, scanstatus;
213 
214 	/*
215 	 * Nothing to clean up, since either
216 	 * ENTER was never pressed, or has already
217 	 * gotten cleaned up.
218 	 */
219 	if (!kbd_last_ret)
220 		return;
221 
222 	kbd_last_ret = 0;
223 	/*
224 	 * Enter key. Need to absorb the break code here, lest it gets
225 	 * leaked out if we exit KDB as the result of processing 'g'.
226 	 *
227 	 * This has several interesting implications:
228 	 * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
229 	 * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
230 	 *   only get a break code at the end of the repeated
231 	 *   sequence. This means we can't propagate the repeated key
232 	 *   press, and must swallow it away.
233 	 * + Need to handle possible PS/2 mouse input.
234 	 * + Need to handle mashed keys.
235 	 */
236 
237 	while (1) {
238 		while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
239 			cpu_relax();
240 
241 		/*
242 		 * Fetch the scancode.
243 		 */
244 		scancode = inb(KBD_DATA_REG);
245 		scanstatus = inb(KBD_STATUS_REG);
246 
247 		/*
248 		 * Skip mouse input.
249 		 */
250 		if (scanstatus & KBD_STAT_MOUSE_OBF)
251 			continue;
252 
253 		/*
254 		 * If we see 0xe0, this is either a break code for KP
255 		 * ENTER, or a repeat make for KP ENTER. Either way,
256 		 * since the second byte is equivalent to an ENTER,
257 		 * skip the 0xe0 and try again.
258 		 *
259 		 * If we see 0x1c, this must be a repeat ENTER or KP
260 		 * ENTER (and we swallowed 0xe0 before). Try again.
261 		 *
262 		 * We can also see make and break codes for other keys
263 		 * mashed before or after pressing ENTER. Thus, if we
264 		 * see anything other than 0x9c, we have to try again.
265 		 *
266 		 * Note, if you held some key as ENTER was depressed,
267 		 * that break code would get leaked out.
268 		 */
269 		if (scancode != 0x9c)
270 			continue;
271 
272 		return;
273 	}
274 }
275