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