#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nextkbd.c,v 1.19 2023/02/03 23:13:00 tsutsui Exp $");
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/queue.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/intr.h>
#include <machine/autoconf.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>
#include <next68k/dev/nextkbdvar.h>
#include <next68k/dev/wskbdmap_next.h>
#include <next68k/next68k/isr.h>
#include <next68k/dev/intiovar.h>
struct nextkbd_internal {
int num_ints;
int polling;
int isconsole;
bus_space_tag_t iot;
bus_space_handle_t ioh;
struct nextkbd_softc *t_sc;
uint32_t mods;
};
struct mon_regs {
uint32_t mon_csr;
uint32_t mon_1;
uint32_t mon_data;
};
static int attached = 0;
int nextkbd_match(device_t, cfdata_t, void *);
void nextkbd_attach(device_t, device_t, void *);
int nextkbc_cnattach(bus_space_tag_t);
CFATTACH_DECL_NEW(nextkbd, sizeof(struct nextkbd_softc),
nextkbd_match, nextkbd_attach, NULL, NULL);
int nextkbd_enable(void *, int);
void nextkbd_set_leds(void *, int);
int nextkbd_ioctl(void *, u_long, void *, int, struct lwp *);
const struct wskbd_accessops nextkbd_accessops = {
nextkbd_enable,
nextkbd_set_leds,
nextkbd_ioctl,
};
void nextkbd_cngetc(void *, u_int *, int *);
void nextkbd_cnpollc(void *, int);
const struct wskbd_consops nextkbd_consops = {
nextkbd_cngetc,
nextkbd_cnpollc,
};
const struct wskbd_mapdata nextkbd_keymapdata = {
nextkbd_keydesctab,
KB_US,
};
static int nextkbd_read_data(struct nextkbd_internal *);
static int nextkbd_decode(struct nextkbd_internal *, int, u_int *, int *);
static struct nextkbd_internal nextkbd_consdata;
static int nextkbd_is_console(bus_space_tag_t);
int nextkbdhard(void *);
static int
nextkbd_is_console(bus_space_tag_t bst)
{
return (nextkbd_consdata.isconsole && (bst == nextkbd_consdata.iot));
}
int
nextkbd_match(device_t parent, cfdata_t match, void *aux)
{
struct intio_attach_args *ia = (struct intio_attach_args *)aux;
if (attached)
return 0;
ia->ia_addr = (void *)NEXT_P_MON;
return 1;
}
void
nextkbd_attach(device_t parent, device_t self, void *aux)
{
struct nextkbd_softc *sc = device_private(self);
struct intio_attach_args *ia = aux;
int isconsole;
struct wskbddev_attach_args a;
printf("\n");
isconsole = nextkbd_is_console(ia->ia_bst);
if (isconsole) {
sc->id = &nextkbd_consdata;
} else {
sc->id = kmem_zalloc(sizeof(struct nextkbd_internal), KM_SLEEP);
sc->id->iot = ia->ia_bst;
if (bus_space_map(sc->id->iot, NEXT_P_MON,
sizeof(struct mon_regs), 0, &sc->id->ioh)) {
printf("%s: can't map mon status control register\n",
device_xname(self));
return;
}
}
sc->id->t_sc = sc;
isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE),
0, NULL);
INTR_ENABLE(NEXT_I_KYBD_MOUSE);
a.console = isconsole;
a.keymap = &nextkbd_keymapdata;
a.accessops = &nextkbd_accessops;
a.accesscookie = sc;
sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
attached = 1;
}
int
nextkbd_enable(void *v, int on)
{
return 0;
}
void
nextkbd_set_leds(void *v, int leds)
{
struct nextkbd_softc *sc = v;
uint32_t hw_leds = 0;
int s;
sc->sc_leds &= ~ NEXT_WSKBD_LEDS;
sc->sc_leds |= (leds & NEXT_WSKBD_LEDS);
if (sc->sc_leds & WSKBD_LED_CAPS) {
hw_leds |= 0x30000;
}
s = spltty();
bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5);
bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds);
splx(s);
}
int
nextkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct nextkbd_softc *sc = v;
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = WSKBD_TYPE_NEXT;
return 0;
case WSKBDIO_SETLEDS:
nextkbd_set_leds (sc, *(int *)data);
return 0;
case WSKBDIO_GETLEDS:
*(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS;
return 0;
case WSKBDIO_COMPLEXBELL:
return 0;
}
return EPASSTHROUGH;
}
int
nextkbdhard(void *arg)
{
struct nextkbd_softc *sc = arg;
int type, key, val;
if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE))
return 0;
#define CSR_INT 0x00800000
#define CSR_DATA 0x00400000
#define KD_KEYMASK 0x007f
#define KD_DIRECTION 0x0080
#define KD_CNTL 0x0100
#define KD_LSHIFT 0x0200
#define KD_RSHIFT 0x0400
#define KD_LCOMM 0x0800
#define KD_RCOMM 0x1000
#define KD_LALT 0x2000
#define KD_RALT 0x4000
#define KD_VALID 0x8000
#define KD_MODS 0x4f00
val = nextkbd_read_data(sc->id);
if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) {
wskbd_input(sc->sc_wskbddev, type, key);
}
return 1;
}
int
nextkbd_cnattach(bus_space_tag_t bst)
{
bus_space_handle_t bsh;
if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs), 0, &bsh))
return ENXIO;
memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
nextkbd_consdata.iot = bst;
nextkbd_consdata.ioh = bsh;
nextkbd_consdata.isconsole = 1;
wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
&nextkbd_keymapdata);
return 0;
}
void
nextkbd_cngetc(void *v, u_int *type, int *data)
{
struct nextkbd_internal *t = v;
int val;
for (;;) {
if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) {
val = nextkbd_read_data(t);
if (val != -1 && nextkbd_decode(t, val, type, data))
return;
}
}
}
void
nextkbd_cnpollc(void *v, int on)
{
struct nextkbd_internal *t = v;
t->polling = on;
if (on) {
INTR_DISABLE(NEXT_I_KYBD_MOUSE);
} else {
INTR_ENABLE(NEXT_I_KYBD_MOUSE);
}
}
static int
nextkbd_read_data(struct nextkbd_internal *id)
{
unsigned char device;
struct mon_regs stat = { 0 };
bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3);
if ((stat.mon_csr & CSR_INT) != 0 &&
(stat.mon_csr & CSR_DATA) != 0) {
stat.mon_csr &= ~CSR_INT;
id->num_ints++;
bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr);
device = stat.mon_data >> 28;
if (device != 1)
return -1;
return stat.mon_data & 0xffff;
}
return -1;
}
static int
nextkbd_decode(struct nextkbd_internal *id, int datain, u_int *type,
int *dataout)
{
#if 0
printf("datain %08x mods %08x\n", datain, id->mods);
#endif
if (((datain ^ id->mods) & KD_LSHIFT) != 0) {
id->mods ^= KD_LSHIFT;
*dataout = 90;
if (datain & KD_LSHIFT)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RSHIFT) != 0) {
id->mods ^= KD_RSHIFT;
*dataout = 91;
if ((datain & KD_RSHIFT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_LALT) != 0) {
id->mods ^= KD_LALT;
*dataout = 92;
if ((datain & KD_LALT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RALT) != 0) {
id->mods ^= KD_RALT;
*dataout = 93;
if ((datain & KD_RALT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_CNTL) != 0) {
id->mods ^= KD_CNTL;
*dataout = 94;
if ((datain & KD_CNTL) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_LCOMM) != 0) {
id->mods ^= KD_LCOMM;
*dataout = 95;
if ((datain & KD_LCOMM) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RCOMM) != 0) {
id->mods ^= KD_RCOMM;
*dataout = 96;
if ((datain & KD_RCOMM) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if ((datain & KD_KEYMASK) != 0) {
if ((datain & KD_DIRECTION) != 0)
*type = WSCONS_EVENT_KEY_UP;
else
*type = WSCONS_EVENT_KEY_DOWN;
*dataout = (datain & KD_KEYMASK);
} else {
*dataout = 0;
}
return 1;
}