wsmouse.c revision 1.24 1 /* $NetBSD: wsmouse.c,v 1.24 2002/09/06 13:18:43 gehenna 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.24 2002/09/06 13:18:43 gehenna 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 struct cfattach wsmouse_ca = {
156 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
157 wsmouse_detach, wsmouse_activate
158 };
159
160 #if NWSMOUSE > 0
161 extern struct cfdriver wsmouse_cd;
162 #endif /* NWSMOUSE > 0 */
163
164 dev_type_open(wsmouseopen);
165 dev_type_close(wsmouseclose);
166 dev_type_read(wsmouseread);
167 dev_type_ioctl(wsmouseioctl);
168 dev_type_poll(wsmousepoll);
169
170 const struct cdevsw wsmouse_cdevsw = {
171 wsmouseopen, wsmouseclose, wsmouseread, nowrite, wsmouseioctl,
172 nostop, notty, wsmousepoll, nommap,
173 };
174
175 #if NWSMUX > 0
176 struct wssrcops wsmouse_srcops = {
177 WSMUX_MOUSE,
178 wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
179 };
180 #endif
181
182 /*
183 * Print function (for parent devices).
184 */
185 int
186 wsmousedevprint(void *aux, const char *pnp)
187 {
188
189 if (pnp)
190 printf("wsmouse at %s", pnp);
191 return (UNCONF);
192 }
193
194 int
195 wsmouse_match(struct device *parent, struct cfdata *match, void *aux)
196 {
197 return (1);
198 }
199
200 void
201 wsmouse_attach(struct device *parent, struct device *self, void *aux)
202 {
203 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
204 struct wsmousedev_attach_args *ap = aux;
205 #if NWSMUX > 0
206 int mux, error;
207 #endif
208
209 sc->sc_accessops = ap->accessops;
210 sc->sc_accesscookie = ap->accesscookie;
211
212 #if NWSMUX > 0
213 sc->sc_base.me_ops = &wsmouse_srcops;
214 mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
215 if (mux >= 0) {
216 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
217 if (error)
218 printf(" attach error=%d", error);
219 else
220 printf(" mux %d", mux);
221 }
222 #else
223 if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
224 printf(" (mux ignored)");
225 #endif
226
227 printf("\n");
228 }
229
230 int
231 wsmouse_activate(struct device *self, enum devact act)
232 {
233 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
234
235 if (act == DVACT_DEACTIVATE)
236 sc->sc_dying = 1;
237 return (0);
238 }
239
240 /*
241 * Detach a mouse. To keep track of users of the softc we keep
242 * a reference count that's incremented while inside, e.g., read.
243 * If the mouse is active and the reference count is > 0 (0 is the
244 * normal state) we post an event and then wait for the process
245 * that had the reference to wake us up again. Then we blow away the
246 * vnode and return (which will deallocate the softc).
247 */
248 int
249 wsmouse_detach(struct device *self, int flags)
250 {
251 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
252 struct wseventvar *evar;
253 int maj, mn;
254 int s;
255
256 #if NWSMUX > 0
257 /* Tell parent mux we're leaving. */
258 if (sc->sc_base.me_parent != NULL) {
259 DPRINTF(("wsmouse_detach:\n"));
260 wsmux_detach_sc(&sc->sc_base);
261 }
262 #endif
263
264 /* If we're open ... */
265 evar = sc->sc_base.me_evp;
266 if (evar != NULL && evar->io != NULL) {
267 s = spltty();
268 if (--sc->sc_refcnt >= 0) {
269 /* Wake everyone by generating a dummy event. */
270 if (++evar->put >= WSEVENT_QSIZE)
271 evar->put = 0;
272 WSEVENT_WAKEUP(evar);
273 /* Wait for processes to go away. */
274 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
275 printf("wsmouse_detach: %s didn't detach\n",
276 sc->sc_base.me_dv.dv_xname);
277 }
278 splx(s);
279 }
280
281 /* locate the major number */
282 maj = cdevsw_lookup_major(&wsmouse_cdevsw);
283
284 /* Nuke the vnodes for any open instances (calls close). */
285 mn = self->dv_unit;
286 vdevgone(maj, mn, mn, VCHR);
287
288 return (0);
289 }
290
291 void
292 wsmouse_input(struct device *wsmousedev, u_int btns /* 0 is up */,
293 int x, int y, int z, u_int flags)
294 {
295 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
296 struct wscons_event *ev;
297 struct wseventvar *evar;
298 int mb, ub, d, get, put, any;
299
300 /*
301 * Discard input if not open.
302 */
303 evar = sc->sc_base.me_evp;
304 if (evar == NULL)
305 return;
306
307 #ifdef DIAGNOSTIC
308 if (evar->q == NULL) {
309 printf("wsmouse_input: evar->q=NULL\n");
310 return;
311 }
312 #endif
313
314 #if NWSMUX > 0
315 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
316 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
317 #endif
318
319 sc->sc_mb = btns;
320 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
321 sc->sc_dx += x;
322 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
323 sc->sc_dy += y;
324 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
325 sc->sc_dz += z;
326
327 /*
328 * We have at least one event (mouse button, delta-X, or
329 * delta-Y; possibly all three, and possibly three separate
330 * button events). Deliver these events until we are out
331 * of changes or out of room. As events get delivered,
332 * mark them `unchanged'.
333 */
334 ub = sc->sc_ub;
335 any = 0;
336 get = evar->get;
337 put = evar->put;
338 ev = &evar->q[put];
339
340 /* NEXT prepares to put the next event, backing off if necessary */
341 #define NEXT \
342 if ((++put) % WSEVENT_QSIZE == get) { \
343 put--; \
344 goto out; \
345 }
346 /* ADVANCE completes the `put' of the event */
347 #define ADVANCE \
348 ev++; \
349 if (put >= WSEVENT_QSIZE) { \
350 put = 0; \
351 ev = &evar->q[0]; \
352 } \
353 any = 1
354 /* TIMESTAMP sets `time' field of the event to the current time */
355 #define TIMESTAMP \
356 do { \
357 int s; \
358 s = splhigh(); \
359 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
360 splx(s); \
361 } while (0)
362
363 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
364 if (sc->sc_x != x) {
365 NEXT;
366 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
367 ev->value = x;
368 TIMESTAMP;
369 ADVANCE;
370 sc->sc_x = x;
371 }
372 } else {
373 if (sc->sc_dx) {
374 NEXT;
375 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
376 ev->value = sc->sc_dx;
377 TIMESTAMP;
378 ADVANCE;
379 sc->sc_dx = 0;
380 }
381 }
382 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
383 if (sc->sc_y != y) {
384 NEXT;
385 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
386 ev->value = y;
387 TIMESTAMP;
388 ADVANCE;
389 sc->sc_y = y;
390 }
391 } else {
392 if (sc->sc_dy) {
393 NEXT;
394 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
395 ev->value = sc->sc_dy;
396 TIMESTAMP;
397 ADVANCE;
398 sc->sc_dy = 0;
399 }
400 }
401 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
402 if (sc->sc_z != z) {
403 NEXT;
404 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
405 ev->value = z;
406 TIMESTAMP;
407 ADVANCE;
408 sc->sc_z = z;
409 }
410 } else {
411 if (sc->sc_dz) {
412 NEXT;
413 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
414 ev->value = sc->sc_dz;
415 TIMESTAMP;
416 ADVANCE;
417 sc->sc_dz = 0;
418 }
419 }
420
421 mb = sc->sc_mb;
422 while ((d = mb ^ ub) != 0) {
423 /*
424 * Mouse button change. Find the first change and drop
425 * it into the event queue.
426 */
427 NEXT;
428 ev->value = ffs(d) - 1;
429
430 KASSERT(ev->value >= 0);
431
432 d = 1 << ev->value;
433 ev->type =
434 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
435 TIMESTAMP;
436 ADVANCE;
437 ub ^= d;
438 }
439 out:
440 if (any) {
441 sc->sc_ub = ub;
442 evar->put = put;
443 WSEVENT_WAKEUP(evar);
444 #if NWSMUX > 0
445 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
446 sc->sc_base.me_dv.dv_xname, evar));
447 #endif
448 }
449 }
450
451 int
452 wsmouseopen(dev_t dev, int flags, int mode, struct proc *p)
453 {
454 struct wsmouse_softc *sc;
455 struct wseventvar *evar;
456 int error, unit;
457
458 unit = minor(dev);
459 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
460 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
461 return (ENXIO);
462
463 #if NWSMUX > 0
464 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
465 sc->sc_base.me_parent, p));
466 #endif
467
468 if (sc->sc_dying)
469 return (EIO);
470
471 if ((flags & (FREAD | FWRITE)) == FWRITE)
472 return (0); /* always allow open for write
473 so ioctl() is possible. */
474
475 if (sc->sc_base.me_evp != NULL)
476 return (EBUSY);
477
478 evar = &sc->sc_base.me_evar;
479 wsevent_init(evar);
480 sc->sc_base.me_evp = evar;
481 evar->io = p;
482
483 error = wsmousedoopen(sc, evar);
484 if (error) {
485 DPRINTF(("wsmouseopen: %s open failed\n",
486 sc->sc_base.me_dv.dv_xname));
487 sc->sc_base.me_evp = NULL;
488 wsevent_fini(evar);
489 }
490 return (error);
491 }
492
493 int
494 wsmouseclose(dev_t dev, int flags, int mode, struct proc *p)
495 {
496 struct wsmouse_softc *sc =
497 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
498 struct wseventvar *evar = sc->sc_base.me_evp;
499
500 if (evar == NULL)
501 /* not open for read */
502 return (0);
503 sc->sc_base.me_evp = NULL;
504 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
505 wsevent_fini(evar);
506
507 return (0);
508 }
509
510 int
511 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
512 {
513 sc->sc_base.me_evp = evp;
514 sc->sc_x = INVALID_X;
515 sc->sc_y = INVALID_Y;
516 sc->sc_z = INVALID_Z;
517
518 /* enable the device, and punt if that's not possible */
519 return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
520 }
521
522 int
523 wsmouseread(dev_t dev, struct uio *uio, int flags)
524 {
525 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
526 int error;
527
528 if (sc->sc_dying)
529 return (EIO);
530
531 #ifdef DIAGNOSTIC
532 if (sc->sc_base.me_evp == NULL) {
533 printf("wsmouseread: evp == NULL\n");
534 return (EINVAL);
535 }
536 #endif
537
538 sc->sc_refcnt++;
539 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
540 if (--sc->sc_refcnt < 0) {
541 wakeup(sc);
542 error = EIO;
543 }
544 return (error);
545 }
546
547 int
548 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
549 {
550 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
551 cmd, data, flag, p));
552 }
553
554 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
555 int
556 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
557 struct proc *p)
558 {
559 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
560 int error;
561
562 sc->sc_refcnt++;
563 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
564 if (--sc->sc_refcnt < 0)
565 wakeup(sc);
566 return (error);
567 }
568
569 int
570 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data,
571 int flag, struct proc *p)
572 {
573 int error;
574
575 if (sc->sc_dying)
576 return (EIO);
577
578 /*
579 * Try the generic ioctls that the wsmouse interface supports.
580 */
581 switch (cmd) {
582 case FIONBIO: /* we will remove this someday (soon???) */
583 return (0);
584
585 case FIOASYNC:
586 if (sc->sc_base.me_evp == NULL)
587 return (EINVAL);
588 sc->sc_base.me_evp->async = *(int *)data != 0;
589 return (0);
590
591 case TIOCSPGRP:
592 if (sc->sc_base.me_evp == NULL)
593 return (EINVAL);
594 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
595 return (EPERM);
596 return (0);
597 }
598
599 /*
600 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
601 * if it didn't recognize the request.
602 */
603 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
604 data, flag, p);
605 return (error != -1 ? error : ENOTTY);
606 }
607
608 int
609 wsmousepoll(dev_t dev, int events, struct proc *p)
610 {
611 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
612
613 if (sc->sc_base.me_evp == NULL)
614 return (EINVAL);
615 return (wsevent_poll(sc->sc_base.me_evp, events, p));
616 }
617
618 #if NWSMUX > 0
619 int
620 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
621 {
622 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
623
624 if (sc->sc_base.me_evp != NULL)
625 return (EBUSY);
626
627 return wsmousedoopen(sc, evp);
628 }
629
630 int
631 wsmouse_mux_close(struct wsevsrc *me)
632 {
633 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
634
635 sc->sc_base.me_evp = NULL;
636 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
637
638 return (0);
639 }
640
641 int
642 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
643 {
644 struct wsmouse_softc *sc;
645
646 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
647 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
648 return (ENXIO);
649
650 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
651 return (EBUSY);
652
653 return (wsmux_attach_sc(muxsc, &sc->sc_base));
654 }
655 #endif /* NWSMUX > 0 */
656