ms.c revision 1.20.4.3 1 /* $NetBSD: ms.c,v 1.20.4.3 2002/10/10 18:42:23 jdolecek 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.20.4.3 2002/10/10 18:42:23 jdolecek 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 "locators.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
96 };
97
98 /****************************************************************
99 * Entry points for /dev/mouse
100 * (open,close,read,write,...)
101 ****************************************************************/
102
103 int
104 msopen(dev, flags, mode, p)
105 dev_t dev;
106 int flags, mode;
107 struct proc *p;
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 = p;
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, p)
138 dev_t dev;
139 int flags, mode;
140 struct proc *p;
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, p)
172 dev_t dev;
173 u_long cmd;
174 caddr_t data;
175 int flag;
176 struct proc *p;
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 TIOCSPGRP:
192 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
193 return (EPERM);
194 return (0);
195
196 case VUIDGFORMAT:
197 /* we only do firm_events */
198 *(int *)data = VUID_FIRM_EVENT;
199 return (0);
200
201 case VUIDSFORMAT:
202 if (*(int *)data != VUID_FIRM_EVENT)
203 return (EINVAL);
204 return (0);
205 }
206 return (ENOTTY);
207 }
208
209 int
210 mspoll(dev, events, p)
211 dev_t dev;
212 int events;
213 struct proc *p;
214 {
215 struct ms_softc *ms;
216
217 ms = ms_cd.cd_devs[minor(dev)];
218 return (ev_poll(&ms->ms_events, events, p));
219 }
220
221 int
222 mskqfilter(dev, kn)
223 dev_t dev;
224 struct knote *kn;
225 {
226 struct ms_softc *ms;
227
228 ms = ms_cd.cd_devs[minor(dev)];
229 return (ev_kqfilter(&ms->ms_events, kn));
230 }
231
232 /****************************************************************
233 * Middle layer (translator)
234 ****************************************************************/
235
236 /*
237 * Called by our ms_softint() routine on input.
238 */
239 void
240 ms_input(ms, c)
241 struct ms_softc *ms;
242 int c;
243 {
244 struct firm_event *fe;
245 int mb, ub, d, get, put, any;
246 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
247 static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
248
249 /*
250 * Discard input if not ready. Drop sync on parity or framing
251 * error; gain sync on button byte.
252 */
253 if (ms->ms_ready == 0)
254 return;
255 if (c == -1) {
256 ms->ms_byteno = -1;
257 return;
258 }
259 if ((c & ~0x0f) == 0x80) { /* if in 0x80..0x8f */
260 if (c & 8) {
261 ms->ms_byteno = 1; /* short form (3 bytes) */
262 } else {
263 ms->ms_byteno = 0; /* long form (5 bytes) */
264 }
265 }
266
267 /*
268 * Run the decode loop, adding to the current information.
269 * We add, rather than replace, deltas, so that if the event queue
270 * fills, we accumulate data for when it opens up again.
271 */
272 switch (ms->ms_byteno) {
273
274 case -1:
275 return;
276
277 case 0:
278 /* buttons (long form) */
279 ms->ms_byteno = 2;
280 ms->ms_mb = (~c) & 0x7;
281 return;
282
283 case 1:
284 /* buttons (short form) */
285 ms->ms_byteno = 4;
286 ms->ms_mb = (~c) & 0x7;
287 return;
288
289 case 2:
290 /* first delta-x */
291 ms->ms_byteno = 3;
292 ms->ms_dx += (char)c;
293 return;
294
295 case 3:
296 /* first delta-y */
297 ms->ms_byteno = 4;
298 ms->ms_dy += (char)c;
299 return;
300
301 case 4:
302 /* second delta-x */
303 ms->ms_byteno = 5;
304 ms->ms_dx += (char)c;
305 return;
306
307 case 5:
308 /* second delta-y */
309 ms->ms_byteno = -1; /* wait for button-byte again */
310 ms->ms_dy += (char)c;
311 break;
312
313 default:
314 panic("ms_rint");
315 /* NOTREACHED */
316 }
317
318 /*
319 * We have at least one event (mouse button, delta-X, or
320 * delta-Y; possibly all three, and possibly three separate
321 * button events). Deliver these events until we are out
322 * of changes or out of room. As events get delivered,
323 * mark them `unchanged'.
324 */
325 any = 0;
326 get = ms->ms_events.ev_get;
327 put = ms->ms_events.ev_put;
328 fe = &ms->ms_events.ev_q[put];
329
330 /* NEXT prepares to put the next event, backing off if necessary */
331 #define NEXT \
332 if ((++put) % EV_QSIZE == get) { \
333 put--; \
334 goto out; \
335 }
336 /* ADVANCE completes the `put' of the event */
337 #define ADVANCE \
338 fe++; \
339 if (put >= EV_QSIZE) { \
340 put = 0; \
341 fe = &ms->ms_events.ev_q[0]; \
342 } \
343 any = 1
344
345 mb = ms->ms_mb;
346 ub = ms->ms_ub;
347 while ((d = mb ^ ub) != 0) {
348 /*
349 * Mouse button change. Convert up to three changes
350 * to the `first' change, and drop it into the event queue.
351 */
352 NEXT;
353 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
354 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
355 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
356 fe->time = time;
357 ADVANCE;
358 ub ^= d;
359 }
360 if (ms->ms_dx) {
361 NEXT;
362 fe->id = LOC_X_DELTA;
363 fe->value = ms->ms_dx;
364 fe->time = time;
365 ADVANCE;
366 ms->ms_dx = 0;
367 }
368 if (ms->ms_dy) {
369 NEXT;
370 fe->id = LOC_Y_DELTA;
371 fe->value = ms->ms_dy;
372 fe->time = time;
373 ADVANCE;
374 ms->ms_dy = 0;
375 }
376 out:
377 if (any) {
378 ms->ms_ub = ub;
379 ms->ms_events.ev_put = put;
380 EV_WAKEUP(&ms->ms_events);
381 }
382 }
383