1 #include "devices/kbd.h"
6 #include "devices/intq.h"
7 #include "threads/interrupt.h"
8 #include "threads/io.h"
10 /* Keyboard data register port. */
13 /* Shift state bits. */
14 #define LSHIFT 0x01 /* Left Shift. */
15 #define RSHIFT 0x02 /* Right Shift. */
16 #define LALT 0x04 /* Left Alt. */
17 #define RALT 0x08 /* Right Alt. */
18 #define LCTRL 0x10 /* Left Ctrl. */
19 #define RCTRL 0x20 /* Right Ctrl. */
20 #define CAPS 0x40 /* Caps Lock. */
22 /* Current shift state. */
23 static unsigned shift_state;
25 /* Keyboard buffer. */
26 static struct intq buffer;
28 static intr_handler_func keyboard_interrupt;
30 /* Initializes the keyboard. */
34 intq_init (&buffer, "keyboard");
35 intr_register (0x21, 0, INTR_OFF, keyboard_interrupt, "8042 Keyboard");
38 /* Retrieves a key from the keyboard buffer.
39 If the buffer is empty, waits for a key to be pressed. */
46 key = intq_getc (&buffer);
47 intq_unlock (&buffer);
52 /* Maps a set of contiguous scancodes into characters. */
55 uint8_t first_scancode; /* First scancode. */
56 const char *chars; /* chars[0] has scancode first_scancode,
57 chars[1] has scancode first_scancode + 1,
58 and so on to the end of the string. */
61 /* Keys that produce the same characters regardless of whether
62 the Shift keys are down. Case of letters is an exception
63 that we handle elsewhere. */
64 static const struct keymap invariant_keymap[] =
68 {0x0f, "\tQWERTYUIOP"},
77 /* Characters for keys pressed without Shift, for those keys
79 static const struct keymap unshifted_keymap[] =
81 {0x02, "1234567890-="},
89 /* Characters for keys pressed with Shift, for those keys where
91 static const struct keymap shifted_keymap[] =
93 {0x02, "!@#$%^&*()_+"},
101 static bool map_key (const struct keymap[], unsigned scancode, uint8_t *);
104 keyboard_interrupt (struct intr_frame *args UNUSED)
106 /* Status of shift keys. */
107 bool shift = (shift_state & (LSHIFT | RSHIFT)) != 0;
108 bool alt = (shift_state & (LALT | RALT)) != 0;
109 bool ctrl = (shift_state & (LCTRL | RCTRL)) != 0;
110 bool caps = (shift_state & CAPS) != 0;
112 /* Keyboard scancode. */
115 /* False if key pressed, true if key released. */
118 /* Character that corresponds to `code'. */
121 /* Read scancode, including second byte if prefix code. */
122 code = inb (DATA_REG);
124 code = (code << 8) | inb (DATA_REG);
126 /* Bit 0x80 distinguishes key press from key release
127 (even if there's a prefix). */
128 release = (code & 0x80) != 0;
138 else if (map_key (invariant_keymap, code, &c)
139 || (!shift && map_key (unshifted_keymap, code, &c))
140 || (shift && map_key (shifted_keymap, code, &c)))
142 /* Ordinary character. */
145 /* Handle Ctrl, Shift.
146 Note that Ctrl overrides Shift. */
147 if (ctrl && c >= 0x40 && c < 0x60)
149 /* A is 0x41, Ctrl+A is 0x01, etc. */
152 else if (shift == caps)
155 /* Handle Alt by setting the high bit.
156 This 0x80 is unrelated to the one used to
157 distinguish key press from key release. */
161 /* Append to keyboard buffer. */
162 if (!intq_full (&buffer))
163 intq_putc (&buffer, c);
168 /* Table of shift keys.
169 Maps a keycode into a shift_state bit. */
170 static const unsigned shift_keys[][2] =
181 const unsigned (*key)[2];
183 /* Scan the table. */
184 for (key = shift_keys; (*key)[0] != 0; key++)
185 if ((*key)[0] == code)
188 shift_state &= ~(*key)[1];
190 shift_state |= (*key)[1];
196 /* Scans the array of keymaps K for SCANCODE.
197 If found, sets *C to the corresponding character and returns
199 If not found, returns false and C is ignored. */
201 map_key (const struct keymap k[], unsigned scancode, uint8_t *c)
203 for (; k->first_scancode != 0; k++)
204 if (scancode >= k->first_scancode
205 && scancode < k->first_scancode + strlen (k->chars))
207 *c = k->chars[scancode - k->first_scancode];