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/>.
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
;
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
,
93 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND
, GRUB_KEYBOARD_KEY_F11
,
94 /* 0x58 */ GRUB_KEYBOARD_KEY_F12
, 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,
108 /* 0x72 */ 0, GRUB_KEYBOARD_KEY_JP_RO
,
113 /* 0x7c */ 0, GRUB_KEYBOARD_KEY_JP_YEN
,
114 /* 0x7e */ GRUB_KEYBOARD_KEY_KPCOMMA
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,
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,
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
,
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
,
188 /* 0x60 */ 0, GRUB_KEYBOARD_KEY_102ND
,
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
,
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,
205 /* 0x82 */ 0, GRUB_KEYBOARD_KEY_F7
,
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
;
232 keyboard_controller_wait_until_ready (void)
234 while (! KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS
)));
240 grub_uint64_t endtime
;
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
);
252 at_command (grub_uint8_t data
)
255 for (i
= 0; i
< GRUB_AT_TRIES
; i
++)
258 keyboard_controller_wait_until_ready ();
259 grub_outb (data
, KEYBOARD_REG_STATUS
);
261 if (ack
== GRUB_AT_NACK
)
263 if (ack
== GRUB_AT_ACK
)
267 return (i
!= GRUB_AT_TRIES
);
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
281 #define USE_SCANCODE_SET 0
284 #if !USE_SCANCODE_SET
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
);
297 write_mode (int mode
)
300 for (i
= 0; i
< GRUB_AT_TRIES
; i
++)
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 ();
309 if (ack
== GRUB_AT_NACK
)
311 if (ack
== GRUB_AT_ACK
)
316 return (i
!= GRUB_AT_TRIES
);
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)
338 if (ret
== 0x41 || ret
== 2)
340 if (ret
== 0x3f || ret
== 3)
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");
357 #if !USE_SCANCODE_SET
362 grub_keyboard_controller_write (grub_keyboard_controller_orig
363 & ~KEYBOARD_AT_TRANSLATE
);
366 current_set
= query_mode ();
367 grub_dprintf ("atkeyb", "returned set %d\n", current_set
);
368 if (current_set
== 2)
372 current_set
= query_mode ();
373 grub_dprintf ("atkeyb", "returned set %d\n", current_set
);
374 if (current_set
== 1)
376 grub_dprintf ("atkeyb", "no supported scancode set found\n");
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
);
390 fetch_key (int *is_break
)
396 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS
)))
398 at_key
= grub_inb (KEYBOARD_REG_DATA
);
399 /* May happen if no keyboard is connected. Just ignore this. */
408 if ((current_set
== 2 || current_set
== 3) && at_key
== 0xf0)
414 /* Setting LEDs may generate ACKs. */
415 if (at_key
== GRUB_AT_ACK
)
418 was_ext
= e0_received
;
424 *is_break
= !!(at_key
& 0x80);
426 ret
= set1_mapping
[at_key
& 0x7f];
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
;
439 *is_break
= f0_received
;
442 ret
= set2_mapping
[at_key
];
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
;
460 grub_dprintf ("atkeyb", "Unknown key 0xe0+0x%02x from set %d\n",
461 at_key
, current_set
);
463 grub_dprintf ("atkeyb", "Unknown key 0x%02x from set %d\n",
464 at_key
, current_set
);
470 /* FIXME: This should become an interrupt service routine. For now
471 it's just used to catch events from control keys. */
473 grub_keyboard_isr (grub_keyboard_key_t key
, int is_break
)
478 case GRUB_KEYBOARD_KEY_LEFT_SHIFT
:
479 at_keyboard_status
|= GRUB_TERM_STATUS_LSHIFT
;
481 case GRUB_KEYBOARD_KEY_RIGHT_SHIFT
:
482 at_keyboard_status
|= GRUB_TERM_STATUS_RSHIFT
;
484 case GRUB_KEYBOARD_KEY_LEFT_CTRL
:
485 at_keyboard_status
|= GRUB_TERM_STATUS_LCTRL
;
487 case GRUB_KEYBOARD_KEY_RIGHT_CTRL
:
488 at_keyboard_status
|= GRUB_TERM_STATUS_RCTRL
;
490 case GRUB_KEYBOARD_KEY_RIGHT_ALT
:
491 at_keyboard_status
|= GRUB_TERM_STATUS_RALT
;
493 case GRUB_KEYBOARD_KEY_LEFT_ALT
:
494 at_keyboard_status
|= GRUB_TERM_STATUS_LALT
;
502 case GRUB_KEYBOARD_KEY_LEFT_SHIFT
:
503 at_keyboard_status
&= ~GRUB_TERM_STATUS_LSHIFT
;
505 case GRUB_KEYBOARD_KEY_RIGHT_SHIFT
:
506 at_keyboard_status
&= ~GRUB_TERM_STATUS_RSHIFT
;
508 case GRUB_KEYBOARD_KEY_LEFT_CTRL
:
509 at_keyboard_status
&= ~GRUB_TERM_STATUS_LCTRL
;
511 case GRUB_KEYBOARD_KEY_RIGHT_CTRL
:
512 at_keyboard_status
&= ~GRUB_TERM_STATUS_RCTRL
;
514 case GRUB_KEYBOARD_KEY_RIGHT_ALT
:
515 at_keyboard_status
&= ~GRUB_TERM_STATUS_RALT
;
517 case GRUB_KEYBOARD_KEY_LEFT_ALT
:
518 at_keyboard_status
&= ~GRUB_TERM_STATUS_LALT
;
525 /* If there is a raw key pending, return it; otherwise return -1. */
527 grub_keyboard_getkey (void)
532 key
= fetch_key (&is_break
);
536 if (grub_keyboard_isr (key
, is_break
))
544 grub_at_keyboard_is_alive (void)
546 if (current_set
!= 0)
549 && KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS
))
550 && grub_inb (KEYBOARD_REG_DATA
) == 0x55)
552 grub_keyboard_controller_init ();
556 if (KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS
)))
558 grub_outb (0xaa, KEYBOARD_REG_STATUS
);
564 /* If there is a character pending, return it;
565 otherwise return GRUB_TERM_NO_KEY. */
567 grub_at_keyboard_getkey (struct grub_term_input
*term
__attribute__ ((unused
)))
571 if (!grub_at_keyboard_is_alive ())
572 return GRUB_TERM_NO_KEY
;
574 code
= grub_keyboard_getkey ();
576 return GRUB_TERM_NO_KEY
;
577 #ifdef DEBUG_AT_KEYBOARD
578 grub_dprintf ("atkeyb", "Detected key 0x%x\n", 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
));
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
));
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
;
606 return grub_term_map_key (code
, at_keyboard_status
);
611 grub_keyboard_controller_init (void)
613 at_keyboard_status
= 0;
614 /* Drain input buffer. */
617 keyboard_controller_wait_until_ready ();
618 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS
)))
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;
631 grub_keyboard_controller_orig
= grub_keyboard_controller_read ();
632 grub_keyboard_orig_set
= query_mode ();
635 keyboard_controller_led (led_status
);
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
;
650 grub_at_fini_hw (int noreturn
__attribute__ ((unused
)))
652 return grub_keyboard_controller_fini (NULL
);
656 grub_at_restore_hw (void)
658 if (current_set
== 0)
659 return GRUB_ERR_NONE
;
661 /* Drain input buffer. */
664 keyboard_controller_wait_until_ready ();
665 if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS
)))
667 keyboard_controller_wait_until_ready ();
668 grub_inb (KEYBOARD_REG_DATA
);
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
);