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