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