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