Subversion Repositories HelenOS-historic

Rev

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

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