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