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