preprocessor cleanup: __sparc
[unleashed/tickless.git] / usr / src / uts / common / io / kb8042 / kb8042.c
blobf1a7894785baca3754bdd7ebc309bad6de675e8c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
21 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
23 /* All Rights Reserved */
26 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #define KEYMAP_SIZE_VARIABLE
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"
50 #include <sys/i8042.h>
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 #include <sys/policy.h>
61 * For any keyboard, there is a unique code describing the position
62 * of the key on a keyboard. We refer to the code as "station number".
63 * The following table is used to map the station numbers from ps2
64 * AT/XT keyboards to that of a USB one.
66 * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
67 * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
68 * when in KB_PC mode. Note that this does not need to be made conditional
69 * on the architecture for which this module is complied because there are no
70 * keys that map to K8042_STOP on non-SPARC platforms.
72 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
73 /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35,
74 /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42,
75 /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24,
76 /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4,
77 /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15,
78 /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27,
79 /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56,
80 /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231,
81 /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0,
82 /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80,
83 /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0,
84 /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84,
85 /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91,
86 /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0,
87 /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65,
88 /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0,
89 /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0,
90 /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0,
91 /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0,
92 /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0,
93 /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0,
94 /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0,
95 /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0,
96 /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0,
97 /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0,
98 /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,
99 /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0,
100 /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0,
101 /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0,
102 /* 232 */ 0, 128, 129, 127, 0, 0, 0, 0,
103 /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
104 /* 248 */ 0, 0, 0, 0
108 #define MAX_KB8042_WAIT_MAX_MS 500 /* 500ms total */
109 #define MAX_KB8042_RETRIES 5
111 enum state_return { STATE_NORMAL, STATE_INTERNAL };
113 static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
114 static uint_t kb8042_intr(caddr_t arg);
115 static void kb8042_wait_poweron(struct kb8042 *kb8042);
116 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
117 static int kb8042_xlate_leds(int);
118 static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
119 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
120 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
121 static boolean_t kb8042_polled_keycheck(
122 struct kbtrans_hardware *hw, int *key,
123 enum keystate *state);
124 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
125 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
126 enum keystate state);
127 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
128 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
129 static void kb8042_iocdatamsg(queue_t *, mblk_t *);
130 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
131 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
132 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
133 static void kb8042_cleanup(struct kb8042 *kb8042);
135 static struct kbtrans_callbacks kb8042_callbacks = {
136 kb8042_streams_setled,
137 kb8042_polled_setled,
138 kb8042_polled_keycheck,
141 extern struct keyboard keyindex_pc;
143 #define DRIVER_NAME(dip) ddi_driver_name(dip)
145 static char module_name[] = "kb8042";
147 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
148 cred_t *credp);
149 static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
150 static int kb8042_wsrv();
152 struct module_info kb8042_sinfo = {
153 42, /* Module ID */
154 module_name,
155 0, 32, /* Minimum and maximum packet sizes */
156 256, 128 /* High and low water marks */
159 static struct qinit kb8042_rinit = {
160 NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
163 static struct qinit kb8042_winit = {
164 putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
167 struct streamtab
168 kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
170 struct kb8042 Kdws = {0};
171 static dev_info_t *kb8042_dip = NULL;
173 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
174 void **result);
175 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
176 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
178 static struct cb_ops cb_kb8042_ops = {
179 nulldev, /* cb_open */
180 nulldev, /* cb_close */
181 nodev, /* cb_strategy */
182 nodev, /* cb_print */
183 nodev, /* cb_dump */
184 nodev, /* cb_read */
185 nodev, /* cb_write */
186 nodev, /* cb_ioctl */
187 nodev, /* cb_devmap */
188 nodev, /* cb_mmap */
189 nodev, /* cb_segmap */
190 nochpoll, /* cb_chpoll */
191 ddi_prop_op, /* cb_prop_op */
192 &kb8042_str_info, /* cb_stream */
193 D_MP
196 struct dev_ops kb8042_ops = {
197 DEVO_REV, /* devo_rev */
198 0, /* devo_refcnt */
199 kb8042_getinfo, /* devo_getinfo */
200 nulldev, /* devo_identify */
201 nulldev, /* devo_probe */
202 kb8042_attach, /* devo_attach */
203 kb8042_detach, /* devo_detach */
204 nodev, /* devo_reset */
205 &cb_kb8042_ops, /* devo_cb_ops */
206 NULL, /* devo_bus_ops */
207 NULL, /* devo_power */
208 ddi_quiesce_not_needed, /* devo_quiesce */
213 * This is the loadable module wrapper.
215 #include <sys/modctl.h>
218 * Module linkage information for the kernel.
220 static struct modldrv modldrv = {
221 &mod_driverops, /* Type of module. This one is a driver */
222 "PS/2 keyboard driver",
223 &kb8042_ops, /* driver ops */
226 static struct modlinkage modlinkage = {
227 MODREV_1,
228 (void *) &modldrv,
229 NULL
233 _init(void)
235 int rv;
237 rv = mod_install(&modlinkage);
238 return (rv);
243 _fini(void)
245 return (mod_remove(&modlinkage));
249 _info(struct modinfo *modinfop)
251 return (mod_info(&modlinkage, modinfop));
255 static int
256 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
258 int rc;
259 int scanset;
260 int leds;
262 struct kb8042 *kb8042 = &Kdws;
263 static ddi_device_acc_attr_t attr = {
264 DDI_DEVICE_ATTR_V0,
265 DDI_NEVERSWAP_ACC,
266 DDI_STRICTORDER_ACC,
269 switch (cmd) {
270 case DDI_RESUME:
271 leds = kb8042->leds.commanded;
272 kb8042->w_init = 0;
273 kb8042_init(kb8042, B_TRUE);
274 kb8042_setled(kb8042, leds, B_FALSE);
275 mutex_enter(&kb8042->w_hw_mutex);
276 kb8042->suspended = B_FALSE;
277 if (kb8042->w_qp != NULL) {
278 enableok(WR(kb8042->w_qp));
279 qenable(WR(kb8042->w_qp));
281 cv_broadcast(&kb8042->suspend_cv);
282 mutex_exit(&kb8042->w_hw_mutex);
283 return (DDI_SUCCESS);
285 case DDI_ATTACH:
286 if (kb8042_dip != NULL)
287 return (DDI_FAILURE);
288 /* The rest of the function is for attach */
289 break;
291 default:
292 return (DDI_FAILURE);
295 kb8042->debugger.mod1 = 58; /* Left Ctrl */
296 kb8042->debugger.mod2 = 60; /* Left Alt */
297 kb8042->debugger.trigger = 33; /* D */
298 kb8042->debugger.mod1_down = B_FALSE;
299 kb8042->debugger.mod2_down = B_FALSE;
300 kb8042->debugger.enabled = B_FALSE;
302 kb8042_dip = devi;
303 kb8042->init_state = KB8042_UNINITIALIZED;
305 kb8042->polled_synthetic_release_pending = B_FALSE;
307 if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
308 DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
309 goto failure;
312 kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
314 rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
315 (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
316 if (rc != DDI_SUCCESS) {
317 #ifdef DEBUG
318 cmn_err(CE_WARN, "kb8042_attach: can't map registers");
319 #endif
320 goto failure;
323 kb8042->init_state |= KB8042_REGS_MAPPED;
325 if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
326 DDI_SUCCESS) {
327 cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie");
328 goto failure;
331 mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
332 cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
333 cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
334 cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
335 kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
337 kb8042_init(kb8042, B_FALSE);
339 /* x86 systems use scan code set 1 -- no detection required */
340 scanset = 1;
341 if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
342 cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
343 "Unknown scan code set `%d'.", scanset);
344 /* Scan code set is not supported */
345 goto failure;
349 * Turn on interrupts...
351 if (ddi_add_intr(devi, 0,
352 &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
353 kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
354 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
355 goto failure;
358 kb8042->init_state |= KB8042_INTR_ADDED;
360 ddi_report_dev(devi);
362 #ifdef DEBUG
363 cmn_err(CE_CONT, "?%s instance #%d READY\n",
364 DRIVER_NAME(devi), ddi_get_instance(devi));
365 #endif
367 return (DDI_SUCCESS);
369 failure:
370 kb8042_cleanup(kb8042);
371 return (DDI_FAILURE);
374 static int
375 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
377 struct kb8042 *kb8042 = &Kdws;
379 switch (cmd) {
380 case DDI_SUSPEND:
381 mutex_enter(&kb8042->w_hw_mutex);
382 ASSERT(kb8042->ops >= 0);
383 while (kb8042->ops > 0)
384 cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
385 kb8042->suspended = B_TRUE;
386 mutex_exit(&kb8042->w_hw_mutex);
387 return (DDI_SUCCESS);
389 case DDI_DETACH:
390 /* If someone has a stream open, fail to detach */
391 if (kb8042->w_qp != NULL)
392 return (DDI_FAILURE);
394 ASSERT(kb8042_dip == dip);
396 kb8042_cleanup(kb8042);
398 return (DDI_SUCCESS);
400 default:
401 return (DDI_FAILURE);
405 /*ARGSUSED*/
406 static int
407 kb8042_getinfo(
408 dev_info_t *dip,
409 ddi_info_cmd_t infocmd,
410 void *arg,
411 void **result)
413 register int error;
415 switch (infocmd) {
416 case DDI_INFO_DEVT2DEVINFO:
417 if (kb8042_dip == NULL) {
418 error = DDI_FAILURE;
419 } else {
420 *result = (void *) kb8042_dip;
421 error = DDI_SUCCESS;
423 break;
424 case DDI_INFO_DEVT2INSTANCE:
425 *result = NULL;
426 error = DDI_SUCCESS;
427 break;
428 default:
429 error = DDI_FAILURE;
430 break;
432 return (error);
435 static void
436 kb8042_cleanup(struct kb8042 *kb8042)
438 ASSERT(kb8042_dip != NULL);
440 if (kb8042->init_state & KB8042_INTR_ADDED)
441 ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
443 if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
444 cv_destroy(&kb8042->cmd_cv);
445 cv_destroy(&kb8042->suspend_cv);
446 cv_destroy(&kb8042->ops_cv);
447 mutex_destroy(&kb8042->w_hw_mutex);
450 if (kb8042->init_state & KB8042_REGS_MAPPED)
451 ddi_regs_map_free(&kb8042->handle);
453 if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
454 ddi_remove_minor_node(kb8042_dip, NULL);
456 kb8042->init_state = KB8042_UNINITIALIZED;
457 kb8042_dip = NULL;
460 static void
461 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
463 if (kb8042->w_init)
464 return;
466 if (!from_resume) {
467 kb8042->w_kblayout = 0; /* Default to US */
468 kb8042->w_qp = (queue_t *)NULL;
469 kb8042->simulated_kbd_type = KB_PC;
470 kb8042->leds.commanded = -1; /* Unknown initial state */
471 kb8042->leds.desired = -1; /* Unknown initial state */
474 kb8042_wait_poweron(kb8042);
476 kb8042->kb_old_key_pos = 0;
479 * Explicitly grab and release the 8042 lock outside of
480 * kb8042_send_to_keyboard, because this is the only situation
481 * where a polling interface is used with locking required.
483 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
484 /* Set up the command state machine and start it running. */
485 kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
486 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
488 kb8042->w_init++;
490 (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
493 /*ARGSUSED2*/
494 static int
495 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
497 struct kb8042 *kb8042;
498 int err = 0;
499 int initial_leds;
500 int initial_led_mask;
502 kb8042 = &Kdws;
504 mutex_enter(&kb8042->w_hw_mutex);
505 if (qp->q_ptr) {
506 kb8042->w_dev = *devp;
507 mutex_exit(&kb8042->w_hw_mutex);
508 return (0);
511 if (secpolicy_console(credp) != 0) {
512 mutex_exit(&kb8042->w_hw_mutex);
513 return (EPERM);
516 while (kb8042->suspended) {
517 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
518 0) {
519 mutex_exit(&kb8042->w_hw_mutex);
520 return (EINTR);
524 kb8042->w_dev = *devp;
525 qp->q_ptr = (caddr_t)kb8042;
526 WR(qp)->q_ptr = qp->q_ptr;
527 if (!kb8042->w_qp)
528 kb8042->w_qp = qp;
530 ASSERT(kb8042->ops >= 0);
531 kb8042->ops++;
532 mutex_exit(&kb8042->w_hw_mutex);
534 kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
535 err = kbtrans_streams_init(qp, sflag,
536 (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
537 &kb8042->hw_kbtrans,
538 initial_leds, initial_led_mask);
539 if (err != 0)
540 goto out;
542 kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
544 kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
545 kb8042->polledio.cons_polledio_argument =
546 (cons_polledio_arg_t)kb8042;
547 kb8042->polledio.cons_polledio_putchar = NULL;
548 kb8042->polledio.cons_polledio_getchar =
549 (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
550 kb8042->polledio.cons_polledio_ischar =
551 (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
552 kb8042->polledio.cons_polledio_enter = NULL;
553 kb8042->polledio.cons_polledio_exit = NULL;
554 kb8042->polledio.cons_polledio_setled =
555 (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
556 kb8042->polledio.cons_polledio_keycheck =
557 (boolean_t (*)(cons_polledio_arg_t, int *,
558 enum keystate *))kb8042_polled_keycheck;
560 qprocson(qp);
562 kbtrans_streams_enable(kb8042->hw_kbtrans);
564 out:
565 mutex_enter(&kb8042->w_hw_mutex);
566 ASSERT(kb8042->ops > 0);
567 kb8042->ops--;
568 if (kb8042->ops == 0)
569 cv_broadcast(&kb8042->ops_cv);
570 mutex_exit(&kb8042->w_hw_mutex);
572 return (err);
575 /*ARGSUSED1*/
576 static int
577 kb8042_close(queue_t *qp, int flag, cred_t *credp)
579 struct kb8042 *kb8042;
581 /* If a beep is in progress, stop that */
582 (void) beeper_off();
584 kb8042 = (struct kb8042 *)qp->q_ptr;
586 mutex_enter(&kb8042->w_hw_mutex);
587 while (kb8042->suspended) {
588 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
589 0) {
590 mutex_exit(&kb8042->w_hw_mutex);
591 return (EINTR);
595 ASSERT(kb8042->ops >= 0);
596 kb8042->ops++;
597 mutex_exit(&kb8042->w_hw_mutex);
599 (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
601 kb8042->w_qp = (queue_t *)NULL;
602 qprocsoff(qp);
604 mutex_enter(&kb8042->w_hw_mutex);
605 ASSERT(kb8042->ops > 0);
606 kb8042->ops--;
607 if (kb8042->ops == 0)
608 cv_broadcast(&kb8042->ops_cv);
609 mutex_exit(&kb8042->w_hw_mutex);
611 return (0);
614 static int
615 kb8042_wsrv(queue_t *qp)
617 struct kb8042 *kb8042;
619 mblk_t *mp;
620 boolean_t suspended;
622 kb8042 = (struct kb8042 *)qp->q_ptr;
624 mutex_enter(&kb8042->w_hw_mutex);
625 suspended = kb8042->suspended;
626 ASSERT(kb8042->ops >= 0);
627 if (!suspended)
628 kb8042->ops++;
629 mutex_exit(&kb8042->w_hw_mutex);
631 #ifdef NO_KB_DEBUG
632 while (!suspended && (mp = getq(qp)) != NULL) {
633 #else
635 * Not taking keyboard input while suspending can make debugging
636 * difficult. However, we still do the ops counting so that we
637 * don't suspend at a bad time.
639 while ((mp = getq(qp))) {
640 #endif
641 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
642 case KBTRANS_MESSAGE_HANDLED:
643 continue;
644 case KBTRANS_MESSAGE_NOT_HANDLED:
645 break;
647 switch (mp->b_datap->db_type) {
648 case M_IOCTL:
649 kb8042_ioctlmsg(kb8042, qp, mp);
650 continue;
651 case M_IOCDATA:
652 kb8042_iocdatamsg(qp, mp);
653 continue;
654 case M_DELAY:
655 case M_STARTI:
656 case M_STOPI:
657 case M_READ: /* ignore, no buffered data */
658 freemsg(mp);
659 continue;
660 case M_FLUSH:
661 *mp->b_rptr &= ~FLUSHW;
662 if (*mp->b_rptr & FLUSHR)
663 qreply(qp, mp);
664 else
665 freemsg(mp);
666 continue;
667 default:
668 cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
669 mp->b_datap->db_type);
670 freemsg(mp);
671 continue;
675 mutex_enter(&kb8042->w_hw_mutex);
676 if (!suspended) {
677 ASSERT(kb8042->ops > 0);
678 kb8042->ops--;
679 if (kb8042->ops == 0)
680 cv_broadcast(&kb8042->ops_cv);
682 mutex_exit(&kb8042->w_hw_mutex);
684 return (0);
687 static void
688 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
690 struct iocblk *iocp;
691 mblk_t *datap;
692 int error;
693 int tmp;
694 int cycles;
695 int frequency;
696 int msecs;
698 iocp = (struct iocblk *)mp->b_rptr;
700 switch (iocp->ioc_cmd) {
702 case CONSOPENPOLLEDIO:
703 error = miocpullup(mp, sizeof (struct cons_polledio *));
704 if (error != 0) {
705 miocnak(qp, mp, 0, error);
706 return;
710 * We are given an appropriate-sized data block,
711 * and return a pointer to our structure in it.
713 *(struct cons_polledio **)mp->b_cont->b_rptr =
714 &kb8042->polledio;
715 mp->b_datap->db_type = M_IOCACK;
716 iocp->ioc_error = 0;
717 qreply(qp, mp);
718 break;
720 case CONSCLOSEPOLLEDIO:
721 miocack(qp, mp, 0, 0);
722 break;
724 case CONSSETABORTENABLE:
725 if (iocp->ioc_count != TRANSPARENT) {
726 miocnak(qp, mp, 0, EINVAL);
727 return;
730 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
731 miocack(qp, mp, 0, 0);
732 break;
735 * Valid only in TR_UNTRANS_MODE mode.
737 case CONSSETKBDTYPE:
738 error = miocpullup(mp, sizeof (int));
739 if (error != 0) {
740 miocnak(qp, mp, 0, error);
741 return;
743 tmp = *(int *)mp->b_cont->b_rptr;
744 if (tmp != KB_PC && tmp != KB_USB) {
745 miocnak(qp, mp, 0, EINVAL);
746 break;
748 kb8042->simulated_kbd_type = tmp;
749 miocack(qp, mp, 0, 0);
750 break;
752 case KIOCLAYOUT:
753 if (kb8042->w_kblayout == -1) {
754 miocnak(qp, mp, 0, EINVAL);
755 return;
758 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
759 miocnak(qp, mp, 0, ENOMEM);
760 return;
763 if (kb8042->simulated_kbd_type == KB_USB)
764 *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
765 else
766 *(int *)datap->b_wptr = kb8042->w_kblayout;
768 datap->b_wptr += sizeof (int);
769 if (mp->b_cont)
770 freemsg(mp->b_cont);
771 mp->b_cont = datap;
772 iocp->ioc_count = sizeof (int);
773 mp->b_datap->db_type = M_IOCACK;
774 iocp->ioc_error = 0;
775 qreply(qp, mp);
776 break;
778 case KIOCSLAYOUT:
779 if (iocp->ioc_count != TRANSPARENT) {
780 miocnak(qp, mp, 0, EINVAL);
781 return;
784 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
785 miocack(qp, mp, 0, 0);
786 break;
788 case KIOCCMD:
789 error = miocpullup(mp, sizeof (int));
790 if (error != 0) {
791 miocnak(qp, mp, 0, error);
792 return;
795 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
796 miocack(qp, mp, 0, 0);
797 break;
799 case KIOCMKTONE:
800 if (iocp->ioc_count != TRANSPARENT) {
801 miocnak(qp, mp, 0, EINVAL);
802 return;
805 tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
806 cycles = tmp & 0xffff;
807 msecs = (tmp >> 16) & 0xffff;
809 if (cycles == 0)
810 frequency = UINT16_MAX;
811 else if (cycles == UINT16_MAX)
812 frequency = 0;
813 else {
814 frequency = (PIT_HZ + cycles / 2) / cycles;
815 if (frequency > UINT16_MAX)
816 frequency = UINT16_MAX;
819 error = beep_mktone(frequency, msecs);
820 if (error != 0)
821 miocnak(qp, mp, 0, error);
822 else
823 miocack(qp, mp, 0, 0);
824 break;
826 default:
827 #ifdef DEBUG1
828 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
829 #endif
830 miocnak(qp, mp, 0, EINVAL);
831 return;
836 * Process a byte received from the keyboard
838 static void
839 kb8042_received_byte(
840 struct kb8042 *kb8042,
841 int scancode) /* raw scan code */
843 boolean_t legit; /* is this a legit key pos'n? */
844 int key_pos = -1;
845 enum keystate state;
846 boolean_t synthetic_release_needed;
849 * Intercept ACK and RESEND and signal the condition that
850 * kb8042_send_and_wait is waiting for.
852 if (scancode == KB_ACK) {
853 mutex_enter(&kb8042->w_hw_mutex);
854 kb8042->acked = 1;
855 cv_signal(&kb8042->cmd_cv);
856 mutex_exit(&kb8042->w_hw_mutex);
857 return;
858 } else if (scancode == KB_RESEND) {
859 mutex_enter(&kb8042->w_hw_mutex);
860 kb8042->need_retry = 1;
861 cv_signal(&kb8042->cmd_cv);
862 mutex_exit(&kb8042->w_hw_mutex);
863 return;
866 if (!kb8042->w_init) /* can't do anything anyway */
867 return;
869 legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
870 &synthetic_release_needed);
872 if (legit == 0) {
873 /* Eaten by translation */
874 return;
878 * Don't know if we want this permanently, but it seems interesting
879 * for the moment.
881 if (key_pos == kb8042->debugger.mod1) {
882 kb8042->debugger.mod1_down = (state == KEY_PRESSED);
884 if (key_pos == kb8042->debugger.mod2) {
885 kb8042->debugger.mod2_down = (state == KEY_PRESSED);
887 if (kb8042->debugger.enabled &&
888 key_pos == kb8042->debugger.trigger &&
889 kb8042->debugger.mod1_down &&
890 kb8042->debugger.mod2_down) {
892 * Require new presses of the modifiers.
894 kb8042->debugger.mod1_down = B_FALSE;
895 kb8042->debugger.mod2_down = B_FALSE;
896 abort_sequence_enter(NULL);
897 return;
901 * If there's no queue above us - as can happen if we've been
902 * attached but not opened - drop the keystroke.
903 * Note that we do this here instead of above so that
904 * Ctrl-Alt-D still works.
906 if (kb8042->w_qp == NULL) {
907 return;
911 * This is to filter out auto repeat since it can't be
912 * turned off at the hardware. (Yeah, yeah, PS/2 keyboards
913 * can. Don't know whether they've taken over the world.
914 * Don't think so.)
916 if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
917 return;
921 kb8042_process_key(kb8042, key_pos, state);
924 * This is a total hack. For some stupid reason, the two additional
925 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
926 * only. We synthesize a release immediately.
928 if (synthetic_release_needed) {
929 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
930 kb8042_process_key(kb8042, key_pos, state);
935 static void
936 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
937 enum keystate state)
939 kbtrans_key_t key;
941 ASSERT(key_pos >= 0 && key_pos <= 255);
942 if (kb8042->simulated_kbd_type == KB_PC) {
943 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
944 } else if (kb8042->simulated_kbd_type == KB_USB) {
945 key = keytab_pc2usb[key_pos];
946 if (key != 0) {
947 kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
953 * Called from interrupt handler when keyboard interrupt occurs.
955 static uint_t
956 kb8042_intr(caddr_t arg)
958 uchar_t scancode; /* raw scan code */
959 int rc;
960 struct kb8042 *kb8042 = (struct kb8042 *)arg;
962 rc = DDI_INTR_UNCLAIMED;
964 if (kb8042->init_state == KB8042_UNINITIALIZED)
965 return (DDI_INTR_UNCLAIMED);
967 /* don't care if drv_setparm succeeds */
968 (void) drv_setparm(SYSRINT, 1);
970 while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
971 != 0) {
972 rc = DDI_INTR_CLAIMED;
974 scancode = ddi_get8(kb8042->handle,
975 kb8042->addr + I8042_INT_INPUT_DATA);
977 kb8042_received_byte(kb8042, scancode);
980 return (rc);
983 static void
984 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
986 struct copyresp *csp;
988 csp = (struct copyresp *)mp->b_rptr;
989 if (csp->cp_rval) {
990 freemsg(mp);
991 return;
994 switch (csp->cp_cmd) {
995 default:
996 miocack(qp, mp, 0, 0);
997 break;
1001 static boolean_t
1002 kb8042_polled_keycheck(
1003 struct kbtrans_hardware *hw,
1004 int *key,
1005 enum keystate *state)
1007 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1008 int scancode;
1009 boolean_t legit;
1010 boolean_t synthetic_release_needed;
1012 if (kb8042->polled_synthetic_release_pending) {
1013 *key = kb8042->polled_synthetic_release_key;
1014 *state = KEY_RELEASED;
1015 kb8042->polled_synthetic_release_pending = B_FALSE;
1016 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1017 return (B_TRUE);
1020 for (;;) {
1021 if (ddi_get8(kb8042->handle,
1022 kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
1023 return (B_FALSE);
1026 scancode = ddi_get8(kb8042->handle,
1027 kb8042->addr + I8042_POLL_INPUT_DATA);
1029 legit = KeyboardConvertScan(kb8042, scancode, key, state,
1030 &synthetic_release_needed);
1031 if (!legit) {
1032 continue;
1035 * For the moment at least, we rely on hardware autorepeat
1036 * for polled I/O autorepeat. However, for coordination
1037 * with the interrupt-driven code, maintain the last key
1038 * pressed.
1040 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1043 * This is a total hack to support two additional keys
1044 * on Korean keyboards. They report only on press, and
1045 * so we synthesize a release. Most likely this will
1046 * never be important to polled I/O, but if I do it
1047 * "right" the first time it _won't_ be an issue.
1049 if (synthetic_release_needed) {
1050 kb8042->polled_synthetic_release_pending = B_TRUE;
1051 kb8042->polled_synthetic_release_key = *key;
1054 if (kb8042->simulated_kbd_type == KB_USB) {
1055 *key = keytab_pc2usb[*key];
1057 return (B_TRUE);
1061 static void
1062 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1064 kb8042->leds.desired = led_state;
1066 if (!polled)
1067 mutex_enter(&kb8042->w_hw_mutex);
1069 if (kb8042->leds.desired != kb8042->leds.commanded) {
1070 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1073 if (!polled)
1074 mutex_exit(&kb8042->w_hw_mutex);
1077 static void
1078 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
1080 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1081 kb8042_setled(kb8042, led_state, B_TRUE);
1084 static void
1085 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1087 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1088 kb8042_setled(kb8042, led_state, B_FALSE);
1092 static int
1093 kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
1095 uint8_t *outp = kb8042->addr +
1096 (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
1097 uint8_t *inavp = kb8042->addr +
1098 (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
1099 uint8_t *inp = kb8042->addr +
1100 (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
1101 uint8_t b;
1102 int ms_waited;
1103 int timedout;
1104 int expire;
1105 int retries = 0;
1107 do {
1108 kb8042->acked = 0;
1109 kb8042->need_retry = 0;
1110 ms_waited = 0; /* Zero it whether polled or not */
1111 timedout = 0;
1113 ddi_put8(kb8042->handle, outp, u8);
1115 while (!kb8042->acked && !kb8042->need_retry && !timedout) {
1117 if (polled) {
1118 if (ddi_get8(kb8042->handle, inavp)) {
1119 b = ddi_get8(kb8042->handle, inp);
1120 switch (b) {
1121 case KB_ACK:
1122 kb8042->acked = 1;
1123 break;
1124 case KB_RESEND:
1125 kb8042->need_retry = 1;
1126 break;
1127 default:
1129 * drop it: We should never
1130 * get scancodes while
1131 * we're in the middle of a
1132 * command anyway.
1134 #ifdef DEBUG
1135 cmn_err(CE_WARN, "!Unexpected "
1136 " byte 0x%x", b);
1137 #endif
1138 break;
1143 * Wait 1ms if an ACK wasn't received yet
1145 if (!kb8042->acked) {
1146 drv_usecwait(1000);
1147 ms_waited++;
1148 if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
1149 timedout = B_TRUE;
1151 } else {
1152 /* Interrupt-driven */
1153 expire = ddi_get_lbolt() +
1154 drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
1157 * If cv_timedwait returned -1 and we neither
1158 * received an ACK nor a RETRY response, then
1159 * we timed out.
1161 if (cv_timedwait(&kb8042->cmd_cv,
1162 &kb8042->w_hw_mutex, expire) == -1 &&
1163 !kb8042->acked && !kb8042->need_retry) {
1164 timedout = B_TRUE;
1169 } while ((kb8042->need_retry || timedout) &&
1170 ++retries < MAX_KB8042_RETRIES);
1172 return (kb8042->acked);
1176 * kb8042_send_to_keyboard should be called with w_hw_mutex held if
1177 * polled is FALSE.
1179 static void
1180 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
1184 * KB_SET_LED and KB_ENABLE are special commands which require blocking
1185 * other 8042 consumers while executing.
1187 * Other commands/data are sent using the single put8 I/O access
1188 * function.
1190 if (byte == KB_SET_LED) {
1192 if (!polled) {
1193 (void) ddi_get8(kb8042->handle, kb8042->addr +
1194 I8042_LOCK);
1197 if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
1199 * Ignore return value, as there's nothing we can
1200 * do about it if the SET LED command fails.
1202 (void) kb8042_send_and_wait(kb8042,
1203 kb8042_xlate_leds(kb8042->leds.desired), polled);
1206 if (!polled) {
1207 (void) ddi_get8(kb8042->handle, kb8042->addr +
1208 I8042_UNLOCK);
1210 kb8042->leds.commanded = kb8042->leds.desired;
1212 } else if (byte == KB_ENABLE) {
1214 if (!polled) {
1215 (void) ddi_get8(kb8042->handle, kb8042->addr +
1216 I8042_LOCK);
1219 (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
1221 if (!polled) {
1222 (void) ddi_get8(kb8042->handle, kb8042->addr +
1223 I8042_UNLOCK);
1226 } else {
1227 /* All other commands use the "normal" virtual output port */
1228 if (polled) {
1229 ddi_put8(kb8042->handle,
1230 kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
1231 } else {
1232 ddi_put8(kb8042->handle,
1233 kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
1239 * Wait until the keyboard is fully up, maybe.
1240 * We may be the first person to talk to the keyboard, in which case
1241 * it's patiently waiting to say "AA" to us to tell us it's up.
1242 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
1243 * good bet that we've already spent that long getting to that point,
1244 * so we'll only wait long enough for the communications electronics to
1245 * run.
1247 static void
1248 kb8042_wait_poweron(struct kb8042 *kb8042)
1250 int cnt;
1251 int ready;
1253 /* wait for up to 250 ms for a response */
1254 for (cnt = 0; cnt < 250; cnt++) {
1255 ready = ddi_get8(kb8042->handle,
1256 kb8042->addr + I8042_INT_INPUT_AVAIL);
1257 if (ready != 0)
1258 break;
1259 drv_usecwait(1000);
1263 * If there's something pending, read and discard it. If not,
1264 * assume things are OK anyway - maybe somebody else ate it
1265 * already. (On a PC, the BIOS almost certainly did.)
1267 if (ready != 0) {
1268 (void) ddi_get8(kb8042->handle,
1269 kb8042->addr + I8042_INT_INPUT_DATA);
1273 static int
1274 kb8042_xlate_leds(int led)
1276 int res;
1278 res = 0;
1280 if (led & LED_NUM_LOCK)
1281 res |= LED_NUM;
1282 if (led & LED_SCROLL_LOCK)
1283 res |= LED_SCR;
1284 if (led & LED_CAPS_LOCK)
1285 res |= LED_CAP;
1287 return (res);
1290 /*ARGSUSED*/
1291 static void
1292 kb8042_get_initial_leds(
1293 struct kb8042 *kb8042,
1294 int *initial_leds,
1295 int *initial_led_mask)
1297 #if defined(__i386) || defined(__amd64)
1298 extern caddr_t p0_va;
1299 uint8_t bios_kb_flag;
1301 bios_kb_flag = p0_va[BIOS_KB_FLAG];
1303 *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1304 *initial_leds = 0;
1305 if (bios_kb_flag & BIOS_CAPS_STATE)
1306 *initial_leds |= LED_CAPS_LOCK;
1307 if (bios_kb_flag & BIOS_NUM_STATE)
1308 *initial_leds |= LED_NUM_LOCK;
1309 if (bios_kb_flag & BIOS_SCROLL_STATE)
1310 *initial_leds |= LED_SCROLL_LOCK;
1311 #else
1312 *initial_leds = 0;
1313 *initial_led_mask = 0;
1314 #endif
1317 static boolean_t
1318 kb8042_autorepeat_detect(
1319 struct kb8042 *kb8042,
1320 int key_pos,
1321 enum keystate state)
1323 if (state == KEY_RELEASED) {
1324 if (kb8042->kb_old_key_pos == key_pos)
1325 kb8042->kb_old_key_pos = 0;
1326 } else {
1327 if (kb8042->kb_old_key_pos == key_pos) {
1328 return (B_TRUE);
1330 kb8042->kb_old_key_pos = key_pos;
1332 return (B_FALSE);
1335 /* ARGSUSED */
1336 static void
1337 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1339 switch (cmd) {
1340 case KBD_CMD_BELL:
1341 (void) beeper_on(BEEP_TYPE4);
1342 break;
1343 case KBD_CMD_NOBELL:
1344 (void) beeper_off();
1345 break;
1351 * This is a pass-thru routine to get a character at poll time.
1353 static int
1354 kb8042_polled_getchar(cons_polledio_arg_t arg)
1356 struct kb8042 *kb8042;
1358 kb8042 = (struct kb8042 *)arg;
1360 return (kbtrans_getchar(kb8042->hw_kbtrans));
1364 * This is a pass-thru routine to get a character at poll time.
1366 static int
1367 kb8042_polled_ischar(cons_polledio_arg_t arg)
1369 struct kb8042 *kb8042;
1371 kb8042 = (struct kb8042 *)arg;
1373 return (kbtrans_ischar(kb8042->hw_kbtrans));