#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vrdsiu_mouse.c,v 1.14 2021/08/07 16:18:54 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <machine/bus.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>
#include <hpcmips/vr/vripvar.h>
#include <hpcmips/vr/vrdsiureg.h>
#include <hpcmips/vr/cmureg.h>
enum vrdsiu_mouse_stat {
VRDSIU_MOUSE_STAT_DISABLE,
VRDSIU_MOUSE_STAT_ENABLE
};
enum vrdsiu_ps2_input_state {
VRDSIU_PS2_INPUT_STATE_BYTE0,
VRDSIU_PS2_INPUT_STATE_BYTE1,
VRDSIU_PS2_INPUT_STATE_BYTE2
};
struct vrdsiu_softc {
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
int sc_unit;
void *sc_handler;
vrip_chipset_tag_t sc_vrip;
enum vrdsiu_mouse_stat sc_mouse_stat;
device_t sc_wsmousedev;
};
static int asimOld = 0;
static int vrdsiu_match(device_t, cfdata_t, void *);
static void vrdsiu_attach(device_t, device_t, void *);
static void vrdsiu_write(struct vrdsiu_softc *, int, unsigned short);
static unsigned short vrdsiu_read(struct vrdsiu_softc *, int);
static int vrdsiu_intr(void *);
static void vrdsiu_mouse_intr(struct vrdsiu_softc *);
static int vrdsiu_mouse_enable(void *);
static int vrdsiu_mouse_ioctl(void *, u_long, void *, int, struct lwp *);
static void vrdsiu_mouse_disable(void *);
const struct wsmouse_accessops vrdsiu_accessops = {
vrdsiu_mouse_enable,
vrdsiu_mouse_ioctl,
vrdsiu_mouse_disable
};
CFATTACH_DECL_NEW(vrdsiu_mouse, sizeof(struct vrdsiu_softc),
vrdsiu_match, vrdsiu_attach, NULL, NULL);
static inline void
vrdsiu_write(struct vrdsiu_softc *sc, int port, unsigned short val)
{
bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
}
static inline unsigned short
vrdsiu_read(struct vrdsiu_softc *sc, int port)
{
return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
}
static int
vrdsiu_match(device_t parent, cfdata_t cf, void *aux)
{
return 1;
}
static void
vrdsiu_attach(device_t parent, device_t self, void *aux)
{
struct vrdsiu_softc *sc = device_private(self);
struct vrip_attach_args *va = aux;
struct wsmousedev_attach_args wsmaa;
int res;
bus_space_tag_t iot = va->va_iot;
if (va->va_parent_ioh != 0)
res = bus_space_subregion(iot, va->va_parent_ioh, va->va_addr,
va->va_size, &sc->sc_ioh);
else
res = bus_space_map(iot, va->va_addr, va->va_size, 0,
&sc->sc_ioh);
if (res != 0) {
printf(": can't map bus space\n");
return;
}
sc->sc_iot = iot;
sc->sc_unit = va->va_unit;
sc->sc_vrip = va->va_vc;
if (!(sc->sc_handler =
vrip_intr_establish(sc->sc_vrip, sc->sc_unit, 0, IPL_TTY,
vrdsiu_intr, sc))) {
printf(": can't map interrupt line\n");
return;
}
vrdsiu_mouse_disable(sc);
printf("\n");
wsmaa.accessops = &vrdsiu_accessops;
wsmaa.accesscookie = sc;
sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint,
CFARGS_NONE);
asimOld = vrdsiu_read(sc, DSIUASIM00_REG_W);
vrip_power(sc->sc_vrip, sc->sc_unit, 1);
}
int
vrdsiu_mouse_enable(void *v)
{
struct vrdsiu_softc *sc = v;
if (sc->sc_mouse_stat != VRDSIU_MOUSE_STAT_DISABLE)
return EBUSY;
sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_ENABLE;
vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 1);
vrip_power(sc->sc_vrip, sc->sc_unit, 1);
return 0;
}
void
vrdsiu_mouse_disable(void *v)
{
struct vrdsiu_softc *sc = v;
vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 0);
sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_DISABLE;
}
int
vrdsiu_mouse_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
switch (cmd)
{
case WSMOUSEIO_GTYPE:
*(u_int *)data = WSMOUSE_TYPE_PS2;
break;
case WSMOUSEIO_SRES:
break;
default:
return -1;
}
return 0;
}
int
vrdsiu_intr(void *arg)
{
struct vrdsiu_softc *sc = arg;
vrdsiu_mouse_intr(sc);
return 0;
}
#define PS2_L_BUTTON_MASK (1 << 0)
#define PS2_R_BUTTON_MASK (1 << 1)
#define PS2_M_BUTTON_MASK (1 << 2)
#define PS2_BYTE0_BIT3_MASK (1 << 3)
#define PS2_DX_SIGN_MASK (1 << 4)
#define PS2_DY_SIGN_MASK (1 << 5)
#define PS2_DX_OVERFLOW_MASK (1 << 6)
#define PS2_DY_OVERFLOW_MASK (1 << 7)
#define WSC_L_BUTTON 0x01
#define WSC_M_BUTTON 0x02
#define WSC_R_BUTTON 0x04
void
vrdsiu_mouse_intr(struct vrdsiu_softc *sc)
{
u_int intrReason;
unsigned char b;
static int dx;
static int dy;
static u_char buttons;
static enum vrdsiu_ps2_input_state ps2_state = 0;
intrReason = vrdsiu_read(sc, DSIUINTR0_REG_W);
vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld & ~DSIUASIM00_RXE0);
if (intrReason & DSIUINTR0_INTSER0)
ps2_state = 0;
if ((intrReason & DSIUINTR0_INTSR0) == 0)
goto done;
b = (unsigned char)vrdsiu_read(sc, DSIURXB0L_REG_W);
if (sc->sc_mouse_stat == VRDSIU_MOUSE_STAT_DISABLE)
{
goto done;
}
switch (ps2_state)
{
case VRDSIU_PS2_INPUT_STATE_BYTE0:
if ((b & PS2_BYTE0_BIT3_MASK) == 0)
goto done;
if (b & (PS2_M_BUTTON_MASK | PS2_DX_OVERFLOW_MASK |
PS2_DY_OVERFLOW_MASK))
goto done;
buttons = ((b & PS2_L_BUTTON_MASK) ? WSC_L_BUTTON : 0)
| ((b & PS2_M_BUTTON_MASK) ? WSC_M_BUTTON : 0)
| ((b & PS2_R_BUTTON_MASK) ? WSC_R_BUTTON : 0);
ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE1;
break;
case VRDSIU_PS2_INPUT_STATE_BYTE1:
dx = (signed char)b;
if (dx == -128)
dx = -127;
ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE2;
break;
case VRDSIU_PS2_INPUT_STATE_BYTE2:
dy = (signed char)b;
if (dy == -128)
dy = -127;
wsmouse_input(sc->sc_wsmousedev,
buttons,
dx, dy, 0, 0,
WSMOUSE_INPUT_DELTA);
ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE0;
break;
}
done:
vrdsiu_write(sc, DSIUINTR0_REG_W, intrReason);
vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld | DSIUASIM00_RXE0);
}