Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.13
      1  1.13     bjh21 /* $NetBSD: wsmouse.c,v 1.13 2001/02/13 01:14:45 bjh21 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.13     bjh21 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.13 2001/02/13 01:14:45 bjh21 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.1  drochner #include <sys/param.h>
     85   1.1  drochner #include <sys/conf.h>
     86   1.1  drochner #include <sys/ioctl.h>
     87   1.5  augustss #include <sys/fcntl.h>
     88   1.1  drochner #include <sys/kernel.h>
     89   1.1  drochner #include <sys/proc.h>
     90   1.1  drochner #include <sys/syslog.h>
     91   1.1  drochner #include <sys/systm.h>
     92   1.1  drochner #include <sys/tty.h>
     93   1.1  drochner #include <sys/signalvar.h>
     94   1.1  drochner #include <sys/device.h>
     95   1.7  augustss #include <sys/vnode.h>
     96   1.1  drochner 
     97   1.1  drochner #include <dev/wscons/wsconsio.h>
     98   1.1  drochner #include <dev/wscons/wsmousevar.h>
     99   1.1  drochner #include <dev/wscons/wseventvar.h>
    100   1.1  drochner 
    101   1.3  augustss #include "wsmouse.h"
    102   1.8  augustss #include "wsmux.h"
    103   1.8  augustss #include "wsdisplay.h"
    104   1.8  augustss #include "wskbd.h"
    105   1.8  augustss 
    106   1.9  augustss #if NWSMUX > 0
    107   1.8  augustss #include <dev/wscons/wsmuxvar.h>
    108   1.8  augustss #endif
    109   1.3  augustss 
    110  1.11  takemura #define INVALID_X	INT_MAX
    111  1.11  takemura #define INVALID_Y	INT_MAX
    112  1.11  takemura #define INVALID_Z	INT_MAX
    113  1.11  takemura 
    114   1.1  drochner struct wsmouse_softc {
    115   1.1  drochner 	struct device	sc_dv;
    116   1.1  drochner 
    117   1.1  drochner 	const struct wsmouse_accessops *sc_accessops;
    118   1.1  drochner 	void		*sc_accesscookie;
    119   1.1  drochner 
    120   1.1  drochner 	int		sc_ready;	/* accepting events */
    121   1.1  drochner 	struct wseventvar sc_events;	/* event queue state */
    122   1.1  drochner 
    123   1.1  drochner 	u_int		sc_mb;		/* mouse button state */
    124   1.1  drochner 	u_int		sc_ub;		/* user button state */
    125   1.1  drochner 	int		sc_dx;		/* delta-x */
    126   1.1  drochner 	int		sc_dy;		/* delta-y */
    127   1.4  drochner 	int		sc_dz;		/* delta-z */
    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.7  augustss 
    132   1.7  augustss 	int		sc_refcnt;
    133   1.7  augustss 	u_char		sc_dying;	/* device is being detached */
    134   1.7  augustss 
    135   1.9  augustss #if NWSMUX > 0
    136   1.8  augustss 	struct wsmux_softc *sc_mux;
    137   1.8  augustss #endif
    138   1.1  drochner };
    139   1.1  drochner 
    140   1.1  drochner int	wsmouse_match __P((struct device *, struct cfdata *, void *));
    141   1.1  drochner void	wsmouse_attach __P((struct device *, struct device *, void *));
    142   1.7  augustss int	wsmouse_detach __P((struct device *, int));
    143   1.7  augustss int	wsmouse_activate __P((struct device *, enum devact));
    144   1.7  augustss 
    145   1.7  augustss int	wsmouse_do_ioctl __P((struct wsmouse_softc *, u_long, caddr_t,
    146   1.7  augustss 			      int, struct proc *));
    147   1.1  drochner 
    148   1.8  augustss int	wsmousedoclose __P((struct device *, int, int, struct proc *));
    149   1.8  augustss int	wsmousedoioctl __P((struct device *, u_long, caddr_t, int,
    150   1.8  augustss 			    struct proc *));
    151   1.8  augustss 
    152   1.1  drochner struct cfattach wsmouse_ca = {
    153   1.1  drochner 	sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
    154   1.7  augustss 	wsmouse_detach, wsmouse_activate
    155   1.1  drochner };
    156   1.1  drochner 
    157   1.3  augustss #if NWSMOUSE > 0
    158   1.1  drochner extern struct cfdriver wsmouse_cd;
    159   1.3  augustss #endif /* NWSMOUSE > 0 */
    160   1.1  drochner 
    161   1.1  drochner cdev_decl(wsmouse);
    162   1.1  drochner 
    163   1.9  augustss #if NWSMUX > 0
    164   1.8  augustss struct wsmuxops wsmouse_muxops = {
    165   1.8  augustss 	wsmouseopen, wsmousedoclose, wsmousedoioctl, 0, 0
    166   1.8  augustss };
    167   1.9  augustss #endif
    168   1.8  augustss 
    169   1.1  drochner /*
    170   1.1  drochner  * Print function (for parent devices).
    171   1.1  drochner  */
    172   1.1  drochner int
    173   1.1  drochner wsmousedevprint(aux, pnp)
    174   1.1  drochner 	void *aux;
    175   1.1  drochner 	const char *pnp;
    176   1.1  drochner {
    177   1.1  drochner 
    178   1.1  drochner 	if (pnp)
    179   1.1  drochner 		printf("wsmouse at %s", pnp);
    180   1.1  drochner 	return (UNCONF);
    181   1.1  drochner }
    182   1.1  drochner 
    183   1.1  drochner int
    184   1.1  drochner wsmouse_match(parent, match, aux)
    185   1.1  drochner 	struct device *parent;
    186   1.1  drochner 	struct cfdata *match;
    187   1.1  drochner 	void *aux;
    188   1.1  drochner {
    189   1.1  drochner 	return (1);
    190   1.1  drochner }
    191   1.1  drochner 
    192   1.1  drochner void
    193   1.1  drochner wsmouse_attach(parent, self, aux)
    194   1.1  drochner         struct device *parent, *self;
    195   1.1  drochner 	void *aux;
    196   1.1  drochner {
    197   1.1  drochner         struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    198   1.1  drochner 	struct wsmousedev_attach_args *ap = aux;
    199   1.9  augustss #if NWSMUX > 0
    200   1.8  augustss 	int mux;
    201   1.8  augustss #endif
    202   1.1  drochner 
    203   1.1  drochner 	sc->sc_accessops = ap->accessops;
    204   1.1  drochner 	sc->sc_accesscookie = ap->accesscookie;
    205   1.1  drochner 	sc->sc_ready = 0;				/* sanity */
    206   1.7  augustss 
    207   1.9  augustss #if NWSMUX > 0
    208   1.8  augustss 	mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
    209   1.8  augustss 	if (mux != WSMOUSEDEVCF_MUX_DEFAULT) {
    210   1.8  augustss 		wsmux_attach(mux, WSMUX_MOUSE, &sc->sc_dv, &sc->sc_events,
    211   1.8  augustss 			     &sc->sc_mux, &wsmouse_muxops);
    212   1.8  augustss 		printf(" mux %d", mux);
    213   1.8  augustss 	}
    214   1.8  augustss #endif
    215   1.8  augustss 
    216   1.7  augustss 	printf("\n");
    217   1.7  augustss }
    218   1.7  augustss 
    219   1.7  augustss int
    220   1.7  augustss wsmouse_activate(self, act)
    221   1.7  augustss 	struct device *self;
    222   1.7  augustss 	enum devact act;
    223   1.7  augustss {
    224   1.7  augustss 	/* XXX should we do something more? */
    225   1.7  augustss 	return (0);
    226   1.7  augustss }
    227   1.7  augustss 
    228   1.7  augustss /*
    229   1.7  augustss  * Detach a mouse.  To keep track of users of the softc we keep
    230   1.7  augustss  * a reference count that's incremented while inside, e.g., read.
    231   1.7  augustss  * If the mouse is active and the reference count is > 0 (0 is the
    232   1.7  augustss  * normal state) we post an event and then wait for the process
    233   1.7  augustss  * that had the reference to wake us up again.  Then we blow away the
    234   1.7  augustss  * vnode and return (which will deallocate the softc).
    235   1.7  augustss  */
    236   1.7  augustss int
    237   1.7  augustss wsmouse_detach(self, flags)
    238   1.7  augustss 	struct device  *self;
    239   1.7  augustss 	int flags;
    240   1.7  augustss {
    241   1.7  augustss 	struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    242   1.7  augustss 	struct wseventvar *evar;
    243   1.7  augustss 	int maj, mn;
    244   1.7  augustss 	int s;
    245   1.9  augustss #if NWSMUX > 0
    246   1.8  augustss 	int mux;
    247   1.8  augustss #endif
    248   1.7  augustss 
    249   1.7  augustss 	sc->sc_dying = 1;
    250   1.7  augustss 
    251   1.9  augustss #if NWSMUX > 0
    252   1.8  augustss 	mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
    253   1.8  augustss 	if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
    254   1.8  augustss 		wsmux_detach(mux, &sc->sc_dv);
    255   1.8  augustss #endif
    256   1.8  augustss 
    257   1.7  augustss 	evar = &sc->sc_events;
    258   1.7  augustss 	if (evar->io) {
    259   1.7  augustss 		s = spltty();
    260   1.7  augustss 		if (--sc->sc_refcnt >= 0) {
    261   1.7  augustss 			/* Wake everyone by generating a dummy event. */
    262   1.7  augustss 			if (++evar->put >= WSEVENT_QSIZE)
    263   1.7  augustss 				evar->put = 0;
    264   1.7  augustss 			WSEVENT_WAKEUP(evar);
    265   1.7  augustss 			/* Wait for processes to go away. */
    266   1.7  augustss 			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
    267   1.7  augustss 				printf("wsmouse_detach: %s didn't detach\n",
    268   1.7  augustss 				       sc->sc_dv.dv_xname);
    269   1.7  augustss 		}
    270   1.7  augustss 		splx(s);
    271   1.7  augustss 	}
    272   1.7  augustss 
    273   1.7  augustss 	/* locate the major number */
    274   1.7  augustss 	for (maj = 0; maj < nchrdev; maj++)
    275   1.7  augustss 		if (cdevsw[maj].d_open == wsmouseopen)
    276   1.7  augustss 			break;
    277   1.7  augustss 
    278   1.7  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    279   1.7  augustss 	mn = self->dv_unit;
    280   1.7  augustss 	vdevgone(maj, mn, mn, VCHR);
    281   1.7  augustss 
    282   1.7  augustss 	return (0);
    283   1.1  drochner }
    284   1.1  drochner 
    285   1.1  drochner void
    286  1.11  takemura wsmouse_input(wsmousedev, btns, x, y, z, flags)
    287   1.1  drochner 	struct device *wsmousedev;
    288   1.1  drochner 	u_int btns;			/* 0 is up */
    289  1.11  takemura 	int x, y, z;
    290  1.11  takemura 	u_int flags;
    291   1.1  drochner {
    292   1.1  drochner 	struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
    293   1.1  drochner 	struct wscons_event *ev;
    294   1.7  augustss 	struct wseventvar *evar;
    295   1.1  drochner 	int mb, ub, d, get, put, any;
    296   1.1  drochner 
    297   1.1  drochner         /*
    298   1.1  drochner          * Discard input if not ready.
    299   1.1  drochner          */
    300   1.1  drochner 	if (sc->sc_ready == 0)
    301   1.1  drochner 		return;
    302   1.1  drochner 
    303   1.9  augustss #if NWSMUX > 0
    304   1.8  augustss 	if (sc->sc_mux)
    305   1.8  augustss 		evar = &sc->sc_mux->sc_events;
    306   1.8  augustss 	else
    307   1.8  augustss #endif
    308   1.8  augustss 		evar = &sc->sc_events;
    309   1.7  augustss 
    310   1.1  drochner 	sc->sc_mb = btns;
    311  1.11  takemura 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
    312  1.11  takemura 		sc->sc_dx += x;
    313  1.11  takemura 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
    314  1.11  takemura 		sc->sc_dy += y;
    315  1.11  takemura 	if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
    316  1.11  takemura 		sc->sc_dz += z;
    317   1.1  drochner 
    318   1.1  drochner 	/*
    319   1.1  drochner 	 * We have at least one event (mouse button, delta-X, or
    320   1.1  drochner 	 * delta-Y; possibly all three, and possibly three separate
    321   1.1  drochner 	 * button events).  Deliver these events until we are out
    322   1.1  drochner 	 * of changes or out of room.  As events get delivered,
    323   1.1  drochner 	 * mark them `unchanged'.
    324   1.1  drochner 	 */
    325  1.13     bjh21 	ub = sc->sc_ub;
    326   1.1  drochner 	any = 0;
    327   1.7  augustss 	get = evar->get;
    328   1.7  augustss 	put = evar->put;
    329   1.7  augustss 	ev = &evar->q[put];
    330   1.1  drochner 
    331   1.1  drochner 	/* NEXT prepares to put the next event, backing off if necessary */
    332   1.1  drochner #define	NEXT								\
    333   1.1  drochner 	if ((++put) % WSEVENT_QSIZE == get) {				\
    334   1.1  drochner 		put--;							\
    335   1.1  drochner 		goto out;						\
    336   1.1  drochner 	}
    337   1.1  drochner 	/* ADVANCE completes the `put' of the event */
    338   1.1  drochner #define	ADVANCE								\
    339   1.1  drochner 	ev++;								\
    340   1.1  drochner 	if (put >= WSEVENT_QSIZE) {					\
    341   1.1  drochner 		put = 0;						\
    342   1.7  augustss 		ev = &evar->q[0];				\
    343   1.1  drochner 	}								\
    344   1.1  drochner 	any = 1
    345   1.1  drochner 	/* TIMESTAMP sets `time' field of the event to the current time */
    346   1.1  drochner #define TIMESTAMP							\
    347   1.1  drochner 	do {								\
    348   1.1  drochner 		int s;							\
    349   1.1  drochner 		s = splhigh();						\
    350   1.1  drochner 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);			\
    351   1.1  drochner 		splx(s);						\
    352   1.1  drochner 	} while (0)
    353   1.1  drochner 
    354  1.11  takemura 	if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
    355  1.11  takemura 		if (sc->sc_x != x) {
    356  1.11  takemura 			NEXT;
    357  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
    358  1.11  takemura 			ev->value = x;
    359  1.11  takemura 			TIMESTAMP;
    360  1.11  takemura 			ADVANCE;
    361  1.11  takemura 			sc->sc_x = x;
    362  1.11  takemura 		}
    363  1.11  takemura 	} else {
    364  1.11  takemura 		if (sc->sc_dx) {
    365  1.11  takemura 			NEXT;
    366  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
    367  1.11  takemura 			ev->value = sc->sc_dx;
    368  1.11  takemura 			TIMESTAMP;
    369  1.11  takemura 			ADVANCE;
    370  1.11  takemura 			sc->sc_dx = 0;
    371  1.11  takemura 		}
    372   1.1  drochner 	}
    373  1.11  takemura 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
    374  1.11  takemura 		if (sc->sc_y != y) {
    375  1.11  takemura 			NEXT;
    376  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
    377  1.11  takemura 			ev->value = y;
    378  1.11  takemura 			TIMESTAMP;
    379  1.11  takemura 			ADVANCE;
    380  1.11  takemura 			sc->sc_y = y;
    381  1.11  takemura 		}
    382  1.11  takemura 	} else {
    383  1.11  takemura 		if (sc->sc_dy) {
    384  1.11  takemura 			NEXT;
    385  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
    386  1.11  takemura 			ev->value = sc->sc_dy;
    387  1.11  takemura 			TIMESTAMP;
    388  1.11  takemura 			ADVANCE;
    389  1.11  takemura 			sc->sc_dy = 0;
    390  1.11  takemura 		}
    391   1.4  drochner 	}
    392  1.11  takemura 	if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
    393  1.11  takemura 		if (sc->sc_z != z) {
    394  1.11  takemura 			NEXT;
    395  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
    396  1.11  takemura 			ev->value = z;
    397  1.11  takemura 			TIMESTAMP;
    398  1.11  takemura 			ADVANCE;
    399  1.11  takemura 			sc->sc_z = z;
    400  1.11  takemura 		}
    401  1.11  takemura 	} else {
    402  1.11  takemura 		if (sc->sc_dz) {
    403  1.11  takemura 			NEXT;
    404  1.11  takemura 			ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
    405  1.11  takemura 			ev->value = sc->sc_dz;
    406  1.11  takemura 			TIMESTAMP;
    407  1.11  takemura 			ADVANCE;
    408  1.11  takemura 			sc->sc_dz = 0;
    409  1.11  takemura 		}
    410  1.12  takemura 	}
    411  1.12  takemura 
    412  1.12  takemura 	mb = sc->sc_mb;
    413  1.12  takemura 	while ((d = mb ^ ub) != 0) {
    414  1.12  takemura 		/*
    415  1.12  takemura 		 * Mouse button change.  Find the first change and drop
    416  1.12  takemura 		 * it into the event queue.
    417  1.12  takemura 		 */
    418  1.12  takemura 		NEXT;
    419  1.12  takemura 		ev->value = ffs(d) - 1;
    420  1.12  takemura 
    421  1.12  takemura 		KASSERT(ev->value >= 0);
    422  1.12  takemura 
    423  1.12  takemura 		d = 1 << ev->value;
    424  1.12  takemura 		ev->type =
    425  1.12  takemura 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    426  1.12  takemura 		TIMESTAMP;
    427  1.12  takemura 		ADVANCE;
    428  1.12  takemura 		ub ^= d;
    429   1.1  drochner 	}
    430   1.1  drochner out:
    431   1.1  drochner 	if (any) {
    432   1.1  drochner 		sc->sc_ub = ub;
    433   1.7  augustss 		evar->put = put;
    434   1.7  augustss 		WSEVENT_WAKEUP(evar);
    435   1.1  drochner 	}
    436   1.1  drochner }
    437   1.1  drochner 
    438   1.1  drochner int
    439   1.1  drochner wsmouseopen(dev, flags, mode, p)
    440   1.1  drochner 	dev_t dev;
    441   1.1  drochner 	int flags, mode;
    442   1.1  drochner 	struct proc *p;
    443   1.1  drochner {
    444   1.3  augustss #if NWSMOUSE > 0
    445   1.1  drochner 	struct wsmouse_softc *sc;
    446   1.1  drochner 	int error, unit;
    447   1.1  drochner 
    448   1.1  drochner 	unit = minor(dev);
    449   1.1  drochner 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    450   1.1  drochner 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    451   1.1  drochner 		return (ENXIO);
    452   1.1  drochner 
    453   1.7  augustss 	if (sc->sc_dying)
    454   1.7  augustss 		return (EIO);
    455   1.7  augustss 
    456   1.5  augustss 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    457   1.5  augustss 		return (0);			/* always allow open for write
    458   1.5  augustss 						   so ioctl() is possible. */
    459   1.5  augustss 
    460   1.9  augustss #if NWSMUX > 0
    461   1.8  augustss 	if (sc->sc_mux)
    462   1.8  augustss 		return (EBUSY);
    463   1.8  augustss #endif
    464   1.8  augustss 
    465   1.1  drochner 	if (sc->sc_events.io)			/* and that it's not in use */
    466   1.1  drochner 		return (EBUSY);
    467   1.1  drochner 
    468   1.1  drochner 	sc->sc_events.io = p;
    469   1.1  drochner 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    470   1.1  drochner 
    471   1.1  drochner 	sc->sc_ready = 1;			/* start accepting events */
    472  1.11  takemura 	sc->sc_x = INVALID_X;
    473  1.11  takemura 	sc->sc_y = INVALID_Y;
    474  1.11  takemura 	sc->sc_z = INVALID_Z;
    475   1.1  drochner 
    476   1.1  drochner 	/* enable the device, and punt if that's not possible */
    477   1.1  drochner 	error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    478   1.1  drochner 	if (error) {
    479   1.1  drochner 		sc->sc_ready = 0;		/* stop accepting events */
    480   1.1  drochner 		wsevent_fini(&sc->sc_events);
    481   1.1  drochner 		sc->sc_events.io = NULL;
    482   1.1  drochner 		return (error);
    483   1.1  drochner 	}
    484   1.1  drochner 
    485   1.1  drochner 	return (0);
    486   1.3  augustss #else
    487   1.3  augustss 	return (ENXIO);
    488   1.3  augustss #endif /* NWSMOUSE > 0 */
    489   1.1  drochner }
    490   1.1  drochner 
    491   1.1  drochner int
    492   1.1  drochner wsmouseclose(dev, flags, mode, p)
    493   1.1  drochner 	dev_t dev;
    494   1.1  drochner 	int flags, mode;
    495   1.1  drochner 	struct proc *p;
    496   1.1  drochner {
    497   1.3  augustss #if NWSMOUSE > 0
    498   1.8  augustss 	return (wsmousedoclose(wsmouse_cd.cd_devs[minor(dev)],
    499   1.8  augustss 			       flags, mode, p));
    500   1.8  augustss #else
    501   1.8  augustss 	return (ENXIO);
    502   1.8  augustss #endif /* NWSMOUSE > 0 */
    503   1.8  augustss }
    504   1.8  augustss 
    505   1.8  augustss #if NWSMOUSE > 0
    506   1.8  augustss int
    507   1.8  augustss wsmousedoclose(dv, flags, mode, p)
    508   1.8  augustss 	struct device *dv;
    509   1.8  augustss 	int flags, mode;
    510   1.8  augustss 	struct proc *p;
    511   1.8  augustss {
    512   1.8  augustss 	struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
    513   1.5  augustss 
    514   1.5  augustss 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    515   1.5  augustss 		return (0);			/* see wsmouseopen() */
    516   1.1  drochner 
    517   1.1  drochner 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    518   1.1  drochner 
    519   1.1  drochner 	sc->sc_ready = 0;			/* stop accepting events */
    520   1.1  drochner 	wsevent_fini(&sc->sc_events);
    521   1.1  drochner 	sc->sc_events.io = NULL;
    522   1.1  drochner 	return (0);
    523   1.8  augustss }
    524   1.3  augustss #endif /* NWSMOUSE > 0 */
    525   1.1  drochner 
    526   1.1  drochner int
    527   1.1  drochner wsmouseread(dev, uio, flags)
    528   1.1  drochner 	dev_t dev;
    529   1.1  drochner 	struct uio *uio;
    530   1.1  drochner 	int flags;
    531   1.1  drochner {
    532   1.3  augustss #if NWSMOUSE > 0
    533   1.6  augustss 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    534   1.7  augustss 	int error;
    535   1.1  drochner 
    536   1.7  augustss 	if (sc->sc_dying)
    537   1.7  augustss 		return (EIO);
    538   1.7  augustss 
    539   1.7  augustss 	sc->sc_refcnt++;
    540   1.7  augustss 	error = wsevent_read(&sc->sc_events, uio, flags);
    541   1.7  augustss 	if (--sc->sc_refcnt < 0) {
    542   1.7  augustss 		wakeup(sc);
    543   1.7  augustss 		error = EIO;
    544   1.7  augustss 	}
    545   1.7  augustss 	return (error);
    546   1.3  augustss #else
    547   1.3  augustss 	return (ENXIO);
    548   1.3  augustss #endif /* NWSMOUSE > 0 */
    549   1.1  drochner }
    550   1.1  drochner 
    551   1.1  drochner int
    552   1.1  drochner wsmouseioctl(dev, cmd, data, flag, p)
    553   1.1  drochner 	dev_t dev;
    554   1.1  drochner 	u_long cmd;
    555   1.1  drochner 	caddr_t data;
    556   1.1  drochner 	int flag;
    557   1.1  drochner 	struct proc *p;
    558   1.1  drochner {
    559   1.3  augustss #if NWSMOUSE > 0
    560   1.8  augustss 	return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
    561   1.8  augustss 			       cmd, data, flag, p));
    562   1.8  augustss #else
    563   1.8  augustss 	return (ENXIO);
    564   1.8  augustss #endif /* NWSMOUSE > 0 */
    565   1.8  augustss }
    566   1.8  augustss 
    567   1.8  augustss #if NWSMOUSE > 0
    568   1.8  augustss /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    569   1.8  augustss int
    570   1.8  augustss wsmousedoioctl(dv, cmd, data, flag, p)
    571   1.8  augustss 	struct device *dv;
    572   1.8  augustss 	u_long cmd;
    573   1.8  augustss 	caddr_t data;
    574   1.8  augustss 	int flag;
    575   1.8  augustss 	struct proc *p;
    576   1.8  augustss {
    577   1.8  augustss 	struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
    578   1.6  augustss 	int error;
    579   1.1  drochner 
    580   1.7  augustss 	sc->sc_refcnt++;
    581   1.7  augustss 	error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
    582   1.7  augustss 	if (--sc->sc_refcnt < 0)
    583   1.7  augustss 		wakeup(sc);
    584   1.7  augustss 	return (error);
    585   1.7  augustss }
    586   1.7  augustss 
    587   1.7  augustss int
    588   1.7  augustss wsmouse_do_ioctl(sc, cmd, data, flag, p)
    589   1.7  augustss 	struct wsmouse_softc *sc;
    590   1.7  augustss 	u_long cmd;
    591   1.7  augustss 	caddr_t data;
    592   1.7  augustss 	int flag;
    593   1.7  augustss 	struct proc *p;
    594   1.7  augustss {
    595   1.7  augustss 	int error;
    596   1.7  augustss 
    597   1.7  augustss 	if (sc->sc_dying)
    598   1.7  augustss 		return (EIO);
    599   1.7  augustss 
    600   1.1  drochner 	/*
    601   1.1  drochner 	 * Try the generic ioctls that the wsmouse interface supports.
    602   1.1  drochner 	 */
    603   1.1  drochner 	switch (cmd) {
    604   1.1  drochner 	case FIONBIO:		/* we will remove this someday (soon???) */
    605   1.1  drochner 		return (0);
    606   1.1  drochner 
    607   1.1  drochner 	case FIOASYNC:
    608   1.1  drochner 		sc->sc_events.async = *(int *)data != 0;
    609   1.1  drochner 		return (0);
    610   1.1  drochner 
    611   1.1  drochner 	case TIOCSPGRP:
    612   1.1  drochner 		if (*(int *)data != sc->sc_events.io->p_pgid)
    613   1.1  drochner 			return (EPERM);
    614   1.1  drochner 		return (0);
    615   1.1  drochner 	}
    616   1.1  drochner 
    617   1.1  drochner 	/*
    618   1.1  drochner 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    619   1.1  drochner 	 * if it didn't recognize the request.
    620   1.1  drochner 	 */
    621   1.1  drochner 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    622   1.1  drochner 	    data, flag, p);
    623   1.1  drochner 	return (error != -1 ? error : ENOTTY);
    624   1.7  augustss }
    625   1.3  augustss #endif /* NWSMOUSE > 0 */
    626   1.1  drochner 
    627   1.1  drochner int
    628   1.1  drochner wsmousepoll(dev, events, p)
    629   1.1  drochner 	dev_t dev;
    630   1.1  drochner 	int events;
    631   1.1  drochner 	struct proc *p;
    632   1.1  drochner {
    633   1.3  augustss #if NWSMOUSE > 0
    634   1.1  drochner 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    635   1.1  drochner 
    636   1.1  drochner 	return (wsevent_poll(&sc->sc_events, events, p));
    637   1.3  augustss #else
    638   1.3  augustss 	return (0);
    639   1.3  augustss #endif /* NWSMOUSE > 0 */
    640   1.1  drochner }
    641   1.7  augustss 
    642   1.9  augustss #if NWSMUX > 0
    643   1.8  augustss int
    644   1.8  augustss wsmouse_add_mux(unit, muxsc)
    645   1.8  augustss 	int unit;
    646   1.8  augustss 	struct wsmux_softc *muxsc;
    647   1.8  augustss {
    648   1.8  augustss 	struct wsmouse_softc *sc;
    649   1.8  augustss 
    650   1.8  augustss 	if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
    651   1.8  augustss 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    652   1.8  augustss 		return (ENXIO);
    653   1.8  augustss 
    654   1.8  augustss 	if (sc->sc_mux || sc->sc_events.io)
    655   1.8  augustss 		return (EBUSY);
    656   1.8  augustss 
    657   1.8  augustss 	return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
    658   1.8  augustss 				&sc->sc_mux, &wsmouse_muxops));
    659   1.8  augustss }
    660   1.8  augustss 
    661   1.8  augustss int
    662   1.8  augustss wsmouse_rem_mux(unit, muxsc)
    663   1.8  augustss 	int unit;
    664   1.8  augustss 	struct wsmux_softc *muxsc;
    665   1.8  augustss {
    666   1.8  augustss 	struct wsmouse_softc *sc;
    667   1.8  augustss 
    668   1.8  augustss 	if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
    669   1.8  augustss 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    670   1.8  augustss 		return (ENXIO);
    671   1.8  augustss 
    672   1.8  augustss 	return (wsmux_detach_sc(muxsc, &sc->sc_dv));
    673   1.8  augustss }
    674   1.8  augustss 
    675   1.8  augustss #endif
    676