xref: /illumos-gate/usr/src/uts/common/io/kb8042/kb8042.c (revision e8ed0869d5c65afe0c37c4755bf81f7381d1f43c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5281f0747Slt200341  * Common Development and Distribution License (the "License").
6281f0747Slt200341  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2619397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30*e8ed0869SJohn Beck #define	KEYMAP_SIZE_VARIABLE
31*e8ed0869SJohn Beck 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/inline.h>
357c478bd9Sstevel@tonic-gate #include <sys/termio.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/termios.h>
387c478bd9Sstevel@tonic-gate #include <sys/stream.h>
397c478bd9Sstevel@tonic-gate #include <sys/strtty.h>
407c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
417c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
427c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/note.h>
457c478bd9Sstevel@tonic-gate #include "sys/consdev.h"
467c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
477c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
487c478bd9Sstevel@tonic-gate #include "kb8042.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/i8042.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include "sys/kbio.h"		/* for KIOCSLAYOUT */
537c478bd9Sstevel@tonic-gate #include "sys/stat.h"
547c478bd9Sstevel@tonic-gate #include "sys/reboot.h"
557c478bd9Sstevel@tonic-gate #include <sys/promif.h>
567c478bd9Sstevel@tonic-gate #include <sys/beep.h>
57c35aa225Smarx #include <sys/inttypes.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * For any keyboard, there is a unique code describing the position
617c478bd9Sstevel@tonic-gate  * of the key on a keyboard. We refer to the code as "station number".
627c478bd9Sstevel@tonic-gate  * The following table is used to map the station numbers from ps2
637c478bd9Sstevel@tonic-gate  * AT/XT keyboards to that of a USB one.
64fd9cb95cSsethg  *
65fd9cb95cSsethg  * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
66fd9cb95cSsethg  * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
67fd9cb95cSsethg  * when in KB_PC mode.  Note that this does not need to be made conditional
68fd9cb95cSsethg  * on the architecture for which this module is complied because there are no
69fd9cb95cSsethg  * keys that map to K8042_STOP on non-SPARC platforms.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
727c478bd9Sstevel@tonic-gate /*  0 */	0,	53,	30,	31,	32,	33,	34,	35,
737c478bd9Sstevel@tonic-gate /*  8 */	36,	37,	38,	39,	45,	46,	137,	42,
747c478bd9Sstevel@tonic-gate /* 16 */	43,	20,	26,	8,	21,	23,	28,	24,
757c478bd9Sstevel@tonic-gate /* 24 */	12,	18,	19,	47,	48,	49,	57,	4,
767c478bd9Sstevel@tonic-gate /* 32 */	22,	7,	9,	10,	11,	13,	14,	15,
777c478bd9Sstevel@tonic-gate /* 40 */	51,	52,	50,	40,	225,	100,	29,	27,
787c478bd9Sstevel@tonic-gate /* 48 */	6,	25,	5,	17,	16,	54,	55,	56,
797c478bd9Sstevel@tonic-gate /* 56 */	135,	229,	224,	227,	226,	44,	230,	231,
807c478bd9Sstevel@tonic-gate /* 64 */	228,	101,	0,	0,	0,	0,	0,	0,
817c478bd9Sstevel@tonic-gate /* 72 */	0,	0,	0,	73,	76,	0,	0,	80,
827c478bd9Sstevel@tonic-gate /* 80 */	74,	77,	0,	82,	81,	75,	78,	0,
837c478bd9Sstevel@tonic-gate /* 88 */	0,	79,	83,	95,	92,	89,	0,	84,
847c478bd9Sstevel@tonic-gate /* 96 */	96,	93,	90,	98,	85,	97,	94,	91,
857c478bd9Sstevel@tonic-gate /* 104 */	99,	86,	87,	133,	88,	0,	41,	0,
867c478bd9Sstevel@tonic-gate /* 112 */	58,	59,	60,	61,	62,	63,	64,	65,
877c478bd9Sstevel@tonic-gate /* 120 */	66,	67,	68,	69,	70,	71,	72,	0,
887c478bd9Sstevel@tonic-gate /* 128 */	0,	0,	0,	139,	138,	136,	0,	0,
897c478bd9Sstevel@tonic-gate /* 136 */	0,	0,	0,	0,	0,	0,	0,	0,
907c478bd9Sstevel@tonic-gate /* 144 */	0,	0,	0,	0,	0,	0,	0,	0,
917c478bd9Sstevel@tonic-gate /* 152 */	0,	0,	0,	0,	0,	0,	0,	0,
92fd9cb95cSsethg /* 160 */	120,	0,	0,	0,	0,	0,	0,	0,
937c478bd9Sstevel@tonic-gate /* 168 */	0,	0,	0,	0,	0,	0,	0,	0,
947c478bd9Sstevel@tonic-gate /* 176 */	0,	0,	0,	0,	0,	0,	0,	0,
957c478bd9Sstevel@tonic-gate /* 184 */	0,	0,	0,	0,	0,	0,	0,	0,
967c478bd9Sstevel@tonic-gate /* 192 */	0,	0,	0,	0,	0,	0,	0,	0,
977c478bd9Sstevel@tonic-gate /* 200 */	0,	0,	0,	0,	0,	0,	0,	0,
987c478bd9Sstevel@tonic-gate /* 208 */	0,	0,	0,	0,	0,	0,	0,	0,
997c478bd9Sstevel@tonic-gate /* 216 */	0,	0,	0,	0,	0,	0,	0,	0,
1007c478bd9Sstevel@tonic-gate /* 224 */	0,	0,	0,	0,	0,	0,	0,	0,
1017c478bd9Sstevel@tonic-gate /* 232 */	0,	0,	0,	0,	0,	0,	0,	0,
1027c478bd9Sstevel@tonic-gate /* 240 */	0,	0,	0,	0,	0,	0,	0,	0,
1037c478bd9Sstevel@tonic-gate /* 248 */	0,	0,	0,	0
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * DEBUG (or KD_DEBUG for just this module) turns on a flag called
1087c478bd9Sstevel@tonic-gate  * kb8042_enable_debug_hotkey.  If kb8042_enable_debug_hotkey is true,
1097c478bd9Sstevel@tonic-gate  * then the following hotkeys are enabled:
1107c478bd9Sstevel@tonic-gate  *    F10 - turn on debugging "normal" translations
1117c478bd9Sstevel@tonic-gate  *    F9  - turn on debugging "getchar" translations
1127c478bd9Sstevel@tonic-gate  *    F8  - turn on "low level" debugging
1137c478bd9Sstevel@tonic-gate  *    F7  - turn on terse press/release debugging
1147c478bd9Sstevel@tonic-gate  *    F1  - turn off all debugging
1157c478bd9Sstevel@tonic-gate  * The default value for kb8042_enable_debug_hotkey is false, disabling
1167c478bd9Sstevel@tonic-gate  * these hotkeys.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #if	defined(DEBUG) || defined(lint)
1207c478bd9Sstevel@tonic-gate #define	KD_DEBUG
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
1247c478bd9Sstevel@tonic-gate boolean_t	kb8042_enable_debug_hotkey = B_FALSE;
1257c478bd9Sstevel@tonic-gate boolean_t	kb8042_debug = B_FALSE;
1267c478bd9Sstevel@tonic-gate boolean_t	kb8042_getchar_debug = B_FALSE;
1277c478bd9Sstevel@tonic-gate boolean_t	kb8042_low_level_debug = B_FALSE;
1287c478bd9Sstevel@tonic-gate boolean_t	kb8042_pressrelease_debug = B_FALSE;
1297c478bd9Sstevel@tonic-gate static void kb8042_debug_hotkey(int scancode);
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
132fd9cb95cSsethg #ifdef __sparc
133fd9cb95cSsethg #define	USECS_PER_WAIT 100
134fd9cb95cSsethg #define	MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
135fd9cb95cSsethg #define	MIN_DELAY_USECS USECS_PER_WAIT
136fd9cb95cSsethg 
137fd9cb95cSsethg boolean_t kb8042_warn_unknown_scanset = B_TRUE;
138fd9cb95cSsethg int kb8042_default_scanset = 2;
139fd9cb95cSsethg 
140fd9cb95cSsethg #endif
141fd9cb95cSsethg 
1427c478bd9Sstevel@tonic-gate enum state_return { STATE_NORMAL, STATE_INTERNAL };
1437c478bd9Sstevel@tonic-gate 
144fd9cb95cSsethg static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
1457c478bd9Sstevel@tonic-gate static uint_t kb8042_intr(caddr_t arg);
1467c478bd9Sstevel@tonic-gate static void kb8042_wait_poweron(struct kb8042 *kb8042);
1477c478bd9Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
1487c478bd9Sstevel@tonic-gate static int kb8042_xlate_leds(int);
149fd9cb95cSsethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
1507c478bd9Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
1517c478bd9Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
1527c478bd9Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck(
1537c478bd9Sstevel@tonic-gate 			struct kbtrans_hardware *hw, int *key,
1547c478bd9Sstevel@tonic-gate 			enum keystate *state);
1557c478bd9Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
1567c478bd9Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
1577c478bd9Sstevel@tonic-gate 			enum keystate state);
1587c478bd9Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
1597c478bd9Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
1607c478bd9Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *);
1617c478bd9Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
162281f0747Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
163281f0747Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
164fd9cb95cSsethg static void kb8042_cleanup(struct kb8042 *kb8042);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = {
1677c478bd9Sstevel@tonic-gate 	kb8042_streams_setled,
1687c478bd9Sstevel@tonic-gate 	kb8042_polled_setled,
1697c478bd9Sstevel@tonic-gate 	kb8042_polled_keycheck,
1707c478bd9Sstevel@tonic-gate };
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate extern struct keyboard keyindex_pc;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	DRIVER_NAME(dip) ddi_driver_name(dip)
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static	char	module_name[] = "kb8042";
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
1797c478bd9Sstevel@tonic-gate 			cred_t *credp);
1807c478bd9Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
1817c478bd9Sstevel@tonic-gate static int kb8042_wsrv();
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate struct module_info kb8042_sinfo = {
1847c478bd9Sstevel@tonic-gate 	42,		/* Module ID */
1857c478bd9Sstevel@tonic-gate 	module_name,
1867c478bd9Sstevel@tonic-gate 	0, 32,		/* Minimum and maximum packet sizes */
1877c478bd9Sstevel@tonic-gate 	256, 128	/* High and low water marks */
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static struct qinit kb8042_rinit = {
1917c478bd9Sstevel@tonic-gate 	NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static struct qinit kb8042_winit = {
1957c478bd9Sstevel@tonic-gate 	putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate struct streamtab
1997c478bd9Sstevel@tonic-gate 	kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate struct kb8042	Kdws = {0};
202fd9cb95cSsethg static dev_info_t *kb8042_dip = NULL;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2057c478bd9Sstevel@tonic-gate 		void **result);
2067c478bd9Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
207fd9cb95cSsethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_kb8042_ops = {
2107c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_open */
2117c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_close */
2127c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2137c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
2147c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
2157c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
2167c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
2177c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
2187c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2197c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2207c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2217c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2227c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2237c478bd9Sstevel@tonic-gate 	&kb8042_str_info,	/* cb_stream */
2247c478bd9Sstevel@tonic-gate 	D_MP
2257c478bd9Sstevel@tonic-gate };
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate struct dev_ops kb8042_ops = {
2287c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2297c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
2307c478bd9Sstevel@tonic-gate 	kb8042_getinfo,		/* devo_getinfo */
2317c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2327c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2337c478bd9Sstevel@tonic-gate 	kb8042_attach,		/* devo_attach */
234fd9cb95cSsethg 	kb8042_detach,		/* devo_detach */
2357c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
2367c478bd9Sstevel@tonic-gate 	&cb_kb8042_ops,		/* devo_cb_ops */
23719397407SSherry Moore 	(struct bus_ops *)NULL,	/* devo_bus_ops */
23819397407SSherry Moore 	NULL,			/* devo_power */
23919397407SSherry Moore 	ddi_quiesce_not_needed,	/* devo_quiesce */
2407c478bd9Sstevel@tonic-gate };
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2527c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
253bb2d7d5eSSeth Goldberg 	"PS/2 keyboard driver",
2547c478bd9Sstevel@tonic-gate 	&kb8042_ops,	/* driver ops */
2557c478bd9Sstevel@tonic-gate };
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2587c478bd9Sstevel@tonic-gate 	MODREV_1,
2597c478bd9Sstevel@tonic-gate 	(void *) &modldrv,
2607c478bd9Sstevel@tonic-gate 	NULL
2617c478bd9Sstevel@tonic-gate };
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate int
2647c478bd9Sstevel@tonic-gate _init(void)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	int	rv;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	rv = mod_install(&modlinkage);
2697c478bd9Sstevel@tonic-gate 	return (rv);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate int
2747c478bd9Sstevel@tonic-gate _fini(void)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate int
2807c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
285fd9cb95cSsethg #ifdef __sparc
286fd9cb95cSsethg static boolean_t
287fd9cb95cSsethg kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
288fd9cb95cSsethg {
289fd9cb95cSsethg 	int i;
290fd9cb95cSsethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
291fd9cb95cSsethg 	    I8042_INT_INPUT_AVAIL;
292fd9cb95cSsethg 	int reps = timeout_usec / USECS_PER_WAIT;
293fd9cb95cSsethg 
294fd9cb95cSsethg 	for (i = 0; i < reps; i++) {
295fd9cb95cSsethg 		if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
296fd9cb95cSsethg 			return (B_TRUE);
297fd9cb95cSsethg 
298fd9cb95cSsethg 		if (i < (reps - 1))
299fd9cb95cSsethg 			drv_usecwait(USECS_PER_WAIT);
300fd9cb95cSsethg 	}
301fd9cb95cSsethg 	return (B_FALSE);
302fd9cb95cSsethg }
303fd9cb95cSsethg 
304fd9cb95cSsethg static void
305fd9cb95cSsethg kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
306fd9cb95cSsethg {
307fd9cb95cSsethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
308fd9cb95cSsethg 	    I8042_INT_INPUT_DATA;
309fd9cb95cSsethg 
310fd9cb95cSsethg 	while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
311fd9cb95cSsethg 		(void) ddi_get8(kb8042->handle, kb8042->addr + port);
312fd9cb95cSsethg 	}
313fd9cb95cSsethg }
314fd9cb95cSsethg 
315fd9cb95cSsethg static boolean_t
316fd9cb95cSsethg kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
317fd9cb95cSsethg     boolean_t polled, int timeout, int *error, uint8_t *got)
318fd9cb95cSsethg {
319fd9cb95cSsethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
320fd9cb95cSsethg 	    I8042_INT_INPUT_DATA;
321fd9cb95cSsethg 	uint8_t datab;
322fd9cb95cSsethg 	int err;
323fd9cb95cSsethg 	boolean_t rval;
324fd9cb95cSsethg 
325fd9cb95cSsethg 	kb8042_send_to_keyboard(kb8042, send, polled);
326fd9cb95cSsethg 
327fd9cb95cSsethg 	if (kb8042_is_input_avail(kb8042, timeout, polled)) {
328fd9cb95cSsethg 		err = 0;
329fd9cb95cSsethg 		datab = ddi_get8(kb8042->handle, kb8042->addr + port);
330fd9cb95cSsethg 		rval = ((datab == expect) ? B_TRUE : B_FALSE);
331fd9cb95cSsethg 	} else {
332fd9cb95cSsethg 		err = EAGAIN;
333fd9cb95cSsethg 		rval = B_FALSE;
334fd9cb95cSsethg 	}
335fd9cb95cSsethg 
336fd9cb95cSsethg 	if (error != NULL)
337fd9cb95cSsethg 		*error = err;
338fd9cb95cSsethg 	if (got != NULL)
339fd9cb95cSsethg 		*got = datab;
340fd9cb95cSsethg 	return (rval);
341fd9cb95cSsethg }
342fd9cb95cSsethg 
343fd9cb95cSsethg static const char *
344fd9cb95cSsethg kb8042_error_string(int errcode)
345fd9cb95cSsethg {
346fd9cb95cSsethg 	switch (errcode) {
347fd9cb95cSsethg 	case EAGAIN:
348fd9cb95cSsethg 		return ("Timed out");
349fd9cb95cSsethg 	default:
350fd9cb95cSsethg 		return ("Unknown error");
351fd9cb95cSsethg 	}
352fd9cb95cSsethg }
353fd9cb95cSsethg 
354fd9cb95cSsethg static int
355fd9cb95cSsethg kb8042_read_scanset(struct kb8042 *kb8042, boolean_t polled)
356fd9cb95cSsethg {
357fd9cb95cSsethg 	int scanset = -1;
358fd9cb95cSsethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
359fd9cb95cSsethg 	    I8042_INT_INPUT_DATA;
360fd9cb95cSsethg 	int err;
361fd9cb95cSsethg 	uint8_t got;
362fd9cb95cSsethg 
363fd9cb95cSsethg 	kb8042_clear_input_buffer(kb8042, B_TRUE);
364fd9cb95cSsethg 
365fd9cb95cSsethg 	/*
366fd9cb95cSsethg 	 * Send a "change scan code set" command to the keyboard.
367fd9cb95cSsethg 	 * It should respond with an ACK.
368fd9cb95cSsethg 	 */
369fd9cb95cSsethg 	if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, polled,
370fd9cb95cSsethg 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
371fd9cb95cSsethg 		goto fail_read_scanset;
372fd9cb95cSsethg 	}
373fd9cb95cSsethg 
374fd9cb95cSsethg 	/*
375bb2d7d5eSSeth Goldberg 	 * Send a 0.  The keyboard should ACK the 0, then it should send the
376bb2d7d5eSSeth Goldberg 	 * scan code set in use.
377fd9cb95cSsethg 	 */
378fd9cb95cSsethg 	if (kb8042_send_and_expect(kb8042, 0, KB_ACK, polled,
379fd9cb95cSsethg 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
380fd9cb95cSsethg 		goto fail_read_scanset;
381fd9cb95cSsethg 	}
382fd9cb95cSsethg 
383fd9cb95cSsethg 	/*
384fd9cb95cSsethg 	 * The next input byte from the keyboard should be the scan code
385fd9cb95cSsethg 	 * set in use, though some keyboards like to send a few more acks
386fd9cb95cSsethg 	 * just for fun, so blow past those to get the keyboard scan code.
387fd9cb95cSsethg 	 */
388fd9cb95cSsethg 	while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
389fd9cb95cSsethg 	    (scanset = ddi_get8(kb8042->handle, kb8042->addr + port))
390fd9cb95cSsethg 	    == KB_ACK)
391fd9cb95cSsethg 		;
392fd9cb95cSsethg 
393fd9cb95cSsethg #ifdef KD_DEBUG
394fd9cb95cSsethg 	cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
395fd9cb95cSsethg 	    scanset);
396fd9cb95cSsethg #endif
397fd9cb95cSsethg 
398fd9cb95cSsethg 	return (scanset);
399fd9cb95cSsethg 
400fd9cb95cSsethg fail_read_scanset:
401fd9cb95cSsethg #ifdef KD_DEBUG
402fd9cb95cSsethg 	if (err == 0)
403fd9cb95cSsethg 		cmn_err(CE_NOTE, "Could not read current scan set from "
404fd9cb95cSsethg 		    "keyboard: %s. (Expected 0x%x, but got 0x%x).",
405fd9cb95cSsethg 		    kb8042_error_string(err), KB_ACK, got);
406fd9cb95cSsethg 	else
407fd9cb95cSsethg 		cmn_err(CE_NOTE, "Could not read current scan set from "
408fd9cb95cSsethg 		    "keyboard: %s.", kb8042_error_string(err));
409fd9cb95cSsethg #endif
410fd9cb95cSsethg 	return (-1);
411fd9cb95cSsethg }
412fd9cb95cSsethg #endif
413fd9cb95cSsethg 
4147c478bd9Sstevel@tonic-gate static int
4157c478bd9Sstevel@tonic-gate kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	int	rc;
418fd9cb95cSsethg 	int	scanset;
419fd9cb95cSsethg 	int	leds;
4207c478bd9Sstevel@tonic-gate 
421fd9cb95cSsethg 	struct kb8042	*kb8042 = &Kdws;
4227c478bd9Sstevel@tonic-gate 	static ddi_device_acc_attr_t attr = {
4237c478bd9Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
4247c478bd9Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
4257c478bd9Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
4267c478bd9Sstevel@tonic-gate 	};
4277c478bd9Sstevel@tonic-gate 
428fd9cb95cSsethg 	switch (cmd) {
429fd9cb95cSsethg 	case DDI_RESUME:
430fd9cb95cSsethg 		leds = kb8042->leds.commanded;
431fd9cb95cSsethg 		kb8042->w_init = 0;
432fd9cb95cSsethg 		kb8042_init(kb8042, B_TRUE);
433fd9cb95cSsethg 		kb8042_setled(kb8042, leds, B_FALSE);
4342df1fe9cSrandyf 		mutex_enter(&kb8042->w_hw_mutex);
4352df1fe9cSrandyf 		kb8042->suspended = B_FALSE;
4362df1fe9cSrandyf 		if (kb8042->w_qp != NULL) {
4372df1fe9cSrandyf 			enableok(WR(kb8042->w_qp));
4382df1fe9cSrandyf 			qenable(WR(kb8042->w_qp));
4392df1fe9cSrandyf 		}
4402df1fe9cSrandyf 		cv_broadcast(&kb8042->suspend_cv);
4412df1fe9cSrandyf 		mutex_exit(&kb8042->w_hw_mutex);
442fd9cb95cSsethg 		return (DDI_SUCCESS);
443fd9cb95cSsethg 
444fd9cb95cSsethg 	case DDI_ATTACH:
445fd9cb95cSsethg 		if (kb8042_dip != NULL)
4467c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
447fd9cb95cSsethg 		/* The rest of the function is for attach */
448fd9cb95cSsethg 		break;
4497c478bd9Sstevel@tonic-gate 
450fd9cb95cSsethg 	default:
451fd9cb95cSsethg 		return (DDI_FAILURE);
452fd9cb95cSsethg 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	kb8042->debugger.mod1 = 58;	/* Left Ctrl */
4557c478bd9Sstevel@tonic-gate 	kb8042->debugger.mod2 = 60;	/* Left Alt */
4567c478bd9Sstevel@tonic-gate 	kb8042->debugger.trigger = 33;	/* D */
4577c478bd9Sstevel@tonic-gate 	kb8042->debugger.mod1_down = B_FALSE;
4587c478bd9Sstevel@tonic-gate 	kb8042->debugger.mod2_down = B_FALSE;
4597c478bd9Sstevel@tonic-gate 	kb8042->debugger.enabled = B_FALSE;
4607c478bd9Sstevel@tonic-gate 
461fd9cb95cSsethg 	kb8042_dip = devi;
462fd9cb95cSsethg 	kb8042->init_state = KB8042_UNINITIALIZED;
463fd9cb95cSsethg 
4647c478bd9Sstevel@tonic-gate 	kb8042->polled_synthetic_release_pending = B_FALSE;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
4677c478bd9Sstevel@tonic-gate 	    DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
468fd9cb95cSsethg 		goto failure;
4697c478bd9Sstevel@tonic-gate 	}
470fd9cb95cSsethg 
471fd9cb95cSsethg 	kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
4747c478bd9Sstevel@tonic-gate 	    (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
4757c478bd9Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
4767c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
4777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
4787c478bd9Sstevel@tonic-gate #endif
479fd9cb95cSsethg 		goto failure;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
482fd9cb95cSsethg 	kb8042->init_state |= KB8042_REGS_MAPPED;
483fd9cb95cSsethg 
4847c478bd9Sstevel@tonic-gate 	if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
4857c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
4867c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
487fd9cb95cSsethg 		goto failure;
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
4912df1fe9cSrandyf 	cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
4922df1fe9cSrandyf 	cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
493fd9cb95cSsethg 	kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
494fd9cb95cSsethg 
495fd9cb95cSsethg 	kb8042_init(kb8042, B_FALSE);
496fd9cb95cSsethg 
497fd9cb95cSsethg #ifdef __sparc
498fd9cb95cSsethg 	/* Detect the scan code set currently in use */
499fd9cb95cSsethg 	scanset = kb8042_read_scanset(kb8042, B_TRUE);
500fd9cb95cSsethg 
501fd9cb95cSsethg 	if (scanset < 0 && kb8042_warn_unknown_scanset) {
502fd9cb95cSsethg 
503fd9cb95cSsethg 		cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
504fd9cb95cSsethg 		cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
505fd9cb95cSsethg 		cmn_err(CE_CONT, "Defaulting to scan code set %d.  If the "
506fd9cb95cSsethg 		    "keyboard does not ", kb8042_default_scanset);
507fd9cb95cSsethg 		cmn_err(CE_CONT, "work properly, add "
508fd9cb95cSsethg 		    "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
509fd9cb95cSsethg 		    (kb8042_default_scanset == 1) ? 2 : 1);
510fd9cb95cSsethg 		cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
511fd9cb95cSsethg 		    "restart the system.  If you ");
512fd9cb95cSsethg 		cmn_err(CE_CONT, "do not want to see this message in the "
513fd9cb95cSsethg 		    "future, add ");
514fd9cb95cSsethg 		cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
515fd9cb95cSsethg 		    "to /etc/system.\n");
516fd9cb95cSsethg 
517fd9cb95cSsethg 		/* Use the default scan code set. */
518fd9cb95cSsethg 		scanset = kb8042_default_scanset;
519fd9cb95cSsethg 	}
520fd9cb95cSsethg #else
521fd9cb95cSsethg 	/* x86 systems use scan code set 1 -- no detection required */
522fd9cb95cSsethg 	scanset = 1;
523fd9cb95cSsethg #endif
524fd9cb95cSsethg 	if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
525fd9cb95cSsethg 		cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
526fd9cb95cSsethg 		    "Unknown scan code set `%d'.", scanset);
527fd9cb95cSsethg 		/* Scan code set is not supported */
528fd9cb95cSsethg 		goto failure;
529fd9cb95cSsethg 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Turn on interrupts...
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0,
5357c478bd9Sstevel@tonic-gate 	    &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
5367c478bd9Sstevel@tonic-gate 	    kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
5377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
538fd9cb95cSsethg 		goto failure;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
541fd9cb95cSsethg 	kb8042->init_state |= KB8042_INTR_ADDED;
542fd9cb95cSsethg 
5437c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
544fd9cb95cSsethg 
5457c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
546bb2d7d5eSSeth Goldberg 	cmn_err(CE_CONT, "?%s instance #%d READY\n",
54719397407SSherry Moore 	    DRIVER_NAME(devi), ddi_get_instance(devi));
5487c478bd9Sstevel@tonic-gate #endif
549fd9cb95cSsethg 
5507c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
551fd9cb95cSsethg 
552fd9cb95cSsethg failure:
553fd9cb95cSsethg 	kb8042_cleanup(kb8042);
554fd9cb95cSsethg 	return (DDI_FAILURE);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
557fd9cb95cSsethg static int
558fd9cb95cSsethg kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
559fd9cb95cSsethg {
560fd9cb95cSsethg 	struct kb8042 *kb8042 = &Kdws;
561fd9cb95cSsethg 
562fd9cb95cSsethg 	switch (cmd) {
563fd9cb95cSsethg 	case DDI_SUSPEND:
5642df1fe9cSrandyf 		mutex_enter(&kb8042->w_hw_mutex);
5652df1fe9cSrandyf 		ASSERT(kb8042->ops >= 0);
5662df1fe9cSrandyf 		while (kb8042->ops > 0)
5672df1fe9cSrandyf 			cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
5682df1fe9cSrandyf 		kb8042->suspended = B_TRUE;
5692df1fe9cSrandyf 		mutex_exit(&kb8042->w_hw_mutex);
570fd9cb95cSsethg 		return (DDI_SUCCESS);
571fd9cb95cSsethg 
572fd9cb95cSsethg 	case DDI_DETACH:
573fd9cb95cSsethg 		/* If someone has a stream open, fail to detach */
574fd9cb95cSsethg 		if (kb8042->w_qp != NULL)
575fd9cb95cSsethg 			return (DDI_FAILURE);
576fd9cb95cSsethg 
577fd9cb95cSsethg 		ASSERT(kb8042_dip == dip);
578fd9cb95cSsethg 
579fd9cb95cSsethg 		kb8042_cleanup(kb8042);
580fd9cb95cSsethg 
581fd9cb95cSsethg 		return (DDI_SUCCESS);
582fd9cb95cSsethg 
583fd9cb95cSsethg 	default:
584fd9cb95cSsethg 		return (DDI_FAILURE);
585fd9cb95cSsethg 	}
586fd9cb95cSsethg }
587fd9cb95cSsethg 
5887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5897c478bd9Sstevel@tonic-gate static int
5907c478bd9Sstevel@tonic-gate kb8042_getinfo(
5917c478bd9Sstevel@tonic-gate     dev_info_t *dip,
5927c478bd9Sstevel@tonic-gate     ddi_info_cmd_t infocmd,
5937c478bd9Sstevel@tonic-gate     void *arg,
5947c478bd9Sstevel@tonic-gate     void **result)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	register int error;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5997c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6007c478bd9Sstevel@tonic-gate 		if (kb8042_dip == NULL) {
6017c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
6027c478bd9Sstevel@tonic-gate 		} else {
6037c478bd9Sstevel@tonic-gate 			*result = (void *) kb8042_dip;
6047c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6087c478bd9Sstevel@tonic-gate 		*result = (void *)0;
6097c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
6107c478bd9Sstevel@tonic-gate 		break;
6117c478bd9Sstevel@tonic-gate 	default:
6127c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
6137c478bd9Sstevel@tonic-gate 		break;
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	return (error);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static void
619fd9cb95cSsethg kb8042_cleanup(struct kb8042 *kb8042)
620fd9cb95cSsethg {
6210eccbe1bSsethg 	ASSERT(kb8042_dip != NULL);
622fd9cb95cSsethg 
6232df1fe9cSrandyf 	if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
6242df1fe9cSrandyf 		cv_destroy(&kb8042->suspend_cv);
6252df1fe9cSrandyf 		cv_destroy(&kb8042->ops_cv);
626fd9cb95cSsethg 		mutex_destroy(&kb8042->w_hw_mutex);
6272df1fe9cSrandyf 	}
628fd9cb95cSsethg 
629fd9cb95cSsethg 	if (kb8042->init_state & KB8042_INTR_ADDED)
630fd9cb95cSsethg 		ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
631fd9cb95cSsethg 
632fd9cb95cSsethg 	if (kb8042->init_state & KB8042_REGS_MAPPED)
633fd9cb95cSsethg 		ddi_regs_map_free(&kb8042->handle);
634fd9cb95cSsethg 
635fd9cb95cSsethg 	if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
636fd9cb95cSsethg 		ddi_remove_minor_node(kb8042_dip, NULL);
637fd9cb95cSsethg 
638fd9cb95cSsethg 	kb8042->init_state = KB8042_UNINITIALIZED;
6390eccbe1bSsethg 	kb8042_dip = NULL;
640fd9cb95cSsethg }
641fd9cb95cSsethg 
642fd9cb95cSsethg static void
643fd9cb95cSsethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	if (kb8042->w_init)
6467c478bd9Sstevel@tonic-gate 		return;
6477c478bd9Sstevel@tonic-gate 
648fd9cb95cSsethg 	if (!from_resume) {
6497c478bd9Sstevel@tonic-gate 		kb8042->w_kblayout = 0;	/* Default to US */
6507c478bd9Sstevel@tonic-gate 		kb8042->w_qp = (queue_t *)NULL;
651fd9cb95cSsethg 		kb8042->simulated_kbd_type = KB_PC;
652fd9cb95cSsethg 		kb8042->leds.commanded = -1;	/* Unknown initial state */
653fd9cb95cSsethg 		kb8042->leds.desired = -1;	/* Unknown initial state */
654fd9cb95cSsethg 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	kb8042_wait_poweron(kb8042);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	kb8042->kb_old_key_pos = 0;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/* Set up the command state machine and start it running. */
6617c478bd9Sstevel@tonic-gate 	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	kb8042->w_init++;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
6697c478bd9Sstevel@tonic-gate static int
6707c478bd9Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
6737c478bd9Sstevel@tonic-gate 	int err;
6747c478bd9Sstevel@tonic-gate 	int initial_leds;
6757c478bd9Sstevel@tonic-gate 	int initial_led_mask;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	kb8042 = &Kdws;
6787c478bd9Sstevel@tonic-gate 
6792df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
6802df1fe9cSrandyf 	while (kb8042->suspended) {
6812df1fe9cSrandyf 		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
6822df1fe9cSrandyf 		    0) {
6832df1fe9cSrandyf 			mutex_exit(&kb8042->w_hw_mutex);
6842df1fe9cSrandyf 			return (EINTR);
6852df1fe9cSrandyf 		}
6862df1fe9cSrandyf 	}
6872df1fe9cSrandyf 
6887c478bd9Sstevel@tonic-gate 	kb8042->w_dev = *devp;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (qp->q_ptr) {
6912df1fe9cSrandyf 		mutex_exit(&kb8042->w_hw_mutex);
6927c478bd9Sstevel@tonic-gate 		return (0);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 	qp->q_ptr = (caddr_t)kb8042;
6957c478bd9Sstevel@tonic-gate 	WR(qp)->q_ptr = qp->q_ptr;
6967c478bd9Sstevel@tonic-gate 	if (!kb8042->w_qp)
6977c478bd9Sstevel@tonic-gate 		kb8042->w_qp = qp;
6987c478bd9Sstevel@tonic-gate 
6992df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
7002df1fe9cSrandyf 	kb8042->ops++;
7012df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
7022df1fe9cSrandyf 
7037c478bd9Sstevel@tonic-gate 	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
7047c478bd9Sstevel@tonic-gate 	err = kbtrans_streams_init(qp, sflag, credp,
7057c478bd9Sstevel@tonic-gate 	    (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
7067c478bd9Sstevel@tonic-gate 	    &kb8042->hw_kbtrans,
7077c478bd9Sstevel@tonic-gate 	    initial_leds, initial_led_mask);
7087c478bd9Sstevel@tonic-gate 	if (err != 0)
7097c478bd9Sstevel@tonic-gate 		return (err);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
7147c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_argument =
715281f0747Slt200341 	    (cons_polledio_arg_t)kb8042;
7167c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_putchar = NULL;
7177c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_getchar =
718281f0747Slt200341 	    (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
7197c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_ischar =
720281f0747Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
7217c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_enter = NULL;
7227c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_exit = NULL;
7237c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_setled =
724281f0747Slt200341 	    (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
7257c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_keycheck =
726281f0747Slt200341 	    (boolean_t (*)(cons_polledio_arg_t, int *,
7277c478bd9Sstevel@tonic-gate 	    enum keystate *))kb8042_polled_keycheck;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	qprocson(qp);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	kbtrans_streams_enable(kb8042->hw_kbtrans);
7327c478bd9Sstevel@tonic-gate 
7332df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
7342df1fe9cSrandyf 	ASSERT(kb8042->ops > 0);
7352df1fe9cSrandyf 	kb8042->ops--;
7362df1fe9cSrandyf 	if (kb8042->ops == 0)
7372df1fe9cSrandyf 		cv_broadcast(&kb8042->ops_cv);
7382df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
7392df1fe9cSrandyf 
7407c478bd9Sstevel@tonic-gate 	return (0);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
7447c478bd9Sstevel@tonic-gate static int
7457c478bd9Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
7487c478bd9Sstevel@tonic-gate 
749c35aa225Smarx 	/* If a beep is in progress, stop that */
750c35aa225Smarx 	(void) beeper_off();
751c35aa225Smarx 
7527c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7537c478bd9Sstevel@tonic-gate 
7542df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
7552df1fe9cSrandyf 	while (kb8042->suspended) {
7562df1fe9cSrandyf 		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
7572df1fe9cSrandyf 		    0) {
7582df1fe9cSrandyf 			mutex_exit(&kb8042->w_hw_mutex);
7592df1fe9cSrandyf 			return (EINTR);
7602df1fe9cSrandyf 		}
7612df1fe9cSrandyf 	}
7622df1fe9cSrandyf 
7632df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
7642df1fe9cSrandyf 	kb8042->ops++;
7652df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
7662df1fe9cSrandyf 
767e1fec4f9Sry162471 	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);
768e1fec4f9Sry162471 
7697c478bd9Sstevel@tonic-gate 	kb8042->w_qp = (queue_t *)NULL;
7707c478bd9Sstevel@tonic-gate 	qprocsoff(qp);
7717c478bd9Sstevel@tonic-gate 
7722df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
7732df1fe9cSrandyf 	ASSERT(kb8042->ops > 0);
7742df1fe9cSrandyf 	kb8042->ops--;
7752df1fe9cSrandyf 	if (kb8042->ops == 0)
7762df1fe9cSrandyf 		cv_broadcast(&kb8042->ops_cv);
7772df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
7782df1fe9cSrandyf 
7797c478bd9Sstevel@tonic-gate 	return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate static int
7837c478bd9Sstevel@tonic-gate kb8042_wsrv(queue_t *qp)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
7882df1fe9cSrandyf 	boolean_t suspended;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7917c478bd9Sstevel@tonic-gate 
7922df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
7932df1fe9cSrandyf 	suspended = kb8042->suspended;
7942df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
7952df1fe9cSrandyf 	if (!suspended)
7962df1fe9cSrandyf 		kb8042->ops++;
7972df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
7982df1fe9cSrandyf 
7992df1fe9cSrandyf #ifdef NO_KB_DEBUG
8002df1fe9cSrandyf 	while (!suspended && (mp = getq(qp)) != NULL) {
8012df1fe9cSrandyf #else
8022df1fe9cSrandyf 	/*
8032df1fe9cSrandyf 	 * Not taking keyboard input while suspending can make debugging
8042df1fe9cSrandyf 	 * difficult.  However, we still do the ops counting so that we
8052df1fe9cSrandyf 	 * don't suspend at a bad time.
8062df1fe9cSrandyf 	 */
8077c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp))) {
8082df1fe9cSrandyf #endif
8097c478bd9Sstevel@tonic-gate 		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
8107c478bd9Sstevel@tonic-gate 		case KBTRANS_MESSAGE_HANDLED:
8117c478bd9Sstevel@tonic-gate 			continue;
8127c478bd9Sstevel@tonic-gate 		case KBTRANS_MESSAGE_NOT_HANDLED:
8137c478bd9Sstevel@tonic-gate 			break;
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
8167c478bd9Sstevel@tonic-gate 		case M_IOCTL:
8177c478bd9Sstevel@tonic-gate 			kb8042_ioctlmsg(kb8042, qp, mp);
8187c478bd9Sstevel@tonic-gate 			continue;
8197c478bd9Sstevel@tonic-gate 		case M_IOCDATA:
8207c478bd9Sstevel@tonic-gate 			kb8042_iocdatamsg(qp, mp);
8217c478bd9Sstevel@tonic-gate 			continue;
8227c478bd9Sstevel@tonic-gate 		case M_DELAY:
8237c478bd9Sstevel@tonic-gate 		case M_STARTI:
8247c478bd9Sstevel@tonic-gate 		case M_STOPI:
8257c478bd9Sstevel@tonic-gate 		case M_READ:	/* ignore, no buffered data */
8267c478bd9Sstevel@tonic-gate 			freemsg(mp);
8277c478bd9Sstevel@tonic-gate 			continue;
8287c478bd9Sstevel@tonic-gate 		case M_FLUSH:
8297c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
8307c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR)
8317c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
8327c478bd9Sstevel@tonic-gate 			else
8337c478bd9Sstevel@tonic-gate 				freemsg(mp);
8347c478bd9Sstevel@tonic-gate 			continue;
8357c478bd9Sstevel@tonic-gate 		default:
8367c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
8377c478bd9Sstevel@tonic-gate 			    mp->b_datap->db_type);
8387c478bd9Sstevel@tonic-gate 			freemsg(mp);
8397c478bd9Sstevel@tonic-gate 			continue;
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	}
8422df1fe9cSrandyf 
8432df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
8442df1fe9cSrandyf 	if (!suspended) {
8452df1fe9cSrandyf 		ASSERT(kb8042->ops > 0);
8462df1fe9cSrandyf 		kb8042->ops--;
8472df1fe9cSrandyf 		if (kb8042->ops == 0)
8482df1fe9cSrandyf 			cv_broadcast(&kb8042->ops_cv);
8492df1fe9cSrandyf 	}
8502df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
8512df1fe9cSrandyf 
8527c478bd9Sstevel@tonic-gate 	return (0);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate static void
8567c478bd9Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
8597c478bd9Sstevel@tonic-gate 	mblk_t		*datap;
8607c478bd9Sstevel@tonic-gate 	int		error;
8617c478bd9Sstevel@tonic-gate 	int		tmp;
862c35aa225Smarx 	int		cycles;
863c35aa225Smarx 	int		frequency;
864c35aa225Smarx 	int		msecs;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
8717c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
8727c478bd9Sstevel@tonic-gate 		if (error != 0) {
8737c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
8747c478bd9Sstevel@tonic-gate 			return;
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		/*
8787c478bd9Sstevel@tonic-gate 		 * We are given an appropriate-sized data block,
8797c478bd9Sstevel@tonic-gate 		 * and return a pointer to our structure in it.
8807c478bd9Sstevel@tonic-gate 		 */
8817c478bd9Sstevel@tonic-gate 		*(struct cons_polledio **)mp->b_cont->b_rptr =
8827c478bd9Sstevel@tonic-gate 		    &kb8042->polledio;
8837c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
8847c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
8857c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
8867c478bd9Sstevel@tonic-gate 		break;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
8897c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8907c478bd9Sstevel@tonic-gate 		break;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8937c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8947c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8957c478bd9Sstevel@tonic-gate 			return;
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
8997c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * Valid only in TR_UNTRANS_MODE mode.
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate 	case CONSSETKBDTYPE:
9067c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9077c478bd9Sstevel@tonic-gate 		if (error != 0) {
9087c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
9097c478bd9Sstevel@tonic-gate 			return;
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		tmp =  *(int *)mp->b_cont->b_rptr;
9127c478bd9Sstevel@tonic-gate 		if (tmp != KB_PC && tmp != KB_USB) {
9137c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9147c478bd9Sstevel@tonic-gate 			break;
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate 		kb8042->simulated_kbd_type = tmp;
9177c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9187c478bd9Sstevel@tonic-gate 		break;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	case KIOCLAYOUT:
9217c478bd9Sstevel@tonic-gate 		if (kb8042->w_kblayout == -1) {
9227c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9237c478bd9Sstevel@tonic-gate 			return;
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9277c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, ENOMEM);
9287c478bd9Sstevel@tonic-gate 			return;
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB)
9327c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9337c478bd9Sstevel@tonic-gate 		else
9347c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = kb8042->w_kblayout;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9377c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
9387c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9397c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9407c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
9417c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
9427c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
9437c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
9447c478bd9Sstevel@tonic-gate 		break;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	case KIOCSLAYOUT:
9477c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9487c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9497c478bd9Sstevel@tonic-gate 			return;
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
9537c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9547c478bd9Sstevel@tonic-gate 		break;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	case KIOCCMD:
9577c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9587c478bd9Sstevel@tonic-gate 		if (error != 0) {
9597c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
9607c478bd9Sstevel@tonic-gate 			return;
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
9647c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9657c478bd9Sstevel@tonic-gate 		break;
9667c478bd9Sstevel@tonic-gate 
967c35aa225Smarx 	case KIOCMKTONE:
968c35aa225Smarx 		if (iocp->ioc_count != TRANSPARENT) {
969c35aa225Smarx 			miocnak(qp, mp, 0, EINVAL);
970c35aa225Smarx 			return;
971c35aa225Smarx 		}
972c35aa225Smarx 
973c35aa225Smarx 		tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
974c35aa225Smarx 		cycles = tmp & 0xffff;
975c35aa225Smarx 		msecs = (tmp >> 16) & 0xffff;
976c35aa225Smarx 
977c35aa225Smarx 		if (cycles == 0)
978c35aa225Smarx 			frequency = UINT16_MAX;
979c35aa225Smarx 		else if (cycles == UINT16_MAX)
980c35aa225Smarx 			frequency = 0;
981c35aa225Smarx 		else {
982c35aa225Smarx 			frequency = (PIT_HZ + cycles / 2) / cycles;
983c35aa225Smarx 			if (frequency > UINT16_MAX)
984c35aa225Smarx 				frequency = UINT16_MAX;
985c35aa225Smarx 		}
986c35aa225Smarx 
987c35aa225Smarx 		error = beep_mktone(frequency, msecs);
988c35aa225Smarx 		if (error != 0)
989c35aa225Smarx 			miocnak(qp, mp, 0, error);
990c35aa225Smarx 		else
991c35aa225Smarx 			miocack(qp, mp, 0, 0);
992c35aa225Smarx 		break;
993c35aa225Smarx 
9947c478bd9Sstevel@tonic-gate 	default:
9957c478bd9Sstevel@tonic-gate #ifdef DEBUG1
9967c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
9977c478bd9Sstevel@tonic-gate #endif
9987c478bd9Sstevel@tonic-gate 		miocnak(qp, mp, 0, EINVAL);
9997c478bd9Sstevel@tonic-gate 		return;
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate /*
10047c478bd9Sstevel@tonic-gate  * Process a byte received from the keyboard
10057c478bd9Sstevel@tonic-gate  */
10067c478bd9Sstevel@tonic-gate static void
10077c478bd9Sstevel@tonic-gate kb8042_received_byte(
10087c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042,
10097c478bd9Sstevel@tonic-gate 	int		scancode)	/* raw scan code */
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	boolean_t	legit;		/* is this a legit key pos'n? */
10127c478bd9Sstevel@tonic-gate 	int		key_pos = -1;
10137c478bd9Sstevel@tonic-gate 	enum keystate	state;
10147c478bd9Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10177c478bd9Sstevel@tonic-gate 	kb8042_debug_hotkey(scancode);
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (!kb8042->w_init)	/* can't do anything anyway */
10217c478bd9Sstevel@tonic-gate 		return;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
10247c478bd9Sstevel@tonic-gate 	    &synthetic_release_needed);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if (legit == 0) {
10277c478bd9Sstevel@tonic-gate 		/* Eaten by translation */
10287c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10297c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10307c478bd9Sstevel@tonic-gate 			prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode);
10317c478bd9Sstevel@tonic-gate #endif
10327c478bd9Sstevel@tonic-gate 		return;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10367c478bd9Sstevel@tonic-gate 	if (kb8042_debug) {
10377c478bd9Sstevel@tonic-gate 		prom_printf("kb8042_intr:  0x%x -> %s %d",
10387c478bd9Sstevel@tonic-gate 		    scancode,
10397c478bd9Sstevel@tonic-gate 		    state == KEY_RELEASED ? "released" : "pressed",
10407c478bd9Sstevel@tonic-gate 		    key_pos);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate #endif
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * Don't know if we want this permanently, but it seems interesting
10467c478bd9Sstevel@tonic-gate 	 * for the moment.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod1) {
10497c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10507c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10517c478bd9Sstevel@tonic-gate 			prom_printf(" -> debug mod1");
10527c478bd9Sstevel@tonic-gate #endif
10537c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod2) {
10567c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10577c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10587c478bd9Sstevel@tonic-gate 			prom_printf(" -> debug mod2");
10597c478bd9Sstevel@tonic-gate #endif
10607c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 	if (kb8042->debugger.enabled &&
10637c478bd9Sstevel@tonic-gate 	    key_pos == kb8042->debugger.trigger &&
10647c478bd9Sstevel@tonic-gate 	    kb8042->debugger.mod1_down &&
10657c478bd9Sstevel@tonic-gate 	    kb8042->debugger.mod2_down) {
10667c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10677c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10687c478bd9Sstevel@tonic-gate 			prom_printf(" -> debugger\n");
10697c478bd9Sstevel@tonic-gate #endif
10707c478bd9Sstevel@tonic-gate 		/*
10717c478bd9Sstevel@tonic-gate 		 * Require new presses of the modifiers.
10727c478bd9Sstevel@tonic-gate 		 */
10737c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod1_down = B_FALSE;
10747c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod2_down = B_FALSE;
10757c478bd9Sstevel@tonic-gate 		abort_sequence_enter(NULL);
10767c478bd9Sstevel@tonic-gate 		return;
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	/*
10807c478bd9Sstevel@tonic-gate 	 * If there's no queue above us - as can happen if we've been
10817c478bd9Sstevel@tonic-gate 	 * attached but not opened - drop the keystroke.
10827c478bd9Sstevel@tonic-gate 	 * Note that we do this here instead of above so that
10837c478bd9Sstevel@tonic-gate 	 * Ctrl-Alt-D still works.
10847c478bd9Sstevel@tonic-gate 	 */
10857c478bd9Sstevel@tonic-gate 	if (kb8042->w_qp == NULL) {
10867c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10877c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10887c478bd9Sstevel@tonic-gate 			prom_printf(" -> nobody home\n");
10897c478bd9Sstevel@tonic-gate #endif
10907c478bd9Sstevel@tonic-gate 		return;
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/*
10947c478bd9Sstevel@tonic-gate 	 * This is to filter out auto repeat since it can't be
10957c478bd9Sstevel@tonic-gate 	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
10967c478bd9Sstevel@tonic-gate 	 * can.  Don't know whether they've taken over the world.
10977c478bd9Sstevel@tonic-gate 	 * Don't think so.)
10987c478bd9Sstevel@tonic-gate 	 */
10997c478bd9Sstevel@tonic-gate 	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
11007c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
11017c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
11027c478bd9Sstevel@tonic-gate 			prom_printf(" -> autorepeat ignored\n");
11037c478bd9Sstevel@tonic-gate #endif
11047c478bd9Sstevel@tonic-gate 		return;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
11087c478bd9Sstevel@tonic-gate 	if (kb8042_debug)
11097c478bd9Sstevel@tonic-gate 		prom_printf(" -> OK\n");
11107c478bd9Sstevel@tonic-gate #endif
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11137c478bd9Sstevel@tonic-gate 	if (kb8042_pressrelease_debug) {
11147c478bd9Sstevel@tonic-gate 		prom_printf(" %s%d ",
11157c478bd9Sstevel@tonic-gate 		    state == KEY_PRESSED ? "+" : "-",
11167c478bd9Sstevel@tonic-gate 		    key_pos);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate #endif
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * This is a total hack.  For some stupid reason, the two additional
11247c478bd9Sstevel@tonic-gate 	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
11257c478bd9Sstevel@tonic-gate 	 * only.  We synthesize a release immediately.
11267c478bd9Sstevel@tonic-gate 	 */
11277c478bd9Sstevel@tonic-gate 	if (synthetic_release_needed) {
11287c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11297c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
11307c478bd9Sstevel@tonic-gate 			prom_printf("synthetic release %d\n", key_pos);
11317c478bd9Sstevel@tonic-gate 		if (kb8042_pressrelease_debug)
11327c478bd9Sstevel@tonic-gate 			prom_printf(" -%d(s) ", key_pos);
11337c478bd9Sstevel@tonic-gate #endif
11347c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
11357c478bd9Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate static void
11417c478bd9Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
11427c478bd9Sstevel@tonic-gate     enum keystate state)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	kbtrans_key_t key;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	ASSERT(key_pos >= 0 && key_pos <= 255);
11477c478bd9Sstevel@tonic-gate 	if (kb8042->simulated_kbd_type == KB_PC) {
11487c478bd9Sstevel@tonic-gate 		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
11497c478bd9Sstevel@tonic-gate 	} else if (kb8042->simulated_kbd_type == KB_USB) {
11507c478bd9Sstevel@tonic-gate 		key = keytab_pc2usb[key_pos];
11517c478bd9Sstevel@tonic-gate 		if (key != 0) {
11527c478bd9Sstevel@tonic-gate 			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate /*
11587c478bd9Sstevel@tonic-gate  * Called from interrupt handler when keyboard interrupt occurs.
11597c478bd9Sstevel@tonic-gate  */
11607c478bd9Sstevel@tonic-gate static uint_t
11617c478bd9Sstevel@tonic-gate kb8042_intr(caddr_t arg)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	uchar_t scancode;	/* raw scan code */
11647c478bd9Sstevel@tonic-gate 	int rc;
11657c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)arg;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	rc = DDI_INTR_UNCLAIMED;
11687c478bd9Sstevel@tonic-gate 
1169fd9cb95cSsethg 	if (kb8042->init_state == KB8042_UNINITIALIZED)
1170fd9cb95cSsethg 		return (DDI_INTR_UNCLAIMED);
1171fd9cb95cSsethg 
11727c478bd9Sstevel@tonic-gate 	/* don't care if drv_setparm succeeds */
11737c478bd9Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
11767c478bd9Sstevel@tonic-gate 	    != 0) {
11777c478bd9Sstevel@tonic-gate 		rc = DDI_INTR_CLAIMED;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
11807c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_DATA);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11837c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
11847c478bd9Sstevel@tonic-gate 			prom_printf(" <K:%x ", scancode);
11857c478bd9Sstevel@tonic-gate #endif
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 		kb8042_received_byte(kb8042, scancode);
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	return (rc);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate static void
11947c478bd9Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 	struct copyresp	*csp;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
11997c478bd9Sstevel@tonic-gate 	if (csp->cp_rval) {
12007c478bd9Sstevel@tonic-gate 		freemsg(mp);
12017c478bd9Sstevel@tonic-gate 		return;
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	switch (csp->cp_cmd) {
12057c478bd9Sstevel@tonic-gate 	default:
12067c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
12077c478bd9Sstevel@tonic-gate 		break;
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate static boolean_t
12127c478bd9Sstevel@tonic-gate kb8042_polled_keycheck(
12137c478bd9Sstevel@tonic-gate     struct kbtrans_hardware *hw,
12147c478bd9Sstevel@tonic-gate     int *key,
12157c478bd9Sstevel@tonic-gate     enum keystate *state)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
12187c478bd9Sstevel@tonic-gate 	int	scancode;
12197c478bd9Sstevel@tonic-gate 	boolean_t	legit;
12207c478bd9Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	if (kb8042->polled_synthetic_release_pending) {
12237c478bd9Sstevel@tonic-gate 		*key = kb8042->polled_synthetic_release_key;
12247c478bd9Sstevel@tonic-gate 		*state = KEY_RELEASED;
12257c478bd9Sstevel@tonic-gate 		kb8042->polled_synthetic_release_pending = B_FALSE;
12267c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
12277c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug)
12287c478bd9Sstevel@tonic-gate 			prom_printf("synthetic release 0x%x\n", *key);
12297c478bd9Sstevel@tonic-gate #endif
12307c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
12317c478bd9Sstevel@tonic-gate 		return (B_TRUE);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	for (;;) {
12357c478bd9Sstevel@tonic-gate 		if (ddi_get8(kb8042->handle,
12367c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
12377c478bd9Sstevel@tonic-gate 			return (B_FALSE);
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
12417c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_INPUT_DATA);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
12447c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
12457c478bd9Sstevel@tonic-gate 			prom_printf(" g<%x ", scancode);
12467c478bd9Sstevel@tonic-gate #endif
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12497c478bd9Sstevel@tonic-gate 		kb8042_debug_hotkey(scancode);
12507c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug)
12517c478bd9Sstevel@tonic-gate 			prom_printf("polled 0x%x", scancode);
12527c478bd9Sstevel@tonic-gate #endif
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		legit = KeyboardConvertScan(kb8042, scancode, key, state,
12557c478bd9Sstevel@tonic-gate 		    &synthetic_release_needed);
12567c478bd9Sstevel@tonic-gate 		if (!legit) {
12577c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12587c478bd9Sstevel@tonic-gate 			if (kb8042_getchar_debug)
12597c478bd9Sstevel@tonic-gate 				prom_printf(" -> ignored\n");
12607c478bd9Sstevel@tonic-gate #endif
12617c478bd9Sstevel@tonic-gate 			continue;
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12647c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug) {
12657c478bd9Sstevel@tonic-gate 			prom_printf(" -> %s %d\n",
12667c478bd9Sstevel@tonic-gate 			    *state == KEY_PRESSED ? "pressed" : "released",
12677c478bd9Sstevel@tonic-gate 			    *key);
12687c478bd9Sstevel@tonic-gate 		}
12697c478bd9Sstevel@tonic-gate #endif
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * For the moment at least, we rely on hardware autorepeat
12727c478bd9Sstevel@tonic-gate 		 * for polled I/O autorepeat.  However, for coordination
12737c478bd9Sstevel@tonic-gate 		 * with the interrupt-driven code, maintain the last key
12747c478bd9Sstevel@tonic-gate 		 * pressed.
12757c478bd9Sstevel@tonic-gate 		 */
12767c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		/*
12797c478bd9Sstevel@tonic-gate 		 * This is a total hack to support two additional keys
12807c478bd9Sstevel@tonic-gate 		 * on Korean keyboards.  They report only on press, and
12817c478bd9Sstevel@tonic-gate 		 * so we synthesize a release.  Most likely this will
12827c478bd9Sstevel@tonic-gate 		 * never be important to polled  I/O, but if I do it
12837c478bd9Sstevel@tonic-gate 		 * "right" the first time it _won't_ be an issue.
12847c478bd9Sstevel@tonic-gate 		 */
12857c478bd9Sstevel@tonic-gate 		if (synthetic_release_needed) {
12867c478bd9Sstevel@tonic-gate 			kb8042->polled_synthetic_release_pending = B_TRUE;
12877c478bd9Sstevel@tonic-gate 			kb8042->polled_synthetic_release_key = *key;
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB) {
12917c478bd9Sstevel@tonic-gate 			*key = keytab_pc2usb[*key];
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 		return (B_TRUE);
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate static void
1298fd9cb95cSsethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1299fd9cb95cSsethg {
1300fd9cb95cSsethg 	kb8042->leds.desired = led_state;
1301fd9cb95cSsethg 
1302fd9cb95cSsethg 	if (!polled)
1303fd9cb95cSsethg 		mutex_enter(&kb8042->w_hw_mutex);
1304fd9cb95cSsethg 
1305bb2d7d5eSSeth Goldberg 	if (kb8042->leds.desired != kb8042->leds.commanded) {
1306bb2d7d5eSSeth Goldberg 		kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1307bb2d7d5eSSeth Goldberg 	}
1308fd9cb95cSsethg 
1309fd9cb95cSsethg 	if (!polled)
1310fd9cb95cSsethg 		mutex_exit(&kb8042->w_hw_mutex);
1311fd9cb95cSsethg }
1312fd9cb95cSsethg 
1313fd9cb95cSsethg static void
13147c478bd9Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
13157c478bd9Sstevel@tonic-gate {
13167c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1317fd9cb95cSsethg 	kb8042_setled(kb8042, led_state, B_TRUE);
1318fd9cb95cSsethg }
13197c478bd9Sstevel@tonic-gate 
1320fd9cb95cSsethg static void
1321fd9cb95cSsethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1322fd9cb95cSsethg {
1323fd9cb95cSsethg 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1324fd9cb95cSsethg 	kb8042_setled(kb8042, led_state, B_FALSE);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate static void
13287c478bd9Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
13297c478bd9Sstevel@tonic-gate {
1330fbac6366SSeth Goldberg 	uint8_t led_cmd[4];
1331bb2d7d5eSSeth Goldberg 
1332bb2d7d5eSSeth Goldberg 	/*
1333bb2d7d5eSSeth Goldberg 	 * KB_SET_LED and KB_ENABLE are special commands for which the nexus
1334bb2d7d5eSSeth Goldberg 	 * driver is requested to wait for responses before proceeding.
1335bb2d7d5eSSeth Goldberg 	 * KB_SET_LED also provides an option byte for the nexus to send to
1336bb2d7d5eSSeth Goldberg 	 * the keyboard after the acknowledgement.
1337bb2d7d5eSSeth Goldberg 	 *
1338bb2d7d5eSSeth Goldberg 	 * Other commands/data are sent using the single put8 I/O access
1339bb2d7d5eSSeth Goldberg 	 * function.
1340bb2d7d5eSSeth Goldberg 	 */
1341bb2d7d5eSSeth Goldberg 	if (byte == KB_SET_LED) {
1342bb2d7d5eSSeth Goldberg 		/*
1343bb2d7d5eSSeth Goldberg 		 * Initialize the buffer used with _rep_put8.  We
1344bb2d7d5eSSeth Goldberg 		 * expect an ACK after the SET_LED command, at which point
1345bb2d7d5eSSeth Goldberg 		 * the LED byte should be sent to the keyboard.
1346bb2d7d5eSSeth Goldberg 		 */
1347bb2d7d5eSSeth Goldberg 		led_cmd[0] = KB_SET_LED;
1348bb2d7d5eSSeth Goldberg 		led_cmd[1] = KB_ACK;
1349fbac6366SSeth Goldberg 		led_cmd[2] = KB_RESEND;
1350fbac6366SSeth Goldberg 		led_cmd[3] = kb8042_xlate_leds(kb8042->leds.desired);
1351bb2d7d5eSSeth Goldberg 		if (polled) {
1352bb2d7d5eSSeth Goldberg 			ddi_rep_put8(kb8042->handle, &led_cmd[0],
1353fbac6366SSeth Goldberg 			    kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 4, 0);
1354bb2d7d5eSSeth Goldberg 		} else {
1355bb2d7d5eSSeth Goldberg 			ddi_rep_put8(kb8042->handle, &led_cmd[0],
1356fbac6366SSeth Goldberg 			    kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 4, 0);
1357bb2d7d5eSSeth Goldberg 		}
1358bb2d7d5eSSeth Goldberg 		kb8042->leds.commanded = kb8042->leds.desired;
1359bb2d7d5eSSeth Goldberg 
1360bb2d7d5eSSeth Goldberg 	} else if (byte == KB_ENABLE) {
1361bb2d7d5eSSeth Goldberg 
1362bb2d7d5eSSeth Goldberg 		/*
1363bb2d7d5eSSeth Goldberg 		 * Initialize the buffer used with _rep_put8.  We
1364bb2d7d5eSSeth Goldberg 		 * expect an ACK after the KB_ENABLE command.
1365bb2d7d5eSSeth Goldberg 		 */
1366bb2d7d5eSSeth Goldberg 		led_cmd[0] = KB_ENABLE;
1367bb2d7d5eSSeth Goldberg 		led_cmd[1] = KB_ACK;
1368fbac6366SSeth Goldberg 		led_cmd[2] = KB_RESEND;
1369bb2d7d5eSSeth Goldberg 		if (polled) {
1370bb2d7d5eSSeth Goldberg 			ddi_rep_put8(kb8042->handle, &led_cmd[0],
1371fbac6366SSeth Goldberg 			    kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 3, 0);
1372bb2d7d5eSSeth Goldberg 		} else {
1373bb2d7d5eSSeth Goldberg 			ddi_rep_put8(kb8042->handle, &led_cmd[0],
1374fbac6366SSeth Goldberg 			    kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 3, 0);
1375bb2d7d5eSSeth Goldberg 		}
1376bb2d7d5eSSeth Goldberg 	} else {
1377bb2d7d5eSSeth Goldberg 		/* All other commands use the "normal" virtual output port */
13787c478bd9Sstevel@tonic-gate 		if (polled) {
13797c478bd9Sstevel@tonic-gate 			ddi_put8(kb8042->handle,
13807c478bd9Sstevel@tonic-gate 			    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
13817c478bd9Sstevel@tonic-gate 		} else {
13827c478bd9Sstevel@tonic-gate 			ddi_put8(kb8042->handle,
13837c478bd9Sstevel@tonic-gate 			    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
13847c478bd9Sstevel@tonic-gate 		}
1385bb2d7d5eSSeth Goldberg 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
13887c478bd9Sstevel@tonic-gate 	if (kb8042_low_level_debug)
13897c478bd9Sstevel@tonic-gate 		prom_printf(" >K:%x ", byte);
13907c478bd9Sstevel@tonic-gate #endif
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /*
13947c478bd9Sstevel@tonic-gate  * Wait until the keyboard is fully up, maybe.
13957c478bd9Sstevel@tonic-gate  * We may be the first person to talk to the keyboard, in which case
13967c478bd9Sstevel@tonic-gate  * it's patiently waiting to say "AA" to us to tell us it's up.
13977c478bd9Sstevel@tonic-gate  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
13987c478bd9Sstevel@tonic-gate  * good bet that we've already spent that long getting to that point,
13997c478bd9Sstevel@tonic-gate  * so we'll only wait long enough for the communications electronics to
14007c478bd9Sstevel@tonic-gate  * run.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate static void
14037c478bd9Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	int cnt;
14067c478bd9Sstevel@tonic-gate 	int ready;
14077c478bd9Sstevel@tonic-gate 	unsigned char byt;
14087c478bd9Sstevel@tonic-gate 
1409fd9cb95cSsethg 	/* wait for up to 250 ms for a response */
14107c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < 250; cnt++) {
14117c478bd9Sstevel@tonic-gate 		ready = ddi_get8(kb8042->handle,
14127c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_AVAIL);
14137c478bd9Sstevel@tonic-gate 		if (ready != 0)
14147c478bd9Sstevel@tonic-gate 			break;
14157c478bd9Sstevel@tonic-gate 		drv_usecwait(1000);
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/*
14197c478bd9Sstevel@tonic-gate 	 * If there's something pending, read and discard it.  If not,
14207c478bd9Sstevel@tonic-gate 	 * assume things are OK anyway - maybe somebody else ate it
14217c478bd9Sstevel@tonic-gate 	 * already.  (On a PC, the BIOS almost certainly did.)
14227c478bd9Sstevel@tonic-gate 	 */
14237c478bd9Sstevel@tonic-gate 	if (ready != 0) {
14247c478bd9Sstevel@tonic-gate 		byt = ddi_get8(kb8042->handle,
14257c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_DATA);
14267c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
14277c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
14287c478bd9Sstevel@tonic-gate 			prom_printf(" <K:%x ", byt);
14297c478bd9Sstevel@tonic-gate #endif
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate static int
14347c478bd9Sstevel@tonic-gate kb8042_xlate_leds(int led)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	int res;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	res = 0;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (led & LED_NUM_LOCK)
14417c478bd9Sstevel@tonic-gate 		res |= LED_NUM;
14427c478bd9Sstevel@tonic-gate 	if (led & LED_SCROLL_LOCK)
14437c478bd9Sstevel@tonic-gate 		res |= LED_SCR;
14447c478bd9Sstevel@tonic-gate 	if (led & LED_CAPS_LOCK)
14457c478bd9Sstevel@tonic-gate 		res |= LED_CAP;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	return (res);
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14517c478bd9Sstevel@tonic-gate static void
14527c478bd9Sstevel@tonic-gate kb8042_get_initial_leds(
14537c478bd9Sstevel@tonic-gate     struct kb8042 *kb8042,
14547c478bd9Sstevel@tonic-gate     int *initial_leds,
14557c478bd9Sstevel@tonic-gate     int *initial_led_mask)
14567c478bd9Sstevel@tonic-gate {
1457bb2d7d5eSSeth Goldberg #if defined(__i386) || defined(__amd64)
14587c478bd9Sstevel@tonic-gate 	extern caddr_t	p0_va;
14597c478bd9Sstevel@tonic-gate 	uint8_t		bios_kb_flag;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	bios_kb_flag = p0_va[BIOS_KB_FLAG];
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
14647c478bd9Sstevel@tonic-gate 	*initial_leds = 0;
14657c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_CAPS_STATE)
14667c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_CAPS_LOCK;
14677c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_NUM_STATE)
14687c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_NUM_LOCK;
14697c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_SCROLL_STATE)
14707c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_SCROLL_LOCK;
14717c478bd9Sstevel@tonic-gate #else
14727c478bd9Sstevel@tonic-gate 	*initial_leds = 0;
14737c478bd9Sstevel@tonic-gate 	*initial_led_mask = 0;
14747c478bd9Sstevel@tonic-gate #endif
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
14787c478bd9Sstevel@tonic-gate static void
14797c478bd9Sstevel@tonic-gate kb8042_debug_hotkey(int scancode)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	if (!kb8042_enable_debug_hotkey)
14827c478bd9Sstevel@tonic-gate 		return;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	switch (scancode) {
14857c478bd9Sstevel@tonic-gate 	case 0x44:	/* F10 in Scan Set 1 code.  (Set 2 code is 0x09)  */
14867c478bd9Sstevel@tonic-gate 		if (!kb8042_debug) {
14877c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  normal debugging on\n");
14887c478bd9Sstevel@tonic-gate 			kb8042_debug = B_TRUE;
14897c478bd9Sstevel@tonic-gate 		}
14907c478bd9Sstevel@tonic-gate 		break;
14917c478bd9Sstevel@tonic-gate 	case 0x43:	/* F9 in Scan Set 1 code.  (Set 2 code is 0x01) */
14927c478bd9Sstevel@tonic-gate 		if (!kb8042_getchar_debug) {
14937c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  getchar debugging on\n");
14947c478bd9Sstevel@tonic-gate 			kb8042_getchar_debug = B_TRUE;
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 		break;
14977c478bd9Sstevel@tonic-gate 	case 0x42:	/* F8 in Scan Set 1 code.  (Set 2 code is 0x0a) */
14987c478bd9Sstevel@tonic-gate 		if (!kb8042_low_level_debug) {
14997c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  low-level debugging on\n");
15007c478bd9Sstevel@tonic-gate 			kb8042_low_level_debug = B_TRUE;
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 		break;
15037c478bd9Sstevel@tonic-gate 	case 0x41:	/* F7 in Scan Set 1 code.  (Set 2 code is 0x83) */
15047c478bd9Sstevel@tonic-gate 		if (!kb8042_pressrelease_debug) {
15057c478bd9Sstevel@tonic-gate 			prom_printf(
15067c478bd9Sstevel@tonic-gate 			    "\nKeyboard:  press/release debugging on\n");
15077c478bd9Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_TRUE;
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 		break;
15107c478bd9Sstevel@tonic-gate 	case 0x3b:	/* F1 in Scan Set 1 code.  (Set 2 code is 0x05) */
15117c478bd9Sstevel@tonic-gate 		if (kb8042_debug ||
15127c478bd9Sstevel@tonic-gate 		    kb8042_getchar_debug ||
15137c478bd9Sstevel@tonic-gate 		    kb8042_low_level_debug ||
15147c478bd9Sstevel@tonic-gate 		    kb8042_pressrelease_debug) {
15157c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  all debugging off\n");
15167c478bd9Sstevel@tonic-gate 			kb8042_debug = B_FALSE;
15177c478bd9Sstevel@tonic-gate 			kb8042_getchar_debug = B_FALSE;
15187c478bd9Sstevel@tonic-gate 			kb8042_low_level_debug = B_FALSE;
15197c478bd9Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_FALSE;
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 		break;
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate #endif
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate static boolean_t
15277c478bd9Sstevel@tonic-gate kb8042_autorepeat_detect(
15287c478bd9Sstevel@tonic-gate     struct kb8042 *kb8042,
15297c478bd9Sstevel@tonic-gate     int key_pos,
15307c478bd9Sstevel@tonic-gate     enum keystate state)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	if (state == KEY_RELEASED) {
15337c478bd9Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos)
15347c478bd9Sstevel@tonic-gate 			kb8042->kb_old_key_pos = 0;
15357c478bd9Sstevel@tonic-gate 	} else {
15367c478bd9Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos) {
15377c478bd9Sstevel@tonic-gate 			return (B_TRUE);
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 		kb8042->kb_old_key_pos = key_pos;
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	return (B_FALSE);
15427c478bd9Sstevel@tonic-gate }
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate /* ARGSUSED */
15457c478bd9Sstevel@tonic-gate static void
15467c478bd9Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
15477c478bd9Sstevel@tonic-gate {
15487c478bd9Sstevel@tonic-gate 	switch (cmd) {
15497c478bd9Sstevel@tonic-gate 	case KBD_CMD_BELL:
1550c35aa225Smarx 		(void) beeper_on(BEEP_TYPE4);
15517c478bd9Sstevel@tonic-gate 		break;
15527c478bd9Sstevel@tonic-gate 	case KBD_CMD_NOBELL:
1553c35aa225Smarx 		(void) beeper_off();
15547c478bd9Sstevel@tonic-gate 		break;
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate /*
15607c478bd9Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
15617c478bd9Sstevel@tonic-gate  */
15627c478bd9Sstevel@tonic-gate static int
1563281f0747Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	return (kbtrans_getchar(kb8042->hw_kbtrans));
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
15747c478bd9Sstevel@tonic-gate  */
15757c478bd9Sstevel@tonic-gate static int
1576281f0747Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	return (kbtrans_ischar(kb8042->hw_kbtrans));
15837c478bd9Sstevel@tonic-gate }
1584