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