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