|
25 | 25 | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
26 | 26 |
|
27 | 27 | static int kbd_exists; |
| 28 | +static int kbd_last_ret; |
28 | 29 |
|
29 | 30 | /* |
30 | 31 | * Check if the keyboard controller has a keypress for us. |
@@ -90,8 +91,11 @@ int kdb_get_kbd_char(void) |
90 | 91 | return -1; |
91 | 92 | } |
92 | 93 |
|
93 | | - if ((scancode & 0x80) != 0) |
| 94 | + if ((scancode & 0x80) != 0) { |
| 95 | + if (scancode == 0x9c) |
| 96 | + kbd_last_ret = 0; |
94 | 97 | return -1; |
| 98 | + } |
95 | 99 |
|
96 | 100 | scancode &= 0x7f; |
97 | 101 |
|
@@ -178,35 +182,82 @@ int kdb_get_kbd_char(void) |
178 | 182 | return -1; /* ignore unprintables */ |
179 | 183 | } |
180 | 184 |
|
181 | | - if ((scancode & 0x7f) == 0x1c) { |
182 | | - /* |
183 | | - * enter key. All done. Absorb the release scancode. |
184 | | - */ |
| 185 | + if (scancode == 0x1c) { |
| 186 | + kbd_last_ret = 1; |
| 187 | + return 13; |
| 188 | + } |
| 189 | + |
| 190 | + return keychar & 0xff; |
| 191 | +} |
| 192 | +EXPORT_SYMBOL_GPL(kdb_get_kbd_char); |
| 193 | + |
| 194 | +/* |
| 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 |
| 197 | + * code. |
| 198 | + */ |
| 199 | +void kdb_kbd_cleanup_state(void) |
| 200 | +{ |
| 201 | + int scancode, scanstatus; |
| 202 | + |
| 203 | + /* |
| 204 | + * Nothing to clean up, since either |
| 205 | + * ENTER was never pressed, or has already |
| 206 | + * gotten cleaned up. |
| 207 | + */ |
| 208 | + if (!kbd_last_ret) |
| 209 | + return; |
| 210 | + |
| 211 | + kbd_last_ret = 0; |
| 212 | + /* |
| 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'. |
| 215 | + * |
| 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. |
| 224 | + */ |
| 225 | + |
| 226 | + while (1) { |
185 | 227 | while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0) |
186 | | - ; |
| 228 | + cpu_relax(); |
187 | 229 |
|
188 | 230 | /* |
189 | | - * Fetch the scancode |
| 231 | + * Fetch the scancode. |
190 | 232 | */ |
191 | 233 | scancode = inb(KBD_DATA_REG); |
192 | 234 | scanstatus = inb(KBD_STATUS_REG); |
193 | 235 |
|
194 | | - while (scanstatus & KBD_STAT_MOUSE_OBF) { |
195 | | - scancode = inb(KBD_DATA_REG); |
196 | | - scanstatus = inb(KBD_STATUS_REG); |
197 | | - } |
| 236 | + /* |
| 237 | + * Skip mouse input. |
| 238 | + */ |
| 239 | + if (scanstatus & KBD_STAT_MOUSE_OBF) |
| 240 | + continue; |
198 | 241 |
|
199 | | - if (scancode != 0x9c) { |
200 | | - /* |
201 | | - * Wasn't an enter-release, why not? |
202 | | - */ |
203 | | - kdb_printf("kdb: expected enter got 0x%x status 0x%x\n", |
204 | | - scancode, scanstatus); |
205 | | - } |
| 242 | + /* |
| 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. |
| 247 | + * |
| 248 | + * If we see 0x1c, this must be a repeat ENTER or KP |
| 249 | + * ENTER (and we swallowed 0xe0 before). Try again. |
| 250 | + * |
| 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. |
| 254 | + * |
| 255 | + * Note, if you held some key as ENTER was depressed, |
| 256 | + * that break code would get leaked out. |
| 257 | + */ |
| 258 | + if (scancode != 0x9c) |
| 259 | + continue; |
206 | 260 |
|
207 | | - return 13; |
| 261 | + return; |
208 | 262 | } |
209 | | - |
210 | | - return keychar & 0xff; |
211 | 263 | } |
212 | | -EXPORT_SYMBOL_GPL(kdb_get_kbd_char); |
|
0 commit comments