Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pckbport / elantech.c
blob331d97848df67e2b73559f2e2ba30a329cb0a23d
1 /* $NetBSD: elantech.c,v 1.2 2008/12/14 00:53:52 jmcneill Exp $ */
3 /*-
4 * Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
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.
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.
29 #include "opt_pms.h"
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: elantech.c,v 1.2 2008/12/14 00:53:52 jmcneill Exp $");
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>
41 #include <dev/wscons/wsconsio.h>
42 #include <dev/wscons/wsmousevar.h>
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>
50 /* #define ELANTECH_DEBUG */
52 static int elantech_xy_unprecision_nodenum;
53 static int elantech_z_unprecision_nodenum;
55 static int elantech_xy_unprecision = 2;
56 static int elantech_z_unprecision = 3;
58 struct elantech_packet {
59 int16_t ep_x, ep_y, ep_z;
60 int8_t ep_buttons;
61 uint8_t ep_nfingers;
64 static int
65 pms_sysctl_elantech_verify(SYSCTLFN_ARGS)
67 int error, t;
68 struct sysctlnode node;
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;
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;
84 *(int *)rnode->sysctl_data = t;
86 return 0;
89 static void
90 pms_sysctl_elantech(struct sysctllog **clog)
92 const struct sysctlnode *node;
93 int rc, root_num;
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;
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;
106 root_num = node->sysctl_num;
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;
118 elantech_xy_unprecision_nodenum = node->sysctl_num;
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;
130 elantech_z_unprecision_nodenum = node->sysctl_num;
131 return;
133 err:
134 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
137 /* lifted from synaptics.c */
138 static int
139 pms_elantech_send_command(pckbport_tag_t tag, pckbport_slot_t slot,
140 uint8_t syn_cmd)
142 uint8_t cmd[2];
143 int res;
145 cmd[0] = PMS_SET_SCALE11;
146 res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0);
147 if (res)
148 return res;
151 * Need to send 4 Set Resolution commands, with the argument
152 * encoded in the bottom most 2 bits.
154 cmd[0] = PMS_SET_RES;
155 cmd[1] = syn_cmd >> 6;
156 res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
158 cmd[0] = PMS_SET_RES;
159 cmd[1] = (syn_cmd & 0x30) >> 4;
160 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
162 cmd[0] = PMS_SET_RES;
163 cmd[1] = (syn_cmd & 0x0c) >> 2;
164 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
166 cmd[0] = PMS_SET_RES;
167 cmd[1] = (syn_cmd & 0x03);
168 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
170 return res;
173 static int
174 pms_elantech_read_1(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t reg,
175 uint8_t *val)
177 int res;
178 uint8_t cmd;
179 uint8_t resp[3];
181 cmd = ELANTECH_CUSTOM_CMD;
182 res = pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
183 cmd = ELANTECH_REG_READ;
184 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
185 cmd = ELANTECH_CUSTOM_CMD;
186 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
187 cmd = reg;
188 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
189 cmd = PMS_SEND_DEV_STATUS;
190 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 3, resp, 0);
192 if (res == 0)
193 *val = resp[0];
195 return res;
198 static int
199 pms_elantech_write_1(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t reg,
200 uint8_t val)
202 int res;
203 uint8_t cmd;
205 cmd = ELANTECH_CUSTOM_CMD;
206 res = pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
207 cmd = ELANTECH_REG_WRITE;
208 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
209 cmd = ELANTECH_CUSTOM_CMD;
210 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
211 cmd = reg;
212 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
213 cmd = ELANTECH_CUSTOM_CMD;
214 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
215 cmd = val;
216 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
217 cmd = PMS_SET_SCALE11;
218 res |= pckbport_poll_cmd(tag, slot, &cmd, 1, 0, NULL, 0);
220 return res;
223 static int
224 pms_elantech_init(struct pms_softc *psc)
226 uint8_t val;
227 int res;
229 /* set absolute mode */
230 res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x10, 0x54);
231 if (res)
232 return res;
233 res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x11, 0x88);
234 if (res)
235 return res;
236 res = pms_elantech_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0x21, 0x60);
237 if (res)
238 return res;
240 res = pms_elantech_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0x10, &val);
242 if (res)
243 aprint_error_dev(psc->sc_dev, "couldn't set absolute mode\n");
245 return res;
248 static void
249 pms_elantech_input(void *opaque, int data)
251 struct pms_softc *psc = opaque;
252 struct elantech_softc *sc = &psc->u.elantech;
253 struct elantech_packet ep;
254 int s;
256 if (!psc->sc_enabled)
257 return;
259 if ((psc->inputstate == 0 && (data & 0x0c) != 0x0c) ||
260 (psc->inputstate == 3 && (data & 0x0f) != 0x08)) {
261 aprint_debug_dev(psc->sc_dev, "waiting for sync..\n");
262 psc->inputstate = 0;
263 return;
266 psc->packet[psc->inputstate++] = data & 0xff;
267 if (psc->inputstate != 6)
268 return;
270 psc->inputstate = 0;
272 ep.ep_nfingers = (psc->packet[0] & 0xc0) >> 6;
273 ep.ep_buttons = 0;
274 ep.ep_buttons = psc->packet[0] & 1; /* left button */
275 ep.ep_buttons |= (psc->packet[0] & 2) << 1; /* right button */
277 if (ep.ep_nfingers == 0 || ep.ep_nfingers != sc->last_nfingers)
278 sc->initializing = true;
280 switch (ep.ep_nfingers) {
281 case 0:
282 /* FALLTHROUGH */
283 case 1:
284 ep.ep_x = ((int16_t)psc->packet[1] << 8) | psc->packet[2];
285 ep.ep_y = ((int16_t)psc->packet[4] << 8) | psc->packet[5];
287 aprint_debug_dev(psc->sc_dev,
288 "%d finger detected in elantech mode:\n", ep.ep_nfingers);
289 aprint_debug_dev(psc->sc_dev,
290 " X=%d Y=%d\n", ep.ep_x, ep.ep_y);
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]);
296 s = spltty();
297 wsmouse_input(psc->sc_wsmousedev, ep.ep_buttons,
298 sc->initializing ?
299 0 : (ep.ep_x - sc->last_x) >> elantech_xy_unprecision,
300 sc->initializing ?
301 0 : (ep.ep_y - sc->last_y) >> elantech_xy_unprecision,
302 0, 0,
303 WSMOUSE_INPUT_DELTA);
304 splx(s);
306 if (sc->initializing == true ||
307 ((ep.ep_x - sc->last_x) >> elantech_xy_unprecision) != 0)
308 sc->last_x = ep.ep_x;
309 if (sc->initializing == true ||
310 ((ep.ep_y - sc->last_y) >> elantech_xy_unprecision) != 0)
311 sc->last_y = ep.ep_y;
312 break;
313 case 2:
314 /* emulate z axis */
315 ep.ep_z = psc->packet[2];
316 aprint_debug_dev(psc->sc_dev,
317 "2 fingers detected in elantech mode:\n");
318 aprint_debug_dev(psc->sc_dev,
319 " %02x %02x %02x %02x %02x %02x\n",
320 psc->packet[0], psc->packet[1], psc->packet[2],
321 psc->packet[3], psc->packet[4], psc->packet[5]);
323 s = spltty();
324 wsmouse_input(psc->sc_wsmousedev, 0,
325 0, 0,
326 sc->initializing ?
327 0 : (sc->last_z - ep.ep_z) >> elantech_z_unprecision,
329 WSMOUSE_INPUT_DELTA);
330 splx(s);
332 if (sc->initializing == true ||
333 ((sc->last_z - ep.ep_z) >> elantech_z_unprecision) != 0)
334 sc->last_z = ep.ep_z;
335 break;
336 default:
337 aprint_debug_dev(psc->sc_dev, "that's a lot of fingers!\n");
338 return;
341 if (ep.ep_nfingers > 0)
342 sc->initializing = false;
343 sc->last_nfingers = ep.ep_nfingers;
347 pms_elantech_probe_init(void *opaque)
349 struct pms_softc *psc = opaque;
350 struct elantech_softc *sc = &psc->u.elantech;
351 struct sysctllog *clog = NULL;
352 u_char cmd[1], resp[3];
353 uint16_t fwversion;
354 int res;
356 pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot);
358 cmd[0] = PMS_SET_SCALE11;
359 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
360 cmd, 1, 0, NULL, 0)) != 0)
361 goto doreset;
362 cmd[0] = PMS_SET_SCALE11;
363 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
364 cmd, 1, 0, NULL, 0)) != 0)
365 goto doreset;
366 cmd[0] = PMS_SET_SCALE11;
367 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
368 cmd, 1, 0, NULL, 0)) != 0)
369 goto doreset;
371 cmd[0] = PMS_SEND_DEV_STATUS;
372 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
373 cmd, 1, 3, resp, 0)) != 0)
374 goto doreset;
376 if (!ELANTECH_MAGIC(resp)) {
377 #ifdef ELANTECH_DEBUG
378 aprint_error_dev(psc->sc_dev,
379 "bad elantech magic (%X %X %X)\n",
380 resp[0], resp[1], resp[2]);
381 #endif
382 res = 1;
383 goto doreset;
386 res = pms_elantech_send_command(psc->sc_kbctag, psc->sc_kbcslot,
387 ELANTECH_FW_VERSION);
388 cmd[0] = PMS_SEND_DEV_STATUS;
389 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
390 cmd, 1, 3, resp, 0);
391 if (res) {
392 aprint_error_dev(psc->sc_dev,
393 "unable to query elantech firmware version\n");
394 goto doreset;
397 fwversion = (resp[0] << 8) | resp[2];
398 if (fwversion < ELANTECH_MIN_VERSION) {
399 aprint_error_dev(psc->sc_dev,
400 "unsupported Elantech version %d.%d (%X %X %X)\n",
401 resp[0], resp[2], resp[0], resp[1], resp[2]);
402 goto doreset;
404 sc->version = fwversion;
405 aprint_normal_dev(psc->sc_dev, "Elantech touchpad version %d.%d\n",
406 resp[0], resp[2]);
408 res = pms_elantech_init(psc);
409 if (res) {
410 aprint_error_dev(psc->sc_dev,
411 "couldn't initialize elantech touchpad\n");
412 goto doreset;
415 pms_sysctl_elantech(&clog);
416 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
417 pms_elantech_input, psc, device_xname(psc->sc_dev));
419 return 0;
421 doreset:
422 cmd[0] = PMS_RESET;
423 (void)pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
424 1, 2, resp, 1);
425 return res;
428 void
429 pms_elantech_enable(void *opaque)
431 struct pms_softc *psc = opaque;
432 struct elantech_softc *sc = &psc->u.elantech;
434 sc->initializing = true;
437 void
438 pms_elantech_resume(void *opaque)
440 struct pms_softc *psc = opaque;
441 uint8_t cmd, resp[2];
442 int res;
444 cmd = PMS_RESET;
445 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd,
446 1, 2, resp, 1);
447 if (res)
448 aprint_error_dev(psc->sc_dev,
449 "elantech reset on resume failed\n");
450 else {
451 pms_elantech_init(psc);
452 pms_elantech_enable(psc);