Subversion Repositories HelenOS-historic

Rev

Rev 671 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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