xref: /illumos-gate/usr/src/uts/common/io/kb8042/kb8042.c (revision fd9cb95c)
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 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/inline.h>
36 #include <sys/termio.h>
37 #include <sys/stropts.h>
38 #include <sys/termios.h>
39 #include <sys/stream.h>
40 #include <sys/strtty.h>
41 #include <sys/strsubr.h>
42 #include <sys/strsun.h>
43 #include <sys/ddi.h>
44 #include <sys/sunddi.h>
45 #include <sys/note.h>
46 #include "sys/consdev.h"
47 #include <sys/kbd.h>
48 #include <sys/kbtrans.h>
49 #include "kb8042.h"
50 
51 #include <sys/i8042.h>
52 
53 #include "sys/kbio.h"		/* for KIOCSLAYOUT */
54 #include "sys/stat.h"
55 #include "sys/reboot.h"
56 #include <sys/promif.h>
57 #include <sys/beep.h>
58 
59 /*
60  * For any keyboard, there is a unique code describing the position
61  * of the key on a keyboard. We refer to the code as "station number".
62  * The following table is used to map the station numbers from ps2
63  * AT/XT keyboards to that of a USB one.
64  *
65  * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
66  * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
67  * when in KB_PC mode.  Note that this does not need to be made conditional
68  * on the architecture for which this module is complied because there are no
69  * keys that map to K8042_STOP on non-SPARC platforms.
70  */
71 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
72 /*  0 */	0,	53,	30,	31,	32,	33,	34,	35,
73 /*  8 */	36,	37,	38,	39,	45,	46,	137,	42,
74 /* 16 */	43,	20,	26,	8,	21,	23,	28,	24,
75 /* 24 */	12,	18,	19,	47,	48,	49,	57,	4,
76 /* 32 */	22,	7,	9,	10,	11,	13,	14,	15,
77 /* 40 */	51,	52,	50,	40,	225,	100,	29,	27,
78 /* 48 */	6,	25,	5,	17,	16,	54,	55,	56,
79 /* 56 */	135,	229,	224,	227,	226,	44,	230,	231,
80 /* 64 */	228,	101,	0,	0,	0,	0,	0,	0,
81 /* 72 */	0,	0,	0,	73,	76,	0,	0,	80,
82 /* 80 */	74,	77,	0,	82,	81,	75,	78,	0,
83 /* 88 */	0,	79,	83,	95,	92,	89,	0,	84,
84 /* 96 */	96,	93,	90,	98,	85,	97,	94,	91,
85 /* 104 */	99,	86,	87,	133,	88,	0,	41,	0,
86 /* 112 */	58,	59,	60,	61,	62,	63,	64,	65,
87 /* 120 */	66,	67,	68,	69,	70,	71,	72,	0,
88 /* 128 */	0,	0,	0,	139,	138,	136,	0,	0,
89 /* 136 */	0,	0,	0,	0,	0,	0,	0,	0,
90 /* 144 */	0,	0,	0,	0,	0,	0,	0,	0,
91 /* 152 */	0,	0,	0,	0,	0,	0,	0,	0,
92 /* 160 */	120,	0,	0,	0,	0,	0,	0,	0,
93 /* 168 */	0,	0,	0,	0,	0,	0,	0,	0,
94 /* 176 */	0,	0,	0,	0,	0,	0,	0,	0,
95 /* 184 */	0,	0,	0,	0,	0,	0,	0,	0,
96 /* 192 */	0,	0,	0,	0,	0,	0,	0,	0,
97 /* 200 */	0,	0,	0,	0,	0,	0,	0,	0,
98 /* 208 */	0,	0,	0,	0,	0,	0,	0,	0,
99 /* 216 */	0,	0,	0,	0,	0,	0,	0,	0,
100 /* 224 */	0,	0,	0,	0,	0,	0,	0,	0,
101 /* 232 */	0,	0,	0,	0,	0,	0,	0,	0,
102 /* 240 */	0,	0,	0,	0,	0,	0,	0,	0,
103 /* 248 */	0,	0,	0,	0
104 };
105 
106 /*
107  * DEBUG (or KD_DEBUG for just this module) turns on a flag called
108  * kb8042_enable_debug_hotkey.  If kb8042_enable_debug_hotkey is true,
109  * then the following hotkeys are enabled:
110  *    F10 - turn on debugging "normal" translations
111  *    F9  - turn on debugging "getchar" translations
112  *    F8  - turn on "low level" debugging
113  *    F7  - turn on terse press/release debugging
114  *    F1  - turn off all debugging
115  * The default value for kb8042_enable_debug_hotkey is false, disabling
116  * these hotkeys.
117  */
118 
119 #if	defined(DEBUG) || defined(lint)
120 #define	KD_DEBUG
121 #endif
122 
123 #ifdef	KD_DEBUG
124 boolean_t	kb8042_enable_debug_hotkey = B_FALSE;
125 boolean_t	kb8042_debug = B_FALSE;
126 boolean_t	kb8042_getchar_debug = B_FALSE;
127 boolean_t	kb8042_low_level_debug = B_FALSE;
128 boolean_t	kb8042_pressrelease_debug = B_FALSE;
129 static void kb8042_debug_hotkey(int scancode);
130 #endif
131 
132 #ifdef __sparc
133 #define	USECS_PER_WAIT 100
134 #define	MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
135 #define	MIN_DELAY_USECS USECS_PER_WAIT
136 
137 boolean_t kb8042_warn_unknown_scanset = B_TRUE;
138 int kb8042_default_scanset = 2;
139 
140 #endif
141 
142 enum state_return { STATE_NORMAL, STATE_INTERNAL };
143 
144 static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
145 static uint_t kb8042_intr(caddr_t arg);
146 static void kb8042_wait_poweron(struct kb8042 *kb8042);
147 static void kb8042_start_state_machine(struct kb8042 *, boolean_t);
148 static enum state_return kb8042_state_machine(struct kb8042 *, int, boolean_t);
149 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
150 static int kb8042_xlate_leds(int);
151 static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
152 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
153 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
154 static boolean_t kb8042_polled_keycheck(
155 			struct kbtrans_hardware *hw, int *key,
156 			enum keystate *state);
157 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
158 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
159 			enum keystate state);
160 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
161 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
162 static void kb8042_iocdatamsg(queue_t *, mblk_t *);
163 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
164 static int kb8042_polled_ischar(struct cons_polledio_arg *arg);
165 static int kb8042_polled_getchar(struct cons_polledio_arg *arg);
166 static void kb8042_cleanup(struct kb8042 *kb8042);
167 
168 static struct kbtrans_callbacks kb8042_callbacks = {
169 	kb8042_streams_setled,
170 	kb8042_polled_setled,
171 	kb8042_polled_keycheck,
172 };
173 
174 extern struct keyboard keyindex_pc;
175 
176 #define	DRIVER_NAME(dip) ddi_driver_name(dip)
177 
178 static	char	module_name[] = "kb8042";
179 
180 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
181 			cred_t *credp);
182 static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
183 static int kb8042_wsrv();
184 
185 struct module_info kb8042_sinfo = {
186 	42,		/* Module ID */
187 	module_name,
188 	0, 32,		/* Minimum and maximum packet sizes */
189 	256, 128	/* High and low water marks */
190 };
191 
192 static struct qinit kb8042_rinit = {
193 	NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
194 };
195 
196 static struct qinit kb8042_winit = {
197 	putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
198 };
199 
200 struct streamtab
201 	kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
202 
203 struct kb8042	Kdws = {0};
204 static dev_info_t *kb8042_dip = NULL;
205 
206 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
207 		void **result);
208 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
209 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
210 
211 static 	struct cb_ops cb_kb8042_ops = {
212 	nulldev,		/* cb_open */
213 	nulldev,		/* cb_close */
214 	nodev,			/* cb_strategy */
215 	nodev,			/* cb_print */
216 	nodev,			/* cb_dump */
217 	nodev,			/* cb_read */
218 	nodev,			/* cb_write */
219 	nodev,			/* cb_ioctl */
220 	nodev,			/* cb_devmap */
221 	nodev,			/* cb_mmap */
222 	nodev,			/* cb_segmap */
223 	nochpoll,		/* cb_chpoll */
224 	ddi_prop_op,		/* cb_prop_op */
225 	&kb8042_str_info,	/* cb_stream */
226 	D_MP
227 };
228 
229 struct dev_ops kb8042_ops = {
230 	DEVO_REV,		/* devo_rev */
231 	0,			/* devo_refcnt */
232 	kb8042_getinfo,		/* devo_getinfo */
233 	nulldev,		/* devo_identify */
234 	nulldev,		/* devo_probe */
235 	kb8042_attach,		/* devo_attach */
236 	kb8042_detach,		/* devo_detach */
237 	nodev,			/* devo_reset */
238 	&cb_kb8042_ops,		/* devo_cb_ops */
239 	(struct bus_ops *)NULL	/* devo_bus_ops */
240 };
241 
242 
243 /*
244  * This is the loadable module wrapper.
245  */
246 #include <sys/modctl.h>
247 
248 /*
249  * Module linkage information for the kernel.
250  */
251 static struct modldrv modldrv = {
252 	&mod_driverops, /* Type of module.  This one is a driver */
253 	"PS/2 Keyboard %I%, %E%",
254 	&kb8042_ops,	/* driver ops */
255 };
256 
257 static struct modlinkage modlinkage = {
258 	MODREV_1,
259 	(void *) &modldrv,
260 	NULL
261 };
262 
263 int
264 _init(void)
265 {
266 	int	rv;
267 
268 	rv = mod_install(&modlinkage);
269 	return (rv);
270 }
271 
272 
273 int
274 _fini(void)
275 {
276 	return (mod_remove(&modlinkage));
277 }
278 
279 int
280 _info(struct modinfo *modinfop)
281 {
282 	return (mod_info(&modlinkage, modinfop));
283 }
284 
285 #ifdef __sparc
286 static boolean_t
287 kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
288 {
289 	int i;
290 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
291 	    I8042_INT_INPUT_AVAIL;
292 	int reps = timeout_usec / USECS_PER_WAIT;
293 
294 	for (i = 0; i < reps; i++) {
295 		if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
296 			return (B_TRUE);
297 
298 		if (i < (reps - 1))
299 			drv_usecwait(USECS_PER_WAIT);
300 	}
301 	return (B_FALSE);
302 }
303 
304 static void
305 kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
306 {
307 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
308 	    I8042_INT_INPUT_DATA;
309 
310 	while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
311 		(void) ddi_get8(kb8042->handle, kb8042->addr + port);
312 	}
313 }
314 
315 static boolean_t
316 kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
317     boolean_t polled, int timeout, int *error, uint8_t *got)
318 {
319 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
320 	    I8042_INT_INPUT_DATA;
321 	uint8_t datab;
322 	int err;
323 	boolean_t rval;
324 
325 	kb8042_send_to_keyboard(kb8042, send, polled);
326 
327 	if (kb8042_is_input_avail(kb8042, timeout, polled)) {
328 		err = 0;
329 		datab = ddi_get8(kb8042->handle, kb8042->addr + port);
330 		rval = ((datab == expect) ? B_TRUE : B_FALSE);
331 	} else {
332 		err = EAGAIN;
333 		rval = B_FALSE;
334 	}
335 
336 	if (error != NULL)
337 		*error = err;
338 	if (got != NULL)
339 		*got = datab;
340 	return (rval);
341 }
342 
343 static const char *
344 kb8042_error_string(int errcode)
345 {
346 	switch (errcode) {
347 	case EAGAIN:
348 		return ("Timed out");
349 	default:
350 		return ("Unknown error");
351 	}
352 }
353 
354 static int
355 kb8042_read_scanset(struct kb8042 *kb8042, boolean_t polled)
356 {
357 	int scanset = -1;
358 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
359 	    I8042_INT_INPUT_DATA;
360 	int err;
361 	uint8_t got;
362 
363 	kb8042_clear_input_buffer(kb8042, B_TRUE);
364 
365 	/*
366 	 * Send a "change scan code set" command to the keyboard.
367 	 * It should respond with an ACK.
368 	 */
369 	if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, polled,
370 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
371 		goto fail_read_scanset;
372 	}
373 
374 	/*
375 	 * Send a 0.  The keyboard should ACK the 0, then it send the scan code
376 	 * set in use.
377 	 */
378 	if (kb8042_send_and_expect(kb8042, 0, KB_ACK, polled,
379 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
380 		goto fail_read_scanset;
381 	}
382 
383 	/*
384 	 * The next input byte from the keyboard should be the scan code
385 	 * set in use, though some keyboards like to send a few more acks
386 	 * just for fun, so blow past those to get the keyboard scan code.
387 	 */
388 	while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
389 		(scanset = ddi_get8(kb8042->handle, kb8042->addr + port))
390 		    == KB_ACK)
391 		;
392 
393 #ifdef KD_DEBUG
394 	cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
395 	    scanset);
396 #endif
397 
398 	return (scanset);
399 
400 fail_read_scanset:
401 #ifdef KD_DEBUG
402 	if (err == 0)
403 		cmn_err(CE_NOTE, "Could not read current scan set from "
404 		    "keyboard: %s. (Expected 0x%x, but got 0x%x).",
405 		    kb8042_error_string(err), KB_ACK, got);
406 	else
407 		cmn_err(CE_NOTE, "Could not read current scan set from "
408 		    "keyboard: %s.", kb8042_error_string(err));
409 #endif
410 	return (-1);
411 }
412 #endif
413 
414 static int
415 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
416 {
417 	int	rc;
418 	int	scanset;
419 	int	leds;
420 
421 	struct kb8042	*kb8042 = &Kdws;
422 	static ddi_device_acc_attr_t attr = {
423 		DDI_DEVICE_ATTR_V0,
424 		DDI_NEVERSWAP_ACC,
425 		DDI_STRICTORDER_ACC,
426 	};
427 
428 	switch (cmd) {
429 	case DDI_RESUME:
430 		leds = kb8042->leds.commanded;
431 		kb8042->w_init = 0;
432 		kb8042_init(kb8042, B_TRUE);
433 		kb8042_setled(kb8042, leds, B_FALSE);
434 		return (DDI_SUCCESS);
435 
436 	case DDI_ATTACH:
437 		if (kb8042_dip != NULL)
438 			return (DDI_FAILURE);
439 		/* The rest of the function is for attach */
440 		break;
441 
442 	default:
443 		return (DDI_FAILURE);
444 	}
445 
446 	kb8042->debugger.mod1 = 58;	/* Left Ctrl */
447 	kb8042->debugger.mod2 = 60;	/* Left Alt */
448 	kb8042->debugger.trigger = 33;	/* D */
449 	kb8042->debugger.mod1_down = B_FALSE;
450 	kb8042->debugger.mod2_down = B_FALSE;
451 	kb8042->debugger.enabled = B_FALSE;
452 
453 	kb8042_dip = devi;
454 	kb8042->init_state = KB8042_UNINITIALIZED;
455 
456 	kb8042->polled_synthetic_release_pending = B_FALSE;
457 
458 	if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
459 		    DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
460 		goto failure;
461 	}
462 
463 	kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
464 
465 	rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
466 		(offset_t)0, (offset_t)0, &attr, &kb8042->handle);
467 	if (rc != DDI_SUCCESS) {
468 #if	defined(KD_DEBUG)
469 		cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
470 #endif
471 		goto failure;
472 	}
473 
474 	kb8042->init_state |= KB8042_REGS_MAPPED;
475 
476 	if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
477 		DDI_SUCCESS) {
478 		cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
479 		goto failure;
480 	}
481 
482 	mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
483 
484 	kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
485 
486 	kb8042_init(kb8042, B_FALSE);
487 
488 #ifdef __sparc
489 	/* Detect the scan code set currently in use */
490 	scanset = kb8042_read_scanset(kb8042, B_TRUE);
491 
492 	if (scanset < 0 && kb8042_warn_unknown_scanset) {
493 
494 		cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
495 		cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
496 		cmn_err(CE_CONT, "Defaulting to scan code set %d.  If the "
497 		    "keyboard does not ", kb8042_default_scanset);
498 		cmn_err(CE_CONT, "work properly, add "
499 		    "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
500 		    (kb8042_default_scanset == 1) ? 2 : 1);
501 		cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
502 		    "restart the system.  If you ");
503 		cmn_err(CE_CONT, "do not want to see this message in the "
504 		    "future, add ");
505 		cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
506 		    "to /etc/system.\n");
507 
508 		/* Use the default scan code set. */
509 		scanset = kb8042_default_scanset;
510 	}
511 #else
512 	/* x86 systems use scan code set 1 -- no detection required */
513 	scanset = 1;
514 #endif
515 	if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
516 		cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
517 		    "Unknown scan code set `%d'.", scanset);
518 		/* Scan code set is not supported */
519 		goto failure;
520 	}
521 
522 	/*
523 	 * Turn on interrupts...
524 	 */
525 	if (ddi_add_intr(devi, 0,
526 		&kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
527 		kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
528 		cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
529 		goto failure;
530 	}
531 
532 	kb8042->init_state |= KB8042_INTR_ADDED;
533 
534 	ddi_report_dev(devi);
535 
536 #ifdef	KD_DEBUG
537 	cmn_err(CE_CONT, "?%s #%d: version %s\n",
538 	    DRIVER_NAME(devi), ddi_get_instance(devi), "%I% (%E%)");
539 #endif
540 
541 	return (DDI_SUCCESS);
542 
543 failure:
544 	kb8042_cleanup(kb8042);
545 	return (DDI_FAILURE);
546 }
547 
548 static int
549 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
550 {
551 	struct kb8042 *kb8042 = &Kdws;
552 
553 	switch (cmd) {
554 	case DDI_SUSPEND:
555 		return (DDI_SUCCESS);
556 
557 	case DDI_DETACH:
558 		/* If someone has a stream open, fail to detach */
559 		if (kb8042->w_qp != NULL)
560 			return (DDI_FAILURE);
561 
562 		ASSERT(kb8042_dip == dip);
563 
564 		kb8042_cleanup(kb8042);
565 
566 		return (DDI_SUCCESS);
567 
568 	default:
569 		return (DDI_FAILURE);
570 	}
571 }
572 
573 
574 /*ARGSUSED*/
575 static int
576 kb8042_getinfo(
577     dev_info_t *dip,
578     ddi_info_cmd_t infocmd,
579     void *arg,
580     void **result)
581 {
582 	register int error;
583 
584 	switch (infocmd) {
585 	case DDI_INFO_DEVT2DEVINFO:
586 		if (kb8042_dip == NULL) {
587 			error = DDI_FAILURE;
588 		} else {
589 			*result = (void *) kb8042_dip;
590 			error = DDI_SUCCESS;
591 		}
592 		break;
593 	case DDI_INFO_DEVT2INSTANCE:
594 		*result = (void *)0;
595 		error = DDI_SUCCESS;
596 		break;
597 	default:
598 		error = DDI_FAILURE;
599 		break;
600 	}
601 	return (error);
602 }
603 
604 static void
605 kb8042_cleanup(struct kb8042 *kb8042)
606 {
607 
608 	if (kb8042->init_state & KB8042_HW_MUTEX_INITTED)
609 		mutex_destroy(&kb8042->w_hw_mutex);
610 
611 	if (kb8042->init_state & KB8042_INTR_ADDED)
612 		ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
613 
614 	if (kb8042->init_state & KB8042_REGS_MAPPED)
615 		ddi_regs_map_free(&kb8042->handle);
616 
617 	if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
618 		ddi_remove_minor_node(kb8042_dip, NULL);
619 
620 	kb8042->init_state = KB8042_UNINITIALIZED;
621 }
622 
623 static void
624 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
625 {
626 	if (kb8042->w_init)
627 		return;
628 
629 	if (!from_resume) {
630 		kb8042->w_kblayout = 0;	/* Default to US */
631 		kb8042->w_qp = (queue_t *)NULL;
632 		kb8042->simulated_kbd_type = KB_PC;
633 		kb8042->leds.commanded = -1;	/* Unknown initial state */
634 		kb8042->leds.desired = -1;	/* Unknown initial state */
635 	}
636 
637 	kb8042_wait_poweron(kb8042);
638 
639 	kb8042->kb_old_key_pos = 0;
640 
641 	/* Set up the command state machine and start it running. */
642 	kb8042->command_state = KB_COMMAND_STATE_WAIT;
643 	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE);
644 
645 	kb8042->w_init++;
646 
647 	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
648 }
649 
650 /*ARGSUSED2*/
651 static int
652 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
653 {
654 	struct kb8042	*kb8042;
655 	int err;
656 	int initial_leds;
657 	int initial_led_mask;
658 
659 	kb8042 = &Kdws;
660 
661 	kb8042->w_dev = *devp;
662 
663 	if (qp->q_ptr) {
664 		return (0);
665 	}
666 	qp->q_ptr = (caddr_t)kb8042;
667 	WR(qp)->q_ptr = qp->q_ptr;
668 	if (!kb8042->w_qp)
669 		kb8042->w_qp = qp;
670 
671 	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
672 	err = kbtrans_streams_init(qp, sflag, credp,
673 		(struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
674 		&kb8042->hw_kbtrans,
675 		initial_leds, initial_led_mask);
676 	if (err != 0)
677 		return (err);
678 
679 	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
680 
681 	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
682 	kb8042->polledio.cons_polledio_argument =
683 		(struct cons_polledio_arg *)kb8042;
684 	kb8042->polledio.cons_polledio_putchar = NULL;
685 	kb8042->polledio.cons_polledio_getchar =
686 		(int (*)(struct cons_polledio_arg *))kb8042_polled_getchar;
687 	kb8042->polledio.cons_polledio_ischar =
688 		(boolean_t (*)(struct cons_polledio_arg *))kb8042_polled_ischar;
689 	kb8042->polledio.cons_polledio_enter = NULL;
690 	kb8042->polledio.cons_polledio_exit = NULL;
691 	kb8042->polledio.cons_polledio_setled =
692 		(void (*)(struct cons_polledio_arg *, int))kb8042_polled_setled;
693 	kb8042->polledio.cons_polledio_keycheck =
694 		(boolean_t (*)(struct cons_polledio_arg *, int *,
695 		enum keystate *))kb8042_polled_keycheck;
696 
697 	qprocson(qp);
698 
699 	kbtrans_streams_enable(kb8042->hw_kbtrans);
700 
701 	return (0);
702 }
703 
704 /*ARGSUSED1*/
705 static int
706 kb8042_close(queue_t *qp, int flag, cred_t *credp)
707 {
708 	struct kb8042	*kb8042;
709 
710 	kb8042 = (struct kb8042 *)qp->q_ptr;
711 
712 	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);
713 
714 	kb8042->w_qp = (queue_t *)NULL;
715 	qprocsoff(qp);
716 
717 	return (0);
718 }
719 
720 static int
721 kb8042_wsrv(queue_t *qp)
722 {
723 	struct kb8042 *kb8042;
724 
725 	mblk_t	*mp;
726 
727 	kb8042 = (struct kb8042 *)qp->q_ptr;
728 
729 	while ((mp = getq(qp))) {
730 		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
731 		case KBTRANS_MESSAGE_HANDLED:
732 			continue;
733 		case KBTRANS_MESSAGE_NOT_HANDLED:
734 			break;
735 		}
736 		switch (mp->b_datap->db_type) {
737 		case M_IOCTL:
738 			kb8042_ioctlmsg(kb8042, qp, mp);
739 			continue;
740 		case M_IOCDATA:
741 			kb8042_iocdatamsg(qp, mp);
742 			continue;
743 		case M_DELAY:
744 		case M_STARTI:
745 		case M_STOPI:
746 		case M_READ:	/* ignore, no buffered data */
747 			freemsg(mp);
748 			continue;
749 		case M_FLUSH:
750 			*mp->b_rptr &= ~FLUSHW;
751 			if (*mp->b_rptr & FLUSHR)
752 				qreply(qp, mp);
753 			else
754 				freemsg(mp);
755 			continue;
756 		default:
757 			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
758 						mp->b_datap->db_type);
759 			freemsg(mp);
760 			continue;
761 		}
762 	}
763 	return (0);
764 }
765 
766 static void
767 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
768 {
769 	struct iocblk	*iocp;
770 	mblk_t		*datap;
771 	int		error;
772 	int		tmp;
773 
774 	iocp = (struct iocblk *)mp->b_rptr;
775 
776 	switch (iocp->ioc_cmd) {
777 
778 	case CONSOPENPOLLEDIO:
779 		error = miocpullup(mp, sizeof (struct cons_polledio *));
780 		if (error != 0) {
781 			miocnak(qp, mp, 0, error);
782 			return;
783 		}
784 
785 		/*
786 		 * We are given an appropriate-sized data block,
787 		 * and return a pointer to our structure in it.
788 		 */
789 		*(struct cons_polledio **)mp->b_cont->b_rptr =
790 		    &kb8042->polledio;
791 		mp->b_datap->db_type = M_IOCACK;
792 		iocp->ioc_error = 0;
793 		qreply(qp, mp);
794 		break;
795 
796 	case CONSCLOSEPOLLEDIO:
797 		miocack(qp, mp, 0, 0);
798 		break;
799 
800 	case CONSSETABORTENABLE:
801 		if (iocp->ioc_count != TRANSPARENT) {
802 			miocnak(qp, mp, 0, EINVAL);
803 			return;
804 		}
805 
806 		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
807 		miocack(qp, mp, 0, 0);
808 		break;
809 
810 	/*
811 	 * Valid only in TR_UNTRANS_MODE mode.
812 	 */
813 	case CONSSETKBDTYPE:
814 		error = miocpullup(mp, sizeof (int));
815 		if (error != 0) {
816 			miocnak(qp, mp, 0, error);
817 			return;
818 		}
819 		tmp =  *(int *)mp->b_cont->b_rptr;
820 		if (tmp != KB_PC && tmp != KB_USB) {
821 			miocnak(qp, mp, 0, EINVAL);
822 			break;
823 		}
824 		kb8042->simulated_kbd_type = tmp;
825 		miocack(qp, mp, 0, 0);
826 		break;
827 
828 	case KIOCLAYOUT:
829 		if (kb8042->w_kblayout == -1) {
830 			miocnak(qp, mp, 0, EINVAL);
831 			return;
832 		}
833 
834 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
835 			miocnak(qp, mp, 0, ENOMEM);
836 			return;
837 		}
838 
839 		if (kb8042->simulated_kbd_type == KB_USB)
840 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
841 		else
842 			*(int *)datap->b_wptr = kb8042->w_kblayout;
843 
844 		datap->b_wptr += sizeof (int);
845 		if (mp->b_cont)
846 			freemsg(mp->b_cont);
847 		mp->b_cont = datap;
848 		iocp->ioc_count = sizeof (int);
849 		mp->b_datap->db_type = M_IOCACK;
850 		iocp->ioc_error = 0;
851 		qreply(qp, mp);
852 		break;
853 
854 	case KIOCSLAYOUT:
855 		if (iocp->ioc_count != TRANSPARENT) {
856 			miocnak(qp, mp, 0, EINVAL);
857 			return;
858 		}
859 
860 		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
861 		miocack(qp, mp, 0, 0);
862 		break;
863 
864 	case KIOCCMD:
865 		error = miocpullup(mp, sizeof (int));
866 		if (error != 0) {
867 			miocnak(qp, mp, 0, error);
868 			return;
869 		}
870 
871 		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
872 		miocack(qp, mp, 0, 0);
873 		break;
874 
875 	default:
876 #ifdef DEBUG1
877 		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
878 #endif
879 		miocnak(qp, mp, 0, EINVAL);
880 		return;
881 	}
882 }
883 
884 /*
885  * Process a byte received from the keyboard
886  */
887 static void
888 kb8042_received_byte(
889 	struct kb8042	*kb8042,
890 	int		scancode)	/* raw scan code */
891 {
892 	boolean_t	legit;		/* is this a legit key pos'n? */
893 	int		key_pos = -1;
894 	enum keystate	state;
895 	boolean_t	synthetic_release_needed;
896 
897 #ifdef	KD_DEBUG
898 	kb8042_debug_hotkey(scancode);
899 #endif
900 
901 	if (!kb8042->w_init)	/* can't do anything anyway */
902 		return;
903 
904 	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
905 		&synthetic_release_needed);
906 
907 	if (legit == 0) {
908 		/* Eaten by translation */
909 #ifdef	KD_DEBUG
910 		if (kb8042_debug)
911 			prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode);
912 #endif
913 		return;
914 	}
915 
916 #ifdef	KD_DEBUG
917 	if (kb8042_debug) {
918 		prom_printf("kb8042_intr:  0x%x -> %s %d",
919 			scancode,
920 			state == KEY_RELEASED ? "released" : "pressed",
921 			key_pos);
922 	}
923 #endif
924 
925 	/*
926 	 * Don't know if we want this permanently, but it seems interesting
927 	 * for the moment.
928 	 */
929 	if (key_pos == kb8042->debugger.mod1) {
930 #ifdef	KD_DEBUG
931 		if (kb8042_debug)
932 			prom_printf(" -> debug mod1");
933 #endif
934 		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
935 	}
936 	if (key_pos == kb8042->debugger.mod2) {
937 #ifdef	KD_DEBUG
938 		if (kb8042_debug)
939 			prom_printf(" -> debug mod2");
940 #endif
941 		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
942 	}
943 	if (kb8042->debugger.enabled &&
944 	    key_pos == kb8042->debugger.trigger &&
945 	    kb8042->debugger.mod1_down &&
946 	    kb8042->debugger.mod2_down) {
947 #ifdef	KD_DEBUG
948 		if (kb8042_debug)
949 			prom_printf(" -> debugger\n");
950 #endif
951 		/*
952 		 * Require new presses of the modifiers.
953 		 */
954 		kb8042->debugger.mod1_down = B_FALSE;
955 		kb8042->debugger.mod2_down = B_FALSE;
956 		abort_sequence_enter(NULL);
957 		return;
958 	}
959 
960 	/*
961 	 * If there's no queue above us - as can happen if we've been
962 	 * attached but not opened - drop the keystroke.
963 	 * Note that we do this here instead of above so that
964 	 * Ctrl-Alt-D still works.
965 	 */
966 	if (kb8042->w_qp == NULL) {
967 #ifdef	KD_DEBUG
968 		if (kb8042_debug)
969 			prom_printf(" -> nobody home\n");
970 #endif
971 		return;
972 	}
973 
974 	/*
975 	 * This is to filter out auto repeat since it can't be
976 	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
977 	 * can.  Don't know whether they've taken over the world.
978 	 * Don't think so.)
979 	 */
980 	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
981 #ifdef	KD_DEBUG
982 		if (kb8042_debug)
983 			prom_printf(" -> autorepeat ignored\n");
984 #endif
985 		return;
986 	}
987 
988 #ifdef	KD_DEBUG
989 	if (kb8042_debug)
990 		prom_printf(" -> OK\n");
991 #endif
992 
993 #if	defined(KD_DEBUG)
994 	if (kb8042_pressrelease_debug) {
995 		prom_printf(" %s%d ",
996 			state == KEY_PRESSED ? "+" : "-",
997 			key_pos);
998 	}
999 #endif
1000 
1001 		kb8042_process_key(kb8042, key_pos, state);
1002 
1003 	/*
1004 	 * This is a total hack.  For some stupid reason, the two additional
1005 	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
1006 	 * only.  We synthesize a release immediately.
1007 	 */
1008 	if (synthetic_release_needed) {
1009 #if	defined(KD_DEBUG)
1010 		if (kb8042_debug)
1011 			prom_printf("synthetic release %d\n", key_pos);
1012 		if (kb8042_pressrelease_debug)
1013 			prom_printf(" -%d(s) ", key_pos);
1014 #endif
1015 		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
1016 		kb8042_process_key(kb8042, key_pos, state);
1017 	}
1018 }
1019 
1020 
1021 static void
1022 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
1023     enum keystate state)
1024 {
1025 	kbtrans_key_t key;
1026 
1027 	ASSERT(key_pos >= 0 && key_pos <= 255);
1028 	if (kb8042->simulated_kbd_type == KB_PC) {
1029 		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
1030 	} else if (kb8042->simulated_kbd_type == KB_USB) {
1031 		key = keytab_pc2usb[key_pos];
1032 		if (key != 0) {
1033 			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
1034 		}
1035 	}
1036 }
1037 
1038 /*
1039  * Called from interrupt handler when keyboard interrupt occurs.
1040  */
1041 static uint_t
1042 kb8042_intr(caddr_t arg)
1043 {
1044 	uchar_t scancode;	/* raw scan code */
1045 	int rc;
1046 	struct kb8042 *kb8042 = (struct kb8042 *)arg;
1047 
1048 	rc = DDI_INTR_UNCLAIMED;
1049 
1050 	if (kb8042->init_state == KB8042_UNINITIALIZED)
1051 		return (DDI_INTR_UNCLAIMED);
1052 
1053 	/* don't care if drv_setparm succeeds */
1054 	(void) drv_setparm(SYSRINT, 1);
1055 
1056 	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
1057 	    != 0) {
1058 		rc = DDI_INTR_CLAIMED;
1059 
1060 		scancode = ddi_get8(kb8042->handle,
1061 			kb8042->addr + I8042_INT_INPUT_DATA);
1062 
1063 #if	defined(KD_DEBUG)
1064 		if (kb8042_low_level_debug)
1065 			prom_printf(" <K:%x ", scancode);
1066 #endif
1067 
1068 		mutex_enter(&kb8042->w_hw_mutex);
1069 
1070 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
1071 		    STATE_NORMAL) {
1072 			mutex_exit(&kb8042->w_hw_mutex);
1073 			continue;
1074 		}
1075 
1076 
1077 		mutex_exit(&kb8042->w_hw_mutex);
1078 
1079 		kb8042_received_byte(kb8042, scancode);
1080 	}
1081 
1082 	return (rc);
1083 }
1084 
1085 static void
1086 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
1087 {
1088 	struct copyresp	*csp;
1089 
1090 	csp = (struct copyresp *)mp->b_rptr;
1091 	if (csp->cp_rval) {
1092 		freemsg(mp);
1093 		return;
1094 	}
1095 
1096 	switch (csp->cp_cmd) {
1097 	default:
1098 		miocack(qp, mp, 0, 0);
1099 		break;
1100 	}
1101 }
1102 
1103 static boolean_t
1104 kb8042_polled_keycheck(
1105     struct kbtrans_hardware *hw,
1106     int *key,
1107     enum keystate *state)
1108 {
1109 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1110 	int	scancode;
1111 	boolean_t	legit;
1112 	boolean_t	synthetic_release_needed;
1113 
1114 	if (kb8042->polled_synthetic_release_pending) {
1115 		*key = kb8042->polled_synthetic_release_key;
1116 		*state = KEY_RELEASED;
1117 		kb8042->polled_synthetic_release_pending = B_FALSE;
1118 #if	defined(KD_DEBUG)
1119 		if (kb8042_getchar_debug)
1120 			prom_printf("synthetic release 0x%x\n", *key);
1121 #endif
1122 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
1123 		return (B_TRUE);
1124 	}
1125 
1126 	for (;;) {
1127 		if (ddi_get8(kb8042->handle,
1128 		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
1129 			return (B_FALSE);
1130 		}
1131 
1132 		scancode = ddi_get8(kb8042->handle,
1133 				kb8042->addr + I8042_POLL_INPUT_DATA);
1134 
1135 #if	defined(KD_DEBUG)
1136 		if (kb8042_low_level_debug)
1137 			prom_printf(" g<%x ", scancode);
1138 #endif
1139 
1140 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
1141 		    STATE_NORMAL) {
1142 			continue;
1143 		}
1144 
1145 #ifdef	KD_DEBUG
1146 		kb8042_debug_hotkey(scancode);
1147 		if (kb8042_getchar_debug)
1148 			prom_printf("polled 0x%x", scancode);
1149 #endif
1150 
1151 		legit = KeyboardConvertScan(kb8042, scancode, key, state,
1152 			&synthetic_release_needed);
1153 		if (!legit) {
1154 #ifdef	KD_DEBUG
1155 			if (kb8042_getchar_debug)
1156 				prom_printf(" -> ignored\n");
1157 #endif
1158 			continue;
1159 		}
1160 #ifdef	KD_DEBUG
1161 		if (kb8042_getchar_debug) {
1162 			prom_printf(" -> %s %d\n",
1163 				*state == KEY_PRESSED ? "pressed" : "released",
1164 				*key);
1165 		}
1166 #endif
1167 		/*
1168 		 * For the moment at least, we rely on hardware autorepeat
1169 		 * for polled I/O autorepeat.  However, for coordination
1170 		 * with the interrupt-driven code, maintain the last key
1171 		 * pressed.
1172 		 */
1173 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
1174 
1175 		/*
1176 		 * This is a total hack to support two additional keys
1177 		 * on Korean keyboards.  They report only on press, and
1178 		 * so we synthesize a release.  Most likely this will
1179 		 * never be important to polled  I/O, but if I do it
1180 		 * "right" the first time it _won't_ be an issue.
1181 		 */
1182 		if (synthetic_release_needed) {
1183 			kb8042->polled_synthetic_release_pending = B_TRUE;
1184 			kb8042->polled_synthetic_release_key = *key;
1185 		}
1186 
1187 		if (kb8042->simulated_kbd_type == KB_USB) {
1188 			*key = keytab_pc2usb[*key];
1189 		}
1190 		return (B_TRUE);
1191 	}
1192 }
1193 
1194 static void
1195 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1196 {
1197 	kb8042->leds.desired = led_state;
1198 
1199 	if (!polled)
1200 		mutex_enter(&kb8042->w_hw_mutex);
1201 
1202 	kb8042_start_state_machine(kb8042, polled);
1203 
1204 	if (!polled)
1205 		mutex_exit(&kb8042->w_hw_mutex);
1206 }
1207 
1208 static void
1209 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
1210 {
1211 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1212 	kb8042_setled(kb8042, led_state, B_TRUE);
1213 }
1214 
1215 static void
1216 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1217 {
1218 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1219 	kb8042_setled(kb8042, led_state, B_FALSE);
1220 }
1221 
1222 static void
1223 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
1224 {
1225 	if (polled) {
1226 		ddi_put8(kb8042->handle,
1227 		    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
1228 	} else {
1229 		ddi_put8(kb8042->handle,
1230 		    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
1231 	}
1232 
1233 #if	defined(KD_DEBUG)
1234 	if (kb8042_low_level_debug)
1235 		prom_printf(" >K:%x ", byte);
1236 #endif
1237 }
1238 
1239 /*
1240  * Wait until the keyboard is fully up, maybe.
1241  * We may be the first person to talk to the keyboard, in which case
1242  * it's patiently waiting to say "AA" to us to tell us it's up.
1243  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
1244  * good bet that we've already spent that long getting to that point,
1245  * so we'll only wait long enough for the communications electronics to
1246  * run.
1247  */
1248 static void
1249 kb8042_wait_poweron(struct kb8042 *kb8042)
1250 {
1251 	int cnt;
1252 	int ready;
1253 	unsigned char byt;
1254 
1255 	/* wait for up to 250 ms for a response */
1256 	for (cnt = 0; cnt < 250; cnt++) {
1257 		ready = ddi_get8(kb8042->handle,
1258 			kb8042->addr + I8042_INT_INPUT_AVAIL);
1259 		if (ready != 0)
1260 			break;
1261 		drv_usecwait(1000);
1262 	}
1263 
1264 	/*
1265 	 * If there's something pending, read and discard it.  If not,
1266 	 * assume things are OK anyway - maybe somebody else ate it
1267 	 * already.  (On a PC, the BIOS almost certainly did.)
1268 	 */
1269 	if (ready != 0) {
1270 		byt = ddi_get8(kb8042->handle,
1271 			kb8042->addr + I8042_INT_INPUT_DATA);
1272 #if	defined(KD_DEBUG)
1273 		if (kb8042_low_level_debug)
1274 			prom_printf(" <K:%x ", byt);
1275 #endif
1276 	}
1277 }
1278 
1279 static void
1280 kb8042_start_state_machine(struct kb8042 *kb8042, boolean_t polled)
1281 {
1282 	if (kb8042->command_state == KB_COMMAND_STATE_IDLE) {
1283 		if (kb8042->leds.desired != kb8042->leds.commanded) {
1284 			kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1285 			kb8042->command_state = KB_COMMAND_STATE_LED;
1286 		}
1287 	}
1288 }
1289 
1290 enum state_return
1291 kb8042_state_machine(struct kb8042 *kb8042, int scancode, boolean_t polled)
1292 {
1293 	switch (kb8042->command_state) {
1294 	case KB_COMMAND_STATE_IDLE:
1295 		break;
1296 
1297 	case KB_COMMAND_STATE_LED:
1298 		if (scancode == KB_ACK) {
1299 			kb8042_send_to_keyboard(kb8042,
1300 				kb8042_xlate_leds(kb8042->leds.desired),
1301 				polled);
1302 			kb8042->leds.commanded = kb8042->leds.desired;
1303 			kb8042->command_state = KB_COMMAND_STATE_WAIT;
1304 			return (STATE_INTERNAL);
1305 		}
1306 		/* Drop normal scan codes through. */
1307 		break;
1308 
1309 	case KB_COMMAND_STATE_WAIT:
1310 		if (scancode == KB_ACK) {
1311 			kb8042->command_state = KB_COMMAND_STATE_IDLE;
1312 			kb8042_start_state_machine(kb8042, polled);
1313 			return (STATE_INTERNAL);
1314 		}
1315 		/* Drop normal scan codes through. */
1316 		break;
1317 	}
1318 	return (STATE_NORMAL);
1319 }
1320 
1321 static int
1322 kb8042_xlate_leds(int led)
1323 {
1324 	int res;
1325 
1326 	res = 0;
1327 
1328 	if (led & LED_NUM_LOCK)
1329 		res |= LED_NUM;
1330 	if (led & LED_SCROLL_LOCK)
1331 		res |= LED_SCR;
1332 	if (led & LED_CAPS_LOCK)
1333 		res |= LED_CAP;
1334 
1335 	return (res);
1336 }
1337 
1338 /*ARGSUSED*/
1339 static void
1340 kb8042_get_initial_leds(
1341     struct kb8042 *kb8042,
1342     int *initial_leds,
1343     int *initial_led_mask)
1344 {
1345 #if	defined(i86pc)
1346 	extern caddr_t	p0_va;
1347 	uint8_t		bios_kb_flag;
1348 
1349 	bios_kb_flag = p0_va[BIOS_KB_FLAG];
1350 
1351 	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1352 	*initial_leds = 0;
1353 	if (bios_kb_flag & BIOS_CAPS_STATE)
1354 		*initial_leds |= LED_CAPS_LOCK;
1355 	if (bios_kb_flag & BIOS_NUM_STATE)
1356 		*initial_leds |= LED_NUM_LOCK;
1357 	if (bios_kb_flag & BIOS_SCROLL_STATE)
1358 		*initial_leds |= LED_SCROLL_LOCK;
1359 #else
1360 	*initial_leds = 0;
1361 	*initial_led_mask = 0;
1362 #endif
1363 }
1364 
1365 #if	defined(KD_DEBUG)
1366 static void
1367 kb8042_debug_hotkey(int scancode)
1368 {
1369 	if (!kb8042_enable_debug_hotkey)
1370 		return;
1371 
1372 	switch (scancode) {
1373 	case 0x44:	/* F10 in Scan Set 1 code.  (Set 2 code is 0x09)  */
1374 		if (!kb8042_debug) {
1375 			prom_printf("\nKeyboard:  normal debugging on\n");
1376 			kb8042_debug = B_TRUE;
1377 		}
1378 		break;
1379 	case 0x43:	/* F9 in Scan Set 1 code.  (Set 2 code is 0x01) */
1380 		if (!kb8042_getchar_debug) {
1381 			prom_printf("\nKeyboard:  getchar debugging on\n");
1382 			kb8042_getchar_debug = B_TRUE;
1383 		}
1384 		break;
1385 	case 0x42:	/* F8 in Scan Set 1 code.  (Set 2 code is 0x0a) */
1386 		if (!kb8042_low_level_debug) {
1387 			prom_printf("\nKeyboard:  low-level debugging on\n");
1388 			kb8042_low_level_debug = B_TRUE;
1389 		}
1390 		break;
1391 	case 0x41:	/* F7 in Scan Set 1 code.  (Set 2 code is 0x83) */
1392 		if (!kb8042_pressrelease_debug) {
1393 			prom_printf(
1394 			    "\nKeyboard:  press/release debugging on\n");
1395 			kb8042_pressrelease_debug = B_TRUE;
1396 		}
1397 		break;
1398 	case 0x3b:	/* F1 in Scan Set 1 code.  (Set 2 code is 0x05) */
1399 		if (kb8042_debug ||
1400 		    kb8042_getchar_debug ||
1401 		    kb8042_low_level_debug ||
1402 		    kb8042_pressrelease_debug) {
1403 			prom_printf("\nKeyboard:  all debugging off\n");
1404 			kb8042_debug = B_FALSE;
1405 			kb8042_getchar_debug = B_FALSE;
1406 			kb8042_low_level_debug = B_FALSE;
1407 			kb8042_pressrelease_debug = B_FALSE;
1408 		}
1409 		break;
1410 	}
1411 }
1412 #endif
1413 
1414 static boolean_t
1415 kb8042_autorepeat_detect(
1416     struct kb8042 *kb8042,
1417     int key_pos,
1418     enum keystate state)
1419 {
1420 	if (state == KEY_RELEASED) {
1421 		if (kb8042->kb_old_key_pos == key_pos)
1422 			kb8042->kb_old_key_pos = 0;
1423 	} else {
1424 		if (kb8042->kb_old_key_pos == key_pos) {
1425 			return (B_TRUE);
1426 		}
1427 		kb8042->kb_old_key_pos = key_pos;
1428 	}
1429 	return (B_FALSE);
1430 }
1431 
1432 /* ARGSUSED */
1433 static void
1434 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1435 {
1436 	switch (cmd) {
1437 	case KBD_CMD_BELL:
1438 		beeper_on(BEEP_TYPE4);
1439 		break;
1440 	case KBD_CMD_NOBELL:
1441 		beeper_off();
1442 		break;
1443 	}
1444 }
1445 
1446 
1447 /*
1448  * This is a pass-thru routine to get a character at poll time.
1449  */
1450 static int
1451 kb8042_polled_getchar(struct cons_polledio_arg *arg)
1452 {
1453 	struct kb8042	*kb8042;
1454 
1455 	kb8042 = (struct kb8042 *)arg;
1456 
1457 	return (kbtrans_getchar(kb8042->hw_kbtrans));
1458 }
1459 
1460 /*
1461  * This is a pass-thru routine to get a character at poll time.
1462  */
1463 static int
1464 kb8042_polled_ischar(struct cons_polledio_arg *arg)
1465 {
1466 	struct kb8042	*kb8042;
1467 
1468 	kb8042 = (struct kb8042 *)arg;
1469 
1470 	return (kbtrans_ischar(kb8042->hw_kbtrans));
1471 }
1472