kb8042.c revision cd2135d041662c75017487e2d4b9b69b7e0b8349
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
/* All Rights Reserved */
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#define KEYMAP_SIZE_VARIABLE
#include "kb8042.h"
#include <sys/inttypes.h>
/*
* For any keyboard, there is a unique code describing the position
* of the key on a keyboard. We refer to the code as "station number".
* The following table is used to map the station numbers from ps2
*
* A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
* maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
* when in KB_PC mode. Note that this does not need to be made conditional
* on the architecture for which this module is complied because there are no
* keys that map to K8042_STOP on non-SPARC platforms.
*/
/* 0 */ 0, 53, 30, 31, 32, 33, 34, 35,
/* 8 */ 36, 37, 38, 39, 45, 46, 137, 42,
/* 16 */ 43, 20, 26, 8, 21, 23, 28, 24,
/* 24 */ 12, 18, 19, 47, 48, 49, 57, 4,
/* 32 */ 22, 7, 9, 10, 11, 13, 14, 15,
/* 40 */ 51, 52, 50, 40, 225, 100, 29, 27,
/* 48 */ 6, 25, 5, 17, 16, 54, 55, 56,
/* 56 */ 135, 229, 224, 227, 226, 44, 230, 231,
/* 64 */ 228, 101, 0, 0, 0, 0, 0, 0,
/* 72 */ 0, 0, 0, 73, 76, 0, 0, 80,
/* 80 */ 74, 77, 0, 82, 81, 75, 78, 0,
/* 88 */ 0, 79, 83, 95, 92, 89, 0, 84,
/* 96 */ 96, 93, 90, 98, 85, 97, 94, 91,
/* 104 */ 99, 86, 87, 133, 88, 0, 41, 0,
/* 112 */ 58, 59, 60, 61, 62, 63, 64, 65,
/* 120 */ 66, 67, 68, 69, 70, 71, 72, 0,
/* 128 */ 0, 0, 0, 139, 138, 136, 0, 0,
/* 136 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 144 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 152 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 160 */ 120, 0, 0, 0, 0, 0, 0, 0,
/* 168 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 176 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 184 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 192 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 208 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 216 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 224 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 232 */ 0, 128, 129, 127, 0, 0, 0, 0,
/* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 248 */ 0, 0, 0, 0
};
#ifdef __sparc
#define USECS_PER_WAIT 100
#define MIN_DELAY_USECS USECS_PER_WAIT
int kb8042_default_scanset = 2;
#endif
#define MAX_KB8042_RETRIES 5
static int kb8042_xlate_leds(int);
static boolean_t kb8042_polled_keycheck(
static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
static struct kbtrans_callbacks kb8042_callbacks = {
};
extern struct keyboard keyindex_pc;
static char module_name[] = "kb8042";
static int kb8042_wsrv();
struct module_info kb8042_sinfo = {
42, /* Module ID */
0, 32, /* Minimum and maximum packet sizes */
256, 128 /* High and low water marks */
};
static struct qinit kb8042_rinit = {
};
static struct qinit kb8042_winit = {
};
struct streamtab
void **result);
static struct cb_ops cb_kb8042_ops = {
nulldev, /* cb_open */
nulldev, /* cb_close */
nodev, /* cb_strategy */
nodev, /* cb_print */
nodev, /* cb_dump */
nodev, /* cb_read */
nodev, /* cb_write */
nodev, /* cb_ioctl */
nodev, /* cb_devmap */
nodev, /* cb_mmap */
nodev, /* cb_segmap */
nochpoll, /* cb_chpoll */
ddi_prop_op, /* cb_prop_op */
&kb8042_str_info, /* cb_stream */
};
struct dev_ops kb8042_ops = {
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
kb8042_getinfo, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
kb8042_attach, /* devo_attach */
kb8042_detach, /* devo_detach */
nodev, /* devo_reset */
&cb_kb8042_ops, /* devo_cb_ops */
NULL, /* devo_power */
ddi_quiesce_not_needed, /* devo_quiesce */
};
/*
* This is the loadable module wrapper.
*/
/*
* Module linkage information for the kernel.
*/
&mod_driverops, /* Type of module. This one is a driver */
"PS/2 keyboard driver",
&kb8042_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
(void *) &modldrv,
};
int
_init(void)
{
int rv;
return (rv);
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
int
{
}
#ifdef __sparc
static boolean_t
{
int i;
for (i = 0; i < reps; i++) {
return (B_TRUE);
if (i < (reps - 1))
}
return (B_FALSE);
}
static void
{
}
}
/*
* kb8042_send_and_expect does all its I/O via polling interfaces
*/
static boolean_t
{
int err;
err = 0;
} else {
}
return (rval);
}
static const char *
{
switch (errcode) {
case EAGAIN:
return ("Timed out");
default:
return ("Unknown error");
}
}
/*
* kb8042_read_scanset works properly because it is called before ddi_add_intr
* (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
* instead of just storing the data that comes in from the keyboard, which
* would prevent the code here from getting it.)
*/
static int
{
int scanset = -1;
int err;
/*
* Send a "change scan code set" command to the keyboard.
* It should respond with an ACK.
*/
goto fail_read_scanset;
}
/*
* Send a 0. The keyboard should ACK the 0, then it should send the
* scan code set in use.
*/
goto fail_read_scanset;
}
/*
* The next input byte from the keyboard should be the scan code
* set in use, though some keyboards like to send a few more acks
* just for fun, so blow past those to get the keyboard scan code.
*/
;
#ifdef DEBUG
scanset);
#endif
return (scanset);
#ifdef DEBUG
if (err == 0)
"keyboard: %s. (Expected 0x%x, but got 0x%x).",
else
#endif
return (-1);
}
#endif
static int
{
int rc;
int scanset;
int leds;
static ddi_device_acc_attr_t attr = {
};
switch (cmd) {
case DDI_RESUME:
}
return (DDI_SUCCESS);
case DDI_ATTACH:
if (kb8042_dip != NULL)
return (DDI_FAILURE);
/* The rest of the function is for attach */
break;
default:
return (DDI_FAILURE);
}
kb8042_dip = devi;
DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
goto failure;
}
if (rc != DDI_SUCCESS) {
#ifdef DEBUG
#endif
goto failure;
}
DDI_SUCCESS) {
goto failure;
}
#ifdef __sparc
/* Detect the scan code set currently in use */
if (scanset < 0 && kb8042_warn_unknown_scanset) {
"keyboard does not ", kb8042_default_scanset);
"restart the system. If you ");
"future, add ");
/* Use the default scan code set. */
}
#else
/* x86 systems use scan code set 1 -- no detection required */
scanset = 1;
#endif
"Unknown scan code set `%d'.", scanset);
/* Scan code set is not supported */
goto failure;
}
/*
* Turn on interrupts...
*/
if (ddi_add_intr(devi, 0,
goto failure;
}
#ifdef DEBUG
#endif
return (DDI_SUCCESS);
return (DDI_FAILURE);
}
static int
{
switch (cmd) {
case DDI_SUSPEND:
return (DDI_SUCCESS);
case DDI_DETACH:
/* If someone has a stream open, fail to detach */
return (DDI_FAILURE);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*ARGSUSED*/
static int
void *arg,
void **result)
{
register int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if (kb8042_dip == NULL) {
error = DDI_FAILURE;
} else {
*result = (void *) kb8042_dip;
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
break;
}
return (error);
}
static void
{
}
kb8042_dip = NULL;
}
static void
{
return;
if (!from_resume) {
}
kb8042->kb_old_key_pos = 0;
/*
* Explicitly grab and release the 8042 lock outside of
* kb8042_send_to_keyboard, because this is the only situation
* where a polling interface is used with locking required.
*/
/* Set up the command state machine and start it running. */
}
/*ARGSUSED2*/
static int
{
int err = 0;
int initial_leds;
int initial_led_mask;
return (0);
}
if (secpolicy_console(credp) != 0) {
return (EPERM);
}
0) {
return (EINTR);
}
}
&kb8042->hw_kbtrans,
if (err != 0)
goto out;
(int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
(void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
(boolean_t (*)(cons_polledio_arg_t, int *,
enum keystate *))kb8042_polled_keycheck;
out:
return (err);
}
/*ARGSUSED1*/
static int
{
/* If a beep is in progress, stop that */
(void) beeper_off();
0) {
return (EINTR);
}
}
return (0);
}
static int
{
if (!suspended)
#ifdef NO_KB_DEBUG
#else
/*
* Not taking keyboard input while suspending can make debugging
* difficult. However, we still do the ops counting so that we
* don't suspend at a bad time.
*/
#endif
case KBTRANS_MESSAGE_HANDLED:
continue;
break;
}
case M_IOCTL:
continue;
case M_IOCDATA:
continue;
case M_DELAY:
case M_STARTI:
case M_STOPI:
case M_READ: /* ignore, no buffered data */
continue;
case M_FLUSH:
else
continue;
default:
continue;
}
}
if (!suspended) {
}
return (0);
}
static void
{
int error;
int tmp;
int cycles;
int frequency;
int msecs;
case CONSOPENPOLLEDIO:
if (error != 0) {
return;
}
/*
* We are given an appropriate-sized data block,
* and return a pointer to our structure in it.
*/
break;
case CONSCLOSEPOLLEDIO:
break;
case CONSSETABORTENABLE:
return;
}
break;
/*
* Valid only in TR_UNTRANS_MODE mode.
*/
case CONSSETKBDTYPE:
if (error != 0) {
return;
}
break;
}
break;
case KIOCLAYOUT:
return;
}
return;
}
else
break;
case KIOCSLAYOUT:
return;
}
break;
case KIOCCMD:
if (error != 0) {
return;
}
break;
case KIOCMKTONE:
return;
}
if (cycles == 0)
else if (cycles == UINT16_MAX)
frequency = 0;
else {
if (frequency > UINT16_MAX)
}
if (error != 0)
else
break;
default:
#ifdef DEBUG1
#endif
return;
}
}
/*
* Process a byte received from the keyboard
*/
static void
int scancode) /* raw scan code */
{
int key_pos = -1;
/*
* Intercept ACK and RESEND and signal the condition that
* kb8042_send_and_wait is waiting for.
*/
return;
return;
}
return;
if (legit == 0) {
/* Eaten by translation */
return;
}
/*
* Don't know if we want this permanently, but it seems interesting
* for the moment.
*/
}
}
/*
* Require new presses of the modifiers.
*/
return;
}
/*
* If there's no queue above us - as can happen if we've been
* attached but not opened - drop the keystroke.
* Note that we do this here instead of above so that
* Ctrl-Alt-D still works.
*/
return;
}
/*
* This is to filter out auto repeat since it can't be
* turned off at the hardware. (Yeah, yeah, PS/2 keyboards
* can. Don't know whether they've taken over the world.
* Don't think so.)
*/
return;
}
/*
* This is a total hack. For some stupid reason, the two additional
* only. We synthesize a release immediately.
*/
if (synthetic_release_needed) {
}
}
static void
{
if (key != 0) {
}
}
}
/*
* Called from interrupt handler when keyboard interrupt occurs.
*/
static uint_t
{
int rc;
return (DDI_INTR_UNCLAIMED);
/* don't care if drv_setparm succeeds */
!= 0) {
}
return (rc);
}
static void
{
return;
}
default:
break;
}
}
static boolean_t
struct kbtrans_hardware *hw,
int *key,
{
int scancode;
*state = KEY_RELEASED;
return (B_TRUE);
}
for (;;) {
return (B_FALSE);
}
if (!legit) {
continue;
}
/*
* For the moment at least, we rely on hardware autorepeat
* for polled I/O autorepeat. However, for coordination
* with the interrupt-driven code, maintain the last key
* pressed.
*/
/*
* This is a total hack to support two additional keys
* on Korean keyboards. They report only on press, and
* so we synthesize a release. Most likely this will
* never be important to polled I/O, but if I do it
* "right" the first time it _won't_ be an issue.
*/
if (synthetic_release_needed) {
}
}
return (B_TRUE);
}
}
static void
{
if (!polled)
}
if (!polled)
}
static void
{
}
static void
{
}
static int
{
uint8_t b;
int ms_waited;
int timedout;
int expire;
int retries = 0;
do {
kb8042->need_retry = 0;
ms_waited = 0; /* Zero it whether polled or not */
timedout = 0;
if (polled) {
switch (b) {
case KB_ACK:
break;
case KB_RESEND:
break;
default:
/*
* drop it: We should never
* get scancodes while
* we're in the middle of a
* command anyway.
*/
#ifdef DEBUG
" byte 0x%x", b);
#endif
break;
}
}
/*
* Wait 1ms if an ACK wasn't received yet
*/
drv_usecwait(1000);
ms_waited++;
if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
}
} else {
/* Interrupt-driven */
expire = ddi_get_lbolt() +
/*
* If cv_timedwait returned -1 and we neither
* received an ACK nor a RETRY response, then
* we timed out.
*/
}
}
}
++retries < MAX_KB8042_RETRIES);
}
/*
* kb8042_send_to_keyboard should be called with w_hw_mutex held if
* polled is FALSE.
*/
static void
{
/*
* KB_SET_LED and KB_ENABLE are special commands which require blocking
* other 8042 consumers while executing.
*
* function.
*/
if (byte == KB_SET_LED) {
if (!polled) {
}
/*
* Ignore return value, as there's nothing we can
* do about it if the SET LED command fails.
*/
(void) kb8042_send_and_wait(kb8042,
}
if (!polled) {
}
if (!polled) {
}
if (!polled) {
}
} else {
/* All other commands use the "normal" virtual output port */
if (polled) {
} else {
}
}
}
/*
* Wait until the keyboard is fully up, maybe.
* We may be the first person to talk to the keyboard, in which case
* it's patiently waiting to say "AA" to us to tell us it's up.
* In theory it sends the AA in 300ms < n < 9s, but it's a pretty
* good bet that we've already spent that long getting to that point,
* so we'll only wait long enough for the communications electronics to
* run.
*/
static void
{
int cnt;
int ready;
/* wait for up to 250 ms for a response */
if (ready != 0)
break;
drv_usecwait(1000);
}
/*
* If there's something pending, read and discard it. If not,
* assume things are OK anyway - maybe somebody else ate it
* already. (On a PC, the BIOS almost certainly did.)
*/
if (ready != 0) {
}
}
static int
kb8042_xlate_leds(int led)
{
int res;
res = 0;
if (led & LED_NUM_LOCK)
if (led & LED_SCROLL_LOCK)
if (led & LED_CAPS_LOCK)
return (res);
}
/*ARGSUSED*/
static void
int *initial_leds,
int *initial_led_mask)
{
*initial_leds = 0;
if (bios_kb_flag & BIOS_CAPS_STATE)
if (bios_kb_flag & BIOS_NUM_STATE)
*initial_leds |= LED_NUM_LOCK;
if (bios_kb_flag & BIOS_SCROLL_STATE)
#else
*initial_leds = 0;
*initial_led_mask = 0;
#endif
}
static boolean_t
int key_pos,
{
if (state == KEY_RELEASED) {
kb8042->kb_old_key_pos = 0;
} else {
return (B_TRUE);
}
}
return (B_FALSE);
}
/* ARGSUSED */
static void
{
switch (cmd) {
case KBD_CMD_BELL:
(void) beeper_on(BEEP_TYPE4);
break;
case KBD_CMD_NOBELL:
(void) beeper_off();
break;
}
}
/*
* This is a pass-thru routine to get a character at poll time.
*/
static int
{
}
/*
* This is a pass-thru routine to get a character at poll time.
*/
static int
{
}