Home | History | Annotate | Line # | Download | only in pckbport
pms.c revision 1.33
      1  1.33  jakllsch /* $NetBSD: pms.c,v 1.33 2011/09/08 15:26:26 jakllsch Exp $ */
      2   1.1     bjh21 
      3   1.1     bjh21 /*-
      4   1.3  christos  * Copyright (c) 2004 Kentaro Kurahone.
      5   1.3  christos  * Copyright (c) 2004 Ales Krenek.
      6   1.1     bjh21  * Copyright (c) 1994 Charles M. Hannum.
      7   1.1     bjh21  * Copyright (c) 1992, 1993 Erik Forsberg.
      8   1.1     bjh21  * All rights reserved.
      9   1.1     bjh21  *
     10   1.1     bjh21  * Redistribution and use in source and binary forms, with or without
     11   1.1     bjh21  * modification, are permitted provided that the following conditions
     12   1.1     bjh21  * are met:
     13   1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     14   1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     15   1.1     bjh21  *
     16   1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17   1.1     bjh21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18   1.1     bjh21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
     19   1.1     bjh21  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20   1.1     bjh21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21   1.1     bjh21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22   1.1     bjh21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23   1.1     bjh21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24   1.1     bjh21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25   1.1     bjh21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1     bjh21  */
     27   1.1     bjh21 
     28  1.19       dsl #include <sys/cdefs.h>
     29  1.33  jakllsch __KERNEL_RCSID(0, "$NetBSD: pms.c,v 1.33 2011/09/08 15:26:26 jakllsch Exp $");
     30  1.19       dsl 
     31   1.4       scw #include "opt_pms.h"
     32   1.4       scw 
     33   1.1     bjh21 #include <sys/param.h>
     34   1.1     bjh21 #include <sys/systm.h>
     35   1.1     bjh21 #include <sys/device.h>
     36   1.1     bjh21 #include <sys/ioctl.h>
     37   1.1     bjh21 #include <sys/kernel.h>
     38   1.1     bjh21 #include <sys/kthread.h>
     39   1.1     bjh21 
     40  1.20        ad #include <sys/bus.h>
     41   1.1     bjh21 
     42   1.1     bjh21 #include <dev/pckbport/pckbportvar.h>
     43   1.4       scw #ifdef PMS_SYNAPTICS_TOUCHPAD
     44   1.3  christos #include <dev/pckbport/synapticsvar.h>
     45   1.4       scw #endif
     46  1.27  jmcneill #ifdef PMS_ELANTECH_TOUCHPAD
     47  1.27  jmcneill #include <dev/pckbport/elantechvar.h>
     48  1.27  jmcneill #endif
     49   1.1     bjh21 
     50   1.1     bjh21 #include <dev/pckbport/pmsreg.h>
     51   1.3  christos #include <dev/pckbport/pmsvar.h>
     52   1.3  christos 
     53   1.1     bjh21 
     54   1.1     bjh21 #include <dev/wscons/wsconsio.h>
     55   1.1     bjh21 #include <dev/wscons/wsmousevar.h>
     56   1.1     bjh21 
     57   1.1     bjh21 #ifdef PMSDEBUG
     58   1.1     bjh21 int pmsdebug = 1;
     59   1.1     bjh21 #define DPRINTF(x)      if (pmsdebug) printf x
     60   1.1     bjh21 #else
     61   1.1     bjh21 #define DPRINTF(x)
     62   1.1     bjh21 #endif
     63   1.1     bjh21 
     64   1.7  jdolecek const enum pms_type tries[] = {
     65   1.3  christos 	PMS_SCROLL5, PMS_SCROLL3, PMS_STANDARD, PMS_UNKNOWN
     66   1.3  christos };
     67   1.1     bjh21 
     68   1.7  jdolecek const struct pms_protocol pms_protocols[] = {
     69   1.1     bjh21 	{ { 0, 0, 0 }, 0, "unknown protocol" },
     70   1.1     bjh21 	{ { 0, 0, 0 }, 0, "no scroll wheel (3 buttons)" },
     71   1.1     bjh21 	{ { 200, 100, 80 }, 3, "scroll wheel (3 buttons)" },
     72   1.8    rpaulo 	{ { 200, 200, 80 }, 4, "scroll wheel (5 buttons)" },
     73  1.27  jmcneill 	{ { 0, 0, 0 }, 0, "synaptics" },
     74  1.27  jmcneill 	{ { 0, 0, 0 }, 0, "elantech" }
     75   1.1     bjh21 };
     76   1.1     bjh21 
     77   1.1     bjh21 
     78  1.26      cube int pmsprobe(device_t, cfdata_t, void *);
     79  1.26      cube void pmsattach(device_t, device_t, void *);
     80   1.2     bjh21 void pmsinput(void *, int);
     81   1.1     bjh21 
     82  1.25      cube CFATTACH_DECL_NEW(pms, sizeof(struct pms_softc),
     83   1.1     bjh21     pmsprobe, pmsattach, NULL, NULL);
     84   1.1     bjh21 
     85   1.2     bjh21 static int	pms_protocol(pckbport_tag_t, pckbport_slot_t);
     86   1.2     bjh21 static void	do_enable(struct pms_softc *);
     87   1.2     bjh21 static void	do_disable(struct pms_softc *);
     88   1.2     bjh21 static void	pms_reset_thread(void*);
     89   1.2     bjh21 int	pms_enable(void *);
     90  1.17  christos int	pms_ioctl(void *, u_long, void *, int, struct lwp *);
     91   1.2     bjh21 void	pms_disable(void *);
     92  1.22  jmcneill 
     93  1.30    dyoung static bool	pms_suspend(device_t, const pmf_qual_t *);
     94  1.30    dyoung static bool	pms_resume(device_t, const pmf_qual_t *);
     95   1.1     bjh21 
     96   1.1     bjh21 const struct wsmouse_accessops pms_accessops = {
     97   1.1     bjh21 	pms_enable,
     98   1.1     bjh21 	pms_ioctl,
     99   1.1     bjh21 	pms_disable,
    100   1.1     bjh21 };
    101   1.1     bjh21 
    102   1.1     bjh21 static int
    103   1.2     bjh21 pms_protocol(pckbport_tag_t tag, pckbport_slot_t slot)
    104   1.1     bjh21 {
    105   1.1     bjh21 	u_char cmd[2], resp[1];
    106   1.1     bjh21 	int i, j, res;
    107   1.7  jdolecek 	const struct pms_protocol *p;
    108   1.1     bjh21 
    109   1.1     bjh21 	for (j = 0; j < sizeof(tries) / sizeof(tries[0]); ++j) {
    110   1.1     bjh21 		p = &pms_protocols[tries[j]];
    111   1.1     bjh21 		if (!p->rates[0])
    112   1.1     bjh21 			break;
    113   1.1     bjh21 		cmd[0] = PMS_SET_SAMPLE;
    114   1.1     bjh21 		for (i = 0; i < 3; i++) {
    115   1.1     bjh21 			cmd[1] = p->rates[i];
    116   1.1     bjh21 			res = pckbport_enqueue_cmd(tag, slot, cmd, 2, 0, 1, 0);
    117   1.1     bjh21 			if (res)
    118   1.1     bjh21 				return PMS_STANDARD;
    119   1.1     bjh21 		}
    120   1.1     bjh21 
    121   1.1     bjh21 		cmd[0] = PMS_SEND_DEV_ID;
    122   1.1     bjh21 		res = pckbport_enqueue_cmd(tag, slot, cmd, 1, 1, 1, resp);
    123   1.1     bjh21 		if (res)
    124   1.1     bjh21 			return PMS_UNKNOWN;
    125   1.1     bjh21 		if (resp[0] == p->response) {
    126   1.1     bjh21 			DPRINTF(("pms_protocol: found mouse protocol %d\n",
    127   1.1     bjh21 				tries[j]));
    128   1.1     bjh21 			return tries[j];
    129   1.1     bjh21 		}
    130   1.1     bjh21 	}
    131   1.1     bjh21 	DPRINTF(("pms_protocol: standard PS/2 protocol (no scroll wheel)\n"));
    132   1.1     bjh21 	return PMS_STANDARD;
    133   1.1     bjh21 }
    134   1.1     bjh21 
    135   1.1     bjh21 int
    136  1.26      cube pmsprobe(device_t parent, cfdata_t match, void *aux)
    137   1.1     bjh21 {
    138   1.1     bjh21 	struct pckbport_attach_args *pa = aux;
    139   1.1     bjh21 	u_char cmd[1], resp[2];
    140   1.1     bjh21 	int res;
    141   1.1     bjh21 
    142   1.1     bjh21 	if (pa->pa_slot != PCKBPORT_AUX_SLOT)
    143   1.2     bjh21 		return 0;
    144   1.1     bjh21 
    145   1.1     bjh21 	/* Flush any garbage. */
    146   1.1     bjh21 	pckbport_flush(pa->pa_tag, pa->pa_slot);
    147   1.1     bjh21 
    148   1.1     bjh21 	/* reset the device */
    149   1.1     bjh21 	cmd[0] = PMS_RESET;
    150   1.1     bjh21 	res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
    151   1.1     bjh21 	if (res) {
    152  1.28        ad 		aprint_debug("pmsprobe: reset error %d\n", res);
    153   1.2     bjh21 		return 0;
    154   1.1     bjh21 	}
    155   1.1     bjh21 	if (resp[0] != PMS_RSTDONE) {
    156   1.1     bjh21 		printf("pmsprobe: reset response 0x%x\n", resp[0]);
    157   1.2     bjh21 		return 0;
    158   1.1     bjh21 	}
    159   1.1     bjh21 
    160   1.1     bjh21 	/* get type number (0 = mouse) */
    161   1.1     bjh21 	if (resp[1] != 0) {
    162  1.28        ad 		aprint_debug("pmsprobe: type 0x%x\n", resp[1]);
    163   1.2     bjh21 		return 0;
    164   1.1     bjh21 	}
    165   1.1     bjh21 
    166   1.2     bjh21 	return 10;
    167   1.1     bjh21 }
    168   1.1     bjh21 
    169   1.1     bjh21 void
    170  1.26      cube pmsattach(device_t parent, device_t self, void *aux)
    171   1.1     bjh21 {
    172  1.10   thorpej 	struct pms_softc *sc = device_private(self);
    173   1.1     bjh21 	struct pckbport_attach_args *pa = aux;
    174   1.1     bjh21 	struct wsmousedev_attach_args a;
    175   1.3  christos 	u_char cmd[2], resp[2];
    176   1.1     bjh21 	int res;
    177   1.1     bjh21 
    178  1.25      cube 	sc->sc_dev = self;
    179   1.1     bjh21 	sc->sc_kbctag = pa->pa_tag;
    180   1.1     bjh21 	sc->sc_kbcslot = pa->pa_slot;
    181   1.1     bjh21 
    182  1.21  jmcneill 	aprint_naive("\n");
    183  1.21  jmcneill 	aprint_normal("\n");
    184   1.1     bjh21 
    185   1.1     bjh21 	/* Flush any garbage. */
    186   1.1     bjh21 	pckbport_flush(pa->pa_tag, pa->pa_slot);
    187   1.1     bjh21 
    188   1.1     bjh21 	/* reset the device */
    189   1.1     bjh21 	cmd[0] = PMS_RESET;
    190   1.1     bjh21 	res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
    191   1.1     bjh21 	if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
    192  1.28        ad 		aprint_debug("pmsattach: reset error\n");
    193   1.1     bjh21 		return;
    194   1.1     bjh21 	}
    195   1.1     bjh21 	sc->inputstate = 0;
    196   1.1     bjh21 	sc->buttons = 0;
    197   1.1     bjh21 	sc->protocol = PMS_UNKNOWN;
    198   1.1     bjh21 
    199   1.4       scw #ifdef PMS_SYNAPTICS_TOUCHPAD
    200   1.3  christos 	/* Probe for synaptics touchpad. */
    201   1.3  christos 	if (pms_synaptics_probe_init(sc) == 0) {
    202   1.3  christos 		sc->protocol = PMS_SYNAPTICS;
    203   1.4       scw 	} else
    204   1.4       scw #endif
    205  1.27  jmcneill #ifdef PMS_ELANTECH_TOUCHPAD
    206  1.27  jmcneill 	if (pms_elantech_probe_init(sc) == 0) {
    207  1.27  jmcneill 		sc->protocol = PMS_ELANTECH;
    208  1.27  jmcneill 	} else
    209  1.27  jmcneill #endif
    210   1.3  christos 		/* Install generic handler. */
    211   1.3  christos 		pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
    212  1.25      cube 		    pmsinput, sc, device_xname(sc->sc_dev));
    213   1.4       scw 
    214   1.1     bjh21 	a.accessops = &pms_accessops;
    215   1.1     bjh21 	a.accesscookie = sc;
    216   1.1     bjh21 
    217   1.1     bjh21 	/*
    218   1.1     bjh21 	 * Attach the wsmouse, saving a handle to it.
    219   1.1     bjh21 	 * Note that we don't need to check this pointer against NULL
    220   1.1     bjh21 	 * here or in pmsintr, because if this fails pms_enable() will
    221   1.1     bjh21 	 * never be called, so pmsinput() will never be called.
    222   1.1     bjh21 	 */
    223  1.25      cube 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &a, wsmousedevprint);
    224   1.1     bjh21 
    225   1.1     bjh21 	/* no interrupts until enabled */
    226   1.1     bjh21 	cmd[0] = PMS_DEV_DISABLE;
    227   1.1     bjh21 	res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 0, 0, 0);
    228   1.1     bjh21 	if (res)
    229  1.21  jmcneill 		aprint_error("pmsattach: disable error\n");
    230   1.1     bjh21 	pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
    231   1.1     bjh21 
    232  1.18        ad 	kthread_create(PRI_NONE, 0, NULL, pms_reset_thread, sc,
    233  1.31     joerg 	    &sc->sc_event_thread, "%s", device_xname(sc->sc_dev));
    234   1.1     bjh21 
    235  1.22  jmcneill 	if (!pmf_device_register(self, pms_suspend, pms_resume))
    236  1.22  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    237   1.1     bjh21 }
    238   1.1     bjh21 
    239   1.1     bjh21 static void
    240   1.2     bjh21 do_enable(struct pms_softc *sc)
    241   1.1     bjh21 {
    242   1.3  christos 	u_char cmd[2];
    243   1.1     bjh21 	int res;
    244   1.1     bjh21 
    245   1.1     bjh21 	sc->inputstate = 0;
    246   1.1     bjh21 	sc->buttons = 0;
    247   1.1     bjh21 
    248   1.1     bjh21 	pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
    249   1.1     bjh21 
    250   1.4       scw #ifdef PMS_SYNAPTICS_TOUCHPAD
    251   1.3  christos 	if (sc->protocol == PMS_SYNAPTICS)
    252   1.3  christos 		pms_synaptics_enable(sc);
    253   1.4       scw #endif
    254  1.27  jmcneill #ifdef PMS_ELANTECH_TOUCHPAD
    255  1.27  jmcneill 	if (sc->protocol == PMS_ELANTECH)
    256  1.27  jmcneill 		pms_elantech_enable(sc);
    257  1.27  jmcneill #endif
    258   1.3  christos 
    259   1.1     bjh21 	cmd[0] = PMS_DEV_ENABLE;
    260   1.3  christos 	res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
    261   1.3  christos 	    1, 0, 1, 0);
    262   1.1     bjh21 	if (res)
    263  1.21  jmcneill 		aprint_error("pms_enable: command error %d\n", res);
    264   1.1     bjh21 
    265   1.1     bjh21 	if (sc->protocol == PMS_UNKNOWN)
    266   1.1     bjh21 		sc->protocol = pms_protocol(sc->sc_kbctag, sc->sc_kbcslot);
    267   1.1     bjh21 	DPRINTF(("pms_enable: using %s protocol\n",
    268   1.1     bjh21 	    pms_protocols[sc->protocol].name));
    269   1.1     bjh21 #if 0
    270   1.1     bjh21 	{
    271   1.1     bjh21 		u_char scmd[2];
    272   1.1     bjh21 
    273   1.1     bjh21 		scmd[0] = PMS_SET_RES;
    274   1.1     bjh21 		scmd[1] = 3; /* 8 counts/mm */
    275   1.1     bjh21 		res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
    276   1.1     bjh21 		    2, 0, 1, 0);
    277   1.1     bjh21 		if (res)
    278   1.1     bjh21 			printf("pms_enable: setup error1 (%d)\n", res);
    279   1.1     bjh21 
    280   1.1     bjh21 		scmd[0] = PMS_SET_SCALE21;
    281   1.1     bjh21 		res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
    282   1.1     bjh21 		    1, 0, 1, 0);
    283   1.1     bjh21 		if (res)
    284   1.1     bjh21 			printf("pms_enable: setup error2 (%d)\n", res);
    285   1.1     bjh21 
    286   1.1     bjh21 		scmd[0] = PMS_SET_SAMPLE;
    287   1.1     bjh21 		scmd[1] = 100; /* 100 samples/sec */
    288   1.1     bjh21 		res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
    289   1.1     bjh21 		    2, 0, 1, 0);
    290   1.1     bjh21 		if (res)
    291   1.1     bjh21 			printf("pms_enable: setup error3 (%d)\n", res);
    292   1.1     bjh21 	}
    293   1.1     bjh21 #endif
    294   1.1     bjh21 }
    295   1.1     bjh21 
    296   1.1     bjh21 static void
    297   1.2     bjh21 do_disable(struct pms_softc *sc)
    298   1.1     bjh21 {
    299   1.1     bjh21 	u_char cmd[1];
    300   1.1     bjh21 	int res;
    301   1.1     bjh21 
    302   1.1     bjh21 	cmd[0] = PMS_DEV_DISABLE;
    303   1.3  christos 	res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
    304   1.3  christos 	    1, 0, 1, 0);
    305   1.1     bjh21 	if (res)
    306  1.21  jmcneill 		aprint_error("pms_disable: command error\n");
    307   1.1     bjh21 
    308   1.1     bjh21 	pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
    309   1.1     bjh21 }
    310   1.1     bjh21 
    311   1.1     bjh21 int
    312   1.2     bjh21 pms_enable(void *v)
    313   1.1     bjh21 {
    314   1.1     bjh21 	struct pms_softc *sc = v;
    315   1.1     bjh21 	int s;
    316   1.1     bjh21 
    317   1.1     bjh21 	if (sc->sc_enabled)
    318   1.1     bjh21 		return EBUSY;
    319   1.1     bjh21 
    320   1.1     bjh21 	do_enable(sc);
    321   1.1     bjh21 
    322   1.1     bjh21 	s = spltty();
    323   1.1     bjh21 	sc->sc_enabled = 1;
    324   1.1     bjh21 	splx(s);
    325   1.1     bjh21 
    326   1.1     bjh21 	return 0;
    327   1.1     bjh21 }
    328   1.1     bjh21 
    329   1.1     bjh21 void
    330   1.2     bjh21 pms_disable(void *v)
    331   1.1     bjh21 {
    332   1.1     bjh21 	struct pms_softc *sc = v;
    333   1.1     bjh21 	int s;
    334   1.1     bjh21 
    335   1.1     bjh21 	do_disable(sc);
    336   1.1     bjh21 
    337   1.1     bjh21 	s = spltty();
    338   1.1     bjh21 	sc->sc_enabled = 0;
    339   1.1     bjh21 	splx(s);
    340   1.1     bjh21 }
    341   1.1     bjh21 
    342  1.22  jmcneill static bool
    343  1.30    dyoung pms_suspend(device_t dv, const pmf_qual_t *qual)
    344  1.22  jmcneill {
    345  1.22  jmcneill 	struct pms_softc *sc = device_private(dv);
    346  1.22  jmcneill 
    347  1.22  jmcneill 	if (sc->sc_enabled)
    348  1.22  jmcneill 		do_disable(sc);
    349  1.22  jmcneill 
    350  1.22  jmcneill 	return true;
    351  1.22  jmcneill }
    352  1.22  jmcneill 
    353  1.22  jmcneill static bool
    354  1.30    dyoung pms_resume(device_t dv, const pmf_qual_t *qual)
    355   1.1     bjh21 {
    356  1.22  jmcneill 	struct pms_softc *sc = device_private(dv);
    357   1.1     bjh21 
    358   1.4       scw #ifdef PMS_SYNAPTICS_TOUCHPAD
    359  1.22  jmcneill 	if (sc->protocol == PMS_SYNAPTICS) {
    360  1.22  jmcneill 		pms_synaptics_resume(sc);
    361  1.23  jmcneill 		if (sc->sc_enabled) {
    362  1.23  jmcneill 			do_enable(sc);
    363  1.23  jmcneill 		}
    364  1.22  jmcneill 	} else
    365   1.4       scw #endif
    366  1.27  jmcneill #ifdef PMS_ELANTECH_TOUCHPAD
    367  1.27  jmcneill 	if (sc->protocol == PMS_ELANTECH) {
    368  1.27  jmcneill 		pms_elantech_resume(sc);
    369  1.27  jmcneill 		if (sc->sc_enabled) {
    370  1.27  jmcneill 			do_enable(sc);
    371  1.27  jmcneill 		}
    372  1.27  jmcneill 	} else
    373  1.27  jmcneill #endif
    374  1.22  jmcneill 	if (sc->sc_enabled) {
    375  1.22  jmcneill 		/* recheck protocol & init mouse */
    376  1.22  jmcneill 		sc->protocol = PMS_UNKNOWN;
    377  1.22  jmcneill 		do_enable(sc); /* only if we were suspended */
    378   1.1     bjh21 	}
    379  1.22  jmcneill 
    380  1.22  jmcneill 	return true;
    381   1.1     bjh21 }
    382   1.1     bjh21 
    383   1.1     bjh21 int
    384  1.17  christos pms_ioctl(void *v, u_long cmd, void *data, int flag,
    385  1.16  christos     struct lwp *l)
    386   1.1     bjh21 {
    387   1.1     bjh21 	struct pms_softc *sc = v;
    388   1.1     bjh21 	u_char kbcmd[2];
    389   1.1     bjh21 	int i;
    390   1.1     bjh21 
    391   1.1     bjh21 	switch (cmd) {
    392   1.1     bjh21 	case WSMOUSEIO_GTYPE:
    393   1.1     bjh21 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    394   1.1     bjh21 		break;
    395   1.6     perry 
    396   1.1     bjh21 	case WSMOUSEIO_SRES:
    397   1.1     bjh21 		i = (*(u_int *)data - 12) / 25;
    398   1.6     perry 
    399   1.1     bjh21 		if (i < 0)
    400   1.1     bjh21 			i = 0;
    401   1.6     perry 
    402   1.1     bjh21 		if (i > 3)
    403   1.1     bjh21 			i = 3;
    404   1.1     bjh21 
    405   1.1     bjh21 		kbcmd[0] = PMS_SET_RES;
    406   1.6     perry 		kbcmd[1] = i;
    407   1.6     perry 		i = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, kbcmd,
    408   1.1     bjh21 		    2, 0, 1, 0);
    409   1.6     perry 
    410   1.1     bjh21 		if (i)
    411   1.1     bjh21 			printf("pms_ioctl: SET_RES command error\n");
    412   1.1     bjh21 		break;
    413   1.3  christos 
    414   1.1     bjh21 	default:
    415   1.2     bjh21 		return EPASSTHROUGH;
    416   1.1     bjh21 	}
    417   1.2     bjh21 	return 0;
    418   1.1     bjh21 }
    419   1.1     bjh21 
    420   1.1     bjh21 static void
    421   1.2     bjh21 pms_reset_thread(void *arg)
    422   1.1     bjh21 {
    423   1.1     bjh21 	struct pms_softc *sc = arg;
    424   1.1     bjh21 	u_char cmd[1], resp[2];
    425   1.1     bjh21 	int res;
    426   1.1     bjh21 	int save_protocol;
    427   1.1     bjh21 
    428   1.1     bjh21 	for (;;) {
    429   1.1     bjh21 		tsleep(&sc->sc_enabled, PWAIT, "pmsreset", 0);
    430   1.1     bjh21 #ifdef PMSDEBUG
    431   1.1     bjh21 		if (pmsdebug)
    432   1.1     bjh21 #endif
    433   1.1     bjh21 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
    434  1.25      cube 			aprint_debug_dev(sc->sc_dev,
    435  1.25      cube 			    "resetting mouse interface\n");
    436   1.1     bjh21 #endif
    437   1.1     bjh21 		save_protocol = sc->protocol;
    438   1.1     bjh21 		pms_disable(sc);
    439   1.1     bjh21 		cmd[0] = PMS_RESET;
    440   1.3  christos 		res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
    441   1.3  christos 		    1, 2, 1, resp);
    442  1.12  christos 		if (res) {
    443  1.25      cube 			DPRINTF(("%s: reset error %d\n",
    444  1.25      cube 			    device_xname(sc->sc_dev), res));
    445  1.12  christos 		}
    446   1.3  christos 
    447  1.27  jmcneill 		/* For the synaptics and elantech case, leave the protocol alone. */
    448  1.27  jmcneill 		if (sc->protocol != PMS_SYNAPTICS && sc->protocol != PMS_ELANTECH)
    449   1.3  christos 			sc->protocol = PMS_UNKNOWN;
    450   1.4       scw 
    451   1.1     bjh21 		pms_enable(sc);
    452   1.1     bjh21 		if (sc->protocol != save_protocol) {
    453   1.1     bjh21 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
    454  1.25      cube 			aprint_verbose_dev(sc->sc_dev,
    455  1.25      cube 			    "protocol change, sleeping and retrying\n");
    456   1.1     bjh21 #endif
    457   1.1     bjh21 			pms_disable(sc);
    458   1.1     bjh21 			cmd[0] = PMS_RESET;
    459   1.1     bjh21 			res = pckbport_enqueue_cmd(sc->sc_kbctag,
    460   1.1     bjh21 			    sc->sc_kbcslot, cmd, 1, 2, 1, resp);
    461  1.12  christos 			if (res) {
    462   1.1     bjh21 				DPRINTF(("%s: reset error %d\n",
    463  1.25      cube 				    device_xname(sc->sc_dev), res));
    464  1.12  christos 			}
    465   1.1     bjh21 			tsleep(pms_reset_thread, PWAIT, "pmsreset", hz);
    466   1.1     bjh21 			cmd[0] = PMS_RESET;
    467   1.1     bjh21 			res = pckbport_enqueue_cmd(sc->sc_kbctag,
    468   1.1     bjh21 			    sc->sc_kbcslot, cmd, 1, 2, 1, resp);
    469  1.12  christos 			if (res) {
    470   1.1     bjh21 				DPRINTF(("%s: reset error %d\n",
    471  1.25      cube 				    device_xname(sc->sc_dev), res));
    472  1.12  christos 			}
    473   1.1     bjh21 			sc->protocol = PMS_UNKNOWN;	/* reprobe protocol */
    474   1.1     bjh21 			pms_enable(sc);
    475   1.1     bjh21 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
    476   1.1     bjh21 			if (sc->protocol != save_protocol) {
    477   1.1     bjh21 				printf("%s: protocol changed.\n",
    478  1.25      cube 				    device_xname(sc->sc_dev));
    479   1.1     bjh21 			}
    480   1.1     bjh21 #endif
    481   1.1     bjh21 		}
    482   1.1     bjh21 	}
    483   1.1     bjh21 }
    484   1.1     bjh21 
    485   1.1     bjh21 /* Masks for the first byte of a packet */
    486   1.1     bjh21 #define PMS_LBUTMASK 0x01
    487   1.1     bjh21 #define PMS_RBUTMASK 0x02
    488   1.1     bjh21 #define PMS_MBUTMASK 0x04
    489   1.1     bjh21 #define PMS_4BUTMASK 0x10
    490   1.1     bjh21 #define PMS_5BUTMASK 0x20
    491   1.1     bjh21 
    492   1.1     bjh21 void
    493   1.2     bjh21 pmsinput(void *vsc, int data)
    494   1.1     bjh21 {
    495   1.1     bjh21 	struct pms_softc *sc = vsc;
    496   1.1     bjh21 	u_int changed;
    497   1.1     bjh21 	int dx, dy, dz = 0;
    498   1.1     bjh21 	int newbuttons = 0;
    499   1.1     bjh21 
    500   1.1     bjh21 	if (!sc->sc_enabled) {
    501  1.33  jakllsch 		/* Interrupts are not expected. Discard the byte. */
    502   1.1     bjh21 		return;
    503   1.1     bjh21 	}
    504   1.1     bjh21 
    505  1.11    kardel 	getmicrouptime(&sc->current);
    506   1.1     bjh21 
    507   1.1     bjh21 	if (sc->inputstate > 0) {
    508   1.1     bjh21 		struct timeval diff;
    509   1.1     bjh21 
    510   1.1     bjh21 		timersub(&sc->current, &sc->last, &diff);
    511   1.1     bjh21 		/*
    512   1.1     bjh21 		 * Empirically, the delay should be about 1700us on a standard
    513   1.1     bjh21 		 * PS/2 port.  I have seen delays as large as 4500us (rarely)
    514   1.1     bjh21 		 * in regular use.  When using a confused mouse, I generally
    515   1.1     bjh21 		 * see delays at least as large as 30,000us.  -seebs
    516   1.1     bjh21 		 *
    517   1.1     bjh21 		 * The thinkpad trackball returns at 22-23ms. So we use
    518   1.1     bjh21 		 * >= 40ms. In the future, I'll implement adaptable timeout
    519   1.1     bjh21 		 * by increasing the timeout if the mouse reset happens
    520   1.1     bjh21 		 * too frequently -christos
    521   1.1     bjh21 		 */
    522   1.1     bjh21 		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
    523   1.1     bjh21 			DPRINTF(("pms_input: unusual delay (%ld.%06ld s), "
    524   1.1     bjh21 			    "scheduling reset\n",
    525   1.1     bjh21 			    (long)diff.tv_sec, (long)diff.tv_usec));
    526   1.1     bjh21 			sc->inputstate = 0;
    527   1.1     bjh21 			sc->sc_enabled = 0;
    528   1.1     bjh21 			wakeup(&sc->sc_enabled);
    529   1.1     bjh21 			return;
    530   1.1     bjh21 		}
    531   1.1     bjh21 	}
    532   1.1     bjh21 	sc->last = sc->current;
    533   1.1     bjh21 
    534   1.1     bjh21 	if (sc->inputstate == 0) {
    535   1.1     bjh21 		/*
    536   1.2     bjh21 		 * Some devices (seen on trackballs anytime, and on
    537   1.2     bjh21 		 * some mice shortly after reset) output garbage bytes
    538   1.2     bjh21 		 * between packets.  Just ignore them.
    539   1.1     bjh21 		 */
    540   1.1     bjh21 		if ((data & 0xc0) != 0)
    541   1.1     bjh21 			return;	/* not in sync yet, discard input */
    542   1.1     bjh21 	}
    543   1.1     bjh21 
    544   1.1     bjh21 	sc->packet[sc->inputstate++] = data & 0xff;
    545   1.1     bjh21 	switch (sc->inputstate) {
    546   1.1     bjh21 	case 0:
    547   1.1     bjh21 		/* no useful processing can be done yet */
    548   1.1     bjh21 		break;
    549   1.1     bjh21 
    550   1.1     bjh21 	case 1:
    551   1.1     bjh21 		/*
    552   1.1     bjh21 		 * Why should we test for bit 0x8 and insist on it here?
    553   1.1     bjh21 		 * The old (psm.c and psm_intelli.c) drivers didn't do
    554   1.1     bjh21 		 * it, and there are devices where it does harm (that's
    555   1.1     bjh21 		 * why it is not used if using PMS_STANDARD protocol).
    556   1.1     bjh21 		 * Anyway, it does not to cause any harm to accept packets
    557   1.1     bjh21 		 * without this bit.
    558   1.1     bjh21 		 */
    559   1.1     bjh21 #if 0
    560   1.1     bjh21 		if (sc->protocol == PMS_STANDARD)
    561   1.1     bjh21 			break;
    562   1.1     bjh21 		if (!(sc->packet[0] & 0x8)) {
    563   1.1     bjh21 			DPRINTF(("pmsinput: 0x8 not set in first byte "
    564   1.1     bjh21 			    "[0x%02x], resetting\n", sc->packet[0]));
    565   1.1     bjh21 			sc->inputstate = 0;
    566   1.1     bjh21 			sc->sc_enabled = 0;
    567   1.1     bjh21 			wakeup(&sc->sc_enabled);
    568   1.1     bjh21 			return;
    569   1.1     bjh21 		}
    570   1.1     bjh21 #endif
    571   1.1     bjh21 		break;
    572   1.1     bjh21 
    573   1.1     bjh21 	case 2:
    574   1.1     bjh21 		break;
    575   1.1     bjh21 
    576   1.1     bjh21 	case 4:
    577   1.1     bjh21 		/* Case 4 is a superset of case 3. This is *not* an accident. */
    578   1.1     bjh21 		if (sc->protocol == PMS_SCROLL3) {
    579   1.1     bjh21 			dz = sc->packet[3];
    580   1.1     bjh21 			if (dz >= 128)
    581   1.1     bjh21 				dz -= 256;
    582   1.1     bjh21 			if (dz == -128)
    583   1.1     bjh21 				dz = -127;
    584   1.1     bjh21 		} else if (sc->protocol == PMS_SCROLL5) {
    585   1.1     bjh21 			dz = sc->packet[3] & 0xf;
    586   1.1     bjh21 			if (dz >= 8)
    587   1.1     bjh21 				dz -= 16;
    588  1.33  jakllsch 			if (sc->packet[3] & PMS_4BUTMASK)
    589   1.1     bjh21 				newbuttons |= 0x8;
    590  1.33  jakllsch 			if (sc->packet[3] & PMS_5BUTMASK)
    591   1.1     bjh21 				newbuttons |= 0x10;
    592   1.1     bjh21 		} else {
    593   1.1     bjh21 			DPRINTF(("pmsinput: why am I looking at this byte?\n"));
    594   1.1     bjh21 			dz = 0;
    595   1.1     bjh21 		}
    596   1.1     bjh21 		/* FALLTHROUGH */
    597   1.1     bjh21 	case 3:
    598   1.1     bjh21 		/*
    599   1.1     bjh21 		 * This is only an endpoint for scroll protocols with 4
    600   1.1     bjh21 		 * bytes, or the standard protocol with 3.
    601   1.1     bjh21 		 */
    602   1.1     bjh21 		if (sc->protocol != PMS_STANDARD && sc->inputstate == 3)
    603   1.1     bjh21 			break;
    604   1.1     bjh21 
    605   1.1     bjh21 		newbuttons |= ((sc->packet[0] & PMS_LBUTMASK) ? 0x1 : 0) |
    606   1.1     bjh21 		    ((sc->packet[0] & PMS_MBUTMASK) ? 0x2 : 0) |
    607   1.1     bjh21 		    ((sc->packet[0] & PMS_RBUTMASK) ? 0x4 : 0);
    608   1.1     bjh21 
    609   1.1     bjh21 		dx = sc->packet[1];
    610   1.1     bjh21 		if (dx >= 128)
    611   1.1     bjh21 			dx -= 256;
    612   1.1     bjh21 		if (dx == -128)
    613   1.1     bjh21 			dx = -127;
    614   1.1     bjh21 
    615   1.1     bjh21 		dy = sc->packet[2];
    616   1.1     bjh21 		if (dy >= 128)
    617   1.1     bjh21 			dy -= 256;
    618   1.1     bjh21 		if (dy == -128)
    619   1.1     bjh21 			dy = -127;
    620   1.6     perry 
    621   1.1     bjh21 		sc->inputstate = 0;
    622   1.1     bjh21 		changed = (sc->buttons ^ newbuttons);
    623   1.1     bjh21 		sc->buttons = newbuttons;
    624   1.1     bjh21 
    625   1.1     bjh21 #ifdef PMSDEBUG
    626   1.1     bjh21 		if (sc->protocol == PMS_STANDARD) {
    627   1.1     bjh21 			DPRINTF(("pms: packet: 0x%02x%02x%02x\n",
    628   1.1     bjh21 			    sc->packet[0], sc->packet[1], sc->packet[2]));
    629   1.1     bjh21 		} else {
    630   1.1     bjh21 			DPRINTF(("pms: packet: 0x%02x%02x%02x%02x\n",
    631   1.1     bjh21 			    sc->packet[0], sc->packet[1], sc->packet[2],
    632   1.1     bjh21 			    sc->packet[3]));
    633   1.1     bjh21 		}
    634   1.1     bjh21 #endif
    635   1.1     bjh21 		if (dx || dy || dz || changed) {
    636   1.1     bjh21 #ifdef PMSDEBUG
    637   1.1     bjh21 			DPRINTF(("pms: x %+03d y %+03d z %+03d "
    638   1.1     bjh21 			    "buttons 0x%02x\n",	dx, dy, dz, sc->buttons));
    639   1.1     bjh21 #endif
    640   1.1     bjh21 			wsmouse_input(sc->sc_wsmousedev,
    641  1.15    plunky 			    sc->buttons, dx, dy, dz, 0,
    642   1.1     bjh21 			    WSMOUSE_INPUT_DELTA);
    643   1.1     bjh21 		}
    644   1.1     bjh21 		memset(sc->packet, 0, 4);
    645   1.1     bjh21 		break;
    646   1.1     bjh21 
    647   1.1     bjh21 	/* If we get here, we have problems. */
    648   1.1     bjh21 	default:
    649   1.1     bjh21 		printf("pmsinput: very confused.  resetting.\n");
    650   1.1     bjh21 		sc->inputstate = 0;
    651   1.1     bjh21 		sc->sc_enabled = 0;
    652   1.1     bjh21 		wakeup(&sc->sc_enabled);
    653   1.1     bjh21 		return;
    654   1.1     bjh21 	}
    655   1.1     bjh21 }
    656