Subversion Repositories HelenOS-historic

Rev

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

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