Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / vuidmice / vuidm4p.c
bloba9522d0824d5b16d5490dfc74d2e50563a6efb5c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * 4-Byte Mouse Protocol
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"
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
42 #define VUID_BUT(b) BUT((b*2)+1)
45 * VUID_BUT(0) BUT(1) LEFT BUTTON
46 * VUID_BUT(1) BUT(3) RIGHT BUTTON
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 */
54 #define MOUSE_START_CODE (uchar_t)(0x40) /* Start code in char */
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 */
61 extern void VUID_PUTNEXT(queue_t *const, uchar_t, uchar_t, uchar_t, int);
63 int
64 VUID_OPEN(queue_t *const qp)
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.
73 STATEP->nbuttons = 0; /* Don't know. */
75 return (0);
78 static void
79 vuidm4p_sendButtonEvent(queue_t *const qp)
81 int b;
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;
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));
93 void
94 vuidm4p(queue_t *const qp, mblk_t *mp)
96 int r, code;
97 unsigned char *bufp;
99 bufp = mp->b_rptr;
100 r = MBLKL(mp);
102 for (r--; r >= 0; r--) {
103 code = *bufp++;
105 #ifdef VUIDM4P_DEBUG
106 vuidm4p_buf[vuidm4p_ptr] = code;
107 vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
108 #endif
110 if (code & MOUSE_START_CODE) {
112 * sync it here
114 STATEP->state = MOUSE_START;
117 switch (STATEP->state) {
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.
124 default:
125 case MOUSE_START:
126 /* look for sync */
127 if ((code & MOUSE_START_CODE) == 0)
128 break;
130 STATEP->deltax = STATEP->deltay = 0;
131 STATEP->buttons = 0;
133 /* Get the new state for the LEFT & RIGHT Button */
134 STATEP->buttons |=
135 (code & (MOUSE_BUTTON_L | MOUSE_BUTTON_R));
138 * bits 0 & 1 are bits 6 & 7 of X value
139 * (Sign extend them with the cast.)
141 STATEP->deltax = (signed char)((code & 0x03) << 6);
144 * bits 2 & 3 are bits 6 & 7 of Y value
145 * (Sign extend them with the cast.)
147 STATEP->deltay = (signed char)((code & 0x0c) << 4);
148 STATEP->state = MOUSE_BUTTON;
149 /* go to the next state */
150 break;
152 case MOUSE_BUTTON:
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.
158 STATEP->deltax |= code & 0x3f;
159 STATEP->state = MOUSE_DELTA_X;
160 break;
163 * The last part of delta Y, and the packet *may be*
164 * complete
166 case MOUSE_DELTA_X:
167 STATEP->deltay |= code & 0x3f;
168 STATEP->state = MOUSE_DELTA_Y;
170 STATEP->buttons |=
171 (STATEP->oldbuttons & MOUSE_BUTTON_M);
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.
178 if (r > 1 && !(bufp[0] & MOUSE_START_CODE) &&
179 !(bufp[1] & MOUSE_START_CODE)) {
180 STATEP->state = MOUSE_START;
181 break;
184 if (STATEP->buttons != STATEP->oldbuttons) {
185 vuidm4p_sendButtonEvent(qp);
189 * remember state
191 STATEP->oldbuttons = STATEP->buttons;
194 * generate motion Events for delta_x
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);
201 * Reverse the Sign for DELTA_Y
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);
208 STATEP->deltax = STATEP->deltay = 0;
210 /* allow us to keep looking for an optional 4th byte */
211 break;
213 case MOUSE_DELTA_Y:
215 * We've seen delta Y. If we do NOT have the sync
216 * bit set, this indicates the middle button's status.
218 STATEP->state = MOUSE_START;
221 * if we're here, the fourth byte is indeed present
222 * to indicate something with the middle button.
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.
230 if (r > 0 && !(bufp[0] & MOUSE_START_CODE))
231 break;
234 * Check if the byte is a valid middle button state.
235 * It must either be 0x00 or 0x20 only.
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.
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 {
250 * Invalid data in the 4th byte of the packet.
251 * Skip this byte.
253 #ifdef VUIDM4P_DEBUG
254 vuidm4p_break();
255 #endif
256 break;
260 * generate an Event with the middle button's status
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));
270 * remember state
272 STATEP->oldbuttons = STATEP->buttons;
273 break;
276 freemsg(mp);
280 #ifdef VUIDM4P_DEBUG
282 vuidm4p_break()
284 char buf[VBUF_SIZE+1];
285 int i;
287 for (i = 0; i <= VBUF_SIZE; i++) {
288 buf[i] - vuidm4p_buf[vuidm4p_ptr];
289 vuidm4p_ptr = ((vuidm4p_ptr + 1) & VBUF_SIZE);
292 for (i = 0; i <= VBUF_SIZE; i++) {
293 vuidm4p_buf[i] = buf[i];
296 #endif