Home | History | Annotate | Line # | Download | only in pckbport
      1  1.15    andvar /* $NetBSD: alps.c,v 1.15 2021/08/02 12:56:24 andvar Exp $ */
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 2017 Ryo ONODERA <ryo (at) tetera.org>
      5   1.1  christos  * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
      6   1.1  christos  * All rights reserved.
      7   1.1  christos  *
      8   1.1  christos  * Redistribution and use in source and binary forms, with or without
      9   1.1  christos  * modification, are permitted provided that the following conditions
     10   1.1  christos  * are met:
     11   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  christos  *    documentation and/or other materials provided with the distribution.
     16   1.1  christos  *
     17   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     28   1.1  christos  */
     29   1.1  christos 
     30   1.1  christos #include "opt_pms.h"
     31   1.1  christos 
     32   1.1  christos #include <sys/cdefs.h>
     33  1.15    andvar __KERNEL_RCSID(0, "$NetBSD: alps.c,v 1.15 2021/08/02 12:56:24 andvar Exp $");
     34   1.1  christos 
     35   1.1  christos #include <sys/param.h>
     36   1.1  christos #include <sys/systm.h>
     37   1.1  christos #include <sys/device.h>
     38   1.1  christos #include <sys/kernel.h>
     39   1.1  christos #include <sys/sysctl.h>
     40   1.1  christos #include <sys/bus.h>
     41   1.1  christos #include <sys/bitops.h>
     42   1.1  christos 
     43   1.1  christos #include <dev/wscons/wsconsio.h>
     44   1.1  christos #include <dev/wscons/wsmousevar.h>
     45   1.1  christos 
     46   1.1  christos #include <dev/pckbport/pckbportvar.h>
     47   1.1  christos #include <dev/pckbport/pmsreg.h>
     48   1.1  christos #include <dev/pckbport/pmsvar.h>
     49   1.1  christos #include <dev/pckbport/alpsreg.h>
     50   1.1  christos #include <dev/pckbport/alpsvar.h>
     51   1.1  christos 
     52   1.1  christos /* #define ALPS_DEBUG */
     53   1.1  christos 
     54  1.14     ryoon static int alps_touchpad_movement_threshold_nodenum;
     55   1.1  christos static int alps_touchpad_xy_unprecision_nodenum;
     56   1.1  christos static int alps_trackstick_xy_precision_nodenum;
     57   1.1  christos 
     58  1.14     ryoon static int alps_touchpad_movement_threshold = 4;
     59   1.1  christos static int alps_touchpad_xy_unprecision = 2;
     60   1.1  christos static int alps_trackstick_xy_precision = 1;
     61   1.1  christos 
     62   1.1  christos static void pms_alps_input_v7(void *, int);
     63   1.1  christos static void pms_alps_input_v2(void *, int);
     64   1.1  christos 
     65   1.1  christos static int
     66   1.1  christos pms_sysctl_alps_verify(SYSCTLFN_ARGS)
     67   1.1  christos {
     68   1.1  christos 	int error, t;
     69   1.1  christos 	struct sysctlnode node;
     70   1.1  christos 
     71   1.1  christos 	node = *rnode;
     72   1.1  christos 	t = *(int *)rnode->sysctl_data;
     73   1.1  christos 	node.sysctl_data = &t;
     74   1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
     75   1.1  christos 	if (error || newp == NULL)
     76   1.1  christos 		return error;
     77   1.1  christos 
     78   1.1  christos 	if (node.sysctl_num == alps_touchpad_xy_unprecision_nodenum ||
     79   1.1  christos 		node.sysctl_num == alps_trackstick_xy_precision_nodenum) {
     80   1.1  christos 		if (t < 0 || t > 7)
     81   1.1  christos 			return EINVAL;
     82  1.14     ryoon 	} else if (node.sysctl_num == alps_touchpad_movement_threshold_nodenum) {
     83  1.14     ryoon 		if (t < 0)
     84  1.14     ryoon 			return EINVAL;
     85   1.1  christos 	} else
     86   1.1  christos 		return EINVAL;
     87   1.1  christos 
     88   1.1  christos 	*(int *)rnode->sysctl_data = t;
     89   1.1  christos 
     90   1.1  christos 	return 0;
     91   1.1  christos 
     92   1.1  christos }
     93   1.1  christos 
     94   1.1  christos static void
     95   1.1  christos pms_sysctl_alps(struct sysctllog **clog)
     96   1.1  christos {
     97   1.1  christos 	const struct sysctlnode *node;
     98   1.1  christos 	int rc, root_num;
     99   1.1  christos 
    100   1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    101   1.1  christos 		CTLFLAG_PERMANENT, CTLTYPE_NODE, "alps",
    102   1.1  christos 		SYSCTL_DESCR("ALPS touchpad controls"),
    103   1.1  christos 		NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
    104   1.1  christos 		goto err;
    105   1.1  christos 
    106   1.1  christos 	root_num = node->sysctl_num;
    107   1.1  christos 
    108   1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    109   1.1  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    110   1.1  christos 		CTLTYPE_INT, "touchpad_xy_precision_shift",
    111   1.1  christos 		SYSCTL_DESCR("Touchpad X/Y-axis precision shift value"),
    112   1.1  christos 		pms_sysctl_alps_verify, 0,
    113   1.1  christos 		&alps_touchpad_xy_unprecision,
    114   1.1  christos 		0, CTL_HW, root_num, CTL_CREATE,
    115   1.1  christos 		CTL_EOL)) != 0)
    116   1.1  christos 			goto err;
    117   1.1  christos 	alps_touchpad_xy_unprecision_nodenum = node->sysctl_num;
    118   1.1  christos 
    119   1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    120   1.1  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    121  1.13     ryoon 		CTLTYPE_INT, "trackstick_xy_precision_shift",
    122   1.1  christos 		SYSCTL_DESCR("Trackstick X/Y-axis precision value"),
    123   1.1  christos 		pms_sysctl_alps_verify, 0,
    124   1.1  christos 		&alps_trackstick_xy_precision,
    125   1.1  christos 		0, CTL_HW, root_num, CTL_CREATE,
    126   1.1  christos 		CTL_EOL)) != 0)
    127   1.1  christos 			goto err;
    128   1.1  christos 	alps_trackstick_xy_precision_nodenum = node->sysctl_num;
    129   1.1  christos 
    130  1.14     ryoon 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    131  1.14     ryoon 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    132  1.14     ryoon 		CTLTYPE_INT, "touchpad_movement_threshold",
    133  1.14     ryoon 		SYSCTL_DESCR("Minimum reported movement threshold"),
    134  1.14     ryoon 		pms_sysctl_alps_verify, 0,
    135  1.14     ryoon 		&alps_touchpad_movement_threshold,
    136  1.14     ryoon 		0, CTL_HW, root_num, CTL_CREATE,
    137  1.14     ryoon 		CTL_EOL)) != 0)
    138  1.14     ryoon 			goto err;
    139  1.14     ryoon 	alps_touchpad_movement_threshold_nodenum = node->sysctl_num;
    140  1.14     ryoon 
    141   1.1  christos 	return;
    142   1.1  christos 
    143   1.1  christos err:
    144   1.1  christos 	aprint_error("%s: sysctl_createv failed (rc = %d)\n",
    145   1.1  christos 		__func__, rc);
    146   1.1  christos }
    147   1.1  christos 
    148   1.1  christos /*
    149   1.1  christos  * Publish E6 report command and get E6 signature,
    150   1.1  christos  * then check the signature
    151   1.1  christos  */
    152   1.1  christos static int
    153   1.1  christos pms_alps_e6sig(struct pms_softc *psc, uint8_t *e6sig)
    154   1.1  christos {
    155   1.1  christos 	uint8_t cmd[2];
    156   1.1  christos 	int res;
    157   1.1  christos 
    158   1.1  christos 	e6sig[0] = 0;
    159   1.1  christos 	cmd[0] = PMS_SET_RES; /* E8 */
    160   1.1  christos 	cmd[1] = 0;
    161   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    162   1.1  christos 	    cmd, 2, 0, NULL, 0)) != 0)
    163   1.1  christos 		goto err;
    164   1.1  christos 	cmd[0] = PMS_SET_SCALE11; /* E6 */
    165   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    166   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    167   1.1  christos 		goto err;
    168   1.1  christos 	cmd[0] = PMS_SET_SCALE11; /* E6 */
    169   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    170   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    171   1.1  christos 		goto err;
    172   1.1  christos 	cmd[0] = PMS_SET_SCALE11; /* E6 */
    173   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    174   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    175   1.1  christos 		goto err;
    176   1.1  christos 	e6sig[0] = e6sig[1] = e6sig[2] = 0;
    177   1.1  christos 	/* Get E6 signature */
    178   1.6       uwe 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    179   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    180   1.1  christos 	    cmd, 1, 3, e6sig, 0)) != 0)
    181   1.1  christos 		goto err;
    182   1.1  christos 
    183   1.1  christos 	/* ALPS input device returns 00-00-64 as E6 signature */
    184   1.7       uwe 	if ((e6sig[0] & ~0x07u) != 0x00 || /* ignore buttons */
    185   1.7       uwe 	    e6sig[1] != 0x00 ||
    186   1.7       uwe 	    e6sig[2] != 0x64)
    187   1.7       uwe 	{
    188  1.10       uwe 		return ENODEV;	/* This is not an ALPS device */
    189   1.1  christos 	}
    190   1.1  christos 
    191   1.1  christos 	aprint_debug_dev(psc->sc_dev,
    192   1.1  christos 		"ALPS PS/2 E6 signature: 0x%X 0x%X 0x%X\n",
    193   1.1  christos 		e6sig[0], e6sig[1], e6sig[2]);
    194   1.1  christos 
    195   1.1  christos 	return 0;
    196   1.1  christos err:
    197   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to get E6 signature.\n");
    198   1.1  christos 	return res;
    199   1.1  christos }
    200   1.1  christos 
    201   1.1  christos /*
    202   1.1  christos  * Publish E7 report command and get E7 signature
    203   1.1  christos  */
    204   1.1  christos static int
    205   1.1  christos pms_alps_e7sig(struct pms_softc *psc, uint8_t *e7sig)
    206   1.1  christos {
    207   1.1  christos 	uint8_t cmd[2];
    208   1.1  christos 	int res;
    209   1.1  christos 
    210   1.1  christos 	cmd[0] = PMS_SET_RES; /* E8 */
    211   1.1  christos 	cmd[1] = 0;
    212   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    213   1.1  christos 	    cmd, 2, 0, NULL, 0)) != 0)
    214   1.1  christos 		goto err;
    215   1.1  christos 	cmd[0] = PMS_SET_SCALE21; /* E7 */
    216   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    217   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    218   1.1  christos 		goto err;
    219   1.1  christos 	cmd[0] = PMS_SET_SCALE21; /* E7 */
    220   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    221   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    222   1.1  christos 		goto err;
    223   1.1  christos 	cmd[0] = PMS_SET_SCALE21; /* E7 */
    224   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    225   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    226   1.1  christos 		goto err;
    227   1.1  christos 	e7sig[0] = e7sig[1] = e7sig[2] = 0;
    228   1.6       uwe 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    229   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    230   1.1  christos 	    cmd, 1, 3, e7sig, 0)) != 0)
    231   1.1  christos 		goto err;
    232   1.1  christos 
    233   1.1  christos 	aprint_debug_dev(psc->sc_dev,
    234   1.1  christos 		"ALPS PS/2 E7 signature: 0x%X 0x%X 0x%X\n",
    235   1.1  christos 		e7sig[0], e7sig[1], e7sig[2]);
    236   1.1  christos 
    237   1.8       uwe 	if (e7sig[0] != 0x73)
    238  1.10       uwe 		return ENODEV;	/* This is not an ALPS device */
    239   1.8       uwe 
    240   1.1  christos 	return 0;
    241   1.1  christos err:
    242   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to get E7 signature.\n");
    243   1.1  christos 	return res;
    244   1.1  christos }
    245   1.1  christos 
    246   1.1  christos /*
    247   1.1  christos  * Publish EC command and get EC signature
    248   1.1  christos  */
    249   1.1  christos static int
    250   1.1  christos pms_alps_ecsig(struct pms_softc *psc, uint8_t *ecsig)
    251   1.1  christos {
    252   1.1  christos 	uint8_t cmd[2];
    253   1.1  christos 	int res;
    254   1.1  christos 
    255   1.1  christos 	cmd[0] = PMS_SET_RES; /* E8 */
    256   1.1  christos 	cmd[1] = 0;
    257   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    258   1.1  christos 	    cmd, 2, 0, NULL, 0)) != 0)
    259   1.1  christos 		goto err;
    260   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    261   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    262   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    263   1.1  christos 		goto err;
    264   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    265   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    266   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    267   1.1  christos 		goto err;
    268   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    269   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    270   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    271   1.1  christos 		goto err;
    272   1.1  christos 	ecsig[0] = ecsig[1] = ecsig[2] = 0;
    273   1.6       uwe 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    274   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    275   1.1  christos 	    cmd, 1, 3, ecsig, 0)) != 0)
    276   1.1  christos 		goto err;
    277   1.1  christos 
    278   1.1  christos 	aprint_debug_dev(psc->sc_dev,
    279   1.1  christos 		"ALPS PS/2 EC signature: 0x%X 0x%X 0x%X\n",
    280   1.1  christos 		ecsig[0], ecsig[1], ecsig[2]);
    281   1.1  christos 
    282   1.1  christos 	return 0;
    283   1.1  christos 
    284   1.1  christos err:
    285   1.5  jakllsch 	aprint_debug_dev(psc->sc_dev, "Failed to get EC signature.\n");
    286   1.1  christos 	return res;
    287   1.1  christos }
    288   1.1  christos 
    289   1.1  christos /*
    290   1.1  christos  * Enter to command mode
    291   1.1  christos  */
    292   1.1  christos static int
    293   1.1  christos pms_alps_start_command_mode(struct pms_softc *psc)
    294   1.1  christos {
    295   1.1  christos 	uint8_t cmd[1];
    296   1.1  christos 	uint8_t resp[3];
    297   1.1  christos 	int res;
    298   1.1  christos 
    299   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    300   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    301   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    302   1.1  christos 		goto err;
    303   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    304   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    305   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    306   1.1  christos 		goto err;
    307   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
    308   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    309   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    310   1.1  christos 		goto err;
    311   1.1  christos 	resp[0] = resp[1] = resp[2] = 0;
    312   1.6       uwe 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    313   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    314   1.1  christos 	    cmd, 1, 3, resp, 0)) != 0)
    315   1.1  christos 		goto err;
    316   1.1  christos 
    317   1.1  christos 	aprint_debug_dev(psc->sc_dev, "ALPS Firmware ID: 0x%x 0x%X 0x%X\n",
    318   1.1  christos 		resp[0], resp[1], resp[2]);
    319   1.1  christos 
    320   1.1  christos 	if (resp[0] != 0x88 || (resp[1] & __BITS(7, 4)) != 0xb0)
    321   1.1  christos 		return EINVAL;
    322   1.1  christos 
    323   1.1  christos 	return 0;
    324   1.1  christos err:
    325   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to start command mode.\n");
    326   1.1  christos 	return res;
    327   1.1  christos }
    328   1.1  christos 
    329   1.1  christos /*
    330   1.1  christos  * End command mode
    331   1.1  christos  */
    332   1.1  christos static int
    333   1.1  christos pms_alps_end_command_mode(struct pms_softc *psc)
    334   1.1  christos {
    335   1.1  christos 	int res;
    336   1.1  christos 	uint8_t cmd[1];
    337   1.1  christos 
    338   1.1  christos 	cmd[0] = PMS_SET_STREAM; /* EA */
    339   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    340   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    341   1.1  christos 		goto err;
    342   1.1  christos 
    343   1.1  christos 	return res;
    344   1.1  christos 
    345   1.1  christos err:
    346   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to end command mode.\n");
    347   1.1  christos 	return res;
    348   1.1  christos }
    349   1.1  christos 
    350   1.1  christos /*
    351   1.1  christos  * Write nibble (4-bit) data
    352   1.1  christos  */
    353   1.1  christos static int
    354   1.1  christos pms_alps_cm_write_nibble(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t nibble)
    355   1.1  christos {
    356   1.1  christos 	uint8_t cmd[2];
    357   1.1  christos 	uint8_t resp[3];
    358   1.1  christos 	int sendparam;
    359  1.12   msaitoh 	int receive;
    360   1.1  christos 	int res;
    361   1.1  christos 
    362   1.1  christos 	sendparam = alps_v7_nibble_command_data_arr[nibble].sendparam;
    363  1.12   msaitoh 	receive= alps_v7_nibble_command_data_arr[nibble].receive;
    364   1.1  christos 	cmd[0] = alps_v7_nibble_command_data_arr[nibble].command;
    365  1.12   msaitoh 	if (receive) {
    366   1.1  christos 		if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 3, resp, 0)) != 0) {
    367   1.1  christos 			aprint_error("send nibble error: %d\n", res);
    368   1.1  christos 		}
    369   1.1  christos 	} else if (sendparam) {
    370   1.1  christos 		cmd[1] = alps_v7_nibble_command_data_arr[nibble].data;
    371   1.1  christos 		if ((res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0)) != 0) {
    372   1.1  christos 			aprint_error("send nibble error: %d\n", res);
    373   1.1  christos 		}
    374   1.1  christos 	} else {
    375   1.1  christos 		if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0) {
    376   1.1  christos 			aprint_error("send nibble error: %d\n", res);
    377   1.1  christos 		}
    378   1.1  christos 	}
    379   1.1  christos 
    380   1.1  christos 	return res;
    381   1.1  christos }
    382   1.1  christos 
    383   1.1  christos /*
    384   1.1  christos  * Set an register address for read and write
    385   1.1  christos  */
    386   1.1  christos static int
    387   1.1  christos pms_alps_set_address(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg)
    388   1.1  christos {
    389   1.1  christos 	uint8_t cmd[1];
    390   1.1  christos 	uint8_t nibble;
    391   1.1  christos 	int res;
    392   1.1  christos 
    393   1.1  christos 	cmd[0] = PMS_RESET_WRAP_MODE;
    394   1.1  christos 	if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0)
    395   1.1  christos 		goto err;
    396   1.1  christos 
    397   1.1  christos 	/* Set address */
    398   1.1  christos 	nibble = (reg >> 12) & __BITS(3, 0);
    399   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    400   1.1  christos 		goto err;
    401   1.1  christos 	}
    402   1.1  christos 	nibble = (reg >> 8) & __BITS(3, 0);
    403   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    404   1.1  christos 		goto err;
    405   1.1  christos 	}
    406   1.1  christos 	nibble = (reg >> 4) & __BITS(3, 0);
    407   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    408   1.1  christos 		goto err;
    409   1.1  christos 	}
    410   1.1  christos 	nibble = (reg >> 0) & __BITS(3, 0);
    411   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    412   1.1  christos 		goto err;
    413   1.1  christos 	}
    414   1.1  christos 	return res;
    415   1.1  christos 
    416   1.1  christos err:
    417  1.15    andvar 	aprint_error("Failed to set address.\n");
    418   1.1  christos 	return res;
    419   1.1  christos }
    420   1.1  christos 
    421   1.1  christos /*
    422   1.1  christos  * Read one byte from register
    423   1.1  christos  */
    424   1.1  christos static int
    425   1.1  christos pms_alps_cm_read_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
    426   1.1  christos 	uint8_t *val)
    427   1.1  christos {
    428   1.1  christos 	uint8_t cmd[1];
    429   1.1  christos 	uint8_t resp[3];
    430   1.1  christos 	int res;
    431   1.1  christos 
    432   1.1  christos 	if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
    433   1.1  christos 		goto err;
    434   1.1  christos 
    435   1.1  christos 	cmd[0] = PMS_SEND_DEV_STATUS;
    436   1.1  christos 	if ((res = pckbport_poll_cmd(tag, slot,
    437   1.1  christos 	    cmd, 1, 3, resp, 0)) != 0) {
    438   1.1  christos 		goto err;
    439   1.1  christos 	}
    440   1.1  christos 
    441   1.1  christos 	if (reg != ((resp[0] << 8) | resp[1])) {
    442   1.1  christos 		return EINVAL;
    443   1.1  christos 	}
    444   1.1  christos 
    445   1.1  christos 	*val = resp[2];
    446   1.1  christos 	return res;
    447   1.1  christos 
    448   1.1  christos err:
    449   1.1  christos 	aprint_error("Failed to read a value.\n");
    450   1.1  christos 	*val = 0;
    451   1.1  christos 	return res;
    452   1.1  christos }
    453   1.1  christos 
    454   1.1  christos /*
    455   1.1  christos  * Write one byte to register
    456   1.1  christos  */
    457   1.1  christos static int
    458   1.1  christos pms_alps_cm_write_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
    459   1.1  christos 	uint8_t val)
    460   1.1  christos {
    461   1.1  christos 	uint8_t nibble;
    462   1.1  christos 	int res;
    463   1.1  christos 
    464   1.1  christos 	if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
    465   1.1  christos 		goto err;
    466   1.1  christos 
    467   1.1  christos 	nibble = __SHIFTOUT(val, __BITS(7, 4));
    468   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    469   1.1  christos 		goto err;
    470   1.1  christos 	}
    471   1.1  christos 
    472   1.1  christos 	nibble = __SHIFTOUT(val, __BITS(3, 0));
    473   1.1  christos 	if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
    474   1.1  christos 		goto err;
    475   1.1  christos 	}
    476   1.1  christos 
    477   1.1  christos 	return res;
    478   1.1  christos err:
    479   1.1  christos 	aprint_error("Failed to write a value.\n");
    480   1.1  christos 	return res;
    481   1.1  christos }
    482   1.1  christos 
    483   1.1  christos /*
    484   1.1  christos  * Not used practically for initialization
    485   1.1  christos  */
    486   1.1  christos static int
    487   1.1  christos pms_alps_get_resolution_v7(struct pms_softc *psc)
    488   1.1  christos {
    489   1.1  christos #if 0
    490   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
    491   1.1  christos #endif
    492   1.1  christos 	pckbport_tag_t tag = psc->sc_kbctag;
    493   1.1  christos 	pckbport_slot_t slot = psc->sc_kbcslot;
    494   1.1  christos 
    495   1.1  christos 	int res;
    496   1.1  christos 	uint8_t ret;
    497   1.1  christos #if 0
    498   1.1  christos 	uint32_t x_pitch, y_pitch;
    499   1.1  christos 	uint32_t x_elec, y_elec;
    500   1.1  christos 	uint32_t x_phy, y_phy;
    501   1.1  christos #endif
    502   1.1  christos 	/* X/Y pitch */
    503   1.1  christos 	if ((res = pms_alps_cm_read_1(tag, slot, 0xc397, &ret)) != 0) {
    504   1.1  christos 		goto err;
    505   1.1  christos 	}
    506   1.1  christos #if 0
    507   1.1  christos 	/* X pitch */
    508   1.1  christos 	x_pitch = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
    509   1.1  christos 	x_pitch = x_pitch * 2 + 50; /* Unit = 0.1mm */
    510   1.1  christos 
    511   1.1  christos 	/* Y pitch */
    512   1.1  christos 	y_pitch = ret & __BITS(3, 0); /* Lower 4-bit */
    513   1.1  christos 	y_pitch = y_pitch * 2 + 36; /* Unit = 0.1mm */
    514   1.1  christos 
    515   1.1  christos 	/* X/Y electrode */
    516   1.1  christos 	if ((res = pms_alps_cm_read_1(tag, slot, 0xc397 + 1, &ret)) != 0) {
    517   1.1  christos 		goto err;
    518   1.1  christos 	}
    519   1.1  christos 
    520   1.1  christos 	/* X electrode */
    521   1.1  christos 	x_elec = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
    522   1.1  christos 	x_elec = x_elec + 17;
    523   1.1  christos 
    524   1.1  christos 	/* Y electrode */
    525   1.1  christos 	y_elec = ret & __BITS(3, 0); /* Lower 4-bit */
    526   1.1  christos 	y_elec = y_elec + 13;
    527   1.1  christos 
    528   1.1  christos 	/* X/Y physical in unit = 0.1mm */
    529   1.1  christos 	/* X physical */
    530   1.1  christos 	x_phy = (x_elec - 1) * x_pitch;
    531   1.1  christos 	y_phy = (y_elec - 1) * y_pitch;
    532   1.1  christos 
    533   1.1  christos 	/* X/Y resolution (unit) */
    534   1.1  christos 	sc->res_x = 0xfff * 10 / x_phy;
    535   1.1  christos 	sc->res_y = 0x7ff * 10 / y_phy;
    536   1.1  christos #endif
    537   1.1  christos 	return res;
    538   1.1  christos 
    539   1.1  christos err:
    540   1.1  christos 	aprint_error("Failed to get resolution.\n");
    541   1.1  christos 	return res;
    542   1.1  christos }
    543   1.1  christos 
    544   1.1  christos /*
    545   1.1  christos  * Enable tap mode for V2 device
    546   1.1  christos  */
    547   1.1  christos static int
    548   1.1  christos pms_alps_enable_tap_mode_v2(struct pms_softc *psc)
    549   1.1  christos {
    550   1.1  christos 	uint8_t cmd[2];
    551   1.1  christos 	uint8_t resp[3];
    552   1.1  christos 	int res;
    553   1.1  christos 
    554   1.1  christos 	resp[0] = resp[1] = resp[2] = 0;
    555   1.1  christos 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    556   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    557   1.1  christos 	    cmd, 1, 3, resp, 0)) != 0)
    558   1.1  christos 		goto err;
    559   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    560   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    561   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    562   1.1  christos 		goto err;
    563   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    564   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    565   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    566   1.1  christos 		goto err;
    567   1.1  christos 	cmd[0] = PMS_SET_SAMPLE;
    568   1.1  christos 	cmd[1] = 0x0a; /* argument */
    569   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    570   1.1  christos 	    cmd, 2, 0, NULL, 0)) != 0)
    571   1.1  christos 		goto err;
    572   1.1  christos 
    573   1.1  christos 	/* Get status */
    574   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    575   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    576   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    577   1.1  christos 		goto err;
    578   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    579   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    580   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    581   1.1  christos 		goto err;
    582   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    583   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    584   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    585   1.1  christos 		goto err;
    586   1.1  christos 	resp[0] = resp[1] = resp[2] = 0;
    587   1.1  christos 	cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
    588   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    589   1.1  christos 	    cmd, 1, 3, resp, 0)) != 0)
    590   1.1  christos 		goto err;
    591   1.1  christos 
    592   1.1  christos 	aprint_debug_dev(psc->sc_dev, "Tap mode is enabled.\n");
    593   1.1  christos 
    594   1.1  christos 	return 0;
    595   1.1  christos err:
    596   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to enable tap mode.\n");
    597   1.1  christos 	return res;
    598   1.1  christos }
    599   1.1  christos 
    600   1.1  christos static int
    601   1.1  christos pms_alps_init_v2(struct pms_softc *psc)
    602   1.1  christos {
    603   1.1  christos 	uint8_t cmd[1];
    604   1.1  christos 	int res;
    605   1.1  christos 
    606   1.1  christos 	if ((res = pms_alps_enable_tap_mode_v2(psc)) != 0)
    607   1.1  christos 		goto err;
    608   1.1  christos 
    609   1.1  christos 	/* Enable absolute mode */
    610   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    611   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    612   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    613   1.1  christos 		goto err;
    614   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    615   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    616   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    617   1.1  christos 		goto err;
    618   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    619   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    620   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    621   1.1  christos 		goto err;
    622   1.1  christos 	cmd[0] = PMS_DEV_DISABLE; /* F5 */
    623   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    624   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    625   1.1  christos 		goto err;
    626   1.1  christos 	cmd[0] = PMS_DEV_ENABLE; /* F4 */
    627   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    628   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    629   1.1  christos 		goto err;
    630   1.1  christos 
    631   1.1  christos 	/* Enable remote mode */
    632   1.1  christos 	cmd[0] = PMS_SET_REMOTE_MODE; /* F0 */
    633   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    634   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    635   1.1  christos 		goto err;
    636   1.1  christos 
    637   1.1  christos 	/* Start stream mode to get data */
    638   1.1  christos 	cmd[0] = PMS_SET_STREAM; /* EA */
    639   1.1  christos 	if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    640   1.1  christos 	    cmd, 1, 0, NULL, 0)) != 0)
    641   1.1  christos 		goto err;
    642   1.1  christos 
    643   1.1  christos 
    644   1.1  christos 	return 0;
    645   1.1  christos 
    646   1.1  christos err:
    647   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to initialize V2 device.\n");
    648   1.1  christos 	return res;
    649   1.1  christos }
    650   1.1  christos 
    651   1.1  christos static int
    652   1.1  christos pms_alps_init_v7(struct pms_softc *psc)
    653   1.1  christos {
    654   1.1  christos 	uint8_t val;
    655   1.1  christos 	uint8_t nibble;
    656   1.1  christos 	int res;
    657   1.1  christos 
    658   1.1  christos 	/* Start command mode */
    659   1.1  christos 	if ((res = pms_alps_start_command_mode(psc)) != 0) {
    660   1.1  christos 		goto err;
    661   1.1  christos 	}
    662   1.1  christos 	if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2d9, &val)) != 0) {
    663   1.1  christos 		goto err;
    664   1.1  christos 	}
    665   1.1  christos 	if ((res = pms_alps_get_resolution_v7(psc)) != 0) {
    666   1.1  christos 		goto err;
    667   1.1  christos 	}
    668   1.1  christos 	if ((res = pms_alps_cm_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c9, 0x64)) != 0) {
    669   1.1  christos 		goto err;
    670   1.1  christos 	}
    671   1.1  christos 
    672   1.1  christos 	/* Start absolute mode */
    673   1.1  christos 	if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c4, &val)) != 0) {
    674   1.1  christos 		goto err;
    675   1.1  christos 	}
    676   1.1  christos 	/* Do not set address before this, so do not use pms_cm_write_1() */
    677   1.1  christos 	val = val | __BIT(1);
    678   1.1  christos 	nibble = __SHIFTOUT(val, __BITS(7, 4));
    679   1.1  christos 	if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
    680   1.1  christos 		goto err;
    681   1.1  christos 	}
    682   1.1  christos 	nibble = __SHIFTOUT(val, __BITS(3, 0));
    683   1.1  christos 	if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
    684   1.1  christos 		goto err;
    685   1.1  christos 	}
    686   1.1  christos 
    687   1.1  christos 	/* End command mode */
    688   1.1  christos 	if ((res = pms_alps_end_command_mode(psc)) != 0)
    689   1.1  christos 		goto err;
    690   1.1  christos 
    691   1.1  christos 	return res;
    692   1.1  christos 
    693   1.1  christos err:
    694   1.1  christos 	(void)pms_alps_end_command_mode(psc);
    695   1.1  christos 	aprint_error_dev(psc->sc_dev, "Failed to initialize V7 device.\n");
    696   1.1  christos 	return res;
    697   1.1  christos }
    698   1.1  christos 
    699   1.1  christos int
    700   1.1  christos pms_alps_probe_init(void *opaque)
    701   1.1  christos {
    702   1.1  christos 	struct pms_softc *psc = opaque;
    703   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
    704   1.1  christos 	struct sysctllog *clog = NULL;
    705   1.1  christos 	uint8_t e6sig[3];
    706   1.1  christos 	uint8_t e7sig[3];
    707   1.1  christos 	uint8_t ecsig[3];
    708   1.1  christos 	int res;
    709   1.9       uwe 	u_char cmd[1];
    710   1.3       nat 
    711   1.1  christos 	sc->last_x1 = 0;
    712   1.1  christos 	sc->last_y1 = 0;
    713   1.1  christos 	sc->last_x2 = 0;
    714   1.1  christos 	sc->last_y2 = 0;
    715   1.1  christos 	sc->last_nfingers = 0;
    716   1.1  christos 
    717   1.1  christos 	pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot);
    718   1.1  christos 
    719   1.1  christos 	if ((res = pms_alps_e6sig(psc, e6sig)) != 0)
    720  1.10       uwe 		goto err;
    721   1.1  christos 
    722   1.1  christos 	if ((res = pms_alps_e7sig(psc, e7sig)) != 0)
    723   1.1  christos 		goto err;
    724   1.1  christos 
    725   1.1  christos 	if ((res = pms_alps_ecsig(psc, ecsig)) != 0)
    726   1.1  christos 		goto err;
    727   1.1  christos 
    728   1.1  christos 	/* Determine protocol version */
    729   1.1  christos 	if ((ecsig[0] == 0x88) && (__SHIFTOUT(ecsig[1], __BITS(7, 4)) == 0x0b)) {
    730   1.1  christos 		/* V7 device in Toshiba dynabook R63/PS */
    731   1.1  christos 		sc->version = ALPS_PROTO_V7;
    732   1.1  christos 	} else if ((e7sig[0] == 0x73) && (e7sig[1] == 0x02) &&
    733  1.11     ryoon 		((e7sig[2] == 0x14) || (e7sig[2] == 0x0a))) {
    734  1.11     ryoon 		/* 0x14: V2 device in NEC VJ22MF-7 (VersaPro JVF-7) */
    735  1.11     ryoon 		/* 0x0a: V2 devices in Toshiba dynabook satellite B551/D
    736  1.11     ryoon 			 and dynabook SS RX1 */
    737   1.1  christos 		sc->version = ALPS_PROTO_V2;
    738   1.1  christos 	}
    739   1.1  christos 
    740   1.1  christos 	if (sc->version == ALPS_PROTO_V7) {
    741   1.1  christos 		/* Initialize V7 device */
    742   1.1  christos 		if ((res = pms_alps_init_v7(psc)) != 0)
    743   1.1  christos 			goto err;
    744   1.1  christos 		aprint_normal_dev(psc->sc_dev,
    745   1.1  christos 			"ALPS PS/2 V7 pointing device\n");
    746   1.1  christos 	} else if (sc->version == ALPS_PROTO_V2) {
    747   1.1  christos 		/* Initialize V2 pointing device */
    748   1.1  christos 		if ((res = pms_alps_init_v2(psc)) != 0)
    749   1.1  christos 			goto err;
    750   1.1  christos 		aprint_normal_dev(psc->sc_dev,
    751   1.1  christos 			"ALPS PS/2 V2 pointing device\n");
    752   1.1  christos 	} else {
    753   1.3       nat 		res = EINVAL;
    754   1.3       nat 		goto err;
    755   1.1  christos 	}
    756   1.1  christos 
    757   1.1  christos 	/* From sysctl */
    758   1.1  christos 	pms_sysctl_alps(&clog);
    759   1.1  christos 	/* Register hundler */
    760   1.1  christos 	if (sc->version == ALPS_PROTO_V7) {
    761   1.1  christos 		pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
    762   1.1  christos 			pms_alps_input_v7, psc, device_xname(psc->sc_dev));
    763   1.1  christos 	} else if (sc->version == ALPS_PROTO_V2) {
    764   1.1  christos 		pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
    765   1.1  christos 			pms_alps_input_v2, psc, device_xname(psc->sc_dev));
    766   1.1  christos 	} else {
    767   1.3       nat 		res = EINVAL;
    768   1.3       nat 		goto err;
    769   1.1  christos 	}
    770   1.1  christos 	/* Palm detection is enabled. */
    771   1.1  christos 
    772   1.1  christos 	return 0;
    773   1.1  christos 
    774   1.1  christos err:
    775   1.3       nat 	cmd[0] = PMS_RESET;
    776   1.9       uwe 	(void)pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    777   1.9       uwe 	    cmd, 1, 2, NULL, 1);
    778   1.8       uwe 	if (res != ENODEV)
    779  1.10       uwe 		aprint_error_dev(psc->sc_dev, "Failed to initialize an ALPS device.\n");
    780   1.1  christos 	return res;
    781   1.1  christos }
    782   1.1  christos 
    783   1.1  christos void
    784   1.1  christos pms_alps_enable(void *opaque)
    785   1.1  christos {
    786   1.1  christos 	struct pms_softc *psc = opaque;
    787   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
    788   1.1  christos 
    789   1.1  christos 	sc->initializing = true;
    790   1.1  christos }
    791   1.1  christos 
    792   1.1  christos void
    793   1.1  christos pms_alps_resume(void *opaque)
    794   1.1  christos {
    795   1.1  christos 	struct pms_softc *psc = opaque;
    796   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
    797   1.1  christos 	uint8_t cmd, resp[2];
    798   1.1  christos 	int res;
    799   1.1  christos 
    800   1.1  christos 	cmd = PMS_RESET;
    801   1.1  christos 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd,
    802   1.1  christos 		1, 2, resp, 1);
    803   1.1  christos 	if (res)
    804   1.1  christos 	aprint_error_dev(psc->sc_dev,
    805   1.1  christos 		"ALPS reset on resume failed\n");
    806   1.1  christos 	else {
    807   1.1  christos 		if (sc->version == ALPS_PROTO_V7) {
    808   1.1  christos 			(void)pms_alps_init_v7(psc);
    809   1.1  christos 		} else if (sc->version == ALPS_PROTO_V2) {
    810   1.1  christos 			(void)pms_alps_init_v2(psc);
    811   1.1  christos 		} else {
    812   1.1  christos 			/* Not supported */
    813   1.1  christos 		}
    814   1.1  christos 		pms_alps_enable(psc);
    815   1.1  christos 	}
    816   1.1  christos }
    817   1.1  christos 
    818   1.1  christos static void
    819   1.1  christos pms_alps_decode_trackstick_packet_v7(struct pms_softc *psc)
    820   1.1  christos {
    821   1.1  christos 	int s;
    822   1.1  christos 
    823   1.1  christos 	int x, y, z;
    824   1.1  christos 	int dx, dy, dz;
    825   1.1  christos 	int left, middle, right;
    826   1.1  christos 	u_int buttons;
    827   1.1  christos 
    828   1.1  christos 	x = (int8_t)((psc->packet[2] & 0xbf) |
    829   1.1  christos 		((psc->packet[3] & 0x10) << 2));
    830   1.1  christos 	y = (int8_t)((psc->packet[3] & 0x07) | (psc->packet[4] & 0xb8) |
    831   1.1  christos 		((psc->packet[3] & 0x20) << 1));
    832   1.1  christos 	z = (int8_t)((psc->packet[5] & 0x3f) |
    833   1.1  christos 		((psc->packet[3] & 0x80) >> 1));
    834   1.1  christos 
    835   1.1  christos 	dx = x * alps_trackstick_xy_precision;
    836   1.1  christos 	dy = y * alps_trackstick_xy_precision;
    837   1.1  christos 	dz = z * 1;
    838   1.1  christos 
    839   1.1  christos 	left = psc->packet[1] & 0x01;
    840   1.1  christos 	middle = (psc->packet[1] & 0x04) >> 2;
    841   1.1  christos 	right = (psc->packet[1] & 0x02) >> 1;
    842   1.1  christos 	buttons = 0;
    843   1.1  christos 	buttons = (u_int)((left << 0) | (middle << 1) | (right << 2));
    844   1.1  christos 
    845   1.1  christos 	s = spltty();
    846   1.1  christos 	wsmouse_input(psc->sc_wsmousedev,
    847   1.1  christos 		buttons,
    848   1.1  christos 		dx, dy, dz, 0,
    849   1.1  christos 		WSMOUSE_INPUT_DELTA);
    850   1.1  christos 	splx(s);
    851   1.1  christos }
    852   1.1  christos 
    853   1.1  christos static uint8_t
    854   1.1  christos pms_alps_decode_packetid_v7(struct pms_softc *psc)
    855   1.1  christos {
    856   1.1  christos 	if (psc->packet[4] & 0x40)
    857   1.1  christos 		return ALPS_V7_PACKETID_TWOFINGER;
    858   1.1  christos 	else if (psc->packet[4] & 0x01)
    859   1.1  christos 		return ALPS_V7_PACKETID_MULTIFINGER;
    860   1.1  christos 	else if ((psc->packet[0] & 0x10) && !(psc->packet[4] & 0x43))
    861   1.1  christos 		return ALPS_V7_PACKETID_NEWPACKET;
    862   1.1  christos 	else if ((psc->packet[1] == 0x00) && (psc->packet[4] == 0x00))
    863   1.1  christos 		return ALPS_V7_PACKETID_IDLE;
    864   1.1  christos 	else
    865   1.1  christos 		return ALPS_V7_PACKETID_UNKNOWN;
    866   1.1  christos }
    867   1.1  christos 
    868   1.1  christos static void
    869   1.1  christos pms_alps_decode_touchpad_packet_v7(struct pms_softc *psc)
    870   1.1  christos {
    871   1.1  christos 	int s;
    872   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
    873   1.1  christos 	uint8_t packetid;
    874   1.1  christos 
    875   1.1  christos 	uint16_t cur_x1, cur_y1;
    876   1.1  christos 	uint16_t cur_x2, cur_y2;
    877   1.1  christos 	int dx1, dy1;
    878   1.1  christos 	int button;
    879   1.1  christos 	u_int buttons;
    880   1.1  christos 
    881   1.1  christos 	packetid = pms_alps_decode_packetid_v7(psc);
    882   1.1  christos 	switch (packetid) {
    883   1.1  christos 	case ALPS_V7_PACKETID_IDLE:
    884   1.1  christos 		/* Accept meaningful packets only */
    885   1.1  christos 		return;
    886   1.1  christos 	case ALPS_V7_PACKETID_UNKNOWN:
    887   1.1  christos 		/* Accept meaningful packets only */
    888   1.1  christos 		return;
    889   1.1  christos 	case ALPS_V7_PACKETID_NEWPACKET:
    890   1.1  christos 		/* Sent new packet ID to reset status and not decoded */
    891   1.1  christos 		sc->initializing = true;
    892   1.1  christos 		return;
    893   1.1  christos 	}
    894   1.1  christos 
    895   1.1  christos 	/* Decode a number of fingers and locations */
    896   1.1  christos 	/* X0-11 ... X0-0 */
    897   1.1  christos 	cur_x1 = (psc->packet[2] & 0x80) << 4; /* X0-11 */
    898   1.1  christos 	cur_x1 |= (psc->packet[2] & 0x3f) << 5; /* X0-10 ... X0-5 */
    899   1.1  christos 	cur_x1 |= (psc->packet[3] & 0x30) >> 1; /* X0-4, X0-3 */
    900   1.1  christos 	cur_x1 |= psc->packet[3] & 0x07; /* X0-2 ... X0-0 */
    901   1.1  christos 
    902   1.1  christos 	/* Y0-10 ... Y0-0 */
    903   1.1  christos 	cur_y1 = psc->packet[1] << 3; /* Y0-10 ... Y0-3 */
    904   1.1  christos 	cur_y1 |= psc->packet[0] & 0x07; /* Y0-2 ... Y0-0 */
    905   1.1  christos 
    906   1.1  christos 	/* X1-11 ... X1-3 */
    907   1.1  christos 	cur_x2 = (psc->packet[3] & 0x80) << 4; /* X1-11 */
    908   1.1  christos 	cur_x2 |= (psc->packet[4] & 0x80) << 3; /* X1-10 */
    909   1.1  christos 	cur_x2 |= (psc->packet[4] & 0x3f) << 4; /* X1-9 ... X1-4 */
    910   1.1  christos 
    911   1.1  christos 	/* Y1-10 ... Y1-4 */
    912   1.1  christos 	cur_y2 = (psc->packet[5] & 0x80) << 3; /* Y1-10 */
    913   1.1  christos 	cur_y2 |= (psc->packet[5] & 0x3f) << 4; /* Y1-9 .. Y1-4 */
    914   1.1  christos 
    915   1.1  christos 	switch (packetid) {
    916   1.1  christos 	case ALPS_V7_PACKETID_TWOFINGER:
    917   1.1  christos 		cur_x2 &= ~__BITS(3, 0); /* Clear undefined locations */
    918   1.1  christos 		cur_y2 |= __BITS(3, 0); /* Fill undefined locations */
    919   1.1  christos 		break;
    920   1.1  christos 	case ALPS_V7_PACKETID_MULTIFINGER:
    921   1.1  christos 		cur_x2 &= ~__BITS(5, 0); /* Clear undefined locations */
    922   1.1  christos 		cur_y2 &= ~__BIT(5); /* Clear duplicate locations */
    923   1.1  christos 		cur_y2 |= (psc->packet[4] & __BIT(1)) << 4; /* Set another */
    924   1.1  christos 		cur_y2 |= __BITS(4, 0); /* Fill undefined locations */
    925   1.1  christos 		break;
    926   1.1  christos 	}
    927   1.1  christos 
    928   1.1  christos 	cur_y1 = 0x7ff - cur_y1;
    929   1.1  christos 	cur_y2 = 0x7ff - cur_y2;
    930   1.1  christos 
    931   1.1  christos 	/* Handle finger touch reported in cur_x2/y2. only */
    932   1.1  christos 	if (cur_x1 == 0 && cur_y1 == 0 && cur_x2 != 0 && cur_y2 != 0) {
    933   1.1  christos 		cur_x1 = cur_x2;
    934   1.1  christos 		cur_y1 = cur_y2;
    935   1.1  christos 		cur_x2 = 0;
    936   1.1  christos 		cur_y2 = 0;
    937   1.1  christos 	}
    938   1.1  christos 
    939   1.1  christos 	switch (packetid) {
    940   1.1  christos 	case ALPS_V7_PACKETID_TWOFINGER:
    941   1.1  christos 		if ((cur_x2 == 0) && (cur_y2 == 0))
    942   1.1  christos 			sc->nfingers = 1;
    943   1.1  christos 		else
    944   1.1  christos 			sc->nfingers = 2;
    945   1.1  christos 		break;
    946   1.1  christos 	case ALPS_V7_PACKETID_MULTIFINGER:
    947   1.1  christos 		sc->nfingers = 3 + (psc->packet[5] & 0x03);
    948   1.1  christos 		break;
    949   1.1  christos 	}
    950   1.1  christos 
    951   1.1  christos 	button = (psc->packet[0] & 0x80) >> 7;
    952   1.1  christos 	buttons = 0;
    953   1.1  christos 	if (sc->nfingers == 1) {
    954   1.1  christos 		if (button && (cur_y1 > 1700) && (cur_x1 < 1700))
    955   1.1  christos 			buttons |= button << 0; /* Left button */
    956   1.1  christos 		else if (button && (cur_y1 > 1700)
    957   1.1  christos 				&& (1700 <= cur_x1) && (cur_x1 <= 2700))
    958   1.1  christos 			buttons |= button << 1; /* Middle button */
    959   1.1  christos 		else if (button && (cur_y1 > 1700) && (2700 < cur_x1))
    960   1.1  christos 			buttons |= button << 2; /* Right button */
    961   1.1  christos 	} else if (sc->nfingers > 1) {
    962   1.1  christos 		if (button && (cur_y2 > 1700) && (cur_x2 < 1700))
    963   1.1  christos 			buttons |= button << 0; /* Left button */
    964   1.1  christos 		else if (button && (cur_y2 > 1700)
    965   1.1  christos 				&& (1700 <= cur_x2) && (cur_x2 <= 2700))
    966   1.1  christos 			buttons |= button << 1; /* Middle button */
    967   1.1  christos 		else if (button && (cur_y2 > 1700) && (2700 < cur_x2))
    968   1.1  christos 			buttons |= button << 2; /* Right button */
    969   1.1  christos 	}
    970   1.1  christos 
    971   1.1  christos 	/* New touch */
    972   1.1  christos 	if (sc->nfingers == 0 || sc->nfingers != sc->last_nfingers)
    973   1.1  christos 		sc->initializing = true;
    974   1.1  christos 
    975   1.1  christos 	if (sc->initializing == true) {
    976   1.1  christos 		dx1 = 0;
    977   1.1  christos 		dy1 = 0;
    978   1.1  christos 	} else {
    979   1.1  christos 		dx1 = (int16_t)(cur_x1 - sc->last_x1);
    980   1.1  christos 		dy1 = (int16_t)(sc->last_y1 - cur_y1);
    981   1.1  christos 
    982   1.1  christos 		dx1 = dx1 >> alps_touchpad_xy_unprecision;
    983   1.1  christos 		dy1 = dy1 >> alps_touchpad_xy_unprecision;
    984   1.1  christos 	}
    985   1.1  christos 
    986  1.14     ryoon 	if (abs(dx1) < alps_touchpad_movement_threshold) {
    987  1.14     ryoon 		dx1 = 0;
    988  1.14     ryoon 	}
    989  1.14     ryoon 	if (abs(dy1) < alps_touchpad_movement_threshold) {
    990  1.14     ryoon 		dy1 = 0;
    991  1.14     ryoon 	}
    992  1.14     ryoon 
    993   1.1  christos 	/* Allow finger detouch during drag and drop */
    994   1.1  christos 	if ((sc->nfingers < sc->last_nfingers)
    995   1.1  christos 		&& (cur_x2 == sc->last_x1) && (cur_y2 == sc->last_y1)) {
    996   1.1  christos 		sc->last_x1 = sc->last_x2;
    997   1.1  christos 		sc->last_y1 = sc->last_y2;
    998   1.1  christos 		dx1 = 0;
    999   1.1  christos 		dy1 = 0;
   1000   1.1  christos 	}
   1001   1.1  christos 
   1002   1.1  christos 	s = spltty();
   1003   1.1  christos 	wsmouse_input(psc->sc_wsmousedev,
   1004   1.1  christos 		buttons,
   1005   1.1  christos 		dx1, dy1, 0, 0,
   1006   1.1  christos 		WSMOUSE_INPUT_DELTA);
   1007   1.1  christos 	splx(s);
   1008   1.1  christos 
   1009   1.1  christos 	if (sc->initializing == true || (dx1 != 0))
   1010   1.1  christos 		sc->last_x1 = cur_x1;
   1011   1.1  christos 	if (sc->initializing == true || (dy1 != 0))
   1012   1.1  christos 		sc->last_y1 = cur_y1;
   1013   1.1  christos 
   1014   1.1  christos 	if (sc->nfingers > 0)
   1015   1.1  christos 		sc->initializing = false;
   1016   1.1  christos 	sc->last_nfingers = sc->nfingers;
   1017   1.1  christos }
   1018   1.1  christos 
   1019   1.1  christos static void
   1020   1.1  christos pms_alps_dispatch_packet_v7(struct pms_softc *psc)
   1021   1.1  christos {
   1022   1.1  christos 	if ((psc->packet[0] == 0x48) && ((psc->packet[4] & 0x47) == 0x06))
   1023   1.1  christos 		pms_alps_decode_trackstick_packet_v7(psc);
   1024   1.1  christos 	else
   1025   1.1  christos 		pms_alps_decode_touchpad_packet_v7(psc);
   1026   1.1  christos }
   1027   1.1  christos 
   1028   1.1  christos static void
   1029   1.1  christos pms_alps_decode_touchpad_packet_v2(struct pms_softc *psc)
   1030   1.1  christos {
   1031   1.1  christos 	int s;
   1032   1.1  christos 	struct alps_softc *sc = &psc->u.alps;
   1033   1.1  christos 	uint16_t cur_x, cur_y;
   1034   1.1  christos 	int16_t dx, dy;
   1035   1.1  christos 	u_int left, middle, right;
   1036   1.1  christos 	u_int forward, back;
   1037   1.1  christos 	u_int buttons;
   1038   1.1  christos 	uint8_t ges;
   1039   1.1  christos 
   1040   1.1  christos 	sc->nfingers = (psc->packet[2] & 0x02) >> 1;
   1041   1.1  christos 	if (sc->last_nfingers == 0)
   1042   1.1  christos 		sc->initializing = true;
   1043   1.1  christos 
   1044   1.1  christos 	left = psc->packet[3] & 0x01;
   1045   1.1  christos 	right = (psc->packet[3] & 0x02) >> 1;
   1046   1.1  christos 	middle = (psc->packet[3] & 0x04) >> 2;
   1047   1.1  christos 
   1048   1.1  christos 	cur_x = psc->packet[1];
   1049   1.1  christos 	cur_x |= (psc->packet[2] & 0x78) << 4;
   1050   1.1  christos 
   1051   1.1  christos 	cur_y = psc->packet[4];
   1052   1.1  christos 	cur_y |= (psc->packet[3] & 0x70) << 3;
   1053   1.1  christos 
   1054   1.1  christos #if 0
   1055   1.1  christos 	cur_z = psc->packet[5];
   1056   1.1  christos #endif
   1057   1.1  christos 
   1058   1.1  christos 	forward = (psc->packet[2] & 0x04) >> 2;
   1059   1.1  christos 	back = (psc->packet[3] & 0x04) >> 2;
   1060   1.1  christos 	ges = psc->packet[2] & 0x01;
   1061   1.1  christos 
   1062   1.1  christos 	buttons = (left | ges) << 0;
   1063   1.1  christos 	buttons |= (middle | forward | back) << 1;
   1064   1.1  christos 	buttons |= right << 2;
   1065   1.1  christos 
   1066   1.1  christos 	if (sc->initializing == true) {
   1067   1.1  christos 		dx = 0;
   1068   1.1  christos 		dy = 0;
   1069   1.1  christos 	} else {
   1070   1.1  christos 		dx = (cur_x - sc->last_x1);
   1071   1.1  christos 		dy = (sc->last_y1 - cur_y);
   1072   1.1  christos 
   1073   1.1  christos 		dx = dx >> alps_touchpad_xy_unprecision;
   1074   1.1  christos 		dy = dy >> alps_touchpad_xy_unprecision;
   1075   1.1  christos 	}
   1076   1.1  christos 
   1077   1.1  christos 	s = spltty();
   1078   1.1  christos 	wsmouse_input(psc->sc_wsmousedev,
   1079   1.1  christos 		buttons,
   1080   1.1  christos 		dx, dy, 0, 0,
   1081   1.1  christos 		WSMOUSE_INPUT_DELTA);
   1082   1.1  christos 	splx(s);
   1083   1.1  christos 
   1084   1.1  christos 	if (sc->initializing == true || (dx != 0))
   1085   1.1  christos 		sc->last_x1 = cur_x;
   1086   1.1  christos 	if (sc->initializing == true || (dy != 0))
   1087   1.1  christos 		sc->last_y1 = cur_y;
   1088   1.1  christos 
   1089   1.1  christos 	if (sc->nfingers > 0)
   1090   1.1  christos 		sc->initializing = false;
   1091   1.1  christos 	sc->last_nfingers = sc->nfingers;
   1092   1.1  christos }
   1093   1.1  christos 
   1094   1.1  christos static void
   1095   1.1  christos pms_alps_input_v2(void *opaque, int data)
   1096   1.1  christos {
   1097   1.1  christos 	struct pms_softc *psc = opaque;
   1098   1.1  christos 
   1099   1.1  christos 	if (!psc->sc_enabled)
   1100   1.1  christos 		return;
   1101   1.1  christos 
   1102   1.1  christos 	psc->packet[psc->inputstate++] = data & 0xff;
   1103   1.1  christos 	if (psc->inputstate < 6)
   1104   1.1  christos 		return;
   1105   1.1  christos 
   1106   1.1  christos 	pms_alps_decode_touchpad_packet_v2(psc);
   1107   1.1  christos 
   1108   1.1  christos 	psc->inputstate = 0;
   1109   1.1  christos }
   1110   1.1  christos 
   1111   1.1  christos static void
   1112   1.1  christos pms_alps_input_v7(void *opaque, int data)
   1113   1.1  christos {
   1114   1.1  christos 	struct pms_softc *psc = opaque;
   1115   1.1  christos 
   1116   1.1  christos 	if (!psc->sc_enabled)
   1117   1.1  christos 		return;
   1118   1.1  christos 
   1119   1.1  christos 	psc->packet[psc->inputstate++] = data & 0xff;
   1120   1.1  christos 	if (psc->inputstate < 6)
   1121   1.1  christos 		return;
   1122   1.1  christos 
   1123   1.1  christos 	pms_alps_dispatch_packet_v7(psc);
   1124   1.1  christos 
   1125   1.1  christos 	psc->inputstate = 0;
   1126   1.1  christos }
   1127