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