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