xref: /titanic_51/usr/src/uts/common/io/vuidmice/vuidm4p.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * 4-Byte Mouse Protocol
28  */
29 
30 #include <sys/param.h>
31 #include <sys/stream.h>
32 #include <sys/strsun.h>
33 #include <sys/vuid_event.h>
34 #include "vuidmice.h"
35 
36 #ifdef	VUIDM4P_DEBUG
37 #define	VBUF_SIZE	511
38 static unsigned char vuidm4p_buf[VBUF_SIZE+1];
39 static int vuidm4p_ptr = 0;
40 #endif
41 
42 #define	VUID_BUT(b)		BUT((b*2)+1)
43 
44 /*
45  * VUID_BUT(0)	BUT(1)		LEFT  BUTTON
46  * VUID_BUT(1)	BUT(3)		RIGHT BUTTON
47  */
48 
49 #define	MOUSE_NUMBUTTONS	3		/* Number of buttons	*/
50 #define	MOUSE_BUTTON_M		(uchar_t)(0x40)	/* Middle button */
51 #define	MOUSE_BUTTON_L		(uchar_t)(0x20)	/* Left   button */
52 #define	MOUSE_BUTTON_R		(uchar_t)(0x10)	/* Right  button */
53 
54 #define	MOUSE_START_CODE	(uchar_t)(0x40)	/* Start code in char	*/
55 
56 #define	MOUSE_START		0		/* Beginning of packet	*/
57 #define	MOUSE_BUTTON		1		/* Got button status	*/
58 #define	MOUSE_DELTA_X		2		/* got delta X		*/
59 #define	MOUSE_DELTA_Y		3		/* got delta Y		*/
60 
61 extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
62 
63 int
64 VUID_OPEN(queue_t *const qp)
65 {
66 	/*
67 	 * The current kdmconfig tables imply that this module can be used
68 	 * for both 2- and 3- button mice, so based on that evidence we
69 	 * can't assume a constant.  It should be possible to autodetect
70 	 * based on the mouse's startup behavior - "M" means 2 buttons,
71 	 * "M3" means 3 buttons - but that's for another day.
72 	 */
73 	STATEP->nbuttons = 0;	/* Don't know. */
74 
75 	return (0);
76 }
77 
78 static void
79 vuidm4p_sendButtonEvent(queue_t *const qp)
80 {
81 	int b;
82 
83 	/* for the LEFT and RIGHT button, see if it has changed */
84 	for (b = 0; b < 2; b++) {
85 		uchar_t mask = 0x20 >> b;
86 
87 		if ((STATEP->buttons & mask) != (STATEP->oldbuttons & mask))
88 			VUID_PUTNEXT(qp, VUID_BUT(b), FE_PAIR_NONE, 0,
89 			    ((STATEP->buttons & mask) ? 1 : 0));
90 	}
91 }
92 
93 void
94 vuidm4p(queue_t *const qp, mblk_t *mp)
95 {
96 	int r, code;
97 	unsigned char *bufp;
98 
99 	bufp = mp->b_rptr;
100 	r = MBLKL(mp);
101 
102 	for (r--; r >= 0; r--) {
103 		code = *bufp++;
104 
105 #ifdef	VUIDM4P_DEBUG
106 		vuidm4p_buf[vuidm4p_ptr] = code;
107 		vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
108 #endif
109 
110 		if (code & MOUSE_START_CODE) {
111 			/*
112 			 * sync it here
113 			 */
114 			STATEP->state = MOUSE_START;
115 		}
116 
117 		switch (STATEP->state) {
118 			/*
119 			 * Start state. We stay here if the start code is not
120 			 * received thus forcing us back into sync. When we
121 			 * get a start code the	button mask comes with it
122 			 * forcing us to the next state.
123 			 */
124 		default:
125 		case MOUSE_START:
126 			/* look for sync */
127 			if ((code & MOUSE_START_CODE) == 0)
128 				break;
129 
130 			STATEP->deltax = STATEP->deltay = 0;
131 			STATEP->buttons = 0;
132 
133 			/* Get the new state for the LEFT & RIGHT Button */
134 			STATEP->buttons |=
135 			    (code & (MOUSE_BUTTON_L | MOUSE_BUTTON_R));
136 
137 			/*
138 			 * bits 0 & 1 are bits 6 & 7 of X value
139 			 * (Sign extend them with the cast.)
140 			 */
141 			STATEP->deltax = (signed char)((code & 0x03) << 6);
142 
143 			/*
144 			 * bits 2 & 3 are bits 6 & 7 of Y value
145 			 * (Sign extend them with the cast.)
146 			 */
147 			STATEP->deltay = (signed char)((code & 0x0c) << 4);
148 			STATEP->state = MOUSE_BUTTON;
149 				/* go to the next state */
150 			break;
151 
152 		case MOUSE_BUTTON:
153 			/*
154 			 * We receive the remaining 6 bits of delta x,
155 			 * forcing us to the next state. We just piece the
156 			 * value of delta x together.
157 			 */
158 			STATEP->deltax |= code & 0x3f;
159 			STATEP->state = MOUSE_DELTA_X;
160 			break;
161 
162 			/*
163 			 * The last part of delta Y, and the packet *may be*
164 			 * complete
165 			 */
166 		case MOUSE_DELTA_X:
167 			STATEP->deltay |= code & 0x3f;
168 			STATEP->state = MOUSE_DELTA_Y;
169 
170 			STATEP->buttons |=
171 			    (STATEP->oldbuttons & MOUSE_BUTTON_M);
172 
173 			/*
174 			 * If we can peek at the next two mouse characters,
175 			 * and  neither  of  them  is  the start of the next
176 			 * packet, don't use this packet.
177 			 */
178 			if (r > 1 && !(bufp[0] & MOUSE_START_CODE) &&
179 			    !(bufp[1] & MOUSE_START_CODE)) {
180 				STATEP->state = MOUSE_START;
181 				break;
182 			}
183 
184 			if (STATEP->buttons != STATEP->oldbuttons) {
185 				vuidm4p_sendButtonEvent(qp);
186 			}
187 
188 			/*
189 			 * remember state
190 			 */
191 			STATEP->oldbuttons = STATEP->buttons;
192 
193 			/*
194 			 * generate motion Events for delta_x
195 			 */
196 			if (STATEP->deltax)
197 				VUID_PUTNEXT(qp,
198 				    (uchar_t)LOC_X_DELTA, FE_PAIR_ABSOLUTE,
199 				    (uchar_t)LOC_X_ABSOLUTE, STATEP->deltax);
200 			/*
201 			 * Reverse the Sign for DELTA_Y
202 			 */
203 			if (STATEP->deltay)
204 				VUID_PUTNEXT(qp,
205 				    (uchar_t)LOC_Y_DELTA, FE_PAIR_ABSOLUTE,
206 				    (uchar_t)LOC_Y_ABSOLUTE, -STATEP->deltay);
207 
208 			STATEP->deltax = STATEP->deltay = 0;
209 
210 			/* allow us to keep looking for an optional 4th byte */
211 			break;
212 
213 		case MOUSE_DELTA_Y:
214 			/*
215 			 * We've seen delta Y.  If we do NOT have the sync
216 			 * bit set, this indicates the middle button's status.
217 			 */
218 			STATEP->state = MOUSE_START;
219 
220 			/*
221 			 * if we're here, the fourth byte is indeed present
222 			 * to indicate something with the middle button.
223 			 */
224 
225 			/*
226 			 * If we can peek at the next mouse character, and
227 			 * its not the start of the next packet, don't use
228 			 * this packet.
229 			 */
230 			if (r > 0 && !(bufp[0] & MOUSE_START_CODE))
231 				break;
232 
233 			/*
234 			 * Check if the byte is a valid middle button state.
235 			 * It must either be 0x00 or 0x20 only.
236 			 */
237 
238 			/*
239 			 * Get the new state for the MIDDLE Button
240 			 * Left button set in 4th byte indicates that the
241 			 * middle button is pressed, cleared means it
242 			 * has been released.
243 			 */
244 			if (code == MOUSE_BUTTON_L)
245 				STATEP->buttons |= MOUSE_BUTTON_M;
246 			else if (code == 0)
247 				STATEP->buttons &= ~MOUSE_BUTTON_M;
248 			else {
249 				/*
250 				 * Invalid data in the 4th byte of the packet.
251 				 * Skip this byte.
252 				 */
253 #ifdef VUIDM4P_DEBUG
254 				vuidm4p_break();
255 #endif
256 				break;
257 			}
258 
259 			/*
260 			 * generate an Event with the middle button's status
261 			 */
262 			if (STATEP->oldbuttons != STATEP->buttons) {
263 				VUID_PUTNEXT(qp,
264 				    (uchar_t)MS_MIDDLE, FE_PAIR_NONE, 0,
265 				    ((STATEP->buttons & MOUSE_BUTTON_M) ?
266 				    1 : 0));
267 			}
268 
269 			/*
270 			 * remember state
271 			 */
272 			STATEP->oldbuttons = STATEP->buttons;
273 			break;
274 		}
275 	}
276 	freemsg(mp);
277 }
278 
279 
280 #ifdef	VUIDM4P_DEBUG
281 int
282 vuidm4p_break()
283 {
284 	char buf[VBUF_SIZE+1];
285 	int i;
286 
287 	for (i = 0; i <= VBUF_SIZE; i++) {
288 		buf[i] - vuidm4p_buf[vuidm4p_ptr];
289 		vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
290 	}
291 
292 	for (i = 0; i <= VBUF_SIZE; i++) {
293 		vuidm4p_buf[i] = buf[i];
294 	}
295 }
296 #endif
297