xref: /netbsd-src/sys/arch/ews4800mips/dev/ewsms.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ewsms.c,v 1.4 2007/03/04 05:59:47 christos 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.4 2007/03/04 05:59:47 christos 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 	struct device 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 	struct device *sc_wsmousedev;
104 };
105 
106 static int  ewsms_zsc_match(struct device *, struct cfdata *, void *);
107 static void ewsms_zsc_attach(struct device *, struct device *, 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(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
136 ewsms_zsc_match(struct device *parent, struct cfdata *cf, void *aux)
137 {
138 	struct zsc_softc *zsc = (void *)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
151 ewsms_zsc_attach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct zsc_softc *zsc = (void *)parent;
154 	struct ewsms_softc *sc = (void *)self;
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_enabled = 0;
167 	sc->sc_rxq_head = 0;
168 	sc->sc_rxq_tail = 0;
169 	sc->sc_state = EWSMS_STATE_SYNC;
170 
171 	if (zsc->zsc_flags & 0x0002) {
172 		sc->sc_flags |= EWSMS_F_RSTCMD;
173 		sc->sc_flags |= EWSMS_F_FASTBAUD;
174 		sc->sc_flags |= EWSMS_F_SWAPBTN;
175 		sc->sc_flags |= EWSMS_F_SYNC1;
176 	}
177 
178 	ewsms_zsc_reset(cs);
179 
180 	printf(": baud rate %d\n", EWSMS_BAUD);
181 
182 	/* attach wsmouse */
183 	wsmaa.accessops = &ewsms_wsmouse_accessops;
184 	wsmaa.accesscookie = sc;
185 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
186 }
187 
188 int
189 ewsms_zsc_reset(struct zs_chanstate *cs)
190 {
191 	struct ewsms_softc *sc = cs->cs_private;
192 	u_int baud;
193 	int i, s;
194 	const static uint8_t cmd[] =
195 	    { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
196 
197 	s = splzs();
198 	zs_write_reg(cs, 9, ZSWR9_A_RESET);
199 	cs->cs_preg[1] = ZSWR1_RIE;
200 	baud = EWSMS_BAUD;
201 	if (sc->sc_flags & EWSMS_F_FASTBAUD)
202 		baud = EWSMS_BAUD1;
203 	zs_set_speed(cs, baud);
204 	zs_loadchannelregs(cs);
205 
206 	if (sc->sc_flags & EWSMS_F_RSTCMD) {
207 		for (i = 0; i < sizeof(cmd); i++)
208 			zs_putc(cs, cmd[i]);
209 	}
210 
211 	splx(s);
212 
213 	return 0;
214 }
215 
216 void
217 ewsms_zsc_rxint(struct zs_chanstate *cs)
218 {
219 	struct ewsms_softc *sc = cs->cs_private;
220 	u_char c, r;
221 
222 	/* clear errors */
223 	r = zs_read_reg(cs, 1);
224 	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
225 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
226 
227 	/* read byte and append to our queue */
228 	c = zs_read_data(cs);
229 
230 	sc->sc_rxq[sc->sc_rxq_tail] = c;
231 	sc->sc_rxq_tail = EWSMS_NEXTRXQ(sc->sc_rxq_tail);
232 
233 	cs->cs_softreq = 1;
234 }
235 
236 /* We should never get here. */
237 void
238 ewsms_zsc_txint(struct zs_chanstate *cs)
239 {
240 	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
241 
242 	/* seems like the in thing to do */
243 	cs->cs_softreq = 1;
244 }
245 
246 void
247 ewsms_zsc_stint(struct zs_chanstate *cs, int force)
248 {
249 
250 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
251 	cs->cs_softreq = 1;
252 }
253 
254 void
255 ewsms_zsc_softint(struct zs_chanstate *cs)
256 {
257 	struct ewsms_softc *sc = cs->cs_private;
258 	uint8_t sync;
259 
260 	/* No need to keep score if nobody is listening */
261 	if (!sc->sc_enabled) {
262 		sc->sc_rxq_head = sc->sc_rxq_tail;
263 		return;
264 	}
265 
266 	/*
267 	 * Here's the real action. Read a full packet and
268 	 * then let wsmouse know what has happened.
269 	 */
270 	while (sc->sc_rxq_head != sc->sc_rxq_tail) {
271 		int8_t c = sc->sc_rxq[sc->sc_rxq_head];
272 
273 		switch (sc->sc_state) {
274 		case EWSMS_STATE_SYNC:
275 			sync = EWSMS_SYNC0;
276 			if (sc->sc_flags & EWSMS_F_SYNC1)
277 				sync = EWSMS_SYNC1;
278 			if ((c & EWSMS_SYNC_MASK) == sync) {
279 				sc->sc_packet[EWSMS_PACKET_SYNC] = c;
280 				sc->sc_state = EWSMS_STATE_X1;
281 			}
282 			break;
283 
284 		case EWSMS_STATE_X1:
285 			sc->sc_packet[EWSMS_PACKET_X1] = c;
286 			sc->sc_state = EWSMS_STATE_Y1;
287 			break;
288 
289 		case EWSMS_STATE_Y1:
290 			sc->sc_packet[EWSMS_PACKET_Y1] = c;
291 			sc->sc_state = EWSMS_STATE_X2;
292 			break;
293 
294 		case EWSMS_STATE_X2:
295 			sc->sc_packet[EWSMS_PACKET_X2] = c;
296 			sc->sc_state = EWSMS_STATE_Y2;
297 			break;
298 
299 		case EWSMS_STATE_Y2:
300 			sc->sc_packet[EWSMS_PACKET_Y2] = c;
301 
302 			/* tweak wsmouse */
303 			ewsms_wsmouse_input(sc);
304 
305 			sc->sc_state = EWSMS_STATE_SYNC;
306 		}
307 
308 		sc->sc_rxq_head = EWSMS_NEXTRXQ(sc->sc_rxq_head);
309 	}
310 }
311 
312 /******************************************************************************
313  * wsmouse glue
314  ******************************************************************************/
315 
316 static void
317 ewsms_wsmouse_input(struct ewsms_softc *sc)
318 {
319 	u_int btns;
320 	bool bl, bm, br;
321 	int dx, dy;
322 
323 	btns = (uint8_t)sc->sc_packet[EWSMS_PACKET_SYNC] & EWSMS_SYNC_BTN_MASK;
324 	if (sc->sc_flags & EWSMS_F_SWAPBTN) {
325 		bl = (btns & EWSMS_SYNC_BTN_R) == 0;
326 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
327 		br = (btns & EWSMS_SYNC_BTN_L) == 0;
328 	} else {
329 		bl = (btns & EWSMS_SYNC_BTN_L) == 0;
330 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
331 		br = (btns & EWSMS_SYNC_BTN_R) == 0;
332 	}
333 	/* for wsmouse(4), 1 is down, 0 is up, most left button is LSB */
334 	btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0);
335 
336 	/* delta values are signed */
337 	/* moving left is minus, moving right is plus */
338 	dx = (int)sc->sc_packet[EWSMS_PACKET_X1] +
339 	     (int)sc->sc_packet[EWSMS_PACKET_X2];
340 	/* moving back is minus, moving front is plus */
341 	dy = (int)sc->sc_packet[EWSMS_PACKET_Y1] +
342 	     (int)sc->sc_packet[EWSMS_PACKET_Y2];
343 
344 	wsmouse_input(sc->sc_wsmousedev,
345 			btns,
346 			dx, dy, 0, 0,
347 			WSMOUSE_INPUT_DELTA);
348 }
349 
350 static int
351 ewsms_wsmouse_enable(void *cookie)
352 {
353 	struct ewsms_softc *sc = cookie;
354 
355 	if (sc->sc_enabled)
356 		return EBUSY;
357 
358 	sc->sc_state = EWSMS_STATE_SYNC;
359 	sc->sc_enabled = 1;
360 
361 	return 0;
362 }
363 
364 void
365 ewsms_wsmouse_disable(void *cookie)
366 {
367 	struct ewsms_softc *sc = cookie;
368 
369 	sc->sc_enabled = 0;
370 }
371 
372 static int
373 ewsms_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
374     struct lwp *l)
375 {
376 
377 	switch (cmd) {
378 	case WSMOUSEIO_GTYPE:
379 		*(u_int *)data = 0;
380 		break;
381 
382 #ifdef notyet
383 	case WSMOUSEIO_SRES:
384 	case WSMOUSEIO_SSCALE:
385 	case WSMOUSEIO_SRATE:
386 	case WSMOUSEIO_SCALIBCOORDS:
387 	case WSMOUSEIO_GCALIBCOORDS:
388 	case WSMOUSEIO_GETID:
389 #endif
390 
391 	default:
392 		return EPASSTHROUGH;
393 	}
394 
395 	return 0;
396 }
397