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