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