Subversion Repositories HelenOS

Rev

Rev 1963 | 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
 
894 jermar 29
#include <genarch/i8042/i8042.h>
30
#include <arch/drivers/i8042.h>
1 jermar 31
#include <arch/interrupt.h>
32
#include <cpu.h>
33
#include <arch/asm.h>
34
#include <arch.h>
508 jermar 35
#include <synch/spinlock.h>
36
#include <typedefs.h>
511 jermar 37
#include <console/chardev.h>
38
#include <console/console.h>
517 jermar 39
#include <macros.h>
576 palkovsky 40
#include <interrupt.h>
1 jermar 41
 
508 jermar 42
/**
1 jermar 43
 * i8042 processor driver.
508 jermar 44
 * It takes care of low-level keyboard functions.
1 jermar 45
 */
46
 
512 jermar 47
/** Keyboard commands. */
48
#define KBD_ENABLE  0xf4
49
#define KBD_DISABLE 0xf5
50
#define KBD_ACK     0xfa
51
 
670 vana 52
/*
893 jermar 53
 * 60  Write 8042 Command Byte: next data byte written to port 60h is
895 jermar 54
 *     placed in 8042 command register. Format:
893 jermar 55
 *
56
 *    |7|6|5|4|3|2|1|0|8042 Command Byte
57
 *     | | | | | | | `---- 1=enable output register full interrupt
58
 *     | | | | | | `----- should be 0
59
 *     | | | | | `------ 1=set status register system, 0=clear
60
 *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
61
 *     | | | `-------- disable keyboard I/O by driving clock line low
62
 *     | | `--------- disable auxiliary device, drives clock line low
63
 *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
64
 *     `----------- reserved, should be 0
65
 */
670 vana 66
 
893 jermar 67
#define i8042_SET_COMMAND   0x60
68
#define i8042_COMMAND       0x49
894 jermar 69
 
70
#define i8042_BUFFER_FULL_MASK  0x01
893 jermar 71
#define i8042_WAIT_MASK     0x02
670 vana 72
 
508 jermar 73
#define SPECIAL     '?'
74
#define KEY_RELEASE 0x80
75
 
895 jermar 76
/**
77
 * These codes read from i8042 data register are silently ignored.
78
 */
79
#define IGNORE_CODE 0x7f
80
 
508 jermar 81
static void key_released(__u8 sc);
82
static void key_pressed(__u8 sc);
878 vana 83
static char key_read(chardev_t *d);
508 jermar 84
 
85
#define PRESSED_SHIFT       (1<<0)
86
#define PRESSED_CAPSLOCK    (1<<1)
87
#define LOCKED_CAPSLOCK     (1<<0)
88
 
894 jermar 89
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
878 vana 90
 
894 jermar 91
static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE];
878 vana 92
 
623 jermar 93
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
508 jermar 94
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
509 jermar 95
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
508 jermar 96
 
575 palkovsky 97
static void i8042_suspend(chardev_t *);
98
static void i8042_resume(chardev_t *);
511 jermar 99
 
100
static chardev_t kbrd;
101
static chardev_operations_t ops = {
102
    .suspend = i8042_suspend,
878 vana 103
    .resume = i8042_resume,
104
    .read = key_read
511 jermar 105
};
106
 
508 jermar 107
/** Primary meaning of scancodes. */
108
static char sc_primary_map[] = {
109
    SPECIAL, /* 0x00 */
110
    SPECIAL, /* 0x01 - Esc */
111
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
588 palkovsky 112
    '\b', /* 0x0e - Backspace */
508 jermar 113
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
114
    SPECIAL, /* 0x1d - LCtrl */
115
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
116
    '`',
117
    SPECIAL, /* 0x2a - LShift */
118
    '\\',
119
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
120
    SPECIAL, /* 0x36 - RShift */
121
    '*',
122
    SPECIAL, /* 0x38 - LAlt */
123
    ' ',
124
    SPECIAL, /* 0x3a - CapsLock */
125
    SPECIAL, /* 0x3b - F1 */
126
    SPECIAL, /* 0x3c - F2 */
127
    SPECIAL, /* 0x3d - F3 */
128
    SPECIAL, /* 0x3e - F4 */
129
    SPECIAL, /* 0x3f - F5 */
130
    SPECIAL, /* 0x40 - F6 */
131
    SPECIAL, /* 0x41 - F7 */
132
    SPECIAL, /* 0x42 - F8 */
133
    SPECIAL, /* 0x43 - F9 */
134
    SPECIAL, /* 0x44 - F10 */
135
    SPECIAL, /* 0x45 - NumLock */
136
    SPECIAL, /* 0x46 - ScrollLock */
137
    '7', '8', '9', '-',
138
    '4', '5', '6', '+',
139
    '1', '2', '3',
140
    '0', '.',
141
    SPECIAL, /* 0x54 - Alt-SysRq */
142
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
143
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
144
    SPECIAL, /* 0x57 - F11 */
145
    SPECIAL, /* 0x58 - F12 */
146
    SPECIAL, /* 0x59 */
147
    SPECIAL, /* 0x5a */
148
    SPECIAL, /* 0x5b */
149
    SPECIAL, /* 0x5c */
150
    SPECIAL, /* 0x5d */
151
    SPECIAL, /* 0x5e */
152
    SPECIAL, /* 0x5f */
153
    SPECIAL, /* 0x60 */
154
    SPECIAL, /* 0x61 */
155
    SPECIAL, /* 0x62 */
156
    SPECIAL, /* 0x63 */
157
    SPECIAL, /* 0x64 */
158
    SPECIAL, /* 0x65 */
159
    SPECIAL, /* 0x66 */
160
    SPECIAL, /* 0x67 */
161
    SPECIAL, /* 0x68 */
162
    SPECIAL, /* 0x69 */
163
    SPECIAL, /* 0x6a */
164
    SPECIAL, /* 0x6b */
165
    SPECIAL, /* 0x6c */
166
    SPECIAL, /* 0x6d */
167
    SPECIAL, /* 0x6e */
168
    SPECIAL, /* 0x6f */
169
    SPECIAL, /* 0x70 */
170
    SPECIAL, /* 0x71 */
171
    SPECIAL, /* 0x72 */
172
    SPECIAL, /* 0x73 */
173
    SPECIAL, /* 0x74 */
174
    SPECIAL, /* 0x75 */
175
    SPECIAL, /* 0x76 */
176
    SPECIAL, /* 0x77 */
177
    SPECIAL, /* 0x78 */
178
    SPECIAL, /* 0x79 */
179
    SPECIAL, /* 0x7a */
180
    SPECIAL, /* 0x7b */
181
    SPECIAL, /* 0x7c */
182
    SPECIAL, /* 0x7d */
183
    SPECIAL, /* 0x7e */
184
    SPECIAL, /* 0x7f */
185
};
186
 
187
/** Secondary meaning of scancodes. */
188
static char sc_secondary_map[] = {
189
    SPECIAL, /* 0x00 */
190
    SPECIAL, /* 0x01 - Esc */
191
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
192
    SPECIAL, /* 0x0e - Backspace */
193
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
194
    SPECIAL, /* 0x1d - LCtrl */
195
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
196
    '~',
197
    SPECIAL, /* 0x2a - LShift */
198
    '|',
199
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
200
    SPECIAL, /* 0x36 - RShift */
201
    '*',
202
    SPECIAL, /* 0x38 - LAlt */
203
    ' ',
204
    SPECIAL, /* 0x3a - CapsLock */
205
    SPECIAL, /* 0x3b - F1 */
206
    SPECIAL, /* 0x3c - F2 */
207
    SPECIAL, /* 0x3d - F3 */
208
    SPECIAL, /* 0x3e - F4 */
209
    SPECIAL, /* 0x3f - F5 */
210
    SPECIAL, /* 0x40 - F6 */
211
    SPECIAL, /* 0x41 - F7 */
212
    SPECIAL, /* 0x42 - F8 */
213
    SPECIAL, /* 0x43 - F9 */
214
    SPECIAL, /* 0x44 - F10 */
215
    SPECIAL, /* 0x45 - NumLock */
216
    SPECIAL, /* 0x46 - ScrollLock */
217
    '7', '8', '9', '-',
218
    '4', '5', '6', '+',
219
    '1', '2', '3',
220
    '0', '.',
221
    SPECIAL, /* 0x54 - Alt-SysRq */
222
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
223
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
224
    SPECIAL, /* 0x57 - F11 */
225
    SPECIAL, /* 0x58 - F12 */
226
    SPECIAL, /* 0x59 */
227
    SPECIAL, /* 0x5a */
228
    SPECIAL, /* 0x5b */
229
    SPECIAL, /* 0x5c */
230
    SPECIAL, /* 0x5d */
231
    SPECIAL, /* 0x5e */
232
    SPECIAL, /* 0x5f */
233
    SPECIAL, /* 0x60 */
234
    SPECIAL, /* 0x61 */
235
    SPECIAL, /* 0x62 */
236
    SPECIAL, /* 0x63 */
237
    SPECIAL, /* 0x64 */
238
    SPECIAL, /* 0x65 */
239
    SPECIAL, /* 0x66 */
240
    SPECIAL, /* 0x67 */
241
    SPECIAL, /* 0x68 */
242
    SPECIAL, /* 0x69 */
243
    SPECIAL, /* 0x6a */
244
    SPECIAL, /* 0x6b */
245
    SPECIAL, /* 0x6c */
246
    SPECIAL, /* 0x6d */
247
    SPECIAL, /* 0x6e */
248
    SPECIAL, /* 0x6f */
249
    SPECIAL, /* 0x70 */
250
    SPECIAL, /* 0x71 */
251
    SPECIAL, /* 0x72 */
252
    SPECIAL, /* 0x73 */
253
    SPECIAL, /* 0x74 */
254
    SPECIAL, /* 0x75 */
255
    SPECIAL, /* 0x76 */
256
    SPECIAL, /* 0x77 */
257
    SPECIAL, /* 0x78 */
258
    SPECIAL, /* 0x79 */
259
    SPECIAL, /* 0x7a */
260
    SPECIAL, /* 0x7b */
261
    SPECIAL, /* 0x7c */
262
    SPECIAL, /* 0x7d */
263
    SPECIAL, /* 0x7e */
264
    SPECIAL, /* 0x7f */
265
};
266
 
958 jermar 267
static void i8042_interrupt(int n, istate_t *istate);
895 jermar 268
static void i8042_wait(void);
576 palkovsky 269
 
508 jermar 270
/** Initialize i8042. */
1 jermar 271
void i8042_init(void)
272
{
1195 jermar 273
    int i;
274
 
958 jermar 275
    exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
895 jermar 276
    i8042_wait();
894 jermar 277
    i8042_command_write(i8042_SET_COMMAND);
895 jermar 278
    i8042_wait();
894 jermar 279
    i8042_data_write(i8042_COMMAND);
895 jermar 280
    i8042_wait();
670 vana 281
 
512 jermar 282
    trap_virtual_enable_irqs(1<<IRQ_KBD);
575 palkovsky 283
    chardev_initialize("i8042_kbd", &kbrd, &ops);
511 jermar 284
    stdin = &kbrd;
1195 jermar 285
 
1194 vana 286
    /*
1195 jermar 287
     * Clear input buffer.
288
     * Number of iterations is limited to prevent infinite looping.
289
     */
290
    for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
1194 vana 291
        i8042_data_read();
1195 jermar 292
    }  
1 jermar 293
}
294
 
508 jermar 295
/** Process i8042 interrupt.
296
 *
297
 * @param n Interrupt vector.
298
 * @param stack Interrupted stack.
299
 */
958 jermar 300
void i8042_interrupt(int n, istate_t *istate)
1 jermar 301
{
302
    __u8 x;
303
 
304
    trap_virtual_eoi();
894 jermar 305
    x = i8042_data_read();
508 jermar 306
    if (x & KEY_RELEASE)
307
        key_released(x ^ KEY_RELEASE);
308
    else
309
        key_pressed(x);
1 jermar 310
}
508 jermar 311
 
895 jermar 312
/** Wait until the controller reads its data. */
313
void i8042_wait(void) {
314
    while (i8042_status_read() & i8042_WAIT_MASK) {
315
        /* wait */
316
    }
317
}
318
 
508 jermar 319
/** Process release of key.
320
 *
321
 * @param sc Scancode of the key being released.
322
 */
323
void key_released(__u8 sc)
324
{
325
    spinlock_lock(&keylock);
326
    switch (sc) {
327
        case SC_LSHIFT:
328
        case SC_RSHIFT:
329
        keyflags &= ~PRESSED_SHIFT;
330
        break;
331
        case SC_CAPSLOCK:
332
        keyflags &= ~PRESSED_CAPSLOCK;
333
        if (lockflags & LOCKED_CAPSLOCK)
334
            lockflags &= ~LOCKED_CAPSLOCK;
335
        else
336
            lockflags |= LOCKED_CAPSLOCK;
337
        break;
338
        default:
339
        break;
340
    }
341
    spinlock_unlock(&keylock);
342
}
343
 
344
/** Process keypress.
345
 *
346
 * @param sc Scancode of the key being pressed.
347
 */
348
void key_pressed(__u8 sc)
349
{
350
    char *map = sc_primary_map;
351
    char ascii = sc_primary_map[sc];
352
    bool shift, capslock;
353
    bool letter = false;
354
 
355
    spinlock_lock(&keylock);
356
    switch (sc) {
601 palkovsky 357
    case SC_LSHIFT:
358
    case SC_RSHIFT:
508 jermar 359
            keyflags |= PRESSED_SHIFT;
360
        break;
601 palkovsky 361
    case SC_CAPSLOCK:
508 jermar 362
        keyflags |= PRESSED_CAPSLOCK;
363
        break;
601 palkovsky 364
    case SC_SPEC_ESCAPE:
365
        break;
366
    case SC_LEFTARR:
367
        chardev_push_character(&kbrd, 0x1b);
368
        chardev_push_character(&kbrd, 0x5b);
369
        chardev_push_character(&kbrd, 0x44);
370
        break;
371
    case SC_RIGHTARR:
372
        chardev_push_character(&kbrd, 0x1b);
373
        chardev_push_character(&kbrd, 0x5b);
374
        chardev_push_character(&kbrd, 0x43);
375
        break;
376
    case SC_UPARR:
377
        chardev_push_character(&kbrd, 0x1b);
378
        chardev_push_character(&kbrd, 0x5b);
379
        chardev_push_character(&kbrd, 0x41);
380
        break;
381
    case SC_DOWNARR:
382
        chardev_push_character(&kbrd, 0x1b);
383
        chardev_push_character(&kbrd, 0x5b);
384
        chardev_push_character(&kbrd, 0x42);
385
        break;
606 palkovsky 386
    case SC_HOME:
387
        chardev_push_character(&kbrd, 0x1b);
388
        chardev_push_character(&kbrd, 0x4f);
389
        chardev_push_character(&kbrd, 0x48);
390
        break;
391
    case SC_END:
392
        chardev_push_character(&kbrd, 0x1b);
393
        chardev_push_character(&kbrd, 0x4f);
394
        chardev_push_character(&kbrd, 0x46);
395
        break;
396
    case SC_DELETE:
397
        chardev_push_character(&kbrd, 0x1b);
398
        chardev_push_character(&kbrd, 0x5b);
399
        chardev_push_character(&kbrd, 0x33);
400
        chardev_push_character(&kbrd, 0x7e);
401
        break;
601 palkovsky 402
    default:
517 jermar 403
            letter = is_lower(ascii);
508 jermar 404
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
405
        shift = keyflags & PRESSED_SHIFT;
406
        if (letter && capslock)
407
            shift = !shift;
408
        if (shift)
409
            map = sc_secondary_map;
511 jermar 410
        chardev_push_character(&kbrd, map[sc]);
508 jermar 411
        break;
412
    }
413
    spinlock_unlock(&keylock);
414
}
511 jermar 415
 
416
/* Called from getc(). */
575 palkovsky 417
void i8042_resume(chardev_t *d)
511 jermar 418
{
419
}
420
 
421
/* Called from getc(). */
575 palkovsky 422
void i8042_suspend(chardev_t *d)
511 jermar 423
{
424
}
878 vana 425
 
426
static __u8 active_read_buff_read(void)
427
{
428
    static int i=0;
894 jermar 429
    i &= (ACTIVE_READ_BUFF_SIZE-1);
430
    if(!active_read_buff[i]) {
878 vana 431
        return 0;
432
    }
433
    return active_read_buff[i++];
434
}
435
 
436
static void active_read_buff_write(__u8 ch)
437
{
438
    static int i=0;
894 jermar 439
    active_read_buff[i] = ch;
878 vana 440
    i++;
894 jermar 441
    i &= (ACTIVE_READ_BUFF_SIZE-1);
878 vana 442
    active_read_buff[i]=0;
443
}
444
 
445
 
893 jermar 446
static void active_read_key_pressed(__u8 sc)
878 vana 447
{
448
    char *map = sc_primary_map;
449
    char ascii = sc_primary_map[sc];
450
    bool shift, capslock;
451
    bool letter = false;
452
 
453
    /*spinlock_lock(&keylock);*/
454
    switch (sc) {
455
    case SC_LSHIFT:
456
    case SC_RSHIFT:
457
            keyflags |= PRESSED_SHIFT;
458
        break;
459
    case SC_CAPSLOCK:
460
        keyflags |= PRESSED_CAPSLOCK;
461
        break;
462
    case SC_SPEC_ESCAPE:
463
        break;
464
    case SC_LEFTARR:
465
        active_read_buff_write(0x1b);
466
        active_read_buff_write(0x5b);
467
        active_read_buff_write(0x44);
468
        break;
469
    case SC_RIGHTARR:
470
        active_read_buff_write(0x1b);
471
        active_read_buff_write(0x5b);
472
        active_read_buff_write(0x43);
473
        break;
474
    case SC_UPARR:
475
        active_read_buff_write(0x1b);
476
        active_read_buff_write(0x5b);
477
        active_read_buff_write(0x41);
478
        break;
479
    case SC_DOWNARR:
480
        active_read_buff_write(0x1b);
481
        active_read_buff_write(0x5b);
482
        active_read_buff_write(0x42);
483
        break;
484
    case SC_HOME:
485
        active_read_buff_write(0x1b);
486
        active_read_buff_write(0x4f);
487
        active_read_buff_write(0x48);
488
        break;
489
    case SC_END:
490
        active_read_buff_write(0x1b);
491
        active_read_buff_write(0x4f);
492
        active_read_buff_write(0x46);
493
        break;
494
    case SC_DELETE:
495
        active_read_buff_write(0x1b);
496
        active_read_buff_write(0x5b);
497
        active_read_buff_write(0x33);
498
        active_read_buff_write(0x7e);
499
        break;
500
    default:
501
            letter = is_lower(ascii);
502
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
503
        shift = keyflags & PRESSED_SHIFT;
504
        if (letter && capslock)
505
            shift = !shift;
506
        if (shift)
507
            map = sc_secondary_map;
508
        active_read_buff_write(map[sc]);
509
        break;
510
    }
511
    /*spinlock_unlock(&keylock);*/
512
 
513
}
514
 
515
static char key_read(chardev_t *d)
516
{
517
    char ch;   
518
 
894 jermar 519
    while(!(ch = active_read_buff_read())) {
878 vana 520
        __u8 x;
894 jermar 521
        while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK)))
522
            ;
523
        x = i8042_data_read();
895 jermar 524
        if (x != IGNORE_CODE) {
525
            if (x & KEY_RELEASE)
526
                key_released(x ^ KEY_RELEASE);
527
            else
528
                active_read_key_pressed(x);
529
        }
878 vana 530
    }
531
    return ch;
532
}
895 jermar 533
 
534
/** Poll for key press and release events.
535
 *
536
 * This function can be used to implement keyboard polling.
537
 */
538
void i8042_poll(void)
539
{
540
    __u8 x;
541
 
542
    while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
543
        x = i8042_data_read();
544
        if (x != IGNORE_CODE) {
545
            if (x & KEY_RELEASE)
546
                key_released(x ^ KEY_RELEASE);
547
            else
548
                key_pressed(x);
549
        }
550
    }
551
}