kb8042.c revision fbac63660cc7a6ccfe2042916ea594ac07d63fcb
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#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, 0, 0, 0, 0, 0, 0, 0,
/* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 248 */ 0, 0, 0, 0
};
/*
* DEBUG (or KD_DEBUG for just this module) turns on a flag called
* kb8042_enable_debug_hotkey. If kb8042_enable_debug_hotkey is true,
* then the following hotkeys are enabled:
* F10 - turn on debugging "normal" translations
* F9 - turn on debugging "getchar" translations
* F8 - turn on "low level" debugging
* F1 - turn off all debugging
* The default value for kb8042_enable_debug_hotkey is false, disabling
* these hotkeys.
*/
#define KD_DEBUG
#endif
#ifdef KD_DEBUG
static void kb8042_debug_hotkey(int scancode);
#endif
#ifdef __sparc
#define USECS_PER_WAIT 100
#define MIN_DELAY_USECS USECS_PER_WAIT
int kb8042_default_scanset = 2;
#endif
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
{
}
}
static boolean_t
{
int err;
err = 0;
} else {
}
return (rval);
}
static const char *
{
switch (errcode) {
case EAGAIN:
return ("Timed out");
default:
return ("Unknown error");
}
}
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.
*/
== KB_ACK)
;
#ifdef KD_DEBUG
scanset);
#endif
return (scanset);
#ifdef KD_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) {
#if defined(KD_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 KD_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;
/* Set up the command state machine and start it running. */
}
/*ARGSUSED2*/
static int
{
int err;
int initial_leds;
int initial_led_mask;
0) {
return (EINTR);
}
}
return (0);
}
&kb8042->hw_kbtrans,
if (err != 0)
return (err);
(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;
return (0);
}
/*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;
#ifdef KD_DEBUG
#endif
return;
if (legit == 0) {
/* Eaten by translation */
#ifdef KD_DEBUG
if (kb8042_debug)
#endif
return;
}
#ifdef KD_DEBUG
if (kb8042_debug) {
prom_printf("kb8042_intr: 0x%x -> %s %d",
key_pos);
}
#endif
/*
* Don't know if we want this permanently, but it seems interesting
* for the moment.
*/
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> debug mod1");
#endif
}
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> debug mod2");
#endif
}
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> debugger\n");
#endif
/*
* 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.
*/
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> nobody home\n");
#endif
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.)
*/
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> autorepeat ignored\n");
#endif
return;
}
#ifdef KD_DEBUG
if (kb8042_debug)
prom_printf(" -> OK\n");
#endif
#if defined(KD_DEBUG)
if (kb8042_pressrelease_debug) {
prom_printf(" %s%d ",
key_pos);
}
#endif
/*
* This is a total hack. For some stupid reason, the two additional
* only. We synthesize a release immediately.
*/
if (synthetic_release_needed) {
#if defined(KD_DEBUG)
if (kb8042_debug)
#endif
}
}
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) {
#if defined(KD_DEBUG)
#endif
}
return (rc);
}
static void
{
return;
}
default:
break;
}
}
static boolean_t
struct kbtrans_hardware *hw,
int *key,
{
int scancode;
*state = KEY_RELEASED;
#if defined(KD_DEBUG)
if (kb8042_getchar_debug)
#endif
return (B_TRUE);
}
for (;;) {
return (B_FALSE);
}
#if defined(KD_DEBUG)
#endif
#ifdef KD_DEBUG
if (kb8042_getchar_debug)
#endif
if (!legit) {
#ifdef KD_DEBUG
if (kb8042_getchar_debug)
prom_printf(" -> ignored\n");
#endif
continue;
}
#ifdef KD_DEBUG
if (kb8042_getchar_debug) {
prom_printf(" -> %s %d\n",
*key);
}
#endif
/*
* 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 void
{
/*
* KB_SET_LED and KB_ENABLE are special commands for which the nexus
* driver is requested to wait for responses before proceeding.
* KB_SET_LED also provides an option byte for the nexus to send to
* the keyboard after the acknowledgement.
*
* function.
*/
if (byte == KB_SET_LED) {
/*
* Initialize the buffer used with _rep_put8. We
* expect an ACK after the SET_LED command, at which point
* the LED byte should be sent to the keyboard.
*/
led_cmd[0] = KB_SET_LED;
if (polled) {
} else {
}
/*
* Initialize the buffer used with _rep_put8. We
* expect an ACK after the KB_ENABLE command.
*/
if (polled) {
} else {
}
} else {
/* All other commands use the "normal" virtual output port */
if (polled) {
} else {
}
}
#if defined(KD_DEBUG)
#endif
}
/*
* 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;
unsigned char byt;
/* 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) {
#if defined(KD_DEBUG)
#endif
}
}
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
}
#if defined(KD_DEBUG)
static void
{
return;
switch (scancode) {
case 0x44: /* F10 in Scan Set 1 code. (Set 2 code is 0x09) */
if (!kb8042_debug) {
prom_printf("\nKeyboard: normal debugging on\n");
}
break;
case 0x43: /* F9 in Scan Set 1 code. (Set 2 code is 0x01) */
if (!kb8042_getchar_debug) {
prom_printf("\nKeyboard: getchar debugging on\n");
}
break;
case 0x42: /* F8 in Scan Set 1 code. (Set 2 code is 0x0a) */
if (!kb8042_low_level_debug) {
prom_printf("\nKeyboard: low-level debugging on\n");
}
break;
case 0x41: /* F7 in Scan Set 1 code. (Set 2 code is 0x83) */
if (!kb8042_pressrelease_debug) {
}
break;
case 0x3b: /* F1 in Scan Set 1 code. (Set 2 code is 0x05) */
if (kb8042_debug ||
prom_printf("\nKeyboard: all debugging off\n");
}
break;
}
}
#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
{
}