1/*	$NetBSD: zs_ms.c,v 1.6 2007/03/04 06:00:39 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 * 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.6 2007/03/04 06:00:39 christos 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
64struct 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	struct device  *wsmousedev;
90};
91
92static int	zsms_match(device_t, cfdata_t, void *);
93static void	zsms_attach(device_t, device_t, void *);
94static void	zsms_rxint(struct zs_chanstate *);
95static void	zsms_txint(struct zs_chanstate *);
96static void	zsms_stint(struct zs_chanstate *, int);
97static void	zsms_softint(struct zs_chanstate *);
98
99static void	zsms_wsmouse_input(struct zsms_softc *);
100static int	zsms_wsmouse_enable(void *);
101static void	zsms_wsmouse_disable(void *);
102static int	zsms_wsmouse_ioctl(void *, u_long, void *, int,
103						   struct lwp *);
104
105CFATTACH_DECL_NEW(zsms, sizeof(struct zsms_softc),
106    zsms_match, zsms_attach, NULL, NULL);
107
108static struct zsops zsms_zsops = {
109	zsms_rxint,
110	zsms_stint,
111	zsms_txint,
112	zsms_softint
113};
114
115static const struct wsmouse_accessops zsms_wsmouse_accessops = {
116	zsms_wsmouse_enable,
117	zsms_wsmouse_ioctl,
118	zsms_wsmouse_disable
119};
120
121int
122zsms_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
135void
136zsms_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}
175
176void
177zsms_rxint(struct zs_chanstate *cs)
178{
179	uint8_t c, r;
180	struct zsms_softc *sc = cs->cs_private;
181
182	/* clear errors */
183	r = zs_read_reg(cs, 1);
184	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
185		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
186
187	/* read byte and append to our queue */
188	c = zs_read_data(cs);
189
190	sc->rxq[sc->rxq_tail] = c;
191	sc->rxq_tail = (sc->rxq_tail + 1) & ~ZSMS_RXQ_LEN;
192
193	cs->cs_softreq = 1;
194}
195
196/* We should never get here. */
197void
198zsms_txint(struct zs_chanstate *cs)
199{
200
201	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
202
203	/* seems like the in thing to do */
204	cs->cs_softreq = 1;
205}
206
207void
208zsms_stint(struct zs_chanstate *cs, int force)
209{
210
211	zs_write_csr(cs, ZSWR0_RESET_STATUS);
212	cs->cs_softreq = 1;
213}
214
215void
216zsms_softint(struct zs_chanstate *cs)
217{
218	struct zsms_softc *sc = cs->cs_private;
219
220	/* No need to keep score if nobody is listening */
221	if (!sc->enabled) {
222		sc->rxq_head = sc->rxq_tail;
223		return;
224	}
225
226	/*
227	 * Here's the real action. Read a full packet and
228	 * then let wsmouse know what has happened.
229	 */
230	while (sc->rxq_head != sc->rxq_tail) {
231		int8_t c = sc->rxq[sc->rxq_head];
232
233		switch (sc->state) {
234		case ZSMS_STATE_SYNC:
235			if ((c & ZSMS_SYNC_MASK) == ZSMS_SYNC) {
236				sc->packet[ZSMS_PACKET_SYNC] = c;
237				sc->state = ZSMS_STATE_X1;
238			}
239			break;
240
241		case ZSMS_STATE_X1:
242			sc->packet[ZSMS_PACKET_X1] = c;
243			sc->state = ZSMS_STATE_Y1;
244			break;
245
246		case ZSMS_STATE_Y1:
247			sc->packet[ZSMS_PACKET_Y1] = c;
248			sc->state = ZSMS_STATE_X2;
249			break;
250
251		case ZSMS_STATE_X2:
252			sc->packet[ZSMS_PACKET_X2] = c;
253			sc->state = ZSMS_STATE_Y2;
254			break;
255
256		case ZSMS_STATE_Y2:
257			sc->packet[ZSMS_PACKET_Y2] = c;
258
259			/* tweak wsmouse */
260			zsms_wsmouse_input(sc);
261
262			sc->state = ZSMS_STATE_SYNC;
263		}
264
265		sc->rxq_head = (sc->rxq_head + 1) & ~ZSMS_RXQ_LEN;
266	}
267}
268
269/******************************************************************************
270 * wsmouse glue
271 ******************************************************************************/
272
273static void
274zsms_wsmouse_input(struct zsms_softc *sc)
275{
276	u_int	btns;
277	int bl, bm, br;
278	int	x, y;
279
280	btns = (uint8_t)sc->packet[ZSMS_PACKET_SYNC] & ZSMS_SYNC_BTN_MASK;
281
282	bl = (btns & ZSMS_SYNC_BTN_L) == 0;
283	bm = (btns & ZSMS_SYNC_BTN_M) == 0;
284	br = (btns & ZSMS_SYNC_BTN_R) == 0;
285
286	/* for wsmouse(4), 1 is down, 0 is up, the most left button is LSB */
287	btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0);
288
289	x = (int)sc->packet[ZSMS_PACKET_X1] + (int)sc->packet[ZSMS_PACKET_X2];
290	y = (int)sc->packet[ZSMS_PACKET_Y1] + (int)sc->packet[ZSMS_PACKET_Y2];
291
292	wsmouse_input(sc->wsmousedev, btns, x, y, 0, 0, WSMOUSE_INPUT_DELTA);
293}
294
295static int
296zsms_wsmouse_enable(void *cookie)
297{
298	struct zsms_softc *sc = cookie;
299
300	if (sc->enabled)
301		return (EBUSY);
302
303	sc->state = ZSMS_STATE_SYNC;
304	sc->enabled = 1;
305
306	return (0);
307}
308
309void
310zsms_wsmouse_disable(void *cookie)
311{
312	struct zsms_softc *sc = cookie;
313
314	sc->enabled = 0;
315}
316
317static int
318zsms_wsmouse_ioctl(void *cookie, u_long cmd,
319		   void *data, int flag, struct lwp *l)
320{
321
322	switch (cmd) {
323	case WSMOUSEIO_GTYPE:
324		*(u_int *)data = WSMOUSE_TYPE_SGI;
325		break;
326
327#ifdef notyet
328	case WSMOUSEIO_SRES:
329	case WSMOUSEIO_SSCALE:
330	case WSMOUSEIO_SRATE:
331	case WSMOUSEIO_SCALIBCOORDS:
332	case WSMOUSEIO_GCALIBCOORDS:
333	case WSMOUSEIO_GETID:
334#endif
335
336	default:
337		return (EPASSTHROUGH);
338	}
339
340	return (0);
341}
342