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