xref: /titanic_52/usr/src/uts/common/io/kb8042/at_keyprocess.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/kbd.h>
32 #include <sys/kbtrans.h>
33 #include <sys/sunddi.h>
34 #include <sys/consdev.h>
35 #include <sys/promif.h>
36 #include "kb8042.h"
37 
38 /*
39  * A note on the SCAN_SET_2 #ifdef's:  This module was originally
40  * intended for handling Set 2 keyboard scan codes.  Now it has been
41  * switched to handle Set 1 keyboard scan codes.  The Set 2 tables
42  * and some other Set 2 specific elements have been preserved with
43  * these #ifdef's.  Also, the "break_prefix_received" variable and
44  * its surrounding structure were removed from the kb8042 struct,
45  * since Set 1 scan codes don't use this approach.
46  */
47 
48 /*
49  * Debugging for this module is enabled by the kb8042_debug flag
50  * defined in kb8042.c.  See that module for details.  This flag is
51  * hotkey enabled by F10 if the kb8042_enable_debug_hotkey flag is set.
52  */
53 
54 #if	defined(DEBUG) || defined(lint)
55 #define	KD_DEBUG
56 #endif
57 
58 /*
59  * A note on the use of prom_printf here:  Most of these routines can be
60  * called from "polled mode", where we're servicing I/O requests from kmdb.
61  * Normal system services are not available from polled mode; cmn_err will
62  * not work.  prom_printf is the only safe output mechanism.
63  */
64 
65 #if	defined(KD_DEBUG)
66 extern boolean_t kb8042_debug;
67 #define	DEBUG_KD(f)	{ if (kb8042_debug) prom_printf f; }
68 #else
69 #define	DEBUG_KD(f)	/* nothing */
70 #endif
71 
72 #define	KEYBAD		0xff		/* should generate an error */
73 #define	KEYIGN		0xfe		/* ignore this sequence */
74 
75 #define	KEY(code)	(code)
76 #define	INVALID		KEYBAD
77 #define	IGNORE		KEYIGN
78 
79 /*
80  * These are the states of our parsing machine:
81  */
82 #define	STATE_IDLE	0x10000000	/* Awaiting the start of a sequence */
83 #define	STATE_E0	0x100000e0	/* Rec'd an E0 */
84 #define	STATE_E1	0x100000e1	/* Rec'd an E1 (Pause key only) */
85 #define	STATE_E1_1D	0x1000e11d	/* Rec'd an E1 1D (Pause key only) */
86 #ifdef SCAN_SET_2
87 #define	STATE_E1_14	0x1000e114	/* Rec'd an E1 14 (Pause key only) */
88 #endif
89 
90 #define	NELEM(a)	(sizeof (a) / sizeof (a)[0])
91 
92 static const unsigned char	keytab_base[] = {
93 /* scan		key number	keycap */
94 /* 00 */	INVALID,
95 /* 01 */	KEY(110),	/* Esc */
96 /* 02 */	KEY(2),		/* 1 */
97 /* 03 */	KEY(3),		/* 2 */
98 /* 04 */	KEY(4),		/* 3 */
99 /* 05 */	KEY(5),		/* 4 */
100 /* 06 */	KEY(6),		/* 5 */
101 /* 07 */	KEY(7),		/* 6 */
102 /* 08 */	KEY(8),		/* 7 */
103 /* 09 */	KEY(9),		/* 8 */
104 /* 0a */	KEY(10),	/* 9 */
105 /* 0b */	KEY(11),	/* 0 */
106 /* 0c */	KEY(12),	/* - */
107 /* 0d */	KEY(13),	/* = */
108 /* 0e */	KEY(15),	/* backspace */
109 /* 0f */	KEY(16),	/* tab */
110 
111 /* 10 */	KEY(17),	/* Q */
112 /* 11 */	KEY(18),	/* W */
113 /* 12 */	KEY(19),	/* E */
114 /* 13 */	KEY(20),	/* R */
115 /* 14 */	KEY(21),	/* T */
116 /* 15 */	KEY(22),	/* Y */
117 /* 16 */	KEY(23),	/* U */
118 /* 17 */	KEY(24),	/* I */
119 /* 18 */	KEY(25),	/* O */
120 /* 19 */	KEY(26),	/* P */
121 /* 1a */	KEY(27),	/* [ */
122 /* 1b */	KEY(28),	/* ] */
123 /* 1c */	KEY(43),	/* Enter (main) */
124 /* 1d */	KEY(58),	/* L Ctrl */
125 /* 1e */	KEY(31),	/* A */
126 /* 1f */	KEY(32),	/* S */
127 
128 /* 20 */	KEY(33),	/* D */
129 /* 21 */	KEY(34),	/* F */
130 /* 22 */	KEY(35),	/* G */
131 /* 23 */	KEY(36),	/* H */
132 /* 24 */	KEY(37),	/* J */
133 /* 25 */	KEY(38),	/* K */
134 /* 26 */	KEY(39),	/* L */
135 /* 27 */	KEY(40),	/* ; */
136 /* 28 */	KEY(41),	/* ' */
137 /* 29 */	KEY(1),		/* ` */
138 /* 2a */	KEY(44),	/* L Shift */
139 /* 2b */	KEY(29),	/* \ */
140 /* 2c */	KEY(46),	/* Z */
141 /* 2d */	KEY(47),	/* X */
142 /* 2e */	KEY(48),	/* C */
143 /* 2f */	KEY(49),	/* V */
144 
145 /* 30 */	KEY(50),	/* B */
146 /* 31 */	KEY(51),	/* N */
147 /* 32 */	KEY(52),	/* M */
148 /* 33 */	KEY(53),	/* , */
149 /* 34 */	KEY(54),	/* . */
150 /* 35 */	KEY(55),	/* / */
151 /* 36 */	KEY(57),	/* R Shift */
152 /* 37 */	KEY(100),	/* * (num) */
153 /* 38 */	KEY(60),	/* L Alt */
154 /* 39 */	KEY(61),	/* Space */
155 /* 3a */	KEY(30),	/* CapsLock */
156 /* 3b */	KEY(112),	/* F1 */
157 /* 3c */	KEY(113),	/* F2 */
158 /* 3d */	KEY(114),	/* F3 */
159 /* 3e */	KEY(115),	/* F4 */
160 /* 3f */	KEY(116),	/* F5 */
161 
162 /* 40 */	KEY(117),	/* F6 */
163 /* 41 */	KEY(118),	/* F7 */
164 /* 42 */	KEY(119),	/* F8 */
165 /* 43 */	KEY(120),	/* F9 */
166 /* 44 */	KEY(121),	/* F10 */
167 /* 45 */	KEY(90),	/* NumLock */
168 /* 46 */	KEY(125),	/* Scroll Lock */
169 /* 47 */	KEY(91),	/* 7 (num) */
170 /* 48 */	KEY(96),	/* 8 (num) */
171 /* 49 */	KEY(101),	/* 9 (num) */
172 /* 4a */	KEY(105),	/* - (num) */
173 /* 4b */	KEY(92),	/* 4 (num) */
174 /* 4c */	KEY(97),	/* 5 (num) */
175 /* 4d */	KEY(102),	/* 6 (num) */
176 /* 4e */	KEY(106),	/* + (num) */
177 /* 4f */	KEY(93),	/* 1 (num) */
178 
179 /* 50 */	KEY(98),	/* 2 (num) */
180 /* 51 */	KEY(103),	/* 3 (num) */
181 /* 52 */	KEY(99),	/* 0 (num) */
182 /* 53 */	KEY(104),	/* . (num) */
183 /* 54 */	KEY(124),	/* PrintScreen (with Alt) */
184 /* 55 */	INVALID,
185 /* 56 */	KEY(45),	/* not labled (102-key only) */
186 /* 57 */	KEY(122),	/* F11 */
187 /* 58 */	KEY(123),	/* F12 */
188 /* 59 */	INVALID,
189 /* 5a */	INVALID,
190 /* 5b */	INVALID,
191 /* 5c */	INVALID,
192 /* 5d */	INVALID,
193 /* 5e */	INVALID,
194 /* 5f */	INVALID,
195 
196 /* 60 */	INVALID,
197 /* 61 */	INVALID,
198 /* 62 */	INVALID,
199 /* 63 */	INVALID,
200 /* 64 */	INVALID,
201 /* 65 */	INVALID,
202 /* 66 */	INVALID,
203 /* 67 */	INVALID,
204 /* 68 */	INVALID,
205 /* 69 */	INVALID,
206 /* 6a */	INVALID,
207 /* 6b */	INVALID,
208 /* 6c */	INVALID,
209 /* 6d */	INVALID,
210 /* 6e */	INVALID,
211 /* 6f */	INVALID,
212 
213 /* 70 */	KEY(133),	/* Japanese 106-key keyboard */
214 /* 71 */	INVALID,
215 /* 72 */	INVALID,
216 /* 73 */	KEY(56),	/* Japanese 106-key keyboard */
217 /* 74 */	INVALID,
218 /* 75 */	INVALID,
219 /* 76 */	INVALID,
220 /* 77 */	INVALID,
221 /* 78 */	INVALID,
222 /* 79 */	KEY(132),	/* Japanese 106-key keyboard */
223 /* 7a */	INVALID,
224 /* 7b */	KEY(131),	/* Japanese 106-key keyboard */
225 /* 7c */	INVALID,
226 /* 7d */	KEY(14),	/* Japanese 106-key keyboard */
227 /* 7e */	INVALID,
228 /* 7f */	INVALID,
229 };
230 
231 /*
232  * Parse table after receiving an E0 prefix code.
233  *
234  * Generally speaking, keys that were added on the 101-key keyboard are
235  * represented as an E0 followed by the code for an 84-key key.  Software
236  * ignorant of the 101-key keyboard ignores the E0 and so is handled
237  * compatibly.  Many of these variants involve "fake" shift presses
238  * and releases for compatibility; these are also prefixed with E0.
239  * We ignore these fake shifts.
240  */
241 static const unsigned char	keytab_e0[] = {
242 /* 00 */	INVALID,
243 /* 01 */	INVALID,
244 /* 02 */	INVALID,
245 /* 03 */	INVALID,
246 /* 04 */	INVALID,
247 /* 05 */	INVALID,
248 /* 06 */	INVALID,
249 /* 07 */	INVALID,
250 /* 08 */	INVALID,
251 /* 09 */	INVALID,
252 /* 0a */	INVALID,
253 /* 0b */	INVALID,
254 /* 0c */	INVALID,
255 /* 0d */	INVALID,
256 /* 0e */	INVALID,
257 /* 0f */	INVALID,
258 
259 /* 10 */	INVALID,
260 /* 11 */	INVALID,
261 /* 12 */	INVALID,
262 /* 13 */	INVALID,
263 /* 14 */	INVALID,
264 /* 15 */	INVALID,
265 /* 16 */	INVALID,
266 /* 17 */	INVALID,
267 /* 18 */	INVALID,
268 /* 19 */	INVALID,
269 /* 1a */	INVALID,
270 /* 1b */	INVALID,
271 /* 1c */	KEY(108),	/* Enter (num) */
272 /* 1d */	KEY(64),	/* R Ctrl */
273 /* 1e */	INVALID,
274 /* 1f */	INVALID,
275 
276 /* 20 */	INVALID,
277 /* 21 */	INVALID,
278 /* 22 */	INVALID,
279 /* 23 */	INVALID,
280 /* 24 */	INVALID,
281 /* 25 */	INVALID,
282 /* 26 */	INVALID,
283 /* 27 */	INVALID,
284 /* 28 */	INVALID,
285 /* 29 */	INVALID,
286 /* 2a */	INVALID,
287 /* 2b */	INVALID,
288 /* 2c */	INVALID,
289 /* 2d */	INVALID,
290 /* 2e */	INVALID,
291 /* 2f */	INVALID,
292 
293 /* 30 */	INVALID,
294 /* 31 */	INVALID,
295 /* 32 */	INVALID,
296 /* 33 */	INVALID,
297 /* 34 */	INVALID,
298 /* 35 */	KEY(95),	/* / (num) */
299 /* 36 */	INVALID,
300 /* 37 */	KEY(124),	/* PrintScreen (no Alt) */
301 /* 38 */	KEY(62),	/* R Alt */
302 /* 39 */	INVALID,
303 /* 3a */	INVALID,
304 /* 3b */	INVALID,
305 /* 3c */	INVALID,
306 /* 3d */	INVALID,
307 /* 3e */	INVALID,
308 /* 3f */	INVALID,
309 
310 /* 40 */	INVALID,
311 /* 41 */	INVALID,
312 /* 42 */	INVALID,
313 /* 43 */	INVALID,
314 /* 44 */	INVALID,
315 /* 45 */	INVALID,
316 /* 46 */	KEY(126),	/* Pause (with Cntl) */
317 /* 47 */	KEY(80),	/* Home (arrow) */
318 /* 48 */	KEY(83),	/* Up (arrow) */
319 /* 49 */	KEY(85),	/* PgUp (arrow) */
320 /* 4a */	INVALID,
321 /* 4b */	KEY(79),	/* Left (arrow) */
322 /* 4c */	INVALID,
323 /* 4d */	KEY(89),	/* Right (arrow) */
324 /* 4e */	INVALID,
325 /* 4f */	KEY(81),	/* End (arrow) */
326 
327 /* 50 */	KEY(84),	/* Down (arrow) */
328 /* 51 */	KEY(86),	/* PgDn (arrow) */
329 /* 52 */	KEY(75),	/* Insert (arrow) */
330 /* 53 */	KEY(76),	/* Delete (arrow) */
331 /* 54 */	INVALID,
332 /* 55 */	INVALID,
333 /* 56 */	INVALID,
334 /* 57 */	INVALID,
335 /* 58 */	INVALID,
336 /* 59 */	INVALID,
337 /* 5a */	INVALID,
338 /* 5b */	KEY(59),	/* L Window (104-key) */
339 /* 5c */	KEY(63),	/* R Window (104-key) */
340 /* 5d */	KEY(65),	/* Menu (104-key) */
341 /* 5e */	INVALID,
342 /* 5f */	INVALID,
343 
344 /* 60 */	INVALID,
345 /* 61 */	INVALID,
346 /* 62 */	INVALID,
347 /* 63 */	INVALID,
348 /* 64 */	INVALID,
349 /* 65 */	INVALID,
350 /* 66 */	INVALID,
351 /* 67 */	INVALID,
352 /* 68 */	INVALID,
353 /* 69 */	INVALID,
354 /* 6a */	INVALID,
355 /* 6b */	INVALID,
356 /* 6c */	INVALID,
357 /* 6d */	INVALID,
358 /* 6e */	INVALID,
359 /* 6f */	INVALID,
360 
361 /* 70 */	INVALID,
362 /* 71 */	INVALID,
363 /* 72 */	INVALID,
364 /* 73 */	INVALID,
365 /* 74 */	INVALID,
366 /* 75 */	INVALID,
367 /* 76 */	INVALID,
368 /* 77 */	INVALID,
369 /* 78 */	INVALID,
370 /* 79 */	INVALID,
371 /* 7a */	INVALID,
372 /* 7b */	INVALID,
373 /* 7c */	INVALID,
374 /* 7d */	INVALID,
375 /* 7e */	INVALID,
376 };
377 
378 
379 #ifdef SCAN_SET_2
380 /*
381  *	Parse table for the base keyboard state.  The index is the start of
382  *	a new sequence.
383  *
384  * Questionable or unusual cases:
385  * 02		Old kd code says F7. Manual says no.
386  * 7f		Old kd code says this is an 84-key SysReq.  Manual says no.
387  * 87		Old kd code says 1 (num).  Manual says no.
388  * 8c		Old kd code says / (num).  Manual says no.
389  * aa		POST OK.  Handled by code.
390  * e0		Extend prefix.  Handled by code. (switches to E0 table)
391  * e1		Extend prefix.  Handled by code.  (Pause key only)
392  * f0		Break prefix.  Handled by code.
393  * f1		Korean Hangul/Hanja key.  Handled by code.
394  * f2		Korean Hangul key.  Handled by code.
395  * ff		Keyboard internal buffer overrun.  Handled by code.
396  *
397  * Other values past the end of the table are treated as INVALID.
398  */
399 
400 static const unsigned char	keytab_base[] = {
401 /* scan		state		keycap */
402 /* 00 */	INVALID,
403 /* 01 */	KEY(120),	/* F9 */
404 /* 02 */	INVALID,	/* F7?  Old code says so but manual doesn't */
405 /* 03 */	KEY(116),	/* F5 */
406 /* 04 */	KEY(114),	/* F3 */
407 /* 05 */	KEY(112),	/* F1 */
408 /* 06 */	KEY(113),	/* F2 */
409 /* 07 */	KEY(123),	/* F12 */
410 /* 08 */	INVALID,
411 /* 09 */	KEY(121),	/* F10 */
412 /* 0a */	KEY(119),	/* F8 */
413 /* 0b */	KEY(117),	/* F6 */
414 /* 0c */	KEY(115),	/* F4 */
415 /* 0d */	KEY(16),	/* tab */
416 /* 0e */	KEY(1),		/* ` */
417 /* 0f */	INVALID,
418 /* 10 */	INVALID,
419 /* 11 */	KEY(60),	/* L Alt */
420 /* 12 */	KEY(44),	/* L Shift */
421 /* 13 */	KEY(133),	/* Japanese 106-key */
422 /* 14 */	KEY(58),	/* L Ctrl */
423 /* 15 */	KEY(17),	/* Q */
424 /* 16 */	KEY(2),		/* 1 */
425 /* 17 */	INVALID,
426 /* 18 */	INVALID,
427 /* 19 */	INVALID,
428 /* 1a */	KEY(46),	/* Z */
429 /* 1b */	KEY(32),	/* S */
430 /* 1c */	KEY(31),	/* A */
431 /* 1d */	KEY(18),	/* W */
432 /* 1e */	KEY(3),		/* 2 */
433 /* 1f */	INVALID,
434 /* 20 */	INVALID,
435 /* 21 */	KEY(48),	/* C */
436 /* 22 */	KEY(47),	/* X */
437 /* 23 */	KEY(33),	/* D */
438 /* 24 */	KEY(19),	/* E */
439 /* 25 */	KEY(5),		/* 4 */
440 /* 26 */	KEY(4),		/* 3 */
441 /* 27 */	INVALID,
442 /* 28 */	INVALID,
443 /* 29 */	KEY(61),	/* Space */
444 /* 2a */	KEY(49),	/* V */
445 /* 2b */	KEY(34),	/* F */
446 /* 2c */	KEY(21),	/* T */
447 /* 2d */	KEY(20),	/* R */
448 /* 2e */	KEY(6),		/* 5 */
449 /* 2f */	INVALID,
450 /* 30 */	INVALID,
451 /* 31 */	KEY(51),	/* N */
452 /* 32 */	KEY(50),	/* B */
453 /* 33 */	KEY(36),	/* H */
454 /* 34 */	KEY(35),	/* G */
455 /* 35 */	KEY(22),	/* Y */
456 /* 36 */	KEY(7),		/* 6 */
457 /* 37 */	INVALID,
458 /* 38 */	INVALID,
459 /* 39 */	INVALID,
460 /* 3a */	KEY(52),	/* M */
461 /* 3b */	KEY(37),	/* J */
462 /* 3c */	KEY(23),	/* U */
463 /* 3d */	KEY(8),		/* 7 */
464 /* 3e */	KEY(9),		/* 8 */
465 /* 3f */	INVALID,
466 /* 40 */	INVALID,
467 /* 41 */	KEY(53),	/* , */
468 /* 42 */	KEY(38),	/* K */
469 /* 43 */	KEY(24),	/* I */
470 /* 44 */	KEY(25),	/* O */
471 /* 45 */	KEY(11),	/* 0 */
472 /* 46 */	KEY(10),	/* 9 */
473 /* 47 */	INVALID,
474 /* 48 */	INVALID,
475 /* 49 */	KEY(54),	/* . */
476 /* 4a */	KEY(55),	/* / */
477 /* 4b */	KEY(39),	/* L */
478 /* 4c */	KEY(40),	/* ; */
479 /* 4d */	KEY(26),	/* P */
480 /* 4e */	KEY(12),	/* - */
481 /* 4f */	INVALID,
482 /* 50 */	INVALID,
483 /* 51 */	KEY(56),	/* Japanese 106-key */
484 /* 52 */	KEY(41),	/* ' */
485 /* 53 */	INVALID,
486 /* 54 */	KEY(27),	/* [ */
487 /* 55 */	KEY(13),	/* = */
488 /* 56 */	INVALID,
489 /* 57 */	INVALID,
490 /* 58 */	KEY(30),	/* CapsLock */
491 /* 59 */	KEY(57),	/* R Shift */
492 /* 5a */	KEY(43),	/* Enter (main) */
493 /* 5b */	KEY(28),	/* ] */
494 /* 5c */	INVALID,
495 /* 5d */	KEY(29),	/* \, key 42 for 102-key */
496 /* 5e */	INVALID,
497 /* 5f */	INVALID,
498 /* 60 */	INVALID,
499 /* 61 */	KEY(45),	/* 102-key only, typically </> */
500 /* 62 */	INVALID,
501 /* 63 */	INVALID,
502 /* 64 */	KEY(132),	/* Japanese 106-key */
503 /* 65 */	INVALID,
504 /* 66 */	KEY(15),	/* backspace */
505 /* 67 */	KEY(131),	/* Japanese 106-key */
506 /* 68 */	INVALID,
507 /* 69 */	KEY(93),	/* 1 (num) */
508 /* 6a */	KEY(14),	/* Japanese 106-key */
509 /* 6b */	KEY(92),	/* 4 (num) */
510 /* 6c */	KEY(91),	/* 7 (num) */
511 /* 6d */	INVALID,
512 /* 6e */	INVALID,
513 /* 6f */	INVALID,
514 /* 70 */	KEY(99),	/* 0 (num) */
515 /* 71 */	KEY(104),	/* . (num) */
516 /* 72 */	KEY(98),	/* 2 (num) */
517 /* 73 */	KEY(97),	/* 5 (num) */
518 /* 74 */	KEY(102),	/* 6 (num) */
519 /* 75 */	KEY(96),	/* 8 (num) */
520 /* 76 */	KEY(110),	/* Esc */
521 /* 77 */	KEY(90),	/* NumLock */
522 /* 78 */	KEY(122),	/* F11 */
523 /* 79 */	KEY(106),	/* + (num) */
524 /* 7a */	KEY(103),	/* 3 (num) */
525 /* 7b */	KEY(105),	/* - (num) */
526 /* 7c */	KEY(100),	/* * (num) */
527 /* 7d */	KEY(101),	/* 9 (num) */
528 /* 7e */	KEY(125),	/* Scroll Lock */
529 /* 7f */	INVALID,	/* 84-key SysReq?  Manual says no. */
530 /* 80 */	INVALID,
531 /* 81 */	INVALID,
532 /* 82 */	INVALID,
533 /* 83 */	KEY(118),	/* F7 */
534 /* 84 */	KEY(124),	/* PrintScreen (w/ Alt = SysRq) */
535 };
536 
537 /*
538  * Parse table after receiving an E0 prefix code.
539  *
540  * Generally speaking, keys that were added on the 101-key keyboard are
541  * represented as an E0 followed by the code for an 84-key key.  Software
542  * ignorant of the 101-key keyboard ignores the E0 and so is handled
543  * compatibly.  Many of these variants involve "fake" shift presses
544  * and releases for compatibility; these are also prefixed with E0.
545  * We ignore these fake shifts.
546  */
547 static const unsigned char	keytab_e0[] = {
548 /* 00 */	INVALID,
549 /* 01 */	INVALID,
550 /* 02 */	INVALID,
551 /* 03 */	INVALID,
552 /* 04 */	INVALID,
553 /* 05 */	INVALID,
554 /* 06 */	INVALID,
555 /* 07 */	INVALID,
556 /* 08 */	INVALID,
557 /* 09 */	INVALID,
558 /* 0a */	INVALID,
559 /* 0b */	INVALID,
560 /* 0c */	INVALID,
561 /* 0d */	INVALID,
562 /* 0e */	INVALID,
563 /* 0f */	INVALID,
564 /* 10 */	INVALID,
565 /* 11 */	KEY(62),	/* R Alt */
566 /* 12 */	IGNORE,		/* Fake L Shift */
567 /* 13 */	INVALID,
568 /* 14 */	KEY(64),	/* R Ctrl */
569 /* 15 */	INVALID,
570 /* 16 */	INVALID,
571 /* 17 */	INVALID,
572 /* 18 */	INVALID,
573 /* 19 */	INVALID,
574 /* 1a */	INVALID,
575 /* 1b */	INVALID,
576 /* 1c */	INVALID,
577 /* 1d */	INVALID,
578 /* 1e */	INVALID,
579 /* 1f */	KEY(59),	/* L Window (104-key) */
580 /* 20 */	INVALID,
581 /* 21 */	INVALID,
582 /* 22 */	INVALID,
583 /* 23 */	INVALID,
584 /* 24 */	INVALID,
585 /* 25 */	INVALID,
586 /* 26 */	INVALID,
587 /* 27 */	KEY(63),	/* R Window (104-key) */
588 /* 28 */	INVALID,
589 /* 29 */	INVALID,
590 /* 2a */	INVALID,
591 /* 2b */	INVALID,
592 /* 2c */	INVALID,
593 /* 2d */	INVALID,
594 /* 2e */	INVALID,
595 /* 2f */	KEY(65),	/* Menu (104-key) */
596 /* 30 */	INVALID,
597 /* 31 */	INVALID,
598 /* 32 */	INVALID,
599 /* 33 */	INVALID,
600 /* 34 */	INVALID,
601 /* 35 */	INVALID,
602 /* 36 */	INVALID,
603 /* 37 */	INVALID,
604 /* 38 */	INVALID,
605 /* 39 */	INVALID,
606 /* 3a */	INVALID,
607 /* 3b */	INVALID,
608 /* 3c */	INVALID,
609 /* 3d */	INVALID,
610 /* 3e */	INVALID,
611 /* 3f */	INVALID,
612 /* 40 */	INVALID,
613 /* 41 */	INVALID,
614 /* 42 */	INVALID,
615 /* 43 */	INVALID,
616 /* 44 */	INVALID,
617 /* 45 */	INVALID,
618 /* 46 */	INVALID,
619 /* 47 */	INVALID,
620 /* 48 */	INVALID,
621 /* 49 */	INVALID,
622 /* 4a */	KEY(95),	/* / (num) */
623 /* 4b */	INVALID,
624 /* 4c */	INVALID,
625 /* 4d */	INVALID,
626 /* 4e */	INVALID,
627 /* 4f */	INVALID,
628 /* 50 */	INVALID,
629 /* 51 */	INVALID,
630 /* 52 */	INVALID,
631 /* 53 */	INVALID,
632 /* 54 */	INVALID,
633 /* 55 */	INVALID,
634 /* 56 */	INVALID,
635 /* 57 */	INVALID,
636 /* 58 */	INVALID,
637 /* 59 */	IGNORE,		/* Fake R Shift */
638 /* 5a */	KEY(108),	/* Enter (num) */
639 /* 5b */	INVALID,
640 /* 5c */	INVALID,
641 /* 5d */	INVALID,
642 /* 5e */	INVALID,
643 /* 5f */	INVALID,
644 /* 60 */	INVALID,
645 /* 61 */	INVALID,
646 /* 62 */	INVALID,
647 /* 63 */	INVALID,
648 /* 64 */	INVALID,
649 /* 65 */	INVALID,
650 /* 66 */	INVALID,
651 /* 67 */	INVALID,
652 /* 68 */	INVALID,
653 /* 69 */	KEY(81),	/* End (arrow) */
654 /* 6a */	INVALID,
655 /* 6b */	KEY(79),	/* Left (arrow) */
656 /* 6c */	KEY(80),	/* Home (arrow) */
657 /* 6d */	INVALID,
658 /* 6e */	INVALID,
659 /* 6f */	INVALID,
660 /* 70 */	KEY(75),	/* Insert (arrow) */
661 /* 71 */	KEY(76),	/* Delete (arrow) */
662 /* 72 */	KEY(84),	/* Down (arrow) */
663 /* 73 */	INVALID,
664 /* 74 */	KEY(89),	/* Right (arrow) */
665 /* 75 */	KEY(83),	/* Up (arrow) */
666 /* 76 */	INVALID,
667 /* 77 */	INVALID,
668 /* 78 */	INVALID,
669 /* 79 */	INVALID,
670 /* 7a */	KEY(86),	/* PgDn (arrow) */
671 /* 7b */	INVALID,
672 /* 7c */	KEY(124),	/* PrintScreen (no Alt) */
673 /* 7d */	KEY(85),	/* PgUp (arrow) */
674 /* 7e */	KEY(126),	/* Pause (w/Ctrl = Break) */
675 };
676 
677 #endif /* SCAN_SET_2 */
678 
679 /*
680  * Initialize the translation state machine.
681  */
682 void
683 KeyboardConvertScan_init(struct kb8042 *kb8042)
684 {
685 	kb8042->parse_scan_state = STATE_IDLE;
686 }
687 
688 /*
689  *	KeyboardConvertScan(*kb8042, scan, *keynum, *state
690  *		*synthetic_release_needed)
691  *
692  *	State machine that takes scan codes from the keyboard and resolves
693  *	them to key numbers using the above tables.  Returns B_TRUE if this
694  *	scan code completes a scan code sequence, in which case "keynum",
695  *	"state", and "synthetic_release_needed" will be filled in correctly.
696  *
697  *	"synthetic_release_needed" is a hack to handle the additional two
698  *	keys on a Korean keyboard.  They report press only, so we tell the
699  *	upper layer to synthesize the release.
700  */
701 boolean_t
702 KeyboardConvertScan(
703     struct kb8042	*kb8042,
704     unsigned char	scan,
705     int			*keynum,
706     enum keystate	*state,
707     boolean_t		*synthetic_release_needed)
708 {
709 	DEBUG_KD(("KeyboardConvertScan: 0x%02x ", scan));
710 
711 	*synthetic_release_needed = B_FALSE;
712 	*state = KEY_PRESSED;
713 
714 	switch (scan) {
715 	/*
716 	 * First, handle special cases.
717 	 * ACK has already been handled by our caller.
718 	 */
719 
720 #ifdef SCAN_SET_2
721 	/*
722 	 * KAT_BREAK is 0xF0 and is not used for Set 1.   It is
723 	 * also the same as the break code for Japanese key 133.
724 	 * Therefore we don't treat it specially here.
725 	 */
726 	case KAT_BREAK:
727 		DEBUG_KD(("-> break prefix\n"));
728 		return (B_FALSE);	/* not a final keycode */
729 #endif
730 	case KB_ERROR:
731 		/*
732 		 * Perhaps we should reset state here,
733 		 * since we no longer know what's going on.
734 		 */
735 		DEBUG_KD(("-> overrun\n"));
736 		return (B_FALSE);
737 #ifdef SCAN_SET_2
738 	/*
739 	 * The KB_POST_OK code is 0xAA, which is the same as the Set 1
740 	 * break code for L-Shift.  Therefore, we don't treat it specially.
741 	 */
742 	case KB_POST_OK:
743 #endif
744 	case KB_POST_FAIL:
745 		/*
746 		 * Perhaps we should reset the LEDs now.
747 		 * If so, this check should probably be in the main line.
748 		 * Perhaps we should tell the higher layers that the
749 		 * keyboard has been reset.
750 		 */
751 		/*
752 		 * Reset to idle
753 		 */
754 		kb8042->parse_scan_state = STATE_IDLE;
755 		DEBUG_KD(("-> POST %s\n", scan == KB_POST_OK ? "OK" : "FAIL"));
756 		return (B_FALSE);
757 
758 	case KXT_EXTEND:
759 	case KXT_EXTEND2:
760 	case KXT_HANGUL_HANJA:
761 	case KXT_HANGUL:
762 		/*
763 		 * Exclude these keys from the "default" test below.
764 		 */
765 		break;
766 
767 	default:
768 		/*
769 		 * See if it was a key release.
770 		 */
771 		if (scan > 0x80) {
772 			*state = KEY_RELEASED;
773 			scan -= 0x80;
774 		}
775 		break;
776 	}
777 
778 	switch (kb8042->parse_scan_state) {
779 	case STATE_IDLE:
780 		switch (scan) {
781 		case KXT_EXTEND:
782 			kb8042->parse_scan_state = STATE_E0;
783 			DEBUG_KD(("-> state E0\n"));
784 			return (B_FALSE);
785 
786 		case KXT_EXTEND2:
787 			kb8042->parse_scan_state = STATE_E1;
788 			DEBUG_KD(("-> state E1\n"));
789 			return (B_FALSE);
790 
791 		/*
792 		 * We could do the next two in the table, but it would
793 		 * require nearly doubling the size of the table.
794 		 *
795 		 * Also, for some stupid reason these two report presses
796 		 * only.  We tell the upper layer to synthesize a release.
797 		 */
798 		case KXT_HANGUL_HANJA:
799 			*keynum = KEY(150);
800 			*synthetic_release_needed = B_TRUE;
801 			break;
802 
803 		case KXT_HANGUL:
804 			*keynum = KEY(151);
805 			*synthetic_release_needed = B_TRUE;
806 			break;
807 
808 		default:
809 			/*
810 			 * Regular scan code
811 			 */
812 			if (scan < NELEM(keytab_base))
813 				*keynum = keytab_base[scan];
814 			else
815 				*keynum = INVALID;
816 			break;
817 		}
818 		break;
819 
820 	case STATE_E0:		/* Mostly 101-key additions */
821 		if (scan < NELEM(keytab_e0))
822 			*keynum = keytab_e0[scan];
823 		else
824 			*keynum = INVALID;
825 		break;
826 
827 	case STATE_E1:		/* Pause key only */
828 		switch (scan) {
829 		case 0x1d:
830 			kb8042->parse_scan_state = STATE_E1_1D;
831 			DEBUG_KD(("-> state E1 1D\n"));
832 			return (B_FALSE);
833 #ifdef SCAN_SET_2
834 		case 0x14:
835 			kb8042->parse_scan_state = STATE_E1_14;
836 			DEBUG_KD(("-> state E1 14\n"));
837 			return (B_FALSE);
838 #endif
839 		default:
840 			*keynum = INVALID;
841 			break;
842 		}
843 		break;
844 
845 	case STATE_E1_1D:	/* Pause key only */
846 		switch (scan) {
847 		case 0x45:
848 			*keynum = KEY(126);	/* Pause */
849 			break;
850 		default:
851 			*keynum = INVALID;
852 			break;
853 		}
854 		break;
855 #ifdef SCAN_SET_2
856 	case STATE_E1_14:	/* Pause key only */
857 		switch (scan) {
858 		case 0x77:
859 			*keynum = KEY(126);	/* Pause */
860 			break;
861 		default:
862 			*keynum = INVALID;
863 			break;
864 		}
865 		break;
866 #endif
867 	}
868 
869 	/*
870 	 * The results (*keynum, *state, and *synthetic_release_needed)
871 	 * have been filled in, but they are valid only if we return
872 	 * B_TRUE which is only done below.  If we make it to here, we
873 	 * have completed a scan code sequence, so reset parse_scan_state.
874 	 */
875 
876 	kb8042->parse_scan_state = STATE_IDLE;
877 
878 	switch (*keynum) {
879 	case KEYIGN:				/* not a key, nor an error */
880 		DEBUG_KD(("-> hole -> ignored\n"));
881 		return (B_FALSE);		/* also not a final keycode */
882 
883 	case KEYBAD:		/* not part of a legit sequence? */
884 		DEBUG_KD(("-> bad -> ignored\n"));
885 		return (B_FALSE);	/* and return not a final keycode */
886 
887 	default:
888 		/*
889 		 * If we're here, it's a valid keycode.  We've already
890 		 * filled in the return values; return success.
891 		 */
892 
893 		DEBUG_KD(("-> %s keypos %d\n",
894 			*state == KEY_RELEASED ? "released" : "pressed",
895 			*keynum));
896 
897 		return (B_TRUE);		/* resolved to a key */
898 	}
899 }
900