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 * 3-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 #define VUID_BUT(b) BUT((b*2)+1)
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * VUID_BUT(0) BUT(1) LEFT BUTTON
400Sstevel@tonic-gate * VUID_BUT(1) BUT(3) RIGHT BUTTON
410Sstevel@tonic-gate */
420Sstevel@tonic-gate
430Sstevel@tonic-gate #define MOUSE_BUTTON_L (uchar_t)(0x20) /* Left button pressed */
440Sstevel@tonic-gate #define MOUSE_BUTTON_R (uchar_t)(0x10) /* Right button pressed */
450Sstevel@tonic-gate
460Sstevel@tonic-gate #define MOUSE_START_CODE (uchar_t)(0x40) /* Start code in char */
470Sstevel@tonic-gate
480Sstevel@tonic-gate #define MOUSE_START 0 /* Beginning of packet */
490Sstevel@tonic-gate #define MOUSE_BUTTON 1 /* Got button status */
500Sstevel@tonic-gate #define MOUSE_DELTA_X 2 /* got delta X */
510Sstevel@tonic-gate
520Sstevel@tonic-gate extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
530Sstevel@tonic-gate
540Sstevel@tonic-gate int
VUID_OPEN(queue_t * const qp)550Sstevel@tonic-gate VUID_OPEN(queue_t *const qp)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate STATEP->nbuttons = 2;
580Sstevel@tonic-gate
590Sstevel@tonic-gate return (0);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate
620Sstevel@tonic-gate static void
vuidm3p_sendButtonEvent(queue_t * const qp)630Sstevel@tonic-gate vuidm3p_sendButtonEvent(queue_t *const qp)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate int b;
660Sstevel@tonic-gate
670Sstevel@tonic-gate if ((STATEP->buttons == 0x30) && (!STATEP->oldbuttons)) {
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * both buttons going down simultaneously means button
700Sstevel@tonic-gate * two going down
710Sstevel@tonic-gate */
720Sstevel@tonic-gate vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 1);
730Sstevel@tonic-gate return;
740Sstevel@tonic-gate } else if ((!STATEP->buttons) && (STATEP->oldbuttons == 0x30)) {
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * both buttons going up simultaneously means button
770Sstevel@tonic-gate * two going up
780Sstevel@tonic-gate */
790Sstevel@tonic-gate vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 0);
800Sstevel@tonic-gate return;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate * for each button, see if it has changed
850Sstevel@tonic-gate */
860Sstevel@tonic-gate for (b = 0; b < 2; b++) {
870Sstevel@tonic-gate uchar_t mask = 0x20 >> b;
880Sstevel@tonic-gate
890Sstevel@tonic-gate if ((STATEP->buttons & mask) != (STATEP->oldbuttons & mask))
900Sstevel@tonic-gate VUID_PUTNEXT(qp, VUID_BUT(b), FE_PAIR_NONE, 0,
916990Sgd78059 (STATEP->buttons & mask ? 1 : 0));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate void
vuidm3p(queue_t * const qp,mblk_t * mp)960Sstevel@tonic-gate vuidm3p(queue_t *const qp, mblk_t *mp)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate int r, code;
990Sstevel@tonic-gate uchar_t *bufp;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate bufp = mp->b_rptr;
1026990Sgd78059 r = MBLKL(mp);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate for (r--; r >= 0; r--) {
1050Sstevel@tonic-gate code = *bufp++;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /* strip the high-order bit (mouse sends 7-bit data) */
1080Sstevel@tonic-gate code &= 0x7f;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate switch (STATEP->state) {
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Start state. We stay here if the start code is not
1140Sstevel@tonic-gate * received thus forcing us back into sync. When we
1150Sstevel@tonic-gate * get a start code the button mask comes with it
1160Sstevel@tonic-gate * forcing us to the next state.
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate default:
1200Sstevel@tonic-gate case MOUSE_START:
1210Sstevel@tonic-gate start_code:
1220Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /* look for sync */
1250Sstevel@tonic-gate if ((code & MOUSE_START_CODE) == 0)
1260Sstevel@tonic-gate break;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate STATEP->buttons = code & 0x30;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (STATEP->buttons != STATEP->oldbuttons) {
1310Sstevel@tonic-gate vuidm3p_sendButtonEvent(qp);
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * remember state
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate STATEP->oldbuttons = STATEP->buttons;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * bits 0 & 1 are bits 6 & 7 of X value
1400Sstevel@tonic-gate * (Sign extend them with the cast.)
1410Sstevel@tonic-gate */
1420Sstevel@tonic-gate STATEP->deltax = (signed char)((code & 0x03) << 6);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * bits 2 & 3 are bits 6 & 7 of Y value
1460Sstevel@tonic-gate * (Sign extend them with the cast.)
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate STATEP->deltay = (signed char)((code & 0x0c) << 4);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * go to the next state
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate STATEP->state = MOUSE_BUTTON;
1540Sstevel@tonic-gate break;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * We receive the remaining 6 bits of delta x, forcing
1580Sstevel@tonic-gate * us to the next state. We just piece the value of
1590Sstevel@tonic-gate * delta x together.
1600Sstevel@tonic-gate */
1610Sstevel@tonic-gate case MOUSE_BUTTON:
1620Sstevel@tonic-gate if (code & MOUSE_START_CODE) {
1630Sstevel@tonic-gate STATEP->state = MOUSE_START;
1640Sstevel@tonic-gate goto start_code; /* restart */
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate STATEP->deltax |= code & 0x3f;
1680Sstevel@tonic-gate STATEP->state = MOUSE_DELTA_X;
1690Sstevel@tonic-gate break;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * The last part of delta Y, and the packet
1730Sstevel@tonic-gate * *may be* complete
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate case MOUSE_DELTA_X:
1760Sstevel@tonic-gate if (code & MOUSE_START_CODE) {
1770Sstevel@tonic-gate STATEP->state = MOUSE_START;
1780Sstevel@tonic-gate goto start_code; /* restart */
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate STATEP->deltay |= code & 0x3f;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * generate motion Event
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate if (STATEP->deltax)
1870Sstevel@tonic-gate VUID_PUTNEXT(qp, (uchar_t)LOC_X_DELTA,
1880Sstevel@tonic-gate FE_PAIR_ABSOLUTE, (uchar_t)LOC_X_ABSOLUTE,
1890Sstevel@tonic-gate STATEP->deltax);
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate if (STATEP->deltay)
1920Sstevel@tonic-gate VUID_PUTNEXT(qp, (uchar_t)LOC_Y_DELTA,
1930Sstevel@tonic-gate FE_PAIR_ABSOLUTE, (uchar_t)LOC_Y_ABSOLUTE,
1940Sstevel@tonic-gate -STATEP->deltay);
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate STATEP->deltax = STATEP->deltay = 0;
1970Sstevel@tonic-gate break;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate freemsg(mp);
2020Sstevel@tonic-gate }
203