1 /* $NetBSD: elantech.c,v 1.4 2011/09/09 14:29:47 jakllsch 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.4 2011/09/09 14:29:47 jakllsch 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 ((psc->inputstate == 0 && (data & 0x0c) != 0x0c) || 224 (psc->inputstate == 3 && (data & 0x0f) != 0x08)) { 225 aprint_debug_dev(psc->sc_dev, "waiting for sync..\n"); 226 psc->inputstate = 0; 227 return; 228 } 229 230 psc->packet[psc->inputstate++] = data & 0xff; 231 if (psc->inputstate != 6) 232 return; 233 234 psc->inputstate = 0; 235 236 ep.ep_nfingers = (psc->packet[0] & 0xc0) >> 6; 237 ep.ep_buttons = 0; 238 ep.ep_buttons = psc->packet[0] & 1; /* left button */ 239 ep.ep_buttons |= (psc->packet[0] & 2) << 1; /* right button */ 240 241 if (ep.ep_nfingers == 0 || ep.ep_nfingers != sc->last_nfingers) 242 sc->initializing = true; 243 244 switch (ep.ep_nfingers) { 245 case 0: 246 /* FALLTHROUGH */ 247 case 1: 248 ep.ep_x = ((int16_t)psc->packet[1] << 8) | psc->packet[2]; 249 ep.ep_y = ((int16_t)psc->packet[4] << 8) | psc->packet[5]; 250 251 aprint_debug_dev(psc->sc_dev, 252 "%d finger detected in elantech mode:\n", ep.ep_nfingers); 253 aprint_debug_dev(psc->sc_dev, 254 " X=%d Y=%d\n", ep.ep_x, ep.ep_y); 255 aprint_debug_dev(psc->sc_dev, 256 " %02x %02x %02x %02x %02x %02x\n", 257 psc->packet[0], psc->packet[1], psc->packet[2], 258 psc->packet[3], psc->packet[4], psc->packet[5]); 259 260 s = spltty(); 261 wsmouse_input(psc->sc_wsmousedev, ep.ep_buttons, 262 sc->initializing ? 263 0 : (ep.ep_x - sc->last_x) >> elantech_xy_unprecision, 264 sc->initializing ? 265 0 : (ep.ep_y - sc->last_y) >> elantech_xy_unprecision, 266 0, 0, 267 WSMOUSE_INPUT_DELTA); 268 splx(s); 269 270 if (sc->initializing == true || 271 ((ep.ep_x - sc->last_x) >> elantech_xy_unprecision) != 0) 272 sc->last_x = ep.ep_x; 273 if (sc->initializing == true || 274 ((ep.ep_y - sc->last_y) >> elantech_xy_unprecision) != 0) 275 sc->last_y = ep.ep_y; 276 break; 277 case 2: 278 /* emulate z axis */ 279 ep.ep_z = psc->packet[2]; 280 aprint_debug_dev(psc->sc_dev, 281 "2 fingers detected in elantech mode:\n"); 282 aprint_debug_dev(psc->sc_dev, 283 " %02x %02x %02x %02x %02x %02x\n", 284 psc->packet[0], psc->packet[1], psc->packet[2], 285 psc->packet[3], psc->packet[4], psc->packet[5]); 286 287 s = spltty(); 288 wsmouse_input(psc->sc_wsmousedev, 0, 289 0, 0, 290 sc->initializing ? 291 0 : (sc->last_z - ep.ep_z) >> elantech_z_unprecision, 292 0, 293 WSMOUSE_INPUT_DELTA); 294 splx(s); 295 296 if (sc->initializing == true || 297 ((sc->last_z - ep.ep_z) >> elantech_z_unprecision) != 0) 298 sc->last_z = ep.ep_z; 299 break; 300 default: 301 aprint_debug_dev(psc->sc_dev, "that's a lot of fingers!\n"); 302 return; 303 } 304 305 if (ep.ep_nfingers > 0) 306 sc->initializing = false; 307 sc->last_nfingers = ep.ep_nfingers; 308 } 309 310 int 311 pms_elantech_probe_init(void *opaque) 312 { 313 struct pms_softc *psc = opaque; 314 struct elantech_softc *sc = &psc->u.elantech; 315 struct sysctllog *clog = NULL; 316 u_char cmd[1], resp[3]; 317 uint16_t fwversion; 318 int res; 319 320 pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot); 321 322 cmd[0] = PMS_SET_SCALE11; 323 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, 324 cmd, 1, 0, NULL, 0)) != 0) 325 goto doreset; 326 cmd[0] = PMS_SET_SCALE11; 327 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, 328 cmd, 1, 0, NULL, 0)) != 0) 329 goto doreset; 330 cmd[0] = PMS_SET_SCALE11; 331 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, 332 cmd, 1, 0, NULL, 0)) != 0) 333 goto doreset; 334 335 cmd[0] = PMS_SEND_DEV_STATUS; 336 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, 337 cmd, 1, 3, resp, 0)) != 0) 338 goto doreset; 339 340 if (!ELANTECH_MAGIC(resp)) { 341 #ifdef ELANTECH_DEBUG 342 aprint_error_dev(psc->sc_dev, 343 "bad elantech magic (%X %X %X)\n", 344 resp[0], resp[1], resp[2]); 345 #endif 346 res = 1; 347 goto doreset; 348 } 349 350 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, 351 ELANTECH_FW_VERSION); 352 cmd[0] = PMS_SEND_DEV_STATUS; 353 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, 354 cmd, 1, 3, resp, 0); 355 if (res) { 356 aprint_error_dev(psc->sc_dev, 357 "unable to query elantech firmware version\n"); 358 goto doreset; 359 } 360 361 fwversion = (resp[0] << 8) | resp[2]; 362 if (fwversion < ELANTECH_MIN_VERSION) { 363 aprint_error_dev(psc->sc_dev, 364 "unsupported Elantech version %d.%d (%X %X %X)\n", 365 resp[0], resp[2], resp[0], resp[1], resp[2]); 366 goto doreset; 367 } 368 sc->version = fwversion; 369 aprint_normal_dev(psc->sc_dev, "Elantech touchpad version %d.%d\n", 370 resp[0], resp[2]); 371 372 res = pms_elantech_init(psc); 373 if (res) { 374 aprint_error_dev(psc->sc_dev, 375 "couldn't initialize elantech touchpad\n"); 376 goto doreset; 377 } 378 379 pms_sysctl_elantech(&clog); 380 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot, 381 pms_elantech_input, psc, device_xname(psc->sc_dev)); 382 383 return 0; 384 385 doreset: 386 cmd[0] = PMS_RESET; 387 (void)pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 388 1, 2, resp, 1); 389 return res; 390 } 391 392 void 393 pms_elantech_enable(void *opaque) 394 { 395 struct pms_softc *psc = opaque; 396 struct elantech_softc *sc = &psc->u.elantech; 397 398 sc->initializing = true; 399 } 400 401 void 402 pms_elantech_resume(void *opaque) 403 { 404 struct pms_softc *psc = opaque; 405 uint8_t cmd, resp[2]; 406 int res; 407 408 cmd = PMS_RESET; 409 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd, 410 1, 2, resp, 1); 411 if (res) 412 aprint_error_dev(psc->sc_dev, 413 "elantech reset on resume failed\n"); 414 else { 415 pms_elantech_init(psc); 416 pms_elantech_enable(psc); 417 } 418 } 419