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