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