Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.7
      1 /* $NetBSD: wsmouse.c,v 1.7 1999/06/30 06:21:21 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.7 1999/06/30 06:21:21 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 #include <sys/vnode.h>
     98 
     99 #include <dev/wscons/wsconsio.h>
    100 #include <dev/wscons/wsmousevar.h>
    101 #include <dev/wscons/wseventvar.h>
    102 
    103 #include "wsmouse.h"
    104 
    105 struct wsmouse_softc {
    106 	struct device	sc_dv;
    107 
    108 	const struct wsmouse_accessops *sc_accessops;
    109 	void		*sc_accesscookie;
    110 
    111 	int		sc_ready;	/* accepting events */
    112 	struct wseventvar sc_events;	/* event queue state */
    113 
    114 	u_int		sc_mb;		/* mouse button state */
    115 	u_int		sc_ub;		/* user button state */
    116 	int		sc_dx;		/* delta-x */
    117 	int		sc_dy;		/* delta-y */
    118 	int		sc_dz;		/* delta-z */
    119 
    120 	int		sc_refcnt;
    121 	u_char		sc_dying;	/* device is being detached */
    122 
    123 };
    124 
    125 int	wsmouse_match __P((struct device *, struct cfdata *, void *));
    126 void	wsmouse_attach __P((struct device *, struct device *, void *));
    127 int	wsmouse_detach __P((struct device *, int));
    128 int	wsmouse_activate __P((struct device *, enum devact));
    129 
    130 int	wsmouse_do_ioctl __P((struct wsmouse_softc *, u_long, caddr_t,
    131 			      int, struct proc *));
    132 
    133 struct cfattach wsmouse_ca = {
    134 	sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
    135 	wsmouse_detach, wsmouse_activate
    136 };
    137 
    138 #if NWSMOUSE > 0
    139 extern struct cfdriver wsmouse_cd;
    140 #endif /* NWSMOUSE > 0 */
    141 
    142 cdev_decl(wsmouse);
    143 
    144 /*
    145  * Print function (for parent devices).
    146  */
    147 int
    148 wsmousedevprint(aux, pnp)
    149 	void *aux;
    150 	const char *pnp;
    151 {
    152 
    153 	if (pnp)
    154 		printf("wsmouse at %s", pnp);
    155 	return (UNCONF);
    156 }
    157 
    158 int
    159 wsmouse_match(parent, match, aux)
    160 	struct device *parent;
    161 	struct cfdata *match;
    162 	void *aux;
    163 {
    164 	return (1);
    165 }
    166 
    167 void
    168 wsmouse_attach(parent, self, aux)
    169         struct device *parent, *self;
    170 	void *aux;
    171 {
    172         struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    173 	struct wsmousedev_attach_args *ap = aux;
    174 
    175 	sc->sc_accessops = ap->accessops;
    176 	sc->sc_accesscookie = ap->accesscookie;
    177 	sc->sc_ready = 0;				/* sanity */
    178 
    179 	printf("\n");
    180 }
    181 
    182 int
    183 wsmouse_activate(self, act)
    184 	struct device *self;
    185 	enum devact act;
    186 {
    187 	/* XXX should we do something more? */
    188 	return (0);
    189 }
    190 
    191 /*
    192  * Detach a mouse.  To keep track of users of the softc we keep
    193  * a reference count that's incremented while inside, e.g., read.
    194  * If the mouse is active and the reference count is > 0 (0 is the
    195  * normal state) we post an event and then wait for the process
    196  * that had the reference to wake us up again.  Then we blow away the
    197  * vnode and return (which will deallocate the softc).
    198  */
    199 int
    200 wsmouse_detach(self, flags)
    201 	struct device  *self;
    202 	int flags;
    203 {
    204 	struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    205 	struct wseventvar *evar;
    206 	int maj, mn;
    207 	int s;
    208 
    209 	sc->sc_dying = 1;
    210 
    211 	evar = &sc->sc_events;
    212 	if (evar->io) {
    213 		s = spltty();
    214 		if (--sc->sc_refcnt >= 0) {
    215 			/* Wake everyone by generating a dummy event. */
    216 			if (++evar->put >= WSEVENT_QSIZE)
    217 				evar->put = 0;
    218 			WSEVENT_WAKEUP(evar);
    219 			/* Wait for processes to go away. */
    220 			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
    221 				printf("wsmouse_detach: %s didn't detach\n",
    222 				       sc->sc_dv.dv_xname);
    223 		}
    224 		splx(s);
    225 	}
    226 
    227 	/* locate the major number */
    228 	for (maj = 0; maj < nchrdev; maj++)
    229 		if (cdevsw[maj].d_open == wsmouseopen)
    230 			break;
    231 
    232 	/* Nuke the vnodes for any open instances (calls close). */
    233 	mn = self->dv_unit;
    234 	vdevgone(maj, mn, mn, VCHR);
    235 
    236 	return (0);
    237 }
    238 
    239 void
    240 wsmouse_input(wsmousedev, btns, dx, dy, dz)
    241 	struct device *wsmousedev;
    242 	u_int btns;			/* 0 is up */
    243 	int dx, dy, dz;
    244 {
    245 	struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
    246 	struct wscons_event *ev;
    247 	struct wseventvar *evar;
    248 	int mb, ub, d, get, put, any;
    249 
    250         /*
    251          * Discard input if not ready.
    252          */
    253 	if (sc->sc_ready == 0)
    254 		return;
    255 
    256 	evar = &sc->sc_events;
    257 
    258 	sc->sc_mb = btns;
    259 	sc->sc_dx += dx;
    260 	sc->sc_dy += dy;
    261 	sc->sc_dz += dz;
    262 
    263 	/*
    264 	 * We have at least one event (mouse button, delta-X, or
    265 	 * delta-Y; possibly all three, and possibly three separate
    266 	 * button events).  Deliver these events until we are out
    267 	 * of changes or out of room.  As events get delivered,
    268 	 * mark them `unchanged'.
    269 	 */
    270 	any = 0;
    271 	get = evar->get;
    272 	put = evar->put;
    273 	ev = &evar->q[put];
    274 
    275 	/* NEXT prepares to put the next event, backing off if necessary */
    276 #define	NEXT								\
    277 	if ((++put) % WSEVENT_QSIZE == get) {				\
    278 		put--;							\
    279 		goto out;						\
    280 	}
    281 	/* ADVANCE completes the `put' of the event */
    282 #define	ADVANCE								\
    283 	ev++;								\
    284 	if (put >= WSEVENT_QSIZE) {					\
    285 		put = 0;						\
    286 		ev = &evar->q[0];				\
    287 	}								\
    288 	any = 1
    289 	/* TIMESTAMP sets `time' field of the event to the current time */
    290 #define TIMESTAMP							\
    291 	do {								\
    292 		int s;							\
    293 		s = splhigh();						\
    294 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);			\
    295 		splx(s);						\
    296 	} while (0)
    297 
    298 	mb = sc->sc_mb;
    299 	ub = sc->sc_ub;
    300 	while ((d = mb ^ ub) != 0) {
    301 		/*
    302 		 * Mouse button change.  Find the first change and drop
    303 		 * it into the event queue.
    304 		 */
    305 		NEXT;
    306 		ev->value = ffs(d) - 1;
    307 
    308 		KASSERT(ev->value >= 0);
    309 
    310 		d = 1 << ev->value;
    311 		ev->type =
    312 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    313 		TIMESTAMP;
    314 		ADVANCE;
    315 		ub ^= d;
    316 	}
    317 	if (sc->sc_dx) {
    318 		NEXT;
    319 		ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
    320 		ev->value = sc->sc_dx;
    321 		TIMESTAMP;
    322 		ADVANCE;
    323 		sc->sc_dx = 0;
    324 	}
    325 	if (sc->sc_dy) {
    326 		NEXT;
    327 		ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
    328 		ev->value = sc->sc_dy;
    329 		TIMESTAMP;
    330 		ADVANCE;
    331 		sc->sc_dy = 0;
    332 	}
    333 	if (sc->sc_dz) {
    334 		NEXT;
    335 		ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
    336 		ev->value = sc->sc_dz;
    337 		TIMESTAMP;
    338 		ADVANCE;
    339 		sc->sc_dz = 0;
    340 	}
    341 out:
    342 	if (any) {
    343 		sc->sc_ub = ub;
    344 		evar->put = put;
    345 		WSEVENT_WAKEUP(evar);
    346 	}
    347 }
    348 
    349 int
    350 wsmouseopen(dev, flags, mode, p)
    351 	dev_t dev;
    352 	int flags, mode;
    353 	struct proc *p;
    354 {
    355 #if NWSMOUSE > 0
    356 	struct wsmouse_softc *sc;
    357 	int error, unit;
    358 
    359 	unit = minor(dev);
    360 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    361 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    362 		return (ENXIO);
    363 
    364 	if (sc->sc_dying)
    365 		return (EIO);
    366 
    367 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    368 		return (0);			/* always allow open for write
    369 						   so ioctl() is possible. */
    370 
    371 	if (sc->sc_events.io)			/* and that it's not in use */
    372 		return (EBUSY);
    373 
    374 	sc->sc_events.io = p;
    375 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    376 
    377 	sc->sc_ready = 1;			/* start accepting events */
    378 
    379 	/* enable the device, and punt if that's not possible */
    380 	error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    381 	if (error) {
    382 		sc->sc_ready = 0;		/* stop accepting events */
    383 		wsevent_fini(&sc->sc_events);
    384 		sc->sc_events.io = NULL;
    385 		return (error);
    386 	}
    387 
    388 	return (0);
    389 #else
    390 	return (ENXIO);
    391 #endif /* NWSMOUSE > 0 */
    392 }
    393 
    394 int
    395 wsmouseclose(dev, flags, mode, p)
    396 	dev_t dev;
    397 	int flags, mode;
    398 	struct proc *p;
    399 {
    400 #if NWSMOUSE > 0
    401 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    402 
    403 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    404 		return (0);			/* see wsmouseopen() */
    405 
    406 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    407 
    408 	sc->sc_ready = 0;			/* stop accepting events */
    409 	wsevent_fini(&sc->sc_events);
    410 	sc->sc_events.io = NULL;
    411 	return (0);
    412 #else
    413 	return (ENXIO);
    414 #endif /* NWSMOUSE > 0 */
    415 }
    416 
    417 int
    418 wsmouseread(dev, uio, flags)
    419 	dev_t dev;
    420 	struct uio *uio;
    421 	int flags;
    422 {
    423 #if NWSMOUSE > 0
    424 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    425 	int error;
    426 
    427 	if (sc->sc_dying)
    428 		return (EIO);
    429 
    430 	sc->sc_refcnt++;
    431 	error = wsevent_read(&sc->sc_events, uio, flags);
    432 	if (--sc->sc_refcnt < 0) {
    433 		wakeup(sc);
    434 		error = EIO;
    435 	}
    436 	return (error);
    437 #else
    438 	return (ENXIO);
    439 #endif /* NWSMOUSE > 0 */
    440 }
    441 
    442 int
    443 wsmouseioctl(dev, cmd, data, flag, p)
    444 	dev_t dev;
    445 	u_long cmd;
    446 	caddr_t data;
    447 	int flag;
    448 	struct proc *p;
    449 {
    450 #if NWSMOUSE > 0
    451 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    452 	int error;
    453 
    454 	sc->sc_refcnt++;
    455 	error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
    456 	if (--sc->sc_refcnt < 0)
    457 		wakeup(sc);
    458 	return (error);
    459 #else
    460 	return (ENXIO);
    461 #endif /* NWSMOUSE > 0 */
    462 }
    463 
    464 #if NWSMOUSE > 0
    465 int
    466 wsmouse_do_ioctl(sc, cmd, data, flag, p)
    467 	struct wsmouse_softc *sc;
    468 	u_long cmd;
    469 	caddr_t data;
    470 	int flag;
    471 	struct proc *p;
    472 {
    473 	int error;
    474 
    475 	if (sc->sc_dying)
    476 		return (EIO);
    477 
    478 	/*
    479 	 * Try the generic ioctls that the wsmouse interface supports.
    480 	 */
    481 	switch (cmd) {
    482 	case FIONBIO:		/* we will remove this someday (soon???) */
    483 		return (0);
    484 
    485 	case FIOASYNC:
    486 		sc->sc_events.async = *(int *)data != 0;
    487 		return (0);
    488 
    489 	case TIOCSPGRP:
    490 		if (*(int *)data != sc->sc_events.io->p_pgid)
    491 			return (EPERM);
    492 		return (0);
    493 	}
    494 
    495 	/*
    496 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    497 	 * if it didn't recognize the request.
    498 	 */
    499 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    500 	    data, flag, p);
    501 	return (error != -1 ? error : ENOTTY);
    502 }
    503 #endif /* NWSMOUSE > 0 */
    504 
    505 int
    506 wsmousepoll(dev, events, p)
    507 	dev_t dev;
    508 	int events;
    509 	struct proc *p;
    510 {
    511 #if NWSMOUSE > 0
    512 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    513 
    514 	return (wsevent_poll(&sc->sc_events, events, p));
    515 #else
    516 	return (0);
    517 #endif /* NWSMOUSE > 0 */
    518 }
    519 
    520