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