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