17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5281f0747Slt200341 * Common Development and Distribution License (the "License").
6281f0747Slt200341 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
2648633f18SJan Setje-Eilers * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
277c478bd9Sstevel@tonic-gate * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
30e8ed0869SJohn Beck #define KEYMAP_SIZE_VARIABLE
31e8ed0869SJohn Beck
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/inline.h>
357c478bd9Sstevel@tonic-gate #include <sys/termio.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/termios.h>
387c478bd9Sstevel@tonic-gate #include <sys/stream.h>
397c478bd9Sstevel@tonic-gate #include <sys/strtty.h>
407c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
417c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
427c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/note.h>
457c478bd9Sstevel@tonic-gate #include "sys/consdev.h"
467c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
477c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
487c478bd9Sstevel@tonic-gate #include "kb8042.h"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #include <sys/i8042.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #include "sys/kbio.h" /* for KIOCSLAYOUT */
537c478bd9Sstevel@tonic-gate #include "sys/stat.h"
547c478bd9Sstevel@tonic-gate #include "sys/reboot.h"
557c478bd9Sstevel@tonic-gate #include <sys/promif.h>
567c478bd9Sstevel@tonic-gate #include <sys/beep.h>
57c35aa225Smarx #include <sys/inttypes.h>
58*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China #include <sys/policy.h>
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * For any keyboard, there is a unique code describing the position
627c478bd9Sstevel@tonic-gate * of the key on a keyboard. We refer to the code as "station number".
637c478bd9Sstevel@tonic-gate * The following table is used to map the station numbers from ps2
647c478bd9Sstevel@tonic-gate * AT/XT keyboards to that of a USB one.
65fd9cb95cSsethg *
66fd9cb95cSsethg * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
67fd9cb95cSsethg * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
68fd9cb95cSsethg * when in KB_PC mode. Note that this does not need to be made conditional
69fd9cb95cSsethg * on the architecture for which this module is complied because there are no
70fd9cb95cSsethg * keys that map to K8042_STOP on non-SPARC platforms.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
737c478bd9Sstevel@tonic-gate /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35,
747c478bd9Sstevel@tonic-gate /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42,
757c478bd9Sstevel@tonic-gate /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24,
767c478bd9Sstevel@tonic-gate /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4,
777c478bd9Sstevel@tonic-gate /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15,
787c478bd9Sstevel@tonic-gate /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27,
797c478bd9Sstevel@tonic-gate /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56,
807c478bd9Sstevel@tonic-gate /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231,
817c478bd9Sstevel@tonic-gate /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0,
827c478bd9Sstevel@tonic-gate /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80,
837c478bd9Sstevel@tonic-gate /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0,
847c478bd9Sstevel@tonic-gate /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84,
857c478bd9Sstevel@tonic-gate /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91,
867c478bd9Sstevel@tonic-gate /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0,
877c478bd9Sstevel@tonic-gate /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65,
887c478bd9Sstevel@tonic-gate /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0,
897c478bd9Sstevel@tonic-gate /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0,
907c478bd9Sstevel@tonic-gate /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0,
917c478bd9Sstevel@tonic-gate /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0,
927c478bd9Sstevel@tonic-gate /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0,
93fd9cb95cSsethg /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0,
947c478bd9Sstevel@tonic-gate /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0,
957c478bd9Sstevel@tonic-gate /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0,
967c478bd9Sstevel@tonic-gate /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0,
977c478bd9Sstevel@tonic-gate /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0,
987c478bd9Sstevel@tonic-gate /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,
997c478bd9Sstevel@tonic-gate /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0,
1007c478bd9Sstevel@tonic-gate /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0,
1017c478bd9Sstevel@tonic-gate /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0,
1021841b879SSeth Goldberg /* 232 */ 0, 128, 129, 127, 0, 0, 0, 0,
1037c478bd9Sstevel@tonic-gate /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
1047c478bd9Sstevel@tonic-gate /* 248 */ 0, 0, 0, 0
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate
107fd9cb95cSsethg #ifdef __sparc
108fd9cb95cSsethg #define USECS_PER_WAIT 100
109fd9cb95cSsethg #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
110fd9cb95cSsethg #define MIN_DELAY_USECS USECS_PER_WAIT
111fd9cb95cSsethg
112fd9cb95cSsethg boolean_t kb8042_warn_unknown_scanset = B_TRUE;
113fd9cb95cSsethg int kb8042_default_scanset = 2;
114fd9cb95cSsethg
115fd9cb95cSsethg #endif
116fd9cb95cSsethg
11715bfc6b7SSeth Goldberg #define MAX_KB8042_WAIT_MAX_MS 500 /* 500ms total */
11815bfc6b7SSeth Goldberg #define MAX_KB8042_RETRIES 5
11915bfc6b7SSeth Goldberg
1207c478bd9Sstevel@tonic-gate enum state_return { STATE_NORMAL, STATE_INTERNAL };
1217c478bd9Sstevel@tonic-gate
122fd9cb95cSsethg static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
1237c478bd9Sstevel@tonic-gate static uint_t kb8042_intr(caddr_t arg);
1247c478bd9Sstevel@tonic-gate static void kb8042_wait_poweron(struct kb8042 *kb8042);
1257c478bd9Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
1267c478bd9Sstevel@tonic-gate static int kb8042_xlate_leds(int);
127fd9cb95cSsethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
1287c478bd9Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
1297c478bd9Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
1307c478bd9Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck(
1317c478bd9Sstevel@tonic-gate struct kbtrans_hardware *hw, int *key,
1327c478bd9Sstevel@tonic-gate enum keystate *state);
1337c478bd9Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
1347c478bd9Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
1357c478bd9Sstevel@tonic-gate enum keystate state);
1367c478bd9Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
1377c478bd9Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
1387c478bd9Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *);
1397c478bd9Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
140281f0747Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
141281f0747Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
142fd9cb95cSsethg static void kb8042_cleanup(struct kb8042 *kb8042);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = {
1457c478bd9Sstevel@tonic-gate kb8042_streams_setled,
1467c478bd9Sstevel@tonic-gate kb8042_polled_setled,
1477c478bd9Sstevel@tonic-gate kb8042_polled_keycheck,
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate extern struct keyboard keyindex_pc;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate #define DRIVER_NAME(dip) ddi_driver_name(dip)
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate static char module_name[] = "kb8042";
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
1577c478bd9Sstevel@tonic-gate cred_t *credp);
1587c478bd9Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
1597c478bd9Sstevel@tonic-gate static int kb8042_wsrv();
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate struct module_info kb8042_sinfo = {
1627c478bd9Sstevel@tonic-gate 42, /* Module ID */
1637c478bd9Sstevel@tonic-gate module_name,
1647c478bd9Sstevel@tonic-gate 0, 32, /* Minimum and maximum packet sizes */
1657c478bd9Sstevel@tonic-gate 256, 128 /* High and low water marks */
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate static struct qinit kb8042_rinit = {
1697c478bd9Sstevel@tonic-gate NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1707c478bd9Sstevel@tonic-gate };
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static struct qinit kb8042_winit = {
1737c478bd9Sstevel@tonic-gate putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate struct streamtab
1777c478bd9Sstevel@tonic-gate kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate struct kb8042 Kdws = {0};
180fd9cb95cSsethg static dev_info_t *kb8042_dip = NULL;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1837c478bd9Sstevel@tonic-gate void **result);
1847c478bd9Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
185fd9cb95cSsethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate static struct cb_ops cb_kb8042_ops = {
1887c478bd9Sstevel@tonic-gate nulldev, /* cb_open */
1897c478bd9Sstevel@tonic-gate nulldev, /* cb_close */
1907c478bd9Sstevel@tonic-gate nodev, /* cb_strategy */
1917c478bd9Sstevel@tonic-gate nodev, /* cb_print */
1927c478bd9Sstevel@tonic-gate nodev, /* cb_dump */
1937c478bd9Sstevel@tonic-gate nodev, /* cb_read */
1947c478bd9Sstevel@tonic-gate nodev, /* cb_write */
1957c478bd9Sstevel@tonic-gate nodev, /* cb_ioctl */
1967c478bd9Sstevel@tonic-gate nodev, /* cb_devmap */
1977c478bd9Sstevel@tonic-gate nodev, /* cb_mmap */
1987c478bd9Sstevel@tonic-gate nodev, /* cb_segmap */
1997c478bd9Sstevel@tonic-gate nochpoll, /* cb_chpoll */
2007c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
2017c478bd9Sstevel@tonic-gate &kb8042_str_info, /* cb_stream */
2027c478bd9Sstevel@tonic-gate D_MP
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate struct dev_ops kb8042_ops = {
2067c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */
2077c478bd9Sstevel@tonic-gate 0, /* devo_refcnt */
2087c478bd9Sstevel@tonic-gate kb8042_getinfo, /* devo_getinfo */
2097c478bd9Sstevel@tonic-gate nulldev, /* devo_identify */
2107c478bd9Sstevel@tonic-gate nulldev, /* devo_probe */
2117c478bd9Sstevel@tonic-gate kb8042_attach, /* devo_attach */
212fd9cb95cSsethg kb8042_detach, /* devo_detach */
2137c478bd9Sstevel@tonic-gate nodev, /* devo_reset */
2147c478bd9Sstevel@tonic-gate &cb_kb8042_ops, /* devo_cb_ops */
21519397407SSherry Moore (struct bus_ops *)NULL, /* devo_bus_ops */
21619397407SSherry Moore NULL, /* devo_power */
21719397407SSherry Moore ddi_quiesce_not_needed, /* devo_quiesce */
2187c478bd9Sstevel@tonic-gate };
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * This is the loadable module wrapper.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2307c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */
231bb2d7d5eSSeth Goldberg "PS/2 keyboard driver",
2327c478bd9Sstevel@tonic-gate &kb8042_ops, /* driver ops */
2337c478bd9Sstevel@tonic-gate };
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2367c478bd9Sstevel@tonic-gate MODREV_1,
2377c478bd9Sstevel@tonic-gate (void *) &modldrv,
2387c478bd9Sstevel@tonic-gate NULL
2397c478bd9Sstevel@tonic-gate };
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate int
_init(void)2427c478bd9Sstevel@tonic-gate _init(void)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate int rv;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate rv = mod_install(&modlinkage);
2477c478bd9Sstevel@tonic-gate return (rv);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate int
_fini(void)2527c478bd9Sstevel@tonic-gate _fini(void)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2587c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
263fd9cb95cSsethg #ifdef __sparc
264fd9cb95cSsethg static boolean_t
kb8042_is_input_avail(struct kb8042 * kb8042,int timeout_usec,boolean_t polled)265fd9cb95cSsethg kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
266fd9cb95cSsethg {
267fd9cb95cSsethg int i;
268fd9cb95cSsethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
269fd9cb95cSsethg I8042_INT_INPUT_AVAIL;
270fd9cb95cSsethg int reps = timeout_usec / USECS_PER_WAIT;
271fd9cb95cSsethg
272fd9cb95cSsethg for (i = 0; i < reps; i++) {
273fd9cb95cSsethg if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
274fd9cb95cSsethg return (B_TRUE);
275fd9cb95cSsethg
276fd9cb95cSsethg if (i < (reps - 1))
277fd9cb95cSsethg drv_usecwait(USECS_PER_WAIT);
278fd9cb95cSsethg }
279fd9cb95cSsethg return (B_FALSE);
280fd9cb95cSsethg }
281fd9cb95cSsethg
282fd9cb95cSsethg static void
kb8042_clear_input_buffer(struct kb8042 * kb8042,boolean_t polled)283fd9cb95cSsethg kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
284fd9cb95cSsethg {
285fd9cb95cSsethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
286fd9cb95cSsethg I8042_INT_INPUT_DATA;
287fd9cb95cSsethg
288fd9cb95cSsethg while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
289fd9cb95cSsethg (void) ddi_get8(kb8042->handle, kb8042->addr + port);
290fd9cb95cSsethg }
291fd9cb95cSsethg }
292fd9cb95cSsethg
29315bfc6b7SSeth Goldberg /*
29415bfc6b7SSeth Goldberg * kb8042_send_and_expect does all its I/O via polling interfaces
29515bfc6b7SSeth Goldberg */
296fd9cb95cSsethg static boolean_t
kb8042_send_and_expect(struct kb8042 * kb8042,uint8_t send,uint8_t expect,int timeout,int * error,uint8_t * got)297fd9cb95cSsethg kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
29815bfc6b7SSeth Goldberg int timeout, int *error, uint8_t *got)
299fd9cb95cSsethg {
300fd9cb95cSsethg uint8_t datab;
301fd9cb95cSsethg int err;
302fd9cb95cSsethg boolean_t rval;
303fd9cb95cSsethg
30415bfc6b7SSeth Goldberg ddi_put8(kb8042->handle,
30515bfc6b7SSeth Goldberg kb8042->addr + I8042_POLL_OUTPUT_DATA, send);
306fd9cb95cSsethg
30715bfc6b7SSeth Goldberg if (kb8042_is_input_avail(kb8042, timeout, B_TRUE)) {
308fd9cb95cSsethg err = 0;
30915bfc6b7SSeth Goldberg datab = ddi_get8(kb8042->handle,
31015bfc6b7SSeth Goldberg kb8042->addr + I8042_POLL_INPUT_DATA);
311fd9cb95cSsethg rval = ((datab == expect) ? B_TRUE : B_FALSE);
312fd9cb95cSsethg } else {
313fd9cb95cSsethg err = EAGAIN;
314fd9cb95cSsethg rval = B_FALSE;
315fd9cb95cSsethg }
316fd9cb95cSsethg
317fd9cb95cSsethg if (error != NULL)
318fd9cb95cSsethg *error = err;
319fd9cb95cSsethg if (got != NULL)
320fd9cb95cSsethg *got = datab;
321fd9cb95cSsethg return (rval);
322fd9cb95cSsethg }
323fd9cb95cSsethg
324fd9cb95cSsethg static const char *
kb8042_error_string(int errcode)325fd9cb95cSsethg kb8042_error_string(int errcode)
326fd9cb95cSsethg {
327fd9cb95cSsethg switch (errcode) {
328fd9cb95cSsethg case EAGAIN:
329fd9cb95cSsethg return ("Timed out");
330fd9cb95cSsethg default:
331fd9cb95cSsethg return ("Unknown error");
332fd9cb95cSsethg }
333fd9cb95cSsethg }
334fd9cb95cSsethg
33515bfc6b7SSeth Goldberg /*
33615bfc6b7SSeth Goldberg * kb8042_read_scanset works properly because it is called before ddi_add_intr
33715bfc6b7SSeth Goldberg * (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
33815bfc6b7SSeth Goldberg * instead of just storing the data that comes in from the keyboard, which
33915bfc6b7SSeth Goldberg * would prevent the code here from getting it.)
34015bfc6b7SSeth Goldberg */
341fd9cb95cSsethg static int
kb8042_read_scanset(struct kb8042 * kb8042)34215bfc6b7SSeth Goldberg kb8042_read_scanset(struct kb8042 *kb8042)
343fd9cb95cSsethg {
344fd9cb95cSsethg int scanset = -1;
345fd9cb95cSsethg int err;
346fd9cb95cSsethg uint8_t got;
347fd9cb95cSsethg
348fd9cb95cSsethg kb8042_clear_input_buffer(kb8042, B_TRUE);
349fd9cb95cSsethg
350fd9cb95cSsethg /*
351fd9cb95cSsethg * Send a "change scan code set" command to the keyboard.
352fd9cb95cSsethg * It should respond with an ACK.
353fd9cb95cSsethg */
35415bfc6b7SSeth Goldberg if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, MAX_WAIT_USECS,
35515bfc6b7SSeth Goldberg &err, &got) != B_TRUE) {
356fd9cb95cSsethg goto fail_read_scanset;
357fd9cb95cSsethg }
358fd9cb95cSsethg
359fd9cb95cSsethg /*
360bb2d7d5eSSeth Goldberg * Send a 0. The keyboard should ACK the 0, then it should send the
361bb2d7d5eSSeth Goldberg * scan code set in use.
362fd9cb95cSsethg */
36315bfc6b7SSeth Goldberg if (kb8042_send_and_expect(kb8042, 0, KB_ACK, MAX_WAIT_USECS, &err,
36415bfc6b7SSeth Goldberg &got) != B_TRUE) {
365fd9cb95cSsethg goto fail_read_scanset;
366fd9cb95cSsethg }
367fd9cb95cSsethg
368fd9cb95cSsethg /*
369fd9cb95cSsethg * The next input byte from the keyboard should be the scan code
370fd9cb95cSsethg * set in use, though some keyboards like to send a few more acks
371fd9cb95cSsethg * just for fun, so blow past those to get the keyboard scan code.
372fd9cb95cSsethg */
373fd9cb95cSsethg while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
37415bfc6b7SSeth Goldberg (scanset = ddi_get8(kb8042->handle,
37515bfc6b7SSeth Goldberg kb8042->addr + I8042_POLL_INPUT_DATA)) == KB_ACK)
376fd9cb95cSsethg ;
377fd9cb95cSsethg
37815bfc6b7SSeth Goldberg #ifdef DEBUG
379fd9cb95cSsethg cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
380fd9cb95cSsethg scanset);
381fd9cb95cSsethg #endif
382fd9cb95cSsethg
383fd9cb95cSsethg return (scanset);
384fd9cb95cSsethg
385fd9cb95cSsethg fail_read_scanset:
38615bfc6b7SSeth Goldberg #ifdef DEBUG
387fd9cb95cSsethg if (err == 0)
388fd9cb95cSsethg cmn_err(CE_NOTE, "Could not read current scan set from "
389fd9cb95cSsethg "keyboard: %s. (Expected 0x%x, but got 0x%x).",
390fd9cb95cSsethg kb8042_error_string(err), KB_ACK, got);
391fd9cb95cSsethg else
392fd9cb95cSsethg cmn_err(CE_NOTE, "Could not read current scan set from "
393fd9cb95cSsethg "keyboard: %s.", kb8042_error_string(err));
394fd9cb95cSsethg #endif
395fd9cb95cSsethg return (-1);
396fd9cb95cSsethg }
397fd9cb95cSsethg #endif
398fd9cb95cSsethg
3997c478bd9Sstevel@tonic-gate static int
kb8042_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4007c478bd9Sstevel@tonic-gate kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate int rc;
403fd9cb95cSsethg int scanset;
404fd9cb95cSsethg int leds;
4057c478bd9Sstevel@tonic-gate
406fd9cb95cSsethg struct kb8042 *kb8042 = &Kdws;
4077c478bd9Sstevel@tonic-gate static ddi_device_acc_attr_t attr = {
4087c478bd9Sstevel@tonic-gate DDI_DEVICE_ATTR_V0,
4097c478bd9Sstevel@tonic-gate DDI_NEVERSWAP_ACC,
4107c478bd9Sstevel@tonic-gate DDI_STRICTORDER_ACC,
4117c478bd9Sstevel@tonic-gate };
4127c478bd9Sstevel@tonic-gate
413fd9cb95cSsethg switch (cmd) {
414fd9cb95cSsethg case DDI_RESUME:
415fd9cb95cSsethg leds = kb8042->leds.commanded;
416fd9cb95cSsethg kb8042->w_init = 0;
417fd9cb95cSsethg kb8042_init(kb8042, B_TRUE);
418fd9cb95cSsethg kb8042_setled(kb8042, leds, B_FALSE);
4192df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
4202df1fe9cSrandyf kb8042->suspended = B_FALSE;
4212df1fe9cSrandyf if (kb8042->w_qp != NULL) {
4222df1fe9cSrandyf enableok(WR(kb8042->w_qp));
4232df1fe9cSrandyf qenable(WR(kb8042->w_qp));
4242df1fe9cSrandyf }
4252df1fe9cSrandyf cv_broadcast(&kb8042->suspend_cv);
4262df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
427fd9cb95cSsethg return (DDI_SUCCESS);
428fd9cb95cSsethg
429fd9cb95cSsethg case DDI_ATTACH:
430fd9cb95cSsethg if (kb8042_dip != NULL)
4317c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
432fd9cb95cSsethg /* The rest of the function is for attach */
433fd9cb95cSsethg break;
4347c478bd9Sstevel@tonic-gate
435fd9cb95cSsethg default:
436fd9cb95cSsethg return (DDI_FAILURE);
437fd9cb95cSsethg }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate kb8042->debugger.mod1 = 58; /* Left Ctrl */
4407c478bd9Sstevel@tonic-gate kb8042->debugger.mod2 = 60; /* Left Alt */
4417c478bd9Sstevel@tonic-gate kb8042->debugger.trigger = 33; /* D */
4427c478bd9Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE;
4437c478bd9Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE;
4447c478bd9Sstevel@tonic-gate kb8042->debugger.enabled = B_FALSE;
4457c478bd9Sstevel@tonic-gate
446fd9cb95cSsethg kb8042_dip = devi;
447fd9cb95cSsethg kb8042->init_state = KB8042_UNINITIALIZED;
448fd9cb95cSsethg
4497c478bd9Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE;
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
4527c478bd9Sstevel@tonic-gate DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
453fd9cb95cSsethg goto failure;
4547c478bd9Sstevel@tonic-gate }
455fd9cb95cSsethg
456fd9cb95cSsethg kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
4597c478bd9Sstevel@tonic-gate (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
4607c478bd9Sstevel@tonic-gate if (rc != DDI_SUCCESS) {
46115bfc6b7SSeth Goldberg #ifdef DEBUG
4627c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: can't map registers");
4637c478bd9Sstevel@tonic-gate #endif
464fd9cb95cSsethg goto failure;
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
467fd9cb95cSsethg kb8042->init_state |= KB8042_REGS_MAPPED;
468fd9cb95cSsethg
4697c478bd9Sstevel@tonic-gate if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
4707c478bd9Sstevel@tonic-gate DDI_SUCCESS) {
4717c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie");
472fd9cb95cSsethg goto failure;
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
4762df1fe9cSrandyf cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
4772df1fe9cSrandyf cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
47815bfc6b7SSeth Goldberg cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
479fd9cb95cSsethg kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
480fd9cb95cSsethg
481fd9cb95cSsethg kb8042_init(kb8042, B_FALSE);
482fd9cb95cSsethg
483fd9cb95cSsethg #ifdef __sparc
484fd9cb95cSsethg /* Detect the scan code set currently in use */
48515bfc6b7SSeth Goldberg scanset = kb8042_read_scanset(kb8042);
486fd9cb95cSsethg
487fd9cb95cSsethg if (scanset < 0 && kb8042_warn_unknown_scanset) {
488fd9cb95cSsethg
489fd9cb95cSsethg cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
490fd9cb95cSsethg cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
491fd9cb95cSsethg cmn_err(CE_CONT, "Defaulting to scan code set %d. If the "
492fd9cb95cSsethg "keyboard does not ", kb8042_default_scanset);
493fd9cb95cSsethg cmn_err(CE_CONT, "work properly, add "
494fd9cb95cSsethg "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
495fd9cb95cSsethg (kb8042_default_scanset == 1) ? 2 : 1);
496fd9cb95cSsethg cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
497fd9cb95cSsethg "restart the system. If you ");
498fd9cb95cSsethg cmn_err(CE_CONT, "do not want to see this message in the "
499fd9cb95cSsethg "future, add ");
500fd9cb95cSsethg cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
501fd9cb95cSsethg "to /etc/system.\n");
502fd9cb95cSsethg
503fd9cb95cSsethg /* Use the default scan code set. */
504fd9cb95cSsethg scanset = kb8042_default_scanset;
505fd9cb95cSsethg }
506fd9cb95cSsethg #else
507fd9cb95cSsethg /* x86 systems use scan code set 1 -- no detection required */
508fd9cb95cSsethg scanset = 1;
509fd9cb95cSsethg #endif
510fd9cb95cSsethg if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
511fd9cb95cSsethg cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
512fd9cb95cSsethg "Unknown scan code set `%d'.", scanset);
513fd9cb95cSsethg /* Scan code set is not supported */
514fd9cb95cSsethg goto failure;
515fd9cb95cSsethg }
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate * Turn on interrupts...
5197c478bd9Sstevel@tonic-gate */
5207c478bd9Sstevel@tonic-gate if (ddi_add_intr(devi, 0,
5217c478bd9Sstevel@tonic-gate &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
5227c478bd9Sstevel@tonic-gate kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
5237c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
524fd9cb95cSsethg goto failure;
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
527fd9cb95cSsethg kb8042->init_state |= KB8042_INTR_ADDED;
528fd9cb95cSsethg
5297c478bd9Sstevel@tonic-gate ddi_report_dev(devi);
530fd9cb95cSsethg
53115bfc6b7SSeth Goldberg #ifdef DEBUG
532bb2d7d5eSSeth Goldberg cmn_err(CE_CONT, "?%s instance #%d READY\n",
53319397407SSherry Moore DRIVER_NAME(devi), ddi_get_instance(devi));
5347c478bd9Sstevel@tonic-gate #endif
535fd9cb95cSsethg
5367c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
537fd9cb95cSsethg
538fd9cb95cSsethg failure:
539fd9cb95cSsethg kb8042_cleanup(kb8042);
540fd9cb95cSsethg return (DDI_FAILURE);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate
543fd9cb95cSsethg static int
kb8042_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)544fd9cb95cSsethg kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
545fd9cb95cSsethg {
546fd9cb95cSsethg struct kb8042 *kb8042 = &Kdws;
547fd9cb95cSsethg
548fd9cb95cSsethg switch (cmd) {
549fd9cb95cSsethg case DDI_SUSPEND:
5502df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
5512df1fe9cSrandyf ASSERT(kb8042->ops >= 0);
5522df1fe9cSrandyf while (kb8042->ops > 0)
5532df1fe9cSrandyf cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
5542df1fe9cSrandyf kb8042->suspended = B_TRUE;
5552df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
556fd9cb95cSsethg return (DDI_SUCCESS);
557fd9cb95cSsethg
558fd9cb95cSsethg case DDI_DETACH:
559fd9cb95cSsethg /* If someone has a stream open, fail to detach */
560fd9cb95cSsethg if (kb8042->w_qp != NULL)
561fd9cb95cSsethg return (DDI_FAILURE);
562fd9cb95cSsethg
563fd9cb95cSsethg ASSERT(kb8042_dip == dip);
564fd9cb95cSsethg
565fd9cb95cSsethg kb8042_cleanup(kb8042);
566fd9cb95cSsethg
567fd9cb95cSsethg return (DDI_SUCCESS);
568fd9cb95cSsethg
569fd9cb95cSsethg default:
570fd9cb95cSsethg return (DDI_FAILURE);
571fd9cb95cSsethg }
572fd9cb95cSsethg }
573fd9cb95cSsethg
5747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5757c478bd9Sstevel@tonic-gate static int
kb8042_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)5767c478bd9Sstevel@tonic-gate kb8042_getinfo(
5777c478bd9Sstevel@tonic-gate dev_info_t *dip,
5787c478bd9Sstevel@tonic-gate ddi_info_cmd_t infocmd,
5797c478bd9Sstevel@tonic-gate void *arg,
5807c478bd9Sstevel@tonic-gate void **result)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate register int error;
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate switch (infocmd) {
5857c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
5867c478bd9Sstevel@tonic-gate if (kb8042_dip == NULL) {
5877c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
5887c478bd9Sstevel@tonic-gate } else {
5897c478bd9Sstevel@tonic-gate *result = (void *) kb8042_dip;
5907c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate break;
5937c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
5947c478bd9Sstevel@tonic-gate *result = (void *)0;
5957c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
5967c478bd9Sstevel@tonic-gate break;
5977c478bd9Sstevel@tonic-gate default:
5987c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
5997c478bd9Sstevel@tonic-gate break;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate return (error);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate static void
kb8042_cleanup(struct kb8042 * kb8042)605fd9cb95cSsethg kb8042_cleanup(struct kb8042 *kb8042)
606fd9cb95cSsethg {
6070eccbe1bSsethg ASSERT(kb8042_dip != NULL);
608fd9cb95cSsethg
60915bfc6b7SSeth Goldberg if (kb8042->init_state & KB8042_INTR_ADDED)
61015bfc6b7SSeth Goldberg ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
61115bfc6b7SSeth Goldberg
6122df1fe9cSrandyf if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
61315bfc6b7SSeth Goldberg cv_destroy(&kb8042->cmd_cv);
6142df1fe9cSrandyf cv_destroy(&kb8042->suspend_cv);
6152df1fe9cSrandyf cv_destroy(&kb8042->ops_cv);
616fd9cb95cSsethg mutex_destroy(&kb8042->w_hw_mutex);
6172df1fe9cSrandyf }
618fd9cb95cSsethg
619fd9cb95cSsethg if (kb8042->init_state & KB8042_REGS_MAPPED)
620fd9cb95cSsethg ddi_regs_map_free(&kb8042->handle);
621fd9cb95cSsethg
622fd9cb95cSsethg if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
623fd9cb95cSsethg ddi_remove_minor_node(kb8042_dip, NULL);
624fd9cb95cSsethg
625fd9cb95cSsethg kb8042->init_state = KB8042_UNINITIALIZED;
6260eccbe1bSsethg kb8042_dip = NULL;
627fd9cb95cSsethg }
628fd9cb95cSsethg
629fd9cb95cSsethg static void
kb8042_init(struct kb8042 * kb8042,boolean_t from_resume)630fd9cb95cSsethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate if (kb8042->w_init)
6337c478bd9Sstevel@tonic-gate return;
6347c478bd9Sstevel@tonic-gate
635fd9cb95cSsethg if (!from_resume) {
6367c478bd9Sstevel@tonic-gate kb8042->w_kblayout = 0; /* Default to US */
6377c478bd9Sstevel@tonic-gate kb8042->w_qp = (queue_t *)NULL;
638fd9cb95cSsethg kb8042->simulated_kbd_type = KB_PC;
639fd9cb95cSsethg kb8042->leds.commanded = -1; /* Unknown initial state */
640fd9cb95cSsethg kb8042->leds.desired = -1; /* Unknown initial state */
641fd9cb95cSsethg }
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate kb8042_wait_poweron(kb8042);
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate kb8042->kb_old_key_pos = 0;
6467c478bd9Sstevel@tonic-gate
64715bfc6b7SSeth Goldberg /*
64815bfc6b7SSeth Goldberg * Explicitly grab and release the 8042 lock outside of
64915bfc6b7SSeth Goldberg * kb8042_send_to_keyboard, because this is the only situation
65015bfc6b7SSeth Goldberg * where a polling interface is used with locking required.
65115bfc6b7SSeth Goldberg */
65215bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
6537c478bd9Sstevel@tonic-gate /* Set up the command state machine and start it running. */
65415bfc6b7SSeth Goldberg kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
65515bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate kb8042->w_init++;
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
6637c478bd9Sstevel@tonic-gate static int
kb8042_open(queue_t * qp,dev_t * devp,int flag,int sflag,cred_t * credp)6647c478bd9Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate struct kb8042 *kb8042;
667*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China int err = 0;
6687c478bd9Sstevel@tonic-gate int initial_leds;
6697c478bd9Sstevel@tonic-gate int initial_led_mask;
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate kb8042 = &Kdws;
6727c478bd9Sstevel@tonic-gate
6732df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
674*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China if (qp->q_ptr) {
675*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China kb8042->w_dev = *devp;
676*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China mutex_exit(&kb8042->w_hw_mutex);
677*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China return (0);
678*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China }
679*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China
680*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China if (secpolicy_console(credp) != 0) {
681*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China mutex_exit(&kb8042->w_hw_mutex);
682*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China return (EPERM);
683*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China }
684*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China
6852df1fe9cSrandyf while (kb8042->suspended) {
6862df1fe9cSrandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
6872df1fe9cSrandyf 0) {
6882df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
6892df1fe9cSrandyf return (EINTR);
6902df1fe9cSrandyf }
6912df1fe9cSrandyf }
6922df1fe9cSrandyf
6937c478bd9Sstevel@tonic-gate kb8042->w_dev = *devp;
6947c478bd9Sstevel@tonic-gate qp->q_ptr = (caddr_t)kb8042;
6957c478bd9Sstevel@tonic-gate WR(qp)->q_ptr = qp->q_ptr;
6967c478bd9Sstevel@tonic-gate if (!kb8042->w_qp)
6977c478bd9Sstevel@tonic-gate kb8042->w_qp = qp;
6987c478bd9Sstevel@tonic-gate
6992df1fe9cSrandyf ASSERT(kb8042->ops >= 0);
7002df1fe9cSrandyf kb8042->ops++;
7012df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7022df1fe9cSrandyf
7037c478bd9Sstevel@tonic-gate kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
704*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China err = kbtrans_streams_init(qp, sflag,
7057c478bd9Sstevel@tonic-gate (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
7067c478bd9Sstevel@tonic-gate &kb8042->hw_kbtrans,
7077c478bd9Sstevel@tonic-gate initial_leds, initial_led_mask);
7087c478bd9Sstevel@tonic-gate if (err != 0)
709*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China goto out;
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
7147c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_argument =
715281f0747Slt200341 (cons_polledio_arg_t)kb8042;
7167c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_putchar = NULL;
7177c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_getchar =
718281f0747Slt200341 (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
7197c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_ischar =
720281f0747Slt200341 (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
7217c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_enter = NULL;
7227c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_exit = NULL;
7237c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_setled =
724281f0747Slt200341 (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
7257c478bd9Sstevel@tonic-gate kb8042->polledio.cons_polledio_keycheck =
726281f0747Slt200341 (boolean_t (*)(cons_polledio_arg_t, int *,
7277c478bd9Sstevel@tonic-gate enum keystate *))kb8042_polled_keycheck;
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate qprocson(qp);
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate kbtrans_streams_enable(kb8042->hw_kbtrans);
7327c478bd9Sstevel@tonic-gate
733*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China out:
7342df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
7352df1fe9cSrandyf ASSERT(kb8042->ops > 0);
7362df1fe9cSrandyf kb8042->ops--;
7372df1fe9cSrandyf if (kb8042->ops == 0)
7382df1fe9cSrandyf cv_broadcast(&kb8042->ops_cv);
7392df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7402df1fe9cSrandyf
741*cd2135d0Spengcheng chen - Sun Microsystems - Beijing China return (err);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
7457c478bd9Sstevel@tonic-gate static int
kb8042_close(queue_t * qp,int flag,cred_t * credp)7467c478bd9Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate struct kb8042 *kb8042;
7497c478bd9Sstevel@tonic-gate
750c35aa225Smarx /* If a beep is in progress, stop that */
751c35aa225Smarx (void) beeper_off();
752c35aa225Smarx
7537c478bd9Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr;
7547c478bd9Sstevel@tonic-gate
7552df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
7562df1fe9cSrandyf while (kb8042->suspended) {
7572df1fe9cSrandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
7582df1fe9cSrandyf 0) {
7592df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7602df1fe9cSrandyf return (EINTR);
7612df1fe9cSrandyf }
7622df1fe9cSrandyf }
7632df1fe9cSrandyf
7642df1fe9cSrandyf ASSERT(kb8042->ops >= 0);
7652df1fe9cSrandyf kb8042->ops++;
7662df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7672df1fe9cSrandyf
768e1fec4f9Sry162471 (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
769e1fec4f9Sry162471
7707c478bd9Sstevel@tonic-gate kb8042->w_qp = (queue_t *)NULL;
7717c478bd9Sstevel@tonic-gate qprocsoff(qp);
7727c478bd9Sstevel@tonic-gate
7732df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
7742df1fe9cSrandyf ASSERT(kb8042->ops > 0);
7752df1fe9cSrandyf kb8042->ops--;
7762df1fe9cSrandyf if (kb8042->ops == 0)
7772df1fe9cSrandyf cv_broadcast(&kb8042->ops_cv);
7782df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7792df1fe9cSrandyf
7807c478bd9Sstevel@tonic-gate return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate static int
kb8042_wsrv(queue_t * qp)7847c478bd9Sstevel@tonic-gate kb8042_wsrv(queue_t *qp)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate struct kb8042 *kb8042;
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate mblk_t *mp;
7892df1fe9cSrandyf boolean_t suspended;
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr;
7927c478bd9Sstevel@tonic-gate
7932df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
7942df1fe9cSrandyf suspended = kb8042->suspended;
7952df1fe9cSrandyf ASSERT(kb8042->ops >= 0);
7962df1fe9cSrandyf if (!suspended)
7972df1fe9cSrandyf kb8042->ops++;
7982df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
7992df1fe9cSrandyf
8002df1fe9cSrandyf #ifdef NO_KB_DEBUG
8012df1fe9cSrandyf while (!suspended && (mp = getq(qp)) != NULL) {
8022df1fe9cSrandyf #else
8032df1fe9cSrandyf /*
8042df1fe9cSrandyf * Not taking keyboard input while suspending can make debugging
8052df1fe9cSrandyf * difficult. However, we still do the ops counting so that we
8062df1fe9cSrandyf * don't suspend at a bad time.
8072df1fe9cSrandyf */
8087c478bd9Sstevel@tonic-gate while ((mp = getq(qp))) {
8092df1fe9cSrandyf #endif
8107c478bd9Sstevel@tonic-gate switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
8117c478bd9Sstevel@tonic-gate case KBTRANS_MESSAGE_HANDLED:
8127c478bd9Sstevel@tonic-gate continue;
8137c478bd9Sstevel@tonic-gate case KBTRANS_MESSAGE_NOT_HANDLED:
8147c478bd9Sstevel@tonic-gate break;
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
8177c478bd9Sstevel@tonic-gate case M_IOCTL:
8187c478bd9Sstevel@tonic-gate kb8042_ioctlmsg(kb8042, qp, mp);
8197c478bd9Sstevel@tonic-gate continue;
8207c478bd9Sstevel@tonic-gate case M_IOCDATA:
8217c478bd9Sstevel@tonic-gate kb8042_iocdatamsg(qp, mp);
8227c478bd9Sstevel@tonic-gate continue;
8237c478bd9Sstevel@tonic-gate case M_DELAY:
8247c478bd9Sstevel@tonic-gate case M_STARTI:
8257c478bd9Sstevel@tonic-gate case M_STOPI:
8267c478bd9Sstevel@tonic-gate case M_READ: /* ignore, no buffered data */
8277c478bd9Sstevel@tonic-gate freemsg(mp);
8287c478bd9Sstevel@tonic-gate continue;
8297c478bd9Sstevel@tonic-gate case M_FLUSH:
8307c478bd9Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW;
8317c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR)
8327c478bd9Sstevel@tonic-gate qreply(qp, mp);
8337c478bd9Sstevel@tonic-gate else
8347c478bd9Sstevel@tonic-gate freemsg(mp);
8357c478bd9Sstevel@tonic-gate continue;
8367c478bd9Sstevel@tonic-gate default:
8377c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
8387c478bd9Sstevel@tonic-gate mp->b_datap->db_type);
8397c478bd9Sstevel@tonic-gate freemsg(mp);
8407c478bd9Sstevel@tonic-gate continue;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate }
8432df1fe9cSrandyf
8442df1fe9cSrandyf mutex_enter(&kb8042->w_hw_mutex);
8452df1fe9cSrandyf if (!suspended) {
8462df1fe9cSrandyf ASSERT(kb8042->ops > 0);
8472df1fe9cSrandyf kb8042->ops--;
8482df1fe9cSrandyf if (kb8042->ops == 0)
8492df1fe9cSrandyf cv_broadcast(&kb8042->ops_cv);
8502df1fe9cSrandyf }
8512df1fe9cSrandyf mutex_exit(&kb8042->w_hw_mutex);
8522df1fe9cSrandyf
8537c478bd9Sstevel@tonic-gate return (0);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate static void
8577c478bd9Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate struct iocblk *iocp;
8607c478bd9Sstevel@tonic-gate mblk_t *datap;
8617c478bd9Sstevel@tonic-gate int error;
8627c478bd9Sstevel@tonic-gate int tmp;
863c35aa225Smarx int cycles;
864c35aa225Smarx int frequency;
865c35aa225Smarx int msecs;
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate case CONSOPENPOLLEDIO:
8727c478bd9Sstevel@tonic-gate error = miocpullup(mp, sizeof (struct cons_polledio *));
8737c478bd9Sstevel@tonic-gate if (error != 0) {
8747c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, error);
8757c478bd9Sstevel@tonic-gate return;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate * We are given an appropriate-sized data block,
8807c478bd9Sstevel@tonic-gate * and return a pointer to our structure in it.
8817c478bd9Sstevel@tonic-gate */
8827c478bd9Sstevel@tonic-gate *(struct cons_polledio **)mp->b_cont->b_rptr =
8837c478bd9Sstevel@tonic-gate &kb8042->polledio;
8847c478bd9Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
8857c478bd9Sstevel@tonic-gate iocp->ioc_error = 0;
8867c478bd9Sstevel@tonic-gate qreply(qp, mp);
8877c478bd9Sstevel@tonic-gate break;
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate case CONSCLOSEPOLLEDIO:
8907c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
8917c478bd9Sstevel@tonic-gate break;
8927c478bd9Sstevel@tonic-gate
8937c478bd9Sstevel@tonic-gate case CONSSETABORTENABLE:
8947c478bd9Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) {
8957c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
8967c478bd9Sstevel@tonic-gate return;
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
9007c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9017c478bd9Sstevel@tonic-gate break;
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate * Valid only in TR_UNTRANS_MODE mode.
9057c478bd9Sstevel@tonic-gate */
9067c478bd9Sstevel@tonic-gate case CONSSETKBDTYPE:
9077c478bd9Sstevel@tonic-gate error = miocpullup(mp, sizeof (int));
9087c478bd9Sstevel@tonic-gate if (error != 0) {
9097c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, error);
9107c478bd9Sstevel@tonic-gate return;
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate tmp = *(int *)mp->b_cont->b_rptr;
9137c478bd9Sstevel@tonic-gate if (tmp != KB_PC && tmp != KB_USB) {
9147c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9157c478bd9Sstevel@tonic-gate break;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate kb8042->simulated_kbd_type = tmp;
9187c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9197c478bd9Sstevel@tonic-gate break;
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate case KIOCLAYOUT:
9227c478bd9Sstevel@tonic-gate if (kb8042->w_kblayout == -1) {
9237c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9247c478bd9Sstevel@tonic-gate return;
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9287c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, ENOMEM);
9297c478bd9Sstevel@tonic-gate return;
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB)
9337c478bd9Sstevel@tonic-gate *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9347c478bd9Sstevel@tonic-gate else
9357c478bd9Sstevel@tonic-gate *(int *)datap->b_wptr = kb8042->w_kblayout;
9367c478bd9Sstevel@tonic-gate
9377c478bd9Sstevel@tonic-gate datap->b_wptr += sizeof (int);
9387c478bd9Sstevel@tonic-gate if (mp->b_cont)
9397c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
9407c478bd9Sstevel@tonic-gate mp->b_cont = datap;
9417c478bd9Sstevel@tonic-gate iocp->ioc_count = sizeof (int);
9427c478bd9Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
9437c478bd9Sstevel@tonic-gate iocp->ioc_error = 0;
9447c478bd9Sstevel@tonic-gate qreply(qp, mp);
9457c478bd9Sstevel@tonic-gate break;
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate case KIOCSLAYOUT:
9487c478bd9Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) {
9497c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9507c478bd9Sstevel@tonic-gate return;
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
9547c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9557c478bd9Sstevel@tonic-gate break;
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate case KIOCCMD:
9587c478bd9Sstevel@tonic-gate error = miocpullup(mp, sizeof (int));
9597c478bd9Sstevel@tonic-gate if (error != 0) {
9607c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, error);
9617c478bd9Sstevel@tonic-gate return;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
9657c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9667c478bd9Sstevel@tonic-gate break;
9677c478bd9Sstevel@tonic-gate
968c35aa225Smarx case KIOCMKTONE:
969c35aa225Smarx if (iocp->ioc_count != TRANSPARENT) {
970c35aa225Smarx miocnak(qp, mp, 0, EINVAL);
971c35aa225Smarx return;
972c35aa225Smarx }
973c35aa225Smarx
974c35aa225Smarx tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
975c35aa225Smarx cycles = tmp & 0xffff;
976c35aa225Smarx msecs = (tmp >> 16) & 0xffff;
977c35aa225Smarx
978c35aa225Smarx if (cycles == 0)
979c35aa225Smarx frequency = UINT16_MAX;
980c35aa225Smarx else if (cycles == UINT16_MAX)
981c35aa225Smarx frequency = 0;
982c35aa225Smarx else {
983c35aa225Smarx frequency = (PIT_HZ + cycles / 2) / cycles;
984c35aa225Smarx if (frequency > UINT16_MAX)
985c35aa225Smarx frequency = UINT16_MAX;
986c35aa225Smarx }
987c35aa225Smarx
988c35aa225Smarx error = beep_mktone(frequency, msecs);
989c35aa225Smarx if (error != 0)
990c35aa225Smarx miocnak(qp, mp, 0, error);
991c35aa225Smarx else
992c35aa225Smarx miocack(qp, mp, 0, 0);
993c35aa225Smarx break;
994c35aa225Smarx
9957c478bd9Sstevel@tonic-gate default:
9967c478bd9Sstevel@tonic-gate #ifdef DEBUG1
9977c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
9987c478bd9Sstevel@tonic-gate #endif
9997c478bd9Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
10007c478bd9Sstevel@tonic-gate return;
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate * Process a byte received from the keyboard
10067c478bd9Sstevel@tonic-gate */
10077c478bd9Sstevel@tonic-gate static void
10087c478bd9Sstevel@tonic-gate kb8042_received_byte(
10097c478bd9Sstevel@tonic-gate struct kb8042 *kb8042,
10107c478bd9Sstevel@tonic-gate int scancode) /* raw scan code */
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate boolean_t legit; /* is this a legit key pos'n? */
10137c478bd9Sstevel@tonic-gate int key_pos = -1;
10147c478bd9Sstevel@tonic-gate enum keystate state;
10157c478bd9Sstevel@tonic-gate boolean_t synthetic_release_needed;
10167c478bd9Sstevel@tonic-gate
101715bfc6b7SSeth Goldberg /*
101815bfc6b7SSeth Goldberg * Intercept ACK and RESEND and signal the condition that
101915bfc6b7SSeth Goldberg * kb8042_send_and_wait is waiting for.
102015bfc6b7SSeth Goldberg */
102115bfc6b7SSeth Goldberg if (scancode == KB_ACK) {
102215bfc6b7SSeth Goldberg mutex_enter(&kb8042->w_hw_mutex);
102315bfc6b7SSeth Goldberg kb8042->acked = 1;
102415bfc6b7SSeth Goldberg cv_signal(&kb8042->cmd_cv);
102515bfc6b7SSeth Goldberg mutex_exit(&kb8042->w_hw_mutex);
102615bfc6b7SSeth Goldberg return;
102715bfc6b7SSeth Goldberg } else if (scancode == KB_RESEND) {
102815bfc6b7SSeth Goldberg mutex_enter(&kb8042->w_hw_mutex);
102915bfc6b7SSeth Goldberg kb8042->need_retry = 1;
103015bfc6b7SSeth Goldberg cv_signal(&kb8042->cmd_cv);
103115bfc6b7SSeth Goldberg mutex_exit(&kb8042->w_hw_mutex);
103215bfc6b7SSeth Goldberg return;
103315bfc6b7SSeth Goldberg }
10347c478bd9Sstevel@tonic-gate
10357c478bd9Sstevel@tonic-gate if (!kb8042->w_init) /* can't do anything anyway */
10367c478bd9Sstevel@tonic-gate return;
10377c478bd9Sstevel@tonic-gate
10387c478bd9Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
10397c478bd9Sstevel@tonic-gate &synthetic_release_needed);
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate if (legit == 0) {
10427c478bd9Sstevel@tonic-gate /* Eaten by translation */
10437c478bd9Sstevel@tonic-gate return;
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate * Don't know if we want this permanently, but it seems interesting
10487c478bd9Sstevel@tonic-gate * for the moment.
10497c478bd9Sstevel@tonic-gate */
10507c478bd9Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod1) {
10517c478bd9Sstevel@tonic-gate kb8042->debugger.mod1_down = (state == KEY_PRESSED);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod2) {
10547c478bd9Sstevel@tonic-gate kb8042->debugger.mod2_down = (state == KEY_PRESSED);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate if (kb8042->debugger.enabled &&
10577c478bd9Sstevel@tonic-gate key_pos == kb8042->debugger.trigger &&
10587c478bd9Sstevel@tonic-gate kb8042->debugger.mod1_down &&
10597c478bd9Sstevel@tonic-gate kb8042->debugger.mod2_down) {
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate * Require new presses of the modifiers.
10627c478bd9Sstevel@tonic-gate */
10637c478bd9Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE;
10647c478bd9Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE;
10657c478bd9Sstevel@tonic-gate abort_sequence_enter(NULL);
10667c478bd9Sstevel@tonic-gate return;
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate * If there's no queue above us - as can happen if we've been
10717c478bd9Sstevel@tonic-gate * attached but not opened - drop the keystroke.
10727c478bd9Sstevel@tonic-gate * Note that we do this here instead of above so that
10737c478bd9Sstevel@tonic-gate * Ctrl-Alt-D still works.
10747c478bd9Sstevel@tonic-gate */
10757c478bd9Sstevel@tonic-gate if (kb8042->w_qp == NULL) {
10767c478bd9Sstevel@tonic-gate return;
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate
10797c478bd9Sstevel@tonic-gate /*
10807c478bd9Sstevel@tonic-gate * This is to filter out auto repeat since it can't be
10817c478bd9Sstevel@tonic-gate * turned off at the hardware. (Yeah, yeah, PS/2 keyboards
10827c478bd9Sstevel@tonic-gate * can. Don't know whether they've taken over the world.
10837c478bd9Sstevel@tonic-gate * Don't think so.)
10847c478bd9Sstevel@tonic-gate */
10857c478bd9Sstevel@tonic-gate if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
10867c478bd9Sstevel@tonic-gate return;
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate kb8042_process_key(kb8042, key_pos, state);
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate * This is a total hack. For some stupid reason, the two additional
10947c478bd9Sstevel@tonic-gate * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
10957c478bd9Sstevel@tonic-gate * only. We synthesize a release immediately.
10967c478bd9Sstevel@tonic-gate */
10977c478bd9Sstevel@tonic-gate if (synthetic_release_needed) {
10987c478bd9Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
10997c478bd9Sstevel@tonic-gate kb8042_process_key(kb8042, key_pos, state);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate static void
11057c478bd9Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
11067c478bd9Sstevel@tonic-gate enum keystate state)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate kbtrans_key_t key;
11097c478bd9Sstevel@tonic-gate
11107c478bd9Sstevel@tonic-gate ASSERT(key_pos >= 0 && key_pos <= 255);
11117c478bd9Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_PC) {
11127c478bd9Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
11137c478bd9Sstevel@tonic-gate } else if (kb8042->simulated_kbd_type == KB_USB) {
11147c478bd9Sstevel@tonic-gate key = keytab_pc2usb[key_pos];
11157c478bd9Sstevel@tonic-gate if (key != 0) {
11167c478bd9Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate * Called from interrupt handler when keyboard interrupt occurs.
11237c478bd9Sstevel@tonic-gate */
11247c478bd9Sstevel@tonic-gate static uint_t
11257c478bd9Sstevel@tonic-gate kb8042_intr(caddr_t arg)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate uchar_t scancode; /* raw scan code */
11287c478bd9Sstevel@tonic-gate int rc;
11297c478bd9Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)arg;
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate rc = DDI_INTR_UNCLAIMED;
11327c478bd9Sstevel@tonic-gate
1133fd9cb95cSsethg if (kb8042->init_state == KB8042_UNINITIALIZED)
1134fd9cb95cSsethg return (DDI_INTR_UNCLAIMED);
1135fd9cb95cSsethg
11367c478bd9Sstevel@tonic-gate /* don't care if drv_setparm succeeds */
11377c478bd9Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1);
11387c478bd9Sstevel@tonic-gate
11397c478bd9Sstevel@tonic-gate while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
11407c478bd9Sstevel@tonic-gate != 0) {
11417c478bd9Sstevel@tonic-gate rc = DDI_INTR_CLAIMED;
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle,
11447c478bd9Sstevel@tonic-gate kb8042->addr + I8042_INT_INPUT_DATA);
11457c478bd9Sstevel@tonic-gate
11467c478bd9Sstevel@tonic-gate kb8042_received_byte(kb8042, scancode);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate
11497c478bd9Sstevel@tonic-gate return (rc);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate
11527c478bd9Sstevel@tonic-gate static void
11537c478bd9Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
11547c478bd9Sstevel@tonic-gate {
11557c478bd9Sstevel@tonic-gate struct copyresp *csp;
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate csp = (struct copyresp *)mp->b_rptr;
11587c478bd9Sstevel@tonic-gate if (csp->cp_rval) {
11597c478bd9Sstevel@tonic-gate freemsg(mp);
11607c478bd9Sstevel@tonic-gate return;
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate switch (csp->cp_cmd) {
11647c478bd9Sstevel@tonic-gate default:
11657c478bd9Sstevel@tonic-gate miocack(qp, mp, 0, 0);
11667c478bd9Sstevel@tonic-gate break;
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate static boolean_t
11717c478bd9Sstevel@tonic-gate kb8042_polled_keycheck(
11727c478bd9Sstevel@tonic-gate struct kbtrans_hardware *hw,
11737c478bd9Sstevel@tonic-gate int *key,
11747c478bd9Sstevel@tonic-gate enum keystate *state)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw;
11777c478bd9Sstevel@tonic-gate int scancode;
11787c478bd9Sstevel@tonic-gate boolean_t legit;
11797c478bd9Sstevel@tonic-gate boolean_t synthetic_release_needed;
11807c478bd9Sstevel@tonic-gate
11817c478bd9Sstevel@tonic-gate if (kb8042->polled_synthetic_release_pending) {
11827c478bd9Sstevel@tonic-gate *key = kb8042->polled_synthetic_release_key;
11837c478bd9Sstevel@tonic-gate *state = KEY_RELEASED;
11847c478bd9Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE;
11857c478bd9Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state);
11867c478bd9Sstevel@tonic-gate return (B_TRUE);
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate for (;;) {
11907c478bd9Sstevel@tonic-gate if (ddi_get8(kb8042->handle,
11917c478bd9Sstevel@tonic-gate kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
11927c478bd9Sstevel@tonic-gate return (B_FALSE);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate
11957c478bd9Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle,
11967c478bd9Sstevel@tonic-gate kb8042->addr + I8042_POLL_INPUT_DATA);
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, key, state,
11997c478bd9Sstevel@tonic-gate &synthetic_release_needed);
12007c478bd9Sstevel@tonic-gate if (!legit) {
12017c478bd9Sstevel@tonic-gate continue;
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate * For the moment at least, we rely on hardware autorepeat
12057c478bd9Sstevel@tonic-gate * for polled I/O autorepeat. However, for coordination
12067c478bd9Sstevel@tonic-gate * with the interrupt-driven code, maintain the last key
12077c478bd9Sstevel@tonic-gate * pressed.
12087c478bd9Sstevel@tonic-gate */
12097c478bd9Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state);
12107c478bd9Sstevel@tonic-gate
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate * This is a total hack to support two additional keys
12137c478bd9Sstevel@tonic-gate * on Korean keyboards. They report only on press, and
12147c478bd9Sstevel@tonic-gate * so we synthesize a release. Most likely this will
12157c478bd9Sstevel@tonic-gate * never be important to polled I/O, but if I do it
12167c478bd9Sstevel@tonic-gate * "right" the first time it _won't_ be an issue.
12177c478bd9Sstevel@tonic-gate */
12187c478bd9Sstevel@tonic-gate if (synthetic_release_needed) {
12197c478bd9Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_TRUE;
12207c478bd9Sstevel@tonic-gate kb8042->polled_synthetic_release_key = *key;
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB) {
12247c478bd9Sstevel@tonic-gate *key = keytab_pc2usb[*key];
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate return (B_TRUE);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate
12307c478bd9Sstevel@tonic-gate static void
1231fd9cb95cSsethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1232fd9cb95cSsethg {
1233fd9cb95cSsethg kb8042->leds.desired = led_state;
1234fd9cb95cSsethg
1235fd9cb95cSsethg if (!polled)
1236fd9cb95cSsethg mutex_enter(&kb8042->w_hw_mutex);
1237fd9cb95cSsethg
1238bb2d7d5eSSeth Goldberg if (kb8042->leds.desired != kb8042->leds.commanded) {
1239bb2d7d5eSSeth Goldberg kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1240bb2d7d5eSSeth Goldberg }
1241fd9cb95cSsethg
1242fd9cb95cSsethg if (!polled)
1243fd9cb95cSsethg mutex_exit(&kb8042->w_hw_mutex);
1244fd9cb95cSsethg }
1245fd9cb95cSsethg
1246fd9cb95cSsethg static void
12477c478bd9Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw;
1250fd9cb95cSsethg kb8042_setled(kb8042, led_state, B_TRUE);
1251fd9cb95cSsethg }
12527c478bd9Sstevel@tonic-gate
1253fd9cb95cSsethg static void
1254fd9cb95cSsethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1255fd9cb95cSsethg {
1256fd9cb95cSsethg struct kb8042 *kb8042 = (struct kb8042 *)hw;
1257fd9cb95cSsethg kb8042_setled(kb8042, led_state, B_FALSE);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate
126015bfc6b7SSeth Goldberg
126115bfc6b7SSeth Goldberg static int
126215bfc6b7SSeth Goldberg kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
126315bfc6b7SSeth Goldberg {
126415bfc6b7SSeth Goldberg uint8_t *outp = kb8042->addr +
126515bfc6b7SSeth Goldberg (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
126615bfc6b7SSeth Goldberg uint8_t *inavp = kb8042->addr +
126715bfc6b7SSeth Goldberg (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
126815bfc6b7SSeth Goldberg uint8_t *inp = kb8042->addr +
126915bfc6b7SSeth Goldberg (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
127015bfc6b7SSeth Goldberg uint8_t b;
127115bfc6b7SSeth Goldberg int ms_waited;
127215bfc6b7SSeth Goldberg int timedout;
127315bfc6b7SSeth Goldberg int expire;
127415bfc6b7SSeth Goldberg int retries = 0;
127515bfc6b7SSeth Goldberg
127615bfc6b7SSeth Goldberg do {
127715bfc6b7SSeth Goldberg kb8042->acked = 0;
127815bfc6b7SSeth Goldberg kb8042->need_retry = 0;
127915bfc6b7SSeth Goldberg ms_waited = 0; /* Zero it whether polled or not */
128015bfc6b7SSeth Goldberg timedout = 0;
128115bfc6b7SSeth Goldberg
128215bfc6b7SSeth Goldberg ddi_put8(kb8042->handle, outp, u8);
128315bfc6b7SSeth Goldberg
128415bfc6b7SSeth Goldberg while (!kb8042->acked && !kb8042->need_retry && !timedout) {
128515bfc6b7SSeth Goldberg
128615bfc6b7SSeth Goldberg if (polled) {
128715bfc6b7SSeth Goldberg if (ddi_get8(kb8042->handle, inavp)) {
128815bfc6b7SSeth Goldberg b = ddi_get8(kb8042->handle, inp);
128915bfc6b7SSeth Goldberg switch (b) {
129015bfc6b7SSeth Goldberg case KB_ACK:
129115bfc6b7SSeth Goldberg kb8042->acked = 1;
129215bfc6b7SSeth Goldberg break;
129315bfc6b7SSeth Goldberg case KB_RESEND:
129415bfc6b7SSeth Goldberg kb8042->need_retry = 1;
129515bfc6b7SSeth Goldberg break;
129615bfc6b7SSeth Goldberg default:
129715bfc6b7SSeth Goldberg /*
129815bfc6b7SSeth Goldberg * drop it: We should never
129915bfc6b7SSeth Goldberg * get scancodes while
130015bfc6b7SSeth Goldberg * we're in the middle of a
130115bfc6b7SSeth Goldberg * command anyway.
130215bfc6b7SSeth Goldberg */
130315bfc6b7SSeth Goldberg #ifdef DEBUG
130415bfc6b7SSeth Goldberg cmn_err(CE_WARN, "!Unexpected "
130515bfc6b7SSeth Goldberg " byte 0x%x", b);
130615bfc6b7SSeth Goldberg #endif
130715bfc6b7SSeth Goldberg break;
130815bfc6b7SSeth Goldberg }
130915bfc6b7SSeth Goldberg }
131015bfc6b7SSeth Goldberg
131115bfc6b7SSeth Goldberg /*
131215bfc6b7SSeth Goldberg * Wait 1ms if an ACK wasn't received yet
131315bfc6b7SSeth Goldberg */
131415bfc6b7SSeth Goldberg if (!kb8042->acked) {
131515bfc6b7SSeth Goldberg drv_usecwait(1000);
131615bfc6b7SSeth Goldberg ms_waited++;
131715bfc6b7SSeth Goldberg if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
131815bfc6b7SSeth Goldberg timedout = B_TRUE;
131915bfc6b7SSeth Goldberg }
132015bfc6b7SSeth Goldberg } else {
132115bfc6b7SSeth Goldberg /* Interrupt-driven */
132215bfc6b7SSeth Goldberg expire = ddi_get_lbolt() +
132315bfc6b7SSeth Goldberg drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
132415bfc6b7SSeth Goldberg
132515bfc6b7SSeth Goldberg /*
132615bfc6b7SSeth Goldberg * If cv_timedwait returned -1 and we neither
132715bfc6b7SSeth Goldberg * received an ACK nor a RETRY response, then
132815bfc6b7SSeth Goldberg * we timed out.
132915bfc6b7SSeth Goldberg */
133015bfc6b7SSeth Goldberg if (cv_timedwait(&kb8042->cmd_cv,
133115bfc6b7SSeth Goldberg &kb8042->w_hw_mutex, expire) == -1 &&
133215bfc6b7SSeth Goldberg !kb8042->acked && !kb8042->need_retry) {
133315bfc6b7SSeth Goldberg timedout = B_TRUE;
133415bfc6b7SSeth Goldberg }
133515bfc6b7SSeth Goldberg }
133615bfc6b7SSeth Goldberg
133715bfc6b7SSeth Goldberg }
133815bfc6b7SSeth Goldberg } while ((kb8042->need_retry || timedout) &&
133915bfc6b7SSeth Goldberg ++retries < MAX_KB8042_RETRIES);
134015bfc6b7SSeth Goldberg
134115bfc6b7SSeth Goldberg return (kb8042->acked);
134215bfc6b7SSeth Goldberg }
134315bfc6b7SSeth Goldberg
134415bfc6b7SSeth Goldberg /*
134515bfc6b7SSeth Goldberg * kb8042_send_to_keyboard should be called with w_hw_mutex held if
134615bfc6b7SSeth Goldberg * polled is FALSE.
134715bfc6b7SSeth Goldberg */
13487c478bd9Sstevel@tonic-gate static void
13497c478bd9Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
13507c478bd9Sstevel@tonic-gate {
1351bb2d7d5eSSeth Goldberg
1352bb2d7d5eSSeth Goldberg /*
135315bfc6b7SSeth Goldberg * KB_SET_LED and KB_ENABLE are special commands which require blocking
135415bfc6b7SSeth Goldberg * other 8042 consumers while executing.
1355bb2d7d5eSSeth Goldberg *
1356bb2d7d5eSSeth Goldberg * Other commands/data are sent using the single put8 I/O access
1357bb2d7d5eSSeth Goldberg * function.
1358bb2d7d5eSSeth Goldberg */
1359bb2d7d5eSSeth Goldberg if (byte == KB_SET_LED) {
136015bfc6b7SSeth Goldberg
136115bfc6b7SSeth Goldberg if (!polled) {
136215bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr +
136315bfc6b7SSeth Goldberg I8042_LOCK);
136415bfc6b7SSeth Goldberg }
136515bfc6b7SSeth Goldberg
136615bfc6b7SSeth Goldberg if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
1367bb2d7d5eSSeth Goldberg /*
136815bfc6b7SSeth Goldberg * Ignore return value, as there's nothing we can
136915bfc6b7SSeth Goldberg * do about it if the SET LED command fails.
1370bb2d7d5eSSeth Goldberg */
137115bfc6b7SSeth Goldberg (void) kb8042_send_and_wait(kb8042,
137215bfc6b7SSeth Goldberg kb8042_xlate_leds(kb8042->leds.desired), polled);
137315bfc6b7SSeth Goldberg }
137415bfc6b7SSeth Goldberg
137515bfc6b7SSeth Goldberg if (!polled) {
137615bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr +
137715bfc6b7SSeth Goldberg I8042_UNLOCK);
1378bb2d7d5eSSeth Goldberg }
1379bb2d7d5eSSeth Goldberg kb8042->leds.commanded = kb8042->leds.desired;
1380bb2d7d5eSSeth Goldberg
1381bb2d7d5eSSeth Goldberg } else if (byte == KB_ENABLE) {
1382bb2d7d5eSSeth Goldberg
138315bfc6b7SSeth Goldberg if (!polled) {
138415bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr +
138515bfc6b7SSeth Goldberg I8042_LOCK);
1386bb2d7d5eSSeth Goldberg }
138715bfc6b7SSeth Goldberg
138815bfc6b7SSeth Goldberg (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
138915bfc6b7SSeth Goldberg
139015bfc6b7SSeth Goldberg if (!polled) {
139115bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle, kb8042->addr +
139215bfc6b7SSeth Goldberg I8042_UNLOCK);
139315bfc6b7SSeth Goldberg }
139415bfc6b7SSeth Goldberg
1395bb2d7d5eSSeth Goldberg } else {
1396bb2d7d5eSSeth Goldberg /* All other commands use the "normal" virtual output port */
13977c478bd9Sstevel@tonic-gate if (polled) {
13987c478bd9Sstevel@tonic-gate ddi_put8(kb8042->handle,
13997c478bd9Sstevel@tonic-gate kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
14007c478bd9Sstevel@tonic-gate } else {
14017c478bd9Sstevel@tonic-gate ddi_put8(kb8042->handle,
14027c478bd9Sstevel@tonic-gate kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
14037c478bd9Sstevel@tonic-gate }
1404bb2d7d5eSSeth Goldberg }
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate * Wait until the keyboard is fully up, maybe.
14097c478bd9Sstevel@tonic-gate * We may be the first person to talk to the keyboard, in which case
14107c478bd9Sstevel@tonic-gate * it's patiently waiting to say "AA" to us to tell us it's up.
14117c478bd9Sstevel@tonic-gate * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
14127c478bd9Sstevel@tonic-gate * good bet that we've already spent that long getting to that point,
14137c478bd9Sstevel@tonic-gate * so we'll only wait long enough for the communications electronics to
14147c478bd9Sstevel@tonic-gate * run.
14157c478bd9Sstevel@tonic-gate */
14167c478bd9Sstevel@tonic-gate static void
14177c478bd9Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate int cnt;
14207c478bd9Sstevel@tonic-gate int ready;
14217c478bd9Sstevel@tonic-gate
1422fd9cb95cSsethg /* wait for up to 250 ms for a response */
14237c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < 250; cnt++) {
14247c478bd9Sstevel@tonic-gate ready = ddi_get8(kb8042->handle,
14257c478bd9Sstevel@tonic-gate kb8042->addr + I8042_INT_INPUT_AVAIL);
14267c478bd9Sstevel@tonic-gate if (ready != 0)
14277c478bd9Sstevel@tonic-gate break;
14287c478bd9Sstevel@tonic-gate drv_usecwait(1000);
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate
14317c478bd9Sstevel@tonic-gate /*
14327c478bd9Sstevel@tonic-gate * If there's something pending, read and discard it. If not,
14337c478bd9Sstevel@tonic-gate * assume things are OK anyway - maybe somebody else ate it
14347c478bd9Sstevel@tonic-gate * already. (On a PC, the BIOS almost certainly did.)
14357c478bd9Sstevel@tonic-gate */
14367c478bd9Sstevel@tonic-gate if (ready != 0) {
143715bfc6b7SSeth Goldberg (void) ddi_get8(kb8042->handle,
14387c478bd9Sstevel@tonic-gate kb8042->addr + I8042_INT_INPUT_DATA);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate static int
14437c478bd9Sstevel@tonic-gate kb8042_xlate_leds(int led)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate int res;
14467c478bd9Sstevel@tonic-gate
14477c478bd9Sstevel@tonic-gate res = 0;
14487c478bd9Sstevel@tonic-gate
14497c478bd9Sstevel@tonic-gate if (led & LED_NUM_LOCK)
14507c478bd9Sstevel@tonic-gate res |= LED_NUM;
14517c478bd9Sstevel@tonic-gate if (led & LED_SCROLL_LOCK)
14527c478bd9Sstevel@tonic-gate res |= LED_SCR;
14537c478bd9Sstevel@tonic-gate if (led & LED_CAPS_LOCK)
14547c478bd9Sstevel@tonic-gate res |= LED_CAP;
14557c478bd9Sstevel@tonic-gate
14567c478bd9Sstevel@tonic-gate return (res);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14607c478bd9Sstevel@tonic-gate static void
14617c478bd9Sstevel@tonic-gate kb8042_get_initial_leds(
14627c478bd9Sstevel@tonic-gate struct kb8042 *kb8042,
14637c478bd9Sstevel@tonic-gate int *initial_leds,
14647c478bd9Sstevel@tonic-gate int *initial_led_mask)
14657c478bd9Sstevel@tonic-gate {
1466bb2d7d5eSSeth Goldberg #if defined(__i386) || defined(__amd64)
14677c478bd9Sstevel@tonic-gate extern caddr_t p0_va;
14687c478bd9Sstevel@tonic-gate uint8_t bios_kb_flag;
14697c478bd9Sstevel@tonic-gate
14707c478bd9Sstevel@tonic-gate bios_kb_flag = p0_va[BIOS_KB_FLAG];
14717c478bd9Sstevel@tonic-gate
14727c478bd9Sstevel@tonic-gate *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
14737c478bd9Sstevel@tonic-gate *initial_leds = 0;
14747c478bd9Sstevel@tonic-gate if (bios_kb_flag & BIOS_CAPS_STATE)
14757c478bd9Sstevel@tonic-gate *initial_leds |= LED_CAPS_LOCK;
14767c478bd9Sstevel@tonic-gate if (bios_kb_flag & BIOS_NUM_STATE)
14777c478bd9Sstevel@tonic-gate *initial_leds |= LED_NUM_LOCK;
14787c478bd9Sstevel@tonic-gate if (bios_kb_flag & BIOS_SCROLL_STATE)
14797c478bd9Sstevel@tonic-gate *initial_leds |= LED_SCROLL_LOCK;
14807c478bd9Sstevel@tonic-gate #else
14817c478bd9Sstevel@tonic-gate *initial_leds = 0;
14827c478bd9Sstevel@tonic-gate *initial_led_mask = 0;
14837c478bd9Sstevel@tonic-gate #endif
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate
14867c478bd9Sstevel@tonic-gate static boolean_t
14877c478bd9Sstevel@tonic-gate kb8042_autorepeat_detect(
14887c478bd9Sstevel@tonic-gate struct kb8042 *kb8042,
14897c478bd9Sstevel@tonic-gate int key_pos,
14907c478bd9Sstevel@tonic-gate enum keystate state)
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate if (state == KEY_RELEASED) {
14937c478bd9Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos)
14947c478bd9Sstevel@tonic-gate kb8042->kb_old_key_pos = 0;
14957c478bd9Sstevel@tonic-gate } else {
14967c478bd9Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos) {
14977c478bd9Sstevel@tonic-gate return (B_TRUE);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate kb8042->kb_old_key_pos = key_pos;
15007c478bd9Sstevel@tonic-gate }
15017c478bd9Sstevel@tonic-gate return (B_FALSE);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate
15047c478bd9Sstevel@tonic-gate /* ARGSUSED */
15057c478bd9Sstevel@tonic-gate static void
15067c478bd9Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate switch (cmd) {
15097c478bd9Sstevel@tonic-gate case KBD_CMD_BELL:
1510c35aa225Smarx (void) beeper_on(BEEP_TYPE4);
15117c478bd9Sstevel@tonic-gate break;
15127c478bd9Sstevel@tonic-gate case KBD_CMD_NOBELL:
1513c35aa225Smarx (void) beeper_off();
15147c478bd9Sstevel@tonic-gate break;
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate
15187c478bd9Sstevel@tonic-gate
15197c478bd9Sstevel@tonic-gate /*
15207c478bd9Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time.
15217c478bd9Sstevel@tonic-gate */
15227c478bd9Sstevel@tonic-gate static int
1523281f0747Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg)
15247c478bd9Sstevel@tonic-gate {
15257c478bd9Sstevel@tonic-gate struct kb8042 *kb8042;
15267c478bd9Sstevel@tonic-gate
15277c478bd9Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg;
15287c478bd9Sstevel@tonic-gate
15297c478bd9Sstevel@tonic-gate return (kbtrans_getchar(kb8042->hw_kbtrans));
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time.
15347c478bd9Sstevel@tonic-gate */
15357c478bd9Sstevel@tonic-gate static int
1536281f0747Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate struct kb8042 *kb8042;
15397c478bd9Sstevel@tonic-gate
15407c478bd9Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg;
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate return (kbtrans_ischar(kb8042->hw_kbtrans));
15437c478bd9Sstevel@tonic-gate }
1544