Subversion Repositories HelenOS-historic

Rev

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

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