#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ms_pckbport.c,v 1.9 2012/10/27 17:18:11 chs Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <machine/autoconf.h>
#include <sys/bus.h>
#include <machine/intr.h>
#include <dev/pckbport/pckbportvar.h>
#include <dev/pckbport/pmsreg.h>
#include <machine/vuid_event.h>
#include <dev/sun/event_var.h>
#include <dev/sun/msvar.h>
struct ms_pckbport_softc {
struct ms_softc sc_ms;
pckbport_tag_t sc_kbctag;
pckbport_slot_t sc_kbcslot;
int sc_enabled;
};
static int ms_pckbport_match(device_t, cfdata_t, void *);
static void ms_pckbport_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(ms_pckbport, sizeof(struct ms_pckbport_softc),
ms_pckbport_match, ms_pckbport_attach, NULL, NULL);
static int ms_pckbport_iopen(device_t, int);
static int ms_pckbport_iclose(device_t, int);
static void ms_pckbport_input(void *, int);
static int
ms_pckbport_match(device_t parent, cfdata_t cf, void *aux)
{
struct pckbport_attach_args *pa = aux;
return (pa->pa_slot == PCKBPORT_AUX_SLOT);
}
static void
ms_pckbport_attach(device_t parent, device_t self, void *aux)
{
struct ms_pckbport_softc *sc = device_private(self);
struct ms_softc *ms = &sc->sc_ms;
struct pckbport_attach_args *pa = aux;
u_char cmd[1], resp[2];
int res;
ms->ms_dev = self;
sc->sc_kbctag = pa->pa_tag;
sc->sc_kbcslot = pa->pa_slot;
ms->ms_deviopen = ms_pckbport_iopen;
ms->ms_deviclose = ms_pckbport_iclose;
aprint_normal("\n");
cmd[0] = PMS_RESET;
res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot,
cmd, 1, 2, resp, 1);
#ifdef DIAGNOSTIC
if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
aprint_error("%s: reset error\n", __func__);
}
#endif
pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
ms_pckbport_input, sc, device_xname(self));
cmd[0] = PMS_DEV_DISABLE;
res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
1, 0, 0, 0);
if (res)
aprint_error("%s: failed to disable interrupts\n", __func__);
pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
}
static int
ms_pckbport_iopen(device_t self, int flags)
{
struct ms_pckbport_softc *sc = device_private(self);
struct ms_softc *ms = &sc->sc_ms;
u_char cmd[1];
int res;
ms->ms_byteno = 0;
ms->ms_dx = ms->ms_dy = 0;
ms->ms_ub = ms->ms_mb = 0;
pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
cmd[0] = PMS_DEV_ENABLE;
res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
cmd, 1, 0, 1, NULL);
if (res) {
printf("%s: command error\n", __func__);
return (res);
}
sc->sc_enabled = 1;
return (0);
}
static int
ms_pckbport_iclose(device_t self, int flags)
{
struct ms_pckbport_softc *sc = device_private(self);
u_char cmd[1];
int res;
cmd[0] = PMS_DEV_DISABLE;
res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
cmd, 1, 0, 1, NULL);
if (res)
printf("%s: command error\n", __func__);
pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
sc->sc_enabled = 0;
return (0);
}
#define PS2LBUTMASK 0x01
#define PS2RBUTMASK 0x02
#define PS2MBUTMASK 0x04
static void
ms_pckbport_input(void *vsc, int data)
{
struct ms_pckbport_softc *sc = vsc;
struct ms_softc *ms = &sc->sc_ms;
struct firm_event *fe;
int mb, ub, d, get, put, any;
static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
static const int to_id[] = { MS_LEFT, MS_MIDDLE, 0, MS_RIGHT };
if (!sc->sc_enabled) {
return;
}
switch (ms->ms_byteno) {
case 0:
if ((data & 0xc0) == 0) {
ms->ms_mb =
((data & PS2LBUTMASK) ? 0x1 : 0) |
((data & PS2MBUTMASK) ? 0x2 : 0) |
((data & PS2RBUTMASK) ? 0x4 : 0) ;
++ms->ms_byteno;
}
return;
case 1:
ms->ms_dx += (int8_t)data;
++ms->ms_byteno;
return;
case 2:
ms->ms_dy += (int8_t)data;
ms->ms_byteno = 0;
break;
}
any = 0;
get = ms->ms_events.ev_get;
put = ms->ms_events.ev_put;
fe = &ms->ms_events.ev_q[put];
#define NEXT do { \
if ((++put) % EV_QSIZE == get) { \
--put; \
goto out; \
} \
} while (0)
#define ADVANCE do { \
++fe; \
if (put >= EV_QSIZE) { \
put = 0; \
fe = &ms->ms_events.ev_q[0]; \
} \
any = 1; \
} while (0)
ub = ms->ms_ub;
mb = ms->ms_mb;
while ((d = mb ^ ub) != 0) {
NEXT;
d = to_one[d - 1];
fe->id = to_id[d - 1];
fe->value = (mb & d) ? VKEY_DOWN : VKEY_UP;
firm_gettime(fe);
ADVANCE;
ub ^= d;
}
if (ms->ms_dx != 0) {
NEXT;
fe->id = LOC_X_DELTA;
fe->value = ms->ms_dx;
firm_gettime(fe);
ADVANCE;
ms->ms_dx = 0;
}
if (ms->ms_dy != 0) {
NEXT;
fe->id = LOC_Y_DELTA;
fe->value = ms->ms_dy;
firm_gettime(fe);
ADVANCE;
ms->ms_dy = 0;
}
out:
if (any) {
ms->ms_ub = ub;
ms->ms_events.ev_put = put;
EV_WAKEUP(&ms->ms_events);
}
}