xref: /illumos-gate/usr/src/uts/common/io/kb8042/kb8042.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 /*
26c35aa225Smarx  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
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_start_state_machine(struct kb8042 *, boolean_t);
1487c478bd9Sstevel@tonic-gate static enum state_return kb8042_state_machine(struct kb8042 *, int, boolean_t);
1497c478bd9Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
1507c478bd9Sstevel@tonic-gate static int kb8042_xlate_leds(int);
151fd9cb95cSsethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
1527c478bd9Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
1537c478bd9Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
1547c478bd9Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck(
1557c478bd9Sstevel@tonic-gate 			struct kbtrans_hardware *hw, int *key,
1567c478bd9Sstevel@tonic-gate 			enum keystate *state);
1577c478bd9Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
1587c478bd9Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
1597c478bd9Sstevel@tonic-gate 			enum keystate state);
1607c478bd9Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
1617c478bd9Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
1627c478bd9Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *);
1637c478bd9Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
164281f0747Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
165281f0747Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
166fd9cb95cSsethg static void kb8042_cleanup(struct kb8042 *kb8042);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = {
1697c478bd9Sstevel@tonic-gate 	kb8042_streams_setled,
1707c478bd9Sstevel@tonic-gate 	kb8042_polled_setled,
1717c478bd9Sstevel@tonic-gate 	kb8042_polled_keycheck,
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate extern struct keyboard keyindex_pc;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	DRIVER_NAME(dip) ddi_driver_name(dip)
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static	char	module_name[] = "kb8042";
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
1817c478bd9Sstevel@tonic-gate 			cred_t *credp);
1827c478bd9Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
1837c478bd9Sstevel@tonic-gate static int kb8042_wsrv();
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate struct module_info kb8042_sinfo = {
1867c478bd9Sstevel@tonic-gate 	42,		/* Module ID */
1877c478bd9Sstevel@tonic-gate 	module_name,
1887c478bd9Sstevel@tonic-gate 	0, 32,		/* Minimum and maximum packet sizes */
1897c478bd9Sstevel@tonic-gate 	256, 128	/* High and low water marks */
1907c478bd9Sstevel@tonic-gate };
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static struct qinit kb8042_rinit = {
1937c478bd9Sstevel@tonic-gate 	NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static struct qinit kb8042_winit = {
1977c478bd9Sstevel@tonic-gate 	putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate struct streamtab
2017c478bd9Sstevel@tonic-gate 	kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate struct kb8042	Kdws = {0};
204fd9cb95cSsethg static dev_info_t *kb8042_dip = NULL;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2077c478bd9Sstevel@tonic-gate 		void **result);
2087c478bd9Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
209fd9cb95cSsethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_kb8042_ops = {
2127c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_open */
2137c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_close */
2147c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2157c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
2167c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
2177c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
2187c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
2197c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
2207c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2217c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2227c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2237c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2247c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2257c478bd9Sstevel@tonic-gate 	&kb8042_str_info,	/* cb_stream */
2267c478bd9Sstevel@tonic-gate 	D_MP
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate struct dev_ops kb8042_ops = {
2307c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2317c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
2327c478bd9Sstevel@tonic-gate 	kb8042_getinfo,		/* devo_getinfo */
2337c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2347c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2357c478bd9Sstevel@tonic-gate 	kb8042_attach,		/* devo_attach */
236fd9cb95cSsethg 	kb8042_detach,		/* devo_detach */
2377c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
2387c478bd9Sstevel@tonic-gate 	&cb_kb8042_ops,		/* devo_cb_ops */
2397c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL	/* devo_bus_ops */
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 */
2537c478bd9Sstevel@tonic-gate 	"PS/2 Keyboard %I%, %E%",
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 	/*
375fd9cb95cSsethg 	 * Send a 0.  The keyboard should ACK the 0, then it send the scan code
376fd9cb95cSsethg 	 * 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);
434*2df1fe9cSrandyf 		mutex_enter(&kb8042->w_hw_mutex);
435*2df1fe9cSrandyf 		kb8042->suspended = B_FALSE;
436*2df1fe9cSrandyf 		if (kb8042->w_qp != NULL) {
437*2df1fe9cSrandyf 			enableok(WR(kb8042->w_qp));
438*2df1fe9cSrandyf 			qenable(WR(kb8042->w_qp));
439*2df1fe9cSrandyf 		}
440*2df1fe9cSrandyf 		cv_broadcast(&kb8042->suspend_cv);
441*2df1fe9cSrandyf 		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);
491*2df1fe9cSrandyf 	cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
492*2df1fe9cSrandyf 	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
5467c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?%s #%d: version %s\n",
5477c478bd9Sstevel@tonic-gate 	    DRIVER_NAME(devi), ddi_get_instance(devi), "%I% (%E%)");
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:
564*2df1fe9cSrandyf 		mutex_enter(&kb8042->w_hw_mutex);
565*2df1fe9cSrandyf 		ASSERT(kb8042->ops >= 0);
566*2df1fe9cSrandyf 		while (kb8042->ops > 0)
567*2df1fe9cSrandyf 			cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
568*2df1fe9cSrandyf 		kb8042->suspended = B_TRUE;
569*2df1fe9cSrandyf 		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 
588fd9cb95cSsethg 
5897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5907c478bd9Sstevel@tonic-gate static int
5917c478bd9Sstevel@tonic-gate kb8042_getinfo(
5927c478bd9Sstevel@tonic-gate     dev_info_t *dip,
5937c478bd9Sstevel@tonic-gate     ddi_info_cmd_t infocmd,
5947c478bd9Sstevel@tonic-gate     void *arg,
5957c478bd9Sstevel@tonic-gate     void **result)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	register int error;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6007c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6017c478bd9Sstevel@tonic-gate 		if (kb8042_dip == NULL) {
6027c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
6037c478bd9Sstevel@tonic-gate 		} else {
6047c478bd9Sstevel@tonic-gate 			*result = (void *) kb8042_dip;
6057c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		break;
6087c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6097c478bd9Sstevel@tonic-gate 		*result = (void *)0;
6107c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
6117c478bd9Sstevel@tonic-gate 		break;
6127c478bd9Sstevel@tonic-gate 	default:
6137c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	return (error);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate static void
620fd9cb95cSsethg kb8042_cleanup(struct kb8042 *kb8042)
621fd9cb95cSsethg {
6220eccbe1bSsethg 	ASSERT(kb8042_dip != NULL);
623fd9cb95cSsethg 
624*2df1fe9cSrandyf 	if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
625*2df1fe9cSrandyf 		cv_destroy(&kb8042->suspend_cv);
626*2df1fe9cSrandyf 		cv_destroy(&kb8042->ops_cv);
627fd9cb95cSsethg 		mutex_destroy(&kb8042->w_hw_mutex);
628*2df1fe9cSrandyf 	}
629fd9cb95cSsethg 
630fd9cb95cSsethg 	if (kb8042->init_state & KB8042_INTR_ADDED)
631fd9cb95cSsethg 		ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
632fd9cb95cSsethg 
633fd9cb95cSsethg 	if (kb8042->init_state & KB8042_REGS_MAPPED)
634fd9cb95cSsethg 		ddi_regs_map_free(&kb8042->handle);
635fd9cb95cSsethg 
636fd9cb95cSsethg 	if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
637fd9cb95cSsethg 		ddi_remove_minor_node(kb8042_dip, NULL);
638fd9cb95cSsethg 
639fd9cb95cSsethg 	kb8042->init_state = KB8042_UNINITIALIZED;
6400eccbe1bSsethg 	kb8042_dip = NULL;
641fd9cb95cSsethg }
642fd9cb95cSsethg 
643fd9cb95cSsethg static void
644fd9cb95cSsethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	if (kb8042->w_init)
6477c478bd9Sstevel@tonic-gate 		return;
6487c478bd9Sstevel@tonic-gate 
649fd9cb95cSsethg 	if (!from_resume) {
6507c478bd9Sstevel@tonic-gate 		kb8042->w_kblayout = 0;	/* Default to US */
6517c478bd9Sstevel@tonic-gate 		kb8042->w_qp = (queue_t *)NULL;
652fd9cb95cSsethg 		kb8042->simulated_kbd_type = KB_PC;
653fd9cb95cSsethg 		kb8042->leds.commanded = -1;	/* Unknown initial state */
654fd9cb95cSsethg 		kb8042->leds.desired = -1;	/* Unknown initial state */
655fd9cb95cSsethg 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	kb8042_wait_poweron(kb8042);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	kb8042->kb_old_key_pos = 0;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* Set up the command state machine and start it running. */
6627c478bd9Sstevel@tonic-gate 	kb8042->command_state = KB_COMMAND_STATE_WAIT;
6637c478bd9Sstevel@tonic-gate 	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	kb8042->w_init++;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
6717c478bd9Sstevel@tonic-gate static int
6727c478bd9Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
6757c478bd9Sstevel@tonic-gate 	int err;
6767c478bd9Sstevel@tonic-gate 	int initial_leds;
6777c478bd9Sstevel@tonic-gate 	int initial_led_mask;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	kb8042 = &Kdws;
6807c478bd9Sstevel@tonic-gate 
681*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
682*2df1fe9cSrandyf 	while (kb8042->suspended) {
683*2df1fe9cSrandyf 		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
684*2df1fe9cSrandyf 		    0) {
685*2df1fe9cSrandyf 			mutex_exit(&kb8042->w_hw_mutex);
686*2df1fe9cSrandyf 			return (EINTR);
687*2df1fe9cSrandyf 		}
688*2df1fe9cSrandyf 	}
689*2df1fe9cSrandyf 
6907c478bd9Sstevel@tonic-gate 	kb8042->w_dev = *devp;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (qp->q_ptr) {
693*2df1fe9cSrandyf 		mutex_exit(&kb8042->w_hw_mutex);
6947c478bd9Sstevel@tonic-gate 		return (0);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	qp->q_ptr = (caddr_t)kb8042;
6977c478bd9Sstevel@tonic-gate 	WR(qp)->q_ptr = qp->q_ptr;
6987c478bd9Sstevel@tonic-gate 	if (!kb8042->w_qp)
6997c478bd9Sstevel@tonic-gate 		kb8042->w_qp = qp;
7007c478bd9Sstevel@tonic-gate 
701*2df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
702*2df1fe9cSrandyf 	kb8042->ops++;
703*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
704*2df1fe9cSrandyf 
7057c478bd9Sstevel@tonic-gate 	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
7067c478bd9Sstevel@tonic-gate 	err = kbtrans_streams_init(qp, sflag, credp,
7077c478bd9Sstevel@tonic-gate 	    (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
7087c478bd9Sstevel@tonic-gate 	    &kb8042->hw_kbtrans,
7097c478bd9Sstevel@tonic-gate 	    initial_leds, initial_led_mask);
7107c478bd9Sstevel@tonic-gate 	if (err != 0)
7117c478bd9Sstevel@tonic-gate 		return (err);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
7167c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_argument =
717281f0747Slt200341 	    (cons_polledio_arg_t)kb8042;
7187c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_putchar = NULL;
7197c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_getchar =
720281f0747Slt200341 	    (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
7217c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_ischar =
722281f0747Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
7237c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_enter = NULL;
7247c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_exit = NULL;
7257c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_setled =
726281f0747Slt200341 	    (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
7277c478bd9Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_keycheck =
728281f0747Slt200341 	    (boolean_t (*)(cons_polledio_arg_t, int *,
7297c478bd9Sstevel@tonic-gate 	    enum keystate *))kb8042_polled_keycheck;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	qprocson(qp);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	kbtrans_streams_enable(kb8042->hw_kbtrans);
7347c478bd9Sstevel@tonic-gate 
735*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
736*2df1fe9cSrandyf 	ASSERT(kb8042->ops > 0);
737*2df1fe9cSrandyf 	kb8042->ops--;
738*2df1fe9cSrandyf 	if (kb8042->ops == 0)
739*2df1fe9cSrandyf 		cv_broadcast(&kb8042->ops_cv);
740*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
741*2df1fe9cSrandyf 
7427c478bd9Sstevel@tonic-gate 	return (0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
7467c478bd9Sstevel@tonic-gate static int
7477c478bd9Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
7507c478bd9Sstevel@tonic-gate 
751c35aa225Smarx 	/* If a beep is in progress, stop that */
752c35aa225Smarx 	(void) beeper_off();
753c35aa225Smarx 
7547c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7557c478bd9Sstevel@tonic-gate 
756*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
757*2df1fe9cSrandyf 	while (kb8042->suspended) {
758*2df1fe9cSrandyf 		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
759*2df1fe9cSrandyf 		    0) {
760*2df1fe9cSrandyf 			mutex_exit(&kb8042->w_hw_mutex);
761*2df1fe9cSrandyf 			return (EINTR);
762*2df1fe9cSrandyf 		}
763*2df1fe9cSrandyf 	}
764*2df1fe9cSrandyf 
765*2df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
766*2df1fe9cSrandyf 	kb8042->ops++;
767*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
768*2df1fe9cSrandyf 
769e1fec4f9Sry162471 	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);
770e1fec4f9Sry162471 
7717c478bd9Sstevel@tonic-gate 	kb8042->w_qp = (queue_t *)NULL;
7727c478bd9Sstevel@tonic-gate 	qprocsoff(qp);
7737c478bd9Sstevel@tonic-gate 
774*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
775*2df1fe9cSrandyf 	ASSERT(kb8042->ops > 0);
776*2df1fe9cSrandyf 	kb8042->ops--;
777*2df1fe9cSrandyf 	if (kb8042->ops == 0)
778*2df1fe9cSrandyf 		cv_broadcast(&kb8042->ops_cv);
779*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
780*2df1fe9cSrandyf 
7817c478bd9Sstevel@tonic-gate 	return (0);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate static int
7857c478bd9Sstevel@tonic-gate kb8042_wsrv(queue_t *qp)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
790*2df1fe9cSrandyf 	boolean_t suspended;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7937c478bd9Sstevel@tonic-gate 
794*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
795*2df1fe9cSrandyf 	suspended = kb8042->suspended;
796*2df1fe9cSrandyf 	ASSERT(kb8042->ops >= 0);
797*2df1fe9cSrandyf 	if (!suspended)
798*2df1fe9cSrandyf 		kb8042->ops++;
799*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
800*2df1fe9cSrandyf 
801*2df1fe9cSrandyf #ifdef NO_KB_DEBUG
802*2df1fe9cSrandyf 	while (!suspended && (mp = getq(qp)) != NULL) {
803*2df1fe9cSrandyf #else
804*2df1fe9cSrandyf 	/*
805*2df1fe9cSrandyf 	 * Not taking keyboard input while suspending can make debugging
806*2df1fe9cSrandyf 	 * difficult.  However, we still do the ops counting so that we
807*2df1fe9cSrandyf 	 * don't suspend at a bad time.
808*2df1fe9cSrandyf 	 */
8097c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp))) {
810*2df1fe9cSrandyf #endif
8117c478bd9Sstevel@tonic-gate 		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
8127c478bd9Sstevel@tonic-gate 		case KBTRANS_MESSAGE_HANDLED:
8137c478bd9Sstevel@tonic-gate 			continue;
8147c478bd9Sstevel@tonic-gate 		case KBTRANS_MESSAGE_NOT_HANDLED:
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
8187c478bd9Sstevel@tonic-gate 		case M_IOCTL:
8197c478bd9Sstevel@tonic-gate 			kb8042_ioctlmsg(kb8042, qp, mp);
8207c478bd9Sstevel@tonic-gate 			continue;
8217c478bd9Sstevel@tonic-gate 		case M_IOCDATA:
8227c478bd9Sstevel@tonic-gate 			kb8042_iocdatamsg(qp, mp);
8237c478bd9Sstevel@tonic-gate 			continue;
8247c478bd9Sstevel@tonic-gate 		case M_DELAY:
8257c478bd9Sstevel@tonic-gate 		case M_STARTI:
8267c478bd9Sstevel@tonic-gate 		case M_STOPI:
8277c478bd9Sstevel@tonic-gate 		case M_READ:	/* ignore, no buffered data */
8287c478bd9Sstevel@tonic-gate 			freemsg(mp);
8297c478bd9Sstevel@tonic-gate 			continue;
8307c478bd9Sstevel@tonic-gate 		case M_FLUSH:
8317c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
8327c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR)
8337c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
8347c478bd9Sstevel@tonic-gate 			else
8357c478bd9Sstevel@tonic-gate 				freemsg(mp);
8367c478bd9Sstevel@tonic-gate 			continue;
8377c478bd9Sstevel@tonic-gate 		default:
8387c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
8397c478bd9Sstevel@tonic-gate 			    mp->b_datap->db_type);
8407c478bd9Sstevel@tonic-gate 			freemsg(mp);
8417c478bd9Sstevel@tonic-gate 			continue;
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 	}
844*2df1fe9cSrandyf 
845*2df1fe9cSrandyf 	mutex_enter(&kb8042->w_hw_mutex);
846*2df1fe9cSrandyf 	if (!suspended) {
847*2df1fe9cSrandyf 		ASSERT(kb8042->ops > 0);
848*2df1fe9cSrandyf 		kb8042->ops--;
849*2df1fe9cSrandyf 		if (kb8042->ops == 0)
850*2df1fe9cSrandyf 			cv_broadcast(&kb8042->ops_cv);
851*2df1fe9cSrandyf 	}
852*2df1fe9cSrandyf 	mutex_exit(&kb8042->w_hw_mutex);
853*2df1fe9cSrandyf 
8547c478bd9Sstevel@tonic-gate 	return (0);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate static void
8587c478bd9Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
8617c478bd9Sstevel@tonic-gate 	mblk_t		*datap;
8627c478bd9Sstevel@tonic-gate 	int		error;
8637c478bd9Sstevel@tonic-gate 	int		tmp;
864c35aa225Smarx 	int		cycles;
865c35aa225Smarx 	int		frequency;
866c35aa225Smarx 	int		msecs;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
8737c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
8747c478bd9Sstevel@tonic-gate 		if (error != 0) {
8757c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
8767c478bd9Sstevel@tonic-gate 			return;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		/*
8807c478bd9Sstevel@tonic-gate 		 * We are given an appropriate-sized data block,
8817c478bd9Sstevel@tonic-gate 		 * and return a pointer to our structure in it.
8827c478bd9Sstevel@tonic-gate 		 */
8837c478bd9Sstevel@tonic-gate 		*(struct cons_polledio **)mp->b_cont->b_rptr =
8847c478bd9Sstevel@tonic-gate 		    &kb8042->polledio;
8857c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
8867c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
8877c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
8887c478bd9Sstevel@tonic-gate 		break;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
8917c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8927c478bd9Sstevel@tonic-gate 		break;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8957c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8967c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8977c478bd9Sstevel@tonic-gate 			return;
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
9017c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9027c478bd9Sstevel@tonic-gate 		break;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * Valid only in TR_UNTRANS_MODE mode.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	case CONSSETKBDTYPE:
9087c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9097c478bd9Sstevel@tonic-gate 		if (error != 0) {
9107c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
9117c478bd9Sstevel@tonic-gate 			return;
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 		tmp =  *(int *)mp->b_cont->b_rptr;
9147c478bd9Sstevel@tonic-gate 		if (tmp != KB_PC && tmp != KB_USB) {
9157c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9167c478bd9Sstevel@tonic-gate 			break;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 		kb8042->simulated_kbd_type = tmp;
9197c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9207c478bd9Sstevel@tonic-gate 		break;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	case KIOCLAYOUT:
9237c478bd9Sstevel@tonic-gate 		if (kb8042->w_kblayout == -1) {
9247c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9257c478bd9Sstevel@tonic-gate 			return;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9297c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, ENOMEM);
9307c478bd9Sstevel@tonic-gate 			return;
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB)
9347c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9357c478bd9Sstevel@tonic-gate 		else
9367c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = kb8042->w_kblayout;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9397c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
9407c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9417c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9427c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
9437c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
9447c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
9457c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	case KIOCSLAYOUT:
9497c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9507c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
9517c478bd9Sstevel@tonic-gate 			return;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
9557c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9567c478bd9Sstevel@tonic-gate 		break;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	case KIOCCMD:
9597c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9607c478bd9Sstevel@tonic-gate 		if (error != 0) {
9617c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
9627c478bd9Sstevel@tonic-gate 			return;
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
9667c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
9677c478bd9Sstevel@tonic-gate 		break;
9687c478bd9Sstevel@tonic-gate 
969c35aa225Smarx 	case KIOCMKTONE:
970c35aa225Smarx 		if (iocp->ioc_count != TRANSPARENT) {
971c35aa225Smarx 			miocnak(qp, mp, 0, EINVAL);
972c35aa225Smarx 			return;
973c35aa225Smarx 		}
974c35aa225Smarx 
975c35aa225Smarx 		tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
976c35aa225Smarx 		cycles = tmp & 0xffff;
977c35aa225Smarx 		msecs = (tmp >> 16) & 0xffff;
978c35aa225Smarx 
979c35aa225Smarx 		if (cycles == 0)
980c35aa225Smarx 			frequency = UINT16_MAX;
981c35aa225Smarx 		else if (cycles == UINT16_MAX)
982c35aa225Smarx 			frequency = 0;
983c35aa225Smarx 		else {
984c35aa225Smarx 			frequency = (PIT_HZ + cycles / 2) / cycles;
985c35aa225Smarx 			if (frequency > UINT16_MAX)
986c35aa225Smarx 				frequency = UINT16_MAX;
987c35aa225Smarx 		}
988c35aa225Smarx 
989c35aa225Smarx 		error = beep_mktone(frequency, msecs);
990c35aa225Smarx 		if (error != 0)
991c35aa225Smarx 			miocnak(qp, mp, 0, error);
992c35aa225Smarx 		else
993c35aa225Smarx 			miocack(qp, mp, 0, 0);
994c35aa225Smarx 		break;
995c35aa225Smarx 
9967c478bd9Sstevel@tonic-gate 	default:
9977c478bd9Sstevel@tonic-gate #ifdef DEBUG1
9987c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
9997c478bd9Sstevel@tonic-gate #endif
10007c478bd9Sstevel@tonic-gate 		miocnak(qp, mp, 0, EINVAL);
10017c478bd9Sstevel@tonic-gate 		return;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * Process a byte received from the keyboard
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate static void
10097c478bd9Sstevel@tonic-gate kb8042_received_byte(
10107c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042,
10117c478bd9Sstevel@tonic-gate 	int		scancode)	/* raw scan code */
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	boolean_t	legit;		/* is this a legit key pos'n? */
10147c478bd9Sstevel@tonic-gate 	int		key_pos = -1;
10157c478bd9Sstevel@tonic-gate 	enum keystate	state;
10167c478bd9Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10197c478bd9Sstevel@tonic-gate 	kb8042_debug_hotkey(scancode);
10207c478bd9Sstevel@tonic-gate #endif
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if (!kb8042->w_init)	/* can't do anything anyway */
10237c478bd9Sstevel@tonic-gate 		return;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
10267c478bd9Sstevel@tonic-gate 	    &synthetic_release_needed);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (legit == 0) {
10297c478bd9Sstevel@tonic-gate 		/* Eaten by translation */
10307c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10317c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10327c478bd9Sstevel@tonic-gate 			prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode);
10337c478bd9Sstevel@tonic-gate #endif
10347c478bd9Sstevel@tonic-gate 		return;
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10387c478bd9Sstevel@tonic-gate 	if (kb8042_debug) {
10397c478bd9Sstevel@tonic-gate 		prom_printf("kb8042_intr:  0x%x -> %s %d",
10407c478bd9Sstevel@tonic-gate 		    scancode,
10417c478bd9Sstevel@tonic-gate 		    state == KEY_RELEASED ? "released" : "pressed",
10427c478bd9Sstevel@tonic-gate 		    key_pos);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate #endif
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * Don't know if we want this permanently, but it seems interesting
10487c478bd9Sstevel@tonic-gate 	 * for the moment.
10497c478bd9Sstevel@tonic-gate 	 */
10507c478bd9Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod1) {
10517c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10527c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10537c478bd9Sstevel@tonic-gate 			prom_printf(" -> debug mod1");
10547c478bd9Sstevel@tonic-gate #endif
10557c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod2) {
10587c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10597c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10607c478bd9Sstevel@tonic-gate 			prom_printf(" -> debug mod2");
10617c478bd9Sstevel@tonic-gate #endif
10627c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	if (kb8042->debugger.enabled &&
10657c478bd9Sstevel@tonic-gate 	    key_pos == kb8042->debugger.trigger &&
10667c478bd9Sstevel@tonic-gate 	    kb8042->debugger.mod1_down &&
10677c478bd9Sstevel@tonic-gate 	    kb8042->debugger.mod2_down) {
10687c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10697c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10707c478bd9Sstevel@tonic-gate 			prom_printf(" -> debugger\n");
10717c478bd9Sstevel@tonic-gate #endif
10727c478bd9Sstevel@tonic-gate 		/*
10737c478bd9Sstevel@tonic-gate 		 * Require new presses of the modifiers.
10747c478bd9Sstevel@tonic-gate 		 */
10757c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod1_down = B_FALSE;
10767c478bd9Sstevel@tonic-gate 		kb8042->debugger.mod2_down = B_FALSE;
10777c478bd9Sstevel@tonic-gate 		abort_sequence_enter(NULL);
10787c478bd9Sstevel@tonic-gate 		return;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 * If there's no queue above us - as can happen if we've been
10837c478bd9Sstevel@tonic-gate 	 * attached but not opened - drop the keystroke.
10847c478bd9Sstevel@tonic-gate 	 * Note that we do this here instead of above so that
10857c478bd9Sstevel@tonic-gate 	 * Ctrl-Alt-D still works.
10867c478bd9Sstevel@tonic-gate 	 */
10877c478bd9Sstevel@tonic-gate 	if (kb8042->w_qp == NULL) {
10887c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
10897c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
10907c478bd9Sstevel@tonic-gate 			prom_printf(" -> nobody home\n");
10917c478bd9Sstevel@tonic-gate #endif
10927c478bd9Sstevel@tonic-gate 		return;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/*
10967c478bd9Sstevel@tonic-gate 	 * This is to filter out auto repeat since it can't be
10977c478bd9Sstevel@tonic-gate 	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
10987c478bd9Sstevel@tonic-gate 	 * can.  Don't know whether they've taken over the world.
10997c478bd9Sstevel@tonic-gate 	 * Don't think so.)
11007c478bd9Sstevel@tonic-gate 	 */
11017c478bd9Sstevel@tonic-gate 	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
11027c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
11037c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
11047c478bd9Sstevel@tonic-gate 			prom_printf(" -> autorepeat ignored\n");
11057c478bd9Sstevel@tonic-gate #endif
11067c478bd9Sstevel@tonic-gate 		return;
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
11107c478bd9Sstevel@tonic-gate 	if (kb8042_debug)
11117c478bd9Sstevel@tonic-gate 		prom_printf(" -> OK\n");
11127c478bd9Sstevel@tonic-gate #endif
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11157c478bd9Sstevel@tonic-gate 	if (kb8042_pressrelease_debug) {
11167c478bd9Sstevel@tonic-gate 		prom_printf(" %s%d ",
11177c478bd9Sstevel@tonic-gate 		    state == KEY_PRESSED ? "+" : "-",
11187c478bd9Sstevel@tonic-gate 		    key_pos);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate #endif
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * This is a total hack.  For some stupid reason, the two additional
11267c478bd9Sstevel@tonic-gate 	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
11277c478bd9Sstevel@tonic-gate 	 * only.  We synthesize a release immediately.
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	if (synthetic_release_needed) {
11307c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11317c478bd9Sstevel@tonic-gate 		if (kb8042_debug)
11327c478bd9Sstevel@tonic-gate 			prom_printf("synthetic release %d\n", key_pos);
11337c478bd9Sstevel@tonic-gate 		if (kb8042_pressrelease_debug)
11347c478bd9Sstevel@tonic-gate 			prom_printf(" -%d(s) ", key_pos);
11357c478bd9Sstevel@tonic-gate #endif
11367c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
11377c478bd9Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate static void
11437c478bd9Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
11447c478bd9Sstevel@tonic-gate     enum keystate state)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	kbtrans_key_t key;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	ASSERT(key_pos >= 0 && key_pos <= 255);
11497c478bd9Sstevel@tonic-gate 	if (kb8042->simulated_kbd_type == KB_PC) {
11507c478bd9Sstevel@tonic-gate 		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
11517c478bd9Sstevel@tonic-gate 	} else if (kb8042->simulated_kbd_type == KB_USB) {
11527c478bd9Sstevel@tonic-gate 		key = keytab_pc2usb[key_pos];
11537c478bd9Sstevel@tonic-gate 		if (key != 0) {
11547c478bd9Sstevel@tonic-gate 			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * Called from interrupt handler when keyboard interrupt occurs.
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate static uint_t
11637c478bd9Sstevel@tonic-gate kb8042_intr(caddr_t arg)
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate 	uchar_t scancode;	/* raw scan code */
11667c478bd9Sstevel@tonic-gate 	int rc;
11677c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)arg;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	rc = DDI_INTR_UNCLAIMED;
11707c478bd9Sstevel@tonic-gate 
1171fd9cb95cSsethg 	if (kb8042->init_state == KB8042_UNINITIALIZED)
1172fd9cb95cSsethg 		return (DDI_INTR_UNCLAIMED);
1173fd9cb95cSsethg 
11747c478bd9Sstevel@tonic-gate 	/* don't care if drv_setparm succeeds */
11757c478bd9Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
11787c478bd9Sstevel@tonic-gate 	    != 0) {
11797c478bd9Sstevel@tonic-gate 		rc = DDI_INTR_CLAIMED;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
11827c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_DATA);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
11857c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
11867c478bd9Sstevel@tonic-gate 			prom_printf(" <K:%x ", scancode);
11877c478bd9Sstevel@tonic-gate #endif
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		mutex_enter(&kb8042->w_hw_mutex);
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
11927c478bd9Sstevel@tonic-gate 		    STATE_NORMAL) {
11937c478bd9Sstevel@tonic-gate 			mutex_exit(&kb8042->w_hw_mutex);
11947c478bd9Sstevel@tonic-gate 			continue;
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		mutex_exit(&kb8042->w_hw_mutex);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		kb8042_received_byte(kb8042, scancode);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	return (rc);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate static void
12077c478bd9Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	struct copyresp	*csp;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
12127c478bd9Sstevel@tonic-gate 	if (csp->cp_rval) {
12137c478bd9Sstevel@tonic-gate 		freemsg(mp);
12147c478bd9Sstevel@tonic-gate 		return;
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	switch (csp->cp_cmd) {
12187c478bd9Sstevel@tonic-gate 	default:
12197c478bd9Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
12207c478bd9Sstevel@tonic-gate 		break;
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate static boolean_t
12257c478bd9Sstevel@tonic-gate kb8042_polled_keycheck(
12267c478bd9Sstevel@tonic-gate     struct kbtrans_hardware *hw,
12277c478bd9Sstevel@tonic-gate     int *key,
12287c478bd9Sstevel@tonic-gate     enum keystate *state)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
12317c478bd9Sstevel@tonic-gate 	int	scancode;
12327c478bd9Sstevel@tonic-gate 	boolean_t	legit;
12337c478bd9Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (kb8042->polled_synthetic_release_pending) {
12367c478bd9Sstevel@tonic-gate 		*key = kb8042->polled_synthetic_release_key;
12377c478bd9Sstevel@tonic-gate 		*state = KEY_RELEASED;
12387c478bd9Sstevel@tonic-gate 		kb8042->polled_synthetic_release_pending = B_FALSE;
12397c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
12407c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug)
12417c478bd9Sstevel@tonic-gate 			prom_printf("synthetic release 0x%x\n", *key);
12427c478bd9Sstevel@tonic-gate #endif
12437c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
12447c478bd9Sstevel@tonic-gate 		return (B_TRUE);
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	for (;;) {
12487c478bd9Sstevel@tonic-gate 		if (ddi_get8(kb8042->handle,
12497c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
12507c478bd9Sstevel@tonic-gate 			return (B_FALSE);
12517c478bd9Sstevel@tonic-gate 		}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
12547c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_INPUT_DATA);
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
12577c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
12587c478bd9Sstevel@tonic-gate 			prom_printf(" g<%x ", scancode);
12597c478bd9Sstevel@tonic-gate #endif
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
12627c478bd9Sstevel@tonic-gate 		    STATE_NORMAL) {
12637c478bd9Sstevel@tonic-gate 			continue;
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12677c478bd9Sstevel@tonic-gate 		kb8042_debug_hotkey(scancode);
12687c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug)
12697c478bd9Sstevel@tonic-gate 			prom_printf("polled 0x%x", scancode);
12707c478bd9Sstevel@tonic-gate #endif
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 		legit = KeyboardConvertScan(kb8042, scancode, key, state,
12737c478bd9Sstevel@tonic-gate 		    &synthetic_release_needed);
12747c478bd9Sstevel@tonic-gate 		if (!legit) {
12757c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12767c478bd9Sstevel@tonic-gate 			if (kb8042_getchar_debug)
12777c478bd9Sstevel@tonic-gate 				prom_printf(" -> ignored\n");
12787c478bd9Sstevel@tonic-gate #endif
12797c478bd9Sstevel@tonic-gate 			continue;
12807c478bd9Sstevel@tonic-gate 		}
12817c478bd9Sstevel@tonic-gate #ifdef	KD_DEBUG
12827c478bd9Sstevel@tonic-gate 		if (kb8042_getchar_debug) {
12837c478bd9Sstevel@tonic-gate 			prom_printf(" -> %s %d\n",
12847c478bd9Sstevel@tonic-gate 			    *state == KEY_PRESSED ? "pressed" : "released",
12857c478bd9Sstevel@tonic-gate 			    *key);
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate #endif
12887c478bd9Sstevel@tonic-gate 		/*
12897c478bd9Sstevel@tonic-gate 		 * For the moment at least, we rely on hardware autorepeat
12907c478bd9Sstevel@tonic-gate 		 * for polled I/O autorepeat.  However, for coordination
12917c478bd9Sstevel@tonic-gate 		 * with the interrupt-driven code, maintain the last key
12927c478bd9Sstevel@tonic-gate 		 * pressed.
12937c478bd9Sstevel@tonic-gate 		 */
12947c478bd9Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		/*
12977c478bd9Sstevel@tonic-gate 		 * This is a total hack to support two additional keys
12987c478bd9Sstevel@tonic-gate 		 * on Korean keyboards.  They report only on press, and
12997c478bd9Sstevel@tonic-gate 		 * so we synthesize a release.  Most likely this will
13007c478bd9Sstevel@tonic-gate 		 * never be important to polled  I/O, but if I do it
13017c478bd9Sstevel@tonic-gate 		 * "right" the first time it _won't_ be an issue.
13027c478bd9Sstevel@tonic-gate 		 */
13037c478bd9Sstevel@tonic-gate 		if (synthetic_release_needed) {
13047c478bd9Sstevel@tonic-gate 			kb8042->polled_synthetic_release_pending = B_TRUE;
13057c478bd9Sstevel@tonic-gate 			kb8042->polled_synthetic_release_key = *key;
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB) {
13097c478bd9Sstevel@tonic-gate 			*key = keytab_pc2usb[*key];
13107c478bd9Sstevel@tonic-gate 		}
13117c478bd9Sstevel@tonic-gate 		return (B_TRUE);
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate static void
1316fd9cb95cSsethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1317fd9cb95cSsethg {
1318fd9cb95cSsethg 	kb8042->leds.desired = led_state;
1319fd9cb95cSsethg 
1320fd9cb95cSsethg 	if (!polled)
1321fd9cb95cSsethg 		mutex_enter(&kb8042->w_hw_mutex);
1322fd9cb95cSsethg 
1323fd9cb95cSsethg 	kb8042_start_state_machine(kb8042, polled);
1324fd9cb95cSsethg 
1325fd9cb95cSsethg 	if (!polled)
1326fd9cb95cSsethg 		mutex_exit(&kb8042->w_hw_mutex);
1327fd9cb95cSsethg }
1328fd9cb95cSsethg 
1329fd9cb95cSsethg static void
13307c478bd9Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1333fd9cb95cSsethg 	kb8042_setled(kb8042, led_state, B_TRUE);
1334fd9cb95cSsethg }
13357c478bd9Sstevel@tonic-gate 
1336fd9cb95cSsethg static void
1337fd9cb95cSsethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1338fd9cb95cSsethg {
1339fd9cb95cSsethg 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1340fd9cb95cSsethg 	kb8042_setled(kb8042, led_state, B_FALSE);
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate static void
13447c478bd9Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	if (polled) {
13477c478bd9Sstevel@tonic-gate 		ddi_put8(kb8042->handle,
13487c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
13497c478bd9Sstevel@tonic-gate 	} else {
13507c478bd9Sstevel@tonic-gate 		ddi_put8(kb8042->handle,
13517c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
13557c478bd9Sstevel@tonic-gate 	if (kb8042_low_level_debug)
13567c478bd9Sstevel@tonic-gate 		prom_printf(" >K:%x ", byte);
13577c478bd9Sstevel@tonic-gate #endif
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * Wait until the keyboard is fully up, maybe.
13627c478bd9Sstevel@tonic-gate  * We may be the first person to talk to the keyboard, in which case
13637c478bd9Sstevel@tonic-gate  * it's patiently waiting to say "AA" to us to tell us it's up.
13647c478bd9Sstevel@tonic-gate  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
13657c478bd9Sstevel@tonic-gate  * good bet that we've already spent that long getting to that point,
13667c478bd9Sstevel@tonic-gate  * so we'll only wait long enough for the communications electronics to
13677c478bd9Sstevel@tonic-gate  * run.
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate static void
13707c478bd9Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042)
13717c478bd9Sstevel@tonic-gate {
13727c478bd9Sstevel@tonic-gate 	int cnt;
13737c478bd9Sstevel@tonic-gate 	int ready;
13747c478bd9Sstevel@tonic-gate 	unsigned char byt;
13757c478bd9Sstevel@tonic-gate 
1376fd9cb95cSsethg 	/* wait for up to 250 ms for a response */
13777c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < 250; cnt++) {
13787c478bd9Sstevel@tonic-gate 		ready = ddi_get8(kb8042->handle,
13797c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_AVAIL);
13807c478bd9Sstevel@tonic-gate 		if (ready != 0)
13817c478bd9Sstevel@tonic-gate 			break;
13827c478bd9Sstevel@tonic-gate 		drv_usecwait(1000);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/*
13867c478bd9Sstevel@tonic-gate 	 * If there's something pending, read and discard it.  If not,
13877c478bd9Sstevel@tonic-gate 	 * assume things are OK anyway - maybe somebody else ate it
13887c478bd9Sstevel@tonic-gate 	 * already.  (On a PC, the BIOS almost certainly did.)
13897c478bd9Sstevel@tonic-gate 	 */
13907c478bd9Sstevel@tonic-gate 	if (ready != 0) {
13917c478bd9Sstevel@tonic-gate 		byt = ddi_get8(kb8042->handle,
13927c478bd9Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_INPUT_DATA);
13937c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
13947c478bd9Sstevel@tonic-gate 		if (kb8042_low_level_debug)
13957c478bd9Sstevel@tonic-gate 			prom_printf(" <K:%x ", byt);
13967c478bd9Sstevel@tonic-gate #endif
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate static void
14017c478bd9Sstevel@tonic-gate kb8042_start_state_machine(struct kb8042 *kb8042, boolean_t polled)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	if (kb8042->command_state == KB_COMMAND_STATE_IDLE) {
14047c478bd9Sstevel@tonic-gate 		if (kb8042->leds.desired != kb8042->leds.commanded) {
14057c478bd9Sstevel@tonic-gate 			kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
14067c478bd9Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_LED;
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate enum state_return
14127c478bd9Sstevel@tonic-gate kb8042_state_machine(struct kb8042 *kb8042, int scancode, boolean_t polled)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	switch (kb8042->command_state) {
14157c478bd9Sstevel@tonic-gate 	case KB_COMMAND_STATE_IDLE:
14167c478bd9Sstevel@tonic-gate 		break;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	case KB_COMMAND_STATE_LED:
14197c478bd9Sstevel@tonic-gate 		if (scancode == KB_ACK) {
14207c478bd9Sstevel@tonic-gate 			kb8042_send_to_keyboard(kb8042,
14217c478bd9Sstevel@tonic-gate 			    kb8042_xlate_leds(kb8042->leds.desired),
14227c478bd9Sstevel@tonic-gate 			    polled);
14237c478bd9Sstevel@tonic-gate 			kb8042->leds.commanded = kb8042->leds.desired;
14247c478bd9Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_WAIT;
14257c478bd9Sstevel@tonic-gate 			return (STATE_INTERNAL);
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 		/* Drop normal scan codes through. */
14287c478bd9Sstevel@tonic-gate 		break;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	case KB_COMMAND_STATE_WAIT:
14317c478bd9Sstevel@tonic-gate 		if (scancode == KB_ACK) {
14327c478bd9Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_IDLE;
14337c478bd9Sstevel@tonic-gate 			kb8042_start_state_machine(kb8042, polled);
14347c478bd9Sstevel@tonic-gate 			return (STATE_INTERNAL);
14357c478bd9Sstevel@tonic-gate 		}
14367c478bd9Sstevel@tonic-gate 		/* Drop normal scan codes through. */
14377c478bd9Sstevel@tonic-gate 		break;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 	return (STATE_NORMAL);
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate static int
14437c478bd9Sstevel@tonic-gate kb8042_xlate_leds(int led)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate 	int res;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	res = 0;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (led & LED_NUM_LOCK)
14507c478bd9Sstevel@tonic-gate 		res |= LED_NUM;
14517c478bd9Sstevel@tonic-gate 	if (led & LED_SCROLL_LOCK)
14527c478bd9Sstevel@tonic-gate 		res |= LED_SCR;
14537c478bd9Sstevel@tonic-gate 	if (led & LED_CAPS_LOCK)
14547c478bd9Sstevel@tonic-gate 		res |= LED_CAP;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	return (res);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14607c478bd9Sstevel@tonic-gate static void
14617c478bd9Sstevel@tonic-gate kb8042_get_initial_leds(
14627c478bd9Sstevel@tonic-gate     struct kb8042 *kb8042,
14637c478bd9Sstevel@tonic-gate     int *initial_leds,
14647c478bd9Sstevel@tonic-gate     int *initial_led_mask)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate #if	defined(i86pc)
14677c478bd9Sstevel@tonic-gate 	extern caddr_t	p0_va;
14687c478bd9Sstevel@tonic-gate 	uint8_t		bios_kb_flag;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	bios_kb_flag = p0_va[BIOS_KB_FLAG];
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
14737c478bd9Sstevel@tonic-gate 	*initial_leds = 0;
14747c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_CAPS_STATE)
14757c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_CAPS_LOCK;
14767c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_NUM_STATE)
14777c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_NUM_LOCK;
14787c478bd9Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_SCROLL_STATE)
14797c478bd9Sstevel@tonic-gate 		*initial_leds |= LED_SCROLL_LOCK;
14807c478bd9Sstevel@tonic-gate #else
14817c478bd9Sstevel@tonic-gate 	*initial_leds = 0;
14827c478bd9Sstevel@tonic-gate 	*initial_led_mask = 0;
14837c478bd9Sstevel@tonic-gate #endif
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate #if	defined(KD_DEBUG)
14877c478bd9Sstevel@tonic-gate static void
14887c478bd9Sstevel@tonic-gate kb8042_debug_hotkey(int scancode)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	if (!kb8042_enable_debug_hotkey)
14917c478bd9Sstevel@tonic-gate 		return;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	switch (scancode) {
14947c478bd9Sstevel@tonic-gate 	case 0x44:	/* F10 in Scan Set 1 code.  (Set 2 code is 0x09)  */
14957c478bd9Sstevel@tonic-gate 		if (!kb8042_debug) {
14967c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  normal debugging on\n");
14977c478bd9Sstevel@tonic-gate 			kb8042_debug = B_TRUE;
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 		break;
15007c478bd9Sstevel@tonic-gate 	case 0x43:	/* F9 in Scan Set 1 code.  (Set 2 code is 0x01) */
15017c478bd9Sstevel@tonic-gate 		if (!kb8042_getchar_debug) {
15027c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  getchar debugging on\n");
15037c478bd9Sstevel@tonic-gate 			kb8042_getchar_debug = B_TRUE;
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 		break;
15067c478bd9Sstevel@tonic-gate 	case 0x42:	/* F8 in Scan Set 1 code.  (Set 2 code is 0x0a) */
15077c478bd9Sstevel@tonic-gate 		if (!kb8042_low_level_debug) {
15087c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  low-level debugging on\n");
15097c478bd9Sstevel@tonic-gate 			kb8042_low_level_debug = B_TRUE;
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 		break;
15127c478bd9Sstevel@tonic-gate 	case 0x41:	/* F7 in Scan Set 1 code.  (Set 2 code is 0x83) */
15137c478bd9Sstevel@tonic-gate 		if (!kb8042_pressrelease_debug) {
15147c478bd9Sstevel@tonic-gate 			prom_printf(
15157c478bd9Sstevel@tonic-gate 			    "\nKeyboard:  press/release debugging on\n");
15167c478bd9Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_TRUE;
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 		break;
15197c478bd9Sstevel@tonic-gate 	case 0x3b:	/* F1 in Scan Set 1 code.  (Set 2 code is 0x05) */
15207c478bd9Sstevel@tonic-gate 		if (kb8042_debug ||
15217c478bd9Sstevel@tonic-gate 		    kb8042_getchar_debug ||
15227c478bd9Sstevel@tonic-gate 		    kb8042_low_level_debug ||
15237c478bd9Sstevel@tonic-gate 		    kb8042_pressrelease_debug) {
15247c478bd9Sstevel@tonic-gate 			prom_printf("\nKeyboard:  all debugging off\n");
15257c478bd9Sstevel@tonic-gate 			kb8042_debug = B_FALSE;
15267c478bd9Sstevel@tonic-gate 			kb8042_getchar_debug = B_FALSE;
15277c478bd9Sstevel@tonic-gate 			kb8042_low_level_debug = B_FALSE;
15287c478bd9Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_FALSE;
15297c478bd9Sstevel@tonic-gate 		}
15307c478bd9Sstevel@tonic-gate 		break;
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate #endif
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate static boolean_t
15367c478bd9Sstevel@tonic-gate kb8042_autorepeat_detect(
15377c478bd9Sstevel@tonic-gate     struct kb8042 *kb8042,
15387c478bd9Sstevel@tonic-gate     int key_pos,
15397c478bd9Sstevel@tonic-gate     enum keystate state)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	if (state == KEY_RELEASED) {
15427c478bd9Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos)
15437c478bd9Sstevel@tonic-gate 			kb8042->kb_old_key_pos = 0;
15447c478bd9Sstevel@tonic-gate 	} else {
15457c478bd9Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos) {
15467c478bd9Sstevel@tonic-gate 			return (B_TRUE);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 		kb8042->kb_old_key_pos = key_pos;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	return (B_FALSE);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate /* ARGSUSED */
15547c478bd9Sstevel@tonic-gate static void
15557c478bd9Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	switch (cmd) {
15587c478bd9Sstevel@tonic-gate 	case KBD_CMD_BELL:
1559c35aa225Smarx 		(void) beeper_on(BEEP_TYPE4);
15607c478bd9Sstevel@tonic-gate 		break;
15617c478bd9Sstevel@tonic-gate 	case KBD_CMD_NOBELL:
1562c35aa225Smarx 		(void) beeper_off();
15637c478bd9Sstevel@tonic-gate 		break;
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate /*
15697c478bd9Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
15707c478bd9Sstevel@tonic-gate  */
15717c478bd9Sstevel@tonic-gate static int
1572281f0747Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg)
15737c478bd9Sstevel@tonic-gate {
15747c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	return (kbtrans_getchar(kb8042->hw_kbtrans));
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate static int
1585281f0747Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	struct kb8042	*kb8042;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	return (kbtrans_ischar(kb8042->hw_kbtrans));
15927c478bd9Sstevel@tonic-gate }
1593