Subversion Repositories HelenOS

Rev

Rev 511 | Rev 517 | 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>
1 jermar 40
 
508 jermar 41
/**
1 jermar 42
 * i8042 processor driver.
508 jermar 43
 * It takes care of low-level keyboard functions.
1 jermar 44
 */
45
 
512 jermar 46
#define i8042_DATA      0x60
47
#define i8042_STATUS        0x64
48
 
49
/** Keyboard commands. */
50
#define KBD_ENABLE  0xf4
51
#define KBD_DISABLE 0xf5
52
#define KBD_ACK     0xfa
53
 
508 jermar 54
#define SPECIAL     '?'
55
#define KEY_RELEASE 0x80
56
 
57
static void key_released(__u8 sc);
58
static void key_pressed(__u8 sc);
59
 
60
#define PRESSED_SHIFT       (1<<0)
61
#define PRESSED_CAPSLOCK    (1<<1)
62
#define LOCKED_CAPSLOCK     (1<<0)
63
 
509 jermar 64
static spinlock_t keylock;      /**< keylock protects keyflags and lockflags. */
508 jermar 65
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
509 jermar 66
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
508 jermar 67
 
511 jermar 68
static void i8042_suspend(void);
69
static void i8042_resume(void);
70
 
71
static chardev_t kbrd;
72
static chardev_operations_t ops = {
73
    .suspend = i8042_suspend,
74
    .resume = i8042_resume
75
};
76
 
508 jermar 77
/** Primary meaning of scancodes. */
78
static char sc_primary_map[] = {
79
    SPECIAL, /* 0x00 */
80
    SPECIAL, /* 0x01 - Esc */
81
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
82
    SPECIAL, /* 0x0e - Backspace */
83
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
84
    SPECIAL, /* 0x1d - LCtrl */
85
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
86
    '`',
87
    SPECIAL, /* 0x2a - LShift */
88
    '\\',
89
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
90
    SPECIAL, /* 0x36 - RShift */
91
    '*',
92
    SPECIAL, /* 0x38 - LAlt */
93
    ' ',
94
    SPECIAL, /* 0x3a - CapsLock */
95
    SPECIAL, /* 0x3b - F1 */
96
    SPECIAL, /* 0x3c - F2 */
97
    SPECIAL, /* 0x3d - F3 */
98
    SPECIAL, /* 0x3e - F4 */
99
    SPECIAL, /* 0x3f - F5 */
100
    SPECIAL, /* 0x40 - F6 */
101
    SPECIAL, /* 0x41 - F7 */
102
    SPECIAL, /* 0x42 - F8 */
103
    SPECIAL, /* 0x43 - F9 */
104
    SPECIAL, /* 0x44 - F10 */
105
    SPECIAL, /* 0x45 - NumLock */
106
    SPECIAL, /* 0x46 - ScrollLock */
107
    '7', '8', '9', '-',
108
    '4', '5', '6', '+',
109
    '1', '2', '3',
110
    '0', '.',
111
    SPECIAL, /* 0x54 - Alt-SysRq */
112
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
113
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
114
    SPECIAL, /* 0x57 - F11 */
115
    SPECIAL, /* 0x58 - F12 */
116
    SPECIAL, /* 0x59 */
117
    SPECIAL, /* 0x5a */
118
    SPECIAL, /* 0x5b */
119
    SPECIAL, /* 0x5c */
120
    SPECIAL, /* 0x5d */
121
    SPECIAL, /* 0x5e */
122
    SPECIAL, /* 0x5f */
123
    SPECIAL, /* 0x60 */
124
    SPECIAL, /* 0x61 */
125
    SPECIAL, /* 0x62 */
126
    SPECIAL, /* 0x63 */
127
    SPECIAL, /* 0x64 */
128
    SPECIAL, /* 0x65 */
129
    SPECIAL, /* 0x66 */
130
    SPECIAL, /* 0x67 */
131
    SPECIAL, /* 0x68 */
132
    SPECIAL, /* 0x69 */
133
    SPECIAL, /* 0x6a */
134
    SPECIAL, /* 0x6b */
135
    SPECIAL, /* 0x6c */
136
    SPECIAL, /* 0x6d */
137
    SPECIAL, /* 0x6e */
138
    SPECIAL, /* 0x6f */
139
    SPECIAL, /* 0x70 */
140
    SPECIAL, /* 0x71 */
141
    SPECIAL, /* 0x72 */
142
    SPECIAL, /* 0x73 */
143
    SPECIAL, /* 0x74 */
144
    SPECIAL, /* 0x75 */
145
    SPECIAL, /* 0x76 */
146
    SPECIAL, /* 0x77 */
147
    SPECIAL, /* 0x78 */
148
    SPECIAL, /* 0x79 */
149
    SPECIAL, /* 0x7a */
150
    SPECIAL, /* 0x7b */
151
    SPECIAL, /* 0x7c */
152
    SPECIAL, /* 0x7d */
153
    SPECIAL, /* 0x7e */
154
    SPECIAL, /* 0x7f */
155
};
156
 
157
/** Secondary meaning of scancodes. */
158
static char sc_secondary_map[] = {
159
    SPECIAL, /* 0x00 */
160
    SPECIAL, /* 0x01 - Esc */
161
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
162
    SPECIAL, /* 0x0e - Backspace */
163
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
164
    SPECIAL, /* 0x1d - LCtrl */
165
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
166
    '~',
167
    SPECIAL, /* 0x2a - LShift */
168
    '|',
169
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
170
    SPECIAL, /* 0x36 - RShift */
171
    '*',
172
    SPECIAL, /* 0x38 - LAlt */
173
    ' ',
174
    SPECIAL, /* 0x3a - CapsLock */
175
    SPECIAL, /* 0x3b - F1 */
176
    SPECIAL, /* 0x3c - F2 */
177
    SPECIAL, /* 0x3d - F3 */
178
    SPECIAL, /* 0x3e - F4 */
179
    SPECIAL, /* 0x3f - F5 */
180
    SPECIAL, /* 0x40 - F6 */
181
    SPECIAL, /* 0x41 - F7 */
182
    SPECIAL, /* 0x42 - F8 */
183
    SPECIAL, /* 0x43 - F9 */
184
    SPECIAL, /* 0x44 - F10 */
185
    SPECIAL, /* 0x45 - NumLock */
186
    SPECIAL, /* 0x46 - ScrollLock */
187
    '7', '8', '9', '-',
188
    '4', '5', '6', '+',
189
    '1', '2', '3',
190
    '0', '.',
191
    SPECIAL, /* 0x54 - Alt-SysRq */
192
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
193
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
194
    SPECIAL, /* 0x57 - F11 */
195
    SPECIAL, /* 0x58 - F12 */
196
    SPECIAL, /* 0x59 */
197
    SPECIAL, /* 0x5a */
198
    SPECIAL, /* 0x5b */
199
    SPECIAL, /* 0x5c */
200
    SPECIAL, /* 0x5d */
201
    SPECIAL, /* 0x5e */
202
    SPECIAL, /* 0x5f */
203
    SPECIAL, /* 0x60 */
204
    SPECIAL, /* 0x61 */
205
    SPECIAL, /* 0x62 */
206
    SPECIAL, /* 0x63 */
207
    SPECIAL, /* 0x64 */
208
    SPECIAL, /* 0x65 */
209
    SPECIAL, /* 0x66 */
210
    SPECIAL, /* 0x67 */
211
    SPECIAL, /* 0x68 */
212
    SPECIAL, /* 0x69 */
213
    SPECIAL, /* 0x6a */
214
    SPECIAL, /* 0x6b */
215
    SPECIAL, /* 0x6c */
216
    SPECIAL, /* 0x6d */
217
    SPECIAL, /* 0x6e */
218
    SPECIAL, /* 0x6f */
219
    SPECIAL, /* 0x70 */
220
    SPECIAL, /* 0x71 */
221
    SPECIAL, /* 0x72 */
222
    SPECIAL, /* 0x73 */
223
    SPECIAL, /* 0x74 */
224
    SPECIAL, /* 0x75 */
225
    SPECIAL, /* 0x76 */
226
    SPECIAL, /* 0x77 */
227
    SPECIAL, /* 0x78 */
228
    SPECIAL, /* 0x79 */
229
    SPECIAL, /* 0x7a */
230
    SPECIAL, /* 0x7b */
231
    SPECIAL, /* 0x7c */
232
    SPECIAL, /* 0x7d */
233
    SPECIAL, /* 0x7e */
234
    SPECIAL, /* 0x7f */
235
};
236
 
237
/** Initialize i8042. */
1 jermar 238
void i8042_init(void)
239
{
240
    trap_register(VECTOR_KBD, i8042_interrupt);
512 jermar 241
    trap_virtual_enable_irqs(1<<IRQ_KBD);
508 jermar 242
    spinlock_initialize(&keylock);
511 jermar 243
    chardev_initialize(&kbrd, &ops);
244
    stdin = &kbrd;
1 jermar 245
}
246
 
508 jermar 247
/** Process i8042 interrupt.
248
 *
249
 * @param n Interrupt vector.
250
 * @param stack Interrupted stack.
251
 */
206 palkovsky 252
void i8042_interrupt(__u8 n, __native stack[])
1 jermar 253
{
254
    __u8 x;
255
 
256
    trap_virtual_eoi();
512 jermar 257
    x = inb(i8042_DATA);
508 jermar 258
    if (x & KEY_RELEASE)
259
        key_released(x ^ KEY_RELEASE);
260
    else
261
        key_pressed(x);
1 jermar 262
}
508 jermar 263
 
264
/** Process release of key.
265
 *
266
 * @param sc Scancode of the key being released.
267
 */
268
void key_released(__u8 sc)
269
{
270
    spinlock_lock(&keylock);
271
    switch (sc) {
272
        case SC_LSHIFT:
273
        case SC_RSHIFT:
274
        keyflags &= ~PRESSED_SHIFT;
275
        break;
276
        case SC_CAPSLOCK:
277
        keyflags &= ~PRESSED_CAPSLOCK;
278
        if (lockflags & LOCKED_CAPSLOCK)
279
            lockflags &= ~LOCKED_CAPSLOCK;
280
        else
281
            lockflags |= LOCKED_CAPSLOCK;
282
        break;
283
        default:
284
        break;
285
    }
286
    spinlock_unlock(&keylock);
287
}
288
 
289
/** Process keypress.
290
 *
291
 * @param sc Scancode of the key being pressed.
292
 */
293
void key_pressed(__u8 sc)
294
{
295
    char *map = sc_primary_map;
296
    char ascii = sc_primary_map[sc];
297
    bool shift, capslock;
298
    bool letter = false;
299
 
300
    spinlock_lock(&keylock);
301
    switch (sc) {
302
        case SC_LSHIFT:
303
        case SC_RSHIFT:
304
            keyflags |= PRESSED_SHIFT;
305
        break;
306
        case SC_CAPSLOCK:
307
        keyflags |= PRESSED_CAPSLOCK;
308
        break;
309
        default:
310
            letter = (ascii >= 'a') && (ascii <= 'z');
311
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
312
        shift = keyflags & PRESSED_SHIFT;
313
        if (letter && capslock)
314
            shift = !shift;
315
        if (shift)
316
            map = sc_secondary_map;
511 jermar 317
        chardev_push_character(&kbrd, map[sc]);
508 jermar 318
        break;
319
    }
320
    spinlock_unlock(&keylock);
321
}
511 jermar 322
 
323
/* Called from getc(). */
324
void i8042_resume(void)
325
{
326
}
327
 
328
/* Called from getc(). */
329
void i8042_suspend(void)
330
{
331
}