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
= {
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
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
,
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 */
185 nodev
, /* cb_write */
186 nodev
, /* cb_ioctl */
187 nodev
, /* cb_devmap */
189 nodev
, /* cb_segmap */
190 nochpoll
, /* cb_chpoll */
191 ddi_prop_op
, /* cb_prop_op */
192 &kb8042_str_info
, /* cb_stream */
196 struct dev_ops kb8042_ops
= {
197 DEVO_REV
, /* devo_rev */
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
= {
237 rv
= mod_install(&modlinkage
);
245 return (mod_remove(&modlinkage
));
249 _info(struct modinfo
*modinfop
)
251 return (mod_info(&modlinkage
, modinfop
));
256 kb8042_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
262 struct kb8042
*kb8042
= &Kdws
;
263 static ddi_device_acc_attr_t attr
= {
271 leds
= kb8042
->leds
.commanded
;
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
);
286 if (kb8042_dip
!= NULL
)
287 return (DDI_FAILURE
);
288 /* The rest of the function is for attach */
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
;
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
) {
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
) {
318 cmn_err(CE_WARN
, "kb8042_attach: can't map registers");
323 kb8042
->init_state
|= KB8042_REGS_MAPPED
;
325 if (ddi_get_iblock_cookie(devi
, 0, &kb8042
->w_iblock
) !=
327 cmn_err(CE_WARN
, "kb8042_attach: Can't get iblock cookie");
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 */
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 */
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");
358 kb8042
->init_state
|= KB8042_INTR_ADDED
;
360 ddi_report_dev(devi
);
363 cmn_err(CE_CONT
, "?%s instance #%d READY\n",
364 DRIVER_NAME(devi
), ddi_get_instance(devi
));
367 return (DDI_SUCCESS
);
370 kb8042_cleanup(kb8042
);
371 return (DDI_FAILURE
);
375 kb8042_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
377 struct kb8042
*kb8042
= &Kdws
;
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
);
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
);
401 return (DDI_FAILURE
);
409 ddi_info_cmd_t infocmd
,
416 case DDI_INFO_DEVT2DEVINFO
:
417 if (kb8042_dip
== NULL
) {
420 *result
= (void *) kb8042_dip
;
424 case DDI_INFO_DEVT2INSTANCE
:
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
;
461 kb8042_init(struct kb8042
*kb8042
, boolean_t 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
);
490 (void) drv_setparm(SYSRINT
, 1); /* reset keyboard interrupts */
495 kb8042_open(queue_t
*qp
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
497 struct kb8042
*kb8042
;
500 int initial_led_mask
;
504 mutex_enter(&kb8042
->w_hw_mutex
);
506 kb8042
->w_dev
= *devp
;
507 mutex_exit(&kb8042
->w_hw_mutex
);
511 if (secpolicy_console(credp
) != 0) {
512 mutex_exit(&kb8042
->w_hw_mutex
);
516 while (kb8042
->suspended
) {
517 if (cv_wait_sig(&kb8042
->suspend_cv
, &kb8042
->w_hw_mutex
) ==
519 mutex_exit(&kb8042
->w_hw_mutex
);
524 kb8042
->w_dev
= *devp
;
525 qp
->q_ptr
= (caddr_t
)kb8042
;
526 WR(qp
)->q_ptr
= qp
->q_ptr
;
530 ASSERT(kb8042
->ops
>= 0);
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
,
538 initial_leds
, initial_led_mask
);
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
;
562 kbtrans_streams_enable(kb8042
->hw_kbtrans
);
565 mutex_enter(&kb8042
->w_hw_mutex
);
566 ASSERT(kb8042
->ops
> 0);
568 if (kb8042
->ops
== 0)
569 cv_broadcast(&kb8042
->ops_cv
);
570 mutex_exit(&kb8042
->w_hw_mutex
);
577 kb8042_close(queue_t
*qp
, int flag
, cred_t
*credp
)
579 struct kb8042
*kb8042
;
581 /* If a beep is in progress, stop that */
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
) ==
590 mutex_exit(&kb8042
->w_hw_mutex
);
595 ASSERT(kb8042
->ops
>= 0);
597 mutex_exit(&kb8042
->w_hw_mutex
);
599 (void) kbtrans_streams_fini(kb8042
->hw_kbtrans
);
601 kb8042
->w_qp
= (queue_t
*)NULL
;
604 mutex_enter(&kb8042
->w_hw_mutex
);
605 ASSERT(kb8042
->ops
> 0);
607 if (kb8042
->ops
== 0)
608 cv_broadcast(&kb8042
->ops_cv
);
609 mutex_exit(&kb8042
->w_hw_mutex
);
615 kb8042_wsrv(queue_t
*qp
)
617 struct kb8042
*kb8042
;
622 kb8042
= (struct kb8042
*)qp
->q_ptr
;
624 mutex_enter(&kb8042
->w_hw_mutex
);
625 suspended
= kb8042
->suspended
;
626 ASSERT(kb8042
->ops
>= 0);
629 mutex_exit(&kb8042
->w_hw_mutex
);
632 while (!suspended
&& (mp
= getq(qp
)) != NULL
) {
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
))) {
641 switch (kbtrans_streams_message(kb8042
->hw_kbtrans
, mp
)) {
642 case KBTRANS_MESSAGE_HANDLED
:
644 case KBTRANS_MESSAGE_NOT_HANDLED
:
647 switch (mp
->b_datap
->db_type
) {
649 kb8042_ioctlmsg(kb8042
, qp
, mp
);
652 kb8042_iocdatamsg(qp
, mp
);
657 case M_READ
: /* ignore, no buffered data */
661 *mp
->b_rptr
&= ~FLUSHW
;
662 if (*mp
->b_rptr
& FLUSHR
)
668 cmn_err(CE_NOTE
, "kb8042_wsrv: bad msg %x",
669 mp
->b_datap
->db_type
);
675 mutex_enter(&kb8042
->w_hw_mutex
);
677 ASSERT(kb8042
->ops
> 0);
679 if (kb8042
->ops
== 0)
680 cv_broadcast(&kb8042
->ops_cv
);
682 mutex_exit(&kb8042
->w_hw_mutex
);
688 kb8042_ioctlmsg(struct kb8042
*kb8042
, queue_t
*qp
, mblk_t
*mp
)
698 iocp
= (struct iocblk
*)mp
->b_rptr
;
700 switch (iocp
->ioc_cmd
) {
702 case CONSOPENPOLLEDIO
:
703 error
= miocpullup(mp
, sizeof (struct cons_polledio
*));
705 miocnak(qp
, mp
, 0, error
);
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
=
715 mp
->b_datap
->db_type
= M_IOCACK
;
720 case CONSCLOSEPOLLEDIO
:
721 miocack(qp
, mp
, 0, 0);
724 case CONSSETABORTENABLE
:
725 if (iocp
->ioc_count
!= TRANSPARENT
) {
726 miocnak(qp
, mp
, 0, EINVAL
);
730 kb8042
->debugger
.enabled
= *(intptr_t *)mp
->b_cont
->b_rptr
;
731 miocack(qp
, mp
, 0, 0);
735 * Valid only in TR_UNTRANS_MODE mode.
738 error
= miocpullup(mp
, sizeof (int));
740 miocnak(qp
, mp
, 0, error
);
743 tmp
= *(int *)mp
->b_cont
->b_rptr
;
744 if (tmp
!= KB_PC
&& tmp
!= KB_USB
) {
745 miocnak(qp
, mp
, 0, EINVAL
);
748 kb8042
->simulated_kbd_type
= tmp
;
749 miocack(qp
, mp
, 0, 0);
753 if (kb8042
->w_kblayout
== -1) {
754 miocnak(qp
, mp
, 0, EINVAL
);
758 if ((datap
= allocb(sizeof (int), BPRI_HI
)) == NULL
) {
759 miocnak(qp
, mp
, 0, ENOMEM
);
763 if (kb8042
->simulated_kbd_type
== KB_USB
)
764 *(int *)datap
->b_wptr
= KBTRANS_USBKB_DEFAULT_LAYOUT
;
766 *(int *)datap
->b_wptr
= kb8042
->w_kblayout
;
768 datap
->b_wptr
+= sizeof (int);
772 iocp
->ioc_count
= sizeof (int);
773 mp
->b_datap
->db_type
= M_IOCACK
;
779 if (iocp
->ioc_count
!= TRANSPARENT
) {
780 miocnak(qp
, mp
, 0, EINVAL
);
784 kb8042
->w_kblayout
= *(intptr_t *)mp
->b_cont
->b_rptr
;
785 miocack(qp
, mp
, 0, 0);
789 error
= miocpullup(mp
, sizeof (int));
791 miocnak(qp
, mp
, 0, error
);
795 kb8042_type4_cmd(kb8042
, *(int *)mp
->b_cont
->b_rptr
);
796 miocack(qp
, mp
, 0, 0);
800 if (iocp
->ioc_count
!= TRANSPARENT
) {
801 miocnak(qp
, mp
, 0, EINVAL
);
805 tmp
= (int)(*(intptr_t *)mp
->b_cont
->b_rptr
);
806 cycles
= tmp
& 0xffff;
807 msecs
= (tmp
>> 16) & 0xffff;
810 frequency
= UINT16_MAX
;
811 else if (cycles
== UINT16_MAX
)
814 frequency
= (PIT_HZ
+ cycles
/ 2) / cycles
;
815 if (frequency
> UINT16_MAX
)
816 frequency
= UINT16_MAX
;
819 error
= beep_mktone(frequency
, msecs
);
821 miocnak(qp
, mp
, 0, error
);
823 miocack(qp
, mp
, 0, 0);
828 cmn_err(CE_NOTE
, "!kb8042_ioctlmsg %x", iocp
->ioc_cmd
);
830 miocnak(qp
, mp
, 0, EINVAL
);
836 * Process a byte received from the keyboard
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? */
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
);
855 cv_signal(&kb8042
->cmd_cv
);
856 mutex_exit(&kb8042
->w_hw_mutex
);
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
);
866 if (!kb8042
->w_init
) /* can't do anything anyway */
869 legit
= KeyboardConvertScan(kb8042
, scancode
, &key_pos
, &state
,
870 &synthetic_release_needed
);
873 /* Eaten by translation */
878 * Don't know if we want this permanently, but it seems interesting
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
);
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
) {
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.
916 if (kb8042_autorepeat_detect(kb8042
, key_pos
, state
)) {
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
);
936 kb8042_process_key(struct kb8042
*kb8042
, kbtrans_key_t key_pos
,
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
];
947 kbtrans_streams_key(kb8042
->hw_kbtrans
, key
, state
);
953 * Called from interrupt handler when keyboard interrupt occurs.
956 kb8042_intr(caddr_t arg
)
958 uchar_t scancode
; /* raw scan code */
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
)
972 rc
= DDI_INTR_CLAIMED
;
974 scancode
= ddi_get8(kb8042
->handle
,
975 kb8042
->addr
+ I8042_INT_INPUT_DATA
);
977 kb8042_received_byte(kb8042
, scancode
);
984 kb8042_iocdatamsg(queue_t
*qp
, mblk_t
*mp
)
986 struct copyresp
*csp
;
988 csp
= (struct copyresp
*)mp
->b_rptr
;
994 switch (csp
->cp_cmd
) {
996 miocack(qp
, mp
, 0, 0);
1002 kb8042_polled_keycheck(
1003 struct kbtrans_hardware
*hw
,
1005 enum keystate
*state
)
1007 struct kb8042
*kb8042
= (struct kb8042
*)hw
;
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
);
1021 if (ddi_get8(kb8042
->handle
,
1022 kb8042
->addr
+ I8042_POLL_INPUT_AVAIL
) == 0) {
1026 scancode
= ddi_get8(kb8042
->handle
,
1027 kb8042
->addr
+ I8042_POLL_INPUT_DATA
);
1029 legit
= KeyboardConvertScan(kb8042
, scancode
, key
, state
,
1030 &synthetic_release_needed
);
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
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
];
1062 kb8042_setled(struct kb8042
*kb8042
, int led_state
, boolean_t polled
)
1064 kb8042
->leds
.desired
= led_state
;
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
);
1074 mutex_exit(&kb8042
->w_hw_mutex
);
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
);
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
);
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
);
1109 kb8042
->need_retry
= 0;
1110 ms_waited
= 0; /* Zero it whether polled or not */
1113 ddi_put8(kb8042
->handle
, outp
, u8
);
1115 while (!kb8042
->acked
&& !kb8042
->need_retry
&& !timedout
) {
1118 if (ddi_get8(kb8042
->handle
, inavp
)) {
1119 b
= ddi_get8(kb8042
->handle
, inp
);
1125 kb8042
->need_retry
= 1;
1129 * drop it: We should never
1130 * get scancodes while
1131 * we're in the middle of a
1135 cmn_err(CE_WARN
, "!Unexpected "
1143 * Wait 1ms if an ACK wasn't received yet
1145 if (!kb8042
->acked
) {
1148 if (ms_waited
>= MAX_KB8042_WAIT_MAX_MS
)
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
1161 if (cv_timedwait(&kb8042
->cmd_cv
,
1162 &kb8042
->w_hw_mutex
, expire
) == -1 &&
1163 !kb8042
->acked
&& !kb8042
->need_retry
) {
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
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
1190 if (byte
== KB_SET_LED
) {
1193 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
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
);
1207 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1210 kb8042
->leds
.commanded
= kb8042
->leds
.desired
;
1212 } else if (byte
== KB_ENABLE
) {
1215 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1219 (void) kb8042_send_and_wait(kb8042
, KB_ENABLE
, polled
);
1222 (void) ddi_get8(kb8042
->handle
, kb8042
->addr
+
1227 /* All other commands use the "normal" virtual output port */
1229 ddi_put8(kb8042
->handle
,
1230 kb8042
->addr
+ I8042_POLL_OUTPUT_DATA
, byte
);
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
1248 kb8042_wait_poweron(struct kb8042
*kb8042
)
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
);
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.)
1268 (void) ddi_get8(kb8042
->handle
,
1269 kb8042
->addr
+ I8042_INT_INPUT_DATA
);
1274 kb8042_xlate_leds(int led
)
1280 if (led
& LED_NUM_LOCK
)
1282 if (led
& LED_SCROLL_LOCK
)
1284 if (led
& LED_CAPS_LOCK
)
1292 kb8042_get_initial_leds(
1293 struct kb8042
*kb8042
,
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
;
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
;
1313 *initial_led_mask
= 0;
1318 kb8042_autorepeat_detect(
1319 struct kb8042
*kb8042
,
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;
1327 if (kb8042
->kb_old_key_pos
== key_pos
) {
1330 kb8042
->kb_old_key_pos
= key_pos
;
1337 kb8042_type4_cmd(struct kb8042
*kb8042
, int cmd
)
1341 (void) beeper_on(BEEP_TYPE4
);
1343 case KBD_CMD_NOBELL
:
1344 (void) beeper_off();
1351 * This is a pass-thru routine to get a character at poll time.
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.
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
));