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