xref: /onnv-gate/usr/src/uts/common/io/kb8042/kb8042.c (revision 1762:b91aae9ab33e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1762Slt200341  * Common Development and Distribution License (the "License").
6*1762Slt200341  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*1762Slt200341  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/inline.h>
350Sstevel@tonic-gate #include <sys/termio.h>
360Sstevel@tonic-gate #include <sys/stropts.h>
370Sstevel@tonic-gate #include <sys/termios.h>
380Sstevel@tonic-gate #include <sys/stream.h>
390Sstevel@tonic-gate #include <sys/strtty.h>
400Sstevel@tonic-gate #include <sys/strsubr.h>
410Sstevel@tonic-gate #include <sys/strsun.h>
420Sstevel@tonic-gate #include <sys/ddi.h>
430Sstevel@tonic-gate #include <sys/sunddi.h>
440Sstevel@tonic-gate #include <sys/note.h>
450Sstevel@tonic-gate #include "sys/consdev.h"
460Sstevel@tonic-gate #include <sys/kbd.h>
470Sstevel@tonic-gate #include <sys/kbtrans.h>
480Sstevel@tonic-gate #include "kb8042.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/i8042.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include "sys/kbio.h"		/* for KIOCSLAYOUT */
530Sstevel@tonic-gate #include "sys/stat.h"
540Sstevel@tonic-gate #include "sys/reboot.h"
550Sstevel@tonic-gate #include <sys/promif.h>
560Sstevel@tonic-gate #include <sys/beep.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * For any keyboard, there is a unique code describing the position
600Sstevel@tonic-gate  * of the key on a keyboard. We refer to the code as "station number".
610Sstevel@tonic-gate  * The following table is used to map the station numbers from ps2
620Sstevel@tonic-gate  * AT/XT keyboards to that of a USB one.
63822Ssethg  *
64822Ssethg  * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
65822Ssethg  * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
66822Ssethg  * when in KB_PC mode.  Note that this does not need to be made conditional
67822Ssethg  * on the architecture for which this module is complied because there are no
68822Ssethg  * keys that map to K8042_STOP on non-SPARC platforms.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
710Sstevel@tonic-gate /*  0 */	0,	53,	30,	31,	32,	33,	34,	35,
720Sstevel@tonic-gate /*  8 */	36,	37,	38,	39,	45,	46,	137,	42,
730Sstevel@tonic-gate /* 16 */	43,	20,	26,	8,	21,	23,	28,	24,
740Sstevel@tonic-gate /* 24 */	12,	18,	19,	47,	48,	49,	57,	4,
750Sstevel@tonic-gate /* 32 */	22,	7,	9,	10,	11,	13,	14,	15,
760Sstevel@tonic-gate /* 40 */	51,	52,	50,	40,	225,	100,	29,	27,
770Sstevel@tonic-gate /* 48 */	6,	25,	5,	17,	16,	54,	55,	56,
780Sstevel@tonic-gate /* 56 */	135,	229,	224,	227,	226,	44,	230,	231,
790Sstevel@tonic-gate /* 64 */	228,	101,	0,	0,	0,	0,	0,	0,
800Sstevel@tonic-gate /* 72 */	0,	0,	0,	73,	76,	0,	0,	80,
810Sstevel@tonic-gate /* 80 */	74,	77,	0,	82,	81,	75,	78,	0,
820Sstevel@tonic-gate /* 88 */	0,	79,	83,	95,	92,	89,	0,	84,
830Sstevel@tonic-gate /* 96 */	96,	93,	90,	98,	85,	97,	94,	91,
840Sstevel@tonic-gate /* 104 */	99,	86,	87,	133,	88,	0,	41,	0,
850Sstevel@tonic-gate /* 112 */	58,	59,	60,	61,	62,	63,	64,	65,
860Sstevel@tonic-gate /* 120 */	66,	67,	68,	69,	70,	71,	72,	0,
870Sstevel@tonic-gate /* 128 */	0,	0,	0,	139,	138,	136,	0,	0,
880Sstevel@tonic-gate /* 136 */	0,	0,	0,	0,	0,	0,	0,	0,
890Sstevel@tonic-gate /* 144 */	0,	0,	0,	0,	0,	0,	0,	0,
900Sstevel@tonic-gate /* 152 */	0,	0,	0,	0,	0,	0,	0,	0,
91822Ssethg /* 160 */	120,	0,	0,	0,	0,	0,	0,	0,
920Sstevel@tonic-gate /* 168 */	0,	0,	0,	0,	0,	0,	0,	0,
930Sstevel@tonic-gate /* 176 */	0,	0,	0,	0,	0,	0,	0,	0,
940Sstevel@tonic-gate /* 184 */	0,	0,	0,	0,	0,	0,	0,	0,
950Sstevel@tonic-gate /* 192 */	0,	0,	0,	0,	0,	0,	0,	0,
960Sstevel@tonic-gate /* 200 */	0,	0,	0,	0,	0,	0,	0,	0,
970Sstevel@tonic-gate /* 208 */	0,	0,	0,	0,	0,	0,	0,	0,
980Sstevel@tonic-gate /* 216 */	0,	0,	0,	0,	0,	0,	0,	0,
990Sstevel@tonic-gate /* 224 */	0,	0,	0,	0,	0,	0,	0,	0,
1000Sstevel@tonic-gate /* 232 */	0,	0,	0,	0,	0,	0,	0,	0,
1010Sstevel@tonic-gate /* 240 */	0,	0,	0,	0,	0,	0,	0,	0,
1020Sstevel@tonic-gate /* 248 */	0,	0,	0,	0
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * DEBUG (or KD_DEBUG for just this module) turns on a flag called
1070Sstevel@tonic-gate  * kb8042_enable_debug_hotkey.  If kb8042_enable_debug_hotkey is true,
1080Sstevel@tonic-gate  * then the following hotkeys are enabled:
1090Sstevel@tonic-gate  *    F10 - turn on debugging "normal" translations
1100Sstevel@tonic-gate  *    F9  - turn on debugging "getchar" translations
1110Sstevel@tonic-gate  *    F8  - turn on "low level" debugging
1120Sstevel@tonic-gate  *    F7  - turn on terse press/release debugging
1130Sstevel@tonic-gate  *    F1  - turn off all debugging
1140Sstevel@tonic-gate  * The default value for kb8042_enable_debug_hotkey is false, disabling
1150Sstevel@tonic-gate  * these hotkeys.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #if	defined(DEBUG) || defined(lint)
1190Sstevel@tonic-gate #define	KD_DEBUG
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #ifdef	KD_DEBUG
1230Sstevel@tonic-gate boolean_t	kb8042_enable_debug_hotkey = B_FALSE;
1240Sstevel@tonic-gate boolean_t	kb8042_debug = B_FALSE;
1250Sstevel@tonic-gate boolean_t	kb8042_getchar_debug = B_FALSE;
1260Sstevel@tonic-gate boolean_t	kb8042_low_level_debug = B_FALSE;
1270Sstevel@tonic-gate boolean_t	kb8042_pressrelease_debug = B_FALSE;
1280Sstevel@tonic-gate static void kb8042_debug_hotkey(int scancode);
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate 
131822Ssethg #ifdef __sparc
132822Ssethg #define	USECS_PER_WAIT 100
133822Ssethg #define	MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
134822Ssethg #define	MIN_DELAY_USECS USECS_PER_WAIT
135822Ssethg 
136822Ssethg boolean_t kb8042_warn_unknown_scanset = B_TRUE;
137822Ssethg int kb8042_default_scanset = 2;
138822Ssethg 
139822Ssethg #endif
140822Ssethg 
1410Sstevel@tonic-gate enum state_return { STATE_NORMAL, STATE_INTERNAL };
1420Sstevel@tonic-gate 
143822Ssethg static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
1440Sstevel@tonic-gate static uint_t kb8042_intr(caddr_t arg);
1450Sstevel@tonic-gate static void kb8042_wait_poweron(struct kb8042 *kb8042);
1460Sstevel@tonic-gate static void kb8042_start_state_machine(struct kb8042 *, boolean_t);
1470Sstevel@tonic-gate static enum state_return kb8042_state_machine(struct kb8042 *, int, boolean_t);
1480Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
1490Sstevel@tonic-gate static int kb8042_xlate_leds(int);
150822Ssethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
1510Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
1520Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
1530Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck(
1540Sstevel@tonic-gate 			struct kbtrans_hardware *hw, int *key,
1550Sstevel@tonic-gate 			enum keystate *state);
1560Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
1570Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
1580Sstevel@tonic-gate 			enum keystate state);
1590Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
1600Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
1610Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *);
1620Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
163*1762Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
164*1762Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
165822Ssethg static void kb8042_cleanup(struct kb8042 *kb8042);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = {
1680Sstevel@tonic-gate 	kb8042_streams_setled,
1690Sstevel@tonic-gate 	kb8042_polled_setled,
1700Sstevel@tonic-gate 	kb8042_polled_keycheck,
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate extern struct keyboard keyindex_pc;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate #define	DRIVER_NAME(dip) ddi_driver_name(dip)
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static	char	module_name[] = "kb8042";
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
1800Sstevel@tonic-gate 			cred_t *credp);
1810Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
1820Sstevel@tonic-gate static int kb8042_wsrv();
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate struct module_info kb8042_sinfo = {
1850Sstevel@tonic-gate 	42,		/* Module ID */
1860Sstevel@tonic-gate 	module_name,
1870Sstevel@tonic-gate 	0, 32,		/* Minimum and maximum packet sizes */
1880Sstevel@tonic-gate 	256, 128	/* High and low water marks */
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct qinit kb8042_rinit = {
1920Sstevel@tonic-gate 	NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate static struct qinit kb8042_winit = {
1960Sstevel@tonic-gate 	putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1970Sstevel@tonic-gate };
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate struct streamtab
2000Sstevel@tonic-gate 	kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate struct kb8042	Kdws = {0};
203822Ssethg static dev_info_t *kb8042_dip = NULL;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2060Sstevel@tonic-gate 		void **result);
2070Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
208822Ssethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static 	struct cb_ops cb_kb8042_ops = {
2110Sstevel@tonic-gate 	nulldev,		/* cb_open */
2120Sstevel@tonic-gate 	nulldev,		/* cb_close */
2130Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2140Sstevel@tonic-gate 	nodev,			/* cb_print */
2150Sstevel@tonic-gate 	nodev,			/* cb_dump */
2160Sstevel@tonic-gate 	nodev,			/* cb_read */
2170Sstevel@tonic-gate 	nodev,			/* cb_write */
2180Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
2190Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2200Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2210Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2220Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2230Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2240Sstevel@tonic-gate 	&kb8042_str_info,	/* cb_stream */
2250Sstevel@tonic-gate 	D_MP
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate struct dev_ops kb8042_ops = {
2290Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2300Sstevel@tonic-gate 	0,			/* devo_refcnt */
2310Sstevel@tonic-gate 	kb8042_getinfo,		/* devo_getinfo */
2320Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2330Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2340Sstevel@tonic-gate 	kb8042_attach,		/* devo_attach */
235822Ssethg 	kb8042_detach,		/* devo_detach */
2360Sstevel@tonic-gate 	nodev,			/* devo_reset */
2370Sstevel@tonic-gate 	&cb_kb8042_ops,		/* devo_cb_ops */
2380Sstevel@tonic-gate 	(struct bus_ops *)NULL	/* devo_bus_ops */
2390Sstevel@tonic-gate };
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * This is the loadable module wrapper.
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate #include <sys/modctl.h>
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Module linkage information for the kernel.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static struct modldrv modldrv = {
2510Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
2520Sstevel@tonic-gate 	"PS/2 Keyboard %I%, %E%",
2530Sstevel@tonic-gate 	&kb8042_ops,	/* driver ops */
2540Sstevel@tonic-gate };
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static struct modlinkage modlinkage = {
2570Sstevel@tonic-gate 	MODREV_1,
2580Sstevel@tonic-gate 	(void *) &modldrv,
2590Sstevel@tonic-gate 	NULL
2600Sstevel@tonic-gate };
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate int
2630Sstevel@tonic-gate _init(void)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	int	rv;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	rv = mod_install(&modlinkage);
2680Sstevel@tonic-gate 	return (rv);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate int
2730Sstevel@tonic-gate _fini(void)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate int
2790Sstevel@tonic-gate _info(struct modinfo *modinfop)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
284822Ssethg #ifdef __sparc
285822Ssethg static boolean_t
286822Ssethg kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
287822Ssethg {
288822Ssethg 	int i;
289822Ssethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
290822Ssethg 	    I8042_INT_INPUT_AVAIL;
291822Ssethg 	int reps = timeout_usec / USECS_PER_WAIT;
292822Ssethg 
293822Ssethg 	for (i = 0; i < reps; i++) {
294822Ssethg 		if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
295822Ssethg 			return (B_TRUE);
296822Ssethg 
297822Ssethg 		if (i < (reps - 1))
298822Ssethg 			drv_usecwait(USECS_PER_WAIT);
299822Ssethg 	}
300822Ssethg 	return (B_FALSE);
301822Ssethg }
302822Ssethg 
303822Ssethg static void
304822Ssethg kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
305822Ssethg {
306822Ssethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
307822Ssethg 	    I8042_INT_INPUT_DATA;
308822Ssethg 
309822Ssethg 	while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
310822Ssethg 		(void) ddi_get8(kb8042->handle, kb8042->addr + port);
311822Ssethg 	}
312822Ssethg }
313822Ssethg 
314822Ssethg static boolean_t
315822Ssethg kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
316822Ssethg     boolean_t polled, int timeout, int *error, uint8_t *got)
317822Ssethg {
318822Ssethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
319822Ssethg 	    I8042_INT_INPUT_DATA;
320822Ssethg 	uint8_t datab;
321822Ssethg 	int err;
322822Ssethg 	boolean_t rval;
323822Ssethg 
324822Ssethg 	kb8042_send_to_keyboard(kb8042, send, polled);
325822Ssethg 
326822Ssethg 	if (kb8042_is_input_avail(kb8042, timeout, polled)) {
327822Ssethg 		err = 0;
328822Ssethg 		datab = ddi_get8(kb8042->handle, kb8042->addr + port);
329822Ssethg 		rval = ((datab == expect) ? B_TRUE : B_FALSE);
330822Ssethg 	} else {
331822Ssethg 		err = EAGAIN;
332822Ssethg 		rval = B_FALSE;
333822Ssethg 	}
334822Ssethg 
335822Ssethg 	if (error != NULL)
336822Ssethg 		*error = err;
337822Ssethg 	if (got != NULL)
338822Ssethg 		*got = datab;
339822Ssethg 	return (rval);
340822Ssethg }
341822Ssethg 
342822Ssethg static const char *
343822Ssethg kb8042_error_string(int errcode)
344822Ssethg {
345822Ssethg 	switch (errcode) {
346822Ssethg 	case EAGAIN:
347822Ssethg 		return ("Timed out");
348822Ssethg 	default:
349822Ssethg 		return ("Unknown error");
350822Ssethg 	}
351822Ssethg }
352822Ssethg 
353822Ssethg static int
354822Ssethg kb8042_read_scanset(struct kb8042 *kb8042, boolean_t polled)
355822Ssethg {
356822Ssethg 	int scanset = -1;
357822Ssethg 	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
358822Ssethg 	    I8042_INT_INPUT_DATA;
359822Ssethg 	int err;
360822Ssethg 	uint8_t got;
361822Ssethg 
362822Ssethg 	kb8042_clear_input_buffer(kb8042, B_TRUE);
363822Ssethg 
364822Ssethg 	/*
365822Ssethg 	 * Send a "change scan code set" command to the keyboard.
366822Ssethg 	 * It should respond with an ACK.
367822Ssethg 	 */
368822Ssethg 	if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, polled,
369822Ssethg 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
370822Ssethg 		goto fail_read_scanset;
371822Ssethg 	}
372822Ssethg 
373822Ssethg 	/*
374822Ssethg 	 * Send a 0.  The keyboard should ACK the 0, then it send the scan code
375822Ssethg 	 * set in use.
376822Ssethg 	 */
377822Ssethg 	if (kb8042_send_and_expect(kb8042, 0, KB_ACK, polled,
378822Ssethg 	    MAX_WAIT_USECS, &err, &got) != B_TRUE) {
379822Ssethg 		goto fail_read_scanset;
380822Ssethg 	}
381822Ssethg 
382822Ssethg 	/*
383822Ssethg 	 * The next input byte from the keyboard should be the scan code
384822Ssethg 	 * set in use, though some keyboards like to send a few more acks
385822Ssethg 	 * just for fun, so blow past those to get the keyboard scan code.
386822Ssethg 	 */
387822Ssethg 	while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
388822Ssethg 		(scanset = ddi_get8(kb8042->handle, kb8042->addr + port))
389822Ssethg 		    == KB_ACK)
390822Ssethg 		;
391822Ssethg 
392822Ssethg #ifdef KD_DEBUG
393822Ssethg 	cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
394822Ssethg 	    scanset);
395822Ssethg #endif
396822Ssethg 
397822Ssethg 	return (scanset);
398822Ssethg 
399822Ssethg fail_read_scanset:
400822Ssethg #ifdef KD_DEBUG
401822Ssethg 	if (err == 0)
402822Ssethg 		cmn_err(CE_NOTE, "Could not read current scan set from "
403822Ssethg 		    "keyboard: %s. (Expected 0x%x, but got 0x%x).",
404822Ssethg 		    kb8042_error_string(err), KB_ACK, got);
405822Ssethg 	else
406822Ssethg 		cmn_err(CE_NOTE, "Could not read current scan set from "
407822Ssethg 		    "keyboard: %s.", kb8042_error_string(err));
408822Ssethg #endif
409822Ssethg 	return (-1);
410822Ssethg }
411822Ssethg #endif
412822Ssethg 
4130Sstevel@tonic-gate static int
4140Sstevel@tonic-gate kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	int	rc;
417822Ssethg 	int	scanset;
418822Ssethg 	int	leds;
4190Sstevel@tonic-gate 
420822Ssethg 	struct kb8042	*kb8042 = &Kdws;
4210Sstevel@tonic-gate 	static ddi_device_acc_attr_t attr = {
4220Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
4230Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
4240Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
4250Sstevel@tonic-gate 	};
4260Sstevel@tonic-gate 
427822Ssethg 	switch (cmd) {
428822Ssethg 	case DDI_RESUME:
429822Ssethg 		leds = kb8042->leds.commanded;
430822Ssethg 		kb8042->w_init = 0;
431822Ssethg 		kb8042_init(kb8042, B_TRUE);
432822Ssethg 		kb8042_setled(kb8042, leds, B_FALSE);
433822Ssethg 		return (DDI_SUCCESS);
4340Sstevel@tonic-gate 
435822Ssethg 	case DDI_ATTACH:
436822Ssethg 		if (kb8042_dip != NULL)
437822Ssethg 			return (DDI_FAILURE);
438822Ssethg 		/* The rest of the function is for attach */
439822Ssethg 		break;
4400Sstevel@tonic-gate 
441822Ssethg 	default:
442822Ssethg 		return (DDI_FAILURE);
443822Ssethg 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	kb8042->debugger.mod1 = 58;	/* Left Ctrl */
4460Sstevel@tonic-gate 	kb8042->debugger.mod2 = 60;	/* Left Alt */
4470Sstevel@tonic-gate 	kb8042->debugger.trigger = 33;	/* D */
4480Sstevel@tonic-gate 	kb8042->debugger.mod1_down = B_FALSE;
4490Sstevel@tonic-gate 	kb8042->debugger.mod2_down = B_FALSE;
4500Sstevel@tonic-gate 	kb8042->debugger.enabled = B_FALSE;
4510Sstevel@tonic-gate 
452822Ssethg 	kb8042_dip = devi;
453822Ssethg 	kb8042->init_state = KB8042_UNINITIALIZED;
454822Ssethg 
4550Sstevel@tonic-gate 	kb8042->polled_synthetic_release_pending = B_FALSE;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
4580Sstevel@tonic-gate 		    DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
459822Ssethg 		goto failure;
4600Sstevel@tonic-gate 	}
461822Ssethg 
462822Ssethg 	kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
4650Sstevel@tonic-gate 		(offset_t)0, (offset_t)0, &attr, &kb8042->handle);
4660Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
4670Sstevel@tonic-gate #if	defined(KD_DEBUG)
4680Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
4690Sstevel@tonic-gate #endif
470822Ssethg 		goto failure;
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
473822Ssethg 	kb8042->init_state |= KB8042_REGS_MAPPED;
474822Ssethg 
4750Sstevel@tonic-gate 	if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
4760Sstevel@tonic-gate 		DDI_SUCCESS) {
4770Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
478822Ssethg 		goto failure;
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
4820Sstevel@tonic-gate 
483822Ssethg 	kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
484822Ssethg 
485822Ssethg 	kb8042_init(kb8042, B_FALSE);
486822Ssethg 
487822Ssethg #ifdef __sparc
488822Ssethg 	/* Detect the scan code set currently in use */
489822Ssethg 	scanset = kb8042_read_scanset(kb8042, B_TRUE);
490822Ssethg 
491822Ssethg 	if (scanset < 0 && kb8042_warn_unknown_scanset) {
492822Ssethg 
493822Ssethg 		cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
494822Ssethg 		cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
495822Ssethg 		cmn_err(CE_CONT, "Defaulting to scan code set %d.  If the "
496822Ssethg 		    "keyboard does not ", kb8042_default_scanset);
497822Ssethg 		cmn_err(CE_CONT, "work properly, add "
498822Ssethg 		    "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
499822Ssethg 		    (kb8042_default_scanset == 1) ? 2 : 1);
500822Ssethg 		cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
501822Ssethg 		    "restart the system.  If you ");
502822Ssethg 		cmn_err(CE_CONT, "do not want to see this message in the "
503822Ssethg 		    "future, add ");
504822Ssethg 		cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
505822Ssethg 		    "to /etc/system.\n");
506822Ssethg 
507822Ssethg 		/* Use the default scan code set. */
508822Ssethg 		scanset = kb8042_default_scanset;
509822Ssethg 	}
510822Ssethg #else
511822Ssethg 	/* x86 systems use scan code set 1 -- no detection required */
512822Ssethg 	scanset = 1;
513822Ssethg #endif
514822Ssethg 	if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
515822Ssethg 		cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
516822Ssethg 		    "Unknown scan code set `%d'.", scanset);
517822Ssethg 		/* Scan code set is not supported */
518822Ssethg 		goto failure;
519822Ssethg 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Turn on interrupts...
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0,
5250Sstevel@tonic-gate 		&kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
5260Sstevel@tonic-gate 		kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
5270Sstevel@tonic-gate 		cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
528822Ssethg 		goto failure;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
531822Ssethg 	kb8042->init_state |= KB8042_INTR_ADDED;
532822Ssethg 
5330Sstevel@tonic-gate 	ddi_report_dev(devi);
534822Ssethg 
5350Sstevel@tonic-gate #ifdef	KD_DEBUG
5360Sstevel@tonic-gate 	cmn_err(CE_CONT, "?%s #%d: version %s\n",
5370Sstevel@tonic-gate 	    DRIVER_NAME(devi), ddi_get_instance(devi), "%I% (%E%)");
5380Sstevel@tonic-gate #endif
539822Ssethg 
5400Sstevel@tonic-gate 	return (DDI_SUCCESS);
541822Ssethg 
542822Ssethg failure:
543822Ssethg 	kb8042_cleanup(kb8042);
544822Ssethg 	return (DDI_FAILURE);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
547822Ssethg static int
548822Ssethg kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
549822Ssethg {
550822Ssethg 	struct kb8042 *kb8042 = &Kdws;
551822Ssethg 
552822Ssethg 	switch (cmd) {
553822Ssethg 	case DDI_SUSPEND:
554822Ssethg 		return (DDI_SUCCESS);
555822Ssethg 
556822Ssethg 	case DDI_DETACH:
557822Ssethg 		/* If someone has a stream open, fail to detach */
558822Ssethg 		if (kb8042->w_qp != NULL)
559822Ssethg 			return (DDI_FAILURE);
560822Ssethg 
561822Ssethg 		ASSERT(kb8042_dip == dip);
562822Ssethg 
563822Ssethg 		kb8042_cleanup(kb8042);
564822Ssethg 
565822Ssethg 		return (DDI_SUCCESS);
566822Ssethg 
567822Ssethg 	default:
568822Ssethg 		return (DDI_FAILURE);
569822Ssethg 	}
570822Ssethg }
571822Ssethg 
572822Ssethg 
5730Sstevel@tonic-gate /*ARGSUSED*/
5740Sstevel@tonic-gate static int
5750Sstevel@tonic-gate kb8042_getinfo(
5760Sstevel@tonic-gate     dev_info_t *dip,
5770Sstevel@tonic-gate     ddi_info_cmd_t infocmd,
5780Sstevel@tonic-gate     void *arg,
5790Sstevel@tonic-gate     void **result)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	register int error;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	switch (infocmd) {
5840Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5850Sstevel@tonic-gate 		if (kb8042_dip == NULL) {
5860Sstevel@tonic-gate 			error = DDI_FAILURE;
5870Sstevel@tonic-gate 		} else {
5880Sstevel@tonic-gate 			*result = (void *) kb8042_dip;
5890Sstevel@tonic-gate 			error = DDI_SUCCESS;
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		break;
5920Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5930Sstevel@tonic-gate 		*result = (void *)0;
5940Sstevel@tonic-gate 		error = DDI_SUCCESS;
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 	default:
5970Sstevel@tonic-gate 		error = DDI_FAILURE;
5980Sstevel@tonic-gate 		break;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 	return (error);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate static void
604822Ssethg kb8042_cleanup(struct kb8042 *kb8042)
605822Ssethg {
606845Ssethg 	ASSERT(kb8042_dip != NULL);
607822Ssethg 
608822Ssethg 	if (kb8042->init_state & KB8042_HW_MUTEX_INITTED)
609822Ssethg 		mutex_destroy(&kb8042->w_hw_mutex);
610822Ssethg 
611822Ssethg 	if (kb8042->init_state & KB8042_INTR_ADDED)
612822Ssethg 		ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
613822Ssethg 
614822Ssethg 	if (kb8042->init_state & KB8042_REGS_MAPPED)
615822Ssethg 		ddi_regs_map_free(&kb8042->handle);
616822Ssethg 
617822Ssethg 	if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
618822Ssethg 		ddi_remove_minor_node(kb8042_dip, NULL);
619822Ssethg 
620822Ssethg 	kb8042->init_state = KB8042_UNINITIALIZED;
621845Ssethg 	kb8042_dip = NULL;
622822Ssethg }
623822Ssethg 
624822Ssethg static void
625822Ssethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	if (kb8042->w_init)
6280Sstevel@tonic-gate 		return;
6290Sstevel@tonic-gate 
630822Ssethg 	if (!from_resume) {
631822Ssethg 		kb8042->w_kblayout = 0;	/* Default to US */
632822Ssethg 		kb8042->w_qp = (queue_t *)NULL;
633822Ssethg 		kb8042->simulated_kbd_type = KB_PC;
634822Ssethg 		kb8042->leds.commanded = -1;	/* Unknown initial state */
635822Ssethg 		kb8042->leds.desired = -1;	/* Unknown initial state */
636822Ssethg 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	kb8042_wait_poweron(kb8042);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	kb8042->kb_old_key_pos = 0;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/* Set up the command state machine and start it running. */
6430Sstevel@tonic-gate 	kb8042->command_state = KB_COMMAND_STATE_WAIT;
6440Sstevel@tonic-gate 	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	kb8042->w_init++;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*ARGSUSED2*/
6520Sstevel@tonic-gate static int
6530Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate 	struct kb8042	*kb8042;
6560Sstevel@tonic-gate 	int err;
6570Sstevel@tonic-gate 	int initial_leds;
6580Sstevel@tonic-gate 	int initial_led_mask;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	kb8042 = &Kdws;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	kb8042->w_dev = *devp;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (qp->q_ptr) {
6650Sstevel@tonic-gate 		return (0);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	qp->q_ptr = (caddr_t)kb8042;
6680Sstevel@tonic-gate 	WR(qp)->q_ptr = qp->q_ptr;
6690Sstevel@tonic-gate 	if (!kb8042->w_qp)
6700Sstevel@tonic-gate 		kb8042->w_qp = qp;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
6730Sstevel@tonic-gate 	err = kbtrans_streams_init(qp, sflag, credp,
6740Sstevel@tonic-gate 		(struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
6750Sstevel@tonic-gate 		&kb8042->hw_kbtrans,
6760Sstevel@tonic-gate 		initial_leds, initial_led_mask);
6770Sstevel@tonic-gate 	if (err != 0)
6780Sstevel@tonic-gate 		return (err);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
6830Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_argument =
684*1762Slt200341 		(cons_polledio_arg_t)kb8042;
6850Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_putchar = NULL;
6860Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_getchar =
687*1762Slt200341 		(int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
6880Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_ischar =
689*1762Slt200341 		(boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
6900Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_enter = NULL;
6910Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_exit = NULL;
6920Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_setled =
693*1762Slt200341 		(void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
6940Sstevel@tonic-gate 	kb8042->polledio.cons_polledio_keycheck =
695*1762Slt200341 		(boolean_t (*)(cons_polledio_arg_t, int *,
6960Sstevel@tonic-gate 		enum keystate *))kb8042_polled_keycheck;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	qprocson(qp);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	kbtrans_streams_enable(kb8042->hw_kbtrans);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	return (0);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*ARGSUSED1*/
7060Sstevel@tonic-gate static int
7070Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	struct kb8042	*kb8042;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7120Sstevel@tonic-gate 
713672Sry162471 	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);
714672Sry162471 
7150Sstevel@tonic-gate 	kb8042->w_qp = (queue_t *)NULL;
7160Sstevel@tonic-gate 	qprocsoff(qp);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	return (0);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate static int
7220Sstevel@tonic-gate kb8042_wsrv(queue_t *qp)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	struct kb8042 *kb8042;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	mblk_t	*mp;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)qp->q_ptr;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	while ((mp = getq(qp))) {
7310Sstevel@tonic-gate 		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
7320Sstevel@tonic-gate 		case KBTRANS_MESSAGE_HANDLED:
7330Sstevel@tonic-gate 			continue;
7340Sstevel@tonic-gate 		case KBTRANS_MESSAGE_NOT_HANDLED:
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 		}
7370Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
7380Sstevel@tonic-gate 		case M_IOCTL:
7390Sstevel@tonic-gate 			kb8042_ioctlmsg(kb8042, qp, mp);
7400Sstevel@tonic-gate 			continue;
7410Sstevel@tonic-gate 		case M_IOCDATA:
7420Sstevel@tonic-gate 			kb8042_iocdatamsg(qp, mp);
7430Sstevel@tonic-gate 			continue;
7440Sstevel@tonic-gate 		case M_DELAY:
7450Sstevel@tonic-gate 		case M_STARTI:
7460Sstevel@tonic-gate 		case M_STOPI:
7470Sstevel@tonic-gate 		case M_READ:	/* ignore, no buffered data */
7480Sstevel@tonic-gate 			freemsg(mp);
7490Sstevel@tonic-gate 			continue;
7500Sstevel@tonic-gate 		case M_FLUSH:
7510Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
7520Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR)
7530Sstevel@tonic-gate 				qreply(qp, mp);
7540Sstevel@tonic-gate 			else
7550Sstevel@tonic-gate 				freemsg(mp);
7560Sstevel@tonic-gate 			continue;
7570Sstevel@tonic-gate 		default:
7580Sstevel@tonic-gate 			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
7590Sstevel@tonic-gate 						mp->b_datap->db_type);
7600Sstevel@tonic-gate 			freemsg(mp);
7610Sstevel@tonic-gate 			continue;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 	return (0);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate static void
7680Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	struct iocblk	*iocp;
7710Sstevel@tonic-gate 	mblk_t		*datap;
7720Sstevel@tonic-gate 	int		error;
7730Sstevel@tonic-gate 	int		tmp;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
7800Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
7810Sstevel@tonic-gate 		if (error != 0) {
7820Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
7830Sstevel@tonic-gate 			return;
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		/*
7870Sstevel@tonic-gate 		 * We are given an appropriate-sized data block,
7880Sstevel@tonic-gate 		 * and return a pointer to our structure in it.
7890Sstevel@tonic-gate 		 */
7900Sstevel@tonic-gate 		*(struct cons_polledio **)mp->b_cont->b_rptr =
7910Sstevel@tonic-gate 		    &kb8042->polledio;
7920Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
7930Sstevel@tonic-gate 		iocp->ioc_error = 0;
7940Sstevel@tonic-gate 		qreply(qp, mp);
7950Sstevel@tonic-gate 		break;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
7980Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
7990Sstevel@tonic-gate 		break;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8020Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8030Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8040Sstevel@tonic-gate 			return;
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
8080Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8090Sstevel@tonic-gate 		break;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/*
8120Sstevel@tonic-gate 	 * Valid only in TR_UNTRANS_MODE mode.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	case CONSSETKBDTYPE:
8150Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8160Sstevel@tonic-gate 		if (error != 0) {
8170Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
8180Sstevel@tonic-gate 			return;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 		tmp =  *(int *)mp->b_cont->b_rptr;
8210Sstevel@tonic-gate 		if (tmp != KB_PC && tmp != KB_USB) {
8220Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8230Sstevel@tonic-gate 			break;
8240Sstevel@tonic-gate 		}
8250Sstevel@tonic-gate 		kb8042->simulated_kbd_type = tmp;
8260Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8270Sstevel@tonic-gate 		break;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	case KIOCLAYOUT:
8300Sstevel@tonic-gate 		if (kb8042->w_kblayout == -1) {
8310Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8320Sstevel@tonic-gate 			return;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
8360Sstevel@tonic-gate 			miocnak(qp, mp, 0, ENOMEM);
8370Sstevel@tonic-gate 			return;
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB)
8410Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
8420Sstevel@tonic-gate 		else
8430Sstevel@tonic-gate 			*(int *)datap->b_wptr = kb8042->w_kblayout;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
8460Sstevel@tonic-gate 		if (mp->b_cont)
8470Sstevel@tonic-gate 			freemsg(mp->b_cont);
8480Sstevel@tonic-gate 		mp->b_cont = datap;
8490Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
8500Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
8510Sstevel@tonic-gate 		iocp->ioc_error = 0;
8520Sstevel@tonic-gate 		qreply(qp, mp);
8530Sstevel@tonic-gate 		break;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	case KIOCSLAYOUT:
8560Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8570Sstevel@tonic-gate 			miocnak(qp, mp, 0, EINVAL);
8580Sstevel@tonic-gate 			return;
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
8620Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8630Sstevel@tonic-gate 		break;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	case KIOCCMD:
8660Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8670Sstevel@tonic-gate 		if (error != 0) {
8680Sstevel@tonic-gate 			miocnak(qp, mp, 0, error);
8690Sstevel@tonic-gate 			return;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
8730Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
8740Sstevel@tonic-gate 		break;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	default:
8770Sstevel@tonic-gate #ifdef DEBUG1
8780Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
8790Sstevel@tonic-gate #endif
8800Sstevel@tonic-gate 		miocnak(qp, mp, 0, EINVAL);
8810Sstevel@tonic-gate 		return;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * Process a byte received from the keyboard
8870Sstevel@tonic-gate  */
8880Sstevel@tonic-gate static void
8890Sstevel@tonic-gate kb8042_received_byte(
8900Sstevel@tonic-gate 	struct kb8042	*kb8042,
8910Sstevel@tonic-gate 	int		scancode)	/* raw scan code */
8920Sstevel@tonic-gate {
8930Sstevel@tonic-gate 	boolean_t	legit;		/* is this a legit key pos'n? */
8940Sstevel@tonic-gate 	int		key_pos = -1;
8950Sstevel@tonic-gate 	enum keystate	state;
8960Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate #ifdef	KD_DEBUG
8990Sstevel@tonic-gate 	kb8042_debug_hotkey(scancode);
9000Sstevel@tonic-gate #endif
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (!kb8042->w_init)	/* can't do anything anyway */
9030Sstevel@tonic-gate 		return;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
9060Sstevel@tonic-gate 		&synthetic_release_needed);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	if (legit == 0) {
9090Sstevel@tonic-gate 		/* Eaten by translation */
9100Sstevel@tonic-gate #ifdef	KD_DEBUG
9110Sstevel@tonic-gate 		if (kb8042_debug)
9120Sstevel@tonic-gate 			prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode);
9130Sstevel@tonic-gate #endif
9140Sstevel@tonic-gate 		return;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate #ifdef	KD_DEBUG
9180Sstevel@tonic-gate 	if (kb8042_debug) {
9190Sstevel@tonic-gate 		prom_printf("kb8042_intr:  0x%x -> %s %d",
9200Sstevel@tonic-gate 			scancode,
9210Sstevel@tonic-gate 			state == KEY_RELEASED ? "released" : "pressed",
9220Sstevel@tonic-gate 			key_pos);
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate #endif
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Don't know if we want this permanently, but it seems interesting
9280Sstevel@tonic-gate 	 * for the moment.
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod1) {
9310Sstevel@tonic-gate #ifdef	KD_DEBUG
9320Sstevel@tonic-gate 		if (kb8042_debug)
9330Sstevel@tonic-gate 			prom_printf(" -> debug mod1");
9340Sstevel@tonic-gate #endif
9350Sstevel@tonic-gate 		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 	if (key_pos == kb8042->debugger.mod2) {
9380Sstevel@tonic-gate #ifdef	KD_DEBUG
9390Sstevel@tonic-gate 		if (kb8042_debug)
9400Sstevel@tonic-gate 			prom_printf(" -> debug mod2");
9410Sstevel@tonic-gate #endif
9420Sstevel@tonic-gate 		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 	if (kb8042->debugger.enabled &&
9450Sstevel@tonic-gate 	    key_pos == kb8042->debugger.trigger &&
9460Sstevel@tonic-gate 	    kb8042->debugger.mod1_down &&
9470Sstevel@tonic-gate 	    kb8042->debugger.mod2_down) {
9480Sstevel@tonic-gate #ifdef	KD_DEBUG
9490Sstevel@tonic-gate 		if (kb8042_debug)
9500Sstevel@tonic-gate 			prom_printf(" -> debugger\n");
9510Sstevel@tonic-gate #endif
9520Sstevel@tonic-gate 		/*
9530Sstevel@tonic-gate 		 * Require new presses of the modifiers.
9540Sstevel@tonic-gate 		 */
9550Sstevel@tonic-gate 		kb8042->debugger.mod1_down = B_FALSE;
9560Sstevel@tonic-gate 		kb8042->debugger.mod2_down = B_FALSE;
9570Sstevel@tonic-gate 		abort_sequence_enter(NULL);
9580Sstevel@tonic-gate 		return;
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * If there's no queue above us - as can happen if we've been
9630Sstevel@tonic-gate 	 * attached but not opened - drop the keystroke.
9640Sstevel@tonic-gate 	 * Note that we do this here instead of above so that
9650Sstevel@tonic-gate 	 * Ctrl-Alt-D still works.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (kb8042->w_qp == NULL) {
9680Sstevel@tonic-gate #ifdef	KD_DEBUG
9690Sstevel@tonic-gate 		if (kb8042_debug)
9700Sstevel@tonic-gate 			prom_printf(" -> nobody home\n");
9710Sstevel@tonic-gate #endif
9720Sstevel@tonic-gate 		return;
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/*
9760Sstevel@tonic-gate 	 * This is to filter out auto repeat since it can't be
9770Sstevel@tonic-gate 	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
9780Sstevel@tonic-gate 	 * can.  Don't know whether they've taken over the world.
9790Sstevel@tonic-gate 	 * Don't think so.)
9800Sstevel@tonic-gate 	 */
9810Sstevel@tonic-gate 	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
9820Sstevel@tonic-gate #ifdef	KD_DEBUG
9830Sstevel@tonic-gate 		if (kb8042_debug)
9840Sstevel@tonic-gate 			prom_printf(" -> autorepeat ignored\n");
9850Sstevel@tonic-gate #endif
9860Sstevel@tonic-gate 		return;
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate #ifdef	KD_DEBUG
9900Sstevel@tonic-gate 	if (kb8042_debug)
9910Sstevel@tonic-gate 		prom_printf(" -> OK\n");
9920Sstevel@tonic-gate #endif
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate #if	defined(KD_DEBUG)
9950Sstevel@tonic-gate 	if (kb8042_pressrelease_debug) {
9960Sstevel@tonic-gate 		prom_printf(" %s%d ",
9970Sstevel@tonic-gate 			state == KEY_PRESSED ? "+" : "-",
9980Sstevel@tonic-gate 			key_pos);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate #endif
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	/*
10050Sstevel@tonic-gate 	 * This is a total hack.  For some stupid reason, the two additional
10060Sstevel@tonic-gate 	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
10070Sstevel@tonic-gate 	 * only.  We synthesize a release immediately.
10080Sstevel@tonic-gate 	 */
10090Sstevel@tonic-gate 	if (synthetic_release_needed) {
10100Sstevel@tonic-gate #if	defined(KD_DEBUG)
10110Sstevel@tonic-gate 		if (kb8042_debug)
10120Sstevel@tonic-gate 			prom_printf("synthetic release %d\n", key_pos);
10130Sstevel@tonic-gate 		if (kb8042_pressrelease_debug)
10140Sstevel@tonic-gate 			prom_printf(" -%d(s) ", key_pos);
10150Sstevel@tonic-gate #endif
10160Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
10170Sstevel@tonic-gate 		kb8042_process_key(kb8042, key_pos, state);
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate static void
10230Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
10240Sstevel@tonic-gate     enum keystate state)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	kbtrans_key_t key;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	ASSERT(key_pos >= 0 && key_pos <= 255);
10290Sstevel@tonic-gate 	if (kb8042->simulated_kbd_type == KB_PC) {
10300Sstevel@tonic-gate 		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
10310Sstevel@tonic-gate 	} else if (kb8042->simulated_kbd_type == KB_USB) {
10320Sstevel@tonic-gate 		key = keytab_pc2usb[key_pos];
10330Sstevel@tonic-gate 		if (key != 0) {
10340Sstevel@tonic-gate 			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate  * Called from interrupt handler when keyboard interrupt occurs.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate static uint_t
10430Sstevel@tonic-gate kb8042_intr(caddr_t arg)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	uchar_t scancode;	/* raw scan code */
10460Sstevel@tonic-gate 	int rc;
10470Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)arg;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	rc = DDI_INTR_UNCLAIMED;
10500Sstevel@tonic-gate 
1051822Ssethg 	if (kb8042->init_state == KB8042_UNINITIALIZED)
1052822Ssethg 		return (DDI_INTR_UNCLAIMED);
1053822Ssethg 
10540Sstevel@tonic-gate 	/* don't care if drv_setparm succeeds */
10550Sstevel@tonic-gate 	(void) drv_setparm(SYSRINT, 1);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
10580Sstevel@tonic-gate 	    != 0) {
10590Sstevel@tonic-gate 		rc = DDI_INTR_CLAIMED;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
10620Sstevel@tonic-gate 			kb8042->addr + I8042_INT_INPUT_DATA);
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate #if	defined(KD_DEBUG)
10650Sstevel@tonic-gate 		if (kb8042_low_level_debug)
10660Sstevel@tonic-gate 			prom_printf(" <K:%x ", scancode);
10670Sstevel@tonic-gate #endif
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		mutex_enter(&kb8042->w_hw_mutex);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
10720Sstevel@tonic-gate 		    STATE_NORMAL) {
10730Sstevel@tonic-gate 			mutex_exit(&kb8042->w_hw_mutex);
10740Sstevel@tonic-gate 			continue;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		mutex_exit(&kb8042->w_hw_mutex);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		kb8042_received_byte(kb8042, scancode);
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	return (rc);
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate static void
10870Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
10880Sstevel@tonic-gate {
10890Sstevel@tonic-gate 	struct copyresp	*csp;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
10920Sstevel@tonic-gate 	if (csp->cp_rval) {
10930Sstevel@tonic-gate 		freemsg(mp);
10940Sstevel@tonic-gate 		return;
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	switch (csp->cp_cmd) {
10980Sstevel@tonic-gate 	default:
10990Sstevel@tonic-gate 		miocack(qp, mp, 0, 0);
11000Sstevel@tonic-gate 		break;
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate static boolean_t
11050Sstevel@tonic-gate kb8042_polled_keycheck(
11060Sstevel@tonic-gate     struct kbtrans_hardware *hw,
11070Sstevel@tonic-gate     int *key,
11080Sstevel@tonic-gate     enum keystate *state)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
11110Sstevel@tonic-gate 	int	scancode;
11120Sstevel@tonic-gate 	boolean_t	legit;
11130Sstevel@tonic-gate 	boolean_t	synthetic_release_needed;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if (kb8042->polled_synthetic_release_pending) {
11160Sstevel@tonic-gate 		*key = kb8042->polled_synthetic_release_key;
11170Sstevel@tonic-gate 		*state = KEY_RELEASED;
11180Sstevel@tonic-gate 		kb8042->polled_synthetic_release_pending = B_FALSE;
11190Sstevel@tonic-gate #if	defined(KD_DEBUG)
11200Sstevel@tonic-gate 		if (kb8042_getchar_debug)
11210Sstevel@tonic-gate 			prom_printf("synthetic release 0x%x\n", *key);
11220Sstevel@tonic-gate #endif
11230Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
11240Sstevel@tonic-gate 		return (B_TRUE);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	for (;;) {
11280Sstevel@tonic-gate 		if (ddi_get8(kb8042->handle,
11290Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
11300Sstevel@tonic-gate 			return (B_FALSE);
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 		scancode = ddi_get8(kb8042->handle,
11340Sstevel@tonic-gate 				kb8042->addr + I8042_POLL_INPUT_DATA);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate #if	defined(KD_DEBUG)
11370Sstevel@tonic-gate 		if (kb8042_low_level_debug)
11380Sstevel@tonic-gate 			prom_printf(" g<%x ", scancode);
11390Sstevel@tonic-gate #endif
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
11420Sstevel@tonic-gate 		    STATE_NORMAL) {
11430Sstevel@tonic-gate 			continue;
11440Sstevel@tonic-gate 		}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate #ifdef	KD_DEBUG
11470Sstevel@tonic-gate 		kb8042_debug_hotkey(scancode);
11480Sstevel@tonic-gate 		if (kb8042_getchar_debug)
11490Sstevel@tonic-gate 			prom_printf("polled 0x%x", scancode);
11500Sstevel@tonic-gate #endif
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		legit = KeyboardConvertScan(kb8042, scancode, key, state,
11530Sstevel@tonic-gate 			&synthetic_release_needed);
11540Sstevel@tonic-gate 		if (!legit) {
11550Sstevel@tonic-gate #ifdef	KD_DEBUG
11560Sstevel@tonic-gate 			if (kb8042_getchar_debug)
11570Sstevel@tonic-gate 				prom_printf(" -> ignored\n");
11580Sstevel@tonic-gate #endif
11590Sstevel@tonic-gate 			continue;
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate #ifdef	KD_DEBUG
11620Sstevel@tonic-gate 		if (kb8042_getchar_debug) {
11630Sstevel@tonic-gate 			prom_printf(" -> %s %d\n",
11640Sstevel@tonic-gate 				*state == KEY_PRESSED ? "pressed" : "released",
11650Sstevel@tonic-gate 				*key);
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate #endif
11680Sstevel@tonic-gate 		/*
11690Sstevel@tonic-gate 		 * For the moment at least, we rely on hardware autorepeat
11700Sstevel@tonic-gate 		 * for polled I/O autorepeat.  However, for coordination
11710Sstevel@tonic-gate 		 * with the interrupt-driven code, maintain the last key
11720Sstevel@tonic-gate 		 * pressed.
11730Sstevel@tonic-gate 		 */
11740Sstevel@tonic-gate 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		/*
11770Sstevel@tonic-gate 		 * This is a total hack to support two additional keys
11780Sstevel@tonic-gate 		 * on Korean keyboards.  They report only on press, and
11790Sstevel@tonic-gate 		 * so we synthesize a release.  Most likely this will
11800Sstevel@tonic-gate 		 * never be important to polled  I/O, but if I do it
11810Sstevel@tonic-gate 		 * "right" the first time it _won't_ be an issue.
11820Sstevel@tonic-gate 		 */
11830Sstevel@tonic-gate 		if (synthetic_release_needed) {
11840Sstevel@tonic-gate 			kb8042->polled_synthetic_release_pending = B_TRUE;
11850Sstevel@tonic-gate 			kb8042->polled_synthetic_release_key = *key;
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 		if (kb8042->simulated_kbd_type == KB_USB) {
11890Sstevel@tonic-gate 			*key = keytab_pc2usb[*key];
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 		return (B_TRUE);
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate static void
1196822Ssethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1197822Ssethg {
1198822Ssethg 	kb8042->leds.desired = led_state;
1199822Ssethg 
1200822Ssethg 	if (!polled)
1201822Ssethg 		mutex_enter(&kb8042->w_hw_mutex);
1202822Ssethg 
1203822Ssethg 	kb8042_start_state_machine(kb8042, polled);
1204822Ssethg 
1205822Ssethg 	if (!polled)
1206822Ssethg 		mutex_exit(&kb8042->w_hw_mutex);
1207822Ssethg }
1208822Ssethg 
1209822Ssethg static void
12100Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
12110Sstevel@tonic-gate {
12120Sstevel@tonic-gate 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1213822Ssethg 	kb8042_setled(kb8042, led_state, B_TRUE);
1214822Ssethg }
12150Sstevel@tonic-gate 
1216822Ssethg static void
1217822Ssethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1218822Ssethg {
1219822Ssethg 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
1220822Ssethg 	kb8042_setled(kb8042, led_state, B_FALSE);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate static void
12240Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	if (polled) {
12270Sstevel@tonic-gate 		ddi_put8(kb8042->handle,
12280Sstevel@tonic-gate 		    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
12290Sstevel@tonic-gate 	} else {
12300Sstevel@tonic-gate 		ddi_put8(kb8042->handle,
12310Sstevel@tonic-gate 		    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate #if	defined(KD_DEBUG)
12350Sstevel@tonic-gate 	if (kb8042_low_level_debug)
12360Sstevel@tonic-gate 		prom_printf(" >K:%x ", byte);
12370Sstevel@tonic-gate #endif
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * Wait until the keyboard is fully up, maybe.
12420Sstevel@tonic-gate  * We may be the first person to talk to the keyboard, in which case
12430Sstevel@tonic-gate  * it's patiently waiting to say "AA" to us to tell us it's up.
12440Sstevel@tonic-gate  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
12450Sstevel@tonic-gate  * good bet that we've already spent that long getting to that point,
12460Sstevel@tonic-gate  * so we'll only wait long enough for the communications electronics to
12470Sstevel@tonic-gate  * run.
12480Sstevel@tonic-gate  */
12490Sstevel@tonic-gate static void
12500Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	int cnt;
12530Sstevel@tonic-gate 	int ready;
12540Sstevel@tonic-gate 	unsigned char byt;
12550Sstevel@tonic-gate 
1256822Ssethg 	/* wait for up to 250 ms for a response */
12570Sstevel@tonic-gate 	for (cnt = 0; cnt < 250; cnt++) {
12580Sstevel@tonic-gate 		ready = ddi_get8(kb8042->handle,
12590Sstevel@tonic-gate 			kb8042->addr + I8042_INT_INPUT_AVAIL);
12600Sstevel@tonic-gate 		if (ready != 0)
12610Sstevel@tonic-gate 			break;
12620Sstevel@tonic-gate 		drv_usecwait(1000);
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 * If there's something pending, read and discard it.  If not,
12670Sstevel@tonic-gate 	 * assume things are OK anyway - maybe somebody else ate it
12680Sstevel@tonic-gate 	 * already.  (On a PC, the BIOS almost certainly did.)
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	if (ready != 0) {
12710Sstevel@tonic-gate 		byt = ddi_get8(kb8042->handle,
12720Sstevel@tonic-gate 			kb8042->addr + I8042_INT_INPUT_DATA);
12730Sstevel@tonic-gate #if	defined(KD_DEBUG)
12740Sstevel@tonic-gate 		if (kb8042_low_level_debug)
12750Sstevel@tonic-gate 			prom_printf(" <K:%x ", byt);
12760Sstevel@tonic-gate #endif
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate static void
12810Sstevel@tonic-gate kb8042_start_state_machine(struct kb8042 *kb8042, boolean_t polled)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	if (kb8042->command_state == KB_COMMAND_STATE_IDLE) {
12840Sstevel@tonic-gate 		if (kb8042->leds.desired != kb8042->leds.commanded) {
12850Sstevel@tonic-gate 			kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
12860Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_LED;
12870Sstevel@tonic-gate 		}
12880Sstevel@tonic-gate 	}
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate enum state_return
12920Sstevel@tonic-gate kb8042_state_machine(struct kb8042 *kb8042, int scancode, boolean_t polled)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	switch (kb8042->command_state) {
12950Sstevel@tonic-gate 	case KB_COMMAND_STATE_IDLE:
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	case KB_COMMAND_STATE_LED:
12990Sstevel@tonic-gate 		if (scancode == KB_ACK) {
13000Sstevel@tonic-gate 			kb8042_send_to_keyboard(kb8042,
13010Sstevel@tonic-gate 				kb8042_xlate_leds(kb8042->leds.desired),
13020Sstevel@tonic-gate 				polled);
13030Sstevel@tonic-gate 			kb8042->leds.commanded = kb8042->leds.desired;
13040Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_WAIT;
13050Sstevel@tonic-gate 			return (STATE_INTERNAL);
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 		/* Drop normal scan codes through. */
13080Sstevel@tonic-gate 		break;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	case KB_COMMAND_STATE_WAIT:
13110Sstevel@tonic-gate 		if (scancode == KB_ACK) {
13120Sstevel@tonic-gate 			kb8042->command_state = KB_COMMAND_STATE_IDLE;
13130Sstevel@tonic-gate 			kb8042_start_state_machine(kb8042, polled);
13140Sstevel@tonic-gate 			return (STATE_INTERNAL);
13150Sstevel@tonic-gate 		}
13160Sstevel@tonic-gate 		/* Drop normal scan codes through. */
13170Sstevel@tonic-gate 		break;
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate 	return (STATE_NORMAL);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate static int
13230Sstevel@tonic-gate kb8042_xlate_leds(int led)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate 	int res;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	res = 0;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if (led & LED_NUM_LOCK)
13300Sstevel@tonic-gate 		res |= LED_NUM;
13310Sstevel@tonic-gate 	if (led & LED_SCROLL_LOCK)
13320Sstevel@tonic-gate 		res |= LED_SCR;
13330Sstevel@tonic-gate 	if (led & LED_CAPS_LOCK)
13340Sstevel@tonic-gate 		res |= LED_CAP;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	return (res);
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate /*ARGSUSED*/
13400Sstevel@tonic-gate static void
13410Sstevel@tonic-gate kb8042_get_initial_leds(
13420Sstevel@tonic-gate     struct kb8042 *kb8042,
13430Sstevel@tonic-gate     int *initial_leds,
13440Sstevel@tonic-gate     int *initial_led_mask)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate #if	defined(i86pc)
13470Sstevel@tonic-gate 	extern caddr_t	p0_va;
13480Sstevel@tonic-gate 	uint8_t		bios_kb_flag;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	bios_kb_flag = p0_va[BIOS_KB_FLAG];
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
13530Sstevel@tonic-gate 	*initial_leds = 0;
13540Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_CAPS_STATE)
13550Sstevel@tonic-gate 		*initial_leds |= LED_CAPS_LOCK;
13560Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_NUM_STATE)
13570Sstevel@tonic-gate 		*initial_leds |= LED_NUM_LOCK;
13580Sstevel@tonic-gate 	if (bios_kb_flag & BIOS_SCROLL_STATE)
13590Sstevel@tonic-gate 		*initial_leds |= LED_SCROLL_LOCK;
13600Sstevel@tonic-gate #else
13610Sstevel@tonic-gate 	*initial_leds = 0;
13620Sstevel@tonic-gate 	*initial_led_mask = 0;
13630Sstevel@tonic-gate #endif
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate #if	defined(KD_DEBUG)
13670Sstevel@tonic-gate static void
13680Sstevel@tonic-gate kb8042_debug_hotkey(int scancode)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	if (!kb8042_enable_debug_hotkey)
13710Sstevel@tonic-gate 		return;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	switch (scancode) {
13740Sstevel@tonic-gate 	case 0x44:	/* F10 in Scan Set 1 code.  (Set 2 code is 0x09)  */
13750Sstevel@tonic-gate 		if (!kb8042_debug) {
13760Sstevel@tonic-gate 			prom_printf("\nKeyboard:  normal debugging on\n");
13770Sstevel@tonic-gate 			kb8042_debug = B_TRUE;
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 		break;
13800Sstevel@tonic-gate 	case 0x43:	/* F9 in Scan Set 1 code.  (Set 2 code is 0x01) */
13810Sstevel@tonic-gate 		if (!kb8042_getchar_debug) {
13820Sstevel@tonic-gate 			prom_printf("\nKeyboard:  getchar debugging on\n");
13830Sstevel@tonic-gate 			kb8042_getchar_debug = B_TRUE;
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 		break;
13860Sstevel@tonic-gate 	case 0x42:	/* F8 in Scan Set 1 code.  (Set 2 code is 0x0a) */
13870Sstevel@tonic-gate 		if (!kb8042_low_level_debug) {
13880Sstevel@tonic-gate 			prom_printf("\nKeyboard:  low-level debugging on\n");
13890Sstevel@tonic-gate 			kb8042_low_level_debug = B_TRUE;
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		break;
13920Sstevel@tonic-gate 	case 0x41:	/* F7 in Scan Set 1 code.  (Set 2 code is 0x83) */
13930Sstevel@tonic-gate 		if (!kb8042_pressrelease_debug) {
13940Sstevel@tonic-gate 			prom_printf(
13950Sstevel@tonic-gate 			    "\nKeyboard:  press/release debugging on\n");
13960Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_TRUE;
13970Sstevel@tonic-gate 		}
13980Sstevel@tonic-gate 		break;
13990Sstevel@tonic-gate 	case 0x3b:	/* F1 in Scan Set 1 code.  (Set 2 code is 0x05) */
14000Sstevel@tonic-gate 		if (kb8042_debug ||
14010Sstevel@tonic-gate 		    kb8042_getchar_debug ||
14020Sstevel@tonic-gate 		    kb8042_low_level_debug ||
14030Sstevel@tonic-gate 		    kb8042_pressrelease_debug) {
14040Sstevel@tonic-gate 			prom_printf("\nKeyboard:  all debugging off\n");
14050Sstevel@tonic-gate 			kb8042_debug = B_FALSE;
14060Sstevel@tonic-gate 			kb8042_getchar_debug = B_FALSE;
14070Sstevel@tonic-gate 			kb8042_low_level_debug = B_FALSE;
14080Sstevel@tonic-gate 			kb8042_pressrelease_debug = B_FALSE;
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		break;
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate #endif
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate static boolean_t
14160Sstevel@tonic-gate kb8042_autorepeat_detect(
14170Sstevel@tonic-gate     struct kb8042 *kb8042,
14180Sstevel@tonic-gate     int key_pos,
14190Sstevel@tonic-gate     enum keystate state)
14200Sstevel@tonic-gate {
14210Sstevel@tonic-gate 	if (state == KEY_RELEASED) {
14220Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos)
14230Sstevel@tonic-gate 			kb8042->kb_old_key_pos = 0;
14240Sstevel@tonic-gate 	} else {
14250Sstevel@tonic-gate 		if (kb8042->kb_old_key_pos == key_pos) {
14260Sstevel@tonic-gate 			return (B_TRUE);
14270Sstevel@tonic-gate 		}
14280Sstevel@tonic-gate 		kb8042->kb_old_key_pos = key_pos;
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 	return (B_FALSE);
14310Sstevel@tonic-gate }
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate /* ARGSUSED */
14340Sstevel@tonic-gate static void
14350Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
14360Sstevel@tonic-gate {
14370Sstevel@tonic-gate 	switch (cmd) {
14380Sstevel@tonic-gate 	case KBD_CMD_BELL:
14390Sstevel@tonic-gate 		beeper_on(BEEP_TYPE4);
14400Sstevel@tonic-gate 		break;
14410Sstevel@tonic-gate 	case KBD_CMD_NOBELL:
14420Sstevel@tonic-gate 		beeper_off();
14430Sstevel@tonic-gate 		break;
14440Sstevel@tonic-gate 	}
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate /*
14490Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
14500Sstevel@tonic-gate  */
14510Sstevel@tonic-gate static int
1452*1762Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg)
14530Sstevel@tonic-gate {
14540Sstevel@tonic-gate 	struct kb8042	*kb8042;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	return (kbtrans_getchar(kb8042->hw_kbtrans));
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate /*
14620Sstevel@tonic-gate  * This is a pass-thru routine to get a character at poll time.
14630Sstevel@tonic-gate  */
14640Sstevel@tonic-gate static int
1465*1762Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg)
14660Sstevel@tonic-gate {
14670Sstevel@tonic-gate 	struct kb8042	*kb8042;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	kb8042 = (struct kb8042 *)arg;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	return (kbtrans_ischar(kb8042->hw_kbtrans));
14720Sstevel@tonic-gate }
1473