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