#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pckbd.c,v 1.39 2024/02/09 22:08:36 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/ioctl.h>
#include <sys/bus.h>
#include <dev/pckbport/pckbportvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/pckbport/pckbdreg.h>
#include <dev/pckbport/pckbdvar.h>
#include <dev/pckbport/wskbdmap_mfii.h>
#include "locators.h"
#include "opt_pckbd_layout.h"
#include "opt_pckbd_cnattach_may_fail.h"
#include "opt_wsdisplay_compat.h"
struct pckbd_internal {
int t_isconsole;
pckbport_tag_t t_kbctag;
pckbport_slot_t t_kbcslot;
int t_translating;
int t_lastchar;
int t_extended0;
int t_extended1;
int t_releasing;
struct pckbd_softc *t_sc;
};
struct pckbd_softc {
device_t sc_dev;
struct pckbd_internal *id;
int sc_enabled;
int sc_ledstate;
device_t sc_wskbddev;
#ifdef WSDISPLAY_COMPAT_RAWKBD
int rawkbd;
#endif
};
int pckbdprobe(device_t, cfdata_t, void *);
void pckbdattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(pckbd, sizeof(struct pckbd_softc),
pckbdprobe, pckbdattach, NULL, NULL);
int pckbd_enable(void *, int);
void pckbd_set_leds(void *, int);
int pckbd_ioctl(void *, u_long, void *, int, struct lwp *);
const struct wskbd_accessops pckbd_accessops = {
.enable = pckbd_enable,
.set_leds = pckbd_set_leds,
.ioctl = pckbd_ioctl,
};
void pckbd_cngetc(void *, u_int *, int *);
void pckbd_cnpollc(void *, int);
void pckbd_cnbell(void *, u_int, u_int, u_int);
const struct wskbd_consops pckbd_consops = {
.getc = pckbd_cngetc,
.pollc = pckbd_cnpollc,
.bell = pckbd_cnbell,
};
const struct wskbd_mapdata pckbd_keymapdata = {
.keydesc = pckbd_keydesctab,
#ifdef PCKBD_LAYOUT
.layout = PCKBD_LAYOUT,
#else
.layout = KB_US,
#endif
};
void (*pckbd_bell_fn)(void *, u_int, u_int, u_int, int);
void *pckbd_bell_fn_arg;
void pckbd_bell(u_int, u_int, u_int, int);
int pckbd_scancode_translate(struct pckbd_internal *, int);
int pckbd_set_xtscancode(pckbport_tag_t, pckbport_slot_t,
struct pckbd_internal *);
int pckbd_init(struct pckbd_internal *, pckbport_tag_t, pckbport_slot_t,
int);
void pckbd_input(void *, int);
struct pckbd_internal pckbd_consdata;
int
pckbd_set_xtscancode(pckbport_tag_t kbctag, pckbport_slot_t kbcslot,
struct pckbd_internal *id)
{
int xt, res = 0;
u_char cmd[2];
xt = pckbport_xt_translation(kbctag, kbcslot, 1);
if (xt == 1) {
cmd[0] = KBC_SETTABLE;
cmd[1] = 2;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
if (res) {
u_char cmdb[1];
aprint_debug("%s: error setting scanset 2\n", __func__);
cmdb[0] = KBC_RESET;
(void)pckbport_poll_cmd(kbctag, kbcslot, cmdb, 1, 1,
0, 1);
pckbport_flush(kbctag, kbcslot);
res = 0;
}
if (id != NULL)
id->t_translating = 1;
} else if (xt == -1) {
if (id != NULL)
id->t_translating = 0;
} else {
cmd[0] = KBC_SETTABLE;
cmd[1] = 1;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
if (res)
aprint_debug("%s: error setting scanset 1\n", __func__);
if (id != NULL)
id->t_translating = 1;
}
return res;
}
static int
pckbd_is_console(pckbport_tag_t tag, pckbport_slot_t slot)
{
return pckbd_consdata.t_isconsole &&
tag == pckbd_consdata.t_kbctag && slot == pckbd_consdata.t_kbcslot;
}
static bool
pckbd_suspend(device_t dv, const pmf_qual_t *qual)
{
struct pckbd_softc *sc = device_private(dv);
u_char cmd[1];
int res;
cmd[0] = KBC_DISABLE;
res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, 1, 0);
if (res)
return false;
pckbport_slot_enable(sc->id->t_kbctag,
sc->id->t_kbcslot, 0);
sc->sc_enabled = 0;
return true;
}
static bool
pckbd_resume(device_t dv, const pmf_qual_t *qual)
{
struct pckbd_softc *sc = device_private(dv);
u_char cmd[1], resp[1];
int res;
pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
cmd[0] = KBC_RESET;
res = pckbport_poll_cmd(sc->id->t_kbctag,
sc->id->t_kbcslot, cmd, 1, 1, resp, 1);
if (res)
aprint_debug("%s: reset error %d\n", __func__, res);
if (resp[0] != KBR_RSTDONE)
printf("%s: reset response 0x%x\n", __func__, resp[0]);
pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
pckbd_enable(sc, 1);
return true;
}
static bool
check_keyboard_by_id(struct pckbport_attach_args *pa)
{
u_char cmd[1], resp[2];
int res;
cmd[0] = KBC_GETID;
res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 0);
if (res) {
aprint_debug("%s: getid failed with %d\n", __func__, res);
return false;
}
switch (resp[0]) {
case 0xab: case 0xac:
case 0x2b: case 0x5d:
case 0x60: case 0x47:
return true;
default:
aprint_debug("%s: getid returned %#x\n", __func__, resp[0]);
return false;
}
}
int
pckbdprobe(device_t parent, cfdata_t cf, void *aux)
{
struct pckbport_attach_args *pa = aux;
int res;
u_char cmd[1], resp[1];
if ((pa->pa_slot != PCKBPORT_KBD_SLOT) &&
(cf->cf_loc[PCKBPORTCF_SLOT] == PCKBPORTCF_SLOT_DEFAULT))
return 0;
pckbport_flush(pa->pa_tag, pa->pa_slot);
cmd[0] = KBC_RESET;
res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 1, resp, 1);
if (res) {
aprint_debug("%s: reset error %d\n", __func__, res);
if (check_keyboard_by_id(pa)) {
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
return 2;
}
return pckbd_is_console(pa->pa_tag, pa->pa_slot) ? 1 : 0;
}
if (resp[0] != KBR_RSTDONE) {
printf("%s: reset response %#x\n", __func__, resp[0]);
if (check_keyboard_by_id(pa)) {
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
return 2;
}
return 0;
}
pckbport_flush(pa->pa_tag, pa->pa_slot);
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
return 2;
}
void
pckbdattach(device_t parent, device_t self, void *aux)
{
struct pckbd_softc *sc = device_private(self);
struct pckbport_attach_args *pa = aux;
struct wskbddev_attach_args a;
int isconsole;
u_char cmd[1];
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
isconsole = pckbd_is_console(pa->pa_tag, pa->pa_slot);
if (isconsole) {
sc->id = &pckbd_consdata;
cmd[0] = KBC_ENABLE;
(void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, 0, 0);
sc->sc_enabled = 1;
} else {
sc->id = malloc(sizeof(*sc->id), M_DEVBUF, M_WAITOK);
(void) pckbd_init(sc->id, pa->pa_tag, pa->pa_slot, 0);
cmd[0] = KBC_DISABLE;
(void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, 0, 0);
sc->sc_enabled = 0;
}
sc->id->t_sc = sc;
pckbport_set_inputhandler(sc->id->t_kbctag, sc->id->t_kbcslot,
pckbd_input, sc, device_xname(sc->sc_dev));
a.console = isconsole;
a.keymap = &pckbd_keymapdata;
a.accessops = &pckbd_accessops;
a.accesscookie = sc;
if (!pmf_device_register(self, pckbd_suspend, pckbd_resume))
aprint_error_dev(self, "couldn't establish power handler\n");
sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
}
int
pckbd_enable(void *v, int on)
{
struct pckbd_softc *sc = v;
int res;
u_char cmd[1];
if (on) {
if (sc->sc_enabled) {
aprint_debug("%s: bad enable\n", __func__);
return EBUSY;
}
pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 1);
cmd[0] = KBC_ENABLE;
res = pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, NULL, 0);
if (res) {
printf("%s: command error\n", __func__);
return res;
}
res = pckbd_set_xtscancode(sc->id->t_kbctag,
sc->id->t_kbcslot, sc->id);
if (res)
return res;
sc->sc_enabled = 1;
} else {
if (sc->id->t_isconsole)
return EBUSY;
cmd[0] = KBC_DISABLE;
res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, 1, 0);
if (res) {
printf("%s: command error\n", __func__);
return res;
}
pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 0);
sc->sc_enabled = 0;
}
return 0;
}
const u_int8_t pckbd_xtbl[] = {
0,
0x43,
0x89,
0x3f,
0x3d,
0x3b,
0x3c,
0x58,
0,
0x44,
0x42,
0x40,
0x3e,
0x0f,
0x29,
0,
0,
0x38,
0x2a,
0,
0x1d,
0x10,
0x02,
0,
0,
0,
0x2c,
0x1f,
0x1e,
0x11,
0x03,
0,
0,
0x2e,
0x2d,
0x20,
0x12,
0x05,
0x04,
0,
0,
0x39,
0x2f,
0x21,
0x14,
0x13,
0x06,
0,
0,
0x31,
0x30,
0x23,
0x22,
0x15,
0x07,
0,
0,
0,
0x32,
0x24,
0x16,
0x08,
0x09,
0,
0,
0x33,
0x25,
0x17,
0x18,
0x0b,
0x0a,
0,
0,
0x34,
0x35,
0x26,
0x27,
0x19,
0x0c,
0,
0,
0,
0x28,
0,
0x1a,
0x0d,
0,
0,
0x3a,
0x36,
0x1c,
0x1b,
0,
0x2b,
0,
0,
0,
0,
0,
0,
0,
0,
0x0e,
0,
0,
0x4f,
0,
0x4b,
0x47,
0,
0,
0,
0x52,
0x53,
0x50,
0x4c,
0x4d,
0x48,
0x01,
0x45,
0x57,
0x4e,
0x51,
0x4a,
0x37,
0x49,
0x46,
0,
0,
0,
0,
0x41,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0xdb,
0x88,
0x8a,
0x8c,
0x8e,
0x90,
0x92,
0x8b,
0x8d,
0x8f,
0x91
};
const u_int8_t pckbd_xtbl_ext[] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0x38,
0,
0,
0x1d,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0xdd,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0xb5,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0x1c,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0x4f,
0,
0x4b,
0x47,
0,
0,
0,
0x52,
0x53,
0x50,
0,
0x4d,
0x48,
0,
0,
0,
0,
0x51,
0,
0x37,
0x49,
0x46,
0
};
int
pckbd_scancode_translate(struct pckbd_internal *id, int datain)
{
if (id->t_translating != 0)
return datain;
if (datain == KBR_BREAK) {
id->t_releasing = 0x80;
return 0;
}
if (datain == KBR_EXTENDED0 || datain == KBR_EXTENDED1)
return datain;
if (id->t_extended1 == 2 && datain == 0x14)
return 0x1d | id->t_releasing;
else if (id->t_extended1 == 1 && datain == 0x77)
return 0x45 | id->t_releasing;
if (id->t_extended0 != 0) {
if (datain >= sizeof pckbd_xtbl_ext)
datain = 0;
else
datain = pckbd_xtbl_ext[datain];
} else {
if (datain >= sizeof pckbd_xtbl)
datain = 0;
else
datain = pckbd_xtbl[datain];
}
if (datain > 0x7f) {
datain &= 0x7f;
id->t_extended0 = 0x80;
}
if (datain == 0) {
return 0xff;
}
return datain | id->t_releasing;
}
static bool
pckbd_decode(struct pckbd_internal *id, int datain, u_int *type, int *dataout)
{
int key;
int releasing;
if (datain == KBR_EXTENDED0) {
id->t_extended0 = 0x80;
return false;
} else if (datain == KBR_EXTENDED1) {
id->t_extended1 = 2;
return false;
}
releasing = datain & 0x80;
datain &= 0x7f;
if (id->t_extended0 == 0x80) {
switch (datain) {
case 0x2a:
case 0x36:
id->t_extended0 = 0;
return false;
default:
break;
}
}
key = datain | id->t_extended0;
id->t_extended0 = 0;
if (id->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
id->t_extended1 = 1;
return false;
} else if (id->t_extended1 == 1 &&
(datain == 0x45 || datain == 0xc5)) {
id->t_extended1 = 0;
key = 0x7f;
} else if (id->t_extended1 > 0) {
id->t_extended1 = 0;
}
if (id->t_translating != 0) {
id->t_releasing = releasing;
} else {
}
if (id->t_releasing) {
id->t_releasing = 0;
id->t_lastchar = 0;
*type = WSCONS_EVENT_KEY_UP;
} else {
if (key == id->t_lastchar)
return false;
id->t_lastchar = key;
*type = WSCONS_EVENT_KEY_DOWN;
}
*dataout = key;
return true;
}
int
pckbd_init(struct pckbd_internal *t, pckbport_tag_t kbctag,
pckbport_slot_t kbcslot, int console)
{
memset(t, 0, sizeof(struct pckbd_internal));
t->t_isconsole = console;
t->t_kbctag = kbctag;
t->t_kbcslot = kbcslot;
return pckbd_set_xtscancode(kbctag, kbcslot, t);
}
static int
pckbd_led_encode(int led)
{
int res;
res = 0;
if (led & WSKBD_LED_SCROLL)
res |= 0x01;
if (led & WSKBD_LED_NUM)
res |= 0x02;
if (led & WSKBD_LED_CAPS)
res |= 0x04;
return res;
}
static int
pckbd_led_decode(int led)
{
int res;
res = 0;
if (led & 0x01)
res |= WSKBD_LED_SCROLL;
if (led & 0x02)
res |= WSKBD_LED_NUM;
if (led & 0x04)
res |= WSKBD_LED_CAPS;
return res;
}
void
pckbd_set_leds(void *v, int leds)
{
struct pckbd_softc *sc = v;
u_char cmd[2];
cmd[0] = KBC_MODEIND;
cmd[1] = pckbd_led_encode(leds);
sc->sc_ledstate = cmd[1];
(void)pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 2, 0, 0, 0);
}
void
pckbd_input(void *vsc, int data)
{
struct pckbd_softc *sc = vsc;
int key;
u_int type;
data = pckbd_scancode_translate(sc->id, data);
if (data == 0)
return;
#ifdef WSDISPLAY_COMPAT_RAWKBD
if (sc->rawkbd) {
u_char d = data;
wskbd_rawinput(sc->sc_wskbddev, &d, 1);
return;
}
#endif
if (pckbd_decode(sc->id, data, &type, &key))
wskbd_input(sc->sc_wskbddev, type, key);
}
int
pckbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct pckbd_softc *sc = v;
u_char cmdb[2];
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = WSKBD_TYPE_PC_XT;
return 0;
case WSKBDIO_SETLEDS:
cmdb[0] = KBC_MODEIND;
cmdb[1] = pckbd_led_encode(*(int *)data);
sc->sc_ledstate = cmdb[1];
return pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmdb, 2, 0, 1, 0);
case WSKBDIO_GETLEDS:
*(int *)data = pckbd_led_decode(sc->sc_ledstate);
return 0;
#if 0
case WSKBDIO_COMPLEXBELL:
#define d ((struct wskbd_bell_data *)data)
pckbd_bell(d->pitch, d->period, d->volume, 0);
#undef d
return 0;
#endif
#ifdef WSDISPLAY_COMPAT_RAWKBD
case WSKBDIO_SETMODE:
sc->rawkbd = (*(int *)data == WSKBD_RAW);
return 0;
#endif
}
return EPASSTHROUGH;
}
void
pckbd_bell(u_int pitch, u_int period, u_int volume, int poll)
{
if (pckbd_bell_fn != NULL)
(*pckbd_bell_fn)(pckbd_bell_fn_arg, pitch, period,
volume, poll);
}
void
pckbd_unhook_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
{
if (pckbd_bell_fn != fn && pckbd_bell_fn_arg != arg)
return;
pckbd_bell_fn = NULL;
pckbd_bell_fn_arg = NULL;
}
void
pckbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
{
if (pckbd_bell_fn == NULL) {
pckbd_bell_fn = fn;
pckbd_bell_fn_arg = arg;
}
}
int
pckbd_cnattach(pckbport_tag_t kbctag, int kbcslot)
{
int res;
u_char cmd[1];
res = pckbd_init(&pckbd_consdata, kbctag, kbcslot, 1);
#if defined(PCKBD_CNATTACH_MAY_FAIL)
if (res)
return res;
#endif
cmd[0] = KBC_ENABLE;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 0, 0, 0);
#if defined(PCKBD_CNATTACH_MAY_FAIL)
if (res)
return res;
#endif
wskbd_cnattach(&pckbd_consops, &pckbd_consdata, &pckbd_keymapdata);
return res;
}
void
pckbd_cngetc(void *v, u_int *type, int *data)
{
struct pckbd_internal *t = v;
int val;
for (;;) {
val = pckbport_poll_data(t->t_kbctag, t->t_kbcslot);
if (val == -1) {
*type = 0;
*data = 0;
return;
}
val = pckbd_scancode_translate(t, val);
if (val == 0)
continue;
if (pckbd_decode(t, val, type, data))
return;
}
}
void
pckbd_cnpollc(void *v, int on)
{
struct pckbd_internal *t = v;
pckbport_set_poll(t->t_kbctag, t->t_kbcslot, on);
}
void
pckbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
{
pckbd_bell(pitch, period, volume, 1);
}