Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / term / at_keyboard.c
blobc234e92f24606422f9c94dd897b5dae1b348e324
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/dl.h>
20 #include <grub/at_keyboard.h>
21 #include <grub/cpu/at_keyboard.h>
22 #include <grub/cpu/io.h>
23 #include <grub/misc.h>
24 #include <grub/term.h>
25 #include <grub/keyboard_layouts.h>
26 #include <grub/time.h>
27 #include <grub/loader.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 static short at_keyboard_status = 0;
32 static int e0_received = 0;
33 static int f0_received = 0;
35 static grub_uint8_t led_status;
37 #define KEYBOARD_LED_SCROLL (1 << 0)
38 #define KEYBOARD_LED_NUM (1 << 1)
39 #define KEYBOARD_LED_CAPS (1 << 2)
41 static grub_uint8_t grub_keyboard_controller_orig;
42 static grub_uint8_t grub_keyboard_orig_set;
43 static grub_uint8_t current_set;
45 static void
46 grub_keyboard_controller_init (void);
48 static const grub_uint8_t set1_mapping[128] =
50 /* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE,
51 /* 0x02 */ GRUB_KEYBOARD_KEY_1, GRUB_KEYBOARD_KEY_2,
52 /* 0x04 */ GRUB_KEYBOARD_KEY_3, GRUB_KEYBOARD_KEY_4,
53 /* 0x06 */ GRUB_KEYBOARD_KEY_5, GRUB_KEYBOARD_KEY_6,
54 /* 0x08 */ GRUB_KEYBOARD_KEY_7, GRUB_KEYBOARD_KEY_8,
55 /* 0x0a */ GRUB_KEYBOARD_KEY_9, GRUB_KEYBOARD_KEY_0,
56 /* 0x0c */ GRUB_KEYBOARD_KEY_DASH, GRUB_KEYBOARD_KEY_EQUAL,
57 /* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE, GRUB_KEYBOARD_KEY_TAB,
58 /* 0x10 */ GRUB_KEYBOARD_KEY_Q, GRUB_KEYBOARD_KEY_W,
59 /* 0x12 */ GRUB_KEYBOARD_KEY_E, GRUB_KEYBOARD_KEY_R,
60 /* 0x14 */ GRUB_KEYBOARD_KEY_T, GRUB_KEYBOARD_KEY_Y,
61 /* 0x16 */ GRUB_KEYBOARD_KEY_U, GRUB_KEYBOARD_KEY_I,
62 /* 0x18 */ GRUB_KEYBOARD_KEY_O, GRUB_KEYBOARD_KEY_P,
63 /* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET, GRUB_KEYBOARD_KEY_RBRACKET,
64 /* 0x1c */ GRUB_KEYBOARD_KEY_ENTER, GRUB_KEYBOARD_KEY_LEFT_CTRL,
65 /* 0x1e */ GRUB_KEYBOARD_KEY_A, GRUB_KEYBOARD_KEY_S,
66 /* 0x20 */ GRUB_KEYBOARD_KEY_D, GRUB_KEYBOARD_KEY_F,
67 /* 0x22 */ GRUB_KEYBOARD_KEY_G, GRUB_KEYBOARD_KEY_H,
68 /* 0x24 */ GRUB_KEYBOARD_KEY_J, GRUB_KEYBOARD_KEY_K,
69 /* 0x26 */ GRUB_KEYBOARD_KEY_L, GRUB_KEYBOARD_KEY_SEMICOLON,
70 /* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE, GRUB_KEYBOARD_KEY_RQUOTE,
71 /* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT, GRUB_KEYBOARD_KEY_BACKSLASH,
72 /* 0x2c */ GRUB_KEYBOARD_KEY_Z, GRUB_KEYBOARD_KEY_X,
73 /* 0x2e */ GRUB_KEYBOARD_KEY_C, GRUB_KEYBOARD_KEY_V,
74 /* 0x30 */ GRUB_KEYBOARD_KEY_B, GRUB_KEYBOARD_KEY_N,
75 /* 0x32 */ GRUB_KEYBOARD_KEY_M, GRUB_KEYBOARD_KEY_COMMA,
76 /* 0x34 */ GRUB_KEYBOARD_KEY_DOT, GRUB_KEYBOARD_KEY_SLASH,
77 /* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT, GRUB_KEYBOARD_KEY_NUMMUL,
78 /* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT, GRUB_KEYBOARD_KEY_SPACE,
79 /* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_F1,
80 /* 0x3c */ GRUB_KEYBOARD_KEY_F2, GRUB_KEYBOARD_KEY_F3,
81 /* 0x3e */ GRUB_KEYBOARD_KEY_F4, GRUB_KEYBOARD_KEY_F5,
82 /* 0x40 */ GRUB_KEYBOARD_KEY_F6, GRUB_KEYBOARD_KEY_F7,
83 /* 0x42 */ GRUB_KEYBOARD_KEY_F8, GRUB_KEYBOARD_KEY_F9,
84 /* 0x44 */ GRUB_KEYBOARD_KEY_F10, GRUB_KEYBOARD_KEY_NUM_LOCK,
85 /* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK, GRUB_KEYBOARD_KEY_NUM7,
86 /* 0x48 */ GRUB_KEYBOARD_KEY_NUM8, GRUB_KEYBOARD_KEY_NUM9,
87 /* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS, GRUB_KEYBOARD_KEY_NUM4,
88 /* 0x4c */ GRUB_KEYBOARD_KEY_NUM5, GRUB_KEYBOARD_KEY_NUM6,
89 /* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS, GRUB_KEYBOARD_KEY_NUM1,
90 /* 0x50 */ GRUB_KEYBOARD_KEY_NUM2, GRUB_KEYBOARD_KEY_NUM3,
91 /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT, GRUB_KEYBOARD_KEY_NUMDOT,
92 /* 0x54 */ 0, 0,
93 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND, GRUB_KEYBOARD_KEY_F11,
94 /* 0x58 */ GRUB_KEYBOARD_KEY_F12, 0,
95 /* 0x5a */ 0, 0,
96 /* 0x5c */ 0, 0,
97 /* 0x5e */ 0, 0,
98 /* 0x60 */ 0, 0,
99 /* 0x62 */ 0, 0,
100 /* OLPC keys. Just mapped to normal keys. */
101 /* 0x64 */ 0, GRUB_KEYBOARD_KEY_UP,
102 /* 0x66 */ GRUB_KEYBOARD_KEY_DOWN, GRUB_KEYBOARD_KEY_LEFT,
103 /* 0x68 */ GRUB_KEYBOARD_KEY_RIGHT, 0,
104 /* 0x6a */ 0, 0,
105 /* 0x6c */ 0, 0,
106 /* 0x6e */ 0, 0,
107 /* 0x70 */ 0, 0,
108 /* 0x72 */ 0, GRUB_KEYBOARD_KEY_JP_RO,
109 /* 0x74 */ 0, 0,
110 /* 0x76 */ 0, 0,
111 /* 0x78 */ 0, 0,
112 /* 0x7a */ 0, 0,
113 /* 0x7c */ 0, GRUB_KEYBOARD_KEY_JP_YEN,
114 /* 0x7e */ GRUB_KEYBOARD_KEY_KPCOMMA
117 static const struct
119 grub_uint8_t from, to;
120 } set1_e0_mapping[] =
122 {0x1c, GRUB_KEYBOARD_KEY_NUMENTER},
123 {0x1d, GRUB_KEYBOARD_KEY_RIGHT_CTRL},
124 {0x35, GRUB_KEYBOARD_KEY_NUMSLASH },
125 {0x38, GRUB_KEYBOARD_KEY_RIGHT_ALT},
126 {0x47, GRUB_KEYBOARD_KEY_HOME},
127 {0x48, GRUB_KEYBOARD_KEY_UP},
128 {0x49, GRUB_KEYBOARD_KEY_PPAGE},
129 {0x4b, GRUB_KEYBOARD_KEY_LEFT},
130 {0x4d, GRUB_KEYBOARD_KEY_RIGHT},
131 {0x4f, GRUB_KEYBOARD_KEY_END},
132 {0x50, GRUB_KEYBOARD_KEY_DOWN},
133 {0x51, GRUB_KEYBOARD_KEY_NPAGE},
134 {0x52, GRUB_KEYBOARD_KEY_INSERT},
135 {0x53, GRUB_KEYBOARD_KEY_DELETE},
138 static const grub_uint8_t set2_mapping[256] =
140 /* 0x00 */ 0, GRUB_KEYBOARD_KEY_F9,
141 /* 0x02 */ 0, GRUB_KEYBOARD_KEY_F5,
142 /* 0x04 */ GRUB_KEYBOARD_KEY_F3, GRUB_KEYBOARD_KEY_F1,
143 /* 0x06 */ GRUB_KEYBOARD_KEY_F2, GRUB_KEYBOARD_KEY_F12,
144 /* 0x08 */ 0, GRUB_KEYBOARD_KEY_F10,
145 /* 0x0a */ GRUB_KEYBOARD_KEY_F8, GRUB_KEYBOARD_KEY_F6,
146 /* 0x0c */ GRUB_KEYBOARD_KEY_F4, GRUB_KEYBOARD_KEY_TAB,
147 /* 0x0e */ GRUB_KEYBOARD_KEY_RQUOTE, 0,
148 /* 0x10 */ 0, GRUB_KEYBOARD_KEY_LEFT_ALT,
149 /* 0x12 */ GRUB_KEYBOARD_KEY_LEFT_SHIFT, 0,
150 /* 0x14 */ GRUB_KEYBOARD_KEY_LEFT_CTRL, GRUB_KEYBOARD_KEY_Q,
151 /* 0x16 */ GRUB_KEYBOARD_KEY_1, 0,
152 /* 0x18 */ 0, 0,
153 /* 0x1a */ GRUB_KEYBOARD_KEY_Z, GRUB_KEYBOARD_KEY_S,
154 /* 0x1c */ GRUB_KEYBOARD_KEY_A, GRUB_KEYBOARD_KEY_W,
155 /* 0x1e */ GRUB_KEYBOARD_KEY_2, 0,
156 /* 0x20 */ 0, GRUB_KEYBOARD_KEY_C,
157 /* 0x22 */ GRUB_KEYBOARD_KEY_X, GRUB_KEYBOARD_KEY_D,
158 /* 0x24 */ GRUB_KEYBOARD_KEY_E, GRUB_KEYBOARD_KEY_4,
159 /* 0x26 */ GRUB_KEYBOARD_KEY_3, 0,
160 /* 0x28 */ 0, GRUB_KEYBOARD_KEY_SPACE,
161 /* 0x2a */ GRUB_KEYBOARD_KEY_V, GRUB_KEYBOARD_KEY_F,
162 /* 0x2c */ GRUB_KEYBOARD_KEY_T, GRUB_KEYBOARD_KEY_R,
163 /* 0x2e */ GRUB_KEYBOARD_KEY_5, 0,
164 /* 0x30 */ 0, GRUB_KEYBOARD_KEY_N,
165 /* 0x32 */ GRUB_KEYBOARD_KEY_B, GRUB_KEYBOARD_KEY_H,
166 /* 0x34 */ GRUB_KEYBOARD_KEY_G, GRUB_KEYBOARD_KEY_Y,
167 /* 0x36 */ GRUB_KEYBOARD_KEY_6, 0,
168 /* 0x38 */ 0, 0,
169 /* 0x3a */ GRUB_KEYBOARD_KEY_M, GRUB_KEYBOARD_KEY_J,
170 /* 0x3c */ GRUB_KEYBOARD_KEY_U, GRUB_KEYBOARD_KEY_7,
171 /* 0x3e */ GRUB_KEYBOARD_KEY_8, 0,
172 /* 0x40 */ 0, GRUB_KEYBOARD_KEY_COMMA,
173 /* 0x42 */ GRUB_KEYBOARD_KEY_K, GRUB_KEYBOARD_KEY_I,
174 /* 0x44 */ GRUB_KEYBOARD_KEY_O, GRUB_KEYBOARD_KEY_0,
175 /* 0x46 */ GRUB_KEYBOARD_KEY_9, 0,
176 /* 0x48 */ 0, GRUB_KEYBOARD_KEY_DOT,
177 /* 0x4a */ GRUB_KEYBOARD_KEY_SLASH, GRUB_KEYBOARD_KEY_L,
178 /* 0x4c */ GRUB_KEYBOARD_KEY_SEMICOLON, GRUB_KEYBOARD_KEY_P,
179 /* 0x4e */ GRUB_KEYBOARD_KEY_DASH, 0,
180 /* 0x50 */ 0, GRUB_KEYBOARD_KEY_JP_RO,
181 /* 0x52 */ GRUB_KEYBOARD_KEY_DQUOTE, 0,
182 /* 0x54 */ GRUB_KEYBOARD_KEY_LBRACKET, GRUB_KEYBOARD_KEY_EQUAL,
183 /* 0x56 */ 0, 0,
184 /* 0x58 */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_RIGHT_SHIFT,
185 /* 0x5a */ GRUB_KEYBOARD_KEY_ENTER, GRUB_KEYBOARD_KEY_RBRACKET,
186 /* 0x5c */ 0, GRUB_KEYBOARD_KEY_BACKSLASH,
187 /* 0x5e */ 0, 0,
188 /* 0x60 */ 0, GRUB_KEYBOARD_KEY_102ND,
189 /* 0x62 */ 0, 0,
190 /* 0x64 */ 0, 0,
191 /* 0x66 */ GRUB_KEYBOARD_KEY_BACKSPACE, 0,
192 /* 0x68 */ 0, GRUB_KEYBOARD_KEY_NUM1,
193 /* 0x6a */ GRUB_KEYBOARD_KEY_JP_YEN, GRUB_KEYBOARD_KEY_NUM4,
194 /* 0x6c */ GRUB_KEYBOARD_KEY_NUM7, GRUB_KEYBOARD_KEY_KPCOMMA,
195 /* 0x6e */ 0, 0,
196 /* 0x70 */ GRUB_KEYBOARD_KEY_NUMDOT, GRUB_KEYBOARD_KEY_NUM0,
197 /* 0x72 */ GRUB_KEYBOARD_KEY_NUM2, GRUB_KEYBOARD_KEY_NUM5,
198 /* 0x74 */ GRUB_KEYBOARD_KEY_NUM6, GRUB_KEYBOARD_KEY_NUM8,
199 /* 0x76 */ GRUB_KEYBOARD_KEY_ESCAPE, GRUB_KEYBOARD_KEY_NUM_LOCK,
200 /* 0x78 */ GRUB_KEYBOARD_KEY_F11, GRUB_KEYBOARD_KEY_NUMPLUS,
201 /* 0x7a */ GRUB_KEYBOARD_KEY_NUM3, GRUB_KEYBOARD_KEY_NUMMINUS,
202 /* 0x7c */ GRUB_KEYBOARD_KEY_NUMMUL, GRUB_KEYBOARD_KEY_NUM9,
203 /* 0x7e */ GRUB_KEYBOARD_KEY_SCROLL_LOCK, 0,
204 /* 0x80 */ 0, 0,
205 /* 0x82 */ 0, GRUB_KEYBOARD_KEY_F7,
208 static const struct
210 grub_uint8_t from, to;
211 } set2_e0_mapping[] =
213 {0x11, GRUB_KEYBOARD_KEY_RIGHT_ALT},
214 {0x14, GRUB_KEYBOARD_KEY_RIGHT_CTRL},
215 {0x4a, GRUB_KEYBOARD_KEY_NUMSLASH},
216 {0x5a, GRUB_KEYBOARD_KEY_NUMENTER},
217 {0x69, GRUB_KEYBOARD_KEY_END},
218 {0x6b, GRUB_KEYBOARD_KEY_LEFT},
219 {0x6c, GRUB_KEYBOARD_KEY_HOME},
220 {0x70, GRUB_KEYBOARD_KEY_INSERT},
221 {0x71, GRUB_KEYBOARD_KEY_DELETE},
222 {0x72, GRUB_KEYBOARD_KEY_DOWN},
223 {0x74, GRUB_KEYBOARD_KEY_RIGHT},
224 {0x75, GRUB_KEYBOARD_KEY_UP},
225 {0x7a, GRUB_KEYBOARD_KEY_NPAGE},
226 {0x7d, GRUB_KEYBOARD_KEY_PPAGE},
229 static int ping_sent;
231 static void
232 keyboard_controller_wait_until_ready (void)
234 while (! KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)));
237 static grub_uint8_t
238 wait_ack (void)
240 grub_uint64_t endtime;
241 grub_uint8_t ack;
243 endtime = grub_get_time_ms () + 20;
245 ack = grub_inb (KEYBOARD_REG_DATA);
246 while (ack != GRUB_AT_ACK && ack != GRUB_AT_NACK
247 && grub_get_time_ms () < endtime);
248 return ack;
251 static int
252 at_command (grub_uint8_t data)
254 unsigned i;
255 for (i = 0; i < GRUB_AT_TRIES; i++)
257 grub_uint8_t ack;
258 keyboard_controller_wait_until_ready ();
259 grub_outb (data, KEYBOARD_REG_STATUS);
260 ack = wait_ack ();
261 if (ack == GRUB_AT_NACK)
262 continue;
263 if (ack == GRUB_AT_ACK)
264 break;
265 return 0;
267 return (i != GRUB_AT_TRIES);
270 static void
271 grub_keyboard_controller_write (grub_uint8_t c)
273 at_command (KEYBOARD_COMMAND_WRITE);
274 keyboard_controller_wait_until_ready ();
275 grub_outb (c, KEYBOARD_REG_DATA);
278 #if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
279 #define USE_SCANCODE_SET 1
280 #else
281 #define USE_SCANCODE_SET 0
282 #endif
284 #if !USE_SCANCODE_SET
286 static grub_uint8_t
287 grub_keyboard_controller_read (void)
289 at_command (KEYBOARD_COMMAND_READ);
290 keyboard_controller_wait_until_ready ();
291 return grub_inb (KEYBOARD_REG_DATA);
294 #endif
296 static int
297 write_mode (int mode)
299 unsigned i;
300 for (i = 0; i < GRUB_AT_TRIES; i++)
302 grub_uint8_t ack;
303 keyboard_controller_wait_until_ready ();
304 grub_outb (0xf0, KEYBOARD_REG_DATA);
305 keyboard_controller_wait_until_ready ();
306 grub_outb (mode, KEYBOARD_REG_DATA);
307 keyboard_controller_wait_until_ready ();
308 ack = wait_ack ();
309 if (ack == GRUB_AT_NACK)
310 continue;
311 if (ack == GRUB_AT_ACK)
312 break;
313 return 0;
316 return (i != GRUB_AT_TRIES);
319 static int
320 query_mode (void)
322 grub_uint8_t ret;
323 int e;
325 e = write_mode (0);
326 if (!e)
327 return 0;
329 keyboard_controller_wait_until_ready ();
332 ret = grub_inb (KEYBOARD_REG_DATA);
333 while (ret == GRUB_AT_ACK);
335 /* QEMU translates the set even in no-translate mode. */
336 if (ret == 0x43 || ret == 1)
337 return 1;
338 if (ret == 0x41 || ret == 2)
339 return 2;
340 if (ret == 0x3f || ret == 3)
341 return 3;
342 return 0;
345 static void
346 set_scancodes (void)
348 /* You must have visited computer museum. Keyboard without scancode set
349 knowledge. Assume XT. */
350 if (!grub_keyboard_orig_set)
352 grub_dprintf ("atkeyb", "No sets support assumed\n");
353 current_set = 1;
354 return;
357 #if !USE_SCANCODE_SET
358 current_set = 1;
359 return;
360 #else
362 grub_keyboard_controller_write (grub_keyboard_controller_orig
363 & ~KEYBOARD_AT_TRANSLATE);
365 write_mode (2);
366 current_set = query_mode ();
367 grub_dprintf ("atkeyb", "returned set %d\n", current_set);
368 if (current_set == 2)
369 return;
371 write_mode (1);
372 current_set = query_mode ();
373 grub_dprintf ("atkeyb", "returned set %d\n", current_set);
374 if (current_set == 1)
375 return;
376 grub_dprintf ("atkeyb", "no supported scancode set found\n");
377 #endif
380 static void
381 keyboard_controller_led (grub_uint8_t leds)
383 keyboard_controller_wait_until_ready ();
384 grub_outb (0xed, KEYBOARD_REG_DATA);
385 keyboard_controller_wait_until_ready ();
386 grub_outb (leds & 0x7, KEYBOARD_REG_DATA);
389 static int
390 fetch_key (int *is_break)
392 int was_ext = 0;
393 grub_uint8_t at_key;
394 int ret = 0;
396 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
397 return -1;
398 at_key = grub_inb (KEYBOARD_REG_DATA);
399 /* May happen if no keyboard is connected. Just ignore this. */
400 if (at_key == 0xff)
401 return -1;
402 if (at_key == 0xe0)
404 e0_received = 1;
405 return -1;
408 if ((current_set == 2 || current_set == 3) && at_key == 0xf0)
410 f0_received = 1;
411 return -1;
414 /* Setting LEDs may generate ACKs. */
415 if (at_key == GRUB_AT_ACK)
416 return -1;
418 was_ext = e0_received;
419 e0_received = 0;
421 switch (current_set)
423 case 1:
424 *is_break = !!(at_key & 0x80);
425 if (!was_ext)
426 ret = set1_mapping[at_key & 0x7f];
427 else
429 unsigned i;
430 for (i = 0; i < ARRAY_SIZE (set1_e0_mapping); i++)
431 if (set1_e0_mapping[i].from == (at_key & 0x7f))
433 ret = set1_e0_mapping[i].to;
434 break;
437 break;
438 case 2:
439 *is_break = f0_received;
440 f0_received = 0;
441 if (!was_ext)
442 ret = set2_mapping[at_key];
443 else
445 unsigned i;
446 for (i = 0; i < ARRAY_SIZE (set2_e0_mapping); i++)
447 if (set2_e0_mapping[i].from == at_key)
449 ret = set2_e0_mapping[i].to;
450 break;
453 break;
454 default:
455 return -1;
457 if (!ret)
459 if (was_ext)
460 grub_dprintf ("atkeyb", "Unknown key 0xe0+0x%02x from set %d\n",
461 at_key, current_set);
462 else
463 grub_dprintf ("atkeyb", "Unknown key 0x%02x from set %d\n",
464 at_key, current_set);
465 return -1;
467 return ret;
470 /* FIXME: This should become an interrupt service routine. For now
471 it's just used to catch events from control keys. */
472 static int
473 grub_keyboard_isr (grub_keyboard_key_t key, int is_break)
475 if (!is_break)
476 switch (key)
478 case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
479 at_keyboard_status |= GRUB_TERM_STATUS_LSHIFT;
480 return 1;
481 case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
482 at_keyboard_status |= GRUB_TERM_STATUS_RSHIFT;
483 return 1;
484 case GRUB_KEYBOARD_KEY_LEFT_CTRL:
485 at_keyboard_status |= GRUB_TERM_STATUS_LCTRL;
486 return 1;
487 case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
488 at_keyboard_status |= GRUB_TERM_STATUS_RCTRL;
489 return 1;
490 case GRUB_KEYBOARD_KEY_RIGHT_ALT:
491 at_keyboard_status |= GRUB_TERM_STATUS_RALT;
492 return 1;
493 case GRUB_KEYBOARD_KEY_LEFT_ALT:
494 at_keyboard_status |= GRUB_TERM_STATUS_LALT;
495 return 1;
496 default:
497 return 0;
499 else
500 switch (key)
502 case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
503 at_keyboard_status &= ~GRUB_TERM_STATUS_LSHIFT;
504 return 1;
505 case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
506 at_keyboard_status &= ~GRUB_TERM_STATUS_RSHIFT;
507 return 1;
508 case GRUB_KEYBOARD_KEY_LEFT_CTRL:
509 at_keyboard_status &= ~GRUB_TERM_STATUS_LCTRL;
510 return 1;
511 case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
512 at_keyboard_status &= ~GRUB_TERM_STATUS_RCTRL;
513 return 1;
514 case GRUB_KEYBOARD_KEY_RIGHT_ALT:
515 at_keyboard_status &= ~GRUB_TERM_STATUS_RALT;
516 return 1;
517 case GRUB_KEYBOARD_KEY_LEFT_ALT:
518 at_keyboard_status &= ~GRUB_TERM_STATUS_LALT;
519 return 1;
520 default:
521 return 0;
525 /* If there is a raw key pending, return it; otherwise return -1. */
526 static int
527 grub_keyboard_getkey (void)
529 int key;
530 int is_break = 0;
532 key = fetch_key (&is_break);
533 if (key == -1)
534 return -1;
536 if (grub_keyboard_isr (key, is_break))
537 return -1;
538 if (is_break)
539 return -1;
540 return key;
544 grub_at_keyboard_is_alive (void)
546 if (current_set != 0)
547 return 1;
548 if (ping_sent
549 && KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS))
550 && grub_inb (KEYBOARD_REG_DATA) == 0x55)
552 grub_keyboard_controller_init ();
553 return 1;
556 if (KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
558 grub_outb (0xaa, KEYBOARD_REG_STATUS);
559 ping_sent = 1;
561 return 0;
564 /* If there is a character pending, return it;
565 otherwise return GRUB_TERM_NO_KEY. */
566 static int
567 grub_at_keyboard_getkey (struct grub_term_input *term __attribute__ ((unused)))
569 int code;
571 if (!grub_at_keyboard_is_alive ())
572 return GRUB_TERM_NO_KEY;
574 code = grub_keyboard_getkey ();
575 if (code == -1)
576 return GRUB_TERM_NO_KEY;
577 #ifdef DEBUG_AT_KEYBOARD
578 grub_dprintf ("atkeyb", "Detected key 0x%x\n", code);
579 #endif
580 switch (code)
582 case GRUB_KEYBOARD_KEY_CAPS_LOCK:
583 at_keyboard_status ^= GRUB_TERM_STATUS_CAPS;
584 led_status ^= KEYBOARD_LED_CAPS;
585 keyboard_controller_led (led_status);
587 #ifdef DEBUG_AT_KEYBOARD
588 grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & GRUB_KEYBOARD_STATUS_CAPS_LOCK));
589 #endif
590 return GRUB_TERM_NO_KEY;
591 case GRUB_KEYBOARD_KEY_NUM_LOCK:
592 at_keyboard_status ^= GRUB_TERM_STATUS_NUM;
593 led_status ^= KEYBOARD_LED_NUM;
594 keyboard_controller_led (led_status);
596 #ifdef DEBUG_AT_KEYBOARD
597 grub_dprintf ("atkeyb", "num_lock = %d\n", !!(at_keyboard_status & GRUB_KEYBOARD_STATUS_NUM_LOCK));
598 #endif
599 return GRUB_TERM_NO_KEY;
600 case GRUB_KEYBOARD_KEY_SCROLL_LOCK:
601 at_keyboard_status ^= GRUB_TERM_STATUS_SCROLL;
602 led_status ^= KEYBOARD_LED_SCROLL;
603 keyboard_controller_led (led_status);
604 return GRUB_TERM_NO_KEY;
605 default:
606 return grub_term_map_key (code, at_keyboard_status);
610 static void
611 grub_keyboard_controller_init (void)
613 at_keyboard_status = 0;
614 /* Drain input buffer. */
615 while (1)
617 keyboard_controller_wait_until_ready ();
618 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
619 break;
620 keyboard_controller_wait_until_ready ();
621 grub_inb (KEYBOARD_REG_DATA);
623 #if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
624 grub_keyboard_controller_orig = 0;
625 grub_keyboard_orig_set = 2;
626 #elif defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_COREBOOT)
627 /* *BSD relies on those settings. */
628 grub_keyboard_controller_orig = KEYBOARD_AT_TRANSLATE;
629 grub_keyboard_orig_set = 2;
630 #else
631 grub_keyboard_controller_orig = grub_keyboard_controller_read ();
632 grub_keyboard_orig_set = query_mode ();
633 #endif
634 set_scancodes ();
635 keyboard_controller_led (led_status);
638 static grub_err_t
639 grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused)))
641 if (current_set == 0)
642 return GRUB_ERR_NONE;
643 if (grub_keyboard_orig_set)
644 write_mode (grub_keyboard_orig_set);
645 grub_keyboard_controller_write (grub_keyboard_controller_orig);
646 return GRUB_ERR_NONE;
649 static grub_err_t
650 grub_at_fini_hw (int noreturn __attribute__ ((unused)))
652 return grub_keyboard_controller_fini (NULL);
655 static grub_err_t
656 grub_at_restore_hw (void)
658 if (current_set == 0)
659 return GRUB_ERR_NONE;
661 /* Drain input buffer. */
662 while (1)
664 keyboard_controller_wait_until_ready ();
665 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
666 break;
667 keyboard_controller_wait_until_ready ();
668 grub_inb (KEYBOARD_REG_DATA);
670 set_scancodes ();
671 keyboard_controller_led (led_status);
673 return GRUB_ERR_NONE;
677 static struct grub_term_input grub_at_keyboard_term =
679 .name = "at_keyboard",
680 .fini = grub_keyboard_controller_fini,
681 .getkey = grub_at_keyboard_getkey
684 GRUB_MOD_INIT(at_keyboard)
686 grub_term_register_input ("at_keyboard", &grub_at_keyboard_term);
687 grub_loader_register_preboot_hook (grub_at_fini_hw, grub_at_restore_hw,
688 GRUB_LOADER_PREBOOT_HOOK_PRIO_CONSOLE);
691 GRUB_MOD_FINI(at_keyboard)
693 grub_keyboard_controller_fini (NULL);
694 grub_term_unregister_input (&grub_at_keyboard_term);