Subversion Repositories HelenOS

Rev

Rev 1842 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1842 Rev 1843
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
Line 29... Line 29...
29
/** @addtogroup genarch
29
/** @addtogroup genarch
30
 * @{
30
 * @{
31
 */
31
 */
32
/**
32
/**
33
 * @file
33
 * @file
34
 * @brief   i8042 processor driver.
34
 * @brief   Key processing.
35
 *
-
 
36
 * It takes care of low-level keyboard functions.
-
 
37
 */
35
 */
38
 
36
 
39
#include <genarch/kbd/i8042.h>
37
#include <genarch/kbd/key.h>
40
#include <genarch/kbd/scanc.h>
38
#include <genarch/kbd/scanc.h>
41
#include <genarch/kbd/scanc_pc.h>
39
#include <genarch/kbd/scanc_pc.h>
42
#include <arch/drivers/i8042.h>
-
 
43
#include <arch/interrupt.h>
-
 
44
#include <cpu.h>
-
 
45
#include <arch/asm.h>
-
 
46
#include <arch.h>
-
 
47
#include <synch/spinlock.h>
40
#include <synch/spinlock.h>
48
#include <typedefs.h>
-
 
49
#include <console/chardev.h>
41
#include <console/chardev.h>
50
#include <console/console.h>
42
#include <typedefs.h>
51
#include <macros.h>
43
#include <macros.h>
52
#include <interrupt.h>
-
 
53
 
-
 
54
/* Keyboard commands. */
-
 
55
#define KBD_ENABLE  0xf4
-
 
56
#define KBD_DISABLE 0xf5
-
 
57
#define KBD_ACK     0xfa
-
 
58
 
-
 
59
/*
-
 
60
 * 60  Write 8042 Command Byte: next data byte written to port 60h is
-
 
61
 *     placed in 8042 command register. Format:
-
 
62
 *
-
 
63
 *    |7|6|5|4|3|2|1|0|8042 Command Byte
-
 
64
 *     | | | | | | | `---- 1=enable output register full interrupt
-
 
65
 *     | | | | | | `----- should be 0
-
 
66
 *     | | | | | `------ 1=set status register system, 0=clear
-
 
67
 *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
-
 
68
 *     | | | `-------- disable keyboard I/O by driving clock line low
-
 
69
 *     | | `--------- disable auxiliary device, drives clock line low
-
 
70
 *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
-
 
71
 *     `----------- reserved, should be 0
-
 
72
 */
-
 
73
 
-
 
74
#define i8042_SET_COMMAND   0x60
-
 
75
#define i8042_COMMAND       0x69
-
 
76
 
-
 
77
#define i8042_BUFFER_FULL_MASK  0x01
-
 
78
#define i8042_WAIT_MASK     0x02
-
 
79
#define i8042_MOUSE_DATA        0x20
-
 
80
 
-
 
81
#define KEY_RELEASE 0x80
-
 
82
 
-
 
83
static void key_released(uint8_t sc);
-
 
84
static void key_pressed(uint8_t sc);
-
 
85
static char key_read(chardev_t *d);
-
 
86
 
44
 
87
#define PRESSED_SHIFT       (1<<0)
45
#define PRESSED_SHIFT       (1<<0)
88
#define PRESSED_CAPSLOCK    (1<<1)
46
#define PRESSED_CAPSLOCK    (1<<1)
89
#define LOCKED_CAPSLOCK     (1<<0)
47
#define LOCKED_CAPSLOCK     (1<<0)
90
 
48
 
Line 94... Line 52...
94
 
52
 
95
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
53
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
96
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
54
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
97
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
55
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
98
 
56
 
99
static void i8042_suspend(chardev_t *);
-
 
100
static void i8042_resume(chardev_t *);
-
 
101
 
-
 
102
static chardev_t kbrd;
-
 
103
static chardev_operations_t ops = {
-
 
104
    .suspend = i8042_suspend,
-
 
105
    .resume = i8042_resume,
-
 
106
    .read = key_read
-
 
107
};
-
 
108
 
-
 
109
static void i8042_interrupt(int n, istate_t *istate);
-
 
110
static void i8042_wait(void);
-
 
111
 
-
 
112
static iroutine oldvector;
-
 
113
/** Initialize keyboard and service interrupts using kernel routine */
-
 
114
void i8042_grab(void)
-
 
115
{
-
 
116
    oldvector = exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
-
 
117
    i8042_wait();
-
 
118
    i8042_command_write(i8042_SET_COMMAND);
-
 
119
    i8042_wait();
-
 
120
    i8042_data_write(i8042_COMMAND);
-
 
121
    i8042_wait();
-
 
122
}
-
 
123
/** Resume the former interrupt vector */
-
 
124
void i8042_release(void)
-
 
125
{
-
 
126
    if (oldvector)
-
 
127
        exc_register(VECTOR_KBD, "user_interrupt", oldvector);
-
 
128
}
-
 
129
 
-
 
130
/** Initialize i8042. */
-
 
131
void i8042_init(void)
-
 
132
{
-
 
133
    int i;
-
 
134
 
-
 
135
    i8042_grab();
-
 
136
        /* Prevent user from accidentaly releasing calling i8042_resume
-
 
137
     * and disabling keyboard
-
 
138
     */
-
 
139
    oldvector = NULL;
-
 
140
 
-
 
141
    trap_virtual_enable_irqs(1<<IRQ_KBD);
-
 
142
    chardev_initialize("i8042_kbd", &kbrd, &ops);
-
 
143
    stdin = &kbrd;
-
 
144
 
-
 
145
    /*
-
 
146
     * Clear input buffer.
-
 
147
     * Number of iterations is limited to prevent infinite looping.
-
 
148
     */
-
 
149
    for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
-
 
150
        i8042_data_read();
-
 
151
    }  
-
 
152
}
-
 
153
 
-
 
154
/** Process i8042 interrupt.
-
 
155
 *
-
 
156
 * @param n Interrupt vector.
-
 
157
 * @param istate Interrupted state.
-
 
158
 */
-
 
159
void i8042_interrupt(int n, istate_t *istate)
-
 
160
{
-
 
161
    uint8_t x;
-
 
162
    uint8_t status;
-
 
163
 
-
 
164
    while (((status=i8042_status_read()) & i8042_BUFFER_FULL_MASK)) {
-
 
165
        x = i8042_data_read();
-
 
166
 
-
 
167
        if ((status & i8042_MOUSE_DATA))
-
 
168
            continue;
-
 
169
 
-
 
170
        if (x & KEY_RELEASE)
-
 
171
            key_released(x ^ KEY_RELEASE);
-
 
172
        else
-
 
173
            key_pressed(x);
-
 
174
    }
-
 
175
    trap_virtual_eoi();
-
 
176
}
-
 
177
 
-
 
178
/** Wait until the controller reads its data. */
-
 
179
void i8042_wait(void) {
-
 
180
    while (i8042_status_read() & i8042_WAIT_MASK) {
-
 
181
        /* wait */
-
 
182
    }
-
 
183
}
-
 
184
 
-
 
185
/** Process release of key.
57
/** Process release of key.
186
 *
58
 *
187
 * @param sc Scancode of the key being released.
59
 * @param sc Scancode of the key being released.
188
 */
60
 */
189
void key_released(uint8_t sc)
61
void key_released(uint8_t sc)
Line 277... Line 149...
277
        break;
149
        break;
278
    }
150
    }
279
    spinlock_unlock(&keylock);
151
    spinlock_unlock(&keylock);
280
}
152
}
281
 
153
 
282
/* Called from getc(). */
-
 
283
void i8042_resume(chardev_t *d)
-
 
284
{
-
 
285
}
-
 
286
 
-
 
287
/* Called from getc(). */
-
 
288
void i8042_suspend(chardev_t *d)
-
 
289
{
-
 
290
}
-
 
291
 
-
 
292
static uint8_t active_read_buff_read(void)
154
uint8_t active_read_buff_read(void)
293
{
155
{
294
    static int i=0;
156
    static int i=0;
295
    i &= (ACTIVE_READ_BUFF_SIZE-1);
157
    i &= (ACTIVE_READ_BUFF_SIZE-1);
296
    if(!active_read_buff[i]) {
158
    if(!active_read_buff[i]) {
297
        return 0;
159
        return 0;
298
    }
160
    }
299
    return active_read_buff[i++];
161
    return active_read_buff[i++];
300
}
162
}
301
 
163
 
302
static void active_read_buff_write(uint8_t ch)
164
void active_read_buff_write(uint8_t ch)
303
{
165
{
304
    static int i=0;
166
    static int i=0;
305
    active_read_buff[i] = ch;
167
    active_read_buff[i] = ch;
306
    i++;
168
    i++;
307
    i &= (ACTIVE_READ_BUFF_SIZE-1);
169
    i &= (ACTIVE_READ_BUFF_SIZE-1);
308
    active_read_buff[i]=0;
170
    active_read_buff[i]=0;
309
}
171
}
310
 
172
 
311
 
173
 
312
static void active_read_key_pressed(uint8_t sc)
174
void active_read_key_pressed(uint8_t sc)
313
{
175
{
314
    char *map = sc_primary_map;
176
    char *map = sc_primary_map;
315
    char ascii = sc_primary_map[sc];
177
    char ascii = sc_primary_map[sc];
316
    bool shift, capslock;
178
    bool shift, capslock;
317
    bool letter = false;
179
    bool letter = false;
Line 376... Line 238...
376
    }
238
    }
377
    /*spinlock_unlock(&keylock);*/
239
    /*spinlock_unlock(&keylock);*/
378
 
240
 
379
}
241
}
380
 
242
 
381
static char key_read(chardev_t *d)
-
 
382
{
-
 
383
    char ch;   
-
 
384
 
-
 
385
    while(!(ch = active_read_buff_read())) {
-
 
386
        uint8_t x;
-
 
387
        while (!(i8042_status_read() & i8042_BUFFER_FULL_MASK))
-
 
388
            ;
-
 
389
        x = i8042_data_read();
-
 
390
        if (x & KEY_RELEASE)
-
 
391
            key_released(x ^ KEY_RELEASE);
-
 
392
        else
-
 
393
            active_read_key_pressed(x);
-
 
394
    }
-
 
395
    return ch;
-
 
396
}
-
 
397
 
-
 
398
/** Poll for key press and release events.
-
 
399
 *
-
 
400
 * This function can be used to implement keyboard polling.
-
 
401
 */
-
 
402
void i8042_poll(void)
-
 
403
{
-
 
404
    uint8_t x;
-
 
405
 
-
 
406
    while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
-
 
407
        x = i8042_data_read();
-
 
408
        if (x & KEY_RELEASE)
-
 
409
            key_released(x ^ KEY_RELEASE);
-
 
410
        else
-
 
411
            key_pressed(x);
-
 
412
    }
-
 
413
}
-
 
414
 
-
 
415
/** @}
243
/** @}
416
 */
244
 */