wsmouse.c revision 1.13.6.1 1 /* $NetBSD: wsmouse.c,v 1.13.6.1 2001/09/07 04:45:35 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.13.6.1 2001/09/07 04:45:35 thorpej Exp $");
35
36 /*
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This software was developed by the Computer Systems Engineering group
41 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42 * contributed to Berkeley.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Lawrence Berkeley Laboratory.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 *
77 * @(#)ms.c 8.1 (Berkeley) 6/11/93
78 */
79
80 /*
81 * Mouse driver.
82 */
83
84 #include <sys/param.h>
85 #include <sys/conf.h>
86 #include <sys/ioctl.h>
87 #include <sys/fcntl.h>
88 #include <sys/kernel.h>
89 #include <sys/proc.h>
90 #include <sys/syslog.h>
91 #include <sys/systm.h>
92 #include <sys/tty.h>
93 #include <sys/signalvar.h>
94 #include <sys/device.h>
95 #include <sys/vnode.h>
96
97 #include <miscfs/specfs/specdev.h>
98
99 #include <dev/wscons/wsconsio.h>
100 #include <dev/wscons/wsmousevar.h>
101 #include <dev/wscons/wseventvar.h>
102
103 #include "wsmouse.h"
104 #include "wsmux.h"
105 #include "wsdisplay.h"
106 #include "wskbd.h"
107
108 #if NWSMUX > 0
109 #include <dev/wscons/wsmuxvar.h>
110 #endif
111
112 #define INVALID_X INT_MAX
113 #define INVALID_Y INT_MAX
114 #define INVALID_Z INT_MAX
115
116 struct wsmouse_softc {
117 struct device sc_dv;
118
119 const struct wsmouse_accessops *sc_accessops;
120 void *sc_accesscookie;
121
122 int sc_ready; /* accepting events */
123 struct wseventvar sc_events; /* event queue state */
124
125 u_int sc_mb; /* mouse button state */
126 u_int sc_ub; /* user button state */
127 int sc_dx; /* delta-x */
128 int sc_dy; /* delta-y */
129 int sc_dz; /* delta-z */
130 int sc_x; /* absolute-x */
131 int sc_y; /* absolute-y */
132 int sc_z; /* absolute-z */
133
134 int sc_refcnt;
135 u_char sc_dying; /* device is being detached */
136
137 #if NWSMUX > 0
138 struct wsmux_softc *sc_mux;
139 #endif
140 };
141
142 int wsmouse_major = -1;
143
144 int wsmouse_match __P((struct device *, struct cfdata *, void *));
145 void wsmouse_attach __P((struct device *, struct device *, void *));
146 int wsmouse_detach __P((struct device *, int));
147 int wsmouse_activate __P((struct device *, enum devact));
148
149 int wsmouse_do_ioctl __P((struct wsmouse_softc *, u_long, caddr_t,
150 int, struct proc *));
151
152 int wsmousedoclose __P((struct device *, int, int, struct proc *));
153 int wsmousedoioctl __P((struct device *, u_long, caddr_t, int,
154 struct proc *));
155
156 struct cfattach wsmouse_ca = {
157 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
158 wsmouse_detach, wsmouse_activate
159 };
160
161 #if NWSMOUSE > 0
162 extern struct cfdriver wsmouse_cd;
163 #endif /* NWSMOUSE > 0 */
164
165 cdev_decl(wsmouse);
166
167 #if NWSMUX > 0
168 struct wsmuxops wsmouse_muxops = {
169 wsmouseopen, wsmousedoclose, wsmousedoioctl, 0, 0
170 };
171 #endif
172
173 /*
174 * Print function (for parent devices).
175 */
176 int
177 wsmousedevprint(aux, pnp)
178 void *aux;
179 const char *pnp;
180 {
181
182 if (pnp)
183 printf("wsmouse at %s", pnp);
184 return (UNCONF);
185 }
186
187 int
188 wsmouse_match(parent, match, aux)
189 struct device *parent;
190 struct cfdata *match;
191 void *aux;
192 {
193 return (1);
194 }
195
196 void
197 wsmouse_attach(parent, self, aux)
198 struct device *parent, *self;
199 void *aux;
200 {
201 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
202 struct wsmousedev_attach_args *ap = aux;
203 #if NWSMUX > 0
204 int mux;
205 #endif
206
207 sc->sc_accessops = ap->accessops;
208 sc->sc_accesscookie = ap->accesscookie;
209 sc->sc_ready = 0; /* sanity */
210
211 if (wsmouse_major == -1) {
212 int maj;
213
214 for (maj = 0; maj < nchrdev; maj++)
215 if (cdevsw[maj].d_open == wsmouseopen)
216 break;
217 wsmouse_major = maj;
218 }
219
220 #if NWSMUX > 0
221 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
222 if (mux != WSMOUSEDEVCF_MUX_DEFAULT) {
223 wsmux_attach(mux, WSMUX_MOUSE, &sc->sc_dv, &sc->sc_events,
224 &sc->sc_mux, &wsmouse_muxops, wsmouse_major);
225 printf(" mux %d", mux);
226 }
227 #endif
228
229 printf("\n");
230 }
231
232 int
233 wsmouse_activate(self, act)
234 struct device *self;
235 enum devact act;
236 {
237 /* XXX should we do something more? */
238 return (0);
239 }
240
241 /*
242 * Detach a mouse. To keep track of users of the softc we keep
243 * a reference count that's incremented while inside, e.g., read.
244 * If the mouse is active and the reference count is > 0 (0 is the
245 * normal state) we post an event and then wait for the process
246 * that had the reference to wake us up again. Then we blow away the
247 * vnode and return (which will deallocate the softc).
248 */
249 int
250 wsmouse_detach(self, flags)
251 struct device *self;
252 int flags;
253 {
254 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
255 struct wseventvar *evar;
256 int s, mn;
257 #if NWSMUX > 0
258 int mux;
259 #endif
260
261 sc->sc_dying = 1;
262
263 #if NWSMUX > 0
264 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
265 if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
266 wsmux_detach(mux, &sc->sc_dv);
267 #endif
268
269 evar = &sc->sc_events;
270 if (evar->io) {
271 s = spltty();
272 if (--sc->sc_refcnt >= 0) {
273 /* Wake everyone by generating a dummy event. */
274 if (++evar->put >= WSEVENT_QSIZE)
275 evar->put = 0;
276 WSEVENT_WAKEUP(evar);
277 /* Wait for processes to go away. */
278 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
279 printf("wsmouse_detach: %s didn't detach\n",
280 sc->sc_dv.dv_xname);
281 }
282 splx(s);
283 }
284
285 /* Nuke the vnodes for any open instances (calls close). */
286 mn = self->dv_unit;
287 vdevgone(wsmouse_major, mn, mn, VCHR);
288
289 return (0);
290 }
291
292 void
293 wsmouse_input(wsmousedev, btns, x, y, z, flags)
294 struct device *wsmousedev;
295 u_int btns; /* 0 is up */
296 int x, y, z;
297 u_int flags;
298 {
299 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
300 struct wscons_event *ev;
301 struct wseventvar *evar;
302 int mb, ub, d, get, put, any;
303
304 /*
305 * Discard input if not ready.
306 */
307 if (sc->sc_ready == 0)
308 return;
309
310 #if NWSMUX > 0
311 if (sc->sc_mux)
312 evar = &sc->sc_mux->sc_events;
313 else
314 #endif
315 evar = &sc->sc_events;
316
317 sc->sc_mb = btns;
318 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
319 sc->sc_dx += x;
320 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
321 sc->sc_dy += y;
322 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
323 sc->sc_dz += z;
324
325 /*
326 * We have at least one event (mouse button, delta-X, or
327 * delta-Y; possibly all three, and possibly three separate
328 * button events). Deliver these events until we are out
329 * of changes or out of room. As events get delivered,
330 * mark them `unchanged'.
331 */
332 ub = sc->sc_ub;
333 any = 0;
334 get = evar->get;
335 put = evar->put;
336 ev = &evar->q[put];
337
338 /* NEXT prepares to put the next event, backing off if necessary */
339 #define NEXT \
340 if ((++put) % WSEVENT_QSIZE == get) { \
341 put--; \
342 goto out; \
343 }
344 /* ADVANCE completes the `put' of the event */
345 #define ADVANCE \
346 ev++; \
347 if (put >= WSEVENT_QSIZE) { \
348 put = 0; \
349 ev = &evar->q[0]; \
350 } \
351 any = 1
352 /* TIMESTAMP sets `time' field of the event to the current time */
353 #define TIMESTAMP \
354 do { \
355 int s; \
356 s = splhigh(); \
357 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
358 splx(s); \
359 } while (0)
360
361 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
362 if (sc->sc_x != x) {
363 NEXT;
364 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
365 ev->value = x;
366 TIMESTAMP;
367 ADVANCE;
368 sc->sc_x = x;
369 }
370 } else {
371 if (sc->sc_dx) {
372 NEXT;
373 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
374 ev->value = sc->sc_dx;
375 TIMESTAMP;
376 ADVANCE;
377 sc->sc_dx = 0;
378 }
379 }
380 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
381 if (sc->sc_y != y) {
382 NEXT;
383 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
384 ev->value = y;
385 TIMESTAMP;
386 ADVANCE;
387 sc->sc_y = y;
388 }
389 } else {
390 if (sc->sc_dy) {
391 NEXT;
392 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
393 ev->value = sc->sc_dy;
394 TIMESTAMP;
395 ADVANCE;
396 sc->sc_dy = 0;
397 }
398 }
399 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
400 if (sc->sc_z != z) {
401 NEXT;
402 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
403 ev->value = z;
404 TIMESTAMP;
405 ADVANCE;
406 sc->sc_z = z;
407 }
408 } else {
409 if (sc->sc_dz) {
410 NEXT;
411 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
412 ev->value = sc->sc_dz;
413 TIMESTAMP;
414 ADVANCE;
415 sc->sc_dz = 0;
416 }
417 }
418
419 mb = sc->sc_mb;
420 while ((d = mb ^ ub) != 0) {
421 /*
422 * Mouse button change. Find the first change and drop
423 * it into the event queue.
424 */
425 NEXT;
426 ev->value = ffs(d) - 1;
427
428 KASSERT(ev->value >= 0);
429
430 d = 1 << ev->value;
431 ev->type =
432 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
433 TIMESTAMP;
434 ADVANCE;
435 ub ^= d;
436 }
437 out:
438 if (any) {
439 sc->sc_ub = ub;
440 evar->put = put;
441 WSEVENT_WAKEUP(evar);
442 }
443 }
444
445 int
446 wsmouseopen(devvp, flags, mode, p)
447 struct vnode *devvp;
448 int flags, mode;
449 struct proc *p;
450 {
451 #if NWSMOUSE > 0
452 struct wsmouse_softc *sc;
453 int error, unit;
454
455 unit = minor(devvp->v_rdev);
456 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
457 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
458 return (ENXIO);
459
460 if (sc->sc_dying)
461 return (EIO);
462
463 if ((flags & (FREAD | FWRITE)) == FWRITE)
464 return (0); /* always allow open for write
465 so ioctl() is possible. */
466
467 #if NWSMUX > 0
468 if (sc->sc_mux)
469 return (EBUSY);
470 #endif
471
472 if (sc->sc_events.io) /* and that it's not in use */
473 return (EBUSY);
474
475 devvp->v_devcookie = sc;
476
477 sc->sc_events.io = p;
478 wsevent_init(&sc->sc_events); /* may cause sleep */
479
480 sc->sc_ready = 1; /* start accepting events */
481 sc->sc_x = INVALID_X;
482 sc->sc_y = INVALID_Y;
483 sc->sc_z = INVALID_Z;
484
485 /* enable the device, and punt if that's not possible */
486 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
487 if (error) {
488 sc->sc_ready = 0; /* stop accepting events */
489 wsevent_fini(&sc->sc_events);
490 sc->sc_events.io = NULL;
491 return (error);
492 }
493
494 return (0);
495 #else
496 return (ENXIO);
497 #endif /* NWSMOUSE > 0 */
498 }
499
500 int
501 wsmouseclose(devvp, flags, mode, p)
502 struct vnode *devvp;
503 int flags, mode;
504 struct proc *p;
505 {
506 #if NWSMOUSE > 0
507 return (wsmousedoclose(devvp->v_devcookie, flags, mode, p));
508 #else
509 return (ENXIO);
510 #endif /* NWSMOUSE > 0 */
511 }
512
513 #if NWSMOUSE > 0
514 int
515 wsmousedoclose(dv, flags, mode, p)
516 struct device *dv;
517 int flags, mode;
518 struct proc *p;
519 {
520 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
521
522 if ((flags & (FREAD | FWRITE)) == FWRITE)
523 return (0); /* see wsmouseopen() */
524
525 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
526
527 sc->sc_ready = 0; /* stop accepting events */
528 wsevent_fini(&sc->sc_events);
529 sc->sc_events.io = NULL;
530 return (0);
531 }
532 #endif /* NWSMOUSE > 0 */
533
534 int
535 wsmouseread(devvp, uio, flags)
536 struct vnode *devvp;
537 struct uio *uio;
538 int flags;
539 {
540 #if NWSMOUSE > 0
541 struct wsmouse_softc *sc = devvp->v_devcookie;
542 int error;
543
544 if (sc->sc_dying)
545 return (EIO);
546
547 sc->sc_refcnt++;
548 error = wsevent_read(&sc->sc_events, uio, flags);
549 if (--sc->sc_refcnt < 0) {
550 wakeup(sc);
551 error = EIO;
552 }
553 return (error);
554 #else
555 return (ENXIO);
556 #endif /* NWSMOUSE > 0 */
557 }
558
559 int
560 wsmouseioctl(devvp, cmd, data, flag, p)
561 struct vnode *devvp;
562 u_long cmd;
563 caddr_t data;
564 int flag;
565 struct proc *p;
566 {
567 #if NWSMOUSE > 0
568 return (wsmousedoioctl(devvp->v_devcookie, cmd, data, flag, p));
569 #else
570 return (ENXIO);
571 #endif /* NWSMOUSE > 0 */
572 }
573
574 #if NWSMOUSE > 0
575 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
576 int
577 wsmousedoioctl(dv, cmd, data, flag, p)
578 struct device *dv;
579 u_long cmd;
580 caddr_t data;
581 int flag;
582 struct proc *p;
583 {
584 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
585 int error;
586
587 sc->sc_refcnt++;
588 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
589 if (--sc->sc_refcnt < 0)
590 wakeup(sc);
591 return (error);
592 }
593
594 int
595 wsmouse_do_ioctl(sc, cmd, data, flag, p)
596 struct wsmouse_softc *sc;
597 u_long cmd;
598 caddr_t data;
599 int flag;
600 struct proc *p;
601 {
602 int error;
603
604 if (sc->sc_dying)
605 return (EIO);
606
607 /*
608 * Try the generic ioctls that the wsmouse interface supports.
609 */
610 switch (cmd) {
611 case FIONBIO: /* we will remove this someday (soon???) */
612 return (0);
613
614 case FIOASYNC:
615 sc->sc_events.async = *(int *)data != 0;
616 return (0);
617
618 case TIOCSPGRP:
619 if (*(int *)data != sc->sc_events.io->p_pgid)
620 return (EPERM);
621 return (0);
622 }
623
624 /*
625 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
626 * if it didn't recognize the request.
627 */
628 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
629 data, flag, p);
630 return (error != -1 ? error : ENOTTY);
631 }
632 #endif /* NWSMOUSE > 0 */
633
634 int
635 wsmousepoll(devvp, events, p)
636 struct vnode *devvp;
637 int events;
638 struct proc *p;
639 {
640 #if NWSMOUSE > 0
641 struct wsmouse_softc *sc = devvp->v_devcookie;
642
643 return (wsevent_poll(&sc->sc_events, events, p));
644 #else
645 return (0);
646 #endif /* NWSMOUSE > 0 */
647 }
648
649 #if NWSMUX > 0
650 int
651 wsmouse_add_mux(unit, muxsc)
652 int unit;
653 struct wsmux_softc *muxsc;
654 {
655 struct wsmouse_softc *sc;
656
657 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
658 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
659 return (ENXIO);
660
661 if (sc->sc_mux || sc->sc_events.io)
662 return (EBUSY);
663
664 return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
665 &sc->sc_mux, &wsmouse_muxops, wsmouse_major));
666 }
667
668 int
669 wsmouse_rem_mux(unit, muxsc)
670 int unit;
671 struct wsmux_softc *muxsc;
672 {
673 struct wsmouse_softc *sc;
674
675 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
676 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
677 return (ENXIO);
678
679 return (wsmux_detach_sc(muxsc, &sc->sc_dv));
680 }
681
682 #endif
683