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