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