10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56990Sgd78059 * Common Development and Distribution License (the "License").
66990Sgd78059 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9840Sgdamore@opensolaris.org * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * 4-Byte Mouse Protocol
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/stream.h>
326990Sgd78059 #include <sys/strsun.h>
330Sstevel@tonic-gate #include <sys/vuid_event.h>
34*9840Sgdamore@opensolaris.org #include "vuidmice.h"
350Sstevel@tonic-gate
360Sstevel@tonic-gate #ifdef VUIDM4P_DEBUG
370Sstevel@tonic-gate #define VBUF_SIZE 511
380Sstevel@tonic-gate static unsigned char vuidm4p_buf[VBUF_SIZE+1];
390Sstevel@tonic-gate static int vuidm4p_ptr = 0;
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate
420Sstevel@tonic-gate #define VUID_BUT(b) BUT((b*2)+1)
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * VUID_BUT(0) BUT(1) LEFT BUTTON
460Sstevel@tonic-gate * VUID_BUT(1) BUT(3) RIGHT BUTTON
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define MOUSE_NUMBUTTONS 3 /* Number of buttons */
500Sstevel@tonic-gate #define MOUSE_BUTTON_M (uchar_t)(0x40) /* Middle button */
510Sstevel@tonic-gate #define MOUSE_BUTTON_L (uchar_t)(0x20) /* Left button */
520Sstevel@tonic-gate #define MOUSE_BUTTON_R (uchar_t)(0x10) /* Right button */
530Sstevel@tonic-gate
540Sstevel@tonic-gate #define MOUSE_START_CODE (uchar_t)(0x40) /* Start code in char */
550Sstevel@tonic-gate
560Sstevel@tonic-gate #define MOUSE_START 0 /* Beginning of packet */
570Sstevel@tonic-gate #define MOUSE_BUTTON 1 /* Got button status */
580Sstevel@tonic-gate #define MOUSE_DELTA_X 2 /* got delta X */
590Sstevel@tonic-gate #define MOUSE_DELTA_Y 3 /* got delta Y */
600Sstevel@tonic-gate
610Sstevel@tonic-gate extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
620Sstevel@tonic-gate
630Sstevel@tonic-gate int
VUID_OPEN(queue_t * const qp)640Sstevel@tonic-gate VUID_OPEN(queue_t *const qp)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * The current kdmconfig tables imply that this module can be used
680Sstevel@tonic-gate * for both 2- and 3- button mice, so based on that evidence we
690Sstevel@tonic-gate * can't assume a constant. It should be possible to autodetect
700Sstevel@tonic-gate * based on the mouse's startup behavior - "M" means 2 buttons,
710Sstevel@tonic-gate * "M3" means 3 buttons - but that's for another day.
720Sstevel@tonic-gate */
730Sstevel@tonic-gate STATEP->nbuttons = 0; /* Don't know. */
740Sstevel@tonic-gate
750Sstevel@tonic-gate return (0);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate static void
vuidm4p_sendButtonEvent(queue_t * const qp)790Sstevel@tonic-gate vuidm4p_sendButtonEvent(queue_t *const qp)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate int b;
820Sstevel@tonic-gate
830Sstevel@tonic-gate /* for the LEFT and RIGHT button, see if it has changed */
840Sstevel@tonic-gate for (b = 0; b < 2; b++) {
850Sstevel@tonic-gate uchar_t mask = 0x20 >> b;
860Sstevel@tonic-gate
870Sstevel@tonic-gate if ((STATEP->buttons & mask) != (STATEP->oldbuttons & mask))
880Sstevel@tonic-gate VUID_PUTNEXT(qp, VUID_BUT(b), FE_PAIR_NONE, 0,
890Sstevel@tonic-gate ((STATEP->buttons & mask) ? 1 : 0));
900Sstevel@tonic-gate }
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate void
vuidm4p(queue_t * const qp,mblk_t * mp)940Sstevel@tonic-gate vuidm4p(queue_t *const qp, mblk_t *mp)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate int r, code;
970Sstevel@tonic-gate unsigned char *bufp;
980Sstevel@tonic-gate
990Sstevel@tonic-gate bufp = mp->b_rptr;
1006990Sgd78059 r = MBLKL(mp);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate for (r--; r >= 0; r--) {
1030Sstevel@tonic-gate code = *bufp++;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate #ifdef VUIDM4P_DEBUG
1060Sstevel@tonic-gate vuidm4p_buf[vuidm4p_ptr] = code;
1070Sstevel@tonic-gate vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
1080Sstevel@tonic-gate #endif
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (code & MOUSE_START_CODE) {
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * sync it here
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate STATEP->state = MOUSE_START;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate switch (STATEP->state) {
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * Start state. We stay here if the start code is not
1200Sstevel@tonic-gate * received thus forcing us back into sync. When we
1210Sstevel@tonic-gate * get a start code the button mask comes with it
1220Sstevel@tonic-gate * forcing us to the next state.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate default:
1250Sstevel@tonic-gate case MOUSE_START:
1260Sstevel@tonic-gate /* look for sync */
1270Sstevel@tonic-gate if ((code & MOUSE_START_CODE) == 0)
1280Sstevel@tonic-gate break;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
1310Sstevel@tonic-gate STATEP->buttons = 0;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /* Get the new state for the LEFT & RIGHT Button */
1340Sstevel@tonic-gate STATEP->buttons |=
1356990Sgd78059 (code & (MOUSE_BUTTON_L | MOUSE_BUTTON_R));
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * bits 0 & 1 are bits 6 & 7 of X value
1390Sstevel@tonic-gate * (Sign extend them with the cast.)
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate STATEP->deltax = (signed char)((code & 0x03) << 6);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * bits 2 & 3 are bits 6 & 7 of Y value
1450Sstevel@tonic-gate * (Sign extend them with the cast.)
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate STATEP->deltay = (signed char)((code & 0x0c) << 4);
1480Sstevel@tonic-gate STATEP->state = MOUSE_BUTTON;
1490Sstevel@tonic-gate /* go to the next state */
1500Sstevel@tonic-gate break;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate case MOUSE_BUTTON:
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * We receive the remaining 6 bits of delta x,
1550Sstevel@tonic-gate * forcing us to the next state. We just piece the
1560Sstevel@tonic-gate * value of delta x together.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate STATEP->deltax |= code & 0x3f;
1590Sstevel@tonic-gate STATEP->state = MOUSE_DELTA_X;
1600Sstevel@tonic-gate break;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * The last part of delta Y, and the packet *may be*
1640Sstevel@tonic-gate * complete
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate case MOUSE_DELTA_X:
1670Sstevel@tonic-gate STATEP->deltay |= code & 0x3f;
1680Sstevel@tonic-gate STATEP->state = MOUSE_DELTA_Y;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate STATEP->buttons |=
1716990Sgd78059 (STATEP->oldbuttons & MOUSE_BUTTON_M);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate * If we can peek at the next two mouse characters,
1750Sstevel@tonic-gate * and neither of them is the start of the next
1760Sstevel@tonic-gate * packet, don't use this packet.
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate if (r > 1 && !(bufp[0] & MOUSE_START_CODE) &&
1790Sstevel@tonic-gate !(bufp[1] & MOUSE_START_CODE)) {
1800Sstevel@tonic-gate STATEP->state = MOUSE_START;
1810Sstevel@tonic-gate break;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate if (STATEP->buttons != STATEP->oldbuttons) {
1850Sstevel@tonic-gate vuidm4p_sendButtonEvent(qp);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * remember state
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate STATEP->oldbuttons = STATEP->buttons;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * generate motion Events for delta_x
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate if (STATEP->deltax)
1970Sstevel@tonic-gate VUID_PUTNEXT(qp,
1980Sstevel@tonic-gate (uchar_t)LOC_X_DELTA, FE_PAIR_ABSOLUTE,
1990Sstevel@tonic-gate (uchar_t)LOC_X_ABSOLUTE, STATEP->deltax);
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Reverse the Sign for DELTA_Y
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if (STATEP->deltay)
2040Sstevel@tonic-gate VUID_PUTNEXT(qp,
2050Sstevel@tonic-gate (uchar_t)LOC_Y_DELTA, FE_PAIR_ABSOLUTE,
2060Sstevel@tonic-gate (uchar_t)LOC_Y_ABSOLUTE, -STATEP->deltay);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /* allow us to keep looking for an optional 4th byte */
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate case MOUSE_DELTA_Y:
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * We've seen delta Y. If we do NOT have the sync
2160Sstevel@tonic-gate * bit set, this indicates the middle button's status.
2170Sstevel@tonic-gate */
2180Sstevel@tonic-gate STATEP->state = MOUSE_START;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * if we're here, the fourth byte is indeed present
2220Sstevel@tonic-gate * to indicate something with the middle button.
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * If we can peek at the next mouse character, and
2270Sstevel@tonic-gate * its not the start of the next packet, don't use
2280Sstevel@tonic-gate * this packet.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate if (r > 0 && !(bufp[0] & MOUSE_START_CODE))
2310Sstevel@tonic-gate break;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * Check if the byte is a valid middle button state.
2350Sstevel@tonic-gate * It must either be 0x00 or 0x20 only.
2360Sstevel@tonic-gate */
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * Get the new state for the MIDDLE Button
2400Sstevel@tonic-gate * Left button set in 4th byte indicates that the
2410Sstevel@tonic-gate * middle button is pressed, cleared means it
2420Sstevel@tonic-gate * has been released.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate if (code == MOUSE_BUTTON_L)
2450Sstevel@tonic-gate STATEP->buttons |= MOUSE_BUTTON_M;
2460Sstevel@tonic-gate else if (code == 0)
2470Sstevel@tonic-gate STATEP->buttons &= ~MOUSE_BUTTON_M;
2480Sstevel@tonic-gate else {
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * Invalid data in the 4th byte of the packet.
2510Sstevel@tonic-gate * Skip this byte.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate #ifdef VUIDM4P_DEBUG
2540Sstevel@tonic-gate vuidm4p_break();
2550Sstevel@tonic-gate #endif
2560Sstevel@tonic-gate break;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * generate an Event with the middle button's status
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate if (STATEP->oldbuttons != STATEP->buttons) {
2630Sstevel@tonic-gate VUID_PUTNEXT(qp,
2640Sstevel@tonic-gate (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0,
2650Sstevel@tonic-gate ((STATEP->buttons & MOUSE_BUTTON_M) ?
2660Sstevel@tonic-gate 1 : 0));
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate * remember state
2710Sstevel@tonic-gate */
2720Sstevel@tonic-gate STATEP->oldbuttons = STATEP->buttons;
2730Sstevel@tonic-gate break;
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate freemsg(mp);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate #ifdef VUIDM4P_DEBUG
2810Sstevel@tonic-gate int
vuidm4p_break()2820Sstevel@tonic-gate vuidm4p_break()
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate char buf[VBUF_SIZE+1];
2850Sstevel@tonic-gate int i;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate for (i = 0; i <= VBUF_SIZE; i++) {
2880Sstevel@tonic-gate buf[i] - vuidm4p_buf[vuidm4p_ptr];
2890Sstevel@tonic-gate vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate for (i = 0; i <= VBUF_SIZE; i++) {
2930Sstevel@tonic-gate vuidm4p_buf[i] = buf[i];
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate #endif
297