xref: /netbsd-src/sys/arch/pmax/tc/dtms.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: dtms.c,v 1.13 2021/08/07 16:19:02 thorpej Exp $	*/
29aa9b8ebSad 
39aa9b8ebSad /*-
49aa9b8ebSad  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
59aa9b8ebSad  * All rights reserved.
69aa9b8ebSad  *
79aa9b8ebSad  * This code is derived from software contributed to The NetBSD Foundation
89aa9b8ebSad  * by Andrew Doran.
99aa9b8ebSad  *
109aa9b8ebSad  * Redistribution and use in source and binary forms, with or without
119aa9b8ebSad  * modification, are permitted provided that the following conditions
129aa9b8ebSad  * are met:
139aa9b8ebSad  * 1. Redistributions of source code must retain the above copyright
149aa9b8ebSad  *    notice, this list of conditions and the following disclaimer.
159aa9b8ebSad  * 2. Redistributions in binary form must reproduce the above copyright
169aa9b8ebSad  *    notice, this list of conditions and the following disclaimer in the
179aa9b8ebSad  *    documentation and/or other materials provided with the distribution.
189aa9b8ebSad  *
199aa9b8ebSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209aa9b8ebSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219aa9b8ebSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229aa9b8ebSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239aa9b8ebSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249aa9b8ebSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259aa9b8ebSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269aa9b8ebSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279aa9b8ebSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289aa9b8ebSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299aa9b8ebSad  * POSSIBILITY OF SUCH DAMAGE.
309aa9b8ebSad  */
319aa9b8ebSad 
329aa9b8ebSad #include <sys/cdefs.h>
33*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: dtms.c,v 1.13 2021/08/07 16:19:02 thorpej Exp $");
349aa9b8ebSad 
359aa9b8ebSad #include "locators.h"
369aa9b8ebSad 
379aa9b8ebSad #include <sys/param.h>
38a0640b2bSmatt #include <sys/bus.h>
399aa9b8ebSad #include <sys/device.h>
409aa9b8ebSad #include <sys/ioctl.h>
419aa9b8ebSad #include <sys/syslog.h>
42a0640b2bSmatt #include <sys/systm.h>
439aa9b8ebSad 
44a0640b2bSmatt #include <pmax/tc/dtreg.h>
45a0640b2bSmatt #include <pmax/tc/dtvar.h>
469aa9b8ebSad 
479aa9b8ebSad #include <dev/wscons/wsconsio.h>
489aa9b8ebSad #include <dev/wscons/wsmousevar.h>
499aa9b8ebSad 
509aa9b8ebSad struct dtms_softc {
5103335d0bStsutsui 	device_t	sc_dev;
5203335d0bStsutsui 	device_t	sc_wsmousedev;
539aa9b8ebSad 	int		sc_enabled;
549aa9b8ebSad };
559aa9b8ebSad 
5603335d0bStsutsui int	dtms_match(device_t, cfdata_t, void *);
5703335d0bStsutsui void	dtms_attach(device_t, device_t, void *);
589aa9b8ebSad int	dtms_input(void *, int);
599aa9b8ebSad int	dtms_enable(void *);
6053524e44Schristos int	dtms_ioctl(void *, u_long, void *, int, struct lwp *);
619aa9b8ebSad void	dtms_disable(void *);
62520489e1Sad void	dtms_handler(void *, struct dt_msg *);
639aa9b8ebSad 
6403335d0bStsutsui CFATTACH_DECL_NEW(dtms, sizeof(struct dtms_softc),
659aa9b8ebSad     dtms_match, dtms_attach, NULL, NULL);
669aa9b8ebSad 
679aa9b8ebSad const struct wsmouse_accessops dtms_accessops = {
689aa9b8ebSad 	dtms_enable,
699aa9b8ebSad 	dtms_ioctl,
709aa9b8ebSad 	dtms_disable,
719aa9b8ebSad };
729aa9b8ebSad 
739aa9b8ebSad int
dtms_match(device_t parent,cfdata_t cf,void * aux)7403335d0bStsutsui dtms_match(device_t parent, cfdata_t cf, void *aux)
759aa9b8ebSad {
769aa9b8ebSad 	struct dt_attach_args *dta;
779aa9b8ebSad 
789aa9b8ebSad 	dta = aux;
799aa9b8ebSad 	return (dta->dta_addr == DT_ADDR_MOUSE);
809aa9b8ebSad }
819aa9b8ebSad 
829aa9b8ebSad void
dtms_attach(device_t parent,device_t self,void * aux)8303335d0bStsutsui dtms_attach(device_t parent, device_t self, void *aux)
849aa9b8ebSad {
859aa9b8ebSad 	struct wsmousedev_attach_args a;
869aa9b8ebSad 	struct dtms_softc *sc;
879aa9b8ebSad 	struct dt_softc *dt;
889aa9b8ebSad 
8903335d0bStsutsui 	dt = device_private(parent);
9003335d0bStsutsui 	sc = device_private(self);
9103335d0bStsutsui 	sc->sc_dev = self;
929aa9b8ebSad 
939aa9b8ebSad 	printf("\n");
949aa9b8ebSad 
9503335d0bStsutsui 	if (dt_establish_handler(dt, &dt_ms_dv, sc, dtms_handler)) {
9603335d0bStsutsui 		printf("%s: unable to establish handler\n", device_xname(self));
979aa9b8ebSad 		return;
989aa9b8ebSad 	}
999aa9b8ebSad 
1009aa9b8ebSad 	a.accessops = &dtms_accessops;
1019aa9b8ebSad 	a.accesscookie = sc;
102*c7fb772bSthorpej 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
1039aa9b8ebSad }
1049aa9b8ebSad 
1059aa9b8ebSad int
dtms_enable(void * cookie)1069aa9b8ebSad dtms_enable(void *cookie)
1079aa9b8ebSad {
1089aa9b8ebSad 	struct dtms_softc *sc;
1099aa9b8ebSad 
1109aa9b8ebSad 	sc = cookie;
1119aa9b8ebSad 	if (sc->sc_enabled)
1129aa9b8ebSad 		return (EBUSY);
1139aa9b8ebSad 	sc->sc_enabled = 1;
1149aa9b8ebSad 
1159aa9b8ebSad 	return (0);
1169aa9b8ebSad }
1179aa9b8ebSad 
1189aa9b8ebSad void
dtms_disable(void * cookie)1199aa9b8ebSad dtms_disable(void *cookie)
1209aa9b8ebSad {
1219aa9b8ebSad 	struct dtms_softc *sc;
1229aa9b8ebSad 
1239aa9b8ebSad 	sc = cookie;
1249aa9b8ebSad 	sc->sc_enabled = 0;
1259aa9b8ebSad }
1269aa9b8ebSad 
1279aa9b8ebSad int
dtms_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)12853524e44Schristos dtms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
1299aa9b8ebSad {
1309aa9b8ebSad 
1319aa9b8ebSad 	if (cmd == WSMOUSEIO_GTYPE) {
1329aa9b8ebSad 		*(u_int *)data = WSMOUSE_TYPE_VSXXX;
1339aa9b8ebSad 		return (0);
1349aa9b8ebSad 	}
1359aa9b8ebSad 
136248d777dSmhitch 	return (EPASSTHROUGH);
1379aa9b8ebSad }
1389aa9b8ebSad 
1399aa9b8ebSad void
dtms_handler(void * cookie,struct dt_msg * msg)140520489e1Sad dtms_handler(void *cookie, struct dt_msg *msg)
1419aa9b8ebSad {
1429aa9b8ebSad 	struct dtms_softc *sc;
1434deb82dbSmhitch 	int buttons, dx, dy;
1444deb82dbSmhitch 	short tmp;
1459aa9b8ebSad 
146520489e1Sad 	sc = cookie;
1479aa9b8ebSad 
148520489e1Sad 	if (!sc->sc_enabled)
149520489e1Sad 		return;
150520489e1Sad 
151520489e1Sad 	tmp = DT_GET_SHORT(msg->body[0], msg->body[1]);
1524deb82dbSmhitch 	buttons = tmp & 1;
1534deb82dbSmhitch 	if (tmp & 2)
1544deb82dbSmhitch 		buttons |= 4;
1554deb82dbSmhitch 	if (tmp & 4)
1564deb82dbSmhitch 		buttons |= 2;
1579aa9b8ebSad 
158520489e1Sad 	tmp = DT_GET_SHORT(msg->body[2], msg->body[3]);
1599aa9b8ebSad 	if (tmp < 0)
1609aa9b8ebSad 		dx = -(-tmp & 0x1f);
1619aa9b8ebSad 	else
1629aa9b8ebSad 		dx = tmp & 0x1f;
1639aa9b8ebSad 
164520489e1Sad 	tmp = DT_GET_SHORT(msg->body[4], msg->body[5]);
1659aa9b8ebSad 	if (tmp < 0)
1669aa9b8ebSad 		dy = -(-tmp & 0x1f);
1679aa9b8ebSad 	else
1689aa9b8ebSad 		dy = tmp & 0x1f;
1699aa9b8ebSad 
17057c0199dSplunky 	wsmouse_input(sc->sc_wsmousedev,
17157c0199dSplunky 			buttons,
17257c0199dSplunky 			dx, dy, 0, 0,
1739aa9b8ebSad 			WSMOUSE_INPUT_DELTA);
1749aa9b8ebSad }
175