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