1 /* $NetBSD: dtms.c,v 1.13 2021/08/07 16:19:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: dtms.c,v 1.13 2021/08/07 16:19:02 thorpej Exp $");
34
35 #include "locators.h"
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/syslog.h>
42 #include <sys/systm.h>
43
44 #include <pmax/tc/dtreg.h>
45 #include <pmax/tc/dtvar.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsmousevar.h>
49
50 struct dtms_softc {
51 device_t sc_dev;
52 device_t sc_wsmousedev;
53 int sc_enabled;
54 };
55
56 int dtms_match(device_t, cfdata_t, void *);
57 void dtms_attach(device_t, device_t, void *);
58 int dtms_input(void *, int);
59 int dtms_enable(void *);
60 int dtms_ioctl(void *, u_long, void *, int, struct lwp *);
61 void dtms_disable(void *);
62 void dtms_handler(void *, struct dt_msg *);
63
64 CFATTACH_DECL_NEW(dtms, sizeof(struct dtms_softc),
65 dtms_match, dtms_attach, NULL, NULL);
66
67 const struct wsmouse_accessops dtms_accessops = {
68 dtms_enable,
69 dtms_ioctl,
70 dtms_disable,
71 };
72
73 int
dtms_match(device_t parent,cfdata_t cf,void * aux)74 dtms_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 struct dt_attach_args *dta;
77
78 dta = aux;
79 return (dta->dta_addr == DT_ADDR_MOUSE);
80 }
81
82 void
dtms_attach(device_t parent,device_t self,void * aux)83 dtms_attach(device_t parent, device_t self, void *aux)
84 {
85 struct wsmousedev_attach_args a;
86 struct dtms_softc *sc;
87 struct dt_softc *dt;
88
89 dt = device_private(parent);
90 sc = device_private(self);
91 sc->sc_dev = self;
92
93 printf("\n");
94
95 if (dt_establish_handler(dt, &dt_ms_dv, sc, dtms_handler)) {
96 printf("%s: unable to establish handler\n", device_xname(self));
97 return;
98 }
99
100 a.accessops = &dtms_accessops;
101 a.accesscookie = sc;
102 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
103 }
104
105 int
dtms_enable(void * cookie)106 dtms_enable(void *cookie)
107 {
108 struct dtms_softc *sc;
109
110 sc = cookie;
111 if (sc->sc_enabled)
112 return (EBUSY);
113 sc->sc_enabled = 1;
114
115 return (0);
116 }
117
118 void
dtms_disable(void * cookie)119 dtms_disable(void *cookie)
120 {
121 struct dtms_softc *sc;
122
123 sc = cookie;
124 sc->sc_enabled = 0;
125 }
126
127 int
dtms_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)128 dtms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
129 {
130
131 if (cmd == WSMOUSEIO_GTYPE) {
132 *(u_int *)data = WSMOUSE_TYPE_VSXXX;
133 return (0);
134 }
135
136 return (EPASSTHROUGH);
137 }
138
139 void
dtms_handler(void * cookie,struct dt_msg * msg)140 dtms_handler(void *cookie, struct dt_msg *msg)
141 {
142 struct dtms_softc *sc;
143 int buttons, dx, dy;
144 short tmp;
145
146 sc = cookie;
147
148 if (!sc->sc_enabled)
149 return;
150
151 tmp = DT_GET_SHORT(msg->body[0], msg->body[1]);
152 buttons = tmp & 1;
153 if (tmp & 2)
154 buttons |= 4;
155 if (tmp & 4)
156 buttons |= 2;
157
158 tmp = DT_GET_SHORT(msg->body[2], msg->body[3]);
159 if (tmp < 0)
160 dx = -(-tmp & 0x1f);
161 else
162 dx = tmp & 0x1f;
163
164 tmp = DT_GET_SHORT(msg->body[4], msg->body[5]);
165 if (tmp < 0)
166 dy = -(-tmp & 0x1f);
167 else
168 dy = tmp & 0x1f;
169
170 wsmouse_input(sc->sc_wsmousedev,
171 buttons,
172 dx, dy, 0, 0,
173 WSMOUSE_INPUT_DELTA);
174 }
175