wsmouse.c revision 1.4 1 1.4 drochner /* $NetBSD: wsmouse.c,v 1.4 1998/07/27 22:33:22 drochner 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.4 drochner "$NetBSD: wsmouse.c,v 1.4 1998/07/27 22:33:22 drochner 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.1 drochner #include <sys/kernel.h>
90 1.1 drochner #include <sys/proc.h>
91 1.1 drochner #include <sys/syslog.h>
92 1.1 drochner #include <sys/systm.h>
93 1.1 drochner #include <sys/tty.h>
94 1.1 drochner #include <sys/signalvar.h>
95 1.1 drochner #include <sys/device.h>
96 1.1 drochner
97 1.1 drochner #include <dev/wscons/wsconsio.h>
98 1.1 drochner #include <dev/wscons/wsmousevar.h>
99 1.1 drochner #include <dev/wscons/wseventvar.h>
100 1.1 drochner
101 1.3 augustss #include "wsmouse.h"
102 1.3 augustss
103 1.1 drochner struct wsmouse_softc {
104 1.1 drochner struct device sc_dv;
105 1.1 drochner
106 1.1 drochner const struct wsmouse_accessops *sc_accessops;
107 1.1 drochner void *sc_accesscookie;
108 1.1 drochner
109 1.1 drochner int sc_ready; /* accepting events */
110 1.1 drochner struct wseventvar sc_events; /* event queue state */
111 1.1 drochner
112 1.1 drochner u_int sc_mb; /* mouse button state */
113 1.1 drochner u_int sc_ub; /* user button state */
114 1.1 drochner int sc_dx; /* delta-x */
115 1.1 drochner int sc_dy; /* delta-y */
116 1.4 drochner int sc_dz; /* delta-z */
117 1.1 drochner };
118 1.1 drochner
119 1.1 drochner int wsmouse_match __P((struct device *, struct cfdata *, void *));
120 1.1 drochner void wsmouse_attach __P((struct device *, struct device *, void *));
121 1.1 drochner
122 1.1 drochner struct cfattach wsmouse_ca = {
123 1.1 drochner sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
124 1.1 drochner };
125 1.1 drochner
126 1.3 augustss #if NWSMOUSE > 0
127 1.1 drochner extern struct cfdriver wsmouse_cd;
128 1.3 augustss #endif /* NWSMOUSE > 0 */
129 1.1 drochner
130 1.1 drochner cdev_decl(wsmouse);
131 1.1 drochner
132 1.1 drochner /*
133 1.1 drochner * Print function (for parent devices).
134 1.1 drochner */
135 1.1 drochner int
136 1.1 drochner wsmousedevprint(aux, pnp)
137 1.1 drochner void *aux;
138 1.1 drochner const char *pnp;
139 1.1 drochner {
140 1.1 drochner
141 1.1 drochner if (pnp)
142 1.1 drochner printf("wsmouse at %s", pnp);
143 1.1 drochner return (UNCONF);
144 1.1 drochner }
145 1.1 drochner
146 1.1 drochner int
147 1.1 drochner wsmouse_match(parent, match, aux)
148 1.1 drochner struct device *parent;
149 1.1 drochner struct cfdata *match;
150 1.1 drochner void *aux;
151 1.1 drochner {
152 1.1 drochner
153 1.1 drochner return (1);
154 1.1 drochner }
155 1.1 drochner
156 1.1 drochner void
157 1.1 drochner wsmouse_attach(parent, self, aux)
158 1.1 drochner struct device *parent, *self;
159 1.1 drochner void *aux;
160 1.1 drochner {
161 1.1 drochner struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
162 1.1 drochner struct wsmousedev_attach_args *ap = aux;
163 1.1 drochner
164 1.1 drochner printf("\n");
165 1.1 drochner
166 1.1 drochner sc->sc_accessops = ap->accessops;
167 1.1 drochner sc->sc_accesscookie = ap->accesscookie;
168 1.1 drochner sc->sc_ready = 0; /* sanity */
169 1.1 drochner }
170 1.1 drochner
171 1.1 drochner void
172 1.4 drochner wsmouse_input(wsmousedev, btns, dx, dy, dz)
173 1.1 drochner struct device *wsmousedev;
174 1.1 drochner u_int btns; /* 0 is up */
175 1.4 drochner int dx, dy, dz;
176 1.1 drochner {
177 1.1 drochner struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
178 1.1 drochner struct wscons_event *ev;
179 1.1 drochner int mb, ub, d, get, put, any;
180 1.1 drochner
181 1.1 drochner /*
182 1.1 drochner * Discard input if not ready.
183 1.1 drochner */
184 1.1 drochner if (sc->sc_ready == 0)
185 1.1 drochner return;
186 1.1 drochner
187 1.1 drochner sc->sc_mb = btns;
188 1.1 drochner sc->sc_dx += dx;
189 1.1 drochner sc->sc_dy += dy;
190 1.4 drochner sc->sc_dz += dz;
191 1.1 drochner
192 1.1 drochner /*
193 1.1 drochner * We have at least one event (mouse button, delta-X, or
194 1.1 drochner * delta-Y; possibly all three, and possibly three separate
195 1.1 drochner * button events). Deliver these events until we are out
196 1.1 drochner * of changes or out of room. As events get delivered,
197 1.1 drochner * mark them `unchanged'.
198 1.1 drochner */
199 1.1 drochner any = 0;
200 1.1 drochner get = sc->sc_events.get;
201 1.1 drochner put = sc->sc_events.put;
202 1.1 drochner ev = &sc->sc_events.q[put];
203 1.1 drochner
204 1.1 drochner /* NEXT prepares to put the next event, backing off if necessary */
205 1.1 drochner #define NEXT \
206 1.1 drochner if ((++put) % WSEVENT_QSIZE == get) { \
207 1.1 drochner put--; \
208 1.1 drochner goto out; \
209 1.1 drochner }
210 1.1 drochner /* ADVANCE completes the `put' of the event */
211 1.1 drochner #define ADVANCE \
212 1.1 drochner ev++; \
213 1.1 drochner if (put >= WSEVENT_QSIZE) { \
214 1.1 drochner put = 0; \
215 1.1 drochner ev = &sc->sc_events.q[0]; \
216 1.1 drochner } \
217 1.1 drochner any = 1
218 1.1 drochner /* TIMESTAMP sets `time' field of the event to the current time */
219 1.1 drochner #define TIMESTAMP \
220 1.1 drochner do { \
221 1.1 drochner int s; \
222 1.1 drochner s = splhigh(); \
223 1.1 drochner TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
224 1.1 drochner splx(s); \
225 1.1 drochner } while (0)
226 1.1 drochner
227 1.1 drochner mb = sc->sc_mb;
228 1.1 drochner ub = sc->sc_ub;
229 1.1 drochner while ((d = mb ^ ub) != 0) {
230 1.1 drochner /*
231 1.1 drochner * Mouse button change. Find the first change and drop
232 1.1 drochner * it into the event queue.
233 1.1 drochner */
234 1.1 drochner NEXT;
235 1.1 drochner ev->value = ffs(d) - 1;
236 1.1 drochner
237 1.1 drochner KASSERT(ev->value >= 0);
238 1.1 drochner
239 1.1 drochner d = 1 << ev->value;
240 1.1 drochner ev->type =
241 1.1 drochner (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
242 1.1 drochner TIMESTAMP;
243 1.1 drochner ADVANCE;
244 1.1 drochner ub ^= d;
245 1.1 drochner }
246 1.1 drochner if (sc->sc_dx) {
247 1.1 drochner NEXT;
248 1.1 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
249 1.1 drochner ev->value = sc->sc_dx;
250 1.1 drochner TIMESTAMP;
251 1.1 drochner ADVANCE;
252 1.1 drochner sc->sc_dx = 0;
253 1.1 drochner }
254 1.1 drochner if (sc->sc_dy) {
255 1.1 drochner NEXT;
256 1.1 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
257 1.1 drochner ev->value = sc->sc_dy;
258 1.1 drochner TIMESTAMP;
259 1.1 drochner ADVANCE;
260 1.1 drochner sc->sc_dy = 0;
261 1.4 drochner }
262 1.4 drochner if (sc->sc_dz) {
263 1.4 drochner NEXT;
264 1.4 drochner ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
265 1.4 drochner ev->value = sc->sc_dz;
266 1.4 drochner TIMESTAMP;
267 1.4 drochner ADVANCE;
268 1.4 drochner sc->sc_dz = 0;
269 1.1 drochner }
270 1.1 drochner out:
271 1.1 drochner if (any) {
272 1.1 drochner sc->sc_ub = ub;
273 1.1 drochner sc->sc_events.put = put;
274 1.1 drochner WSEVENT_WAKEUP(&sc->sc_events);
275 1.1 drochner }
276 1.1 drochner }
277 1.1 drochner
278 1.1 drochner int
279 1.1 drochner wsmouseopen(dev, flags, mode, p)
280 1.1 drochner dev_t dev;
281 1.1 drochner int flags, mode;
282 1.1 drochner struct proc *p;
283 1.1 drochner {
284 1.3 augustss #if NWSMOUSE > 0
285 1.1 drochner struct wsmouse_softc *sc;
286 1.1 drochner int error, unit;
287 1.1 drochner
288 1.1 drochner unit = minor(dev);
289 1.1 drochner if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
290 1.1 drochner (sc = wsmouse_cd.cd_devs[unit]) == NULL)
291 1.1 drochner return (ENXIO);
292 1.1 drochner
293 1.1 drochner if (sc->sc_events.io) /* and that it's not in use */
294 1.1 drochner return (EBUSY);
295 1.1 drochner
296 1.1 drochner sc->sc_events.io = p;
297 1.1 drochner wsevent_init(&sc->sc_events); /* may cause sleep */
298 1.1 drochner
299 1.1 drochner sc->sc_ready = 1; /* start accepting events */
300 1.1 drochner
301 1.1 drochner /* enable the device, and punt if that's not possible */
302 1.1 drochner error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
303 1.1 drochner if (error) {
304 1.1 drochner sc->sc_ready = 0; /* stop accepting events */
305 1.1 drochner wsevent_fini(&sc->sc_events);
306 1.1 drochner sc->sc_events.io = NULL;
307 1.1 drochner return (error);
308 1.1 drochner }
309 1.1 drochner
310 1.1 drochner return (0);
311 1.3 augustss #else
312 1.3 augustss return (ENXIO);
313 1.3 augustss #endif /* NWSMOUSE > 0 */
314 1.1 drochner }
315 1.1 drochner
316 1.1 drochner int
317 1.1 drochner wsmouseclose(dev, flags, mode, p)
318 1.1 drochner dev_t dev;
319 1.1 drochner int flags, mode;
320 1.1 drochner struct proc *p;
321 1.1 drochner {
322 1.3 augustss #if NWSMOUSE > 0
323 1.1 drochner struct wsmouse_softc *sc;
324 1.1 drochner int unit;
325 1.1 drochner
326 1.1 drochner unit = minor(dev);
327 1.1 drochner if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
328 1.1 drochner (sc = wsmouse_cd.cd_devs[unit]) == NULL)
329 1.1 drochner return (ENXIO);
330 1.1 drochner
331 1.1 drochner (*sc->sc_accessops->disable)(sc->sc_accesscookie);
332 1.1 drochner
333 1.1 drochner sc->sc_ready = 0; /* stop accepting events */
334 1.1 drochner wsevent_fini(&sc->sc_events);
335 1.1 drochner sc->sc_events.io = NULL;
336 1.1 drochner return (0);
337 1.3 augustss #else
338 1.3 augustss return (ENXIO);
339 1.3 augustss #endif /* NWSMOUSE > 0 */
340 1.1 drochner }
341 1.1 drochner
342 1.1 drochner int
343 1.1 drochner wsmouseread(dev, uio, flags)
344 1.1 drochner dev_t dev;
345 1.1 drochner struct uio *uio;
346 1.1 drochner int flags;
347 1.1 drochner {
348 1.3 augustss #if NWSMOUSE > 0
349 1.1 drochner struct wsmouse_softc *sc;
350 1.1 drochner int unit;
351 1.1 drochner
352 1.1 drochner unit = minor(dev);
353 1.1 drochner if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
354 1.1 drochner (sc = wsmouse_cd.cd_devs[unit]) == NULL)
355 1.1 drochner return (ENXIO);
356 1.1 drochner
357 1.1 drochner return (wsevent_read(&sc->sc_events, uio, flags));
358 1.3 augustss #else
359 1.3 augustss return (ENXIO);
360 1.3 augustss #endif /* NWSMOUSE > 0 */
361 1.1 drochner }
362 1.1 drochner
363 1.1 drochner int
364 1.1 drochner wsmouseioctl(dev, cmd, data, flag, p)
365 1.1 drochner dev_t dev;
366 1.1 drochner u_long cmd;
367 1.1 drochner caddr_t data;
368 1.1 drochner int flag;
369 1.1 drochner struct proc *p;
370 1.1 drochner {
371 1.3 augustss #if NWSMOUSE > 0
372 1.1 drochner struct wsmouse_softc *sc;
373 1.1 drochner int unit, error;
374 1.1 drochner
375 1.1 drochner unit = minor(dev);
376 1.1 drochner if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
377 1.1 drochner (sc = wsmouse_cd.cd_devs[unit]) == NULL)
378 1.1 drochner return (ENXIO);
379 1.1 drochner
380 1.1 drochner /*
381 1.1 drochner * Try the generic ioctls that the wsmouse interface supports.
382 1.1 drochner */
383 1.1 drochner switch (cmd) {
384 1.1 drochner case FIONBIO: /* we will remove this someday (soon???) */
385 1.1 drochner return (0);
386 1.1 drochner
387 1.1 drochner case FIOASYNC:
388 1.1 drochner sc->sc_events.async = *(int *)data != 0;
389 1.1 drochner return (0);
390 1.1 drochner
391 1.1 drochner case TIOCSPGRP:
392 1.1 drochner if (*(int *)data != sc->sc_events.io->p_pgid)
393 1.1 drochner return (EPERM);
394 1.1 drochner return (0);
395 1.1 drochner }
396 1.1 drochner
397 1.1 drochner /*
398 1.1 drochner * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
399 1.1 drochner * if it didn't recognize the request.
400 1.1 drochner */
401 1.1 drochner error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
402 1.1 drochner data, flag, p);
403 1.1 drochner return (error != -1 ? error : ENOTTY);
404 1.3 augustss #else
405 1.3 augustss return (ENXIO);
406 1.3 augustss #endif /* NWSMOUSE > 0 */
407 1.1 drochner }
408 1.1 drochner
409 1.1 drochner int
410 1.1 drochner wsmousepoll(dev, events, p)
411 1.1 drochner dev_t dev;
412 1.1 drochner int events;
413 1.1 drochner struct proc *p;
414 1.1 drochner {
415 1.3 augustss #if NWSMOUSE > 0
416 1.1 drochner struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
417 1.1 drochner
418 1.1 drochner return (wsevent_poll(&sc->sc_events, events, p));
419 1.3 augustss #else
420 1.3 augustss return (0);
421 1.3 augustss #endif /* NWSMOUSE > 0 */
422 1.1 drochner }
423