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