xref: /illumos-gate/usr/src/uts/common/io/kb8042/kb8042.c (revision 6451fdbca2f79129a3a09d2fe3f6dd4d062bebff)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/inline.h>
36 #include <sys/termio.h>
37 #include <sys/stropts.h>
38 #include <sys/termios.h>
39 #include <sys/stream.h>
40 #include <sys/strtty.h>
41 #include <sys/strsubr.h>
42 #include <sys/strsun.h>
43 #include <sys/ddi.h>
44 #include <sys/sunddi.h>
45 #include <sys/note.h>
46 #include "sys/consdev.h"
47 #include <sys/kbd.h>
48 #include <sys/kbtrans.h>
49 #include "kb8042.h"
50 
51 #include <sys/i8042.h>
52 
53 #include "sys/kbio.h"		/* for KIOCSLAYOUT */
54 #include "sys/stat.h"
55 #include "sys/reboot.h"
56 #include <sys/promif.h>
57 #include <sys/beep.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 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
66 /*  0 */	0,	53,	30,	31,	32,	33,	34,	35,
67 /*  8 */	36,	37,	38,	39,	45,	46,	137,	42,
68 /* 16 */	43,	20,	26,	8,	21,	23,	28,	24,
69 /* 24 */	12,	18,	19,	47,	48,	49,	57,	4,
70 /* 32 */	22,	7,	9,	10,	11,	13,	14,	15,
71 /* 40 */	51,	52,	50,	40,	225,	100,	29,	27,
72 /* 48 */	6,	25,	5,	17,	16,	54,	55,	56,
73 /* 56 */	135,	229,	224,	227,	226,	44,	230,	231,
74 /* 64 */	228,	101,	0,	0,	0,	0,	0,	0,
75 /* 72 */	0,	0,	0,	73,	76,	0,	0,	80,
76 /* 80 */	74,	77,	0,	82,	81,	75,	78,	0,
77 /* 88 */	0,	79,	83,	95,	92,	89,	0,	84,
78 /* 96 */	96,	93,	90,	98,	85,	97,	94,	91,
79 /* 104 */	99,	86,	87,	133,	88,	0,	41,	0,
80 /* 112 */	58,	59,	60,	61,	62,	63,	64,	65,
81 /* 120 */	66,	67,	68,	69,	70,	71,	72,	0,
82 /* 128 */	0,	0,	0,	139,	138,	136,	0,	0,
83 /* 136 */	0,	0,	0,	0,	0,	0,	0,	0,
84 /* 144 */	0,	0,	0,	0,	0,	0,	0,	0,
85 /* 152 */	0,	0,	0,	0,	0,	0,	0,	0,
86 /* 160 */	0,	0,	0,	0,	0,	0,	0,	0,
87 /* 168 */	0,	0,	0,	0,	0,	0,	0,	0,
88 /* 176 */	0,	0,	0,	0,	0,	0,	0,	0,
89 /* 184 */	0,	0,	0,	0,	0,	0,	0,	0,
90 /* 192 */	0,	0,	0,	0,	0,	0,	0,	0,
91 /* 200 */	0,	0,	0,	0,	0,	0,	0,	0,
92 /* 208 */	0,	0,	0,	0,	0,	0,	0,	0,
93 /* 216 */	0,	0,	0,	0,	0,	0,	0,	0,
94 /* 224 */	0,	0,	0,	0,	0,	0,	0,	0,
95 /* 232 */	0,	0,	0,	0,	0,	0,	0,	0,
96 /* 240 */	0,	0,	0,	0,	0,	0,	0,	0,
97 /* 248 */	0,	0,	0,	0
98 };
99 
100 /*
101  * DEBUG (or KD_DEBUG for just this module) turns on a flag called
102  * kb8042_enable_debug_hotkey.  If kb8042_enable_debug_hotkey is true,
103  * then the following hotkeys are enabled:
104  *    F10 - turn on debugging "normal" translations
105  *    F9  - turn on debugging "getchar" translations
106  *    F8  - turn on "low level" debugging
107  *    F7  - turn on terse press/release debugging
108  *    F1  - turn off all debugging
109  * The default value for kb8042_enable_debug_hotkey is false, disabling
110  * these hotkeys.
111  */
112 
113 #if	defined(DEBUG) || defined(lint)
114 #define	KD_DEBUG
115 #endif
116 
117 #ifdef	KD_DEBUG
118 boolean_t	kb8042_enable_debug_hotkey = B_FALSE;
119 boolean_t	kb8042_debug = B_FALSE;
120 boolean_t	kb8042_getchar_debug = B_FALSE;
121 boolean_t	kb8042_low_level_debug = B_FALSE;
122 boolean_t	kb8042_pressrelease_debug = B_FALSE;
123 static void kb8042_debug_hotkey(int scancode);
124 #endif
125 
126 enum state_return { STATE_NORMAL, STATE_INTERNAL };
127 
128 static void kb8042_init(struct kb8042 *kb8042);
129 static uint_t kb8042_intr(caddr_t arg);
130 static void kb8042_wait_poweron(struct kb8042 *kb8042);
131 static void kb8042_start_state_machine(struct kb8042 *, boolean_t);
132 static enum state_return kb8042_state_machine(struct kb8042 *, int, boolean_t);
133 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
134 static int kb8042_xlate_leds(int);
135 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
136 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
137 static boolean_t kb8042_polled_keycheck(
138 			struct kbtrans_hardware *hw, int *key,
139 			enum keystate *state);
140 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
141 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
142 			enum keystate state);
143 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
144 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
145 static void kb8042_iocdatamsg(queue_t *, mblk_t *);
146 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
147 static int kb8042_polled_ischar(struct cons_polledio_arg *arg);
148 static int kb8042_polled_getchar(struct cons_polledio_arg *arg);
149 
150 static struct kbtrans_callbacks kb8042_callbacks = {
151 	kb8042_streams_setled,
152 	kb8042_polled_setled,
153 	kb8042_polled_keycheck,
154 };
155 
156 extern struct keyboard keyindex_pc;
157 
158 #define	DRIVER_NAME(dip) ddi_driver_name(dip)
159 
160 static	char	module_name[] = "kb8042";
161 
162 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
163 			cred_t *credp);
164 static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
165 static int kb8042_wsrv();
166 
167 struct module_info kb8042_sinfo = {
168 	42,		/* Module ID */
169 	module_name,
170 	0, 32,		/* Minimum and maximum packet sizes */
171 	256, 128	/* High and low water marks */
172 };
173 
174 static struct qinit kb8042_rinit = {
175 	NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
176 };
177 
178 static struct qinit kb8042_winit = {
179 	putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
180 };
181 
182 struct streamtab
183 	kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
184 
185 struct kb8042	Kdws = {0};
186 static dev_info_t *kb8042_dip;
187 
188 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
189 		void **result);
190 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
191 
192 static 	struct cb_ops cb_kb8042_ops = {
193 	nulldev,		/* cb_open */
194 	nulldev,		/* cb_close */
195 	nodev,			/* cb_strategy */
196 	nodev,			/* cb_print */
197 	nodev,			/* cb_dump */
198 	nodev,			/* cb_read */
199 	nodev,			/* cb_write */
200 	nodev,			/* cb_ioctl */
201 	nodev,			/* cb_devmap */
202 	nodev,			/* cb_mmap */
203 	nodev,			/* cb_segmap */
204 	nochpoll,		/* cb_chpoll */
205 	ddi_prop_op,		/* cb_prop_op */
206 	&kb8042_str_info,	/* cb_stream */
207 	D_MP
208 };
209 
210 struct dev_ops kb8042_ops = {
211 	DEVO_REV,		/* devo_rev */
212 	0,			/* devo_refcnt */
213 	kb8042_getinfo,		/* devo_getinfo */
214 	nulldev,		/* devo_identify */
215 	nulldev,		/* devo_probe */
216 	kb8042_attach,		/* devo_attach */
217 	nodev,			/* devo_detach */
218 	nodev,			/* devo_reset */
219 	&cb_kb8042_ops,		/* devo_cb_ops */
220 	(struct bus_ops *)NULL	/* devo_bus_ops */
221 };
222 
223 
224 /*
225  * This is the loadable module wrapper.
226  */
227 #include <sys/modctl.h>
228 
229 /*
230  * Module linkage information for the kernel.
231  */
232 static struct modldrv modldrv = {
233 	&mod_driverops, /* Type of module.  This one is a driver */
234 	"PS/2 Keyboard %I%, %E%",
235 	&kb8042_ops,	/* driver ops */
236 };
237 
238 static struct modlinkage modlinkage = {
239 	MODREV_1,
240 	(void *) &modldrv,
241 	NULL
242 };
243 
244 int
245 _init(void)
246 {
247 	int	rv;
248 
249 	rv = mod_install(&modlinkage);
250 	return (rv);
251 }
252 
253 
254 int
255 _fini(void)
256 {
257 	return (mod_remove(&modlinkage));
258 }
259 
260 int
261 _info(struct modinfo *modinfop)
262 {
263 	return (mod_info(&modlinkage, modinfop));
264 }
265 
266 static int
267 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
268 {
269 	int	rc;
270 
271 	struct kb8042	*kb8042;
272 	static ddi_device_acc_attr_t attr = {
273 		DDI_DEVICE_ATTR_V0,
274 		DDI_NEVERSWAP_ACC,
275 		DDI_STRICTORDER_ACC,
276 	};
277 
278 	if (cmd != DDI_ATTACH)
279 		return (DDI_FAILURE);
280 
281 	kb8042 = &Kdws;
282 
283 	KeyboardConvertScan_init(kb8042);
284 
285 	kb8042->debugger.mod1 = 58;	/* Left Ctrl */
286 	kb8042->debugger.mod2 = 60;	/* Left Alt */
287 	kb8042->debugger.trigger = 33;	/* D */
288 	kb8042->debugger.mod1_down = B_FALSE;
289 	kb8042->debugger.mod2_down = B_FALSE;
290 	kb8042->debugger.enabled = B_FALSE;
291 
292 	kb8042->polled_synthetic_release_pending = B_FALSE;
293 
294 	if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
295 		    DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
296 		ddi_remove_minor_node(devi, NULL);
297 		return (-1);
298 	}
299 	kb8042_dip = devi;
300 
301 	rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
302 		(offset_t)0, (offset_t)0, &attr, &kb8042->handle);
303 	if (rc != DDI_SUCCESS) {
304 #if	defined(KD_DEBUG)
305 		cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
306 #endif
307 		return (rc);
308 	}
309 
310 	if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
311 		DDI_SUCCESS) {
312 		cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
313 		return (DDI_FAILURE);
314 	}
315 
316 	mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
317 
318 	kb8042_init(kb8042);
319 
320 	/*
321 	 * Turn on interrupts...
322 	 */
323 	if (ddi_add_intr(devi, 0,
324 		&kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
325 		kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
326 		cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
327 		return (DDI_FAILURE);
328 	}
329 
330 	ddi_report_dev(devi);
331 #ifdef	KD_DEBUG
332 	cmn_err(CE_CONT, "?%s #%d: version %s\n",
333 	    DRIVER_NAME(devi), ddi_get_instance(devi), "%I% (%E%)");
334 #endif
335 	return (DDI_SUCCESS);
336 }
337 
338 /*ARGSUSED*/
339 static int
340 kb8042_getinfo(
341     dev_info_t *dip,
342     ddi_info_cmd_t infocmd,
343     void *arg,
344     void **result)
345 {
346 	register int error;
347 
348 	switch (infocmd) {
349 	case DDI_INFO_DEVT2DEVINFO:
350 		if (kb8042_dip == NULL) {
351 			error = DDI_FAILURE;
352 		} else {
353 			*result = (void *) kb8042_dip;
354 			error = DDI_SUCCESS;
355 		}
356 		break;
357 	case DDI_INFO_DEVT2INSTANCE:
358 		*result = (void *)0;
359 		error = DDI_SUCCESS;
360 		break;
361 	default:
362 		error = DDI_FAILURE;
363 		break;
364 	}
365 	return (error);
366 }
367 
368 static void
369 kb8042_init(struct kb8042 *kb8042)
370 {
371 	if (kb8042->w_init)
372 		return;
373 
374 	kb8042->w_kblayout = 0;	/* Default to US */
375 
376 	kb8042->w_qp = (queue_t *)NULL;
377 
378 	kb8042_wait_poweron(kb8042);
379 
380 	kb8042->kb_old_key_pos = 0;
381 
382 	kb8042->simulated_kbd_type = KB_PC;
383 
384 	/* Set up the command state machine and start it running. */
385 	kb8042->leds.commanded = -1;	/* Unknown initial state */
386 	kb8042->leds.desired = -1;	/* Unknown initial state */
387 	kb8042->command_state = KB_COMMAND_STATE_WAIT;
388 	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE);
389 
390 	kb8042->w_init++;
391 
392 	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
393 }
394 
395 /*ARGSUSED2*/
396 static int
397 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
398 {
399 	struct kb8042	*kb8042;
400 	int err;
401 	int initial_leds;
402 	int initial_led_mask;
403 
404 	kb8042 = &Kdws;
405 
406 	kb8042->w_dev = *devp;
407 
408 	if (qp->q_ptr) {
409 		return (0);
410 	}
411 	qp->q_ptr = (caddr_t)kb8042;
412 	WR(qp)->q_ptr = qp->q_ptr;
413 	if (!kb8042->w_qp)
414 		kb8042->w_qp = qp;
415 
416 	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
417 	err = kbtrans_streams_init(qp, sflag, credp,
418 		(struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
419 		&kb8042->hw_kbtrans,
420 		initial_leds, initial_led_mask);
421 	if (err != 0)
422 		return (err);
423 
424 	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
425 
426 	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
427 	kb8042->polledio.cons_polledio_argument =
428 		(struct cons_polledio_arg *)kb8042;
429 	kb8042->polledio.cons_polledio_putchar = NULL;
430 	kb8042->polledio.cons_polledio_getchar =
431 		(int (*)(struct cons_polledio_arg *))kb8042_polled_getchar;
432 	kb8042->polledio.cons_polledio_ischar =
433 		(boolean_t (*)(struct cons_polledio_arg *))kb8042_polled_ischar;
434 	kb8042->polledio.cons_polledio_enter = NULL;
435 	kb8042->polledio.cons_polledio_exit = NULL;
436 	kb8042->polledio.cons_polledio_setled =
437 		(void (*)(struct cons_polledio_arg *, int))kb8042_polled_setled;
438 	kb8042->polledio.cons_polledio_keycheck =
439 		(boolean_t (*)(struct cons_polledio_arg *, int *,
440 		enum keystate *))kb8042_polled_keycheck;
441 
442 	qprocson(qp);
443 
444 	kbtrans_streams_enable(kb8042->hw_kbtrans);
445 
446 	return (0);
447 }
448 
449 /*ARGSUSED1*/
450 static int
451 kb8042_close(queue_t *qp, int flag, cred_t *credp)
452 {
453 	struct kb8042	*kb8042;
454 
455 	kb8042 = (struct kb8042 *)qp->q_ptr;
456 
457 	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);
458 
459 	kb8042->w_qp = (queue_t *)NULL;
460 	qprocsoff(qp);
461 
462 	return (0);
463 }
464 
465 static int
466 kb8042_wsrv(queue_t *qp)
467 {
468 	struct kb8042 *kb8042;
469 
470 	mblk_t	*mp;
471 
472 	kb8042 = (struct kb8042 *)qp->q_ptr;
473 
474 	while ((mp = getq(qp))) {
475 		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
476 		case KBTRANS_MESSAGE_HANDLED:
477 			continue;
478 		case KBTRANS_MESSAGE_NOT_HANDLED:
479 			break;
480 		}
481 		switch (mp->b_datap->db_type) {
482 		case M_IOCTL:
483 			kb8042_ioctlmsg(kb8042, qp, mp);
484 			continue;
485 		case M_IOCDATA:
486 			kb8042_iocdatamsg(qp, mp);
487 			continue;
488 		case M_DELAY:
489 		case M_STARTI:
490 		case M_STOPI:
491 		case M_READ:	/* ignore, no buffered data */
492 			freemsg(mp);
493 			continue;
494 		case M_FLUSH:
495 			*mp->b_rptr &= ~FLUSHW;
496 			if (*mp->b_rptr & FLUSHR)
497 				qreply(qp, mp);
498 			else
499 				freemsg(mp);
500 			continue;
501 		default:
502 			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
503 						mp->b_datap->db_type);
504 			freemsg(mp);
505 			continue;
506 		}
507 	}
508 	return (0);
509 }
510 
511 static void
512 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
513 {
514 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
515 
516 	kb8042->leds.desired = led_state;
517 	mutex_enter(&kb8042->w_hw_mutex);
518 
519 	kb8042_start_state_machine(kb8042, B_FALSE);
520 
521 	mutex_exit(&kb8042->w_hw_mutex);
522 }
523 
524 static void
525 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
526 {
527 	struct iocblk	*iocp;
528 	mblk_t		*datap;
529 	int		error;
530 	int		tmp;
531 
532 	iocp = (struct iocblk *)mp->b_rptr;
533 
534 	switch (iocp->ioc_cmd) {
535 
536 	case CONSOPENPOLLEDIO:
537 		error = miocpullup(mp, sizeof (struct cons_polledio *));
538 		if (error != 0) {
539 			miocnak(qp, mp, 0, error);
540 			return;
541 		}
542 
543 		/*
544 		 * We are given an appropriate-sized data block,
545 		 * and return a pointer to our structure in it.
546 		 */
547 		*(struct cons_polledio **)mp->b_cont->b_rptr =
548 		    &kb8042->polledio;
549 		mp->b_datap->db_type = M_IOCACK;
550 		iocp->ioc_error = 0;
551 		qreply(qp, mp);
552 		break;
553 
554 	case CONSCLOSEPOLLEDIO:
555 		miocack(qp, mp, 0, 0);
556 		break;
557 
558 	case CONSSETABORTENABLE:
559 		if (iocp->ioc_count != TRANSPARENT) {
560 			miocnak(qp, mp, 0, EINVAL);
561 			return;
562 		}
563 
564 		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
565 		miocack(qp, mp, 0, 0);
566 		break;
567 
568 	/*
569 	 * Valid only in TR_UNTRANS_MODE mode.
570 	 */
571 	case CONSSETKBDTYPE:
572 		error = miocpullup(mp, sizeof (int));
573 		if (error != 0) {
574 			miocnak(qp, mp, 0, error);
575 			return;
576 		}
577 		tmp =  *(int *)mp->b_cont->b_rptr;
578 		if (tmp != KB_PC && tmp != KB_USB) {
579 			miocnak(qp, mp, 0, EINVAL);
580 			break;
581 		}
582 		kb8042->simulated_kbd_type = tmp;
583 		miocack(qp, mp, 0, 0);
584 		break;
585 
586 	case KIOCLAYOUT:
587 		if (kb8042->w_kblayout == -1) {
588 			miocnak(qp, mp, 0, EINVAL);
589 			return;
590 		}
591 
592 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
593 			miocnak(qp, mp, 0, ENOMEM);
594 			return;
595 		}
596 
597 		if (kb8042->simulated_kbd_type == KB_USB)
598 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
599 		else
600 			*(int *)datap->b_wptr = kb8042->w_kblayout;
601 
602 		datap->b_wptr += sizeof (int);
603 		if (mp->b_cont)
604 			freemsg(mp->b_cont);
605 		mp->b_cont = datap;
606 		iocp->ioc_count = sizeof (int);
607 		mp->b_datap->db_type = M_IOCACK;
608 		iocp->ioc_error = 0;
609 		qreply(qp, mp);
610 		break;
611 
612 	case KIOCSLAYOUT:
613 		if (iocp->ioc_count != TRANSPARENT) {
614 			miocnak(qp, mp, 0, EINVAL);
615 			return;
616 		}
617 
618 		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
619 		miocack(qp, mp, 0, 0);
620 		break;
621 
622 	case KIOCCMD:
623 		error = miocpullup(mp, sizeof (int));
624 		if (error != 0) {
625 			miocnak(qp, mp, 0, error);
626 			return;
627 		}
628 
629 		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
630 		miocack(qp, mp, 0, 0);
631 		break;
632 
633 	default:
634 #ifdef DEBUG1
635 		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
636 #endif
637 		miocnak(qp, mp, 0, EINVAL);
638 		return;
639 	}
640 }
641 
642 /*
643  * Process a byte received from the keyboard
644  */
645 static void
646 kb8042_received_byte(
647 	struct kb8042	*kb8042,
648 	int		scancode)	/* raw scan code */
649 {
650 	boolean_t	legit;		/* is this a legit key pos'n? */
651 	int		key_pos = -1;
652 	enum keystate	state;
653 	boolean_t	synthetic_release_needed;
654 
655 #ifdef	KD_DEBUG
656 	kb8042_debug_hotkey(scancode);
657 #endif
658 
659 	if (!kb8042->w_init)	/* can't do anything anyway */
660 		return;
661 
662 	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
663 		&synthetic_release_needed);
664 
665 	if (legit == 0) {
666 		/* Eaten by translation */
667 #ifdef	KD_DEBUG
668 		if (kb8042_debug)
669 			prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode);
670 #endif
671 		return;
672 	}
673 
674 #ifdef	KD_DEBUG
675 	if (kb8042_debug) {
676 		prom_printf("kb8042_intr:  0x%x -> %s %d",
677 			scancode,
678 			state == KEY_RELEASED ? "released" : "pressed",
679 			key_pos);
680 	}
681 #endif
682 
683 	/*
684 	 * Don't know if we want this permanently, but it seems interesting
685 	 * for the moment.
686 	 */
687 	if (key_pos == kb8042->debugger.mod1) {
688 #ifdef	KD_DEBUG
689 		if (kb8042_debug)
690 			prom_printf(" -> debug mod1");
691 #endif
692 		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
693 	}
694 	if (key_pos == kb8042->debugger.mod2) {
695 #ifdef	KD_DEBUG
696 		if (kb8042_debug)
697 			prom_printf(" -> debug mod2");
698 #endif
699 		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
700 	}
701 	if (kb8042->debugger.enabled &&
702 	    key_pos == kb8042->debugger.trigger &&
703 	    kb8042->debugger.mod1_down &&
704 	    kb8042->debugger.mod2_down) {
705 #ifdef	KD_DEBUG
706 		if (kb8042_debug)
707 			prom_printf(" -> debugger\n");
708 #endif
709 		/*
710 		 * Require new presses of the modifiers.
711 		 */
712 		kb8042->debugger.mod1_down = B_FALSE;
713 		kb8042->debugger.mod2_down = B_FALSE;
714 		abort_sequence_enter(NULL);
715 		return;
716 	}
717 
718 	/*
719 	 * If there's no queue above us - as can happen if we've been
720 	 * attached but not opened - drop the keystroke.
721 	 * Note that we do this here instead of above so that
722 	 * Ctrl-Alt-D still works.
723 	 */
724 	if (kb8042->w_qp == NULL) {
725 #ifdef	KD_DEBUG
726 		if (kb8042_debug)
727 			prom_printf(" -> nobody home\n");
728 #endif
729 		return;
730 	}
731 
732 	/*
733 	 * This is to filter out auto repeat since it can't be
734 	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
735 	 * can.  Don't know whether they've taken over the world.
736 	 * Don't think so.)
737 	 */
738 	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
739 #ifdef	KD_DEBUG
740 		if (kb8042_debug)
741 			prom_printf(" -> autorepeat ignored\n");
742 #endif
743 		return;
744 	}
745 
746 #ifdef	KD_DEBUG
747 	if (kb8042_debug)
748 		prom_printf(" -> OK\n");
749 #endif
750 
751 #if	defined(KD_DEBUG)
752 	if (kb8042_pressrelease_debug) {
753 		prom_printf(" %s%d ",
754 			state == KEY_PRESSED ? "+" : "-",
755 			key_pos);
756 	}
757 #endif
758 
759 		kb8042_process_key(kb8042, key_pos, state);
760 
761 	/*
762 	 * This is a total hack.  For some stupid reason, the two additional
763 	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
764 	 * only.  We synthesize a release immediately.
765 	 */
766 	if (synthetic_release_needed) {
767 #if	defined(KD_DEBUG)
768 		if (kb8042_debug)
769 			prom_printf("synthetic release %d\n", key_pos);
770 		if (kb8042_pressrelease_debug)
771 			prom_printf(" -%d(s) ", key_pos);
772 #endif
773 		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
774 		kb8042_process_key(kb8042, key_pos, state);
775 	}
776 }
777 
778 
779 static void
780 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
781     enum keystate state)
782 {
783 	kbtrans_key_t key;
784 
785 	ASSERT(key_pos >= 0 && key_pos <= 255);
786 	if (kb8042->simulated_kbd_type == KB_PC) {
787 		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
788 	} else if (kb8042->simulated_kbd_type == KB_USB) {
789 		key = keytab_pc2usb[key_pos];
790 		if (key != 0) {
791 			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
792 		}
793 	}
794 }
795 
796 /*
797  * Called from interrupt handler when keyboard interrupt occurs.
798  */
799 static uint_t
800 kb8042_intr(caddr_t arg)
801 {
802 	uchar_t scancode;	/* raw scan code */
803 	int rc;
804 	struct kb8042 *kb8042 = (struct kb8042 *)arg;
805 
806 	rc = DDI_INTR_UNCLAIMED;
807 
808 	/* don't care if drv_setparm succeeds */
809 	(void) drv_setparm(SYSRINT, 1);
810 
811 	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
812 	    != 0) {
813 		rc = DDI_INTR_CLAIMED;
814 
815 		scancode = ddi_get8(kb8042->handle,
816 			kb8042->addr + I8042_INT_INPUT_DATA);
817 
818 #if	defined(KD_DEBUG)
819 		if (kb8042_low_level_debug)
820 			prom_printf(" <K:%x ", scancode);
821 #endif
822 
823 		mutex_enter(&kb8042->w_hw_mutex);
824 
825 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
826 		    STATE_NORMAL) {
827 			mutex_exit(&kb8042->w_hw_mutex);
828 			continue;
829 		}
830 
831 
832 		mutex_exit(&kb8042->w_hw_mutex);
833 
834 		kb8042_received_byte(kb8042, scancode);
835 	}
836 
837 	return (rc);
838 }
839 
840 static void
841 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
842 {
843 	struct copyresp	*csp;
844 
845 	csp = (struct copyresp *)mp->b_rptr;
846 	if (csp->cp_rval) {
847 		freemsg(mp);
848 		return;
849 	}
850 
851 	switch (csp->cp_cmd) {
852 	default:
853 		miocack(qp, mp, 0, 0);
854 		break;
855 	}
856 }
857 
858 static boolean_t
859 kb8042_polled_keycheck(
860     struct kbtrans_hardware *hw,
861     int *key,
862     enum keystate *state)
863 {
864 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
865 	int	scancode;
866 	boolean_t	legit;
867 	boolean_t	synthetic_release_needed;
868 
869 	if (kb8042->polled_synthetic_release_pending) {
870 		*key = kb8042->polled_synthetic_release_key;
871 		*state = KEY_RELEASED;
872 		kb8042->polled_synthetic_release_pending = B_FALSE;
873 #if	defined(KD_DEBUG)
874 		if (kb8042_getchar_debug)
875 			prom_printf("synthetic release 0x%x\n", *key);
876 #endif
877 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
878 		return (B_TRUE);
879 	}
880 
881 	for (;;) {
882 		if (ddi_get8(kb8042->handle,
883 		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
884 			return (B_FALSE);
885 		}
886 
887 		scancode = ddi_get8(kb8042->handle,
888 				kb8042->addr + I8042_POLL_INPUT_DATA);
889 
890 #if	defined(KD_DEBUG)
891 		if (kb8042_low_level_debug)
892 			prom_printf(" g<%x ", scancode);
893 #endif
894 
895 		if (kb8042_state_machine(kb8042, scancode, B_FALSE) !=
896 		    STATE_NORMAL) {
897 			continue;
898 		}
899 
900 #ifdef	KD_DEBUG
901 		kb8042_debug_hotkey(scancode);
902 		if (kb8042_getchar_debug)
903 			prom_printf("polled 0x%x", scancode);
904 #endif
905 
906 		legit = KeyboardConvertScan(kb8042, scancode, key, state,
907 			&synthetic_release_needed);
908 		if (!legit) {
909 #ifdef	KD_DEBUG
910 			if (kb8042_getchar_debug)
911 				prom_printf(" -> ignored\n");
912 #endif
913 			continue;
914 		}
915 #ifdef	KD_DEBUG
916 		if (kb8042_getchar_debug) {
917 			prom_printf(" -> %s %d\n",
918 				*state == KEY_PRESSED ? "pressed" : "released",
919 				*key);
920 		}
921 #endif
922 		/*
923 		 * For the moment at least, we rely on hardware autorepeat
924 		 * for polled I/O autorepeat.  However, for coordination
925 		 * with the interrupt-driven code, maintain the last key
926 		 * pressed.
927 		 */
928 		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
929 
930 		/*
931 		 * This is a total hack to support two additional keys
932 		 * on Korean keyboards.  They report only on press, and
933 		 * so we synthesize a release.  Most likely this will
934 		 * never be important to polled  I/O, but if I do it
935 		 * "right" the first time it _won't_ be an issue.
936 		 */
937 		if (synthetic_release_needed) {
938 			kb8042->polled_synthetic_release_pending = B_TRUE;
939 			kb8042->polled_synthetic_release_key = *key;
940 		}
941 
942 		if (kb8042->simulated_kbd_type == KB_USB) {
943 			*key = keytab_pc2usb[*key];
944 		}
945 		return (B_TRUE);
946 	}
947 }
948 
949 static void
950 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
951 {
952 	struct kb8042 *kb8042 = (struct kb8042 *)hw;
953 
954 	kb8042->leds.desired = led_state;
955 
956 	kb8042_start_state_machine(kb8042, B_TRUE);
957 }
958 
959 static void
960 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
961 {
962 	if (polled) {
963 		ddi_put8(kb8042->handle,
964 		    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
965 	} else {
966 		ddi_put8(kb8042->handle,
967 		    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
968 	}
969 
970 #if	defined(KD_DEBUG)
971 	if (kb8042_low_level_debug)
972 		prom_printf(" >K:%x ", byte);
973 #endif
974 }
975 
976 /*
977  * Wait until the keyboard is fully up, maybe.
978  * We may be the first person to talk to the keyboard, in which case
979  * it's patiently waiting to say "AA" to us to tell us it's up.
980  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
981  * good bet that we've already spent that long getting to that point,
982  * so we'll only wait long enough for the communications electronics to
983  * run.
984  */
985 static void
986 kb8042_wait_poweron(struct kb8042 *kb8042)
987 {
988 	int cnt;
989 	int ready;
990 	unsigned char byt;
991 
992 	/* wait for up to about a quarter-second for response */
993 	for (cnt = 0; cnt < 250; cnt++) {
994 		ready = ddi_get8(kb8042->handle,
995 			kb8042->addr + I8042_INT_INPUT_AVAIL);
996 		if (ready != 0)
997 			break;
998 		drv_usecwait(1000);
999 	}
1000 
1001 	/*
1002 	 * If there's something pending, read and discard it.  If not,
1003 	 * assume things are OK anyway - maybe somebody else ate it
1004 	 * already.  (On a PC, the BIOS almost certainly did.)
1005 	 */
1006 	if (ready != 0) {
1007 		byt = ddi_get8(kb8042->handle,
1008 			kb8042->addr + I8042_INT_INPUT_DATA);
1009 #if	defined(KD_DEBUG)
1010 		if (kb8042_low_level_debug)
1011 			prom_printf(" <K:%x ", byt);
1012 #endif
1013 	}
1014 }
1015 
1016 static void
1017 kb8042_start_state_machine(struct kb8042 *kb8042, boolean_t polled)
1018 {
1019 	if (kb8042->command_state == KB_COMMAND_STATE_IDLE) {
1020 		if (kb8042->leds.desired != kb8042->leds.commanded) {
1021 			kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1022 			kb8042->command_state = KB_COMMAND_STATE_LED;
1023 		}
1024 	}
1025 }
1026 
1027 enum state_return
1028 kb8042_state_machine(struct kb8042 *kb8042, int scancode, boolean_t polled)
1029 {
1030 	switch (kb8042->command_state) {
1031 	case KB_COMMAND_STATE_IDLE:
1032 		break;
1033 
1034 	case KB_COMMAND_STATE_LED:
1035 		if (scancode == KB_ACK) {
1036 			kb8042_send_to_keyboard(kb8042,
1037 				kb8042_xlate_leds(kb8042->leds.desired),
1038 				polled);
1039 			kb8042->leds.commanded = kb8042->leds.desired;
1040 			kb8042->command_state = KB_COMMAND_STATE_WAIT;
1041 			return (STATE_INTERNAL);
1042 		}
1043 		/* Drop normal scan codes through. */
1044 		break;
1045 
1046 	case KB_COMMAND_STATE_WAIT:
1047 		if (scancode == KB_ACK) {
1048 			kb8042->command_state = KB_COMMAND_STATE_IDLE;
1049 			kb8042_start_state_machine(kb8042, polled);
1050 			return (STATE_INTERNAL);
1051 		}
1052 		/* Drop normal scan codes through. */
1053 		break;
1054 	}
1055 	return (STATE_NORMAL);
1056 }
1057 
1058 static int
1059 kb8042_xlate_leds(int led)
1060 {
1061 	int res;
1062 
1063 	res = 0;
1064 
1065 	if (led & LED_NUM_LOCK)
1066 		res |= LED_NUM;
1067 	if (led & LED_SCROLL_LOCK)
1068 		res |= LED_SCR;
1069 	if (led & LED_CAPS_LOCK)
1070 		res |= LED_CAP;
1071 
1072 	return (res);
1073 }
1074 
1075 /*ARGSUSED*/
1076 static void
1077 kb8042_get_initial_leds(
1078     struct kb8042 *kb8042,
1079     int *initial_leds,
1080     int *initial_led_mask)
1081 {
1082 #if	defined(i86pc)
1083 	extern caddr_t	p0_va;
1084 	uint8_t		bios_kb_flag;
1085 
1086 	bios_kb_flag = p0_va[BIOS_KB_FLAG];
1087 
1088 	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1089 	*initial_leds = 0;
1090 	if (bios_kb_flag & BIOS_CAPS_STATE)
1091 		*initial_leds |= LED_CAPS_LOCK;
1092 	if (bios_kb_flag & BIOS_NUM_STATE)
1093 		*initial_leds |= LED_NUM_LOCK;
1094 	if (bios_kb_flag & BIOS_SCROLL_STATE)
1095 		*initial_leds |= LED_SCROLL_LOCK;
1096 #else
1097 	*initial_leds = 0;
1098 	*initial_led_mask = 0;
1099 #endif
1100 }
1101 
1102 #if	defined(KD_DEBUG)
1103 static void
1104 kb8042_debug_hotkey(int scancode)
1105 {
1106 	if (!kb8042_enable_debug_hotkey)
1107 		return;
1108 
1109 	switch (scancode) {
1110 	case 0x44:	/* F10 in Scan Set 1 code.  (Set 2 code is 0x09)  */
1111 		if (!kb8042_debug) {
1112 			prom_printf("\nKeyboard:  normal debugging on\n");
1113 			kb8042_debug = B_TRUE;
1114 		}
1115 		break;
1116 	case 0x43:	/* F9 in Scan Set 1 code.  (Set 2 code is 0x01) */
1117 		if (!kb8042_getchar_debug) {
1118 			prom_printf("\nKeyboard:  getchar debugging on\n");
1119 			kb8042_getchar_debug = B_TRUE;
1120 		}
1121 		break;
1122 	case 0x42:	/* F8 in Scan Set 1 code.  (Set 2 code is 0x0a) */
1123 		if (!kb8042_low_level_debug) {
1124 			prom_printf("\nKeyboard:  low-level debugging on\n");
1125 			kb8042_low_level_debug = B_TRUE;
1126 		}
1127 		break;
1128 	case 0x41:	/* F7 in Scan Set 1 code.  (Set 2 code is 0x83) */
1129 		if (!kb8042_pressrelease_debug) {
1130 			prom_printf(
1131 			    "\nKeyboard:  press/release debugging on\n");
1132 			kb8042_pressrelease_debug = B_TRUE;
1133 		}
1134 		break;
1135 	case 0x3b:	/* F1 in Scan Set 1 code.  (Set 2 code is 0x05) */
1136 		if (kb8042_debug ||
1137 		    kb8042_getchar_debug ||
1138 		    kb8042_low_level_debug ||
1139 		    kb8042_pressrelease_debug) {
1140 			prom_printf("\nKeyboard:  all debugging off\n");
1141 			kb8042_debug = B_FALSE;
1142 			kb8042_getchar_debug = B_FALSE;
1143 			kb8042_low_level_debug = B_FALSE;
1144 			kb8042_pressrelease_debug = B_FALSE;
1145 		}
1146 		break;
1147 	}
1148 }
1149 #endif
1150 
1151 static boolean_t
1152 kb8042_autorepeat_detect(
1153     struct kb8042 *kb8042,
1154     int key_pos,
1155     enum keystate state)
1156 {
1157 	if (state == KEY_RELEASED) {
1158 		if (kb8042->kb_old_key_pos == key_pos)
1159 			kb8042->kb_old_key_pos = 0;
1160 	} else {
1161 		if (kb8042->kb_old_key_pos == key_pos) {
1162 			return (B_TRUE);
1163 		}
1164 		kb8042->kb_old_key_pos = key_pos;
1165 	}
1166 	return (B_FALSE);
1167 }
1168 
1169 /* ARGSUSED */
1170 static void
1171 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1172 {
1173 	switch (cmd) {
1174 	case KBD_CMD_BELL:
1175 		beeper_on(BEEP_TYPE4);
1176 		break;
1177 	case KBD_CMD_NOBELL:
1178 		beeper_off();
1179 		break;
1180 	}
1181 }
1182 
1183 
1184 /*
1185  * This is a pass-thru routine to get a character at poll time.
1186  */
1187 static int
1188 kb8042_polled_getchar(struct cons_polledio_arg *arg)
1189 {
1190 	struct kb8042	*kb8042;
1191 
1192 	kb8042 = (struct kb8042 *)arg;
1193 
1194 	return (kbtrans_getchar(kb8042->hw_kbtrans));
1195 }
1196 
1197 /*
1198  * This is a pass-thru routine to get a character at poll time.
1199  */
1200 static int
1201 kb8042_polled_ischar(struct cons_polledio_arg *arg)
1202 {
1203 	struct kb8042	*kb8042;
1204 
1205 	kb8042 = (struct kb8042 *)arg;
1206 
1207 	return (kbtrans_ischar(kb8042->hw_kbtrans));
1208 }
1209