Home | History | Annotate | Line # | Download | only in wscons
tpcalib.c revision 1.12.14.1
      1  1.12.14.1    martin /*	$NetBSD: tpcalib.c,v 1.12.14.1 2020/04/13 08:04:52 martin Exp $	*/
      2        1.1    tsarna 
      3        1.1    tsarna /*
      4        1.1    tsarna  * Copyright (c) 1999-2003 TAKEMURA Shin All rights reserved.
      5        1.1    tsarna  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      6        1.1    tsarna  *
      7        1.1    tsarna  * Redistribution and use in source and binary forms, with or without
      8        1.1    tsarna  * modification, are permitted provided that the following conditions
      9        1.1    tsarna  * are met:
     10        1.1    tsarna  * 1. Redistributions of source code must retain the above copyright
     11        1.1    tsarna  *    notice, this list of conditions and the following disclaimer.
     12        1.1    tsarna  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1    tsarna  *    notice, this list of conditions and the following disclaimer in the
     14        1.1    tsarna  *    documentation and/or other materials provided with the distribution.
     15        1.1    tsarna  *
     16        1.8     peter  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1    tsarna  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1    tsarna  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19        1.8     peter  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1    tsarna  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1    tsarna  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1    tsarna  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1    tsarna  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1    tsarna  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1    tsarna  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1    tsarna  * SUCH DAMAGE.
     27        1.1    tsarna  *
     28        1.1    tsarna  */
     29        1.1    tsarna 
     30        1.1    tsarna #include <sys/cdefs.h>
     31  1.12.14.1    martin __KERNEL_RCSID(0, "$NetBSD: tpcalib.c,v 1.12.14.1 2020/04/13 08:04:52 martin Exp $");
     32        1.7     peter 
     33        1.7     peter #ifdef _KERNEL_OPT
     34        1.7     peter #include "opt_tpcalib.h"
     35        1.7     peter #endif
     36        1.1    tsarna 
     37        1.1    tsarna #include <sys/param.h>
     38        1.1    tsarna #include <sys/systm.h>
     39        1.1    tsarna #include <sys/device.h>
     40        1.1    tsarna #include <sys/kernel.h>
     41        1.1    tsarna #include <dev/wscons/wsconsio.h>
     42        1.1    tsarna #include <dev/wscons/tpcalibvar.h>
     43        1.1    tsarna 
     44        1.1    tsarna #ifdef TPCALIBDEBUG
     45        1.1    tsarna int	tpcalib_debug = 0;
     46        1.1    tsarna #define	DPRINTF(arg) if (tpcalib_debug) printf arg;
     47        1.1    tsarna #else
     48        1.1    tsarna #define	DPRINTF(arg)
     49        1.1    tsarna #endif
     50        1.1    tsarna 
     51        1.1    tsarna /* mra is defined in mra.c */
     52        1.5       uwe extern int mra_Y_AX1_BX2_C(const int *, int,
     53        1.5       uwe 			   const int *, int, const int *, int, int, int,
     54        1.5       uwe 			   int *, int *, int *);
     55        1.1    tsarna 
     56  1.12.14.1    martin #define SCALE	(1024)
     57        1.1    tsarna 
     58        1.1    tsarna int
     59        1.1    tsarna tpcalib_init(struct tpcalib_softc *sc)
     60        1.1    tsarna {
     61        1.1    tsarna 	tpcalib_reset(sc);
     62        1.1    tsarna 	return (0);
     63        1.1    tsarna }
     64        1.1    tsarna 
     65        1.1    tsarna void
     66        1.1    tsarna tpcalib_reset(struct tpcalib_softc *sc)
     67        1.1    tsarna {
     68        1.1    tsarna 	/* This indicate 'raw mode'. No translation will be done. */
     69        1.1    tsarna 	sc->sc_saved.samplelen = WSMOUSE_CALIBCOORDS_RESET;
     70        1.1    tsarna }
     71        1.1    tsarna 
     72        1.1    tsarna void
     73        1.1    tsarna tpcalib_trans(struct tpcalib_softc *sc, int rawx, int rawy, int *x, int *y)
     74        1.1    tsarna {
     75        1.1    tsarna 	if (sc->sc_saved.samplelen == WSMOUSE_CALIBCOORDS_RESET) {
     76        1.1    tsarna 		/* This indicate 'raw mode'. No translation will be done. */
     77        1.1    tsarna 		*x = rawx;
     78        1.1    tsarna 		*y = rawy;
     79        1.1    tsarna 	} else {
     80        1.1    tsarna 		*x = (sc->sc_ax * rawx + sc->sc_bx * rawy) / SCALE + sc->sc_cx;
     81        1.1    tsarna 		*y = (sc->sc_ay * rawx + sc->sc_by * rawy) / SCALE + sc->sc_cy;
     82        1.1    tsarna 		if (*x < sc->sc_minx) *x = sc->sc_minx;
     83        1.1    tsarna 		if (*y < sc->sc_miny) *y = sc->sc_miny;
     84        1.1    tsarna 		if (sc->sc_maxx < *x)
     85        1.1    tsarna 			*x = sc->sc_maxx;
     86        1.1    tsarna 		if (sc->sc_maxy < *y)
     87        1.1    tsarna 			*y = sc->sc_maxy;
     88        1.1    tsarna 	}
     89        1.1    tsarna }
     90        1.1    tsarna 
     91        1.1    tsarna int
     92       1.11  christos tpcalib_ioctl(struct tpcalib_softc *sc, u_long cmd, void *data,
     93       1.10  christos     int flag, struct lwp *l)
     94        1.1    tsarna {
     95        1.5       uwe 	const struct wsmouse_calibcoords *d;
     96        1.1    tsarna 	int s;
     97        1.1    tsarna 
     98        1.1    tsarna 	switch (cmd) {
     99        1.1    tsarna 	case WSMOUSEIO_SCALIBCOORDS:
    100        1.1    tsarna 		s = sizeof(struct wsmouse_calibcoord);
    101        1.5       uwe 		d = (const struct wsmouse_calibcoords *)data;
    102        1.1    tsarna 		if (d->samplelen == WSMOUSE_CALIBCOORDS_RESET) {
    103        1.1    tsarna 			tpcalib_reset(sc);
    104       1.12  macallan 		}
    105       1.12  macallan 		if (d->samplelen > 0) {
    106        1.1    tsarna 			if (mra_Y_AX1_BX2_C(&d->samples[0].x, s,
    107        1.4       uwe 				    &d->samples[0].rawx, s,
    108        1.4       uwe 				    &d->samples[0].rawy, s,
    109        1.4       uwe 				    d->samplelen, SCALE,
    110        1.4       uwe 				    &sc->sc_ax, &sc->sc_bx, &sc->sc_cx) ||
    111        1.1    tsarna 			    mra_Y_AX1_BX2_C(&d->samples[0].y, s,
    112        1.4       uwe 				    &d->samples[0].rawx, s,
    113        1.4       uwe 				    &d->samples[0].rawy, s,
    114        1.4       uwe 				    d->samplelen, SCALE,
    115        1.4       uwe 				    &sc->sc_ay, &sc->sc_by, &sc->sc_cy)) {
    116        1.1    tsarna 				printf("tpcalib: MRA error");
    117        1.1    tsarna 				tpcalib_reset(sc);
    118        1.3     perry 
    119        1.1    tsarna 				return (EINVAL);
    120        1.1    tsarna 			}
    121       1.12  macallan 		}
    122       1.12  macallan 		sc->sc_minx = d->minx;
    123       1.12  macallan 		sc->sc_maxx = d->maxx;
    124       1.12  macallan 		sc->sc_miny = d->miny;
    125       1.12  macallan 		sc->sc_maxy = d->maxy;
    126       1.12  macallan 		sc->sc_saved = *d;
    127       1.12  macallan 		DPRINTF(("tpcalib: x=%d~%d y=%d~%d\n",
    128       1.12  macallan 		    sc->sc_minx, sc->sc_maxx,
    129       1.12  macallan 		    sc->sc_miny, sc->sc_maxy));
    130       1.12  macallan 		DPRINTF(("tpcalib: Ax=%d Bx=%d Cx=%d\n",
    131       1.12  macallan 		    sc->sc_ax, sc->sc_bx, sc->sc_cx));
    132       1.12  macallan 		DPRINTF(("tpcalib: Ay=%d By=%d Cy=%d\n",
    133       1.12  macallan 		    sc->sc_ay, sc->sc_by, sc->sc_cy));
    134       1.12  macallan 
    135        1.1    tsarna 		break;
    136        1.1    tsarna 
    137        1.1    tsarna 	case WSMOUSEIO_GCALIBCOORDS:
    138        1.5       uwe 		*(struct wsmouse_calibcoords *)data = sc->sc_saved;
    139        1.1    tsarna 		break;
    140        1.1    tsarna 
    141        1.1    tsarna 	default:
    142        1.1    tsarna 		return (EPASSTHROUGH);
    143        1.1    tsarna 	}
    144        1.1    tsarna 	return (0);
    145        1.1    tsarna }
    146