Subversion Repositories HelenOS-historic

Rev

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

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