xref: /netbsd-src/sys/arch/ews4800mips/dev/ewsms.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: ewsms.c,v 1.10 2021/08/07 16:18:53 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  * EWS4800 serial mouse driver attached to zs channel 0 at 4800 or 1200bps.
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  * This driver is taken from sgimips, but ews4800 has the similar protocol.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ewsms.c,v 1.10 2021/08/07 16:18:53 thorpej Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/device.h>
47 
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wsmousevar.h>
50 
51 #include <dev/ic/z8530reg.h>
52 #include <machine/z8530var.h>
53 
54 #define EWSMS_BAUD		1200
55 #define EWSMS_BAUD1		4800
56 
57 #define EWSMS_RXQ_LEN		64	/* power of 2 */
58 #define EWSMS_RXQ_LEN_MASK	(EWSMS_RXQ_LEN - 1)
59 #define EWSMS_NEXTRXQ(x)	(((x) + 1) & EWSMS_RXQ_LEN_MASK)
60 
61 /* protocol */
62 #define EWSMS_SYNC0		0x80
63 #define EWSMS_SYNC1		0x88
64 #define EWSMS_SYNC_MASK		0xf8
65 #define EWSMS_SYNC_BTN_R	0x01
66 #define EWSMS_SYNC_BTN_M	0x02
67 #define EWSMS_SYNC_BTN_L	0x04
68 #define EWSMS_SYNC_BTN_MASK	\
69 	(EWSMS_SYNC_BTN_R|EWSMS_SYNC_BTN_M|EWSMS_SYNC_BTN_L)
70 
71 struct ewsms_softc {
72 	device_t sc_dev;
73 
74 	/* tail-chasing fifo */
75 	uint8_t sc_rxq[EWSMS_RXQ_LEN];
76 	u_int sc_rxq_head;
77 	u_int sc_rxq_tail;
78 
79 	/* 5-byte packet as described above */
80 	int8_t	sc_packet[5];
81 #define EWSMS_PACKET_SYNC	0
82 #define EWSMS_PACKET_X1		1
83 #define EWSMS_PACKET_Y1		2
84 #define EWSMS_PACKET_X2		3
85 #define EWSMS_PACKET_Y2		4
86 
87 	u_int sc_state;
88 #define EWSMS_STATE_SYNC	0x01
89 #define EWSMS_STATE_X1		0x02
90 #define EWSMS_STATE_Y1		0x04
91 #define EWSMS_STATE_X2		0x08
92 #define EWSMS_STATE_Y2		0x10
93 
94 	u_int sc_baud;
95 	u_int sc_flags;
96 #define EWSMS_F_FASTBAUD	0x01
97 #define EWSMS_F_RSTCMD		0x02
98 #define EWSMS_F_SWAPBTN		0x04
99 #define EWSMS_F_SYNC1		0x08
100 
101 	/* wsmouse bits */
102 	int sc_enabled;
103 	device_t sc_wsmousedev;
104 };
105 
106 static int  ewsms_zsc_match(device_t, cfdata_t, void *);
107 static void ewsms_zsc_attach(device_t, device_t, void *);
108 static int  ewsms_zsc_reset(struct zs_chanstate *);
109 static void ewsms_zsc_rxint(struct zs_chanstate *);
110 static void ewsms_zsc_txint(struct zs_chanstate *);
111 static void ewsms_zsc_stint(struct zs_chanstate *, int);
112 static void ewsms_zsc_softint(struct zs_chanstate *);
113 
114 static void ewsms_wsmouse_input(struct ewsms_softc *);
115 static int  ewsms_wsmouse_enable(void *);
116 static void ewsms_wsmouse_disable(void *);
117 static int  ewsms_wsmouse_ioctl(void *, u_long, void *, int, struct lwp *);
118 
119 CFATTACH_DECL_NEW(ewsms_zsc, sizeof(struct ewsms_softc),
120     ewsms_zsc_match, ewsms_zsc_attach, NULL, NULL);
121 
122 static struct zsops ewsms_zsops = {
123 	ewsms_zsc_rxint,
124 	ewsms_zsc_stint,
125 	ewsms_zsc_txint,
126 	ewsms_zsc_softint
127 };
128 
129 static const struct wsmouse_accessops ewsms_wsmouse_accessops = {
130 	ewsms_wsmouse_enable,
131 	ewsms_wsmouse_ioctl,
132 	ewsms_wsmouse_disable
133 };
134 
135 int
ewsms_zsc_match(device_t parent,cfdata_t cf,void * aux)136 ewsms_zsc_match(device_t parent, cfdata_t cf, void *aux)
137 {
138 	struct zsc_softc *zsc = device_private(parent);
139 	struct zsc_attach_args *zsc_args = aux;
140 
141 	/* mouse on channel A */
142 	if ((zsc->zsc_flags & 0x0001 /* kbms port */) != 0 &&
143 	    zsc_args->channel == 0)
144 		/* prior to generic zstty(4) */
145 		return 3;
146 
147 	return 0;
148 }
149 
150 void
ewsms_zsc_attach(device_t parent,device_t self,void * aux)151 ewsms_zsc_attach(device_t parent, device_t self, void *aux)
152 {
153 	struct ewsms_softc *sc = device_private(self);
154 	struct zsc_softc *zsc = device_private(parent);
155 	struct zsc_attach_args *zsc_args = aux;
156 	struct zs_chanstate *cs;
157 	struct wsmousedev_attach_args wsmaa;
158 	int channel;
159 
160 	/* Establish ourself with the MD z8530 driver */
161 	channel = zsc_args->channel;
162 	cs = zsc->zsc_cs[channel];
163 	cs->cs_ops = &ewsms_zsops;
164 	cs->cs_private = sc;
165 
166 	sc->sc_dev = self;
167 	sc->sc_enabled = 0;
168 	sc->sc_rxq_head = 0;
169 	sc->sc_rxq_tail = 0;
170 	sc->sc_state = EWSMS_STATE_SYNC;
171 
172 	if (zsc->zsc_flags & 0x0002) {
173 		sc->sc_flags |= EWSMS_F_RSTCMD;
174 		sc->sc_flags |= EWSMS_F_FASTBAUD;
175 		sc->sc_flags |= EWSMS_F_SWAPBTN;
176 		sc->sc_flags |= EWSMS_F_SYNC1;
177 	}
178 
179 	ewsms_zsc_reset(cs);
180 
181 	aprint_normal(": baud rate %d\n", EWSMS_BAUD);
182 
183 	/* attach wsmouse */
184 	wsmaa.accessops = &ewsms_wsmouse_accessops;
185 	wsmaa.accesscookie = sc;
186 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint,
187 	    CFARGS_NONE);
188 }
189 
190 int
ewsms_zsc_reset(struct zs_chanstate * cs)191 ewsms_zsc_reset(struct zs_chanstate *cs)
192 {
193 	struct ewsms_softc *sc = cs->cs_private;
194 	u_int baud;
195 	int i, s;
196 	static const uint8_t cmd[] =
197 	    { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
198 
199 	s = splzs();
200 	zs_write_reg(cs, 9, ZSWR9_A_RESET);
201 	cs->cs_preg[1] = ZSWR1_RIE;
202 	baud = EWSMS_BAUD;
203 	if (sc->sc_flags & EWSMS_F_FASTBAUD)
204 		baud = EWSMS_BAUD1;
205 	zs_set_speed(cs, baud);
206 	zs_loadchannelregs(cs);
207 
208 	if (sc->sc_flags & EWSMS_F_RSTCMD) {
209 		for (i = 0; i < sizeof(cmd); i++)
210 			zs_putc(cs, cmd[i]);
211 	}
212 
213 	splx(s);
214 
215 	return 0;
216 }
217 
218 void
ewsms_zsc_rxint(struct zs_chanstate * cs)219 ewsms_zsc_rxint(struct zs_chanstate *cs)
220 {
221 	struct ewsms_softc *sc = cs->cs_private;
222 	uint8_t c, r;
223 
224 	/* clear errors */
225 	r = zs_read_reg(cs, 1);
226 	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
227 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
228 
229 	/* read byte and append to our queue */
230 	c = zs_read_data(cs);
231 
232 	sc->sc_rxq[sc->sc_rxq_tail] = c;
233 	sc->sc_rxq_tail = EWSMS_NEXTRXQ(sc->sc_rxq_tail);
234 
235 	cs->cs_softreq = 1;
236 }
237 
238 /* We should never get here. */
239 void
ewsms_zsc_txint(struct zs_chanstate * cs)240 ewsms_zsc_txint(struct zs_chanstate *cs)
241 {
242 
243 	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
244 
245 	/* seems like the in thing to do */
246 	cs->cs_softreq = 1;
247 }
248 
249 void
ewsms_zsc_stint(struct zs_chanstate * cs,int force)250 ewsms_zsc_stint(struct zs_chanstate *cs, int force)
251 {
252 
253 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
254 	cs->cs_softreq = 1;
255 }
256 
257 void
ewsms_zsc_softint(struct zs_chanstate * cs)258 ewsms_zsc_softint(struct zs_chanstate *cs)
259 {
260 	struct ewsms_softc *sc = cs->cs_private;
261 	uint8_t sync;
262 
263 	/* No need to keep score if nobody is listening */
264 	if (!sc->sc_enabled) {
265 		sc->sc_rxq_head = sc->sc_rxq_tail;
266 		return;
267 	}
268 
269 	/*
270 	 * Here's the real action. Read a full packet and
271 	 * then let wsmouse know what has happened.
272 	 */
273 	while (sc->sc_rxq_head != sc->sc_rxq_tail) {
274 		int8_t c = sc->sc_rxq[sc->sc_rxq_head];
275 
276 		switch (sc->sc_state) {
277 		case EWSMS_STATE_SYNC:
278 			sync = EWSMS_SYNC0;
279 			if (sc->sc_flags & EWSMS_F_SYNC1)
280 				sync = EWSMS_SYNC1;
281 			if ((c & EWSMS_SYNC_MASK) == sync) {
282 				sc->sc_packet[EWSMS_PACKET_SYNC] = c;
283 				sc->sc_state = EWSMS_STATE_X1;
284 			}
285 			break;
286 
287 		case EWSMS_STATE_X1:
288 			sc->sc_packet[EWSMS_PACKET_X1] = c;
289 			sc->sc_state = EWSMS_STATE_Y1;
290 			break;
291 
292 		case EWSMS_STATE_Y1:
293 			sc->sc_packet[EWSMS_PACKET_Y1] = c;
294 			sc->sc_state = EWSMS_STATE_X2;
295 			break;
296 
297 		case EWSMS_STATE_X2:
298 			sc->sc_packet[EWSMS_PACKET_X2] = c;
299 			sc->sc_state = EWSMS_STATE_Y2;
300 			break;
301 
302 		case EWSMS_STATE_Y2:
303 			sc->sc_packet[EWSMS_PACKET_Y2] = c;
304 
305 			/* tweak wsmouse */
306 			ewsms_wsmouse_input(sc);
307 
308 			sc->sc_state = EWSMS_STATE_SYNC;
309 		}
310 
311 		sc->sc_rxq_head = EWSMS_NEXTRXQ(sc->sc_rxq_head);
312 	}
313 }
314 
315 /******************************************************************************
316  * wsmouse glue
317  ******************************************************************************/
318 
319 static void
ewsms_wsmouse_input(struct ewsms_softc * sc)320 ewsms_wsmouse_input(struct ewsms_softc *sc)
321 {
322 	u_int btns;
323 	bool bl, bm, br;
324 	int dx, dy;
325 
326 	btns = (uint8_t)sc->sc_packet[EWSMS_PACKET_SYNC] & EWSMS_SYNC_BTN_MASK;
327 	if (sc->sc_flags & EWSMS_F_SWAPBTN) {
328 		bl = (btns & EWSMS_SYNC_BTN_R) == 0;
329 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
330 		br = (btns & EWSMS_SYNC_BTN_L) == 0;
331 	} else {
332 		bl = (btns & EWSMS_SYNC_BTN_L) == 0;
333 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
334 		br = (btns & EWSMS_SYNC_BTN_R) == 0;
335 	}
336 	/* for wsmouse(4), 1 is down, 0 is up, most left button is LSB */
337 	btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0);
338 
339 	/* delta values are signed */
340 	/* moving left is minus, moving right is plus */
341 	dx = (int)sc->sc_packet[EWSMS_PACKET_X1] +
342 	     (int)sc->sc_packet[EWSMS_PACKET_X2];
343 	/* moving back is minus, moving front is plus */
344 	dy = (int)sc->sc_packet[EWSMS_PACKET_Y1] +
345 	     (int)sc->sc_packet[EWSMS_PACKET_Y2];
346 
347 	wsmouse_input(sc->sc_wsmousedev,
348 			btns,
349 			dx, dy, 0, 0,
350 			WSMOUSE_INPUT_DELTA);
351 }
352 
353 static int
ewsms_wsmouse_enable(void * cookie)354 ewsms_wsmouse_enable(void *cookie)
355 {
356 	struct ewsms_softc *sc = cookie;
357 
358 	if (sc->sc_enabled)
359 		return EBUSY;
360 
361 	sc->sc_state = EWSMS_STATE_SYNC;
362 	sc->sc_enabled = 1;
363 
364 	return 0;
365 }
366 
367 void
ewsms_wsmouse_disable(void * cookie)368 ewsms_wsmouse_disable(void *cookie)
369 {
370 	struct ewsms_softc *sc = cookie;
371 
372 	sc->sc_enabled = 0;
373 }
374 
375 static int
ewsms_wsmouse_ioctl(void * cookie,u_long cmd,void * data,int flag,struct lwp * l)376 ewsms_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
377     struct lwp *l)
378 {
379 
380 	switch (cmd) {
381 	case WSMOUSEIO_GTYPE:
382 		*(u_int *)data = 0;
383 		break;
384 
385 #ifdef notyet
386 	case WSMOUSEIO_SRES:
387 	case WSMOUSEIO_SSCALE:
388 	case WSMOUSEIO_SRATE:
389 	case WSMOUSEIO_SCALIBCOORDS:
390 	case WSMOUSEIO_GCALIBCOORDS:
391 	case WSMOUSEIO_GETID:
392 #endif
393 
394 	default:
395 		return EPASSTHROUGH;
396 	}
397 
398 	return 0;
399 }
400