Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.3
      1  1.3  augustss /* $NetBSD: wsmouse.c,v 1.3 1998/07/25 20:02:21 augustss 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 static const char _copyright[] __attribute__ ((unused)) =
     34  1.1  drochner     "Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.";
     35  1.1  drochner static const char _rcsid[] __attribute__ ((unused)) =
     36  1.3  augustss     "$NetBSD: wsmouse.c,v 1.3 1998/07/25 20:02:21 augustss Exp $";
     37  1.1  drochner 
     38  1.1  drochner /*
     39  1.1  drochner  * Copyright (c) 1992, 1993
     40  1.1  drochner  *	The Regents of the University of California.  All rights reserved.
     41  1.1  drochner  *
     42  1.1  drochner  * This software was developed by the Computer Systems Engineering group
     43  1.1  drochner  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     44  1.1  drochner  * contributed to Berkeley.
     45  1.1  drochner  *
     46  1.1  drochner  * All advertising materials mentioning features or use of this software
     47  1.1  drochner  * must display the following acknowledgement:
     48  1.1  drochner  *	This product includes software developed by the University of
     49  1.1  drochner  *	California, Lawrence Berkeley Laboratory.
     50  1.1  drochner  *
     51  1.1  drochner  * Redistribution and use in source and binary forms, with or without
     52  1.1  drochner  * modification, are permitted provided that the following conditions
     53  1.1  drochner  * are met:
     54  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     55  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     56  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     57  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     58  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     59  1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     60  1.1  drochner  *    must display the following acknowledgement:
     61  1.1  drochner  *	This product includes software developed by the University of
     62  1.1  drochner  *	California, Berkeley and its contributors.
     63  1.1  drochner  * 4. Neither the name of the University nor the names of its contributors
     64  1.1  drochner  *    may be used to endorse or promote products derived from this software
     65  1.1  drochner  *    without specific prior written permission.
     66  1.1  drochner  *
     67  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  1.1  drochner  * SUCH DAMAGE.
     78  1.1  drochner  *
     79  1.1  drochner  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
     80  1.1  drochner  */
     81  1.1  drochner 
     82  1.1  drochner /*
     83  1.1  drochner  * Mouse driver.
     84  1.1  drochner  */
     85  1.1  drochner 
     86  1.1  drochner #include <sys/param.h>
     87  1.1  drochner #include <sys/conf.h>
     88  1.1  drochner #include <sys/ioctl.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.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.3  augustss 
    103  1.1  drochner struct wsmouse_softc {
    104  1.1  drochner 	struct device	sc_dv;
    105  1.1  drochner 
    106  1.1  drochner 	const struct wsmouse_accessops *sc_accessops;
    107  1.1  drochner 	void		*sc_accesscookie;
    108  1.1  drochner 
    109  1.1  drochner 	int		sc_ready;	/* accepting events */
    110  1.1  drochner 	struct wseventvar sc_events;	/* event queue state */
    111  1.1  drochner 
    112  1.1  drochner 	u_int		sc_mb;		/* mouse button state */
    113  1.1  drochner 	u_int		sc_ub;		/* user button state */
    114  1.1  drochner 	int		sc_dx;		/* delta-x */
    115  1.1  drochner 	int		sc_dy;		/* delta-y */
    116  1.1  drochner };
    117  1.1  drochner 
    118  1.1  drochner int	wsmouse_match __P((struct device *, struct cfdata *, void *));
    119  1.1  drochner void	wsmouse_attach __P((struct device *, struct device *, void *));
    120  1.1  drochner 
    121  1.1  drochner struct cfattach wsmouse_ca = {
    122  1.1  drochner 	sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
    123  1.1  drochner };
    124  1.1  drochner 
    125  1.3  augustss #if NWSMOUSE > 0
    126  1.1  drochner extern struct cfdriver wsmouse_cd;
    127  1.3  augustss #endif /* NWSMOUSE > 0 */
    128  1.1  drochner 
    129  1.1  drochner cdev_decl(wsmouse);
    130  1.1  drochner 
    131  1.1  drochner /*
    132  1.1  drochner  * Print function (for parent devices).
    133  1.1  drochner  */
    134  1.1  drochner int
    135  1.1  drochner wsmousedevprint(aux, pnp)
    136  1.1  drochner 	void *aux;
    137  1.1  drochner 	const char *pnp;
    138  1.1  drochner {
    139  1.1  drochner 
    140  1.1  drochner 	if (pnp)
    141  1.1  drochner 		printf("wsmouse at %s", pnp);
    142  1.1  drochner 	return (UNCONF);
    143  1.1  drochner }
    144  1.1  drochner 
    145  1.1  drochner int
    146  1.1  drochner wsmouse_match(parent, match, aux)
    147  1.1  drochner 	struct device *parent;
    148  1.1  drochner 	struct cfdata *match;
    149  1.1  drochner 	void *aux;
    150  1.1  drochner {
    151  1.1  drochner 
    152  1.1  drochner 	return (1);
    153  1.1  drochner }
    154  1.1  drochner 
    155  1.1  drochner void
    156  1.1  drochner wsmouse_attach(parent, self, aux)
    157  1.1  drochner         struct device *parent, *self;
    158  1.1  drochner 	void *aux;
    159  1.1  drochner {
    160  1.1  drochner         struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    161  1.1  drochner 	struct wsmousedev_attach_args *ap = aux;
    162  1.1  drochner 
    163  1.1  drochner 	printf("\n");
    164  1.1  drochner 
    165  1.1  drochner 	sc->sc_accessops = ap->accessops;
    166  1.1  drochner 	sc->sc_accesscookie = ap->accesscookie;
    167  1.1  drochner 	sc->sc_ready = 0;				/* sanity */
    168  1.1  drochner }
    169  1.1  drochner 
    170  1.1  drochner void
    171  1.1  drochner wsmouse_input(wsmousedev, btns, dx, dy)
    172  1.1  drochner 	struct device *wsmousedev;
    173  1.1  drochner 	u_int btns;			/* 0 is up */
    174  1.1  drochner 	int dx, dy;
    175  1.1  drochner {
    176  1.1  drochner 	struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
    177  1.1  drochner 	struct wscons_event *ev;
    178  1.1  drochner 	int mb, ub, d, get, put, any;
    179  1.1  drochner 
    180  1.1  drochner         /*
    181  1.1  drochner          * Discard input if not ready.
    182  1.1  drochner          */
    183  1.1  drochner 	if (sc->sc_ready == 0)
    184  1.1  drochner 		return;
    185  1.1  drochner 
    186  1.1  drochner 	sc->sc_mb = btns;
    187  1.1  drochner 	sc->sc_dx += dx;
    188  1.1  drochner 	sc->sc_dy += dy;
    189  1.1  drochner 
    190  1.1  drochner 	/*
    191  1.1  drochner 	 * We have at least one event (mouse button, delta-X, or
    192  1.1  drochner 	 * delta-Y; possibly all three, and possibly three separate
    193  1.1  drochner 	 * button events).  Deliver these events until we are out
    194  1.1  drochner 	 * of changes or out of room.  As events get delivered,
    195  1.1  drochner 	 * mark them `unchanged'.
    196  1.1  drochner 	 */
    197  1.1  drochner 	any = 0;
    198  1.1  drochner 	get = sc->sc_events.get;
    199  1.1  drochner 	put = sc->sc_events.put;
    200  1.1  drochner 	ev = &sc->sc_events.q[put];
    201  1.1  drochner 
    202  1.1  drochner 	/* NEXT prepares to put the next event, backing off if necessary */
    203  1.1  drochner #define	NEXT								\
    204  1.1  drochner 	if ((++put) % WSEVENT_QSIZE == get) {				\
    205  1.1  drochner 		put--;							\
    206  1.1  drochner 		goto out;						\
    207  1.1  drochner 	}
    208  1.1  drochner 	/* ADVANCE completes the `put' of the event */
    209  1.1  drochner #define	ADVANCE								\
    210  1.1  drochner 	ev++;								\
    211  1.1  drochner 	if (put >= WSEVENT_QSIZE) {					\
    212  1.1  drochner 		put = 0;						\
    213  1.1  drochner 		ev = &sc->sc_events.q[0];				\
    214  1.1  drochner 	}								\
    215  1.1  drochner 	any = 1
    216  1.1  drochner 	/* TIMESTAMP sets `time' field of the event to the current time */
    217  1.1  drochner #define TIMESTAMP							\
    218  1.1  drochner 	do {								\
    219  1.1  drochner 		int s;							\
    220  1.1  drochner 		s = splhigh();						\
    221  1.1  drochner 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);			\
    222  1.1  drochner 		splx(s);						\
    223  1.1  drochner 	} while (0)
    224  1.1  drochner 
    225  1.1  drochner 	mb = sc->sc_mb;
    226  1.1  drochner 	ub = sc->sc_ub;
    227  1.1  drochner 	while ((d = mb ^ ub) != 0) {
    228  1.1  drochner 		/*
    229  1.1  drochner 		 * Mouse button change.  Find the first change and drop
    230  1.1  drochner 		 * it into the event queue.
    231  1.1  drochner 		 */
    232  1.1  drochner 		NEXT;
    233  1.1  drochner 		ev->value = ffs(d) - 1;
    234  1.1  drochner 
    235  1.1  drochner 		KASSERT(ev->value >= 0);
    236  1.1  drochner 
    237  1.1  drochner 		d = 1 << ev->value;
    238  1.1  drochner 		ev->type =
    239  1.1  drochner 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    240  1.1  drochner 		TIMESTAMP;
    241  1.1  drochner 		ADVANCE;
    242  1.1  drochner 		ub ^= d;
    243  1.1  drochner 	}
    244  1.1  drochner 	if (sc->sc_dx) {
    245  1.1  drochner 		NEXT;
    246  1.1  drochner 		ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
    247  1.1  drochner 		ev->value = sc->sc_dx;
    248  1.1  drochner 		TIMESTAMP;
    249  1.1  drochner 		ADVANCE;
    250  1.1  drochner 		sc->sc_dx = 0;
    251  1.1  drochner 	}
    252  1.1  drochner 	if (sc->sc_dy) {
    253  1.1  drochner 		NEXT;
    254  1.1  drochner 		ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
    255  1.1  drochner 		ev->value = sc->sc_dy;
    256  1.1  drochner 		TIMESTAMP;
    257  1.1  drochner 		ADVANCE;
    258  1.1  drochner 		sc->sc_dy = 0;
    259  1.1  drochner 	}
    260  1.1  drochner out:
    261  1.1  drochner 	if (any) {
    262  1.1  drochner 		sc->sc_ub = ub;
    263  1.1  drochner 		sc->sc_events.put = put;
    264  1.1  drochner 		WSEVENT_WAKEUP(&sc->sc_events);
    265  1.1  drochner 	}
    266  1.1  drochner }
    267  1.1  drochner 
    268  1.1  drochner int
    269  1.1  drochner wsmouseopen(dev, flags, mode, p)
    270  1.1  drochner 	dev_t dev;
    271  1.1  drochner 	int flags, mode;
    272  1.1  drochner 	struct proc *p;
    273  1.1  drochner {
    274  1.3  augustss #if NWSMOUSE > 0
    275  1.1  drochner 	struct wsmouse_softc *sc;
    276  1.1  drochner 	int error, unit;
    277  1.1  drochner 
    278  1.1  drochner 	unit = minor(dev);
    279  1.1  drochner 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    280  1.1  drochner 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    281  1.1  drochner 		return (ENXIO);
    282  1.1  drochner 
    283  1.1  drochner 	if (sc->sc_events.io)			/* and that it's not in use */
    284  1.1  drochner 		return (EBUSY);
    285  1.1  drochner 
    286  1.1  drochner 	sc->sc_events.io = p;
    287  1.1  drochner 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    288  1.1  drochner 
    289  1.1  drochner 	sc->sc_ready = 1;			/* start accepting events */
    290  1.1  drochner 
    291  1.1  drochner 	/* enable the device, and punt if that's not possible */
    292  1.1  drochner 	error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    293  1.1  drochner 	if (error) {
    294  1.1  drochner 		sc->sc_ready = 0;		/* stop accepting events */
    295  1.1  drochner 		wsevent_fini(&sc->sc_events);
    296  1.1  drochner 		sc->sc_events.io = NULL;
    297  1.1  drochner 		return (error);
    298  1.1  drochner 	}
    299  1.1  drochner 
    300  1.1  drochner 	return (0);
    301  1.3  augustss #else
    302  1.3  augustss 	return (ENXIO);
    303  1.3  augustss #endif /* NWSMOUSE > 0 */
    304  1.1  drochner }
    305  1.1  drochner 
    306  1.1  drochner int
    307  1.1  drochner wsmouseclose(dev, flags, mode, p)
    308  1.1  drochner 	dev_t dev;
    309  1.1  drochner 	int flags, mode;
    310  1.1  drochner 	struct proc *p;
    311  1.1  drochner {
    312  1.3  augustss #if NWSMOUSE > 0
    313  1.1  drochner 	struct wsmouse_softc *sc;
    314  1.1  drochner 	int unit;
    315  1.1  drochner 
    316  1.1  drochner 	unit = minor(dev);
    317  1.1  drochner 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    318  1.1  drochner 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    319  1.1  drochner 		return (ENXIO);
    320  1.1  drochner 
    321  1.1  drochner 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    322  1.1  drochner 
    323  1.1  drochner 	sc->sc_ready = 0;			/* stop accepting events */
    324  1.1  drochner 	wsevent_fini(&sc->sc_events);
    325  1.1  drochner 	sc->sc_events.io = NULL;
    326  1.1  drochner 	return (0);
    327  1.3  augustss #else
    328  1.3  augustss 	return (ENXIO);
    329  1.3  augustss #endif /* NWSMOUSE > 0 */
    330  1.1  drochner }
    331  1.1  drochner 
    332  1.1  drochner int
    333  1.1  drochner wsmouseread(dev, uio, flags)
    334  1.1  drochner 	dev_t dev;
    335  1.1  drochner 	struct uio *uio;
    336  1.1  drochner 	int flags;
    337  1.1  drochner {
    338  1.3  augustss #if NWSMOUSE > 0
    339  1.1  drochner 	struct wsmouse_softc *sc;
    340  1.1  drochner 	int unit;
    341  1.1  drochner 
    342  1.1  drochner 	unit = minor(dev);
    343  1.1  drochner 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    344  1.1  drochner 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    345  1.1  drochner 		return (ENXIO);
    346  1.1  drochner 
    347  1.1  drochner 	return (wsevent_read(&sc->sc_events, uio, flags));
    348  1.3  augustss #else
    349  1.3  augustss 	return (ENXIO);
    350  1.3  augustss #endif /* NWSMOUSE > 0 */
    351  1.1  drochner }
    352  1.1  drochner 
    353  1.1  drochner int
    354  1.1  drochner wsmouseioctl(dev, cmd, data, flag, p)
    355  1.1  drochner 	dev_t dev;
    356  1.1  drochner 	u_long cmd;
    357  1.1  drochner 	caddr_t data;
    358  1.1  drochner 	int flag;
    359  1.1  drochner 	struct proc *p;
    360  1.1  drochner {
    361  1.3  augustss #if NWSMOUSE > 0
    362  1.1  drochner 	struct wsmouse_softc *sc;
    363  1.1  drochner 	int unit, error;
    364  1.1  drochner 
    365  1.1  drochner 	unit = minor(dev);
    366  1.1  drochner 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    367  1.1  drochner 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    368  1.1  drochner 		return (ENXIO);
    369  1.1  drochner 
    370  1.1  drochner 	/*
    371  1.1  drochner 	 * Try the generic ioctls that the wsmouse interface supports.
    372  1.1  drochner 	 */
    373  1.1  drochner 	switch (cmd) {
    374  1.1  drochner 	case FIONBIO:		/* we will remove this someday (soon???) */
    375  1.1  drochner 		return (0);
    376  1.1  drochner 
    377  1.1  drochner 	case FIOASYNC:
    378  1.1  drochner 		sc->sc_events.async = *(int *)data != 0;
    379  1.1  drochner 		return (0);
    380  1.1  drochner 
    381  1.1  drochner 	case TIOCSPGRP:
    382  1.1  drochner 		if (*(int *)data != sc->sc_events.io->p_pgid)
    383  1.1  drochner 			return (EPERM);
    384  1.1  drochner 		return (0);
    385  1.1  drochner 	}
    386  1.1  drochner 
    387  1.1  drochner 	/*
    388  1.1  drochner 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    389  1.1  drochner 	 * if it didn't recognize the request.
    390  1.1  drochner 	 */
    391  1.1  drochner 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    392  1.1  drochner 	    data, flag, p);
    393  1.1  drochner 	return (error != -1 ? error : ENOTTY);
    394  1.3  augustss #else
    395  1.3  augustss 	return (ENXIO);
    396  1.3  augustss #endif /* NWSMOUSE > 0 */
    397  1.1  drochner }
    398  1.1  drochner 
    399  1.1  drochner int
    400  1.1  drochner wsmousepoll(dev, events, p)
    401  1.1  drochner 	dev_t dev;
    402  1.1  drochner 	int events;
    403  1.1  drochner 	struct proc *p;
    404  1.1  drochner {
    405  1.3  augustss #if NWSMOUSE > 0
    406  1.1  drochner 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    407  1.1  drochner 
    408  1.1  drochner 	return (wsevent_poll(&sc->sc_events, events, p));
    409  1.3  augustss #else
    410  1.3  augustss 	return (0);
    411  1.3  augustss #endif /* NWSMOUSE > 0 */
    412  1.1  drochner }
    413