Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.55.10.6
      1 /* $NetBSD: wsmouse.c,v 1.55.10.6 2009/01/17 13:29:10 mjf 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.55.10.6 2009/01/17 13:29:10 mjf 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 
    129 #include <dev/wscons/wsconsio.h>
    130 #include <dev/wscons/wsmousevar.h>
    131 #include <dev/wscons/wseventvar.h>
    132 #include <dev/wscons/wsmuxvar.h>
    133 
    134 #if defined(WSMUX_DEBUG) && NWSMUX > 0
    135 #define DPRINTF(x)	if (wsmuxdebug) printf x
    136 #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
    137 extern int wsmuxdebug;
    138 #else
    139 #define DPRINTF(x)
    140 #define DPRINTFN(n,x)
    141 #endif
    142 
    143 #define INVALID_X	INT_MAX
    144 #define INVALID_Y	INT_MAX
    145 #define INVALID_Z	INT_MAX
    146 
    147 struct wsmouse_softc {
    148 	struct wsevsrc	sc_base;
    149 
    150 	const struct wsmouse_accessops *sc_accessops;
    151 	void		*sc_accesscookie;
    152 
    153 	u_int		sc_mb;		/* mouse button state */
    154 	u_int		sc_ub;		/* user button state */
    155 	int		sc_dx;		/* delta-x */
    156 	int		sc_dy;		/* delta-y */
    157 	int		sc_dz;		/* delta-z */
    158 	int		sc_dw;		/* delta-w */
    159 	int		sc_x;		/* absolute-x */
    160 	int		sc_y;		/* absolute-y */
    161 	int		sc_z;		/* absolute-z */
    162 	int		sc_w;		/* absolute-w */
    163 
    164 	int		sc_refcnt;
    165 	u_char		sc_dying;	/* device is being detached */
    166 
    167 	struct wsmouse_repeat	sc_repeat;
    168 	int			sc_repeat_button;
    169 	callout_t		sc_repeat_callout;
    170 	unsigned int		sc_repeat_delay;
    171 };
    172 
    173 static int  wsmouse_match(device_t, cfdata_t, void *);
    174 static void wsmouse_attach(device_t, device_t, void *);
    175 static int  wsmouse_detach(device_t, int);
    176 static int  wsmouse_activate(device_t, enum devact);
    177 
    178 static int  wsmouse_do_ioctl(struct wsmouse_softc *, u_long, void *,
    179 			     int, struct lwp *);
    180 
    181 #if NWSMUX > 0
    182 static int  wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
    183 static int  wsmouse_mux_close(struct wsevsrc *);
    184 #endif
    185 
    186 static int  wsmousedoioctl(device_t, u_long, void *, int, struct lwp *);
    187 
    188 static int  wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
    189 
    190 CFATTACH_DECL_NEW(wsmouse, sizeof (struct wsmouse_softc),
    191     wsmouse_match, wsmouse_attach, wsmouse_detach, wsmouse_activate);
    192 
    193 static void wsmouse_repeat(void *v);
    194 
    195 extern struct cfdriver wsmouse_cd;
    196 
    197 dev_type_open(wsmouseopen);
    198 dev_type_close(wsmouseclose);
    199 dev_type_read(wsmouseread);
    200 dev_type_ioctl(wsmouseioctl);
    201 dev_type_poll(wsmousepoll);
    202 dev_type_kqfilter(wsmousekqfilter);
    203 
    204 const struct cdevsw wsmouse_cdevsw = {
    205 	wsmouseopen, wsmouseclose, wsmouseread, nowrite, wsmouseioctl,
    206 	nostop, notty, wsmousepoll, nommap, wsmousekqfilter, D_OTHER
    207 };
    208 
    209 #if NWSMUX > 0
    210 struct wssrcops wsmouse_srcops = {
    211 	WSMUX_MOUSE,
    212 	wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
    213 };
    214 #endif
    215 
    216 /*
    217  * Print function (for parent devices).
    218  */
    219 int
    220 wsmousedevprint(void *aux, const char *pnp)
    221 {
    222 
    223 	if (pnp)
    224 		aprint_normal("wsmouse at %s", pnp);
    225 	return (UNCONF);
    226 }
    227 
    228 int
    229 wsmouse_match(device_t parent, cfdata_t match, void *aux)
    230 {
    231 	return (1);
    232 }
    233 
    234 void
    235 wsmouse_attach(device_t parent, device_t self, void *aux)
    236 {
    237         struct wsmouse_softc *sc = device_private(self);
    238 	struct wsmousedev_attach_args *ap = aux;
    239 #if NWSMUX > 0
    240 	int mux, error;
    241 #endif
    242 	int major;
    243 
    244 	sc->sc_base.me_dv = self;
    245 	sc->sc_accessops = ap->accessops;
    246 	sc->sc_accesscookie = ap->accesscookie;
    247 
    248 	/* Initialize button repeating. */
    249 	memset(&sc->sc_repeat, 0, sizeof(sc->sc_repeat));
    250 	sc->sc_repeat_button = -1;
    251 	sc->sc_repeat_delay = 0;
    252 	callout_init(&sc->sc_repeat_callout, 0);
    253 	callout_setfunc(&sc->sc_repeat_callout, wsmouse_repeat, sc);
    254 
    255 #if NWSMUX > 0
    256 	sc->sc_base.me_ops = &wsmouse_srcops;
    257 	mux = device_cfdata(self)->wsmousedevcf_mux;
    258 	if (mux >= 0) {
    259 		error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
    260 		if (error)
    261 			aprint_error(" attach error=%d", error);
    262 		else
    263 			aprint_normal(" mux %d", mux);
    264 	}
    265 #else
    266 	if (device_cfdata(self)->wsmousedevcf_mux >= 0)
    267 		aprint_normal(" (mux ignored)");
    268 #endif
    269 
    270 	aprint_naive("\n");
    271 	aprint_normal("\n");
    272 
    273 	if (!pmf_device_register(self, NULL, NULL))
    274 		aprint_error_dev(self, "couldn't establish power handler\n");
    275 
    276 	major = cdevsw_lookup_major(&wsmouse_cdevsw);
    277 	device_register_name(makedev(major, device_unit(self)), self,
    278 	    true, DEV_OTHER, device_xname(self));
    279 }
    280 
    281 int
    282 wsmouse_activate(device_t self, enum devact act)
    283 {
    284 	struct wsmouse_softc *sc = device_private(self);
    285 
    286 	if (act == DVACT_DEACTIVATE)
    287 		sc->sc_dying = 1;
    288 	return (0);
    289 }
    290 
    291 /*
    292  * Detach a mouse.  To keep track of users of the softc we keep
    293  * a reference count that's incremented while inside, e.g., read.
    294  * If the mouse is active and the reference count is > 0 (0 is the
    295  * normal state) we post an event and then wait for the process
    296  * that had the reference to wake us up again.  Then we blow away the
    297  * vnode and return (which will deallocate the softc).
    298  */
    299 int
    300 wsmouse_detach(device_t self, int flags)
    301 {
    302 	struct wsmouse_softc *sc = device_private(self);
    303 	struct wseventvar *evar;
    304 	int maj, mn;
    305 	int s;
    306 
    307 #if NWSMUX > 0
    308 	/* Tell parent mux we're leaving. */
    309 	if (sc->sc_base.me_parent != NULL) {
    310 		DPRINTF(("wsmouse_detach:\n"));
    311 		wsmux_detach_sc(&sc->sc_base);
    312 	}
    313 #endif
    314 
    315 	/* If we're open ... */
    316 	evar = sc->sc_base.me_evp;
    317 	if (evar != NULL && evar->io != NULL) {
    318 		s = spltty();
    319 		if (--sc->sc_refcnt >= 0) {
    320 			struct wscons_event event;
    321 
    322 			/* Wake everyone by generating a dummy event. */
    323 			event.type = 0;
    324 			event.value = 0;
    325 			if (wsevent_inject(evar, &event, 1) != 0)
    326 				wsevent_wakeup(evar);
    327 
    328 			/* Wait for processes to go away. */
    329 			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
    330 				printf("wsmouse_detach: %s didn't detach\n",
    331 				       device_xname(self));
    332 		}
    333 		splx(s);
    334 	}
    335 
    336 	device_deregister_all(self);
    337 
    338 	/* locate the major number */
    339 	maj = cdevsw_lookup_major(&wsmouse_cdevsw);
    340 
    341 	/* Nuke the vnodes for any open instances (calls close). */
    342 	mn = device_unit(self);
    343 	vdevgone(maj, mn, mn, VCHR);
    344 
    345 	return (0);
    346 }
    347 
    348 void
    349 wsmouse_input(device_t wsmousedev, u_int btns /* 0 is up */,
    350 	int x, int y, int z, int w, u_int flags)
    351 {
    352 	struct wsmouse_softc *sc = device_private(wsmousedev);
    353 	struct wseventvar *evar;
    354 	int mb, ub, d, nevents;
    355 	/* one for each dimension (4) + a bit for each button */
    356 	struct wscons_event events[4 + sizeof(d) * 8];
    357 
    358         /*
    359          * Discard input if not open.
    360          */
    361 	evar = sc->sc_base.me_evp;
    362 	if (evar == NULL)
    363 		return;
    364 
    365 #ifdef DIAGNOSTIC
    366 	if (evar->q == NULL) {
    367 		printf("wsmouse_input: evar->q=NULL\n");
    368 		return;
    369 	}
    370 #endif
    371 
    372 #if NWSMUX > 0
    373 	DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
    374 		    device_xname(sc->sc_base.me_dv),
    375 		    sc->sc_base.me_parent, evar));
    376 #endif
    377 
    378 	sc->sc_mb = btns;
    379 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
    380 		sc->sc_dx += x;
    381 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
    382 		sc->sc_dy += y;
    383 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
    384 		sc->sc_dz += z;
    385 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_W))
    386 		sc->sc_dw += w;
    387 
    388 	/*
    389 	 * We have at least one event (mouse button, delta-X, or
    390 	 * delta-Y; possibly all three, and possibly three separate
    391 	 * button events).  Deliver these events until we are out
    392 	 * of changes or out of room.  As events get delivered,
    393 	 * mark them `unchanged'.
    394 	 */
    395 	ub = sc->sc_ub;
    396 	nevents = 0;
    397 
    398 	if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
    399 		if (sc->sc_x != x) {
    400 			events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
    401 			events[nevents].value = x;
    402 			nevents++;
    403 		}
    404 	} else {
    405 		if (sc->sc_dx) {
    406 			events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_X;
    407 			events[nevents].value = sc->sc_dx;
    408 			nevents++;
    409 		}
    410 	}
    411 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
    412 		if (sc->sc_y != y) {
    413 			events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
    414 			events[nevents].value = y;
    415 			nevents++;
    416 		}
    417 	} else {
    418 		if (sc->sc_dy) {
    419 			events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Y;
    420 			events[nevents].value = sc->sc_dy;
    421 			nevents++;
    422 		}
    423 	}
    424 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
    425 		if (sc->sc_z != z) {
    426 			events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
    427 			events[nevents].value = z;
    428 			nevents++;
    429 		}
    430 	} else {
    431 		if (sc->sc_dz) {
    432 			events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Z;
    433 			events[nevents].value = sc->sc_dz;
    434 			nevents++;
    435 		}
    436 	}
    437 	if (flags & WSMOUSE_INPUT_ABSOLUTE_W) {
    438 		if (sc->sc_w != w) {
    439 			events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_W;
    440 			events[nevents].value = w;
    441 			nevents++;
    442 		}
    443 	} else {
    444 		if (sc->sc_dw) {
    445 			events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_W;
    446 			events[nevents].value = sc->sc_dw;
    447 			nevents++;
    448 		}
    449 	}
    450 
    451 	mb = sc->sc_mb;
    452 	while ((d = mb ^ ub) != 0) {
    453 		int btnno;
    454 
    455 		/*
    456 		 * Cancel button repeating if button status changed.
    457 		 */
    458 		if (sc->sc_repeat_button != -1) {
    459 			KASSERT(sc->sc_repeat_button >= 0);
    460 			KASSERT(sc->sc_repeat.wr_buttons &
    461                             (1 << sc->sc_repeat_button));
    462 			ub &= ~(1 << sc->sc_repeat_button);
    463 			sc->sc_repeat_button = -1;
    464 			callout_stop(&sc->sc_repeat_callout);
    465 		}
    466 
    467 		/*
    468 		 * Mouse button change.  Find the first change and drop
    469 		 * it into the event queue.
    470 		 */
    471 		btnno = ffs(d) - 1;
    472 		KASSERT(btnno >= 0);
    473 
    474 		if (nevents >= sizeof(events) / sizeof(events[0])) {
    475 			aprint_error_dev(sc->sc_base.me_dv,
    476 			    "Event queue full (button status mb=0x%x"
    477 			    " ub=0x%x)\n", mb, ub);
    478 			break;
    479 		}
    480 
    481 		events[nevents].type =
    482 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    483 		events[nevents].value = btnno;
    484 		nevents++;
    485 
    486 		ub ^= (1 << btnno);
    487 
    488 		/*
    489 		 * Program button repeating if configured for this button.
    490 		 */
    491 		if ((mb & d) && (sc->sc_repeat.wr_buttons & (1 << btnno)) &&
    492 		    sc->sc_repeat.wr_delay_first > 0) {
    493 			sc->sc_repeat_button = btnno;
    494 			sc->sc_repeat_delay = sc->sc_repeat.wr_delay_first;
    495 			callout_schedule(&sc->sc_repeat_callout,
    496 			    mstohz(sc->sc_repeat_delay));
    497 		}
    498 	}
    499 
    500 	if (nevents == 0 || wsevent_inject(evar, events, nevents) == 0) {
    501 		/* All events were correctly injected into the queue.
    502 		 * Synchronize the mouse's status with what the user
    503 		 * has received. */
    504 		sc->sc_x = x; sc->sc_dx = 0;
    505 		sc->sc_y = y; sc->sc_dy = 0;
    506 		sc->sc_z = z; sc->sc_dz = 0;
    507 		sc->sc_w = w; sc->sc_dw = 0;
    508 		sc->sc_ub = ub;
    509 #if NWSMUX > 0
    510 		DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
    511 			    device_xname(sc->sc_base.me_dv), evar));
    512 #endif
    513 	}
    514 }
    515 
    516 static void
    517 wsmouse_repeat(void *v)
    518 {
    519 	int oldspl;
    520 	unsigned int newdelay;
    521 	struct wsmouse_softc *sc;
    522 	struct wscons_event events[2];
    523 
    524 	oldspl = spltty();
    525 	sc = (struct wsmouse_softc *)v;
    526 
    527 	if (sc->sc_repeat_button == -1) {
    528 		/* Race condition: a "button up" event came in when
    529 		 * this function was already called but did not do
    530 		 * spltty() yet. */
    531 		splx(oldspl);
    532 		return;
    533 	}
    534 	KASSERT(sc->sc_repeat_button >= 0);
    535 
    536 	KASSERT(sc->sc_repeat.wr_buttons & (1 << sc->sc_repeat_button));
    537 
    538 	newdelay = sc->sc_repeat_delay;
    539 
    540 	events[0].type = WSCONS_EVENT_MOUSE_UP;
    541 	events[0].value = sc->sc_repeat_button;
    542 	events[1].type = WSCONS_EVENT_MOUSE_DOWN;
    543 	events[1].value = sc->sc_repeat_button;
    544 
    545 	if (wsevent_inject(sc->sc_base.me_evp, events, 2) == 0) {
    546 		sc->sc_ub = 1 << sc->sc_repeat_button;
    547 
    548 		if (newdelay - sc->sc_repeat.wr_delay_decrement <
    549 		    sc->sc_repeat.wr_delay_minimum)
    550 			newdelay = sc->sc_repeat.wr_delay_minimum;
    551 		else if (newdelay > sc->sc_repeat.wr_delay_minimum)
    552 			newdelay -= sc->sc_repeat.wr_delay_decrement;
    553 		KASSERT(newdelay >= sc->sc_repeat.wr_delay_minimum &&
    554 		    newdelay <= sc->sc_repeat.wr_delay_first);
    555 	}
    556 
    557 	/*
    558 	 * Reprogram the repeating event.
    559 	 */
    560 	sc->sc_repeat_delay = newdelay;
    561 	callout_schedule(&sc->sc_repeat_callout, mstohz(newdelay));
    562 
    563 	splx(oldspl);
    564 }
    565 
    566 int
    567 wsmouseopen(dev_t dev, int flags, int mode, struct lwp *l)
    568 {
    569 	struct wsmouse_softc *sc;
    570 	struct wseventvar *evar;
    571 	int error;
    572 
    573 	sc = device_lookup_private(&wsmouse_cd, minor(dev));
    574 	if (sc == NULL)
    575 		return ENXIO;
    576 
    577 #if NWSMUX > 0
    578 	DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", device_xname(sc->sc_base.me_dv),
    579 		 sc->sc_base.me_parent, l));
    580 #endif
    581 
    582 	if (sc->sc_dying)
    583 		return (EIO);
    584 
    585 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    586 		return (0);			/* always allow open for write
    587 						   so ioctl() is possible. */
    588 
    589 	if (sc->sc_base.me_evp != NULL)
    590 		return (EBUSY);
    591 
    592 	evar = &sc->sc_base.me_evar;
    593 	wsevent_init(evar, l->l_proc);
    594 	sc->sc_base.me_evp = evar;
    595 
    596 	error = wsmousedoopen(sc, evar);
    597 	if (error) {
    598 		DPRINTF(("wsmouseopen: %s open failed\n",
    599 			 device_xname(sc->sc_base.me_dv)));
    600 		sc->sc_base.me_evp = NULL;
    601 		wsevent_fini(evar);
    602 	}
    603 	return (error);
    604 }
    605 
    606 int
    607 wsmouseclose(dev_t dev, int flags, int mode,
    608     struct lwp *l)
    609 {
    610 	struct wsmouse_softc *sc =
    611 	    device_lookup_private(&wsmouse_cd, minor(dev));
    612 	struct wseventvar *evar = sc->sc_base.me_evp;
    613 
    614 	if (evar == NULL)
    615 		/* not open for read */
    616 		return (0);
    617 	sc->sc_base.me_evp = NULL;
    618 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    619 	wsevent_fini(evar);
    620 
    621 	return (0);
    622 }
    623 
    624 int
    625 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
    626 {
    627 	sc->sc_base.me_evp = evp;
    628 	sc->sc_x = INVALID_X;
    629 	sc->sc_y = INVALID_Y;
    630 	sc->sc_z = INVALID_Z;
    631 
    632 	/* Stop button repeating when messing with the device. */
    633 	if (sc->sc_repeat_button != -1) {
    634 		KASSERT(sc->sc_repeat_button >= 0);
    635 		sc->sc_repeat_button = -1;
    636 		callout_stop(&sc->sc_repeat_callout);
    637 	}
    638 
    639 	/* enable the device, and punt if that's not possible */
    640 	return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    641 }
    642 
    643 int
    644 wsmouseread(dev_t dev, struct uio *uio, int flags)
    645 {
    646 	struct wsmouse_softc *sc =
    647 	    device_lookup_private(&wsmouse_cd, minor(dev));
    648 	int error;
    649 
    650 	if (sc->sc_dying)
    651 		return (EIO);
    652 
    653 #ifdef DIAGNOSTIC
    654 	if (sc->sc_base.me_evp == NULL) {
    655 		printf("wsmouseread: evp == NULL\n");
    656 		return (EINVAL);
    657 	}
    658 #endif
    659 
    660 	sc->sc_refcnt++;
    661 	error = wsevent_read(sc->sc_base.me_evp, uio, flags);
    662 	if (--sc->sc_refcnt < 0) {
    663 		wakeup(sc);
    664 		error = EIO;
    665 	}
    666 	return (error);
    667 }
    668 
    669 int
    670 wsmouseioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    671 {
    672 	return (wsmousedoioctl(device_lookup(&wsmouse_cd, minor(dev)),
    673 			       cmd, data, flag, l));
    674 }
    675 
    676 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    677 int
    678 wsmousedoioctl(device_t dv, u_long cmd, void *data, int flag,
    679 	       struct lwp *l)
    680 {
    681 	struct wsmouse_softc *sc = device_private(dv);
    682 	int error;
    683 
    684 	sc->sc_refcnt++;
    685 	error = wsmouse_do_ioctl(sc, cmd, data, flag, l);
    686 	if (--sc->sc_refcnt < 0)
    687 		wakeup(sc);
    688 	return (error);
    689 }
    690 
    691 int
    692 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, void *data,
    693 		 int flag, struct lwp *l)
    694 {
    695 	int error;
    696 	struct wsmouse_repeat *wr;
    697 
    698 	if (sc->sc_dying)
    699 		return (EIO);
    700 
    701 	/*
    702 	 * Try the generic ioctls that the wsmouse interface supports.
    703 	 */
    704 	switch (cmd) {
    705 	case FIONBIO:		/* we will remove this someday (soon???) */
    706 		return (0);
    707 
    708 	case FIOASYNC:
    709 		if (sc->sc_base.me_evp == NULL)
    710 			return (EINVAL);
    711 		sc->sc_base.me_evp->async = *(int *)data != 0;
    712 		return (0);
    713 
    714 	case FIOSETOWN:
    715 		if (sc->sc_base.me_evp == NULL)
    716 			return (EINVAL);
    717 		if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
    718 		    && *(int *)data != sc->sc_base.me_evp->io->p_pid)
    719 			return (EPERM);
    720 		return (0);
    721 
    722 	case TIOCSPGRP:
    723 		if (sc->sc_base.me_evp == NULL)
    724 			return (EINVAL);
    725 		if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
    726 			return (EPERM);
    727 		return (0);
    728 	}
    729 
    730 	/*
    731 	 * Try the wsmouse specific ioctls.
    732 	 */
    733 	switch (cmd) {
    734 	case WSMOUSEIO_GETREPEAT:
    735 		wr = (struct wsmouse_repeat *)data;
    736 		memcpy(wr, &sc->sc_repeat, sizeof(sc->sc_repeat));
    737 		return 0;
    738 
    739 	case WSMOUSEIO_SETREPEAT:
    740 		if ((flag & FWRITE) == 0)
    741 			return EACCES;
    742 
    743 		/* Validate input data. */
    744 		wr = (struct wsmouse_repeat *)data;
    745 		if (wr->wr_delay_first != 0 &&
    746 		    (wr->wr_delay_first < wr->wr_delay_decrement ||
    747 		     wr->wr_delay_first < wr->wr_delay_minimum ||
    748 		     wr->wr_delay_first < wr->wr_delay_minimum +
    749 		     wr->wr_delay_decrement))
    750 			return EINVAL;
    751 
    752 		/* Stop current repeating and set new data. */
    753 		sc->sc_repeat_button = -1;
    754 		callout_stop(&sc->sc_repeat_callout);
    755 		memcpy(&sc->sc_repeat, wr, sizeof(sc->sc_repeat));
    756 
    757 		return 0;
    758 
    759 	case WSMOUSEIO_SETVERSION:
    760 		return wsevent_setversion(sc->sc_base.me_evp, *(int *)data);
    761 	}
    762 
    763 	/*
    764 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    765 	 * if it didn't recognize the request.
    766 	 */
    767 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    768 	    data, flag, l);
    769 	return (error); /* may be EPASSTHROUGH */
    770 }
    771 
    772 int
    773 wsmousepoll(dev_t dev, int events, struct lwp *l)
    774 {
    775 	struct wsmouse_softc *sc =
    776 	    device_lookup_private(&wsmouse_cd, minor(dev));
    777 
    778 	if (sc->sc_base.me_evp == NULL)
    779 		return (POLLERR);
    780 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
    781 }
    782 
    783 int
    784 wsmousekqfilter(dev_t dev, struct knote *kn)
    785 {
    786 	struct wsmouse_softc *sc =
    787 	    device_lookup_private(&wsmouse_cd, minor(dev));
    788 
    789 	if (sc->sc_base.me_evp == NULL)
    790 		return (1);
    791 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
    792 }
    793 
    794 #if NWSMUX > 0
    795 int
    796 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
    797 {
    798 	struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
    799 
    800 	if (sc->sc_base.me_evp != NULL)
    801 		return (EBUSY);
    802 
    803 	return wsmousedoopen(sc, evp);
    804 }
    805 
    806 int
    807 wsmouse_mux_close(struct wsevsrc *me)
    808 {
    809 	struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
    810 
    811 	sc->sc_base.me_evp = NULL;
    812 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    813 
    814 	return (0);
    815 }
    816 
    817 int
    818 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
    819 {
    820 	struct wsmouse_softc *sc;
    821 
    822 	sc = device_lookup_private(&wsmouse_cd, unit);
    823 	if (sc == NULL)
    824 		return ENXIO;
    825 
    826 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    827 		return (EBUSY);
    828 
    829 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    830 }
    831 #endif /* NWSMUX > 0 */
    832