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