xref: /netbsd-src/sys/dev/sun/ms.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: ms.c,v 1.18 2000/03/30 12:45:42 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * Mouse driver (/dev/mouse)
49  */
50 
51 /*
52  * Zilog Z8530 Dual UART driver (mouse interface)
53  *
54  * This is the "slave" driver that will be attached to
55  * the "zsc" driver for a Sun mouse.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/conf.h>
61 #include <sys/device.h>
62 #include <sys/ioctl.h>
63 #include <sys/kernel.h>
64 #include <sys/proc.h>
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/time.h>
68 #include <sys/syslog.h>
69 #include <sys/select.h>
70 #include <sys/poll.h>
71 
72 #include <machine/vuid_event.h>
73 
74 #include <dev/ic/z8530reg.h>
75 #include <machine/z8530var.h>
76 #include <dev/sun/event_var.h>
77 #include <dev/sun/msvar.h>
78 
79 #include "locators.h"
80 
81 cdev_decl(ms);	/* open, close, read, write, ioctl, stop, ... */
82 
83 extern struct cfdriver ms_cd;
84 
85 /****************************************************************
86  *  Entry points for /dev/mouse
87  *  (open,close,read,write,...)
88  ****************************************************************/
89 
90 int
91 msopen(dev, flags, mode, p)
92 	dev_t dev;
93 	int flags, mode;
94 	struct proc *p;
95 {
96 	struct ms_softc *ms;
97 	int unit;
98 
99 	unit = minor(dev);
100 	if (unit >= ms_cd.cd_ndevs)
101 		return (ENXIO);
102 	ms = ms_cd.cd_devs[unit];
103 	if (ms == NULL)
104 		return (ENXIO);
105 
106 	/* This is an exclusive open device. */
107 	if (ms->ms_events.ev_io)
108 		return (EBUSY);
109 	ms->ms_events.ev_io = p;
110 	ev_init(&ms->ms_events);	/* may cause sleep */
111 
112 	ms->ms_ready = 1;		/* start accepting events */
113 	return (0);
114 }
115 
116 int
117 msclose(dev, flags, mode, p)
118 	dev_t dev;
119 	int flags, mode;
120 	struct proc *p;
121 {
122 	struct ms_softc *ms;
123 
124 	ms = ms_cd.cd_devs[minor(dev)];
125 	ms->ms_ready = 0;		/* stop accepting events */
126 	ev_fini(&ms->ms_events);
127 
128 	ms->ms_events.ev_io = NULL;
129 	return (0);
130 }
131 
132 int
133 msread(dev, uio, flags)
134 	dev_t dev;
135 	struct uio *uio;
136 	int flags;
137 {
138 	struct ms_softc *ms;
139 
140 	ms = ms_cd.cd_devs[minor(dev)];
141 	return (ev_read(&ms->ms_events, uio, flags));
142 }
143 
144 /* this routine should not exist, but is convenient to write here for now */
145 int
146 mswrite(dev, uio, flags)
147 	dev_t dev;
148 	struct uio *uio;
149 	int flags;
150 {
151 
152 	return (EOPNOTSUPP);
153 }
154 
155 int
156 msioctl(dev, cmd, data, flag, p)
157 	dev_t dev;
158 	u_long cmd;
159 	caddr_t data;
160 	int flag;
161 	struct proc *p;
162 {
163 	struct ms_softc *ms;
164 
165 	ms = ms_cd.cd_devs[minor(dev)];
166 
167 	switch (cmd) {
168 
169 	case FIONBIO:		/* we will remove this someday (soon???) */
170 		return (0);
171 
172 	case FIOASYNC:
173 		ms->ms_events.ev_async = *(int *)data != 0;
174 		return (0);
175 
176 	case TIOCSPGRP:
177 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
178 			return (EPERM);
179 		return (0);
180 
181 	case VUIDGFORMAT:
182 		/* we only do firm_events */
183 		*(int *)data = VUID_FIRM_EVENT;
184 		return (0);
185 
186 	case VUIDSFORMAT:
187 		if (*(int *)data != VUID_FIRM_EVENT)
188 			return (EINVAL);
189 		return (0);
190 	}
191 	return (ENOTTY);
192 }
193 
194 int
195 mspoll(dev, events, p)
196 	dev_t dev;
197 	int events;
198 	struct proc *p;
199 {
200 	struct ms_softc *ms;
201 
202 	ms = ms_cd.cd_devs[minor(dev)];
203 	return (ev_poll(&ms->ms_events, events, p));
204 }
205 
206 
207 /****************************************************************
208  * Middle layer (translator)
209  ****************************************************************/
210 
211 /*
212  * Called by our ms_softint() routine on input.
213  */
214 void
215 ms_input(ms, c)
216 	struct ms_softc *ms;
217 	int c;
218 {
219 	struct firm_event *fe;
220 	int mb, ub, d, get, put, any;
221 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
222 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
223 
224 	/*
225 	 * Discard input if not ready.  Drop sync on parity or framing
226 	 * error; gain sync on button byte.
227 	 */
228 	if (ms->ms_ready == 0)
229 		return;
230 	if (c == -1) {
231 		ms->ms_byteno = -1;
232 		return;
233 	}
234 	if ((c & ~0x0f) == 0x80) {	/* if in 0x80..0x8f */
235 		if (c & 8) {
236 			ms->ms_byteno = 1;	/* short form (3 bytes) */
237 		} else {
238 			ms->ms_byteno = 0;	/* long form (5 bytes) */
239 		}
240 	}
241 
242 	/*
243 	 * Run the decode loop, adding to the current information.
244 	 * We add, rather than replace, deltas, so that if the event queue
245 	 * fills, we accumulate data for when it opens up again.
246 	 */
247 	switch (ms->ms_byteno) {
248 
249 	case -1:
250 		return;
251 
252 	case 0:
253 		/* buttons (long form) */
254 		ms->ms_byteno = 2;
255 		ms->ms_mb = (~c) & 0x7;
256 		return;
257 
258 	case 1:
259 		/* buttons (short form) */
260 		ms->ms_byteno = 4;
261 		ms->ms_mb = (~c) & 0x7;
262 		return;
263 
264 	case 2:
265 		/* first delta-x */
266 		ms->ms_byteno = 3;
267 		ms->ms_dx += (char)c;
268 		return;
269 
270 	case 3:
271 		/* first delta-y */
272 		ms->ms_byteno = 4;
273 		ms->ms_dy += (char)c;
274 		return;
275 
276 	case 4:
277 		/* second delta-x */
278 		ms->ms_byteno = 5;
279 		ms->ms_dx += (char)c;
280 		return;
281 
282 	case 5:
283 		/* second delta-y */
284 		ms->ms_byteno = -1;	/* wait for button-byte again */
285 		ms->ms_dy += (char)c;
286 		break;
287 
288 	default:
289 		panic("ms_rint");
290 		/* NOTREACHED */
291 	}
292 
293 	/*
294 	 * We have at least one event (mouse button, delta-X, or
295 	 * delta-Y; possibly all three, and possibly three separate
296 	 * button events).  Deliver these events until we are out
297 	 * of changes or out of room.  As events get delivered,
298 	 * mark them `unchanged'.
299 	 */
300 	any = 0;
301 	get = ms->ms_events.ev_get;
302 	put = ms->ms_events.ev_put;
303 	fe = &ms->ms_events.ev_q[put];
304 
305 	/* NEXT prepares to put the next event, backing off if necessary */
306 #define	NEXT \
307 	if ((++put) % EV_QSIZE == get) { \
308 		put--; \
309 		goto out; \
310 	}
311 	/* ADVANCE completes the `put' of the event */
312 #define	ADVANCE \
313 	fe++; \
314 	if (put >= EV_QSIZE) { \
315 		put = 0; \
316 		fe = &ms->ms_events.ev_q[0]; \
317 	} \
318 	any = 1
319 
320 	mb = ms->ms_mb;
321 	ub = ms->ms_ub;
322 	while ((d = mb ^ ub) != 0) {
323 		/*
324 		 * Mouse button change.  Convert up to three changes
325 		 * to the `first' change, and drop it into the event queue.
326 		 */
327 		NEXT;
328 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
329 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
330 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
331 		fe->time = time;
332 		ADVANCE;
333 		ub ^= d;
334 	}
335 	if (ms->ms_dx) {
336 		NEXT;
337 		fe->id = LOC_X_DELTA;
338 		fe->value = ms->ms_dx;
339 		fe->time = time;
340 		ADVANCE;
341 		ms->ms_dx = 0;
342 	}
343 	if (ms->ms_dy) {
344 		NEXT;
345 		fe->id = LOC_Y_DELTA;
346 		fe->value = ms->ms_dy;
347 		fe->time = time;
348 		ADVANCE;
349 		ms->ms_dy = 0;
350 	}
351 out:
352 	if (any) {
353 		ms->ms_ub = ub;
354 		ms->ms_events.ev_put = put;
355 		EV_WAKEUP(&ms->ms_events);
356 	}
357 }
358