xref: /onnv-gate/usr/src/uts/common/io/vuidmice/vuidm3p.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * 3-Byte Mouse Protocol
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/stream.h>
35*0Sstevel@tonic-gate #include <sys/vuid_event.h>
36*0Sstevel@tonic-gate #include <sys/vuidmice.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #define	VUID_BUT(b)		BUT((b*2)+1)
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * VUID_BUT(0)	BUT(1)		LEFT  BUTTON
42*0Sstevel@tonic-gate  * VUID_BUT(1)	BUT(3)		RIGHT BUTTON
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #define	MOUSE_BUTTON_L		(uchar_t)(0x20)	/* Left button pressed	*/
46*0Sstevel@tonic-gate #define	MOUSE_BUTTON_R		(uchar_t)(0x10)	/* Right button pressed	*/
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	MOUSE_START_CODE	(uchar_t)(0x40)	/* Start code in char	*/
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	MOUSE_START		0		/* Beginning of packet	*/
51*0Sstevel@tonic-gate #define	MOUSE_BUTTON		1		/* Got button status	*/
52*0Sstevel@tonic-gate #define	MOUSE_DELTA_X		2		/* got delta X		*/
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate int
57*0Sstevel@tonic-gate VUID_OPEN(queue_t *const qp)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	STATEP->nbuttons = 2;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	return (0);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void
65*0Sstevel@tonic-gate vuidm3p_sendButtonEvent(queue_t *const qp)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	int b;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if ((STATEP->buttons == 0x30) && (!STATEP->oldbuttons)) {
70*0Sstevel@tonic-gate 		/*
71*0Sstevel@tonic-gate 		 * both buttons going down simultaneously means button
72*0Sstevel@tonic-gate 		 * two going down
73*0Sstevel@tonic-gate 		 */
74*0Sstevel@tonic-gate 		vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 1);
75*0Sstevel@tonic-gate 		return;
76*0Sstevel@tonic-gate 	} else if ((!STATEP->buttons) && (STATEP->oldbuttons == 0x30)) {
77*0Sstevel@tonic-gate 		/*
78*0Sstevel@tonic-gate 		 * both buttons going up simultaneously means button
79*0Sstevel@tonic-gate 		 * two going up
80*0Sstevel@tonic-gate 		 */
81*0Sstevel@tonic-gate 		vuidm3p_putnext(qp, (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0, 0);
82*0Sstevel@tonic-gate 		return;
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * for each button, see if it has changed
87*0Sstevel@tonic-gate 	 */
88*0Sstevel@tonic-gate 	for (b = 0; b < 2; b++) {
89*0Sstevel@tonic-gate 		uchar_t mask = 0x20 >> b;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 		if ((STATEP->buttons & mask) != (STATEP->oldbuttons & mask))
92*0Sstevel@tonic-gate 			VUID_PUTNEXT(qp, VUID_BUT(b), FE_PAIR_NONE, 0,
93*0Sstevel@tonic-gate 				(STATEP->buttons & mask ? 1 : 0));
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate void
98*0Sstevel@tonic-gate vuidm3p(queue_t *const qp, mblk_t *mp)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	int r, code;
101*0Sstevel@tonic-gate 	uchar_t *bufp;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	bufp = mp->b_rptr;
104*0Sstevel@tonic-gate 	r = mp->b_wptr - mp->b_rptr;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	for (r--; r >= 0; r--) {
107*0Sstevel@tonic-gate 		code = *bufp++;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		/* strip the high-order bit (mouse sends 7-bit data) */
110*0Sstevel@tonic-gate 		code &= 0x7f;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		switch (STATEP->state) {
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		/*
115*0Sstevel@tonic-gate 		 * Start state. We stay here if the start code is not
116*0Sstevel@tonic-gate 		 * received thus forcing us back into sync. When we
117*0Sstevel@tonic-gate 		 * get a start code the	button mask comes with it
118*0Sstevel@tonic-gate 		 * forcing us to the next state.
119*0Sstevel@tonic-gate 		 */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		default:
122*0Sstevel@tonic-gate 		case MOUSE_START:
123*0Sstevel@tonic-gate start_code:
124*0Sstevel@tonic-gate 			STATEP->deltax = STATEP->deltay = 0;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 			/* look for sync */
127*0Sstevel@tonic-gate 			if ((code & MOUSE_START_CODE) == 0)
128*0Sstevel@tonic-gate 				break;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 			STATEP->buttons = code & 0x30;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 			if (STATEP->buttons != STATEP->oldbuttons) {
133*0Sstevel@tonic-gate 				vuidm3p_sendButtonEvent(qp);
134*0Sstevel@tonic-gate 				/*
135*0Sstevel@tonic-gate 				 * remember state
136*0Sstevel@tonic-gate 				 */
137*0Sstevel@tonic-gate 				STATEP->oldbuttons = STATEP->buttons;
138*0Sstevel@tonic-gate 			}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 			/*
141*0Sstevel@tonic-gate 			 * bits 0 & 1 are bits 6 & 7 of X value
142*0Sstevel@tonic-gate 			 * (Sign extend them with the cast.)
143*0Sstevel@tonic-gate 			 */
144*0Sstevel@tonic-gate 			STATEP->deltax = (signed char)((code & 0x03) << 6);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			/*
147*0Sstevel@tonic-gate 			 * bits 2 & 3 are bits 6 & 7 of Y value
148*0Sstevel@tonic-gate 			 * (Sign extend them with the cast.)
149*0Sstevel@tonic-gate 			 */
150*0Sstevel@tonic-gate 			STATEP->deltay = (signed char)((code & 0x0c) << 4);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 			/*
153*0Sstevel@tonic-gate 			 * go to the next state
154*0Sstevel@tonic-gate 			 */
155*0Sstevel@tonic-gate 			STATEP->state = MOUSE_BUTTON;
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 			/*
159*0Sstevel@tonic-gate 			 * We receive the remaining 6 bits of delta x, forcing
160*0Sstevel@tonic-gate 			 * us to the next state. We just piece the value of
161*0Sstevel@tonic-gate 			 * delta x together.
162*0Sstevel@tonic-gate 			 */
163*0Sstevel@tonic-gate 		case MOUSE_BUTTON:
164*0Sstevel@tonic-gate 			if (code & MOUSE_START_CODE) {
165*0Sstevel@tonic-gate 				STATEP->state = MOUSE_START;
166*0Sstevel@tonic-gate 				goto start_code;	/* restart */
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 			STATEP->deltax |= code & 0x3f;
170*0Sstevel@tonic-gate 			STATEP->state = MOUSE_DELTA_X;
171*0Sstevel@tonic-gate 			break;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 			/*
174*0Sstevel@tonic-gate 			 * The last part of delta Y, and the packet
175*0Sstevel@tonic-gate 			 * *may be* complete
176*0Sstevel@tonic-gate 			 */
177*0Sstevel@tonic-gate 		case MOUSE_DELTA_X:
178*0Sstevel@tonic-gate 			if (code & MOUSE_START_CODE) {
179*0Sstevel@tonic-gate 				STATEP->state = MOUSE_START;
180*0Sstevel@tonic-gate 				goto start_code;	/* restart */
181*0Sstevel@tonic-gate 			}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 			STATEP->deltay |= code & 0x3f;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 			/*
186*0Sstevel@tonic-gate 			 * generate motion Event
187*0Sstevel@tonic-gate 			 */
188*0Sstevel@tonic-gate 			if (STATEP->deltax)
189*0Sstevel@tonic-gate 				VUID_PUTNEXT(qp, (uchar_t)LOC_X_DELTA,
190*0Sstevel@tonic-gate 				    FE_PAIR_ABSOLUTE, (uchar_t)LOC_X_ABSOLUTE,
191*0Sstevel@tonic-gate 				    STATEP->deltax);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 			if (STATEP->deltay)
194*0Sstevel@tonic-gate 				VUID_PUTNEXT(qp, (uchar_t)LOC_Y_DELTA,
195*0Sstevel@tonic-gate 				    FE_PAIR_ABSOLUTE, (uchar_t)LOC_Y_ABSOLUTE,
196*0Sstevel@tonic-gate 				    -STATEP->deltay);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 			STATEP->deltax = STATEP->deltay = 0;
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	freemsg(mp);
204*0Sstevel@tonic-gate }
205