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