xref: /netbsd-src/sys/dev/sun/ms.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: ms.c,v 1.26 2003/06/29 22:30:49 fvdl 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/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.26 2003/06/29 22:30:49 fvdl Exp $");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/conf.h>
64 #include <sys/device.h>
65 #include <sys/ioctl.h>
66 #include <sys/kernel.h>
67 #include <sys/proc.h>
68 #include <sys/signal.h>
69 #include <sys/signalvar.h>
70 #include <sys/time.h>
71 #include <sys/syslog.h>
72 #include <sys/select.h>
73 #include <sys/poll.h>
74 
75 #include <machine/vuid_event.h>
76 
77 #include <dev/ic/z8530reg.h>
78 #include <machine/z8530var.h>
79 #include <dev/sun/event_var.h>
80 #include <dev/sun/msvar.h>
81 
82 #include <dev/wscons/wsconsio.h>
83 #include <dev/wscons/wsmousevar.h>
84 
85 #include "locators.h"
86 #include "wsmouse.h"
87 
88 extern struct cfdriver ms_cd;
89 
90 dev_type_open(msopen);
91 dev_type_close(msclose);
92 dev_type_read(msread);
93 dev_type_ioctl(msioctl);
94 dev_type_poll(mspoll);
95 dev_type_kqfilter(mskqfilter);
96 
97 const struct cdevsw ms_cdevsw = {
98 	msopen, msclose, msread, nowrite, msioctl,
99 	nostop, notty, mspoll, nommap, mskqfilter,
100 };
101 
102 /****************************************************************
103  *  Entry points for /dev/mouse
104  *  (open,close,read,write,...)
105  ****************************************************************/
106 
107 int
108 msopen(dev, flags, mode, p)
109 	dev_t dev;
110 	int flags, mode;
111 	struct proc *p;
112 {
113 	struct ms_softc *ms;
114 	int unit;
115 
116 	unit = minor(dev);
117 	if (unit >= ms_cd.cd_ndevs)
118 		return (ENXIO);
119 	ms = ms_cd.cd_devs[unit];
120 	if (ms == NULL)
121 		return (ENXIO);
122 
123 	/* This is an exclusive open device. */
124 	if (ms->ms_events.ev_io)
125 		return (EBUSY);
126 
127 	if (ms->ms_deviopen) {
128 		int err;
129 		err = (*ms->ms_deviopen)((struct device *)ms, flags);
130 		if (err)
131 			return (err);
132 	}
133 	ms->ms_events.ev_io = p;
134 	ev_init(&ms->ms_events);	/* may cause sleep */
135 
136 	ms->ms_ready = 1;		/* start accepting events */
137 	return (0);
138 }
139 
140 int
141 msclose(dev, flags, mode, p)
142 	dev_t dev;
143 	int flags, mode;
144 	struct proc *p;
145 {
146 	struct ms_softc *ms;
147 
148 	ms = ms_cd.cd_devs[minor(dev)];
149 	ms->ms_ready = 0;		/* stop accepting events */
150 	ev_fini(&ms->ms_events);
151 
152 	ms->ms_events.ev_io = NULL;
153 	if (ms->ms_deviclose) {
154 		int err;
155 		err = (*ms->ms_deviclose)((struct device *)ms, flags);
156 		if (err)
157 			return (err);
158 	}
159 	return (0);
160 }
161 
162 int
163 msread(dev, uio, flags)
164 	dev_t dev;
165 	struct uio *uio;
166 	int flags;
167 {
168 	struct ms_softc *ms;
169 
170 	ms = ms_cd.cd_devs[minor(dev)];
171 	return (ev_read(&ms->ms_events, uio, flags));
172 }
173 
174 int
175 msioctl(dev, cmd, data, flag, p)
176 	dev_t dev;
177 	u_long cmd;
178 	caddr_t data;
179 	int flag;
180 	struct proc *p;
181 {
182 	struct ms_softc *ms;
183 
184 	ms = ms_cd.cd_devs[minor(dev)];
185 
186 	switch (cmd) {
187 
188 	case FIONBIO:		/* we will remove this someday (soon???) */
189 		return (0);
190 
191 	case FIOASYNC:
192 		ms->ms_events.ev_async = *(int *)data != 0;
193 		return (0);
194 
195 	case TIOCSPGRP:
196 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
197 			return (EPERM);
198 		return (0);
199 
200 	case VUIDGFORMAT:
201 		/* we only do firm_events */
202 		*(int *)data = VUID_FIRM_EVENT;
203 		return (0);
204 
205 	case VUIDSFORMAT:
206 		if (*(int *)data != VUID_FIRM_EVENT)
207 			return (EINVAL);
208 		return (0);
209 	}
210 	return (ENOTTY);
211 }
212 
213 int
214 mspoll(dev, events, p)
215 	dev_t dev;
216 	int events;
217 	struct proc *p;
218 {
219 	struct ms_softc *ms;
220 
221 	ms = ms_cd.cd_devs[minor(dev)];
222 	return (ev_poll(&ms->ms_events, events, p));
223 }
224 
225 int
226 mskqfilter(dev, kn)
227 	dev_t dev;
228 	struct knote *kn;
229 {
230 	struct ms_softc *ms;
231 
232 	ms = ms_cd.cd_devs[minor(dev)];
233 	return (ev_kqfilter(&ms->ms_events, kn));
234 }
235 
236 /****************************************************************
237  * Middle layer (translator)
238  ****************************************************************/
239 
240 /*
241  * Called by our ms_softint() routine on input.
242  */
243 void
244 ms_input(ms, c)
245 	struct ms_softc *ms;
246 	int c;
247 {
248 	struct firm_event *fe;
249 	int mb, ub, d, get, put, any;
250 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
251 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
252 
253 	/*
254 	 * Discard input if not ready.  Drop sync on parity or framing
255 	 * error; gain sync on button byte.
256 	 */
257 	if (ms->ms_ready == 0)
258 		return;
259 	if (c == -1) {
260 		ms->ms_byteno = -1;
261 		return;
262 	}
263 	if ((c & ~0x0f) == 0x80) {	/* if in 0x80..0x8f */
264 		if (c & 8) {
265 			ms->ms_byteno = 1;	/* short form (3 bytes) */
266 		} else {
267 			ms->ms_byteno = 0;	/* long form (5 bytes) */
268 		}
269 	}
270 
271 	/*
272 	 * Run the decode loop, adding to the current information.
273 	 * We add, rather than replace, deltas, so that if the event queue
274 	 * fills, we accumulate data for when it opens up again.
275 	 */
276 	switch (ms->ms_byteno) {
277 
278 	case -1:
279 		return;
280 
281 	case 0:
282 		/* buttons (long form) */
283 		ms->ms_byteno = 2;
284 		ms->ms_mb = (~c) & 0x7;
285 		return;
286 
287 	case 1:
288 		/* buttons (short form) */
289 		ms->ms_byteno = 4;
290 		ms->ms_mb = (~c) & 0x7;
291 		return;
292 
293 	case 2:
294 		/* first delta-x */
295 		ms->ms_byteno = 3;
296 		ms->ms_dx += (char)c;
297 		return;
298 
299 	case 3:
300 		/* first delta-y */
301 		ms->ms_byteno = 4;
302 		ms->ms_dy += (char)c;
303 		return;
304 
305 	case 4:
306 		/* second delta-x */
307 		ms->ms_byteno = 5;
308 		ms->ms_dx += (char)c;
309 		return;
310 
311 	case 5:
312 		/* second delta-y */
313 		ms->ms_byteno = -1;	/* wait for button-byte again */
314 		ms->ms_dy += (char)c;
315 		break;
316 
317 	default:
318 		panic("ms_rint");
319 		/* NOTREACHED */
320 	}
321 
322 #if NWSMOUSE > 0
323 	if (ms->ms_wsmousedev != NULL && ms->ms_ready == 2) {
324 		mb = ((ms->ms_mb & 4) >> 2) |
325 			(ms->ms_mb & 2) |
326 			((ms->ms_mb & 1) << 2);
327 		wsmouse_input(ms->ms_wsmousedev,
328 			      mb, ms->ms_dx, ms->ms_dy, 0,
329 			      WSMOUSE_INPUT_DELTA);
330 		ms->ms_dx = 0;
331 		ms->ms_dy = 0;
332 		return;
333 	}
334 #endif
335 	/*
336 	 * We have at least one event (mouse button, delta-X, or
337 	 * delta-Y; possibly all three, and possibly three separate
338 	 * button events).  Deliver these events until we are out
339 	 * of changes or out of room.  As events get delivered,
340 	 * mark them `unchanged'.
341 	 */
342 	any = 0;
343 	get = ms->ms_events.ev_get;
344 	put = ms->ms_events.ev_put;
345 	fe = &ms->ms_events.ev_q[put];
346 
347 	/* NEXT prepares to put the next event, backing off if necessary */
348 #define	NEXT \
349 	if ((++put) % EV_QSIZE == get) { \
350 		put--; \
351 		goto out; \
352 	}
353 	/* ADVANCE completes the `put' of the event */
354 #define	ADVANCE \
355 	fe++; \
356 	if (put >= EV_QSIZE) { \
357 		put = 0; \
358 		fe = &ms->ms_events.ev_q[0]; \
359 	} \
360 	any = 1
361 
362 	mb = ms->ms_mb;
363 	ub = ms->ms_ub;
364 	while ((d = mb ^ ub) != 0) {
365 		/*
366 		 * Mouse button change.  Convert up to three changes
367 		 * to the `first' change, and drop it into the event queue.
368 		 */
369 		NEXT;
370 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
371 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
372 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
373 		fe->time = time;
374 		ADVANCE;
375 		ub ^= d;
376 	}
377 	if (ms->ms_dx) {
378 		NEXT;
379 		fe->id = LOC_X_DELTA;
380 		fe->value = ms->ms_dx;
381 		fe->time = time;
382 		ADVANCE;
383 		ms->ms_dx = 0;
384 	}
385 	if (ms->ms_dy) {
386 		NEXT;
387 		fe->id = LOC_Y_DELTA;
388 		fe->value = ms->ms_dy;
389 		fe->time = time;
390 		ADVANCE;
391 		ms->ms_dy = 0;
392 	}
393 out:
394 	if (any) {
395 		ms->ms_ub = ub;
396 		ms->ms_events.ev_put = put;
397 		EV_WAKEUP(&ms->ms_events);
398 	}
399 }
400