ms.c revision 1.16 1 /* $NetBSD: ms.c,v 1.16 1999/05/14 06:42:02 mrg 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 register 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 register struct ms_softc *ms;
217 register int c;
218 {
219 register struct firm_event *fe;
220 register 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 & ~7) == 0x80) /* if in 0x80..0x87 */
235 ms->ms_byteno = 0;
236
237 /*
238 * Run the decode loop, adding to the current information.
239 * We add, rather than replace, deltas, so that if the event queue
240 * fills, we accumulate data for when it opens up again.
241 */
242 switch (ms->ms_byteno) {
243
244 case -1:
245 return;
246
247 case 0:
248 /* buttons */
249 ms->ms_byteno = 1;
250 ms->ms_mb = (~c) & 0x7;
251 return;
252
253 case 1:
254 /* first delta-x */
255 ms->ms_byteno = 2;
256 ms->ms_dx += (char)c;
257 return;
258
259 case 2:
260 /* first delta-y */
261 ms->ms_byteno = 3;
262 ms->ms_dy += (char)c;
263 return;
264
265 case 3:
266 /* second delta-x */
267 ms->ms_byteno = 4;
268 ms->ms_dx += (char)c;
269 return;
270
271 case 4:
272 /* second delta-x */
273 ms->ms_byteno = -1; /* wait for button-byte again */
274 ms->ms_dy += (char)c;
275 break;
276
277 default:
278 panic("ms_rint");
279 /* NOTREACHED */
280 }
281
282 /*
283 * We have at least one event (mouse button, delta-X, or
284 * delta-Y; possibly all three, and possibly three separate
285 * button events). Deliver these events until we are out
286 * of changes or out of room. As events get delivered,
287 * mark them `unchanged'.
288 */
289 any = 0;
290 get = ms->ms_events.ev_get;
291 put = ms->ms_events.ev_put;
292 fe = &ms->ms_events.ev_q[put];
293
294 /* NEXT prepares to put the next event, backing off if necessary */
295 #define NEXT \
296 if ((++put) % EV_QSIZE == get) { \
297 put--; \
298 goto out; \
299 }
300 /* ADVANCE completes the `put' of the event */
301 #define ADVANCE \
302 fe++; \
303 if (put >= EV_QSIZE) { \
304 put = 0; \
305 fe = &ms->ms_events.ev_q[0]; \
306 } \
307 any = 1
308
309 mb = ms->ms_mb;
310 ub = ms->ms_ub;
311 while ((d = mb ^ ub) != 0) {
312 /*
313 * Mouse button change. Convert up to three changes
314 * to the `first' change, and drop it into the event queue.
315 */
316 NEXT;
317 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
318 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
319 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
320 fe->time = time;
321 ADVANCE;
322 ub ^= d;
323 }
324 if (ms->ms_dx) {
325 NEXT;
326 fe->id = LOC_X_DELTA;
327 fe->value = ms->ms_dx;
328 fe->time = time;
329 ADVANCE;
330 ms->ms_dx = 0;
331 }
332 if (ms->ms_dy) {
333 NEXT;
334 fe->id = LOC_Y_DELTA;
335 fe->value = ms->ms_dy;
336 fe->time = time;
337 ADVANCE;
338 ms->ms_dy = 0;
339 }
340 out:
341 if (any) {
342 ms->ms_ub = ub;
343 ms->ms_events.ev_put = put;
344 EV_WAKEUP(&ms->ms_events);
345 }
346 }
347