xref: /netbsd/sys/arch/sgimips/dev/zs_ms.c (revision beecddb6)
1 /*	$NetBSD: zs_ms.c,v 1.10 2021/08/07 16:19:04 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Steve Rumble
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * IP12/IP20 serial mouse driver attached to zs channel 1 at 4800bps.
32  * This layer feeds wsmouse.
33  *
34  * 5 byte packets: sync, x1, y1, x2, y2
35  * sync format: binary 10000LMR (left, middle, right) 0 is down
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: zs_ms.c,v 1.10 2021/08/07 16:19:04 thorpej Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsmousevar.h>
48 
49 #include <dev/ic/z8530reg.h>
50 #include <machine/machtype.h>
51 #include <machine/z8530var.h>
52 
53 #define ZSMS_BAUD	4800
54 #define ZSMS_RXQ_LEN	64	/* power of 2 */
55 
56 /* protocol */
57 #define ZSMS_SYNC		0x80
58 #define ZSMS_SYNC_MASK		0xf8
59 #define ZSMS_SYNC_BTN_R		0x01
60 #define ZSMS_SYNC_BTN_M		0x02
61 #define ZSMS_SYNC_BTN_L		0x04
62 #define ZSMS_SYNC_BTN_MASK	0x07
63 
64 struct zsms_softc {
65 	device_t	sc_dev;
66 
67 	/* tail-chasing fifo */
68 	uint8_t		rxq[ZSMS_RXQ_LEN];
69 	uint8_t		rxq_head;
70 	uint8_t		rxq_tail;
71 
72 	/* 5-byte packet as described above */
73 #define	ZSMS_PACKET_SYNC	0
74 #define	ZSMS_PACKET_X1		1
75 #define	ZSMS_PACKET_Y1		2
76 #define	ZSMS_PACKET_X2		3
77 #define	ZSMS_PACKET_Y2		4
78 	int8_t		packet[5];
79 
80 #define ZSMS_STATE_SYNC	0x01
81 #define ZSMS_STATE_X1	0x02
82 #define ZSMS_STATE_Y1	0x04
83 #define ZSMS_STATE_X2	0x08
84 #define ZSMS_STATE_Y2	0x10
85 	uint8_t		state;
86 
87 	/* wsmouse bits */
88 	int		enabled;
89 	device_t	wsmousedev;
90 };
91 
92 static int	zsms_match(device_t, cfdata_t, void *);
93 static void	zsms_attach(device_t, device_t, void *);
94 static void	zsms_rxint(struct zs_chanstate *);
95 static void	zsms_txint(struct zs_chanstate *);
96 static void	zsms_stint(struct zs_chanstate *, int);
97 static void	zsms_softint(struct zs_chanstate *);
98 
99 static void	zsms_wsmouse_input(struct zsms_softc *);
100 static int	zsms_wsmouse_enable(void *);
101 static void	zsms_wsmouse_disable(void *);
102 static int	zsms_wsmouse_ioctl(void *, u_long, void *, int,
103 						   struct lwp *);
104 
105 CFATTACH_DECL_NEW(zsms, sizeof(struct zsms_softc),
106     zsms_match, zsms_attach, NULL, NULL);
107 
108 static struct zsops zsms_zsops = {
109 	zsms_rxint,
110 	zsms_stint,
111 	zsms_txint,
112 	zsms_softint
113 };
114 
115 static const struct wsmouse_accessops zsms_wsmouse_accessops = {
116 	zsms_wsmouse_enable,
117 	zsms_wsmouse_ioctl,
118 	zsms_wsmouse_disable
119 };
120 
121 int
zsms_match(device_t parent,cfdata_t cf,void * aux)122 zsms_match(device_t parent, cfdata_t cf, void *aux)
123 {
124 
125 	if (mach_type == MACH_SGI_IP12 || mach_type == MACH_SGI_IP20) {
126 		struct zsc_attach_args *args = aux;
127 
128 		if (args->channel == 1)
129 			return (1);
130 	}
131 
132 	return (0);
133 }
134 
135 void
zsms_attach(device_t parent,device_t self,void * aux)136 zsms_attach(device_t parent, device_t self, void *aux)
137 {
138 	int				s, channel;
139 	struct zsc_softc	       *zsc;
140 	struct zsms_softc	       *sc;
141 	struct zsc_attach_args	       *args;
142 	struct zs_chanstate	       *cs;
143 	struct wsmousedev_attach_args	wsmaa;
144 
145 	zsc = device_private(parent);
146 	sc = device_private(self);
147 	sc->sc_dev = self;
148 	args = aux;
149 
150 	/* Establish ourself with the MD z8530 driver */
151 	channel = args->channel;
152 	cs = zsc->zsc_cs[channel];
153 	cs->cs_ops = &zsms_zsops;
154 	cs->cs_private = sc;
155 
156 	sc->enabled = 0;
157 	sc->rxq_head = 0;
158 	sc->rxq_tail = 0;
159 	sc->state = ZSMS_STATE_SYNC;
160 
161 	aprint_normal(": baud rate %d\n", ZSMS_BAUD);
162 
163 	s = splzs();
164 	zs_write_reg(cs, 9, (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET);
165 	cs->cs_preg[1] = ZSWR1_RIE;
166 	zs_set_speed(cs, ZSMS_BAUD);
167 	zs_loadchannelregs(cs);
168 	splx(s);
169 
170 	/* attach wsmouse */
171 	wsmaa.accessops =	&zsms_wsmouse_accessops;
172 	wsmaa.accesscookie =	sc;
173 	sc->wsmousedev =	config_found(self, &wsmaa, wsmousedevprint,
174 					     CFARGS_NONE);
175 }
176 
177 void
zsms_rxint(struct zs_chanstate * cs)178 zsms_rxint(struct zs_chanstate *cs)
179 {
180 	uint8_t c, r;
181 	struct zsms_softc *sc = cs->cs_private;
182 
183 	/* clear errors */
184 	r = zs_read_reg(cs, 1);
185 	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
186 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
187 
188 	/* read byte and append to our queue */
189 	c = zs_read_data(cs);
190 
191 	sc->rxq[sc->rxq_tail] = c;
192 	sc->rxq_tail = (sc->rxq_tail + 1) & ~ZSMS_RXQ_LEN;
193 
194 	cs->cs_softreq = 1;
195 }
196 
197 /* We should never get here. */
198 void
zsms_txint(struct zs_chanstate * cs)199 zsms_txint(struct zs_chanstate *cs)
200 {
201 
202 	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
203 
204 	/* seems like the in thing to do */
205 	cs->cs_softreq = 1;
206 }
207 
208 void
zsms_stint(struct zs_chanstate * cs,int force)209 zsms_stint(struct zs_chanstate *cs, int force)
210 {
211 
212 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
213 	cs->cs_softreq = 1;
214 }
215 
216 void
zsms_softint(struct zs_chanstate * cs)217 zsms_softint(struct zs_chanstate *cs)
218 {
219 	struct zsms_softc *sc = cs->cs_private;
220 
221 	/* No need to keep score if nobody is listening */
222 	if (!sc->enabled) {
223 		sc->rxq_head = sc->rxq_tail;
224 		return;
225 	}
226 
227 	/*
228 	 * Here's the real action. Read a full packet and
229 	 * then let wsmouse know what has happened.
230 	 */
231 	while (sc->rxq_head != sc->rxq_tail) {
232 		int8_t c = sc->rxq[sc->rxq_head];
233 
234 		switch (sc->state) {
235 		case ZSMS_STATE_SYNC:
236 			if ((c & ZSMS_SYNC_MASK) == ZSMS_SYNC) {
237 				sc->packet[ZSMS_PACKET_SYNC] = c;
238 				sc->state = ZSMS_STATE_X1;
239 			}
240 			break;
241 
242 		case ZSMS_STATE_X1:
243 			sc->packet[ZSMS_PACKET_X1] = c;
244 			sc->state = ZSMS_STATE_Y1;
245 			break;
246 
247 		case ZSMS_STATE_Y1:
248 			sc->packet[ZSMS_PACKET_Y1] = c;
249 			sc->state = ZSMS_STATE_X2;
250 			break;
251 
252 		case ZSMS_STATE_X2:
253 			sc->packet[ZSMS_PACKET_X2] = c;
254 			sc->state = ZSMS_STATE_Y2;
255 			break;
256 
257 		case ZSMS_STATE_Y2:
258 			sc->packet[ZSMS_PACKET_Y2] = c;
259 
260 			/* tweak wsmouse */
261 			zsms_wsmouse_input(sc);
262 
263 			sc->state = ZSMS_STATE_SYNC;
264 		}
265 
266 		sc->rxq_head = (sc->rxq_head + 1) & ~ZSMS_RXQ_LEN;
267 	}
268 }
269 
270 /******************************************************************************
271  * wsmouse glue
272  ******************************************************************************/
273 
274 static void
zsms_wsmouse_input(struct zsms_softc * sc)275 zsms_wsmouse_input(struct zsms_softc *sc)
276 {
277 	u_int	btns;
278 	int bl, bm, br;
279 	int	x, y;
280 
281 	btns = (uint8_t)sc->packet[ZSMS_PACKET_SYNC] & ZSMS_SYNC_BTN_MASK;
282 
283 	bl = (btns & ZSMS_SYNC_BTN_L) == 0;
284 	bm = (btns & ZSMS_SYNC_BTN_M) == 0;
285 	br = (btns & ZSMS_SYNC_BTN_R) == 0;
286 
287 	/* for wsmouse(4), 1 is down, 0 is up, the most left button is LSB */
288 	btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0);
289 
290 	x = (int)sc->packet[ZSMS_PACKET_X1] + (int)sc->packet[ZSMS_PACKET_X2];
291 	y = (int)sc->packet[ZSMS_PACKET_Y1] + (int)sc->packet[ZSMS_PACKET_Y2];
292 
293 	wsmouse_input(sc->wsmousedev, btns, x, y, 0, 0, WSMOUSE_INPUT_DELTA);
294 }
295 
296 static int
zsms_wsmouse_enable(void * cookie)297 zsms_wsmouse_enable(void *cookie)
298 {
299 	struct zsms_softc *sc = cookie;
300 
301 	if (sc->enabled)
302 		return (EBUSY);
303 
304 	sc->state = ZSMS_STATE_SYNC;
305 	sc->enabled = 1;
306 
307 	return (0);
308 }
309 
310 void
zsms_wsmouse_disable(void * cookie)311 zsms_wsmouse_disable(void *cookie)
312 {
313 	struct zsms_softc *sc = cookie;
314 
315 	sc->enabled = 0;
316 }
317 
318 static int
zsms_wsmouse_ioctl(void * cookie,u_long cmd,void * data,int flag,struct lwp * l)319 zsms_wsmouse_ioctl(void *cookie, u_long cmd,
320 		   void *data, int flag, struct lwp *l)
321 {
322 
323 	switch (cmd) {
324 	case WSMOUSEIO_GTYPE:
325 		*(u_int *)data = WSMOUSE_TYPE_SGI;
326 		break;
327 
328 #ifdef notyet
329 	case WSMOUSEIO_SRES:
330 	case WSMOUSEIO_SSCALE:
331 	case WSMOUSEIO_SRATE:
332 	case WSMOUSEIO_SCALIBCOORDS:
333 	case WSMOUSEIO_GCALIBCOORDS:
334 	case WSMOUSEIO_GETID:
335 #endif
336 
337 	default:
338 		return (EPASSTHROUGH);
339 	}
340 
341 	return (0);
342 }
343