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. */
43 enum intr_level old_level;
46 old_level = intr_disable ();
47 key = intq_getc (&buffer);
48 intr_set_level (old_level);
53 /* Maps a set of contiguous scancodes into characters. */
56 uint8_t first_scancode; /* First scancode. */
57 const char *chars; /* chars[0] has scancode first_scancode,
58 chars[1] has scancode first_scancode + 1,
59 and so on to the end of the string. */
62 /* Keys that produce the same characters regardless of whether
63 the Shift keys are down. Case of letters is an exception
64 that we handle elsewhere. */
65 static const struct keymap invariant_keymap[] =
69 {0x0f, "\tQWERTYUIOP"},
78 /* Characters for keys pressed without Shift, for those keys
80 static const struct keymap unshifted_keymap[] =
82 {0x02, "1234567890-="},
90 /* Characters for keys pressed with Shift, for those keys where
92 static const struct keymap shifted_keymap[] =
94 {0x02, "!@#$%^&*()_+"},
102 static bool map_key (const struct keymap[], unsigned scancode, uint8_t *);
105 keyboard_interrupt (struct intr_frame *args UNUSED)
107 /* Status of shift keys. */
108 bool shift = (shift_state & (LSHIFT | RSHIFT)) != 0;
109 bool alt = (shift_state & (LALT | RALT)) != 0;
110 bool ctrl = (shift_state & (LCTRL | RCTRL)) != 0;
111 bool caps = (shift_state & CAPS) != 0;
113 /* Keyboard scancode. */
116 /* False if key pressed, true if key released. */
119 /* Character that corresponds to `code'. */
122 /* Read scancode, including second byte if prefix code. */
123 code = inb (DATA_REG);
125 code = (code << 8) | inb (DATA_REG);
127 /* Bit 0x80 distinguishes key press from key release
128 (even if there's a prefix). */
129 release = (code & 0x80) != 0;
139 else if (map_key (invariant_keymap, code, &c)
140 || (!shift && map_key (unshifted_keymap, code, &c))
141 || (shift && map_key (shifted_keymap, code, &c)))
143 /* Ordinary character. */
146 /* Handle Ctrl, Shift.
147 Note that Ctrl overrides Shift. */
148 if (ctrl && c >= 0x40 && c < 0x60)
150 /* A is 0x41, Ctrl+A is 0x01, etc. */
153 else if (shift == caps)
156 /* Handle Alt by setting the high bit.
157 This 0x80 is unrelated to the one used to
158 distinguish key press from key release. */
162 /* Append to keyboard buffer. */
163 if (!intq_full (&buffer))
164 intq_putc (&buffer, c);
169 /* Table of shift keys.
170 Maps a keycode into a shift_state bit. */
171 static const unsigned shift_keys[][2] =
182 const unsigned (*key)[2];
184 /* Scan the table. */
185 for (key = shift_keys; (*key)[0] != 0; key++)
186 if ((*key)[0] == code)
189 shift_state &= ~(*key)[1];
191 shift_state |= (*key)[1];
197 /* Scans the array of keymaps K for SCANCODE.
198 If found, sets *C to the corresponding character and returns
200 If not found, returns false and C is ignored. */
202 map_key (const struct keymap k[], unsigned scancode, uint8_t *c)
204 for (; k->first_scancode != 0; k++)
205 if (scancode >= k->first_scancode
206 && scancode < k->first_scancode + strlen (k->chars))
208 *c = k->chars[scancode - k->first_scancode];