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