2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2007 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/machine/console.h>
20
#include <grub/cpu/io.h>
21
#include <grub/misc.h>
27
#define CAPS_LOCK 0x3a
29
#define KEYBOARD_STATUS_SHIFT_L (1 << 0)
30
#define KEYBOARD_STATUS_SHIFT_R (1 << 1)
31
#define KEYBOARD_STATUS_ALT_L (1 << 2)
32
#define KEYBOARD_STATUS_ALT_R (1 << 3)
33
#define KEYBOARD_STATUS_CTRL_L (1 << 4)
34
#define KEYBOARD_STATUS_CTRL_R (1 << 5)
35
#define KEYBOARD_STATUS_CAPS_LOCK (1 << 6)
37
#define KEYBOARD_REG_DATA 0x60
38
#define KEYBOARD_REG_STATUS 0x64
40
#define KEYBOARD_ISMAKE(x) !((x) & 0x80)
41
#define KEYBOARD_ISREADY(x) (((x) & 0x01) == 0)
42
#define KEYBOARD_SCANCODE(x) ((x) & 0x7f)
44
static short at_keyboard_status = 0;
46
static char keyboard_map[128] =
48
'\0', '\0', '1', '2', '3', '4', '5', '6',
49
'7', '8', '9', '0', '-', '=', '\b', '\t',
50
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
51
'o', 'p', '[', ']', '\n', '\0', 'a', 's',
52
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
53
'\'', '`', '\0', '\\', 'z', 'x', 'c', 'v',
54
'b', 'n', 'm', ',', '.', '/', '\0', '*',
55
'\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
56
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '7',
57
'8', '9', '-', '4', '5', '6', '+', '1',
61
static char keyboard_map_shift[128] =
63
'\0', '\0', '!', '@', '#', '$', '%', '^',
64
'&', '*', '(', ')', '_', '+', '\b', '\t',
65
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
66
'O', 'P', '{', '}', '\n', '\0', 'A', 'S',
67
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
68
'\"', '~', '\0', '|', 'Z', 'X', 'C', 'V',
69
'B', 'N', 'M', '<', '>', '?', '\0', '*',
70
'\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
71
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '7',
72
'8', '9', '-', '4', '5', '6', '+', '1',
76
/* FIXME: This should become an interrupt service routine. For now
77
it's just used to catch events from control keys. */
79
grub_keyboard_isr (char key)
81
char is_make = KEYBOARD_ISMAKE (key);
82
key = KEYBOARD_SCANCODE (key);
87
at_keyboard_status |= KEYBOARD_STATUS_SHIFT_L;
90
at_keyboard_status |= KEYBOARD_STATUS_SHIFT_R;
93
at_keyboard_status |= KEYBOARD_STATUS_CTRL_L;
96
at_keyboard_status |= KEYBOARD_STATUS_ALT_L;
99
/* Skip grub_dprintf. */
106
at_keyboard_status &= ~KEYBOARD_STATUS_SHIFT_L;
109
at_keyboard_status &= ~KEYBOARD_STATUS_SHIFT_R;
112
at_keyboard_status &= ~KEYBOARD_STATUS_CTRL_L;
115
at_keyboard_status &= ~KEYBOARD_STATUS_ALT_L;
118
/* Skip grub_dprintf. */
121
grub_dprintf ("atkeyb", "Control key 0x%0x was %s\n", key, is_make ? "pressed" : "unpressed");
124
/* If there is a raw key pending, return it; otherwise return -1. */
126
grub_keyboard_getkey ()
129
if (KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
131
key = grub_inb (KEYBOARD_REG_DATA);
132
/* FIXME */ grub_keyboard_isr (key);
133
if (! KEYBOARD_ISMAKE (key))
135
return (KEYBOARD_SCANCODE (key));
138
/* If there is a character pending, return it; otherwise return -1. */
140
grub_console_checkkey ()
143
key = grub_keyboard_getkey ();
146
grub_dprintf ("atkeyb", "Detected key 0x%x\n", key);
150
if (at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK)
151
at_keyboard_status &= ~KEYBOARD_STATUS_CAPS_LOCK;
153
at_keyboard_status |= KEYBOARD_STATUS_CAPS_LOCK;
154
grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK));
158
if (at_keyboard_status & (KEYBOARD_STATUS_SHIFT_L | KEYBOARD_STATUS_SHIFT_R))
159
key = keyboard_map_shift[key];
161
key = keyboard_map[key];
162
if (at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK)
164
if ((key >= 'a') && (key <= 'z'))
166
else if ((key >= 'A') && (key <= 'Z'))
174
grub_console_getkey ()
179
key = grub_console_checkkey ();