source: mainline/kbd/arch/ia32/src/kbd.c@ e8d77a4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e8d77a4 was e8d77a4, checked in by Jakub Vana <jakub.vana@…>, 19 years ago

Keyboard esc esc esc shortcut to kconsole & gxemul patch to work return

  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
3 * Copyright (C) 2006 Josef Cejka
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <arch/kbd.h>
31#include <ipc/ipc.h>
32
33#define SPECIAL 255
34#define KEY_RELEASE 0x80
35
36/**
37 * These codes read from i8042 data register are silently ignored.
38 */
39#define IGNORE_CODE 0x7f
40
41#define PRESSED_SHIFT (1<<0)
42#define PRESSED_CAPSLOCK (1<<1)
43#define LOCKED_CAPSLOCK (1<<0)
44
45/** Scancodes. */
46#define SC_ESC 0x01
47#define SC_BACKSPACE 0x0e
48#define SC_LSHIFT 0x2a
49#define SC_RSHIFT 0x36
50#define SC_CAPSLOCK 0x3a
51#define SC_SPEC_ESCAPE 0xe0
52#define SC_LEFTARR 0x4b
53#define SC_RIGHTARR 0x4d
54#define SC_UPARR 0x48
55#define SC_DOWNARR 0x50
56#define SC_DELETE 0x53
57#define SC_HOME 0x47
58#define SC_END 0x4f
59
60#define FUNCTION_KEYS 0x100
61
62static volatile int keyflags; /**< Tracking of multiple keypresses. */
63static volatile int lockflags; /**< Tracking of multiple keys lockings. */
64
65/** Primary meaning of scancodes. */
66static int sc_primary_map[] = {
67 SPECIAL, /* 0x00 */
68 SPECIAL, /* 0x01 - Esc */
69 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
70 '\b', /* 0x0e - Backspace */
71 '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
72 SPECIAL, /* 0x1d - LCtrl */
73 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
74 '`',
75 SPECIAL, /* 0x2a - LShift */
76 '\\',
77 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
78 SPECIAL, /* 0x36 - RShift */
79 '*',
80 SPECIAL, /* 0x38 - LAlt */
81 ' ',
82 SPECIAL, /* 0x3a - CapsLock */
83 (FUNCTION_KEYS | 1), /* 0x3b - F1 */
84 (FUNCTION_KEYS | 2), /* 0x3c - F2 */
85 (FUNCTION_KEYS | 3), /* 0x3d - F3 */
86 (FUNCTION_KEYS | 4), /* 0x3e - F4 */
87 (FUNCTION_KEYS | 5), /* 0x3f - F5 */
88 (FUNCTION_KEYS | 6), /* 0x40 - F6 */
89 (FUNCTION_KEYS | 7), /* 0x41 - F7 */
90 (FUNCTION_KEYS | 8), /* 0x42 - F8 */
91 (FUNCTION_KEYS | 9), /* 0x43 - F9 */
92 (FUNCTION_KEYS | 10), /* 0x44 - F10 */
93 SPECIAL, /* 0x45 - NumLock */
94 SPECIAL, /* 0x46 - ScrollLock */
95 '7', '8', '9', '-',
96 '4', '5', '6', '+',
97 '1', '2', '3',
98 '0', '.',
99 SPECIAL, /* 0x54 - Alt-SysRq */
100 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
101 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
102 (FUNCTION_KEYS | 11), /* 0x57 - F11 */
103 (FUNCTION_KEYS | 12), /* 0x58 - F12 */
104 SPECIAL, /* 0x59 */
105 SPECIAL, /* 0x5a */
106 SPECIAL, /* 0x5b */
107 SPECIAL, /* 0x5c */
108 SPECIAL, /* 0x5d */
109 SPECIAL, /* 0x5e */
110 SPECIAL, /* 0x5f */
111 SPECIAL, /* 0x60 */
112 SPECIAL, /* 0x61 */
113 SPECIAL, /* 0x62 */
114 SPECIAL, /* 0x63 */
115 SPECIAL, /* 0x64 */
116 SPECIAL, /* 0x65 */
117 SPECIAL, /* 0x66 */
118 SPECIAL, /* 0x67 */
119 SPECIAL, /* 0x68 */
120 SPECIAL, /* 0x69 */
121 SPECIAL, /* 0x6a */
122 SPECIAL, /* 0x6b */
123 SPECIAL, /* 0x6c */
124 SPECIAL, /* 0x6d */
125 SPECIAL, /* 0x6e */
126 SPECIAL, /* 0x6f */
127 SPECIAL, /* 0x70 */
128 SPECIAL, /* 0x71 */
129 SPECIAL, /* 0x72 */
130 SPECIAL, /* 0x73 */
131 SPECIAL, /* 0x74 */
132 SPECIAL, /* 0x75 */
133 SPECIAL, /* 0x76 */
134 SPECIAL, /* 0x77 */
135 SPECIAL, /* 0x78 */
136 SPECIAL, /* 0x79 */
137 SPECIAL, /* 0x7a */
138 SPECIAL, /* 0x7b */
139 SPECIAL, /* 0x7c */
140 SPECIAL, /* 0x7d */
141 SPECIAL, /* 0x7e */
142 SPECIAL, /* 0x7f */
143};
144
145/** Secondary meaning of scancodes. */
146static int sc_secondary_map[] = {
147 SPECIAL, /* 0x00 */
148 0x1b, /* 0x01 - Esc */
149 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
150 SPECIAL, /* 0x0e - Backspace */
151 '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
152 SPECIAL, /* 0x1d - LCtrl */
153 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
154 '~',
155 SPECIAL, /* 0x2a - LShift */
156 '|',
157 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
158 SPECIAL, /* 0x36 - RShift */
159 '*',
160 SPECIAL, /* 0x38 - LAlt */
161 ' ',
162 SPECIAL, /* 0x3a - CapsLock */
163 SPECIAL, /* 0x3b - F1 */
164 SPECIAL, /* 0x3c - F2 */
165 SPECIAL, /* 0x3d - F3 */
166 SPECIAL, /* 0x3e - F4 */
167 SPECIAL, /* 0x3f - F5 */
168 SPECIAL, /* 0x40 - F6 */
169 SPECIAL, /* 0x41 - F7 */
170 SPECIAL, /* 0x42 - F8 */
171 SPECIAL, /* 0x43 - F9 */
172 SPECIAL, /* 0x44 - F10 */
173 SPECIAL, /* 0x45 - NumLock */
174 SPECIAL, /* 0x46 - ScrollLock */
175 '7', '8', '9', '-',
176 '4', '5', '6', '+',
177 '1', '2', '3',
178 '0', '.',
179 SPECIAL, /* 0x54 - Alt-SysRq */
180 SPECIAL, /* 0x55 - F11/F12/PF1/FN */
181 SPECIAL, /* 0x56 - unlabelled key next to LAlt */
182 SPECIAL, /* 0x57 - F11 */
183 SPECIAL, /* 0x58 - F12 */
184 SPECIAL, /* 0x59 */
185 SPECIAL, /* 0x5a */
186 SPECIAL, /* 0x5b */
187 SPECIAL, /* 0x5c */
188 SPECIAL, /* 0x5d */
189 SPECIAL, /* 0x5e */
190 SPECIAL, /* 0x5f */
191 SPECIAL, /* 0x60 */
192 SPECIAL, /* 0x61 */
193 SPECIAL, /* 0x62 */
194 SPECIAL, /* 0x63 */
195 SPECIAL, /* 0x64 */
196 SPECIAL, /* 0x65 */
197 SPECIAL, /* 0x66 */
198 SPECIAL, /* 0x67 */
199 SPECIAL, /* 0x68 */
200 SPECIAL, /* 0x69 */
201 SPECIAL, /* 0x6a */
202 SPECIAL, /* 0x6b */
203 SPECIAL, /* 0x6c */
204 SPECIAL, /* 0x6d */
205 SPECIAL, /* 0x6e */
206 SPECIAL, /* 0x6f */
207 SPECIAL, /* 0x70 */
208 SPECIAL, /* 0x71 */
209 SPECIAL, /* 0x72 */
210 SPECIAL, /* 0x73 */
211 SPECIAL, /* 0x74 */
212 SPECIAL, /* 0x75 */
213 SPECIAL, /* 0x76 */
214 SPECIAL, /* 0x77 */
215 SPECIAL, /* 0x78 */
216 SPECIAL, /* 0x79 */
217 SPECIAL, /* 0x7a */
218 SPECIAL, /* 0x7b */
219 SPECIAL, /* 0x7c */
220 SPECIAL, /* 0x7d */
221 SPECIAL, /* 0x7e */
222 SPECIAL, /* 0x7f */
223};
224
225irq_cmd_t i8042_cmds[1] = {
226 { CMD_PORT_READ_1, (void *)0x60, 0 }
227};
228
229irq_code_t i8042_kbd = {
230 1,
231 i8042_cmds
232};
233
234static int key_released(keybuffer_t *keybuffer, unsigned char key)
235{
236 switch (key) {
237 case SC_LSHIFT:
238 case SC_RSHIFT:
239 keyflags &= ~PRESSED_SHIFT;
240 break;
241 case SC_CAPSLOCK:
242 keyflags &= ~PRESSED_CAPSLOCK;
243 if (lockflags & LOCKED_CAPSLOCK)
244 lockflags &= ~LOCKED_CAPSLOCK;
245 else
246 lockflags |= LOCKED_CAPSLOCK;
247 break;
248 default:
249 break;
250 }
251}
252
253static int key_pressed(keybuffer_t *keybuffer, unsigned char key)
254{
255 int *map = sc_primary_map;
256 int ascii = sc_primary_map[key];
257 int shift, capslock;
258 int letter = 0;
259
260 static int esc_count=0;
261
262
263 if ( key == SC_ESC ) {
264 esc_count++;
265 if ( esc_count == 3 ) {
266 __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
267 }
268 } else {
269 esc_count=0;
270 }
271
272
273
274 switch (key) {
275 case SC_LSHIFT:
276 case SC_RSHIFT:
277 keyflags |= PRESSED_SHIFT;
278 break;
279 case SC_CAPSLOCK:
280 keyflags |= PRESSED_CAPSLOCK;
281 break;
282 case SC_SPEC_ESCAPE:
283 break;
284 /* case SC_LEFTARR:
285 if (keybuffer_available(keybuffer) >= 3) {
286 keybuffer_push(keybuffer, 0x1b);
287 keybuffer_push(keybuffer, 0x5b);
288 keybuffer_push(keybuffer, 0x44);
289 }
290 break;
291 case SC_RIGHTARR:
292 if (keybuffer_available(keybuffer) >= 3) {
293 keybuffer_push(keybuffer, 0x1b);
294 keybuffer_push(keybuffer, 0x5b);
295 keybuffer_push(keybuffer, 0x43);
296 }
297 break;
298 case SC_UPARR:
299 if (keybuffer_available(keybuffer) >= 3) {
300 keybuffer_push(keybuffer, 0x1b);
301 keybuffer_push(keybuffer, 0x5b);
302 keybuffer_push(keybuffer, 0x41);
303 }
304 break;
305 case SC_DOWNARR:
306 if (keybuffer_available(keybuffer) >= 3) {
307 keybuffer_push(keybuffer, 0x1b);
308 keybuffer_push(keybuffer, 0x5b);
309 keybuffer_push(keybuffer, 0x42);
310 }
311 break;
312 case SC_HOME:
313 if (keybuffer_available(keybuffer) >= 3) {
314 keybuffer_push(keybuffer, 0x1b);
315 keybuffer_push(keybuffer, 0x4f);
316 keybuffer_push(keybuffer, 0x48);
317 }
318 break;
319 case SC_END:
320 if (keybuffer_available(keybuffer) >= 3) {
321 keybuffer_push(keybuffer, 0x1b);
322 keybuffer_push(keybuffer, 0x4f);
323 keybuffer_push(keybuffer, 0x46);
324 }
325 break;
326 case SC_DELETE:
327 if (keybuffer_available(keybuffer) >= 4) {
328 keybuffer_push(keybuffer, 0x1b);
329 keybuffer_push(keybuffer, 0x5b);
330 keybuffer_push(keybuffer, 0x33);
331 keybuffer_push(keybuffer, 0x7e);
332 }
333 break;
334 */ default:
335 letter = ((ascii >= 'a') && (ascii <= 'z'));
336 capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
337 shift = keyflags & PRESSED_SHIFT;
338 if (letter && capslock)
339 shift = !shift;
340 if (shift)
341 map = sc_secondary_map;
342 if (map[key] != SPECIAL)
343 keybuffer_push(keybuffer, map[key]);
344 break;
345 }
346}
347
348/** Register uspace irq handler
349 * @return
350 */
351int kbd_arch_init(void)
352{
353 return !(ipc_register_irq(1, &i8042_kbd));
354}
355
356int kbd_arch_process(keybuffer_t *keybuffer, int scan_code)
357{
358 if (scan_code != IGNORE_CODE) {
359 if (scan_code & KEY_RELEASE)
360 key_released(keybuffer, scan_code ^ KEY_RELEASE);
361 else
362 key_pressed(keybuffer, scan_code);
363 }
364 return 1;
365}
Note: See TracBrowser for help on using the repository browser.