xref: /titanic_50/usr/src/uts/common/io/kb8042/at_keyprocess.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
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 2005 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  * Debugging for this module is enabled by the kb8042_debug flag
40  * defined in kb8042.c.  See that module for details.  This flag is
41  * hotkey enabled by F10 if the kb8042_enable_debug_hotkey flag is set.
42  */
43 
44 #if	defined(DEBUG) || defined(lint)
45 #define	KD_DEBUG
46 #endif
47 
48 /*
49  * A note on the use of prom_printf here:  Most of these routines can be
50  * called from "polled mode", where we're servicing I/O requests from kmdb.
51  * Normal system services are not available from polled mode; cmn_err will
52  * not work.  prom_printf is the only safe output mechanism.
53  */
54 
55 #if	defined(KD_DEBUG)
56 extern boolean_t kb8042_debug;
57 #define	DEBUG_KD(f)	{ if (kb8042_debug) prom_printf f; }
58 #else
59 #define	DEBUG_KD(f)	/* nothing */
60 #endif
61 
62 #define	KEYBAD		0xff		/* should generate an error */
63 #define	KEYIGN		0xfe		/* ignore this sequence */
64 
65 #define	KEY(code)	(code)
66 #define	INVALID		KEYBAD
67 #define	IGNORE		KEYIGN
68 
69 #define	NELEM(a)	(sizeof (a) / sizeof (a)[0])
70 
71 /*
72  * These are the states of our parsing machine:
73  */
74 #define	STATE_IDLE	0x00000001 /* Awaiting the start of a sequence */
75 #define	STATE_E0	0x00000002 /* Rec'd an E0 */
76 #define	STATE_E1	0x00000004 /* Rec'd an E1 (Pause key only) */
77 #define	STATE_E1_1D	0x00000008 /* Rec'd an E1 1D (Pause key only) */
78 #define	STATE_E1_14	0x00000010 /* Rec'd an E1 14 (Pause key only) */
79 #define	STATE_E1_14_77			0x00000020
80 #define	STATE_E1_14_77_E1		0x00000040
81 #define	STATE_E1_14_77_E1_F0		0x00000080
82 #define	STATE_E1_14_77_E1_F0_14		0x00000100
83 #define	STATE_E1_14_77_E1_F0_14_F0	0x00000200
84 
85 static boolean_t KeyboardConvertScan_set1(struct kb8042	*, unsigned char, int *,
86     enum keystate *, boolean_t *);
87 static boolean_t KeyboardConvertScan_set2(struct kb8042	*, unsigned char, int *,
88     enum keystate *, boolean_t *);
89 
90 static const unsigned char *keytab_base = NULL;
91 static int keytab_base_length = 0;
92 static const unsigned char *keytab_e0 = NULL;
93 static int keytab_e0_length = 0;
94 static boolean_t (*KeyboardConvertScan_fn)(struct kb8042 *, unsigned char,
95     int *, enum keystate *, boolean_t *) = NULL;
96 
97 static const unsigned char	keytab_base_set1[] = {
98 /* scan		key number	keycap */
99 /* 00 */	INVALID,
100 /* 01 */	KEY(110),	/* Esc */
101 /* 02 */	KEY(2),		/* 1 */
102 /* 03 */	KEY(3),		/* 2 */
103 /* 04 */	KEY(4),		/* 3 */
104 /* 05 */	KEY(5),		/* 4 */
105 /* 06 */	KEY(6),		/* 5 */
106 /* 07 */	KEY(7),		/* 6 */
107 /* 08 */	KEY(8),		/* 7 */
108 /* 09 */	KEY(9),		/* 8 */
109 /* 0a */	KEY(10),	/* 9 */
110 /* 0b */	KEY(11),	/* 0 */
111 /* 0c */	KEY(12),	/* - */
112 /* 0d */	KEY(13),	/* = */
113 /* 0e */	KEY(15),	/* backspace */
114 /* 0f */	KEY(16),	/* tab */
115 
116 /* 10 */	KEY(17),	/* Q */
117 /* 11 */	KEY(18),	/* W */
118 /* 12 */	KEY(19),	/* E */
119 /* 13 */	KEY(20),	/* R */
120 /* 14 */	KEY(21),	/* T */
121 /* 15 */	KEY(22),	/* Y */
122 /* 16 */	KEY(23),	/* U */
123 /* 17 */	KEY(24),	/* I */
124 /* 18 */	KEY(25),	/* O */
125 /* 19 */	KEY(26),	/* P */
126 /* 1a */	KEY(27),	/* [ */
127 /* 1b */	KEY(28),	/* ] */
128 /* 1c */	KEY(43),	/* Enter (main) */
129 /* 1d */	KEY(58),	/* L Ctrl */
130 /* 1e */	KEY(31),	/* A */
131 /* 1f */	KEY(32),	/* S */
132 
133 /* 20 */	KEY(33),	/* D */
134 /* 21 */	KEY(34),	/* F */
135 /* 22 */	KEY(35),	/* G */
136 /* 23 */	KEY(36),	/* H */
137 /* 24 */	KEY(37),	/* J */
138 /* 25 */	KEY(38),	/* K */
139 /* 26 */	KEY(39),	/* L */
140 /* 27 */	KEY(40),	/* ; */
141 /* 28 */	KEY(41),	/* ' */
142 /* 29 */	KEY(1),		/* ` */
143 /* 2a */	KEY(44),	/* L Shift */
144 /* 2b */	KEY(29),	/* \ */
145 /* 2c */	KEY(46),	/* Z */
146 /* 2d */	KEY(47),	/* X */
147 /* 2e */	KEY(48),	/* C */
148 /* 2f */	KEY(49),	/* V */
149 
150 /* 30 */	KEY(50),	/* B */
151 /* 31 */	KEY(51),	/* N */
152 /* 32 */	KEY(52),	/* M */
153 /* 33 */	KEY(53),	/* , */
154 /* 34 */	KEY(54),	/* . */
155 /* 35 */	KEY(55),	/* / */
156 /* 36 */	KEY(57),	/* R Shift */
157 /* 37 */	KEY(100),	/* * (num) */
158 /* 38 */	KEY(60),	/* L Alt */
159 /* 39 */	KEY(61),	/* Space */
160 /* 3a */	KEY(30),	/* CapsLock */
161 /* 3b */	KEY(112),	/* F1 */
162 /* 3c */	KEY(113),	/* F2 */
163 /* 3d */	KEY(114),	/* F3 */
164 /* 3e */	KEY(115),	/* F4 */
165 /* 3f */	KEY(116),	/* F5 */
166 
167 /* 40 */	KEY(117),	/* F6 */
168 /* 41 */	KEY(118),	/* F7 */
169 /* 42 */	KEY(119),	/* F8 */
170 /* 43 */	KEY(120),	/* F9 */
171 /* 44 */	KEY(121),	/* F10 */
172 /* 45 */	KEY(90),	/* NumLock */
173 /* 46 */	KEY(125),	/* Scroll Lock */
174 /* 47 */	KEY(91),	/* 7 (num) */
175 /* 48 */	KEY(96),	/* 8 (num) */
176 /* 49 */	KEY(101),	/* 9 (num) */
177 /* 4a */	KEY(105),	/* - (num) */
178 /* 4b */	KEY(92),	/* 4 (num) */
179 /* 4c */	KEY(97),	/* 5 (num) */
180 /* 4d */	KEY(102),	/* 6 (num) */
181 /* 4e */	KEY(106),	/* + (num) */
182 /* 4f */	KEY(93),	/* 1 (num) */
183 
184 /* 50 */	KEY(98),	/* 2 (num) */
185 /* 51 */	KEY(103),	/* 3 (num) */
186 /* 52 */	KEY(99),	/* 0 (num) */
187 /* 53 */	KEY(104),	/* . (num) */
188 /* 54 */	KEY(124),	/* PrintScreen (with Alt) */
189 /* 55 */	INVALID,
190 /* 56 */	KEY(45),	/* not labled (102-key only) */
191 /* 57 */	KEY(122),	/* F11 */
192 /* 58 */	KEY(123),	/* F12 */
193 /* 59 */	INVALID,
194 /* 5a */	INVALID,
195 /* 5b */	INVALID,
196 /* 5c */	INVALID,
197 /* 5d */	INVALID,
198 /* 5e */	INVALID,
199 /* 5f */	INVALID,
200 
201 /* 60 */	INVALID,
202 /* 61 */	INVALID,
203 /* 62 */	INVALID,
204 /* 63 */	INVALID,
205 /* 64 */	INVALID,
206 /* 65 */	INVALID,
207 /* 66 */	INVALID,
208 /* 67 */	INVALID,
209 /* 68 */	INVALID,
210 /* 69 */	INVALID,
211 /* 6a */	INVALID,
212 /* 6b */	INVALID,
213 /* 6c */	INVALID,
214 /* 6d */	INVALID,
215 /* 6e */	INVALID,
216 /* 6f */	INVALID,
217 
218 /* 70 */	KEY(133),	/* Japanese 106-key keyboard */
219 /* 71 */	INVALID,
220 /* 72 */	INVALID,
221 /* 73 */	KEY(56),	/* Japanese 106-key keyboard */
222 /* 74 */	INVALID,
223 /* 75 */	INVALID,
224 /* 76 */	INVALID,
225 /* 77 */	INVALID,
226 /* 78 */	INVALID,
227 /* 79 */	KEY(132),	/* Japanese 106-key keyboard */
228 /* 7a */	INVALID,
229 /* 7b */	KEY(131),	/* Japanese 106-key keyboard */
230 /* 7c */	INVALID,
231 /* 7d */	KEY(14),	/* Japanese 106-key keyboard */
232 /* 7e */	INVALID,
233 /* 7f */	INVALID,
234 };
235 
236 /*
237  * Parse table after receiving an E0 prefix code.
238  *
239  * Generally speaking, keys that were added on the 101-key keyboard are
240  * represented as an E0 followed by the code for an 84-key key.  Software
241  * ignorant of the 101-key keyboard ignores the E0 and so is handled
242  * compatibly.  Many of these variants involve "fake" shift presses
243  * and releases for compatibility; these are also prefixed with E0.
244  * We ignore these fake shifts.
245  */
246 static const unsigned char	keytab_e0_set1[] = {
247 /* 00 */	INVALID,
248 /* 01 */	INVALID,
249 /* 02 */	INVALID,
250 /* 03 */	INVALID,
251 /* 04 */	INVALID,
252 /* 05 */	INVALID,
253 /* 06 */	INVALID,
254 /* 07 */	INVALID,
255 /* 08 */	INVALID,
256 /* 09 */	INVALID,
257 /* 0a */	INVALID,
258 /* 0b */	INVALID,
259 /* 0c */	INVALID,
260 /* 0d */	INVALID,
261 /* 0e */	INVALID,
262 /* 0f */	INVALID,
263 
264 /* 10 */	INVALID,
265 /* 11 */	INVALID,
266 /* 12 */	INVALID,
267 /* 13 */	INVALID,
268 /* 14 */	INVALID,
269 /* 15 */	INVALID,
270 /* 16 */	INVALID,
271 /* 17 */	INVALID,
272 /* 18 */	INVALID,
273 /* 19 */	INVALID,
274 /* 1a */	INVALID,
275 /* 1b */	INVALID,
276 /* 1c */	KEY(108),	/* Enter (num) */
277 /* 1d */	KEY(64),	/* R Ctrl */
278 /* 1e */	INVALID,
279 /* 1f */	INVALID,
280 
281 /* 20 */	INVALID,
282 /* 21 */	INVALID,
283 /* 22 */	INVALID,
284 /* 23 */	INVALID,
285 /* 24 */	INVALID,
286 /* 25 */	INVALID,
287 /* 26 */	INVALID,
288 /* 27 */	INVALID,
289 /* 28 */	INVALID,
290 /* 29 */	INVALID,
291 /* 2a */	INVALID,
292 /* 2b */	INVALID,
293 /* 2c */	INVALID,
294 /* 2d */	INVALID,
295 /* 2e */	INVALID,
296 /* 2f */	INVALID,
297 
298 /* 30 */	INVALID,
299 /* 31 */	INVALID,
300 /* 32 */	INVALID,
301 /* 33 */	INVALID,
302 /* 34 */	INVALID,
303 /* 35 */	KEY(95),	/* / (num) */
304 /* 36 */	INVALID,
305 /* 37 */	KEY(124),	/* PrintScreen (no Alt) */
306 /* 38 */	KEY(62),	/* R Alt */
307 /* 39 */	INVALID,
308 /* 3a */	INVALID,
309 /* 3b */	INVALID,
310 /* 3c */	INVALID,
311 /* 3d */	INVALID,
312 /* 3e */	INVALID,
313 /* 3f */	INVALID,
314 
315 /* 40 */	INVALID,
316 /* 41 */	INVALID,
317 /* 42 */	INVALID,
318 /* 43 */	INVALID,
319 /* 44 */	INVALID,
320 /* 45 */	INVALID,
321 /* 46 */	KEY(126),	/* Pause (with Cntl) */
322 /* 47 */	KEY(80),	/* Home (arrow) */
323 /* 48 */	KEY(83),	/* Up (arrow) */
324 /* 49 */	KEY(85),	/* PgUp (arrow) */
325 /* 4a */	INVALID,
326 /* 4b */	KEY(79),	/* Left (arrow) */
327 /* 4c */	INVALID,
328 /* 4d */	KEY(89),	/* Right (arrow) */
329 /* 4e */	INVALID,
330 /* 4f */	KEY(81),	/* End (arrow) */
331 
332 /* 50 */	KEY(84),	/* Down (arrow) */
333 /* 51 */	KEY(86),	/* PgDn (arrow) */
334 /* 52 */	KEY(75),	/* Insert (arrow) */
335 /* 53 */	KEY(76),	/* Delete (arrow) */
336 /* 54 */	INVALID,
337 /* 55 */	INVALID,
338 /* 56 */	INVALID,
339 /* 57 */	INVALID,
340 /* 58 */	INVALID,
341 /* 59 */	INVALID,
342 /* 5a */	INVALID,
343 /* 5b */	KEY(59),	/* L Window (104-key) */
344 /* 5c */	KEY(63),	/* R Window (104-key) */
345 /* 5d */	KEY(65),	/* Menu (104-key) */
346 /* 5e */	INVALID,
347 /* 5f */	INVALID,
348 
349 /* 60 */	INVALID,
350 /* 61 */	INVALID,
351 /* 62 */	INVALID,
352 /* 63 */	INVALID,
353 /* 64 */	INVALID,
354 /* 65 */	INVALID,
355 /* 66 */	INVALID,
356 /* 67 */	INVALID,
357 /* 68 */	INVALID,
358 /* 69 */	INVALID,
359 /* 6a */	INVALID,
360 /* 6b */	INVALID,
361 /* 6c */	INVALID,
362 /* 6d */	INVALID,
363 /* 6e */	INVALID,
364 /* 6f */	INVALID,
365 
366 /* 70 */	INVALID,
367 /* 71 */	INVALID,
368 /* 72 */	INVALID,
369 /* 73 */	INVALID,
370 /* 74 */	INVALID,
371 /* 75 */	INVALID,
372 /* 76 */	INVALID,
373 /* 77 */	INVALID,
374 /* 78 */	INVALID,
375 /* 79 */	INVALID,
376 /* 7a */	INVALID,
377 /* 7b */	INVALID,
378 /* 7c */	INVALID,
379 /* 7d */	INVALID,
380 /* 7e */	INVALID,
381 };
382 
383 
384 /*
385  *	Parse table for the base keyboard state.  The index is the start of
386  *	a new sequence.
387  *
388  * Questionable or unusual cases:
389  * 02		On some SPARC keyboards, this is the scan code for the STOP
390  *		key.  The KEY() value was chosen so that it maps to a
391  *		HOLE entry in the keytables in kb8042_keytables.c; therefore,
392  *		the STOP key code is only translated properly when kb8042
393  *		is "emulating" a USB keyboard (which it is by default--
394  *		see conskbd.c).
395  * 7f		Old kd code says this is an 84-key SysReq.  Manual says no.
396  * 87		Old kd code says 1 (num).  Manual says no.
397  * 8c		Old kd code says / (num).  Manual says no.
398  * aa		POST OK.  Handled by code.
399  * e0		Extend prefix.  Handled by code. (switches to E0 table)
400  * e1		Extend prefix.  Handled by code.  (Pause key only)
401  * f0		Break prefix.  Handled by code.
402  * f1		Korean Hangul/Hanja key.  Handled by code.
403  * f2		Korean Hangul key.  Handled by code.
404  * ff		Keyboard internal buffer overrun.  Handled by code.
405  *
406  * Other values past the end of the table are treated as INVALID.
407  */
408 
409 static const unsigned char	keytab_base_set2[] = {
410 /* scan		state		keycap */
411 /* 00 */	INVALID,
412 /* 01 */	KEY(120),	/* F9 */
413 #if defined(__sparc)
414 /* 02 */	KEY(K8042_STOP), /* STOP */
415 #else
416 /* 02 */	INVALID,	/* F7?  Old code says so but manual doesn't */
417 #endif
418 /* 03 */	KEY(116),	/* F5 */
419 /* 04 */	KEY(114),	/* F3 */
420 /* 05 */	KEY(112),	/* F1 */
421 /* 06 */	KEY(113),	/* F2 */
422 /* 07 */	KEY(123),	/* F12 */
423 /* 08 */	INVALID,
424 /* 09 */	KEY(121),	/* F10 */
425 /* 0a */	KEY(119),	/* F8 */
426 /* 0b */	KEY(117),	/* F6 */
427 /* 0c */	KEY(115),	/* F4 */
428 /* 0d */	KEY(16),	/* tab */
429 /* 0e */	KEY(1),		/* ` */
430 /* 0f */	INVALID,
431 /* 10 */	INVALID,
432 /* 11 */	KEY(60),	/* L Alt */
433 /* 12 */	KEY(44),	/* L Shift */
434 /* 13 */	KEY(133),	/* Japanese 106-key */
435 /* 14 */	KEY(58),	/* L Ctrl */
436 /* 15 */	KEY(17),	/* Q */
437 /* 16 */	KEY(2),		/* 1 */
438 /* 17 */	INVALID,
439 /* 18 */	INVALID,
440 /* 19 */	INVALID,
441 /* 1a */	KEY(46),	/* Z */
442 /* 1b */	KEY(32),	/* S */
443 /* 1c */	KEY(31),	/* A */
444 /* 1d */	KEY(18),	/* W */
445 /* 1e */	KEY(3),		/* 2 */
446 /* 1f */	INVALID,
447 /* 20 */	INVALID,
448 /* 21 */	KEY(48),	/* C */
449 /* 22 */	KEY(47),	/* X */
450 /* 23 */	KEY(33),	/* D */
451 /* 24 */	KEY(19),	/* E */
452 /* 25 */	KEY(5),		/* 4 */
453 /* 26 */	KEY(4),		/* 3 */
454 /* 27 */	INVALID,
455 /* 28 */	INVALID,
456 /* 29 */	KEY(61),	/* Space */
457 /* 2a */	KEY(49),	/* V */
458 /* 2b */	KEY(34),	/* F */
459 /* 2c */	KEY(21),	/* T */
460 /* 2d */	KEY(20),	/* R */
461 /* 2e */	KEY(6),		/* 5 */
462 /* 2f */	INVALID,
463 /* 30 */	INVALID,
464 /* 31 */	KEY(51),	/* N */
465 /* 32 */	KEY(50),	/* B */
466 /* 33 */	KEY(36),	/* H */
467 /* 34 */	KEY(35),	/* G */
468 /* 35 */	KEY(22),	/* Y */
469 /* 36 */	KEY(7),		/* 6 */
470 /* 37 */	INVALID,
471 /* 38 */	INVALID,
472 /* 39 */	INVALID,
473 /* 3a */	KEY(52),	/* M */
474 /* 3b */	KEY(37),	/* J */
475 /* 3c */	KEY(23),	/* U */
476 /* 3d */	KEY(8),		/* 7 */
477 /* 3e */	KEY(9),		/* 8 */
478 /* 3f */	INVALID,
479 /* 40 */	INVALID,
480 /* 41 */	KEY(53),	/* , */
481 /* 42 */	KEY(38),	/* K */
482 /* 43 */	KEY(24),	/* I */
483 /* 44 */	KEY(25),	/* O */
484 /* 45 */	KEY(11),	/* 0 */
485 /* 46 */	KEY(10),	/* 9 */
486 /* 47 */	INVALID,
487 /* 48 */	INVALID,
488 /* 49 */	KEY(54),	/* . */
489 /* 4a */	KEY(55),	/* / */
490 /* 4b */	KEY(39),	/* L */
491 /* 4c */	KEY(40),	/* ; */
492 /* 4d */	KEY(26),	/* P */
493 /* 4e */	KEY(12),	/* - */
494 /* 4f */	INVALID,
495 /* 50 */	INVALID,
496 /* 51 */	KEY(56),	/* Japanese 106-key */
497 /* 52 */	KEY(41),	/* ' */
498 /* 53 */	INVALID,
499 /* 54 */	KEY(27),	/* [ */
500 /* 55 */	KEY(13),	/* = */
501 /* 56 */	INVALID,
502 /* 57 */	INVALID,
503 /* 58 */	KEY(30),	/* CapsLock */
504 /* 59 */	KEY(57),	/* R Shift */
505 /* 5a */	KEY(43),	/* Enter (main) */
506 /* 5b */	KEY(28),	/* ] */
507 /* 5c */	INVALID,
508 /* 5d */	KEY(29),	/* \, key 42 for 102-key */
509 /* 5e */	INVALID,
510 /* 5f */	INVALID,
511 /* 60 */	INVALID,
512 /* 61 */	KEY(45),	/* 102-key only, typically </> */
513 /* 62 */	INVALID,
514 /* 63 */	INVALID,
515 /* 64 */	KEY(132),	/* Japanese 106-key */
516 /* 65 */	INVALID,
517 /* 66 */	KEY(15),	/* backspace */
518 /* 67 */	KEY(131),	/* Japanese 106-key */
519 /* 68 */	INVALID,
520 /* 69 */	KEY(93),	/* 1 (num) */
521 /* 6a */	KEY(14),	/* Japanese 106-key */
522 /* 6b */	KEY(92),	/* 4 (num) */
523 /* 6c */	KEY(91),	/* 7 (num) */
524 /* 6d */	INVALID,
525 /* 6e */	INVALID,
526 /* 6f */	INVALID,
527 /* 70 */	KEY(99),	/* 0 (num) */
528 /* 71 */	KEY(104),	/* . (num) */
529 /* 72 */	KEY(98),	/* 2 (num) */
530 /* 73 */	KEY(97),	/* 5 (num) */
531 /* 74 */	KEY(102),	/* 6 (num) */
532 /* 75 */	KEY(96),	/* 8 (num) */
533 /* 76 */	KEY(110),	/* Esc */
534 /* 77 */	KEY(90),	/* NumLock */
535 /* 78 */	KEY(122),	/* F11 */
536 /* 79 */	KEY(106),	/* + (num) */
537 /* 7a */	KEY(103),	/* 3 (num) */
538 /* 7b */	KEY(105),	/* - (num) */
539 /* 7c */	KEY(100),	/* * (num) */
540 /* 7d */	KEY(101),	/* 9 (num) */
541 /* 7e */	KEY(125),	/* Scroll Lock */
542 /* 7f */	INVALID,	/* 84-key SysReq?  Manual says no. */
543 /* 80 */	INVALID,
544 /* 81 */	INVALID,
545 /* 82 */	INVALID,
546 /* 83 */	KEY(118),	/* F7 */
547 /* 84 */	KEY(124),	/* PrintScreen (w/ Alt = SysRq) */
548 };
549 
550 /*
551  * Parse table after receiving an E0 prefix code.
552  *
553  * Generally speaking, keys that were added on the 101-key keyboard are
554  * represented as an E0 followed by the code for an 84-key key.  Software
555  * ignorant of the 101-key keyboard ignores the E0 and so is handled
556  * compatibly.  Many of these variants involve "fake" shift presses
557  * and releases for compatibility; these are also prefixed with E0.
558  * We ignore these fake shifts.
559  */
560 static const unsigned char	keytab_e0_set2[] = {
561 /* 00 */	INVALID,
562 /* 01 */	INVALID,
563 /* 02 */	INVALID,
564 /* 03 */	INVALID,
565 /* 04 */	INVALID,
566 /* 05 */	INVALID,
567 /* 06 */	INVALID,
568 /* 07 */	INVALID,
569 /* 08 */	INVALID,
570 /* 09 */	INVALID,
571 /* 0a */	INVALID,
572 /* 0b */	INVALID,
573 /* 0c */	INVALID,
574 /* 0d */	INVALID,
575 /* 0e */	INVALID,
576 /* 0f */	INVALID,
577 /* 10 */	INVALID,
578 /* 11 */	KEY(62),	/* R Alt */
579 /* 12 */	IGNORE,		/* Fake L Shift */
580 /* 13 */	INVALID,
581 /* 14 */	KEY(64),	/* R Ctrl */
582 /* 15 */	INVALID,
583 /* 16 */	INVALID,
584 /* 17 */	INVALID,
585 /* 18 */	INVALID,
586 /* 19 */	INVALID,
587 /* 1a */	INVALID,
588 /* 1b */	INVALID,
589 /* 1c */	INVALID,
590 /* 1d */	INVALID,
591 /* 1e */	INVALID,
592 /* 1f */	KEY(59),	/* L Window (104-key) */
593 /* 20 */	INVALID,
594 /* 21 */	INVALID,
595 /* 22 */	INVALID,
596 /* 23 */	INVALID,
597 /* 24 */	INVALID,
598 /* 25 */	INVALID,
599 /* 26 */	INVALID,
600 /* 27 */	KEY(63),	/* R Window (104-key) */
601 /* 28 */	INVALID,
602 /* 29 */	INVALID,
603 /* 2a */	INVALID,
604 /* 2b */	INVALID,
605 /* 2c */	INVALID,
606 /* 2d */	INVALID,
607 /* 2e */	INVALID,
608 /* 2f */	KEY(65),	/* Menu (104-key) */
609 /* 30 */	INVALID,
610 /* 31 */	INVALID,
611 /* 32 */	INVALID,
612 /* 33 */	INVALID,
613 /* 34 */	INVALID,
614 /* 35 */	INVALID,
615 /* 36 */	INVALID,
616 /* 37 */	INVALID,
617 /* 38 */	INVALID,
618 /* 39 */	INVALID,
619 /* 3a */	INVALID,
620 /* 3b */	INVALID,
621 /* 3c */	INVALID,
622 /* 3d */	INVALID,
623 /* 3e */	INVALID,
624 /* 3f */	INVALID,
625 /* 40 */	INVALID,
626 /* 41 */	INVALID,
627 /* 42 */	INVALID,
628 /* 43 */	INVALID,
629 /* 44 */	INVALID,
630 /* 45 */	INVALID,
631 /* 46 */	INVALID,
632 /* 47 */	INVALID,
633 /* 48 */	INVALID,
634 /* 49 */	INVALID,
635 /* 4a */	KEY(95),	/* / (num) */
636 /* 4b */	INVALID,
637 /* 4c */	INVALID,
638 /* 4d */	INVALID,
639 /* 4e */	INVALID,
640 /* 4f */	INVALID,
641 /* 50 */	INVALID,
642 /* 51 */	INVALID,
643 /* 52 */	INVALID,
644 /* 53 */	INVALID,
645 /* 54 */	INVALID,
646 /* 55 */	INVALID,
647 /* 56 */	INVALID,
648 /* 57 */	INVALID,
649 /* 58 */	INVALID,
650 /* 59 */	IGNORE,		/* Fake R Shift */
651 /* 5a */	KEY(108),	/* Enter (num) */
652 /* 5b */	INVALID,
653 /* 5c */	INVALID,
654 /* 5d */	INVALID,
655 /* 5e */	INVALID,
656 /* 5f */	INVALID,
657 /* 60 */	INVALID,
658 /* 61 */	INVALID,
659 /* 62 */	INVALID,
660 /* 63 */	INVALID,
661 /* 64 */	INVALID,
662 /* 65 */	INVALID,
663 /* 66 */	INVALID,
664 /* 67 */	INVALID,
665 /* 68 */	INVALID,
666 /* 69 */	KEY(81),	/* End (arrow) */
667 /* 6a */	INVALID,
668 /* 6b */	KEY(79),	/* Left (arrow) */
669 /* 6c */	KEY(80),	/* Home (arrow) */
670 /* 6d */	INVALID,
671 /* 6e */	INVALID,
672 /* 6f */	INVALID,
673 /* 70 */	KEY(75),	/* Insert (arrow) */
674 /* 71 */	KEY(76),	/* Delete (arrow) */
675 /* 72 */	KEY(84),	/* Down (arrow) */
676 /* 73 */	INVALID,
677 /* 74 */	KEY(89),	/* Right (arrow) */
678 /* 75 */	KEY(83),	/* Up (arrow) */
679 /* 76 */	INVALID,
680 /* 77 */	INVALID,
681 /* 78 */	INVALID,
682 /* 79 */	INVALID,
683 /* 7a */	KEY(86),	/* PgDn (arrow) */
684 /* 7b */	INVALID,
685 /* 7c */	KEY(124),	/* PrintScreen (no Alt) */
686 /* 7d */	KEY(85),	/* PgUp (arrow) */
687 /* 7e */	KEY(126),	/* Pause (w/Ctrl = Break) */
688 };
689 
690 
691 /*
692  * Initialize the translation state machine.
693  */
694 int
695 KeyboardConvertScan_init(struct kb8042 *kb8042, int scanset)
696 {
697 	kb8042->parse_scan_state = STATE_IDLE;
698 	kb8042->break_received = 0;
699 
700 	if (scanset == 1) {
701 		KeyboardConvertScan_fn = &KeyboardConvertScan_set1;
702 		keytab_base = keytab_base_set1;
703 		keytab_base_length = NELEM(keytab_base_set1);
704 		keytab_e0 = keytab_e0_set1;
705 		keytab_e0_length = NELEM(keytab_e0_set1);
706 	} else if (scanset == 2) {
707 		KeyboardConvertScan_fn = &KeyboardConvertScan_set2;
708 		keytab_base = keytab_base_set2;
709 		keytab_base_length = NELEM(keytab_base_set2);
710 		keytab_e0 = keytab_e0_set2;
711 		keytab_e0_length = NELEM(keytab_e0_set2);
712 	} else {
713 		return (DDI_FAILURE);
714 	}
715 
716 	return (DDI_SUCCESS);
717 }
718 
719 boolean_t
720 KeyboardConvertScan(
721     struct kb8042	*kb8042,
722     unsigned char	scan,
723     int			*keynum,
724     enum keystate	*state,
725     boolean_t		*synthetic_release_needed)
726 {
727 	ASSERT(KeyboardConvertScan_fn != NULL);
728 
729 	return ((*KeyboardConvertScan_fn)(kb8042, scan, keynum, state,
730 	    synthetic_release_needed));
731 }
732 
733 /*
734  *	KeyboardConvertScan(*kb8042, scan, *keynum, *state
735  *		*synthetic_release_needed)
736  *
737  *	State machine that takes scan codes from the keyboard and resolves
738  *	them to key numbers using the above tables.  Returns B_TRUE if this
739  *	scan code completes a scan code sequence, in which case "keynum",
740  *	"state", and "synthetic_release_needed" will be filled in correctly.
741  *
742  *	"synthetic_release_needed" is a hack to handle the additional two
743  *	keys on a Korean keyboard.  They report press only, so we tell the
744  *	upper layer to synthesize the release.
745  */
746 boolean_t
747 KeyboardConvertScan_set1(
748     struct kb8042	*kb8042,
749     unsigned char	scan,
750     int			*keynum,
751     enum keystate	*state,
752     boolean_t		*synthetic_release_needed)
753 {
754 	DEBUG_KD(("KeyboardConvertScan_set1: 0x%02x ", scan));
755 
756 	*synthetic_release_needed = B_FALSE;
757 	*state = KEY_PRESSED;
758 
759 	switch (scan) {
760 	/*
761 	 * First, handle special cases.
762 	 * ACK has already been handled by our caller.
763 	 */
764 	case KB_ERROR:
765 		/*
766 		 * Perhaps we should reset state here,
767 		 * since we no longer know what's going on.
768 		 */
769 		DEBUG_KD(("-> overrun\n"));
770 		return (B_FALSE);
771 	case KB_POST_FAIL:
772 		/*
773 		 * Perhaps we should reset the LEDs now.
774 		 * If so, this check should probably be in the main line.
775 		 * Perhaps we should tell the higher layers that the
776 		 * keyboard has been reset.
777 		 */
778 		/*
779 		 * Reset to idle
780 		 */
781 		kb8042->parse_scan_state = STATE_IDLE;
782 		DEBUG_KD(("-> POST %s\n", scan == KB_POST_OK ? "OK" : "FAIL"));
783 		return (B_FALSE);
784 
785 	case KXT_EXTEND:
786 	case KXT_EXTEND2:
787 	case KXT_HANGUL_HANJA:
788 	case KXT_HANGUL:
789 		/*
790 		 * Exclude these keys from the "default" test below.
791 		 */
792 		break;
793 
794 	default:
795 		/*
796 		 * See if it was a key release.
797 		 */
798 		if (scan > 0x80) {
799 			*state = KEY_RELEASED;
800 			scan -= 0x80;
801 		}
802 		break;
803 	}
804 
805 	if (kb8042->break_received) {
806 		*state = KEY_RELEASED;
807 		kb8042->break_received = 0;
808 	}
809 
810 	switch (kb8042->parse_scan_state) {
811 	case STATE_IDLE:
812 		switch (scan) {
813 		case KXT_EXTEND:
814 			kb8042->parse_scan_state = STATE_E0;
815 			DEBUG_KD(("-> state E0\n"));
816 			return (B_FALSE);
817 
818 		case KXT_EXTEND2:
819 			kb8042->parse_scan_state = STATE_E1;
820 			DEBUG_KD(("-> state E1\n"));
821 			return (B_FALSE);
822 
823 		/*
824 		 * We could do the next two in the table, but it would
825 		 * require nearly doubling the size of the table.
826 		 *
827 		 * Also, for some stupid reason these two report presses
828 		 * only.  We tell the upper layer to synthesize a release.
829 		 */
830 		case KXT_HANGUL_HANJA:
831 			*keynum = KEY(150);
832 			*synthetic_release_needed = B_TRUE;
833 			break;
834 
835 		case KXT_HANGUL:
836 			*keynum = KEY(151);
837 			*synthetic_release_needed = B_TRUE;
838 			break;
839 
840 		default:
841 			/*
842 			 * Regular scan code
843 			 */
844 			if (scan < keytab_base_length)
845 				*keynum = keytab_base[scan];
846 			else
847 				*keynum = INVALID;
848 			break;
849 		}
850 		break;
851 
852 	case STATE_E0:		/* Mostly 101-key additions */
853 		if (scan < keytab_e0_length)
854 			*keynum = keytab_e0[scan];
855 		else
856 			*keynum = INVALID;
857 		break;
858 
859 	case STATE_E1:		/* Pause key only */
860 		switch (scan) {
861 		case 0x1d:
862 			kb8042->parse_scan_state = STATE_E1_1D;
863 			DEBUG_KD(("-> state E1 1D\n"));
864 			return (B_FALSE);
865 		default:
866 			*keynum = INVALID;
867 			break;
868 		}
869 		break;
870 
871 	case STATE_E1_1D:	/* Pause key only */
872 		switch (scan) {
873 		case 0x45:
874 			*keynum = KEY(126);	/* Pause */
875 			break;
876 		default:
877 			*keynum = INVALID;
878 			break;
879 		}
880 		break;
881 	}
882 
883 	/*
884 	 * The results (*keynum, *state, and *synthetic_release_needed)
885 	 * have been filled in, but they are valid only if we return
886 	 * B_TRUE which is only done below.  If we make it to here, we
887 	 * have completed a scan code sequence, so reset parse_scan_state.
888 	 */
889 
890 	kb8042->parse_scan_state = STATE_IDLE;
891 
892 	switch (*keynum) {
893 	case KEYIGN:				/* not a key, nor an error */
894 		DEBUG_KD(("-> hole -> ignored\n"));
895 		return (B_FALSE);		/* also not a final keycode */
896 
897 	case KEYBAD:		/* not part of a legit sequence? */
898 		DEBUG_KD(("-> bad -> ignored\n"));
899 		return (B_FALSE);	/* and return not a final keycode */
900 
901 	default:
902 		/*
903 		 * If we're here, it's a valid keycode.  We've already
904 		 * filled in the return values; return success.
905 		 */
906 
907 		DEBUG_KD(("-> %s keypos %d\n",
908 			*state == KEY_RELEASED ? "released" : "pressed",
909 			*keynum));
910 
911 		return (B_TRUE);		/* resolved to a key */
912 	}
913 }
914 
915 /*
916  *	KeyboardConvertScan(*kb8042, scan, *keynum, *state
917  *		*synthetic_release_needed)
918  *
919  *	State machine that takes scan codes from the keyboard and resolves
920  *	them to key numbers using the above tables.  Returns B_TRUE if this
921  *	scan code completes a scan code sequence, in which case "keynum",
922  *	"state", and "synthetic_release_needed" will be filled in correctly.
923  *
924  *	"synthetic_release_needed" is a hack to handle the additional two
925  *	keys on a Korean keyboard.  They report press only, so we tell the
926  *	upper layer to synthesize the release.
927  */
928 boolean_t
929 KeyboardConvertScan_set2(
930     struct kb8042	*kb8042,
931     unsigned char	scan,
932     int			*keynum,
933     enum keystate	*state,
934     boolean_t		*synthetic_release_needed)
935 {
936 	DEBUG_KD(("KeyboardConvertScan_set2: 0x%02x ", scan));
937 
938 	*synthetic_release_needed = B_FALSE;
939 	*state = KEY_PRESSED;
940 
941 	switch (scan) {
942 	/*
943 	 * First, handle special cases.
944 	 * ACK has already been handled by our caller.
945 	 */
946 
947 	/*
948 	 * KAT_BREAK is 0xF0. It is the same as the break code for Japanese
949 	 * key 133.
950 	 * Therefore we don't treat it specially here.
951 	 */
952 	case KAT_BREAK:
953 		/* Switch states so we can recognize the code that follows */
954 		kb8042->break_received = 1;
955 		DEBUG_KD(("-> break prefix\n"));
956 		return (B_FALSE);	/* not a final keycode */
957 
958 	case KB_ERROR:
959 		/*
960 		 * Perhaps we should reset state here,
961 		 * since we no longer know what's going on.
962 		 */
963 		DEBUG_KD(("-> overrun\n"));
964 		return (B_FALSE);
965 
966 	case KB_POST_OK:
967 	case KB_POST_FAIL:
968 		/*
969 		 * Perhaps we should reset the LEDs now.
970 		 * If so, this check should probably be in the main line.
971 		 * Perhaps we should tell the higher layers that the
972 		 * keyboard has been reset.
973 		 */
974 		/*
975 		 * Reset to idle
976 		 */
977 		kb8042->parse_scan_state = STATE_IDLE;
978 		DEBUG_KD(("-> POST %s\n", scan == KB_POST_OK ? "OK" : "FAIL"));
979 		return (B_FALSE);
980 	}
981 
982 	if (kb8042->break_received) {
983 		*state = KEY_RELEASED;
984 		kb8042->break_received = 0;
985 	}
986 
987 	switch (kb8042->parse_scan_state) {
988 	case STATE_IDLE:
989 		switch (scan) {
990 		case KXT_EXTEND:
991 			kb8042->parse_scan_state = STATE_E0;
992 			DEBUG_KD(("-> state E0\n"));
993 			return (B_FALSE);
994 
995 		case KXT_EXTEND2:
996 			kb8042->parse_scan_state = STATE_E1;
997 			DEBUG_KD(("-> state E1\n"));
998 			return (B_FALSE);
999 
1000 		/*
1001 		 * We could do the next two in the table, but it would
1002 		 * require nearly doubling the size of the table.
1003 		 *
1004 		 * Also, for some stupid reason these two report presses
1005 		 * only.  We tell the upper layer to synthesize a release.
1006 		 */
1007 		case KXT_HANGUL_HANJA:
1008 			*keynum = KEY(150);
1009 			*synthetic_release_needed = B_TRUE;
1010 			break;
1011 
1012 		case KXT_HANGUL:
1013 			*keynum = KEY(151);
1014 			*synthetic_release_needed = B_TRUE;
1015 			break;
1016 
1017 		default:
1018 			/*
1019 			 * Regular scan code
1020 			 */
1021 			if (scan < keytab_base_length)
1022 				*keynum = keytab_base[scan];
1023 			else
1024 				*keynum = INVALID;
1025 			break;
1026 		}
1027 		break;
1028 
1029 	case STATE_E0:		/* Mostly 101-key additions */
1030 		if (scan < keytab_e0_length)
1031 			*keynum = keytab_e0[scan];
1032 		else
1033 			*keynum = INVALID;
1034 		break;
1035 
1036 	case STATE_E1:		/* Pause key only */
1037 		switch (scan) {
1038 		case 0x14:
1039 			kb8042->parse_scan_state = STATE_E1_14;
1040 			DEBUG_KD(("-> state E1 14\n"));
1041 			return (B_FALSE);
1042 		default:
1043 			*keynum = INVALID;
1044 			break;
1045 		}
1046 		break;
1047 
1048 	case STATE_E1_14:	/* Pause key only */
1049 		if (scan == 0x77) {
1050 			kb8042->parse_scan_state = STATE_E1_14_77;
1051 			return (B_FALSE);
1052 		} else {
1053 			*keynum = INVALID;
1054 		}
1055 		break;
1056 
1057 	case STATE_E1_14_77:
1058 		if (scan == 0xE1) {
1059 			kb8042->parse_scan_state = STATE_E1_14_77_E1;
1060 			return (B_FALSE);
1061 		} else {
1062 			*keynum = INVALID;
1063 		}
1064 		break;
1065 
1066 	case STATE_E1_14_77_E1:
1067 		if (scan == 0xF0) {
1068 			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0;
1069 			return (B_FALSE);
1070 		} else {
1071 			*keynum = INVALID;
1072 		}
1073 		break;
1074 
1075 	case STATE_E1_14_77_E1_F0:
1076 		if (scan == 0x14) {
1077 			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0_14;
1078 			return (B_FALSE);
1079 		} else {
1080 			*keynum = INVALID;
1081 		}
1082 		break;
1083 
1084 	case STATE_E1_14_77_E1_F0_14:
1085 		if (scan == 0xF0) {
1086 			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0_14_F0;
1087 			return (B_FALSE);
1088 		} else {
1089 			*keynum = INVALID;
1090 		}
1091 		break;
1092 
1093 	case STATE_E1_14_77_E1_F0_14_F0:
1094 		if (scan == 0x77) {
1095 			*keynum = KEY(126);	/* Pause */
1096 		} else {
1097 			*keynum = INVALID;
1098 		}
1099 		break;
1100 	}
1101 
1102 	/*
1103 	 * The results (*keynum, *state, and *synthetic_release_needed)
1104 	 * have been filled in, but they are valid only if we return
1105 	 * B_TRUE which is only done below.  If we make it to here, we
1106 	 * have completed a scan code sequence, so reset parse_scan_state.
1107 	 */
1108 
1109 	if (kb8042->break_received) {
1110 		*state = KEY_RELEASED;
1111 		kb8042->break_received = 0;
1112 	}
1113 
1114 	kb8042->parse_scan_state = STATE_IDLE;
1115 
1116 	switch (*keynum) {
1117 	case KEYIGN:				/* not a key, nor an error */
1118 		DEBUG_KD(("-> hole -> ignored\n"));
1119 		return (B_FALSE);		/* also not a final keycode */
1120 
1121 	case KEYBAD:		/* not part of a legit sequence? */
1122 		DEBUG_KD(("-> bad -> ignored\n"));
1123 		return (B_FALSE);	/* and return not a final keycode */
1124 
1125 	default:
1126 		/*
1127 		 * If we're here, it's a valid keycode.  We've already
1128 		 * filled in the return values; return success.
1129 		 */
1130 
1131 		DEBUG_KD(("-> %s keypos %d\n",
1132 			*state == KEY_RELEASED ? "released" : "pressed",
1133 			*keynum));
1134 
1135 		return (B_TRUE);		/* resolved to a key */
1136 	}
1137 }
1138