summaryrefslogtreecommitdiff
path: root/shared/vmmouse_client.c
blob: c5d529e2b9fe75018f9d946ba39318b3e49a2ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright 2002-2006 by VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */

/*
 * vmmouse_client.c --
 *
 *      VMware Virtual Mouse Client
 *
 * This module provides functions to enable, operate and process
 * packets via the VMMouse module hosted in the VMX.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "vmmouse_client.h"
#include "vmmouse_proto.h"

/*
 *----------------------------------------------------------------------------
 *
 * VMMouseClientVMCheck --
 *
 *      Checks if we're running in a VM by sending the GETVERSION command.
 *
 * Returns:
 *      0 if we're running natively/the version command failed,
 *      1 if we're in a VM.
 *
 *----------------------------------------------------------------------------
 */

static Bool
VMMouseClientVMCheck(void)
{
   VMMouseProtoCmd vmpc;

   vmpc.in.vEbx = ~VMMOUSE_PROTO_MAGIC;
   vmpc.in.command = VMMOUSE_PROTO_CMD_GETVERSION;
   VMMouseProto_SendCmd(&vmpc);

   /*
    * ebx should contain VMMOUSE_PROTO_MAGIC
    * eax should contain version
    */
   if (vmpc.out.vEbx != VMMOUSE_PROTO_MAGIC || vmpc.out.vEax == 0xffffffff) {
      return FALSE;
   }

   return TRUE;
}


/*
 *----------------------------------------------------------------------
 *
 * VMMouseClient_Disable --
 *
 *	Tries to disable VMMouse communication mode on the host.
 *	The caller is responsible for maintaining state (we don't check
 *	if we're enabled before attempting to disable the VMMouse).
 *
 * Results:
 *	TRUE if we successfully disable the VMMouse communication mode,
 *	FALSE if something went wrong.
 *
 * Side effects:
 *	Disables the absolute positioning mode.
 *
 *----------------------------------------------------------------------
 */

void
VMMouseClient_Disable(void)
{
   uint32_t status;
   VMMouseProtoCmd vmpc;

   VMwareLog(("VMMouseClient_Disable: writing disable command to port\n"));
   vmpc.in.vEbx = VMMOUSE_CMD_DISABLE;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND;
   VMMouseProto_SendCmd(&vmpc);
   /*
    * We should get 0xffff in the flags now.
    */
   vmpc.in.vEbx = 0;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_STATUS;
   VMMouseProto_SendCmd(&vmpc);
   status = vmpc.out.vEax;
   if ((status & VMMOUSE_ERROR) != VMMOUSE_ERROR) {
      VMwareLog(("VMMouseClient_Disable: wrong status returned\n"));
   }
}


/*
 *----------------------------------------------------------------------
 *
 * VMMouseClient_Enable --
 *
 *	Public Enable entry point. The driver calls this once it feels
 *	ready to deal with VMMouse stuff. For now, we just try to enable
 *	and return the result, but conceivably we could do more.
 *
 * Results:
 *	TRUE if the enable succeeds, FALSE otherwise.
 *
 * Side effects:
 *	Causes host-side state change.
 *
 *----------------------------------------------------------------------
 */

Bool
VMMouseClient_Enable(void) {

   uint32_t status;
   uint32_t data;
   VMMouseProtoCmd vmpc;

   /*
    * First, make sure we're in a VM; i.e. in dualboot configurations we might
    * find ourselves running on real hardware.
    */

   if (!VMMouseClientVMCheck()) {
      return FALSE;
   }

   VMwareLog(("VMMouseClientVMCheck succeeded, checking VMMOUSE version\n"));
   VMwareLog(("VMMouseClient_Enable: READ_ID 0x%08x, VERSION_ID 0x%08x\n",
       VMMOUSE_CMD_READ_ID, VMMOUSE_VERSION_ID));

   /*
    * We probe for the VMMouse backend by sending the ENABLE
    * command to the mouse. We should get back the VERSION_ID on
    * the data port.
    */
   vmpc.in.vEbx = VMMOUSE_CMD_READ_ID;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND;
   VMMouseProto_SendCmd(&vmpc);

   /*
    * Check whether the VMMOUSE_VERSION_ID is available to read
    */
   vmpc.in.vEbx = 0;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_STATUS;
   VMMouseProto_SendCmd(&vmpc);
   status = vmpc.out.vEax;
   if ((status & 0x0000ffff) == 0) {
      VMwareLog(("VMMouseClient_Enable: no data on port."));
      return FALSE;
   }

   /*
    * Get the VMMOUSE_VERSION_ID then
    */
   /* Get just one item */
   vmpc.in.vEbx = 1;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_DATA;
   VMMouseProto_SendCmd(&vmpc);
   data = vmpc.out.vEax;
   if (data!= VMMOUSE_VERSION_ID) {
      VMwareLog(("VMMouseClient_Enable: data was not VERSION_ID"));
      return FALSE;
   }

   /*
    * Restrict access to the VMMouse backdoor handler.
    */
   vmpc.in.vEbx = VMMOUSE_RESTRICT_IOPL;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_RESTRICT;
   VMMouseProto_SendCmd(&vmpc);

   /*
    * To quote Jeremy, "Go Go Go!"
    */

   VMwareLog(("VMMouseClient_Enable: go go go!\n"));
   return TRUE;
}


/*
 *----------------------------------------------------------------------
 *
 * VMMouseClient_GetInput --
 *
 *	Retrieves a 4-word input packet from the VMMouse data port and
 *	stores it in the specified input structure.
 *
 * Results:
 *	The number of packets in the queue, including the retrieved
 *	packet.
 *
 * Side effects:
 *	Could cause host state change.
 *
 *----------------------------------------------------------------------
 */

unsigned int
VMMouseClient_GetInput (PVMMOUSE_INPUT_DATA pvmmouseInput) {

   uint32_t status;
   uint16_t numWords;
   uint32_t packetInfo;
   VMMouseProtoCmd vmpc;

   /*
    * The status dword has two parts: the high 16 bits are
    * for flags, the low 16-bits are the number of DWORDs
    * waiting in the data queue. VMMOUSE_ERROR is a special
    * case that indicates there's something wrong on the
    * host end, e.g. the VMMouse was disabled on the host-side.
    */
   vmpc.in.vEbx = 0;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_STATUS;
   VMMouseProto_SendCmd(&vmpc);
   status = vmpc.out.vEax;
   if ((status & VMMOUSE_ERROR) == VMMOUSE_ERROR) {
      VMwareLog(("VMMouseClient_GetInput: VMMOUSE_ERROR status, abort!\n"));
      return VMMOUSE_ERROR;
   }

   /*
    * We don't use the status flags, just get the words
    */
   numWords = status & 0x0000ffff;

   if ((numWords % 4) != 0) {
      VMwareLog(("VMMouseClient_GetInput: invalid status numWords, abort!\n"));
      return (0);
   }

   if (numWords == 0) {
      return (0);
   }

   /*
    * The VMMouse uses a 4-dword packet protocol:
    *	DWORD 0: Button State and per-packet flags
    *	DWORD 1: X position (absolute or relative)
    *	DWORD 2: Y position (absolute or relative)
    *	DWORD 3: Z position (relative)
    */
   /* Get 4 items at once */
   vmpc.in.vEbx = 4;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_DATA;
   VMMouseProto_SendCmd(&vmpc);
   packetInfo = vmpc.out.vEax;
   pvmmouseInput->Flags = (packetInfo & 0xffff0000) >> 16;
   pvmmouseInput->Buttons = (packetInfo & 0x0000ffff);

   /* Note that Z is always signed, and X/Y are signed in relative mode. */
   pvmmouseInput->X = (int)vmpc.out.vEbx;
   pvmmouseInput->Y = (int)vmpc.out.vEcx;
   pvmmouseInput->Z = (int)vmpc.out.vEdx;

   /*
    * Return number of packets (including this one) in queue.
    */
   return (numWords >> 2);
}


/*
 *----------------------------------------------------------------------------
 *
 * VMMouseClient_RequestRelative --
 *
 *      Request that the host switch to posting relative packets. It's just
 *      advisory, so we make no guarantees about if/when the switch will
 *      happen.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Host may start posting relative packets in the near future.
 *
 *----------------------------------------------------------------------------
 */

void
VMMouseClient_RequestRelative(void)
{
   VMMouseProtoCmd vmpc;

   VMwareLog(("VMMouseClient: requesting relative mode\n"));
   vmpc.in.vEbx = VMMOUSE_CMD_REQUEST_RELATIVE;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND;
   VMMouseProto_SendCmd(&vmpc);
}


/*
 *----------------------------------------------------------------------------
 *
 * VMMouseClient_RequestAbsolute --
 *
 *      Request that the host switch to posting absolute packets. It's just
 *      advisory, so we make no guarantees about if/when the switch will
 *      happen.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Host may start posting absolute packets in the near future.
 *
 *----------------------------------------------------------------------------
 */

void
VMMouseClient_RequestAbsolute(void)
{
   VMMouseProtoCmd vmpc;

   VMwareLog(("VMMouseClient: requesting absolute mode\n"));
   vmpc.in.vEbx = VMMOUSE_CMD_REQUEST_ABSOLUTE;
   vmpc.in.command = VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND;
   VMMouseProto_SendCmd(&vmpc);
}