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