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]
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>
43 #include <sys/sunddi.h>
45 #include "sys/consdev.h"
47 #include <sys/kbtrans.h>
50 #include <sys/i8042.h>
52 #include "sys/kbio.h" /* for KIOCSLAYOUT */
54 #include "sys/reboot.h"
55 #include <sys/promif.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,
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
,
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
= {
164 .qi_qopen
= kb8042_open
,
165 .qi_qclose
= kb8042_close
,
167 .qi_minfo
= &kb8042_sinfo
,
174 static struct qinit kb8042_winit
= {
176 .qi_srvp
= kb8042_wsrv
,
177 .qi_qopen
= kb8042_open
,
178 .qi_qclose
= kb8042_close
,
180 .qi_minfo
= &kb8042_sinfo
,
187 struct streamtab kb8042_str_info
= {
188 .st_rdinit
= &kb8042_rinit
,
189 .st_wrinit
= &kb8042_winit
,
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
,
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
= {
205 .cb_strategy
= nodev
,
214 .cb_chpoll
= nochpoll
,
215 .cb_prop_op
= ddi_prop_op
,
216 .cb_str
= &kb8042_str_info
,
223 struct dev_ops kb8042_ops
= {
224 .devo_rev
= DEVO_REV
,
226 .devo_getinfo
= kb8042_getinfo
,
227 .devo_identify
= nulldev
,
228 .devo_probe
= nulldev
,
229 .devo_attach
= kb8042_attach
,
230 .devo_detach
= kb8042_detach
,
232 .devo_cb_ops
= &cb_kb8042_ops
,
233 .devo_bus_ops
= 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
= {
255 .ml_linkage
= { &modldrv
, NULL
}
263 rv
= mod_install(&modlinkage
);
271 return (mod_remove(&modlinkage
));
275 _info(struct modinfo
*modinfop
)
277 return (mod_info(&modlinkage
, modinfop
));
282 kb8042_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
288 struct kb8042
*kb8042
= &Kdws
;
289 static ddi_device_acc_attr_t attr
= {
297 leds
= kb8042
->leds
.commanded
;
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
);
312 if (kb8042_dip
!= NULL
)
313 return (DDI_FAILURE
);
314 /* The rest of the function is for attach */
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
;
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
) {
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
) {
344 cmn_err(CE_WARN
, "kb8042_attach: can't map registers");
349 kb8042
->init_state
|= KB8042_REGS_MAPPED
;
351 if (ddi_get_iblock_cookie(devi
, 0, &kb8042
->w_iblock
) !=
353 cmn_err(CE_WARN
, "kb8042_attach: Can't get iblock cookie");
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 */
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 */
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");
384 kb8042
->init_state
|= KB8042_INTR_ADDED
;
386 ddi_report_dev(devi
);
389 cmn_err(CE_CONT
, "?%s instance #%d READY\n",
390 DRIVER_NAME(devi
), ddi_get_instance(devi
));
393 return (DDI_SUCCESS
);
396 kb8042_cleanup(kb8042
);
397 return (DDI_FAILURE
);
401 kb8042_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
403 struct kb8042
*kb8042
= &Kdws
;
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
);
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
);
427 return (DDI_FAILURE
);
435 ddi_info_cmd_t infocmd
,
442 case DDI_INFO_DEVT2DEVINFO
:
443 if (kb8042_dip
== NULL
) {
446 *result
= (void *) kb8042_dip
;
450 case DDI_INFO_DEVT2INSTANCE
:
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
;
487 kb8042_init(struct kb8042
*kb8042
, boolean_t 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
);
516 (void) drv_setparm(SYSRINT
, 1); /* reset keyboard interrupts */
521 kb8042_open(queue_t
*qp
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
523 struct kb8042
*kb8042
;
526 int initial_led_mask
;
530 mutex_enter(&kb8042
->w_hw_mutex
);
532 kb8042
->w_dev
= *devp
;
533 mutex_exit(&kb8042
->w_hw_mutex
);
537 if (secpolicy_console(credp
) != 0) {
538 mutex_exit(&kb8042
->w_hw_mutex
);
542 while (kb8042
->suspended
) {
543 if (cv_wait_sig(&kb8042
->suspend_cv
, &kb8042
->w_hw_mutex
) ==
545 mutex_exit(&kb8042
->w_hw_mutex
);
550 kb8042
->w_dev
= *devp
;
551 qp
->q_ptr
= (caddr_t
)kb8042
;
552 WR(qp
)->q_ptr
= qp
->q_ptr
;
556 ASSERT(kb8042
->ops
>= 0);
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
,
564 initial_leds
, initial_led_mask
);
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
;
588 kbtrans_streams_enable(kb8042
->hw_kbtrans
);
591 mutex_enter(&kb8042
->w_hw_mutex
);
592 ASSERT(kb8042
->ops
> 0);
594 if (kb8042
->ops
== 0)
595 cv_broadcast(&kb8042
->ops_cv
);
596 mutex_exit(&kb8042
->w_hw_mutex
);
603 kb8042_close(queue_t
*qp
, int flag
, cred_t
*credp
)
605 struct kb8042
*kb8042
;
607 /* If a beep is in progress, stop that */
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
) ==
616 mutex_exit(&kb8042
->w_hw_mutex
);
621 ASSERT(kb8042
->ops
>= 0);
623 mutex_exit(&kb8042
->w_hw_mutex
);
625 (void) kbtrans_streams_fini(kb8042
->hw_kbtrans
);
627 kb8042
->w_qp
= (queue_t
*)NULL
;
630 mutex_enter(&kb8042
->w_hw_mutex
);
631 ASSERT(kb8042
->ops
> 0);
633 if (kb8042
->ops
== 0)
634 cv_broadcast(&kb8042
->ops_cv
);
635 mutex_exit(&kb8042
->w_hw_mutex
);
641 kb8042_wsrv(queue_t
*qp
)
643 struct kb8042
*kb8042
;
648 kb8042
= (struct kb8042
*)qp
->q_ptr
;
650 mutex_enter(&kb8042
->w_hw_mutex
);
651 suspended
= kb8042
->suspended
;
652 ASSERT(kb8042
->ops
>= 0);
655 mutex_exit(&kb8042
->w_hw_mutex
);
658 while (!suspended
&& (mp
= getq(qp
)) != NULL
) {
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
))) {
667 switch (kbtrans_streams_message(kb8042
->hw_kbtrans
, mp
)) {
668 case KBTRANS_MESSAGE_HANDLED
:
670 case KBTRANS_MESSAGE_NOT_HANDLED
:
673 switch (mp
->b_datap
->db_type
) {
675 kb8042_ioctlmsg(kb8042
, qp
, mp
);
678 kb8042_iocdatamsg(qp
, mp
);
683 case M_READ
: /* ignore, no buffered data */
687 *mp
->b_rptr
&= ~FLUSHW
;
688 if (*mp
->b_rptr
& FLUSHR
)
694 cmn_err(CE_NOTE
, "kb8042_wsrv: bad msg %x",
695 mp
->b_datap
->db_type
);
701 mutex_enter(&kb8042
->w_hw_mutex
);
703 ASSERT(kb8042
->ops
> 0);
705 if (kb8042
->ops
== 0)
706 cv_broadcast(&kb8042
->ops_cv
);
708 mutex_exit(&kb8042
->w_hw_mutex
);
714 kb8042_ioctlmsg(struct kb8042
*kb8042
, queue_t
*qp
, mblk_t
*mp
)
724 iocp
= (struct iocblk
*)mp
->b_rptr
;
726 switch (iocp
->ioc_cmd
) {
728 case CONSOPENPOLLEDIO
:
729 error
= miocpullup(mp
, sizeof (struct cons_polledio
*));
731 miocnak(qp
, mp
, 0, error
);
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
=
741 mp
->b_datap
->db_type
= M_IOCACK
;
746 case CONSCLOSEPOLLEDIO
:
747 miocack(qp
, mp
, 0, 0);
750 case CONSSETABORTENABLE
:
751 if (iocp
->ioc_count
!= TRANSPARENT
) {
752 miocnak(qp
, mp
, 0, EINVAL
);
756 kb8042
->debugger
.enabled
= *(intptr_t *)mp
->b_cont
->b_rptr
;
757 miocack(qp
, mp
, 0, 0);
761 * Valid only in TR_UNTRANS_MODE mode.
764 error
= miocpullup(mp
, sizeof (int));
766 miocnak(qp
, mp
, 0, error
);
769 tmp
= *(int *)mp
->b_cont
->b_rptr
;
770 if (tmp
!= KB_PC
&& tmp
!= KB_USB
) {
771 miocnak(qp
, mp
, 0, EINVAL
);
774 kb8042
->simulated_kbd_type
= tmp
;
775 miocack(qp
, mp
, 0, 0);
779 if (kb8042
->w_kblayout
== -1) {
780 miocnak(qp
, mp
, 0, EINVAL
);
784 if ((datap
= allocb(sizeof (int), BPRI_HI
)) == NULL
) {
785 miocnak(qp
, mp
, 0, ENOMEM
);
789 if (kb8042
->simulated_kbd_type
== KB_USB
)
790 *(int *)datap
->b_wptr
= KBTRANS_USBKB_DEFAULT_LAYOUT
;
792 *(int *)datap
->b_wptr
= kb8042
->w_kblayout
;
794 datap
->b_wptr
+= sizeof (int);
798 iocp
->ioc_count
= sizeof (int);
799 mp
->b_datap
->db_type
= M_IOCACK
;
805 if (iocp
->ioc_count
!= TRANSPARENT
) {
806 miocnak(qp
, mp
, 0, EINVAL
);
810 kb8042
->w_kblayout
= *(intptr_t *)mp
->b_cont
->b_rptr
;
811 miocack(qp
, mp
, 0, 0);
815 error
= miocpullup(mp
, sizeof (int));
817 miocnak(qp
, mp
, 0, error
);
821 kb8042_type4_cmd(kb8042
, *(int *)mp
->b_cont
->b_rptr
);
822 miocack(qp
, mp
, 0, 0);
826 if (iocp
->ioc_count
!= TRANSPARENT
) {
827 miocnak(qp
, mp
, 0, EINVAL
);
831 tmp
= (int)(*(intptr_t *)mp
->b_cont
->b_rptr
);
832 cycles
= tmp
& 0xffff;
833 msecs
= (tmp
>> 16) & 0xffff;
836 frequency
= UINT16_MAX
;
837 else if (cycles
== UINT16_MAX
)
840 frequency
= (PIT_HZ
+ cycles
/ 2) / cycles
;
841 if (frequency
> UINT16_MAX
)
842 frequency
= UINT16_MAX
;
845 error
= beep_mktone(frequency
, msecs
);
847 miocnak(qp
, mp
, 0, error
);
849 miocack(qp
, mp
, 0, 0);
854 cmn_err(CE_NOTE
, "!kb8042_ioctlmsg %x", iocp
->ioc_cmd
);
856 miocnak(qp
, mp
, 0, EINVAL
);
862 * Process a byte received from the keyboard
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? */
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
);
881 cv_signal(&kb8042
->cmd_cv
);
882 mutex_exit(&kb8042
->w_hw_mutex
);
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
);
892 if (!kb8042
->w_init
) /* can't do anything anyway */
895 legit
= KeyboardConvertScan(kb8042
, scancode
, &key_pos
, &state
,
896 &synthetic_release_needed
);
899 /* Eaten by translation */
904 * Don't know if we want this permanently, but it seems interesting
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
);
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
) {
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.
942 if (kb8042_autorepeat_detect(kb8042
, key_pos
, state
)) {
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
);
962 kb8042_process_key(struct kb8042
*kb8042
, kbtrans_key_t key_pos
,
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
];
973 kbtrans_streams_key(kb8042
->hw_kbtrans
, key
, state
);
979 * Called from interrupt handler when keyboard interrupt occurs.
982 kb8042_intr(caddr_t arg
)
984 uchar_t scancode
; /* raw scan code */
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
)
998 rc
= DDI_INTR_CLAIMED
;
1000 scancode
= ddi_get8(kb8042
->handle
,
1001 kb8042
->addr
+ I8042_INT_INPUT_DATA
);
1003 kb8042_received_byte(kb8042
, scancode
);
1010 kb8042_iocdatamsg(queue_t
*qp
, mblk_t
*mp
)
1012 struct copyresp
*csp
;
1014 csp
= (struct copyresp
*)mp
->b_rptr
;
1020 switch (csp
->cp_cmd
) {
1022 miocack(qp
, mp
, 0, 0);
1028 kb8042_polled_keycheck(
1029 struct kbtrans_hardware
*hw
,
1031 enum keystate
*state
)
1033 struct kb8042
*kb8042
= (struct kb8042
*)hw
;
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
);
1047 if (ddi_get8(kb8042
->handle
,
1048 kb8042
->addr
+ I8042_POLL_INPUT_AVAIL
) == 0) {
1052 scancode
= ddi_get8(kb8042
->handle
,
1053 kb8042
->addr
+ I8042_POLL_INPUT_DATA
);
1055 legit
= KeyboardConvertScan(kb8042
, scancode
, key
, state
,
1056 &synthetic_release_needed
);
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
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
];
1088 kb8042_setled(struct kb8042
*kb8042
, int led_state
, boolean_t polled
)
1090 kb8042
->leds
.desired
= led_state
;
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
);
1100 mutex_exit(&kb8042
->w_hw_mutex
);
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
);
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
);
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
);
1135 kb8042
->need_retry
= 0;
1136 ms_waited
= 0; /* Zero it whether polled or not */
1139 ddi_put8(kb8042
->handle
, outp
, u8
);
1141 while (!kb8042
->acked
&& !kb8042
->need_retry
&& !timedout
) {
1144 if (ddi_get8(kb8042
->handle
, inavp
)) {
1145 b
= ddi_get8(kb8042
->handle
, inp
);
1151 kb8042
->need_retry
= 1;
1155 * drop it: We should never
1156 * get scancodes while
1157 * we're in the middle of a
1161 cmn_err(CE_WARN
, "!Unexpected "
1169 * Wait 1ms if an ACK wasn't received yet
1171 if (!kb8042
->acked
) {
1174 if (ms_waited
>= MAX_KB8042_WAIT_MAX_MS
)
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
1187 if (cv_timedwait(&kb8042
->cmd_cv
,
1188 &kb8042
->w_hw_mutex
, expire
) == -1 &&
1189 !kb8042
->acked
&& !kb8042
->need_retry
) {
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
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
1216 if (byte
== KB_SET_LED
) {
1219 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
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
);
1233 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1236 kb8042
->leds
.commanded
= kb8042
->leds
.desired
;
1238 } else if (byte
== KB_ENABLE
) {
1241 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1245 (void) kb8042_send_and_wait(kb8042
, KB_ENABLE
, polled
);
1248 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1253 /* All other commands use the "normal" virtual output port */
1255 ddi_put8(kb8042
->handle
,
1256 kb8042
->addr
+ I8042_POLL_OUTPUT_DATA
, byte
);
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
1274 kb8042_wait_poweron(struct kb8042
*kb8042
)
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
);
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.)
1294 (void) ddi_get8(kb8042
->handle
,
1295 kb8042
->addr
+ I8042_INT_INPUT_DATA
);
1300 kb8042_xlate_leds(int led
)
1306 if (led
& LED_NUM_LOCK
)
1308 if (led
& LED_SCROLL_LOCK
)
1310 if (led
& LED_CAPS_LOCK
)
1318 kb8042_get_initial_leds(
1319 struct kb8042
*kb8042
,
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
;
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
;
1339 *initial_led_mask
= 0;
1344 kb8042_autorepeat_detect(
1345 struct kb8042
*kb8042
,
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;
1353 if (kb8042
->kb_old_key_pos
== key_pos
) {
1356 kb8042
->kb_old_key_pos
= key_pos
;
1363 kb8042_type4_cmd(struct kb8042
*kb8042
, int cmd
)
1367 (void) beeper_on(BEEP_TYPE4
);
1369 case KBD_CMD_NOBELL
:
1370 (void) beeper_off();
1377 * This is a pass-thru routine to get a character at poll time.
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.
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
));