aboutsummaryrefslogtreecommitdiff
path: root/kernel/drivers/keyboard.c
blob: c0c4b5ee93be13578ab9b883972b8669f45f93bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "drivers/keyboard.h"

#include "drivers/tty/tty.h"

#include "main/interrupt.h"
#include "main/io.h"

#define IRQ_KEYBOARD 1

/* Indicates that one of these is "being held down" */
#define SHIFT_MASK 0x1
#define CTRL_MASK 0x2
/* Indicates that an escape code was the previous key received */
#define ESC_MASK 0x4
static int curmask = 0;

/* Where to read from to get scancodes */
#define KEYBOARD_IN_PORT 0x60
#define KEYBOARD_CMD_PORT 0x61

/* Scancodes for special keys */
#define LSHIFT 0x2a
#define RSHIFT 0x36
#define CTRL 0x1d
/* Right ctrl is escaped */
/* Our keyboard driver totally ignores ALT */

#define ESC0 0xe0
#define ESC1 0xe1

/* If the scancode & BREAK_MASK, it's a break code; otherwise, it's a make code
 */
#define BREAK_MASK 0x80

#define NORMAL_KEY_HIGH 0x39

/* Some sneaky value to indicate we don't actually pass anything to the terminal
 */
#define NO_CHAR 0xff

#define F1_SCANCODE 0x3b
#define F12_SCANCODE (F1_SCANCODE + 11)

/* Scancode tables copied from
   http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html */

/* The scancode table for "normal" scancodes - from 02 to 39 */
/* Unsupported chars are symbolized by \0 */
static const char *normal_scancodes =
    "\0"               /* Error */
    "\e"               /* Escape key */
    "1234567890-="     /* Top row */
    "\b"               /* Backspace */
    "\tqwertyuiop[]\n" /* Next row - ish */
    "\0"               /* Left ctrl */
    "asdfghjkl;\'`"
    "\0" /* Lshift */
    "\\"
    "zxcvbnm,./"
    "\0\0\0" /* Rshift, prtscrn, Lalt */
    " ";     /* Space bar */
/* As above, but if shift is pressed */
static const char *shift_scancodes =
    "\0"
    "\e"
    "!@#$%^&*()_+"
    "\b"
    "\tQWERTYUIOP{}\n"
    "\0"
    "ASDFGHJKL:\"~"
    "\0"
    "|"
    "ZXCVBNM<>?"
    "\0\0\0"
    " ";

static keyboard_char_handler_t keyboard_handler = NULL;

/* This is the function we register with the interrupt handler - it reads the
 * scancode and, if appropriate, call's the tty's receive_char function */
static long keyboard_intr_handler(regs_t *regs)
{
    uint8_t sc;     /* The scancode we receive */
    int break_code; /* Was it a break code */
    /* the resulting character ('\0' -> ignored char) */
    uint8_t c = NO_CHAR;
    /* Get the scancode */
    sc = inb(KEYBOARD_IN_PORT);
    /* Separate out the break code */
    break_code = sc & BREAK_MASK;
    sc &= ~BREAK_MASK;

    /* dbg(DBG_KB, ("scancode 0x%x, break 0x%x\n", sc, break_code)); */

    /* The order of this conditional is very, very tricky - be careful when
     * editing! */

    /* Most break codes are ignored */
    if (break_code)
    {
        /* Shift/ctrl release */
        if (sc == LSHIFT || sc == RSHIFT)
        {
            curmask &= ~SHIFT_MASK;
        }
        else if (sc == CTRL)
        {
            curmask &= ~CTRL_MASK;
        }
    }
    /* Check for the special keys */
    else if (sc == LSHIFT || sc == RSHIFT)
    {
        curmask |= SHIFT_MASK;
    }
    else if (sc == CTRL)
    {
        curmask |= CTRL_MASK;
    }
    /* All escaped keys past this point (anything except right shift and right
     * ctrl) will be ignored */
    else if (curmask & ESC_MASK)
    {
        /* Escape mask only lasts for one key */
        curmask &= ~ESC_MASK;
    }
    /* Now check for escape code */
    else if (sc == ESC0 || sc == ESC1)
    {
        curmask |= ESC_MASK;
    }

    else if (sc >= F1_SCANCODE && sc <= F12_SCANCODE)
    {
        c = (uint8_t)(F1 + (sc - F1_SCANCODE));
    }
    /* Check for Ctrl+Backspace which indicates scroll down */
    else if ((curmask & CTRL_MASK) && (curmask & SHIFT_MASK) &&
             sc == SCROLL_DOWN)
    {
        c = SCROLL_DOWN_PAGE;
    }

    else if ((curmask & CTRL_MASK) && (curmask & SHIFT_MASK) &&
             sc == SCROLL_UP)
    {
        c = SCROLL_UP_PAGE;
    }

    else if ((curmask & CTRL_MASK) && sc == SCROLL_DOWN)
    {
        c = SCROLL_DOWN;
    }
    /* Check for Ctrl+Enter which indicates scroll down */
    else if ((curmask & CTRL_MASK) && sc == SCROLL_UP)
    {
        c = SCROLL_UP;
    }
    /* Check to make sure the key isn't high enough that it won't be found in
     * tables */
    else if (sc > NORMAL_KEY_HIGH)
    {
        /* ignore */
    }
    /* Control characters */
    else if (curmask & CTRL_MASK)
    {
        /* Because of the way ASCII works, the control chars are based on the
         * values of the shifted chars produced without control */
        c = (uint8_t)shift_scancodes[sc];
        /* Range of chars that have corresponding control chars */
        if (c >= 0x40 && c < 0x60)
        {
            c -= 0x40;
        }
        else
        {
            c = NO_CHAR;
        }
    }
    /* Capitals */
    else if (curmask & SHIFT_MASK)
    {
        c = (uint8_t)shift_scancodes[sc];
    }
    else
    {
        c = (uint8_t)normal_scancodes[sc];
    }

    if (c != NO_CHAR)
    {
        keyboard_handler(c);
    }
    else
    {
        //        panic("get rid of me: char was: %c (%d) (%x)\n", c, c, c);
    }
    dbg(DBG_KB, "received scancode 0x%x; resolved to char 0x%x\n", sc, c);
    return 0;
}

void keyboard_init(keyboard_char_handler_t handler)
{
    intr_map(IRQ_KEYBOARD, INTR_KEYBOARD);
    intr_register(INTR_KEYBOARD, keyboard_intr_handler);
    keyboard_handler = handler;
}