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