Subversion Repositories HelenOS-historic

Rev

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

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