Subversion Repositories HelenOS

Rev

Rev 606 | Rev 670 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <arch/i8042.h>
30
#include <arch/i8259.h>
31
#include <arch/interrupt.h>
32
#include <cpu.h>
33
#include <arch/asm.h>
34
#include <arch.h>
35
#include <print.h>
508 jermar 36
#include <synch/spinlock.h>
37
#include <typedefs.h>
511 jermar 38
#include <console/chardev.h>
39
#include <console/console.h>
517 jermar 40
#include <macros.h>
576 palkovsky 41
#include <interrupt.h>
1 jermar 42
 
508 jermar 43
/**
1 jermar 44
 * i8042 processor driver.
508 jermar 45
 * It takes care of low-level keyboard functions.
1 jermar 46
 */
47
 
512 jermar 48
#define i8042_DATA      0x60
49
#define i8042_STATUS        0x64
50
 
51
/** Keyboard commands. */
52
#define KBD_ENABLE  0xf4
53
#define KBD_DISABLE 0xf5
54
#define KBD_ACK     0xfa
55
 
508 jermar 56
#define SPECIAL     '?'
57
#define KEY_RELEASE 0x80
58
 
59
static void key_released(__u8 sc);
60
static void key_pressed(__u8 sc);
61
 
62
#define PRESSED_SHIFT       (1<<0)
63
#define PRESSED_CAPSLOCK    (1<<1)
64
#define LOCKED_CAPSLOCK     (1<<0)
65
 
623 jermar 66
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
508 jermar 67
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
509 jermar 68
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
508 jermar 69
 
575 palkovsky 70
static void i8042_suspend(chardev_t *);
71
static void i8042_resume(chardev_t *);
511 jermar 72
 
73
static chardev_t kbrd;
74
static chardev_operations_t ops = {
75
    .suspend = i8042_suspend,
76
    .resume = i8042_resume
77
};
78
 
508 jermar 79
/** Primary meaning of scancodes. */
80
static char sc_primary_map[] = {
81
    SPECIAL, /* 0x00 */
82
    SPECIAL, /* 0x01 - Esc */
83
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
588 palkovsky 84
    '\b', /* 0x0e - Backspace */
508 jermar 85
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
86
    SPECIAL, /* 0x1d - LCtrl */
87
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
88
    '`',
89
    SPECIAL, /* 0x2a - LShift */
90
    '\\',
91
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
92
    SPECIAL, /* 0x36 - RShift */
93
    '*',
94
    SPECIAL, /* 0x38 - LAlt */
95
    ' ',
96
    SPECIAL, /* 0x3a - CapsLock */
97
    SPECIAL, /* 0x3b - F1 */
98
    SPECIAL, /* 0x3c - F2 */
99
    SPECIAL, /* 0x3d - F3 */
100
    SPECIAL, /* 0x3e - F4 */
101
    SPECIAL, /* 0x3f - F5 */
102
    SPECIAL, /* 0x40 - F6 */
103
    SPECIAL, /* 0x41 - F7 */
104
    SPECIAL, /* 0x42 - F8 */
105
    SPECIAL, /* 0x43 - F9 */
106
    SPECIAL, /* 0x44 - F10 */
107
    SPECIAL, /* 0x45 - NumLock */
108
    SPECIAL, /* 0x46 - ScrollLock */
109
    '7', '8', '9', '-',
110
    '4', '5', '6', '+',
111
    '1', '2', '3',
112
    '0', '.',
113
    SPECIAL, /* 0x54 - Alt-SysRq */
114
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
115
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
116
    SPECIAL, /* 0x57 - F11 */
117
    SPECIAL, /* 0x58 - F12 */
118
    SPECIAL, /* 0x59 */
119
    SPECIAL, /* 0x5a */
120
    SPECIAL, /* 0x5b */
121
    SPECIAL, /* 0x5c */
122
    SPECIAL, /* 0x5d */
123
    SPECIAL, /* 0x5e */
124
    SPECIAL, /* 0x5f */
125
    SPECIAL, /* 0x60 */
126
    SPECIAL, /* 0x61 */
127
    SPECIAL, /* 0x62 */
128
    SPECIAL, /* 0x63 */
129
    SPECIAL, /* 0x64 */
130
    SPECIAL, /* 0x65 */
131
    SPECIAL, /* 0x66 */
132
    SPECIAL, /* 0x67 */
133
    SPECIAL, /* 0x68 */
134
    SPECIAL, /* 0x69 */
135
    SPECIAL, /* 0x6a */
136
    SPECIAL, /* 0x6b */
137
    SPECIAL, /* 0x6c */
138
    SPECIAL, /* 0x6d */
139
    SPECIAL, /* 0x6e */
140
    SPECIAL, /* 0x6f */
141
    SPECIAL, /* 0x70 */
142
    SPECIAL, /* 0x71 */
143
    SPECIAL, /* 0x72 */
144
    SPECIAL, /* 0x73 */
145
    SPECIAL, /* 0x74 */
146
    SPECIAL, /* 0x75 */
147
    SPECIAL, /* 0x76 */
148
    SPECIAL, /* 0x77 */
149
    SPECIAL, /* 0x78 */
150
    SPECIAL, /* 0x79 */
151
    SPECIAL, /* 0x7a */
152
    SPECIAL, /* 0x7b */
153
    SPECIAL, /* 0x7c */
154
    SPECIAL, /* 0x7d */
155
    SPECIAL, /* 0x7e */
156
    SPECIAL, /* 0x7f */
157
};
158
 
159
/** Secondary meaning of scancodes. */
160
static char sc_secondary_map[] = {
161
    SPECIAL, /* 0x00 */
162
    SPECIAL, /* 0x01 - Esc */
163
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
164
    SPECIAL, /* 0x0e - Backspace */
165
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
166
    SPECIAL, /* 0x1d - LCtrl */
167
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
168
    '~',
169
    SPECIAL, /* 0x2a - LShift */
170
    '|',
171
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
172
    SPECIAL, /* 0x36 - RShift */
173
    '*',
174
    SPECIAL, /* 0x38 - LAlt */
175
    ' ',
176
    SPECIAL, /* 0x3a - CapsLock */
177
    SPECIAL, /* 0x3b - F1 */
178
    SPECIAL, /* 0x3c - F2 */
179
    SPECIAL, /* 0x3d - F3 */
180
    SPECIAL, /* 0x3e - F4 */
181
    SPECIAL, /* 0x3f - F5 */
182
    SPECIAL, /* 0x40 - F6 */
183
    SPECIAL, /* 0x41 - F7 */
184
    SPECIAL, /* 0x42 - F8 */
185
    SPECIAL, /* 0x43 - F9 */
186
    SPECIAL, /* 0x44 - F10 */
187
    SPECIAL, /* 0x45 - NumLock */
188
    SPECIAL, /* 0x46 - ScrollLock */
189
    '7', '8', '9', '-',
190
    '4', '5', '6', '+',
191
    '1', '2', '3',
192
    '0', '.',
193
    SPECIAL, /* 0x54 - Alt-SysRq */
194
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
195
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
196
    SPECIAL, /* 0x57 - F11 */
197
    SPECIAL, /* 0x58 - F12 */
198
    SPECIAL, /* 0x59 */
199
    SPECIAL, /* 0x5a */
200
    SPECIAL, /* 0x5b */
201
    SPECIAL, /* 0x5c */
202
    SPECIAL, /* 0x5d */
203
    SPECIAL, /* 0x5e */
204
    SPECIAL, /* 0x5f */
205
    SPECIAL, /* 0x60 */
206
    SPECIAL, /* 0x61 */
207
    SPECIAL, /* 0x62 */
208
    SPECIAL, /* 0x63 */
209
    SPECIAL, /* 0x64 */
210
    SPECIAL, /* 0x65 */
211
    SPECIAL, /* 0x66 */
212
    SPECIAL, /* 0x67 */
213
    SPECIAL, /* 0x68 */
214
    SPECIAL, /* 0x69 */
215
    SPECIAL, /* 0x6a */
216
    SPECIAL, /* 0x6b */
217
    SPECIAL, /* 0x6c */
218
    SPECIAL, /* 0x6d */
219
    SPECIAL, /* 0x6e */
220
    SPECIAL, /* 0x6f */
221
    SPECIAL, /* 0x70 */
222
    SPECIAL, /* 0x71 */
223
    SPECIAL, /* 0x72 */
224
    SPECIAL, /* 0x73 */
225
    SPECIAL, /* 0x74 */
226
    SPECIAL, /* 0x75 */
227
    SPECIAL, /* 0x76 */
228
    SPECIAL, /* 0x77 */
229
    SPECIAL, /* 0x78 */
230
    SPECIAL, /* 0x79 */
231
    SPECIAL, /* 0x7a */
232
    SPECIAL, /* 0x7b */
233
    SPECIAL, /* 0x7c */
234
    SPECIAL, /* 0x7d */
235
    SPECIAL, /* 0x7e */
236
    SPECIAL, /* 0x7f */
237
};
238
 
576 palkovsky 239
static void i8042_interrupt(int n, void *stack);
240
 
508 jermar 241
/** Initialize i8042. */
1 jermar 242
void i8042_init(void)
243
{
576 palkovsky 244
    exc_register(VECTOR_KBD, "i8042_interrupt", i8042_interrupt);
512 jermar 245
    trap_virtual_enable_irqs(1<<IRQ_KBD);
575 palkovsky 246
    chardev_initialize("i8042_kbd", &kbrd, &ops);
511 jermar 247
    stdin = &kbrd;
1 jermar 248
}
249
 
508 jermar 250
/** Process i8042 interrupt.
251
 *
252
 * @param n Interrupt vector.
253
 * @param stack Interrupted stack.
254
 */
576 palkovsky 255
void i8042_interrupt(int n, void *stack)
1 jermar 256
{
257
    __u8 x;
258
 
259
    trap_virtual_eoi();
512 jermar 260
    x = inb(i8042_DATA);
508 jermar 261
    if (x & KEY_RELEASE)
262
        key_released(x ^ KEY_RELEASE);
263
    else
264
        key_pressed(x);
1 jermar 265
}
508 jermar 266
 
267
/** Process release of key.
268
 *
269
 * @param sc Scancode of the key being released.
270
 */
271
void key_released(__u8 sc)
272
{
273
    spinlock_lock(&keylock);
274
    switch (sc) {
275
        case SC_LSHIFT:
276
        case SC_RSHIFT:
277
        keyflags &= ~PRESSED_SHIFT;
278
        break;
279
        case SC_CAPSLOCK:
280
        keyflags &= ~PRESSED_CAPSLOCK;
281
        if (lockflags & LOCKED_CAPSLOCK)
282
            lockflags &= ~LOCKED_CAPSLOCK;
283
        else
284
            lockflags |= LOCKED_CAPSLOCK;
285
        break;
286
        default:
287
        break;
288
    }
289
    spinlock_unlock(&keylock);
290
}
291
 
292
/** Process keypress.
293
 *
294
 * @param sc Scancode of the key being pressed.
295
 */
296
void key_pressed(__u8 sc)
297
{
298
    char *map = sc_primary_map;
299
    char ascii = sc_primary_map[sc];
300
    bool shift, capslock;
301
    bool letter = false;
302
 
303
    spinlock_lock(&keylock);
304
    switch (sc) {
601 palkovsky 305
    case SC_LSHIFT:
306
    case SC_RSHIFT:
508 jermar 307
            keyflags |= PRESSED_SHIFT;
308
        break;
601 palkovsky 309
    case SC_CAPSLOCK:
508 jermar 310
        keyflags |= PRESSED_CAPSLOCK;
311
        break;
601 palkovsky 312
    case SC_SPEC_ESCAPE:
313
        break;
314
    case SC_LEFTARR:
315
        chardev_push_character(&kbrd, 0x1b);
316
        chardev_push_character(&kbrd, 0x5b);
317
        chardev_push_character(&kbrd, 0x44);
318
        break;
319
    case SC_RIGHTARR:
320
        chardev_push_character(&kbrd, 0x1b);
321
        chardev_push_character(&kbrd, 0x5b);
322
        chardev_push_character(&kbrd, 0x43);
323
        break;
324
    case SC_UPARR:
325
        chardev_push_character(&kbrd, 0x1b);
326
        chardev_push_character(&kbrd, 0x5b);
327
        chardev_push_character(&kbrd, 0x41);
328
        break;
329
    case SC_DOWNARR:
330
        chardev_push_character(&kbrd, 0x1b);
331
        chardev_push_character(&kbrd, 0x5b);
332
        chardev_push_character(&kbrd, 0x42);
333
        break;
606 palkovsky 334
    case SC_HOME:
335
        chardev_push_character(&kbrd, 0x1b);
336
        chardev_push_character(&kbrd, 0x4f);
337
        chardev_push_character(&kbrd, 0x48);
338
        break;
339
    case SC_END:
340
        chardev_push_character(&kbrd, 0x1b);
341
        chardev_push_character(&kbrd, 0x4f);
342
        chardev_push_character(&kbrd, 0x46);
343
        break;
344
    case SC_DELETE:
345
        chardev_push_character(&kbrd, 0x1b);
346
        chardev_push_character(&kbrd, 0x5b);
347
        chardev_push_character(&kbrd, 0x33);
348
        chardev_push_character(&kbrd, 0x7e);
349
        break;
601 palkovsky 350
    default:
517 jermar 351
            letter = is_lower(ascii);
508 jermar 352
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
353
        shift = keyflags & PRESSED_SHIFT;
354
        if (letter && capslock)
355
            shift = !shift;
356
        if (shift)
357
            map = sc_secondary_map;
511 jermar 358
        chardev_push_character(&kbrd, map[sc]);
508 jermar 359
        break;
360
    }
361
    spinlock_unlock(&keylock);
362
}
511 jermar 363
 
364
/* Called from getc(). */
575 palkovsky 365
void i8042_resume(chardev_t *d)
511 jermar 366
{
367
}
368
 
369
/* Called from getc(). */
575 palkovsky 370
void i8042_suspend(chardev_t *d)
511 jermar 371
{
372
}