xref: /netbsd-src/sys/dev/pckbport/elantech.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: elantech.c,v 1.5 2012/01/07 10:27:58 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
5  * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "opt_pms.h"
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: elantech.c,v 1.5 2012/01/07 10:27:58 jmcneill Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/bus.h>
40 
41 #include <dev/wscons/wsconsio.h>
42 #include <dev/wscons/wsmousevar.h>
43 
44 #include <dev/pckbport/pckbportvar.h>
45 #include <dev/pckbport/elantechreg.h>
46 #include <dev/pckbport/elantechvar.h>
47 #include <dev/pckbport/pmsreg.h>
48 #include <dev/pckbport/pmsvar.h>
49 
50 /* #define ELANTECH_DEBUG */
51 
52 static int elantech_xy_unprecision_nodenum;
53 static int elantech_z_unprecision_nodenum;
54 
55 static int elantech_xy_unprecision = 2;
56 static int elantech_z_unprecision = 3;
57 
58 struct elantech_packet {
59 	int16_t		ep_x, ep_y, ep_z;
60 	int8_t		ep_buttons;
61 	uint8_t		ep_nfingers;
62 };
63 
64 static int
65 pms_sysctl_elantech_verify(SYSCTLFN_ARGS)
66 {
67 	int error, t;
68 	struct sysctlnode node;
69 
70 	node = *rnode;
71 	t = *(int *)rnode->sysctl_data;
72 	node.sysctl_data = &t;
73 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
74 	if (error || newp == NULL)
75 		return error;
76 
77 	if (node.sysctl_num == elantech_xy_unprecision_nodenum ||
78 	    node.sysctl_num == elantech_z_unprecision_nodenum) {
79 		if (t < 0 || t > 7)
80 			return EINVAL;
81 	} else
82 		return EINVAL;
83 
84 	*(int *)rnode->sysctl_data = t;
85 
86 	return 0;
87 }
88 
89 static void
90 pms_sysctl_elantech(struct sysctllog **clog)
91 {
92 	const struct sysctlnode *node;
93 	int rc, root_num;
94 
95 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
96 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
97 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
98 		goto err;
99 
100 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
101 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "elantech",
102 	    SYSCTL_DESCR("Elantech touchpad controls"),
103 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
104 		goto err;
105 
106 	root_num = node->sysctl_num;
107 
108 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
109 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
110 	    CTLTYPE_INT, "xy_precision_shift",
111 	    SYSCTL_DESCR("X/Y-axis precision shift value"),
112 	    pms_sysctl_elantech_verify, 0,
113 	    &elantech_xy_unprecision,
114 	    0, CTL_HW, root_num, CTL_CREATE,
115 	    CTL_EOL)) != 0)
116 		goto err;
117 
118 	elantech_xy_unprecision_nodenum = node->sysctl_num;
119 
120 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
121 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
122 	    CTLTYPE_INT, "z_precision_shift",
123 	    SYSCTL_DESCR("Z-axis precision shift value"),
124 	    pms_sysctl_elantech_verify, 0,
125 	    &elantech_z_unprecision,
126 	    0, CTL_HW, root_num, CTL_CREATE,
127 	    CTL_EOL)) != 0)
128 		goto err;
129 
130 	elantech_z_unprecision_nodenum = node->sysctl_num;
131 	return;
132 
133 err:
134 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
135 }
136 
137 static int
138 pms_elantech_read_1(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t reg,
139     uint8_t *val)
140 {
141 	int res;
142 	uint8_t cmd;
143 	uint8_t resp[3];
144 
145 	cmd = ELANTECH_CUSTOM_CMD;
146 	res = pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
147 	cmd = ELANTECH_REG_READ;
148 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
149 	cmd = ELANTECH_CUSTOM_CMD;
150 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
151 	cmd = reg;
152 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
153 	cmd = PMS_SEND_DEV_STATUS;
154 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 3, resp, 0);
155 
156 	if (res == 0)
157 		*val = resp[0];
158 
159 	return res;
160 }
161 
162 static int
163 pms_elantech_write_1(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t reg,
164     uint8_t val)
165 {
166 	int res;
167 	uint8_t cmd;
168 
169 	cmd = ELANTECH_CUSTOM_CMD;
170 	res = pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
171 	cmd = ELANTECH_REG_WRITE;
172 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
173 	cmd = ELANTECH_CUSTOM_CMD;
174 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
175 	cmd = reg;
176 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
177 	cmd = ELANTECH_CUSTOM_CMD;
178 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
179 	cmd = val;
180 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
181 	cmd = PMS_SET_SCALE11;
182 	res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
183 
184 	return res;
185 }
186 
187 static int
188 pms_elantech_init(struct pms_softc *psc)
189 {
190 	uint8_t val;
191 	int res;
192 
193 	/* set absolute mode */
194 	res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x10, 0x54);
195 	if (res)
196 		return res;
197 	res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x11, 0x88);
198 	if (res)
199 		return res;
200 	res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x21, 0x60);
201 	if (res)
202 		return res;
203 
204 	res = pms_elantech_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0x10, &val);
205 
206 	if (res)
207 		aprint_error_dev(psc->sc_dev, "couldn't set absolute mode\n");
208 
209 	return res;
210 }
211 
212 static void
213 pms_elantech_input(void *opaque, int data)
214 {
215 	struct pms_softc *psc = opaque;
216 	struct elantech_softc *sc = &psc->u.elantech;
217 	struct elantech_packet ep;
218 	int s;
219 
220 	if (!psc->sc_enabled)
221 		return;
222 
223 	if (sc->version >= 0x020800) {
224 		if ((psc->inputstate == 0 && (data & 0x0c) != 0x04) ||
225 		    (psc->inputstate == 3 && (data & 0x0f) != 0x02)) {
226 			aprint_debug_dev(psc->sc_dev, "waiting for sync..\n");
227 			psc->inputstate = 0;
228 			return;
229 		}
230 	} else {
231 		if ((psc->inputstate == 0 && (data & 0x0c) != 0x0c) ||
232 		    (psc->inputstate == 3 && (data & 0x0e) != 0x08)) {
233 			aprint_debug_dev(psc->sc_dev, "waiting for sync..\n");
234 			psc->inputstate = 0;
235 			return;
236 		}
237 	}
238 
239 	psc->packet[psc->inputstate++] = data & 0xff;
240 	if (psc->inputstate != 6)
241 		return;
242 
243 	psc->inputstate = 0;
244 
245 	ep.ep_nfingers = (psc->packet[0] & 0xc0) >> 6;
246 	ep.ep_buttons = 0;
247 	ep.ep_buttons = psc->packet[0] & 1;		/* left button */
248 	ep.ep_buttons |= (psc->packet[0] & 2) << 1;	/* right button */
249 
250 	if (ep.ep_nfingers == 0 || ep.ep_nfingers != sc->last_nfingers)
251 		sc->initializing = true;
252 
253 	switch (ep.ep_nfingers) {
254 	case 0:
255 		/* FALLTHROUGH */
256 	case 1:
257 		ep.ep_x = ((int16_t)(psc->packet[1] & 0xf) << 8) | psc->packet[2];
258 		ep.ep_y = ((int16_t)(psc->packet[4] & 0xf) << 8) | psc->packet[5];
259 
260 		aprint_debug_dev(psc->sc_dev,
261 		    "%d finger detected in elantech mode:\n", ep.ep_nfingers);
262 		aprint_debug_dev(psc->sc_dev,
263 		    "  X=%d Y=%d\n", ep.ep_x, ep.ep_y);
264 		aprint_debug_dev(psc->sc_dev,
265 		    "  %02x %02x %02x %02x %02x %02x\n",
266 		    psc->packet[0], psc->packet[1], psc->packet[2],
267 		    psc->packet[3], psc->packet[4], psc->packet[5]);
268 
269 		s = spltty();
270 		wsmouse_input(psc->sc_wsmousedev, ep.ep_buttons,
271 		    sc->initializing ?
272 		      0 : (ep.ep_x - sc->last_x) >> elantech_xy_unprecision,
273 		    sc->initializing ?
274 		      0 : (ep.ep_y - sc->last_y) >> elantech_xy_unprecision,
275 		    0, 0,
276 		    WSMOUSE_INPUT_DELTA);
277 		splx(s);
278 
279 		if (sc->initializing == true ||
280 		    ((ep.ep_x - sc->last_x) >> elantech_xy_unprecision) != 0)
281 			sc->last_x = ep.ep_x;
282 		if (sc->initializing == true ||
283 		    ((ep.ep_y - sc->last_y) >> elantech_xy_unprecision) != 0)
284 			sc->last_y = ep.ep_y;
285 		break;
286 	case 2:
287 		/* emulate z axis */
288 		ep.ep_z = psc->packet[2];
289 		aprint_debug_dev(psc->sc_dev,
290 		    "2 fingers detected in elantech mode:\n");
291 		aprint_debug_dev(psc->sc_dev,
292 		    "  %02x %02x %02x %02x %02x %02x\n",
293 		    psc->packet[0], psc->packet[1], psc->packet[2],
294 		    psc->packet[3], psc->packet[4], psc->packet[5]);
295 
296 		s = spltty();
297 		wsmouse_input(psc->sc_wsmousedev, 0,
298 		    0, 0,
299 		    sc->initializing ?
300 		      0 : (sc->last_z - ep.ep_z) >> elantech_z_unprecision,
301 		    0,
302 		    WSMOUSE_INPUT_DELTA);
303 		splx(s);
304 
305 		if (sc->initializing == true ||
306 		    ((sc->last_z - ep.ep_z) >> elantech_z_unprecision) != 0)
307 			sc->last_z = ep.ep_z;
308 		break;
309 	default:
310 		aprint_debug_dev(psc->sc_dev, "that's a lot of fingers!\n");
311 		return;
312 	}
313 
314 	if (ep.ep_nfingers > 0)
315 		sc->initializing = false;
316 	sc->last_nfingers = ep.ep_nfingers;
317 }
318 
319 int
320 pms_elantech_probe_init(void *opaque)
321 {
322 	struct pms_softc *psc = opaque;
323 	struct elantech_softc *sc = &psc->u.elantech;
324 	struct sysctllog *clog = NULL;
325 	u_char cmd[1], resp[3];
326 	uint16_t fwversion;
327 	int res;
328 
329 	pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot);
330 
331 	cmd[0] = PMS_SET_SCALE11;
332 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
333 	    cmd, 1, 0, NULL, 0)) != 0)
334 		goto doreset;
335 	cmd[0] = PMS_SET_SCALE11;
336 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
337 	    cmd, 1, 0, NULL, 0)) != 0)
338 		goto doreset;
339 	cmd[0] = PMS_SET_SCALE11;
340 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
341 	    cmd, 1, 0, NULL, 0)) != 0)
342 		goto doreset;
343 
344 	cmd[0] = PMS_SEND_DEV_STATUS;
345 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
346 	    cmd, 1, 3, resp, 0)) != 0)
347 		goto doreset;
348 
349 	if (!ELANTECH_MAGIC(resp)) {
350 #ifdef ELANTECH_DEBUG
351 		aprint_error_dev(psc->sc_dev,
352 		    "bad elantech magic (%X %X %X)\n",
353 		    resp[0], resp[1], resp[2]);
354 #endif
355 		res = 1;
356 		goto doreset;
357 	}
358 
359 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
360 	    ELANTECH_FW_VERSION);
361 	cmd[0] = PMS_SEND_DEV_STATUS;
362 	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
363 	    cmd, 1, 3, resp, 0);
364 	if (res) {
365 		aprint_error_dev(psc->sc_dev,
366 		    "unable to query elantech firmware version\n");
367 		goto doreset;
368 	}
369 
370 	fwversion = (resp[0] << 8) | resp[2];
371 	if (fwversion < ELANTECH_MIN_VERSION) {
372 		aprint_error_dev(psc->sc_dev,
373 		    "unsupported Elantech version %d.%d (%X %X %X)\n",
374 		    resp[0], resp[2], resp[0], resp[1], resp[2]);
375 		goto doreset;
376 	}
377 	sc->version = (resp[0] << 16) | (resp[1] << 8) | resp[2];
378 	aprint_normal_dev(psc->sc_dev, "Elantech touchpad version %d.%d (%06x)\n",
379 	    resp[0], resp[2], sc->version);
380 
381 	res = pms_elantech_init(psc);
382 	if (res) {
383 		aprint_error_dev(psc->sc_dev,
384 		    "couldn't initialize elantech touchpad\n");
385 		goto doreset;
386 	}
387 
388 	pms_sysctl_elantech(&clog);
389 	pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
390 	    pms_elantech_input, psc, device_xname(psc->sc_dev));
391 
392 	return 0;
393 
394 doreset:
395 	cmd[0] = PMS_RESET;
396 	(void)pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
397 	    1, 2, resp, 1);
398 	return res;
399 }
400 
401 void
402 pms_elantech_enable(void *opaque)
403 {
404 	struct pms_softc *psc = opaque;
405 	struct elantech_softc *sc = &psc->u.elantech;
406 
407 	sc->initializing = true;
408 }
409 
410 void
411 pms_elantech_resume(void *opaque)
412 {
413 	struct pms_softc *psc = opaque;
414 	uint8_t cmd, resp[2];
415 	int res;
416 
417 	cmd = PMS_RESET;
418 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd,
419 	    1, 2, resp, 1);
420 	if (res)
421 		aprint_error_dev(psc->sc_dev,
422 		    "elantech reset on resume failed\n");
423 	else {
424 		pms_elantech_init(psc);
425 		pms_elantech_enable(psc);
426 	}
427 }
428