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