Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.39
      1 /* $NetBSD: wsmouse.c,v 1.39 2006/02/05 17:38:33 jmmv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julio M. Merino Vidal.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *      This product includes software developed by Christopher G. Demetriou
     53  *	for the NetBSD Project.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1992, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * This software was developed by the Computer Systems Engineering group
     74  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     75  * contributed to Berkeley.
     76  *
     77  * All advertising materials mentioning features or use of this software
     78  * must display the following acknowledgement:
     79  *	This product includes software developed by the University of
     80  *	California, Lawrence Berkeley Laboratory.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. Neither the name of the University nor the names of its contributors
     91  *    may be used to endorse or promote products derived from this software
     92  *    without specific prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
    107  */
    108 
    109 /*
    110  * Mouse driver.
    111  */
    112 
    113 #include <sys/cdefs.h>
    114 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.39 2006/02/05 17:38:33 jmmv Exp $");
    115 
    116 #include "wsmouse.h"
    117 #include "wsdisplay.h"
    118 #include "wsmux.h"
    119 
    120 #include <sys/param.h>
    121 #include <sys/conf.h>
    122 #include <sys/ioctl.h>
    123 #include <sys/poll.h>
    124 #include <sys/fcntl.h>
    125 #include <sys/kernel.h>
    126 #include <sys/proc.h>
    127 #include <sys/syslog.h>
    128 #include <sys/systm.h>
    129 #include <sys/tty.h>
    130 #include <sys/signalvar.h>
    131 #include <sys/device.h>
    132 #include <sys/vnode.h>
    133 #include <sys/callout.h>
    134 #include <sys/malloc.h>
    135 
    136 #include <dev/wscons/wsconsio.h>
    137 #include <dev/wscons/wsmousevar.h>
    138 #include <dev/wscons/wseventvar.h>
    139 #include <dev/wscons/wsmuxvar.h>
    140 
    141 #if defined(WSMUX_DEBUG) && NWSMUX > 0
    142 #define DPRINTF(x)	if (wsmuxdebug) printf x
    143 #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
    144 extern int wsmuxdebug;
    145 #else
    146 #define DPRINTF(x)
    147 #define DPRINTFN(n,x)
    148 #endif
    149 
    150 #define INVALID_X	INT_MAX
    151 #define INVALID_Y	INT_MAX
    152 #define INVALID_Z	INT_MAX
    153 
    154 struct wsmouse_softc {
    155 	struct wsevsrc	sc_base;
    156 
    157 	const struct wsmouse_accessops *sc_accessops;
    158 	void		*sc_accesscookie;
    159 
    160 	u_int		sc_mb;		/* mouse button state */
    161 	u_int		sc_ub;		/* user button state */
    162 	int		sc_dx;		/* delta-x */
    163 	int		sc_dy;		/* delta-y */
    164 	int		sc_dz;		/* delta-z */
    165 	int		sc_dw;		/* delta-w */
    166 	int		sc_x;		/* absolute-x */
    167 	int		sc_y;		/* absolute-y */
    168 	int		sc_z;		/* absolute-z */
    169 	int		sc_w;		/* absolute-w */
    170 
    171 	int		sc_refcnt;
    172 	u_char		sc_dying;	/* device is being detached */
    173 
    174 	struct wsmouse_repeat	sc_repeat;
    175 	int			sc_repeat_button;
    176 	struct callout		sc_repeat_callout;
    177 	unsigned int		sc_repeat_delay;
    178 };
    179 
    180 static int  wsmouse_match(struct device *, struct cfdata *, void *);
    181 static void wsmouse_attach(struct device *, struct device *, void *);
    182 static int  wsmouse_detach(struct device *, int);
    183 static int  wsmouse_activate(struct device *, enum devact);
    184 
    185 static int  wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t,
    186 			     int, struct lwp *);
    187 
    188 #if NWSMUX > 0
    189 static int  wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
    190 static int  wsmouse_mux_close(struct wsevsrc *);
    191 #endif
    192 
    193 static int  wsmousedoioctl(struct device *, u_long, caddr_t, int, struct lwp *);
    194 
    195 static int  wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
    196 
    197 CFATTACH_DECL(wsmouse, sizeof (struct wsmouse_softc),
    198     wsmouse_match, wsmouse_attach, wsmouse_detach, wsmouse_activate);
    199 
    200 static void wsmouse_repeat(void *v);
    201 
    202 extern struct cfdriver wsmouse_cd;
    203 
    204 dev_type_open(wsmouseopen);
    205 dev_type_close(wsmouseclose);
    206 dev_type_read(wsmouseread);
    207 dev_type_ioctl(wsmouseioctl);
    208 dev_type_poll(wsmousepoll);
    209 dev_type_kqfilter(wsmousekqfilter);
    210 
    211 const struct cdevsw wsmouse_cdevsw = {
    212 	wsmouseopen, wsmouseclose, wsmouseread, nowrite, wsmouseioctl,
    213 	nostop, notty, wsmousepoll, nommap, wsmousekqfilter,
    214 };
    215 
    216 #if NWSMUX > 0
    217 struct wssrcops wsmouse_srcops = {
    218 	WSMUX_MOUSE,
    219 	wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
    220 };
    221 #endif
    222 
    223 /*
    224  * Print function (for parent devices).
    225  */
    226 int
    227 wsmousedevprint(void *aux, const char *pnp)
    228 {
    229 
    230 	if (pnp)
    231 		aprint_normal("wsmouse at %s", pnp);
    232 	return (UNCONF);
    233 }
    234 
    235 int
    236 wsmouse_match(struct device *parent, struct cfdata *match, void *aux)
    237 {
    238 	return (1);
    239 }
    240 
    241 void
    242 wsmouse_attach(struct device *parent, struct device *self, void *aux)
    243 {
    244         struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    245 	struct wsmousedev_attach_args *ap = aux;
    246 #if NWSMUX > 0
    247 	int mux, error;
    248 #endif
    249 
    250 	sc->sc_accessops = ap->accessops;
    251 	sc->sc_accesscookie = ap->accesscookie;
    252 
    253 	/* Initialize button repeating. */
    254 	memset(&sc->sc_repeat, 0, sizeof(sc->sc_repeat));
    255 	sc->sc_repeat_button = -1;
    256 	sc->sc_repeat_delay = 0;
    257 	callout_init(&sc->sc_repeat_callout);
    258 
    259 #if NWSMUX > 0
    260 	sc->sc_base.me_ops = &wsmouse_srcops;
    261 	mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
    262 	if (mux >= 0) {
    263 		error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
    264 		if (error)
    265 			printf(" attach error=%d", error);
    266 		else
    267 			printf(" mux %d", mux);
    268 	}
    269 #else
    270 	if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
    271 		printf(" (mux ignored)");
    272 #endif
    273 
    274 	printf("\n");
    275 }
    276 
    277 int
    278 wsmouse_activate(struct device *self, enum devact act)
    279 {
    280 	struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    281 
    282 	if (act == DVACT_DEACTIVATE)
    283 		sc->sc_dying = 1;
    284 	return (0);
    285 }
    286 
    287 /*
    288  * Detach a mouse.  To keep track of users of the softc we keep
    289  * a reference count that's incremented while inside, e.g., read.
    290  * If the mouse is active and the reference count is > 0 (0 is the
    291  * normal state) we post an event and then wait for the process
    292  * that had the reference to wake us up again.  Then we blow away the
    293  * vnode and return (which will deallocate the softc).
    294  */
    295 int
    296 wsmouse_detach(struct device  *self, int flags)
    297 {
    298 	struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    299 	struct wseventvar *evar;
    300 	int maj, mn;
    301 	int s;
    302 
    303 #if NWSMUX > 0
    304 	/* Tell parent mux we're leaving. */
    305 	if (sc->sc_base.me_parent != NULL) {
    306 		DPRINTF(("wsmouse_detach:\n"));
    307 		wsmux_detach_sc(&sc->sc_base);
    308 	}
    309 #endif
    310 
    311 	/* If we're open ... */
    312 	evar = sc->sc_base.me_evp;
    313 	if (evar != NULL && evar->io != NULL) {
    314 		s = spltty();
    315 		if (--sc->sc_refcnt >= 0) {
    316 			/* Wake everyone by generating a dummy event. */
    317 			if (++evar->put >= WSEVENT_QSIZE)
    318 				evar->put = 0;
    319 			WSEVENT_WAKEUP(evar);
    320 			/* Wait for processes to go away. */
    321 			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
    322 				printf("wsmouse_detach: %s didn't detach\n",
    323 				       sc->sc_base.me_dv.dv_xname);
    324 		}
    325 		splx(s);
    326 	}
    327 
    328 	/* locate the major number */
    329 	maj = cdevsw_lookup_major(&wsmouse_cdevsw);
    330 
    331 	/* Nuke the vnodes for any open instances (calls close). */
    332 	mn = self->dv_unit;
    333 	vdevgone(maj, mn, mn, VCHR);
    334 
    335 	return (0);
    336 }
    337 
    338 void
    339 wsmouse_input_xyzw(struct device *wsmousedev, u_int btns /* 0 is up */,
    340 	int x, int y, int z, int w, u_int flags)
    341 {
    342 	struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
    343 	struct wscons_event *ev;
    344 	struct wseventvar *evar;
    345 	int mb, ub, d, get, put, any;
    346 
    347         /*
    348          * Discard input if not open.
    349          */
    350 	evar = sc->sc_base.me_evp;
    351 	if (evar == NULL)
    352 		return;
    353 
    354 #ifdef DIAGNOSTIC
    355 	if (evar->q == NULL) {
    356 		printf("wsmouse_input: evar->q=NULL\n");
    357 		return;
    358 	}
    359 #endif
    360 
    361 #if NWSMUX > 0
    362 	DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
    363 		    sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
    364 #endif
    365 
    366 	sc->sc_mb = btns;
    367 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
    368 		sc->sc_dx += x;
    369 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
    370 		sc->sc_dy += y;
    371 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
    372 		sc->sc_dz += z;
    373 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_W))
    374 		sc->sc_dw += w;
    375 
    376 	/*
    377 	 * We have at least one event (mouse button, delta-X, or
    378 	 * delta-Y; possibly all three, and possibly three separate
    379 	 * button events).  Deliver these events until we are out
    380 	 * of changes or out of room.  As events get delivered,
    381 	 * mark them `unchanged'.
    382 	 */
    383 	ub = sc->sc_ub;
    384 	any = 0;
    385 	get = evar->get;
    386 	put = evar->put;
    387 	ev = &evar->q[put];
    388 
    389 	/* NEXT prepares to put the next event, backing off if necessary */
    390 #define	NEXT								\
    391 	if ((++put) % WSEVENT_QSIZE == get) {				\
    392 		put--;							\
    393 		goto out;						\
    394 	}
    395 	/* ADVANCE completes the `put' of the event */
    396 #define	ADVANCE								\
    397 	ev++;								\
    398 	if (put >= WSEVENT_QSIZE) {					\
    399 		put = 0;						\
    400 		ev = &evar->q[0];				\
    401 	}								\
    402 	any = 1
    403 	/* TIMESTAMP sets `time' field of the event to the current time */
    404 #define TIMESTAMP							\
    405 	do {								\
    406 		int s;							\
    407 		s = splhigh();						\
    408 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);			\
    409 		splx(s);						\
    410 	} while (0)
    411 
    412 	if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
    413 		if (sc->sc_x != x) {
    414 			NEXT;
    415 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
    416 			ev->value = x;
    417 			TIMESTAMP;
    418 			ADVANCE;
    419 			sc->sc_x = x;
    420 		}
    421 	} else {
    422 		if (sc->sc_dx) {
    423 			NEXT;
    424 			ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
    425 			ev->value = sc->sc_dx;
    426 			TIMESTAMP;
    427 			ADVANCE;
    428 			sc->sc_dx = 0;
    429 		}
    430 	}
    431 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
    432 		if (sc->sc_y != y) {
    433 			NEXT;
    434 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
    435 			ev->value = y;
    436 			TIMESTAMP;
    437 			ADVANCE;
    438 			sc->sc_y = y;
    439 		}
    440 	} else {
    441 		if (sc->sc_dy) {
    442 			NEXT;
    443 			ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
    444 			ev->value = sc->sc_dy;
    445 			TIMESTAMP;
    446 			ADVANCE;
    447 			sc->sc_dy = 0;
    448 		}
    449 	}
    450 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
    451 		if (sc->sc_z != z) {
    452 			NEXT;
    453 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
    454 			ev->value = z;
    455 			TIMESTAMP;
    456 			ADVANCE;
    457 			sc->sc_z = z;
    458 		}
    459 	} else {
    460 		if (sc->sc_dz) {
    461 			NEXT;
    462 			ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
    463 			ev->value = sc->sc_dz;
    464 			TIMESTAMP;
    465 			ADVANCE;
    466 			sc->sc_dz = 0;
    467 		}
    468 	}
    469 	if (flags & WSMOUSE_INPUT_ABSOLUTE_W) {
    470 		if (sc->sc_w != w) {
    471 			NEXT;
    472 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_W;
    473 			ev->value = w;
    474 			TIMESTAMP;
    475 			ADVANCE;
    476 			sc->sc_w = w;
    477 		}
    478 	} else {
    479 		if (sc->sc_dw) {
    480 			NEXT;
    481 			ev->type = WSCONS_EVENT_MOUSE_DELTA_W;
    482 			ev->value = sc->sc_dw;
    483 			TIMESTAMP;
    484 			ADVANCE;
    485 			sc->sc_dw = 0;
    486 		}
    487 	}
    488 
    489 	mb = sc->sc_mb;
    490 	while ((d = mb ^ ub) != 0) {
    491 		int btnno;
    492 
    493 		/*
    494 		 * Cancel button repeating if button status changed.
    495 		 */
    496 		if (sc->sc_repeat_button != -1) {
    497 			KASSERT(sc->sc_repeat_button >= 0);
    498 			KASSERT(sc->sc_repeat.wr_buttons &
    499                             (1 << sc->sc_repeat_button));
    500 			ub &= ~(1 << sc->sc_repeat_button);
    501 			sc->sc_repeat_button = -1;
    502 			callout_stop(&sc->sc_repeat_callout);
    503 		}
    504 
    505 		/*
    506 		 * Mouse button change.  Find the first change and drop
    507 		 * it into the event queue.
    508 		 */
    509 		NEXT;
    510 		btnno = ffs(d) - 1;
    511 		KASSERT(btnno >= 0);
    512 
    513 		ev->value = btnno;
    514 		d = 1 << ev->value;
    515 		ev->type =
    516 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    517 		TIMESTAMP;
    518 		ADVANCE;
    519 		ub ^= d;
    520 
    521 		/*
    522 		 * Program button repeating if configured for this button.
    523 		 */
    524 		if ((mb & d) && (sc->sc_repeat.wr_buttons & (1 << btnno)) &&
    525 		    sc->sc_repeat.wr_delay_first > 0) {
    526 			sc->sc_repeat_button = btnno;
    527 			sc->sc_repeat_delay = sc->sc_repeat.wr_delay_first;
    528 			callout_reset(&sc->sc_repeat_callout,
    529 			    (hz * sc->sc_repeat_delay) / 1000, wsmouse_repeat,
    530 			    sc);
    531 		}
    532 	}
    533 out:
    534 	if (any) {
    535 		sc->sc_ub = ub;
    536 		evar->put = put;
    537 		WSEVENT_WAKEUP(evar);
    538 #if NWSMUX > 0
    539 		DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
    540 			    sc->sc_base.me_dv.dv_xname, evar));
    541 #endif
    542 	}
    543 }
    544 
    545 static void
    546 wsmouse_repeat(void *v)
    547 {
    548 	int oldspl;
    549 	unsigned int newdelay;
    550 	struct wsmouse_softc *sc;
    551 	struct wseventvar *evar;
    552 
    553 	oldspl = spltty();
    554 	sc = (struct wsmouse_softc *)v;
    555 
    556 	if (sc->sc_repeat_button == -1) {
    557 		/* Race condition: a "button up" event came in when
    558 		 * this function was already called but did not do
    559 		 * spltty() yet. */
    560 		splx(oldspl);
    561 		return;
    562 	}
    563 	KASSERT(sc->sc_repeat_button >= 0);
    564 
    565 	KASSERT(sc->sc_repeat.wr_buttons & (1 << sc->sc_repeat_button));
    566 
    567 	newdelay = sc->sc_repeat_delay;
    568 
    569 	evar = sc->sc_base.me_evp;
    570 	if ((evar->put + 1) % WSEVENT_QSIZE != evar->get &&
    571 	    (evar->put + 2) % WSEVENT_QSIZE != evar->get) {
    572 		/* Queue has room for at least two more events. */
    573 		int oldspl2;
    574 		struct wscons_event *ev;
    575 
    576 		/* Construct and inject new button up event. */
    577 		ev = &evar->q[evar->put];
    578 		ev->value = sc->sc_repeat_button;
    579 		ev->type = WSCONS_EVENT_MOUSE_UP;
    580 		oldspl2 = splhigh();
    581 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);
    582 		splx(oldspl2);
    583 		evar->put = (evar->put + 1) % WSEVENT_QSIZE;
    584 
    585 		/* Construct and inject new button down event. */
    586 		ev = &evar->q[evar->put];
    587 		ev->value = sc->sc_repeat_button;
    588 		ev->type = WSCONS_EVENT_MOUSE_DOWN;
    589 		oldspl2 = splhigh();
    590 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);
    591 		splx(oldspl2);
    592 		evar->put = (evar->put + 1) % WSEVENT_QSIZE;
    593 
    594 		sc->sc_ub = 1 << sc->sc_repeat_button;
    595 
    596 		WSEVENT_WAKEUP(evar);
    597 
    598 		if (newdelay - sc->sc_repeat.wr_delay_decrement <
    599 		    sc->sc_repeat.wr_delay_minimum)
    600 			newdelay = sc->sc_repeat.wr_delay_minimum;
    601 		else if (newdelay > sc->sc_repeat.wr_delay_minimum)
    602 			newdelay -= sc->sc_repeat.wr_delay_decrement;
    603 		KASSERT(newdelay >= sc->sc_repeat.wr_delay_minimum &&
    604 		    newdelay <= sc->sc_repeat.wr_delay_first);
    605 	}
    606 
    607 	/*
    608 	 * Reprogram the repeating event.
    609 	 */
    610 	sc->sc_repeat_delay = newdelay;
    611 	callout_reset(&sc->sc_repeat_callout, (hz * newdelay) / 1000,
    612 	    wsmouse_repeat, sc);
    613 
    614 	splx(oldspl);
    615 }
    616 
    617 int
    618 wsmouseopen(dev_t dev, int flags, int mode, struct lwp *l)
    619 {
    620 	struct wsmouse_softc *sc;
    621 	struct wseventvar *evar;
    622 	int error, unit;
    623 
    624 	unit = minor(dev);
    625 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    626 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    627 		return (ENXIO);
    628 
    629 #if NWSMUX > 0
    630 	DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
    631 		 sc->sc_base.me_parent, l));
    632 #endif
    633 
    634 	if (sc->sc_dying)
    635 		return (EIO);
    636 
    637 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    638 		return (0);			/* always allow open for write
    639 						   so ioctl() is possible. */
    640 
    641 	if (sc->sc_base.me_evp != NULL)
    642 		return (EBUSY);
    643 
    644 	evar = &sc->sc_base.me_evar;
    645 	wsevent_init(evar);
    646 	sc->sc_base.me_evp = evar;
    647 	evar->io = l->l_proc;
    648 
    649 	error = wsmousedoopen(sc, evar);
    650 	if (error) {
    651 		DPRINTF(("wsmouseopen: %s open failed\n",
    652 			 sc->sc_base.me_dv.dv_xname));
    653 		sc->sc_base.me_evp = NULL;
    654 		wsevent_fini(evar);
    655 	}
    656 	return (error);
    657 }
    658 
    659 int
    660 wsmouseclose(dev_t dev, int flags, int mode, struct lwp *l)
    661 {
    662 	struct wsmouse_softc *sc =
    663 	    (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
    664 	struct wseventvar *evar = sc->sc_base.me_evp;
    665 
    666 	if (evar == NULL)
    667 		/* not open for read */
    668 		return (0);
    669 	sc->sc_base.me_evp = NULL;
    670 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    671 	wsevent_fini(evar);
    672 
    673 	return (0);
    674 }
    675 
    676 int
    677 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
    678 {
    679 	sc->sc_base.me_evp = evp;
    680 	sc->sc_x = INVALID_X;
    681 	sc->sc_y = INVALID_Y;
    682 	sc->sc_z = INVALID_Z;
    683 
    684 	/* Stop button repeating when messing with the device. */
    685 	if (sc->sc_repeat_button != -1) {
    686 		KASSERT(sc->sc_repeat_button >= 0);
    687 		sc->sc_repeat_button = -1;
    688 		callout_stop(&sc->sc_repeat_callout);
    689 	}
    690 
    691 	/* enable the device, and punt if that's not possible */
    692 	return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    693 }
    694 
    695 int
    696 wsmouseread(dev_t dev, struct uio *uio, int flags)
    697 {
    698 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    699 	int error;
    700 
    701 	if (sc->sc_dying)
    702 		return (EIO);
    703 
    704 #ifdef DIAGNOSTIC
    705 	if (sc->sc_base.me_evp == NULL) {
    706 		printf("wsmouseread: evp == NULL\n");
    707 		return (EINVAL);
    708 	}
    709 #endif
    710 
    711 	sc->sc_refcnt++;
    712 	error = wsevent_read(sc->sc_base.me_evp, uio, flags);
    713 	if (--sc->sc_refcnt < 0) {
    714 		wakeup(sc);
    715 		error = EIO;
    716 	}
    717 	return (error);
    718 }
    719 
    720 int
    721 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    722 {
    723 	return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
    724 			       cmd, data, flag, l));
    725 }
    726 
    727 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    728 int
    729 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    730 	       struct lwp *l)
    731 {
    732 	struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
    733 	int error;
    734 
    735 	sc->sc_refcnt++;
    736 	error = wsmouse_do_ioctl(sc, cmd, data, flag, l);
    737 	if (--sc->sc_refcnt < 0)
    738 		wakeup(sc);
    739 	return (error);
    740 }
    741 
    742 int
    743 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data,
    744 		 int flag, struct lwp *l)
    745 {
    746 	int error;
    747 	struct wsmouse_repeat *wr;
    748 
    749 	if (sc->sc_dying)
    750 		return (EIO);
    751 
    752 	/*
    753 	 * Try the generic ioctls that the wsmouse interface supports.
    754 	 */
    755 	switch (cmd) {
    756 	case FIONBIO:		/* we will remove this someday (soon???) */
    757 		return (0);
    758 
    759 	case FIOASYNC:
    760 		if (sc->sc_base.me_evp == NULL)
    761 			return (EINVAL);
    762 		sc->sc_base.me_evp->async = *(int *)data != 0;
    763 		return (0);
    764 
    765 	case FIOSETOWN:
    766 		if (sc->sc_base.me_evp == NULL)
    767 			return (EINVAL);
    768 		if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
    769 		    && *(int *)data != sc->sc_base.me_evp->io->p_pid)
    770 			return (EPERM);
    771 		return (0);
    772 
    773 	case TIOCSPGRP:
    774 		if (sc->sc_base.me_evp == NULL)
    775 			return (EINVAL);
    776 		if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
    777 			return (EPERM);
    778 		return (0);
    779 	}
    780 
    781 	/*
    782 	 * Try the wsmouse specific ioctls.
    783 	 */
    784 	switch (cmd) {
    785 	case WSMOUSEIO_GETREPEAT:
    786 		wr = (struct wsmouse_repeat *)data;
    787 		memcpy(wr, &sc->sc_repeat, sizeof(sc->sc_repeat));
    788 		return 0;
    789 
    790 	case WSMOUSEIO_SETREPEAT:
    791 		if ((flag & FWRITE) == 0)
    792 			return EACCES;
    793 
    794 		/* Validate input data. */
    795 		wr = (struct wsmouse_repeat *)data;
    796 		if (wr->wr_delay_first != 0 &&
    797 		    (wr->wr_delay_first < wr->wr_delay_decrement ||
    798 		     wr->wr_delay_first < wr->wr_delay_minimum ||
    799 		     wr->wr_delay_first < wr->wr_delay_minimum +
    800 		     wr->wr_delay_decrement))
    801 			return EINVAL;
    802 
    803 		/* Stop current repeating and set new data. */
    804 		sc->sc_repeat_button = -1;
    805 		callout_stop(&sc->sc_repeat_callout);
    806 		memcpy(&sc->sc_repeat, wr, sizeof(sc->sc_repeat));
    807 
    808 		return 0;
    809 	}
    810 
    811 	/*
    812 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    813 	 * if it didn't recognize the request.
    814 	 */
    815 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    816 	    data, flag, l);
    817 	return (error); /* may be EPASSTHROUGH */
    818 }
    819 
    820 int
    821 wsmousepoll(dev_t dev, int events, struct lwp *l)
    822 {
    823 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    824 
    825 	if (sc->sc_base.me_evp == NULL)
    826 		return (POLLERR);
    827 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
    828 }
    829 
    830 int
    831 wsmousekqfilter(dev_t dev, struct knote *kn)
    832 {
    833 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    834 
    835 	if (sc->sc_base.me_evp == NULL)
    836 		return (1);
    837 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
    838 }
    839 
    840 #if NWSMUX > 0
    841 int
    842 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
    843 {
    844 	struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
    845 
    846 	if (sc->sc_base.me_evp != NULL)
    847 		return (EBUSY);
    848 
    849 	return wsmousedoopen(sc, evp);
    850 }
    851 
    852 int
    853 wsmouse_mux_close(struct wsevsrc *me)
    854 {
    855 	struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
    856 
    857 	sc->sc_base.me_evp = NULL;
    858 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    859 
    860 	return (0);
    861 }
    862 
    863 int
    864 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
    865 {
    866 	struct wsmouse_softc *sc;
    867 
    868 	if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
    869 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    870 		return (ENXIO);
    871 
    872 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    873 		return (EBUSY);
    874 
    875 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    876 }
    877 #endif /* NWSMUX > 0 */
    878