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