Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / kb8042 / kb8042.c
blobc221be713817cf743d35080b7ad6f3d3dadeb036
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 .mi_idnum = 42, /* Module ID */
154 .mi_idname = module_name, /* Module name */
155 .mi_minpsz = 0, /* Minimum packet size */
156 .mi_maxpsz = 32, /* Maximum packet size */
157 .mi_hiwat = 256, /* High water mark */
158 .mi_lowat = 128 /* Low water mark */
161 static struct qinit kb8042_rinit = {
162 .qi_putp = NULL,
163 .qi_srvp = NULL,
164 .qi_qopen = kb8042_open,
165 .qi_qclose = kb8042_close,
166 .qi_qadmin = NULL,
167 .qi_minfo = &kb8042_sinfo,
168 .qi_mstat = NULL,
169 .qi_rwp = NULL,
170 .qi_infop = NULL,
171 .qi_struiot = 0
174 static struct qinit kb8042_winit = {
175 .qi_putp = putq,
176 .qi_srvp = kb8042_wsrv,
177 .qi_qopen = kb8042_open,
178 .qi_qclose = kb8042_close,
179 .qi_qadmin = NULL,
180 .qi_minfo = &kb8042_sinfo,
181 .qi_mstat = NULL,
182 .qi_rwp = NULL,
183 .qi_infop = NULL,
184 .qi_struiot = 0
187 struct streamtab kb8042_str_info = {
188 .st_rdinit = &kb8042_rinit,
189 .st_wrinit = &kb8042_winit,
190 .st_muxrinit = NULL,
191 .st_muxwinit = NULL
194 struct kb8042 Kdws = {0};
195 static dev_info_t *kb8042_dip = NULL;
197 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
198 void **result);
199 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
200 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
202 static struct cb_ops cb_kb8042_ops = {
203 .cb_open = nulldev,
204 .cb_close = nulldev,
205 .cb_strategy = nodev,
206 .cb_print = nodev,
207 .cb_dump = nodev,
208 .cb_read = nodev,
209 .cb_write = nodev,
210 .cb_ioctl = nodev,
211 .cb_devmap = nodev,
212 .cb_mmap = nodev,
213 .cb_segmap = nodev,
214 .cb_chpoll = nochpoll,
215 .cb_prop_op = ddi_prop_op,
216 .cb_str = &kb8042_str_info,
217 .cb_flag = D_MP,
218 .cb_rev = CB_REV,
219 .cb_aread = nodev,
220 .cb_awrite = nodev
223 struct dev_ops kb8042_ops = {
224 .devo_rev = DEVO_REV,
225 .devo_refcnt = 0,
226 .devo_getinfo = kb8042_getinfo,
227 .devo_identify = nulldev,
228 .devo_probe = nulldev,
229 .devo_attach = kb8042_attach,
230 .devo_detach = kb8042_detach,
231 .devo_reset = nodev,
232 .devo_cb_ops = &cb_kb8042_ops,
233 .devo_bus_ops = NULL,
234 .devo_power = NULL,
235 .devo_quiesce = ddi_quiesce_not_needed
240 * This is the loadable module wrapper.
242 #include <sys/modctl.h>
245 * Module linkage information for the kernel.
247 static struct modldrv modldrv = {
248 .drv_modops = &mod_driverops, /* Type of module. */
249 .drv_linkinfo = "PS/2 keyboard driver",
250 .drv_dev_ops = &kb8042_ops, /* driver ops */
253 static struct modlinkage modlinkage = {
254 .ml_rev = MODREV_1,
255 .ml_linkage = { &modldrv, NULL }
259 _init(void)
261 int rv;
263 rv = mod_install(&modlinkage);
264 return (rv);
269 _fini(void)
271 return (mod_remove(&modlinkage));
275 _info(struct modinfo *modinfop)
277 return (mod_info(&modlinkage, modinfop));
281 static int
282 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
284 int rc;
285 int scanset;
286 int leds;
288 struct kb8042 *kb8042 = &Kdws;
289 static ddi_device_acc_attr_t attr = {
290 DDI_DEVICE_ATTR_V0,
291 DDI_NEVERSWAP_ACC,
292 DDI_STRICTORDER_ACC,
295 switch (cmd) {
296 case DDI_RESUME:
297 leds = kb8042->leds.commanded;
298 kb8042->w_init = 0;
299 kb8042_init(kb8042, B_TRUE);
300 kb8042_setled(kb8042, leds, B_FALSE);
301 mutex_enter(&kb8042->w_hw_mutex);
302 kb8042->suspended = B_FALSE;
303 if (kb8042->w_qp != NULL) {
304 enableok(WR(kb8042->w_qp));
305 qenable(WR(kb8042->w_qp));
307 cv_broadcast(&kb8042->suspend_cv);
308 mutex_exit(&kb8042->w_hw_mutex);
309 return (DDI_SUCCESS);
311 case DDI_ATTACH:
312 if (kb8042_dip != NULL)
313 return (DDI_FAILURE);
314 /* The rest of the function is for attach */
315 break;
317 default:
318 return (DDI_FAILURE);
321 kb8042->debugger.mod1 = 58; /* Left Ctrl */
322 kb8042->debugger.mod2 = 60; /* Left Alt */
323 kb8042->debugger.trigger = 33; /* D */
324 kb8042->debugger.mod1_down = B_FALSE;
325 kb8042->debugger.mod2_down = B_FALSE;
326 kb8042->debugger.enabled = B_FALSE;
328 kb8042_dip = devi;
329 kb8042->init_state = KB8042_UNINITIALIZED;
331 kb8042->polled_synthetic_release_pending = B_FALSE;
333 if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
334 DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
335 goto failure;
338 kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
340 rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
341 0, 0, &attr, &kb8042->handle);
342 if (rc != DDI_SUCCESS) {
343 #ifdef DEBUG
344 cmn_err(CE_WARN, "kb8042_attach: can't map registers");
345 #endif
346 goto failure;
349 kb8042->init_state |= KB8042_REGS_MAPPED;
351 if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
352 DDI_SUCCESS) {
353 cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie");
354 goto failure;
357 mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
358 cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
359 cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
360 cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
361 kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
363 kb8042_init(kb8042, B_FALSE);
365 /* x86 systems use scan code set 1 -- no detection required */
366 scanset = 1;
367 if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
368 cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
369 "Unknown scan code set `%d'.", scanset);
370 /* Scan code set is not supported */
371 goto failure;
375 * Turn on interrupts...
377 if (ddi_add_intr(devi, 0,
378 &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
379 kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
380 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
381 goto failure;
384 kb8042->init_state |= KB8042_INTR_ADDED;
386 ddi_report_dev(devi);
388 #ifdef DEBUG
389 cmn_err(CE_CONT, "?%s instance #%d READY\n",
390 DRIVER_NAME(devi), ddi_get_instance(devi));
391 #endif
393 return (DDI_SUCCESS);
395 failure:
396 kb8042_cleanup(kb8042);
397 return (DDI_FAILURE);
400 static int
401 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
403 struct kb8042 *kb8042 = &Kdws;
405 switch (cmd) {
406 case DDI_SUSPEND:
407 mutex_enter(&kb8042->w_hw_mutex);
408 ASSERT(kb8042->ops >= 0);
409 while (kb8042->ops > 0)
410 cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
411 kb8042->suspended = B_TRUE;
412 mutex_exit(&kb8042->w_hw_mutex);
413 return (DDI_SUCCESS);
415 case DDI_DETACH:
416 /* If someone has a stream open, fail to detach */
417 if (kb8042->w_qp != NULL)
418 return (DDI_FAILURE);
420 ASSERT(kb8042_dip == dip);
422 kb8042_cleanup(kb8042);
424 return (DDI_SUCCESS);
426 default:
427 return (DDI_FAILURE);
431 /*ARGSUSED*/
432 static int
433 kb8042_getinfo(
434 dev_info_t *dip,
435 ddi_info_cmd_t infocmd,
436 void *arg,
437 void **result)
439 register int error;
441 switch (infocmd) {
442 case DDI_INFO_DEVT2DEVINFO:
443 if (kb8042_dip == NULL) {
444 error = DDI_FAILURE;
445 } else {
446 *result = (void *) kb8042_dip;
447 error = DDI_SUCCESS;
449 break;
450 case DDI_INFO_DEVT2INSTANCE:
451 *result = NULL;
452 error = DDI_SUCCESS;
453 break;
454 default:
455 error = DDI_FAILURE;
456 break;
458 return (error);
461 static void
462 kb8042_cleanup(struct kb8042 *kb8042)
464 ASSERT(kb8042_dip != NULL);
466 if (kb8042->init_state & KB8042_INTR_ADDED)
467 ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
469 if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
470 cv_destroy(&kb8042->cmd_cv);
471 cv_destroy(&kb8042->suspend_cv);
472 cv_destroy(&kb8042->ops_cv);
473 mutex_destroy(&kb8042->w_hw_mutex);
476 if (kb8042->init_state & KB8042_REGS_MAPPED)
477 ddi_regs_map_free(&kb8042->handle);
479 if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
480 ddi_remove_minor_node(kb8042_dip, NULL);
482 kb8042->init_state = KB8042_UNINITIALIZED;
483 kb8042_dip = NULL;
486 static void
487 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
489 if (kb8042->w_init)
490 return;
492 if (!from_resume) {
493 kb8042->w_kblayout = 0; /* Default to US */
494 kb8042->w_qp = (queue_t *)NULL;
495 kb8042->simulated_kbd_type = KB_PC;
496 kb8042->leds.commanded = -1; /* Unknown initial state */
497 kb8042->leds.desired = -1; /* Unknown initial state */
500 kb8042_wait_poweron(kb8042);
502 kb8042->kb_old_key_pos = 0;
505 * Explicitly grab and release the 8042 lock outside of
506 * kb8042_send_to_keyboard, because this is the only situation
507 * where a polling interface is used with locking required.
509 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
510 /* Set up the command state machine and start it running. */
511 kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
512 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
514 kb8042->w_init++;
516 (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
519 /*ARGSUSED2*/
520 static int
521 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
523 struct kb8042 *kb8042;
524 int err = 0;
525 int initial_leds;
526 int initial_led_mask;
528 kb8042 = &Kdws;
530 mutex_enter(&kb8042->w_hw_mutex);
531 if (qp->q_ptr) {
532 kb8042->w_dev = *devp;
533 mutex_exit(&kb8042->w_hw_mutex);
534 return (0);
537 if (secpolicy_console(credp) != 0) {
538 mutex_exit(&kb8042->w_hw_mutex);
539 return (EPERM);
542 while (kb8042->suspended) {
543 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
544 0) {
545 mutex_exit(&kb8042->w_hw_mutex);
546 return (EINTR);
550 kb8042->w_dev = *devp;
551 qp->q_ptr = (caddr_t)kb8042;
552 WR(qp)->q_ptr = qp->q_ptr;
553 if (!kb8042->w_qp)
554 kb8042->w_qp = qp;
556 ASSERT(kb8042->ops >= 0);
557 kb8042->ops++;
558 mutex_exit(&kb8042->w_hw_mutex);
560 kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
561 err = kbtrans_streams_init(qp, sflag,
562 (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
563 &kb8042->hw_kbtrans,
564 initial_leds, initial_led_mask);
565 if (err != 0)
566 goto out;
568 kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
570 kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
571 kb8042->polledio.cons_polledio_argument =
572 (cons_polledio_arg_t)kb8042;
573 kb8042->polledio.cons_polledio_putchar = NULL;
574 kb8042->polledio.cons_polledio_getchar =
575 (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
576 kb8042->polledio.cons_polledio_ischar =
577 (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
578 kb8042->polledio.cons_polledio_enter = NULL;
579 kb8042->polledio.cons_polledio_exit = NULL;
580 kb8042->polledio.cons_polledio_setled =
581 (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
582 kb8042->polledio.cons_polledio_keycheck =
583 (boolean_t (*)(cons_polledio_arg_t, int *,
584 enum keystate *))kb8042_polled_keycheck;
586 qprocson(qp);
588 kbtrans_streams_enable(kb8042->hw_kbtrans);
590 out:
591 mutex_enter(&kb8042->w_hw_mutex);
592 ASSERT(kb8042->ops > 0);
593 kb8042->ops--;
594 if (kb8042->ops == 0)
595 cv_broadcast(&kb8042->ops_cv);
596 mutex_exit(&kb8042->w_hw_mutex);
598 return (err);
601 /*ARGSUSED1*/
602 static int
603 kb8042_close(queue_t *qp, int flag, cred_t *credp)
605 struct kb8042 *kb8042;
607 /* If a beep is in progress, stop that */
608 (void) beeper_off();
610 kb8042 = (struct kb8042 *)qp->q_ptr;
612 mutex_enter(&kb8042->w_hw_mutex);
613 while (kb8042->suspended) {
614 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
615 0) {
616 mutex_exit(&kb8042->w_hw_mutex);
617 return (EINTR);
621 ASSERT(kb8042->ops >= 0);
622 kb8042->ops++;
623 mutex_exit(&kb8042->w_hw_mutex);
625 (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
627 kb8042->w_qp = (queue_t *)NULL;
628 qprocsoff(qp);
630 mutex_enter(&kb8042->w_hw_mutex);
631 ASSERT(kb8042->ops > 0);
632 kb8042->ops--;
633 if (kb8042->ops == 0)
634 cv_broadcast(&kb8042->ops_cv);
635 mutex_exit(&kb8042->w_hw_mutex);
637 return (0);
640 static int
641 kb8042_wsrv(queue_t *qp)
643 struct kb8042 *kb8042;
645 mblk_t *mp;
646 boolean_t suspended;
648 kb8042 = (struct kb8042 *)qp->q_ptr;
650 mutex_enter(&kb8042->w_hw_mutex);
651 suspended = kb8042->suspended;
652 ASSERT(kb8042->ops >= 0);
653 if (!suspended)
654 kb8042->ops++;
655 mutex_exit(&kb8042->w_hw_mutex);
657 #ifdef NO_KB_DEBUG
658 while (!suspended && (mp = getq(qp)) != NULL) {
659 #else
661 * Not taking keyboard input while suspending can make debugging
662 * difficult. However, we still do the ops counting so that we
663 * don't suspend at a bad time.
665 while ((mp = getq(qp))) {
666 #endif
667 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
668 case KBTRANS_MESSAGE_HANDLED:
669 continue;
670 case KBTRANS_MESSAGE_NOT_HANDLED:
671 break;
673 switch (mp->b_datap->db_type) {
674 case M_IOCTL:
675 kb8042_ioctlmsg(kb8042, qp, mp);
676 continue;
677 case M_IOCDATA:
678 kb8042_iocdatamsg(qp, mp);
679 continue;
680 case M_DELAY:
681 case M_STARTI:
682 case M_STOPI:
683 case M_READ: /* ignore, no buffered data */
684 freemsg(mp);
685 continue;
686 case M_FLUSH:
687 *mp->b_rptr &= ~FLUSHW;
688 if (*mp->b_rptr & FLUSHR)
689 qreply(qp, mp);
690 else
691 freemsg(mp);
692 continue;
693 default:
694 cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
695 mp->b_datap->db_type);
696 freemsg(mp);
697 continue;
701 mutex_enter(&kb8042->w_hw_mutex);
702 if (!suspended) {
703 ASSERT(kb8042->ops > 0);
704 kb8042->ops--;
705 if (kb8042->ops == 0)
706 cv_broadcast(&kb8042->ops_cv);
708 mutex_exit(&kb8042->w_hw_mutex);
710 return (0);
713 static void
714 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
716 struct iocblk *iocp;
717 mblk_t *datap;
718 int error;
719 int tmp;
720 int cycles;
721 int frequency;
722 int msecs;
724 iocp = (struct iocblk *)mp->b_rptr;
726 switch (iocp->ioc_cmd) {
728 case CONSOPENPOLLEDIO:
729 error = miocpullup(mp, sizeof (struct cons_polledio *));
730 if (error != 0) {
731 miocnak(qp, mp, 0, error);
732 return;
736 * We are given an appropriate-sized data block,
737 * and return a pointer to our structure in it.
739 *(struct cons_polledio **)mp->b_cont->b_rptr =
740 &kb8042->polledio;
741 mp->b_datap->db_type = M_IOCACK;
742 iocp->ioc_error = 0;
743 qreply(qp, mp);
744 break;
746 case CONSCLOSEPOLLEDIO:
747 miocack(qp, mp, 0, 0);
748 break;
750 case CONSSETABORTENABLE:
751 if (iocp->ioc_count != TRANSPARENT) {
752 miocnak(qp, mp, 0, EINVAL);
753 return;
756 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
757 miocack(qp, mp, 0, 0);
758 break;
761 * Valid only in TR_UNTRANS_MODE mode.
763 case CONSSETKBDTYPE:
764 error = miocpullup(mp, sizeof (int));
765 if (error != 0) {
766 miocnak(qp, mp, 0, error);
767 return;
769 tmp = *(int *)mp->b_cont->b_rptr;
770 if (tmp != KB_PC && tmp != KB_USB) {
771 miocnak(qp, mp, 0, EINVAL);
772 break;
774 kb8042->simulated_kbd_type = tmp;
775 miocack(qp, mp, 0, 0);
776 break;
778 case KIOCLAYOUT:
779 if (kb8042->w_kblayout == -1) {
780 miocnak(qp, mp, 0, EINVAL);
781 return;
784 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
785 miocnak(qp, mp, 0, ENOMEM);
786 return;
789 if (kb8042->simulated_kbd_type == KB_USB)
790 *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
791 else
792 *(int *)datap->b_wptr = kb8042->w_kblayout;
794 datap->b_wptr += sizeof (int);
795 if (mp->b_cont)
796 freemsg(mp->b_cont);
797 mp->b_cont = datap;
798 iocp->ioc_count = sizeof (int);
799 mp->b_datap->db_type = M_IOCACK;
800 iocp->ioc_error = 0;
801 qreply(qp, mp);
802 break;
804 case KIOCSLAYOUT:
805 if (iocp->ioc_count != TRANSPARENT) {
806 miocnak(qp, mp, 0, EINVAL);
807 return;
810 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
811 miocack(qp, mp, 0, 0);
812 break;
814 case KIOCCMD:
815 error = miocpullup(mp, sizeof (int));
816 if (error != 0) {
817 miocnak(qp, mp, 0, error);
818 return;
821 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
822 miocack(qp, mp, 0, 0);
823 break;
825 case KIOCMKTONE:
826 if (iocp->ioc_count != TRANSPARENT) {
827 miocnak(qp, mp, 0, EINVAL);
828 return;
831 tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
832 cycles = tmp & 0xffff;
833 msecs = (tmp >> 16) & 0xffff;
835 if (cycles == 0)
836 frequency = UINT16_MAX;
837 else if (cycles == UINT16_MAX)
838 frequency = 0;
839 else {
840 frequency = (PIT_HZ + cycles / 2) / cycles;
841 if (frequency > UINT16_MAX)
842 frequency = UINT16_MAX;
845 error = beep_mktone(frequency, msecs);
846 if (error != 0)
847 miocnak(qp, mp, 0, error);
848 else
849 miocack(qp, mp, 0, 0);
850 break;
852 default:
853 #ifdef DEBUG1
854 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
855 #endif
856 miocnak(qp, mp, 0, EINVAL);
857 return;
862 * Process a byte received from the keyboard
864 static void
865 kb8042_received_byte(
866 struct kb8042 *kb8042,
867 int scancode) /* raw scan code */
869 boolean_t legit; /* is this a legit key pos'n? */
870 int key_pos = -1;
871 enum keystate state;
872 boolean_t synthetic_release_needed;
875 * Intercept ACK and RESEND and signal the condition that
876 * kb8042_send_and_wait is waiting for.
878 if (scancode == KB_ACK) {
879 mutex_enter(&kb8042->w_hw_mutex);
880 kb8042->acked = 1;
881 cv_signal(&kb8042->cmd_cv);
882 mutex_exit(&kb8042->w_hw_mutex);
883 return;
884 } else if (scancode == KB_RESEND) {
885 mutex_enter(&kb8042->w_hw_mutex);
886 kb8042->need_retry = 1;
887 cv_signal(&kb8042->cmd_cv);
888 mutex_exit(&kb8042->w_hw_mutex);
889 return;
892 if (!kb8042->w_init) /* can't do anything anyway */
893 return;
895 legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
896 &synthetic_release_needed);
898 if (legit == 0) {
899 /* Eaten by translation */
900 return;
904 * Don't know if we want this permanently, but it seems interesting
905 * for the moment.
907 if (key_pos == kb8042->debugger.mod1) {
908 kb8042->debugger.mod1_down = (state == KEY_PRESSED);
910 if (key_pos == kb8042->debugger.mod2) {
911 kb8042->debugger.mod2_down = (state == KEY_PRESSED);
913 if (kb8042->debugger.enabled &&
914 key_pos == kb8042->debugger.trigger &&
915 kb8042->debugger.mod1_down &&
916 kb8042->debugger.mod2_down) {
918 * Require new presses of the modifiers.
920 kb8042->debugger.mod1_down = B_FALSE;
921 kb8042->debugger.mod2_down = B_FALSE;
922 abort_sequence_enter(NULL);
923 return;
927 * If there's no queue above us - as can happen if we've been
928 * attached but not opened - drop the keystroke.
929 * Note that we do this here instead of above so that
930 * Ctrl-Alt-D still works.
932 if (kb8042->w_qp == NULL) {
933 return;
937 * This is to filter out auto repeat since it can't be
938 * turned off at the hardware. (Yeah, yeah, PS/2 keyboards
939 * can. Don't know whether they've taken over the world.
940 * Don't think so.)
942 if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
943 return;
947 kb8042_process_key(kb8042, key_pos, state);
950 * This is a total hack. For some stupid reason, the two additional
951 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
952 * only. We synthesize a release immediately.
954 if (synthetic_release_needed) {
955 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
956 kb8042_process_key(kb8042, key_pos, state);
961 static void
962 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
963 enum keystate state)
965 kbtrans_key_t key;
967 ASSERT(key_pos >= 0 && key_pos <= 255);
968 if (kb8042->simulated_kbd_type == KB_PC) {
969 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
970 } else if (kb8042->simulated_kbd_type == KB_USB) {
971 key = keytab_pc2usb[key_pos];
972 if (key != 0) {
973 kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
979 * Called from interrupt handler when keyboard interrupt occurs.
981 static uint_t
982 kb8042_intr(caddr_t arg)
984 uchar_t scancode; /* raw scan code */
985 int rc;
986 struct kb8042 *kb8042 = (struct kb8042 *)arg;
988 rc = DDI_INTR_UNCLAIMED;
990 if (kb8042->init_state == KB8042_UNINITIALIZED)
991 return (DDI_INTR_UNCLAIMED);
993 /* don't care if drv_setparm succeeds */
994 (void) drv_setparm(SYSRINT, 1);
996 while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
997 != 0) {
998 rc = DDI_INTR_CLAIMED;
1000 scancode = ddi_get8(kb8042->handle,
1001 kb8042->addr + I8042_INT_INPUT_DATA);
1003 kb8042_received_byte(kb8042, scancode);
1006 return (rc);
1009 static void
1010 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
1012 struct copyresp *csp;
1014 csp = (struct copyresp *)mp->b_rptr;
1015 if (csp->cp_rval) {
1016 freemsg(mp);
1017 return;
1020 switch (csp->cp_cmd) {
1021 default:
1022 miocack(qp, mp, 0, 0);
1023 break;
1027 static boolean_t
1028 kb8042_polled_keycheck(
1029 struct kbtrans_hardware *hw,
1030 int *key,
1031 enum keystate *state)
1033 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1034 int scancode;
1035 boolean_t legit;
1036 boolean_t synthetic_release_needed;
1038 if (kb8042->polled_synthetic_release_pending) {
1039 *key = kb8042->polled_synthetic_release_key;
1040 *state = KEY_RELEASED;
1041 kb8042->polled_synthetic_release_pending = B_FALSE;
1042 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1043 return (B_TRUE);
1046 for (;;) {
1047 if (ddi_get8(kb8042->handle,
1048 kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
1049 return (B_FALSE);
1052 scancode = ddi_get8(kb8042->handle,
1053 kb8042->addr + I8042_POLL_INPUT_DATA);
1055 legit = KeyboardConvertScan(kb8042, scancode, key, state,
1056 &synthetic_release_needed);
1057 if (!legit) {
1058 continue;
1061 * For the moment at least, we rely on hardware autorepeat
1062 * for polled I/O autorepeat. However, for coordination
1063 * with the interrupt-driven code, maintain the last key
1064 * pressed.
1066 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1069 * This is a total hack to support two additional keys
1070 * on Korean keyboards. They report only on press, and
1071 * so we synthesize a release. Most likely this will
1072 * never be important to polled I/O, but if I do it
1073 * "right" the first time it _won't_ be an issue.
1075 if (synthetic_release_needed) {
1076 kb8042->polled_synthetic_release_pending = B_TRUE;
1077 kb8042->polled_synthetic_release_key = *key;
1080 if (kb8042->simulated_kbd_type == KB_USB) {
1081 *key = keytab_pc2usb[*key];
1083 return (B_TRUE);
1087 static void
1088 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1090 kb8042->leds.desired = led_state;
1092 if (!polled)
1093 mutex_enter(&kb8042->w_hw_mutex);
1095 if (kb8042->leds.desired != kb8042->leds.commanded) {
1096 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1099 if (!polled)
1100 mutex_exit(&kb8042->w_hw_mutex);
1103 static void
1104 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
1106 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1107 kb8042_setled(kb8042, led_state, B_TRUE);
1110 static void
1111 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1113 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1114 kb8042_setled(kb8042, led_state, B_FALSE);
1118 static int
1119 kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
1121 uint8_t *outp = kb8042->addr +
1122 (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
1123 uint8_t *inavp = kb8042->addr +
1124 (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
1125 uint8_t *inp = kb8042->addr +
1126 (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
1127 uint8_t b;
1128 int ms_waited;
1129 int timedout;
1130 int expire;
1131 int retries = 0;
1133 do {
1134 kb8042->acked = 0;
1135 kb8042->need_retry = 0;
1136 ms_waited = 0; /* Zero it whether polled or not */
1137 timedout = 0;
1139 ddi_put8(kb8042->handle, outp, u8);
1141 while (!kb8042->acked && !kb8042->need_retry && !timedout) {
1143 if (polled) {
1144 if (ddi_get8(kb8042->handle, inavp)) {
1145 b = ddi_get8(kb8042->handle, inp);
1146 switch (b) {
1147 case KB_ACK:
1148 kb8042->acked = 1;
1149 break;
1150 case KB_RESEND:
1151 kb8042->need_retry = 1;
1152 break;
1153 default:
1155 * drop it: We should never
1156 * get scancodes while
1157 * we're in the middle of a
1158 * command anyway.
1160 #ifdef DEBUG
1161 cmn_err(CE_WARN, "!Unexpected "
1162 " byte 0x%x", b);
1163 #endif
1164 break;
1169 * Wait 1ms if an ACK wasn't received yet
1171 if (!kb8042->acked) {
1172 drv_usecwait(1000);
1173 ms_waited++;
1174 if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
1175 timedout = B_TRUE;
1177 } else {
1178 /* Interrupt-driven */
1179 expire = ddi_get_lbolt() +
1180 drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
1183 * If cv_timedwait returned -1 and we neither
1184 * received an ACK nor a RETRY response, then
1185 * we timed out.
1187 if (cv_timedwait(&kb8042->cmd_cv,
1188 &kb8042->w_hw_mutex, expire) == -1 &&
1189 !kb8042->acked && !kb8042->need_retry) {
1190 timedout = B_TRUE;
1195 } while ((kb8042->need_retry || timedout) &&
1196 ++retries < MAX_KB8042_RETRIES);
1198 return (kb8042->acked);
1202 * kb8042_send_to_keyboard should be called with w_hw_mutex held if
1203 * polled is FALSE.
1205 static void
1206 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
1210 * KB_SET_LED and KB_ENABLE are special commands which require blocking
1211 * other 8042 consumers while executing.
1213 * Other commands/data are sent using the single put8 I/O access
1214 * function.
1216 if (byte == KB_SET_LED) {
1218 if (!polled) {
1219 (void) ddi_get8(kb8042->handle, kb8042->addr +
1220 I8042_LOCK);
1223 if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
1225 * Ignore return value, as there's nothing we can
1226 * do about it if the SET LED command fails.
1228 (void) kb8042_send_and_wait(kb8042,
1229 kb8042_xlate_leds(kb8042->leds.desired), polled);
1232 if (!polled) {
1233 (void) ddi_get8(kb8042->handle, kb8042->addr +
1234 I8042_UNLOCK);
1236 kb8042->leds.commanded = kb8042->leds.desired;
1238 } else if (byte == KB_ENABLE) {
1240 if (!polled) {
1241 (void) ddi_get8(kb8042->handle, kb8042->addr +
1242 I8042_LOCK);
1245 (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
1247 if (!polled) {
1248 (void) ddi_get8(kb8042->handle, kb8042->addr +
1249 I8042_UNLOCK);
1252 } else {
1253 /* All other commands use the "normal" virtual output port */
1254 if (polled) {
1255 ddi_put8(kb8042->handle,
1256 kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
1257 } else {
1258 ddi_put8(kb8042->handle,
1259 kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
1265 * Wait until the keyboard is fully up, maybe.
1266 * We may be the first person to talk to the keyboard, in which case
1267 * it's patiently waiting to say "AA" to us to tell us it's up.
1268 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
1269 * good bet that we've already spent that long getting to that point,
1270 * so we'll only wait long enough for the communications electronics to
1271 * run.
1273 static void
1274 kb8042_wait_poweron(struct kb8042 *kb8042)
1276 int cnt;
1277 int ready;
1279 /* wait for up to 250 ms for a response */
1280 for (cnt = 0; cnt < 250; cnt++) {
1281 ready = ddi_get8(kb8042->handle,
1282 kb8042->addr + I8042_INT_INPUT_AVAIL);
1283 if (ready != 0)
1284 break;
1285 drv_usecwait(1000);
1289 * If there's something pending, read and discard it. If not,
1290 * assume things are OK anyway - maybe somebody else ate it
1291 * already. (On a PC, the BIOS almost certainly did.)
1293 if (ready != 0) {
1294 (void) ddi_get8(kb8042->handle,
1295 kb8042->addr + I8042_INT_INPUT_DATA);
1299 static int
1300 kb8042_xlate_leds(int led)
1302 int res;
1304 res = 0;
1306 if (led & LED_NUM_LOCK)
1307 res |= LED_NUM;
1308 if (led & LED_SCROLL_LOCK)
1309 res |= LED_SCR;
1310 if (led & LED_CAPS_LOCK)
1311 res |= LED_CAP;
1313 return (res);
1316 /*ARGSUSED*/
1317 static void
1318 kb8042_get_initial_leds(
1319 struct kb8042 *kb8042,
1320 int *initial_leds,
1321 int *initial_led_mask)
1323 #if defined(__i386) || defined(__amd64)
1324 extern caddr_t p0_va;
1325 uint8_t bios_kb_flag;
1327 bios_kb_flag = p0_va[BIOS_KB_FLAG];
1329 *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1330 *initial_leds = 0;
1331 if (bios_kb_flag & BIOS_CAPS_STATE)
1332 *initial_leds |= LED_CAPS_LOCK;
1333 if (bios_kb_flag & BIOS_NUM_STATE)
1334 *initial_leds |= LED_NUM_LOCK;
1335 if (bios_kb_flag & BIOS_SCROLL_STATE)
1336 *initial_leds |= LED_SCROLL_LOCK;
1337 #else
1338 *initial_leds = 0;
1339 *initial_led_mask = 0;
1340 #endif
1343 static boolean_t
1344 kb8042_autorepeat_detect(
1345 struct kb8042 *kb8042,
1346 int key_pos,
1347 enum keystate state)
1349 if (state == KEY_RELEASED) {
1350 if (kb8042->kb_old_key_pos == key_pos)
1351 kb8042->kb_old_key_pos = 0;
1352 } else {
1353 if (kb8042->kb_old_key_pos == key_pos) {
1354 return (B_TRUE);
1356 kb8042->kb_old_key_pos = key_pos;
1358 return (B_FALSE);
1361 /* ARGSUSED */
1362 static void
1363 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1365 switch (cmd) {
1366 case KBD_CMD_BELL:
1367 (void) beeper_on(BEEP_TYPE4);
1368 break;
1369 case KBD_CMD_NOBELL:
1370 (void) beeper_off();
1371 break;
1377 * This is a pass-thru routine to get a character at poll time.
1379 static int
1380 kb8042_polled_getchar(cons_polledio_arg_t arg)
1382 struct kb8042 *kb8042;
1384 kb8042 = (struct kb8042 *)arg;
1386 return (kbtrans_getchar(kb8042->hw_kbtrans));
1390 * This is a pass-thru routine to get a character at poll time.
1392 static int
1393 kb8042_polled_ischar(cons_polledio_arg_t arg)
1395 struct kb8042 *kb8042;
1397 kb8042 = (struct kb8042 *)arg;
1399 return (kbtrans_ischar(kb8042->hw_kbtrans));