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