wsmouse.c revision 1.6 1 1.6 augustss /* $NetBSD: wsmouse.c,v 1.6 1999/01/10 18:22:14 augustss Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 1.1 drochner *
6 1.1 drochner * Redistribution and use in source and binary forms, with or without
7 1.1 drochner * modification, are permitted provided that the following conditions
8 1.1 drochner * are met:
9 1.1 drochner * 1. Redistributions of source code must retain the above copyright
10 1.1 drochner * notice, this list of conditions and the following disclaimer.
11 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 drochner * notice, this list of conditions and the following disclaimer in the
13 1.1 drochner * documentation and/or other materials provided with the distribution.
14 1.1 drochner * 3. All advertising materials mentioning features or use of this software
15 1.1 drochner * must display the following acknowledgement:
16 1.1 drochner * This product includes software developed by Christopher G. Demetriou
17 1.1 drochner * for the NetBSD Project.
18 1.1 drochner * 4. The name of the author may not be used to endorse or promote products
19 1.1 drochner * derived from this software without specific prior written permission
20 1.1 drochner *
21 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 drochner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 drochner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 drochner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 drochner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 drochner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 drochner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 drochner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 drochner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 drochner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 drochner */
32 1.1 drochner
33 1.1 drochner static const char _copyright[] __attribute__ ((unused)) =
34 1.1 drochner "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 1.1 drochner static const char _rcsid[] __attribute__ ((unused)) =
36 1.6 augustss "$NetBSD: wsmouse.c,v 1.6 1999/01/10 18:22:14 augustss Exp $";
37 1.1 drochner
38 1.1 drochner /*
39 1.1 drochner * Copyright (c) 1992, 1993
40 1.1 drochner * The Regents of the University of California. All rights reserved.
41 1.1 drochner *
42 1.1 drochner * This software was developed by the Computer Systems Engineering group
43 1.1 drochner * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 1.1 drochner * contributed to Berkeley.
45 1.1 drochner *
46 1.1 drochner * All advertising materials mentioning features or use of this software
47 1.1 drochner * must display the following acknowledgement:
48 1.1 drochner * This product includes software developed by the University of
49 1.1 drochner * California, Lawrence Berkeley Laboratory.
50 1.1 drochner *
51 1.1 drochner * Redistribution and use in source and binary forms, with or without
52 1.1 drochner * modification, are permitted provided that the following conditions
53 1.1 drochner * are met:
54 1.1 drochner * 1. Redistributions of source code must retain the above copyright
55 1.1 drochner * notice, this list of conditions and the following disclaimer.
56 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
57 1.1 drochner * notice, this list of conditions and the following disclaimer in the
58 1.1 drochner * documentation and/or other materials provided with the distribution.
59 1.1 drochner * 3. All advertising materials mentioning features or use of this software
60 1.1 drochner * must display the following acknowledgement:
61 1.1 drochner * This product includes software developed by the University of
62 1.1 drochner * California, Berkeley and its contributors.
63 1.1 drochner * 4. Neither the name of the University nor the names of its contributors
64 1.1 drochner * may be used to endorse or promote products derived from this software
65 1.1 drochner * without specific prior written permission.
66 1.1 drochner *
67 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 1.1 drochner * SUCH DAMAGE.
78 1.1 drochner *
79 1.1 drochner * @(#)ms.c 8.1 (Berkeley) 6/11/93
80 1.1 drochner */
81 1.1 drochner
82 1.1 drochner /*
83 1.1 drochner * Mouse driver.
84 1.1 drochner */
85 1.1 drochner
86 1.1 drochner #include <sys/param.h>
87 1.1 drochner #include <sys/conf.h>
88 1.1 drochner #include <sys/ioctl.h>
89 1.5 augustss #include <sys/fcntl.h>
90 1.1 drochner #include <sys/kernel.h>
91 1.1 drochner #include <sys/proc.h>
92 1.1 drochner #include <sys/syslog.h>
93 1.1 drochner #include <sys/systm.h>
94 1.1 drochner #include <sys/tty.h>
95 1.1 drochner #include <sys/signalvar.h>
96 1.1 drochner #include <sys/device.h>
97 1.1 drochner
98 1.1 drochner #include <dev/wscons/wsconsio.h>
99 1.1 drochner #include <dev/wscons/wsmousevar.h>
100 1.1 drochner #include <dev/wscons/wseventvar.h>
101 1.1 drochner
102 1.3 augustss #include "wsmouse.h"
103 1.3 augustss
104 1.1 drochner struct wsmouse_softc {
105 1.1 drochner struct device sc_dv;
106 1.1 drochner
107 1.1 drochner const struct wsmouse_accessops *sc_accessops;
108 1.1 drochner void *sc_accesscookie;
109 1.1 drochner
110 1.1 drochner int sc_ready; /* accepting events */
111 1.1 drochner struct wseventvar sc_events; /* event queue state */
112 1.1 drochner
113 1.1 drochner u_int sc_mb; /* mouse button state */
114 1.1 drochner u_int sc_ub; /* user button state */
115 1.1 drochner int sc_dx; /* delta-x */
116 1.1 drochner int sc_dy; /* delta-y */
117 1.4 drochner int sc_dz; /* delta-z */
118 1.1 drochner };
119 1.1 drochner
120 1.1 drochner int wsmouse_match __P((struct device *, struct cfdata *, void *));
121 1.1 drochner void wsmouse_attach __P((struct device *, struct device *, void *));
122 1.1 drochner
123 1.1 drochner struct cfattach wsmouse_ca = {
124 1.1 drochner sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
125 1.1 drochner };
126 1.1 drochner
127 1.3 augustss #if NWSMOUSE > 0
128 1.1 drochner extern struct cfdriver wsmouse_cd;
129 1.3 augustss #endif /* NWSMOUSE > 0 */
130 1.1 drochner
131 1.1 drochner cdev_decl(wsmouse);
132 1.1 drochner
133 1.1 drochner /*
134 1.1 drochner * Print function (for parent devices).
135 1.1 drochner */
136 1.1 drochner int
137 1.1 drochner wsmousedevprint(aux, pnp)
138 1.1 drochner void *aux;
139 1.1 drochner const char *pnp;
140 1.1 drochner {
141 1.1 drochner
142 1.1 drochner if (pnp)
143 1.1 drochner printf("wsmouse at %s", pnp);
144 1.1 drochner return (UNCONF);
145 1.1 drochner }
146 1.1 drochner
147 1.1 drochner int
148 1.1 drochner wsmouse_match(parent, match, aux)
149 1.1 drochner struct device *parent;
150 1.1 drochner struct cfdata *match;
151 1.1 drochner void *aux;
152 1.1 drochner {
153 1.1 drochner
154 1.1 drochner return (1);
155 1.1 drochner }
156 1.1 drochner
157 1.1 drochner void
158 1.1 drochner wsmouse_attach(parent, self, aux)
159 1.1 drochner struct device *parent, *self;
160 1.1 drochner void *aux;
161 1.1 drochner {
162 1.1 drochner struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
163 1.1 drochner struct wsmousedev_attach_args *ap = aux;
164 1.1 drochner
165 1.1 drochner printf("\n");
166 1.1 drochner
167 1.1 drochner sc->sc_accessops = ap->accessops;
168 1.1 drochner sc->sc_accesscookie = ap->accesscookie;
169 1.1 drochner sc->sc_ready = 0; /* sanity */
170 1.1 drochner }
171 1.1 drochner
172 1.1 drochner void
173 1.4 drochner wsmouse_input(wsmousedev, btns, dx, dy, dz)
174 1.1 drochner struct device *wsmousedev;
175 1.1 drochner u_int btns; /* 0 is up */
176 1.4 drochner int dx, dy, dz;
177 1.1 drochner {
178 1.1 drochner struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
179 1.1 drochner struct wscons_event *ev;
180 1.1 drochner int mb, ub, d, get, put, any;
181 1.1 drochner
182 1.1 drochner /*
183 1.1 drochner * Discard input if not ready.
184 1.1 drochner */
185 1.1 drochner if (sc->sc_ready == 0)
186 1.1 drochner return;
187 1.1 drochner
188 1.1 drochner sc->sc_mb = btns;
189 1.1 drochner sc->sc_dx += dx;
190 1.1 drochner sc->sc_dy += dy;
191 1.4 drochner sc->sc_dz += dz;
192 1.1 drochner
193 1.1 drochner /*
194 1.1 drochner * We have at least one event (mouse button, delta-X, or
195 1.1 drochner * delta-Y; possibly all three, and possibly three separate
196 1.1 drochner * button events). Deliver these events until we are out
197 1.1 drochner * of changes or out of room. As events get delivered,
198 1.1 drochner * mark them `unchanged'.
199 1.1 drochner */
200 1.1 drochner any = 0;
201 1.1 drochner get = sc->sc_events.get;
202 1.1 drochner put = sc->sc_events.put;
203 1.1 drochner ev = &sc->sc_events.q[put];
204 1.1 drochner
205 1.1 drochner /* NEXT prepares to put the next event, backing off if necessary */
206 1.1 drochner #define NEXT \
207 1.1 drochner if ((++put) % WSEVENT_QSIZE == get) { \
208 1.1 drochner put--; \
209 1.1 drochner goto out; \
210 1.1 drochner }
211 1.1 drochner /* ADVANCE completes the `put' of the event */
212 1.1 drochner #define ADVANCE \
213 1.1 drochner ev++; \
214 1.1 drochner if (put >= WSEVENT_QSIZE) { \
215 1.1 drochner put = 0; \
216 1.1 drochner ev = &sc->sc_events.q[0]; \
217 1.1 drochner } \
218 1.1 drochner any = 1
219 1.1 drochner /* TIMESTAMP sets `time' field of the event to the current time */
220 1.1 drochner #define TIMESTAMP \
221 1.1 drochner do { \
222 1.1 drochner int s; \
223 1.1 drochner s = splhigh(); \
224 1.1 drochner TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
225 1.1 drochner splx(s); \
226 1.1 drochner } while (0)
227 1.1 drochner
228 1.1 drochner mb = sc->sc_mb;
229 1.1 drochner ub = sc->sc_ub;
230 1.1 drochner while ((d = mb ^ ub) != 0) {
231 1.1 drochner /*
232 1.1 drochner * Mouse button change. Find the first change and drop
233 1.1 drochner * it into the event queue.
234 1.1 drochner */
235 1.1 drochner NEXT;
236 1.1 drochner ev->value = ffs(d) - 1;
237 1.1 drochner
238 1.1 drochner KASSERT(ev->value >= 0);
239 1.1 drochner
240 1.1 drochner d = 1 << ev->value;
241 1.1 drochner ev->type =
242 1.1 drochner (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
243 1.1 drochner TIMESTAMP;
244 1.1 drochner ADVANCE;
245 1.1 drochner ub ^= d;
246 1.1 drochner }
247 1.1 drochner if (sc->sc_dx) {
248 1.1 drochner NEXT;
249 1.1 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
250 1.1 drochner ev->value = sc->sc_dx;
251 1.1 drochner TIMESTAMP;
252 1.1 drochner ADVANCE;
253 1.1 drochner sc->sc_dx = 0;
254 1.1 drochner }
255 1.1 drochner if (sc->sc_dy) {
256 1.1 drochner NEXT;
257 1.1 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
258 1.1 drochner ev->value = sc->sc_dy;
259 1.1 drochner TIMESTAMP;
260 1.1 drochner ADVANCE;
261 1.1 drochner sc->sc_dy = 0;
262 1.4 drochner }
263 1.4 drochner if (sc->sc_dz) {
264 1.4 drochner NEXT;
265 1.4 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
266 1.4 drochner ev->value = sc->sc_dz;
267 1.4 drochner TIMESTAMP;
268 1.4 drochner ADVANCE;
269 1.4 drochner sc->sc_dz = 0;
270 1.1 drochner }
271 1.1 drochner out:
272 1.1 drochner if (any) {
273 1.1 drochner sc->sc_ub = ub;
274 1.1 drochner sc->sc_events.put = put;
275 1.1 drochner WSEVENT_WAKEUP(&sc->sc_events);
276 1.1 drochner }
277 1.1 drochner }
278 1.1 drochner
279 1.1 drochner int
280 1.1 drochner wsmouseopen(dev, flags, mode, p)
281 1.1 drochner dev_t dev;
282 1.1 drochner int flags, mode;
283 1.1 drochner struct proc *p;
284 1.1 drochner {
285 1.3 augustss #if NWSMOUSE > 0
286 1.1 drochner struct wsmouse_softc *sc;
287 1.1 drochner int error, unit;
288 1.1 drochner
289 1.1 drochner unit = minor(dev);
290 1.1 drochner if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
291 1.1 drochner (sc = wsmouse_cd.cd_devs[unit]) == NULL)
292 1.1 drochner return (ENXIO);
293 1.1 drochner
294 1.5 augustss if ((flags & (FREAD | FWRITE)) == FWRITE)
295 1.5 augustss return (0); /* always allow open for write
296 1.5 augustss so ioctl() is possible. */
297 1.5 augustss
298 1.1 drochner if (sc->sc_events.io) /* and that it's not in use */
299 1.1 drochner return (EBUSY);
300 1.1 drochner
301 1.1 drochner sc->sc_events.io = p;
302 1.1 drochner wsevent_init(&sc->sc_events); /* may cause sleep */
303 1.1 drochner
304 1.1 drochner sc->sc_ready = 1; /* start accepting events */
305 1.1 drochner
306 1.1 drochner /* enable the device, and punt if that's not possible */
307 1.1 drochner error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
308 1.1 drochner if (error) {
309 1.1 drochner sc->sc_ready = 0; /* stop accepting events */
310 1.1 drochner wsevent_fini(&sc->sc_events);
311 1.1 drochner sc->sc_events.io = NULL;
312 1.1 drochner return (error);
313 1.1 drochner }
314 1.1 drochner
315 1.1 drochner return (0);
316 1.3 augustss #else
317 1.3 augustss return (ENXIO);
318 1.3 augustss #endif /* NWSMOUSE > 0 */
319 1.1 drochner }
320 1.1 drochner
321 1.1 drochner int
322 1.1 drochner wsmouseclose(dev, flags, mode, p)
323 1.1 drochner dev_t dev;
324 1.1 drochner int flags, mode;
325 1.1 drochner struct proc *p;
326 1.1 drochner {
327 1.3 augustss #if NWSMOUSE > 0
328 1.6 augustss struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
329 1.5 augustss
330 1.5 augustss if ((flags & (FREAD | FWRITE)) == FWRITE)
331 1.5 augustss return (0); /* see wsmouseopen() */
332 1.1 drochner
333 1.1 drochner (*sc->sc_accessops->disable)(sc->sc_accesscookie);
334 1.1 drochner
335 1.1 drochner sc->sc_ready = 0; /* stop accepting events */
336 1.1 drochner wsevent_fini(&sc->sc_events);
337 1.1 drochner sc->sc_events.io = NULL;
338 1.1 drochner return (0);
339 1.3 augustss #else
340 1.3 augustss return (ENXIO);
341 1.3 augustss #endif /* NWSMOUSE > 0 */
342 1.1 drochner }
343 1.1 drochner
344 1.1 drochner int
345 1.1 drochner wsmouseread(dev, uio, flags)
346 1.1 drochner dev_t dev;
347 1.1 drochner struct uio *uio;
348 1.1 drochner int flags;
349 1.1 drochner {
350 1.3 augustss #if NWSMOUSE > 0
351 1.6 augustss struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
352 1.1 drochner
353 1.1 drochner return (wsevent_read(&sc->sc_events, uio, flags));
354 1.3 augustss #else
355 1.3 augustss return (ENXIO);
356 1.3 augustss #endif /* NWSMOUSE > 0 */
357 1.1 drochner }
358 1.1 drochner
359 1.1 drochner int
360 1.1 drochner wsmouseioctl(dev, cmd, data, flag, p)
361 1.1 drochner dev_t dev;
362 1.1 drochner u_long cmd;
363 1.1 drochner caddr_t data;
364 1.1 drochner int flag;
365 1.1 drochner struct proc *p;
366 1.1 drochner {
367 1.3 augustss #if NWSMOUSE > 0
368 1.6 augustss struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
369 1.6 augustss int error;
370 1.1 drochner
371 1.1 drochner /*
372 1.1 drochner * Try the generic ioctls that the wsmouse interface supports.
373 1.1 drochner */
374 1.1 drochner switch (cmd) {
375 1.1 drochner case FIONBIO: /* we will remove this someday (soon???) */
376 1.1 drochner return (0);
377 1.1 drochner
378 1.1 drochner case FIOASYNC:
379 1.1 drochner sc->sc_events.async = *(int *)data != 0;
380 1.1 drochner return (0);
381 1.1 drochner
382 1.1 drochner case TIOCSPGRP:
383 1.1 drochner if (*(int *)data != sc->sc_events.io->p_pgid)
384 1.1 drochner return (EPERM);
385 1.1 drochner return (0);
386 1.1 drochner }
387 1.1 drochner
388 1.1 drochner /*
389 1.1 drochner * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
390 1.1 drochner * if it didn't recognize the request.
391 1.1 drochner */
392 1.1 drochner error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
393 1.1 drochner data, flag, p);
394 1.1 drochner return (error != -1 ? error : ENOTTY);
395 1.3 augustss #else
396 1.3 augustss return (ENXIO);
397 1.3 augustss #endif /* NWSMOUSE > 0 */
398 1.1 drochner }
399 1.1 drochner
400 1.1 drochner int
401 1.1 drochner wsmousepoll(dev, events, p)
402 1.1 drochner dev_t dev;
403 1.1 drochner int events;
404 1.1 drochner struct proc *p;
405 1.1 drochner {
406 1.3 augustss #if NWSMOUSE > 0
407 1.1 drochner struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
408 1.1 drochner
409 1.1 drochner return (wsevent_poll(&sc->sc_events, events, p));
410 1.3 augustss #else
411 1.3 augustss return (0);
412 1.3 augustss #endif /* NWSMOUSE > 0 */
413 1.1 drochner }
414