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