Subversion Repositories HelenOS-historic

Rev

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