tpcalib.c revision 1.1
1/*	$NetBSD: tpcalib.c,v 1.1 2004/05/28 17:52:06 tsarna 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.1 2004/05/28 17:52:06 tsarna 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#include <machine/platid.h>
41#include <machine/platid_mask.h>
42
43#define TPCALIBDEBUG
44#ifdef TPCALIBDEBUG
45int	tpcalib_debug = 0;
46#define	DPRINTF(arg) if (tpcalib_debug) printf arg;
47#else
48#define	DPRINTF(arg)
49#endif
50
51/* mra is defined in mra.c */
52extern int mra_Y_AX1_BX2_C(int *, int, int *, int, int *, int, int, int, int *,
53    int *, int *);
54
55#define SCALE	(1024*256)
56
57int
58tpcalib_init(struct tpcalib_softc *sc)
59{
60	tpcalib_reset(sc);
61	return (0);
62}
63
64void
65tpcalib_reset(struct tpcalib_softc *sc)
66{
67	/* This indicate 'raw mode'. No translation will be done. */
68	sc->sc_saved.samplelen = WSMOUSE_CALIBCOORDS_RESET;
69}
70
71void
72tpcalib_trans(struct tpcalib_softc *sc, int rawx, int rawy, int *x, int *y)
73{
74	if (sc->sc_saved.samplelen == WSMOUSE_CALIBCOORDS_RESET) {
75		/* This indicate 'raw mode'. No translation will be done. */
76		*x = rawx;
77		*y = rawy;
78	} else {
79		*x = (sc->sc_ax * rawx + sc->sc_bx * rawy) / SCALE + sc->sc_cx;
80		*y = (sc->sc_ay * rawx + sc->sc_by * rawy) / SCALE + sc->sc_cy;
81		if (*x < sc->sc_minx) *x = sc->sc_minx;
82		if (*y < sc->sc_miny) *y = sc->sc_miny;
83		if (sc->sc_maxx < *x)
84			*x = sc->sc_maxx;
85		if (sc->sc_maxy < *y)
86			*y = sc->sc_maxy;
87	}
88}
89
90int
91tpcalib_ioctl(struct tpcalib_softc *sc, u_long cmd, caddr_t data, int flag,
92    struct proc *p)
93{
94	struct wsmouse_calibcoords *d;
95	struct wsmouse_id *id;
96	char *idstr;
97	int s;
98
99	switch (cmd) {
100	case WSMOUSEIO_SCALIBCOORDS:
101		s = sizeof(struct wsmouse_calibcoord);
102		d = (struct wsmouse_calibcoords *)data;
103		if (d->samplelen == WSMOUSE_CALIBCOORDS_RESET) {
104			tpcalib_reset(sc);
105		} else
106			if (mra_Y_AX1_BX2_C(&d->samples[0].x, s,
107			    &d->samples[0].rawx, s,
108			    &d->samples[0].rawy, s,
109			    d->samplelen, SCALE,
110			    &sc->sc_ax, &sc->sc_bx, &sc->sc_cx) ||
111			    mra_Y_AX1_BX2_C(&d->samples[0].y, s,
112				&d->samples[0].rawx, s,
113				&d->samples[0].rawy, s,
114				d->samplelen, SCALE,
115				&sc->sc_ay, &sc->sc_by, &sc->sc_cy)) {
116				printf("tpcalib: MRA error");
117				tpcalib_reset(sc);
118
119				return (EINVAL);
120			} else {
121				sc->sc_minx = d->minx;
122				sc->sc_maxx = d->maxx;
123				sc->sc_miny = d->miny;
124				sc->sc_maxy = d->maxy;
125				sc->sc_saved = *d;
126				DPRINTF(("tpcalib: x=%d~%d y=%d~%d\n",
127				    sc->sc_minx, sc->sc_maxx,
128				    sc->sc_miny, sc->sc_maxy));
129				DPRINTF(("tpcalib: Ax=%d Bx=%d Cx=%d\n",
130				    sc->sc_ax, sc->sc_bx, sc->sc_cx));
131				DPRINTF(("tpcalib: Ay=%d By=%d Cy=%d\n",
132				    sc->sc_ay, sc->sc_by, sc->sc_cy));
133			}
134		break;
135
136	case WSMOUSEIO_GCALIBCOORDS:
137		d = (struct wsmouse_calibcoords *)data;
138		*d = sc->sc_saved;
139		break;
140
141	case WSMOUSEIO_GETID:
142		/*
143		 * return unique ID string,
144		 * "<vendor> <model> <serial number>"
145		 */
146		id = (struct wsmouse_id *)data;
147		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
148			return (EINVAL);
149		idstr = platid_name(&platid);
150		s = strlen(idstr);
151		if (WSMOUSE_ID_MAXLEN - 10 < s)
152			s = WSMOUSE_ID_MAXLEN - 10;
153		memcpy(id->data, idstr, s);
154		strcpy(&id->data[s], " SN000000");
155		id->length = s + 9;
156		break;
157
158	default:
159		return (EPASSTHROUGH);
160	}
161	return (0);
162}
163