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