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