Home | History | Annotate | Line # | Download | only in wscons
wsmouse.c revision 1.2
      1 /* $NetBSD: wsmouse.c,v 1.2 1998/06/09 07:34:23 thorpej 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.2 1998/06/09 07:34:23 thorpej 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/kernel.h>
     90 #include <sys/proc.h>
     91 #include <sys/syslog.h>
     92 #include <sys/systm.h>
     93 #include <sys/tty.h>
     94 #include <sys/signalvar.h>
     95 #include <sys/device.h>
     96 
     97 #include <dev/wscons/wsconsio.h>
     98 #include <dev/wscons/wsmousevar.h>
     99 #include <dev/wscons/wseventvar.h>
    100 
    101 struct wsmouse_softc {
    102 	struct device	sc_dv;
    103 
    104 	const struct wsmouse_accessops *sc_accessops;
    105 	void		*sc_accesscookie;
    106 
    107 	int		sc_ready;	/* accepting events */
    108 	struct wseventvar sc_events;	/* event queue state */
    109 
    110 	u_int		sc_mb;		/* mouse button state */
    111 	u_int		sc_ub;		/* user button state */
    112 	int		sc_dx;		/* delta-x */
    113 	int		sc_dy;		/* delta-y */
    114 };
    115 
    116 int	wsmouse_match __P((struct device *, struct cfdata *, void *));
    117 void	wsmouse_attach __P((struct device *, struct device *, void *));
    118 
    119 struct cfattach wsmouse_ca = {
    120 	sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
    121 };
    122 
    123 extern struct cfdriver wsmouse_cd;
    124 
    125 cdev_decl(wsmouse);
    126 
    127 /*
    128  * Print function (for parent devices).
    129  */
    130 int
    131 wsmousedevprint(aux, pnp)
    132 	void *aux;
    133 	const char *pnp;
    134 {
    135 
    136 	if (pnp)
    137 		printf("wsmouse at %s", pnp);
    138 	return (UNCONF);
    139 }
    140 
    141 int
    142 wsmouse_match(parent, match, aux)
    143 	struct device *parent;
    144 	struct cfdata *match;
    145 	void *aux;
    146 {
    147 
    148 	return (1);
    149 }
    150 
    151 void
    152 wsmouse_attach(parent, self, aux)
    153         struct device *parent, *self;
    154 	void *aux;
    155 {
    156         struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
    157 	struct wsmousedev_attach_args *ap = aux;
    158 
    159 	printf("\n");
    160 
    161 	sc->sc_accessops = ap->accessops;
    162 	sc->sc_accesscookie = ap->accesscookie;
    163 	sc->sc_ready = 0;				/* sanity */
    164 }
    165 
    166 void
    167 wsmouse_input(wsmousedev, btns, dx, dy)
    168 	struct device *wsmousedev;
    169 	u_int btns;			/* 0 is up */
    170 	int dx, dy;
    171 {
    172 	struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
    173 	struct wscons_event *ev;
    174 	int mb, ub, d, get, put, any;
    175 
    176         /*
    177          * Discard input if not ready.
    178          */
    179 	if (sc->sc_ready == 0)
    180 		return;
    181 
    182 	sc->sc_mb = btns;
    183 	sc->sc_dx += dx;
    184 	sc->sc_dy += dy;
    185 
    186 	/*
    187 	 * We have at least one event (mouse button, delta-X, or
    188 	 * delta-Y; possibly all three, and possibly three separate
    189 	 * button events).  Deliver these events until we are out
    190 	 * of changes or out of room.  As events get delivered,
    191 	 * mark them `unchanged'.
    192 	 */
    193 	any = 0;
    194 	get = sc->sc_events.get;
    195 	put = sc->sc_events.put;
    196 	ev = &sc->sc_events.q[put];
    197 
    198 	/* NEXT prepares to put the next event, backing off if necessary */
    199 #define	NEXT								\
    200 	if ((++put) % WSEVENT_QSIZE == get) {				\
    201 		put--;							\
    202 		goto out;						\
    203 	}
    204 	/* ADVANCE completes the `put' of the event */
    205 #define	ADVANCE								\
    206 	ev++;								\
    207 	if (put >= WSEVENT_QSIZE) {					\
    208 		put = 0;						\
    209 		ev = &sc->sc_events.q[0];				\
    210 	}								\
    211 	any = 1
    212 	/* TIMESTAMP sets `time' field of the event to the current time */
    213 #define TIMESTAMP							\
    214 	do {								\
    215 		int s;							\
    216 		s = splhigh();						\
    217 		TIMEVAL_TO_TIMESPEC(&time, &ev->time);			\
    218 		splx(s);						\
    219 	} while (0)
    220 
    221 	mb = sc->sc_mb;
    222 	ub = sc->sc_ub;
    223 	while ((d = mb ^ ub) != 0) {
    224 		/*
    225 		 * Mouse button change.  Find the first change and drop
    226 		 * it into the event queue.
    227 		 */
    228 		NEXT;
    229 		ev->value = ffs(d) - 1;
    230 
    231 		KASSERT(ev->value >= 0);
    232 
    233 		d = 1 << ev->value;
    234 		ev->type =
    235 		    (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
    236 		TIMESTAMP;
    237 		ADVANCE;
    238 		ub ^= d;
    239 	}
    240 	if (sc->sc_dx) {
    241 		NEXT;
    242 		ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
    243 		ev->value = sc->sc_dx;
    244 		TIMESTAMP;
    245 		ADVANCE;
    246 		sc->sc_dx = 0;
    247 	}
    248 	if (sc->sc_dy) {
    249 		NEXT;
    250 		ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
    251 		ev->value = sc->sc_dy;
    252 		TIMESTAMP;
    253 		ADVANCE;
    254 		sc->sc_dy = 0;
    255 	}
    256 out:
    257 	if (any) {
    258 		sc->sc_ub = ub;
    259 		sc->sc_events.put = put;
    260 		WSEVENT_WAKEUP(&sc->sc_events);
    261 	}
    262 }
    263 
    264 int
    265 wsmouseopen(dev, flags, mode, p)
    266 	dev_t dev;
    267 	int flags, mode;
    268 	struct proc *p;
    269 {
    270 	struct wsmouse_softc *sc;
    271 	int error, unit;
    272 
    273 	unit = minor(dev);
    274 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    275 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    276 		return (ENXIO);
    277 
    278 	if (sc->sc_events.io)			/* and that it's not in use */
    279 		return (EBUSY);
    280 
    281 	sc->sc_events.io = p;
    282 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    283 
    284 	sc->sc_ready = 1;			/* start accepting events */
    285 
    286 	/* enable the device, and punt if that's not possible */
    287 	error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
    288 	if (error) {
    289 		sc->sc_ready = 0;		/* stop accepting events */
    290 		wsevent_fini(&sc->sc_events);
    291 		sc->sc_events.io = NULL;
    292 		return (error);
    293 	}
    294 
    295 	return (0);
    296 }
    297 
    298 int
    299 wsmouseclose(dev, flags, mode, p)
    300 	dev_t dev;
    301 	int flags, mode;
    302 	struct proc *p;
    303 {
    304 	struct wsmouse_softc *sc;
    305 	int unit;
    306 
    307 	unit = minor(dev);
    308 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    309 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    310 		return (ENXIO);
    311 
    312 	(*sc->sc_accessops->disable)(sc->sc_accesscookie);
    313 
    314 	sc->sc_ready = 0;			/* stop accepting events */
    315 	wsevent_fini(&sc->sc_events);
    316 	sc->sc_events.io = NULL;
    317 	return (0);
    318 }
    319 
    320 int
    321 wsmouseread(dev, uio, flags)
    322 	dev_t dev;
    323 	struct uio *uio;
    324 	int flags;
    325 {
    326 	struct wsmouse_softc *sc;
    327 	int unit;
    328 
    329 	unit = minor(dev);
    330 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    331 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    332 		return (ENXIO);
    333 
    334 	return (wsevent_read(&sc->sc_events, uio, flags));
    335 }
    336 
    337 int
    338 wsmouseioctl(dev, cmd, data, flag, p)
    339 	dev_t dev;
    340 	u_long cmd;
    341 	caddr_t data;
    342 	int flag;
    343 	struct proc *p;
    344 {
    345 	struct wsmouse_softc *sc;
    346 	int unit, error;
    347 
    348 	unit = minor(dev);
    349 	if (unit >= wsmouse_cd.cd_ndevs ||	/* make sure it was attached */
    350 	    (sc = wsmouse_cd.cd_devs[unit]) == NULL)
    351 		return (ENXIO);
    352 
    353 	/*
    354 	 * Try the generic ioctls that the wsmouse interface supports.
    355 	 */
    356 	switch (cmd) {
    357 	case FIONBIO:		/* we will remove this someday (soon???) */
    358 		return (0);
    359 
    360 	case FIOASYNC:
    361 		sc->sc_events.async = *(int *)data != 0;
    362 		return (0);
    363 
    364 	case TIOCSPGRP:
    365 		if (*(int *)data != sc->sc_events.io->p_pgid)
    366 			return (EPERM);
    367 		return (0);
    368 	}
    369 
    370 	/*
    371 	 * Try the mouse driver for WSMOUSEIO ioctls.  It returns -1
    372 	 * if it didn't recognize the request.
    373 	 */
    374 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    375 	    data, flag, p);
    376 	return (error != -1 ? error : ENOTTY);
    377 }
    378 
    379 int
    380 wsmousepoll(dev, events, p)
    381 	dev_t dev;
    382 	int events;
    383 	struct proc *p;
    384 {
    385 	struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
    386 
    387 	return (wsevent_poll(&sc->sc_events, events, p));
    388 }
    389