VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/DevPS2M.cpp@ 82192

Last change on this file since 82192 was 82191, checked in by vboxsync, 5 years ago

DevPS2: Moved XlateAT2PC to the DevPS2.cpp file. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.9 KB
Line 
1/* $Id: DevPS2M.cpp 82191 2019-11-25 17:48:55Z vboxsync $ */
2/** @file
3 * PS2M - PS/2 auxiliary device (mouse) emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * References:
20 *
21 * The Undocumented PC (2nd Ed.), Frank van Gilluwe, Addison-Wesley, 1996.
22 * IBM TrackPoint System Version 4.0 Engineering Specification, 1999.
23 * ELAN Microelectronics eKM8025 USB & PS/2 Mouse Controller, 2006.
24 *
25 *
26 * Notes:
27 *
28 * - The auxiliary device commands are very similar to keyboard commands.
29 * Most keyboard commands which do not specifically deal with the keyboard
30 * (enable, disable, reset) have identical counterparts.
31 * - The code refers to 'auxiliary device' and 'mouse'; these terms are not
32 * quite interchangeable. 'Auxiliary device' is used when referring to the
33 * generic PS/2 auxiliary device interface and 'mouse' when referring to
34 * a mouse attached to the auxiliary port.
35 * - The basic modes of operation are reset, stream, and remote. Those are
36 * mutually exclusive. Stream and remote modes can additionally have wrap
37 * mode enabled.
38 * - The auxiliary device sends unsolicited data to the host only when it is
39 * both in stream mode and enabled. Otherwise it only responds to commands.
40 *
41 *
42 * There are three report packet formats supported by the emulated device. The
43 * standard three-byte PS/2 format (with middle button support), IntelliMouse
44 * four-byte format with added scroll wheel, and IntelliMouse Explorer four-byte
45 * format with reduced scroll wheel range but two additional buttons. Note that
46 * the first three bytes of the report are always the same.
47 *
48 * Upon reset, the mouse is always in the standard PS/2 mode. A special 'knock'
49 * sequence can be used to switch to ImPS/2 or ImEx mode. Three consecutive
50 * Set Sampling Rate (0F3h) commands with arguments 200, 100, 80 switch to ImPS/2
51 * mode. While in ImPS/2 or PS/2 mode, three consecutive Set Sampling Rate
52 * commands with arguments 200, 200, 80 switch to ImEx mode. The Read ID (0F2h)
53 * command will report the currently selected protocol.
54 *
55 * There is an extended ImEx mode with support for horizontal scrolling. It is
56 * entered from ImEx mode with a 200, 80, 40 sequence of Set Sampling Rate
57 * commands. It does not change the reported protocol (it remains 4, or ImEx)
58 * but changes the meaning of the 4th byte.
59 *
60 *
61 * Standard PS/2 pointing device three-byte report packet format:
62 *
63 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
64 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
65 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
66 * | Byte 1 | Y ovfl | X ovfl | Y sign | X sign | Sync | M btn | R btn | L btn |
67 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
68 * | Byte 2 | X movement delta (two's complement) |
69 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
70 * | Byte 3 | Y movement delta (two's complement) |
71 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
72 *
73 * - The sync bit is always set. It allows software to synchronize data packets
74 * as the X/Y position data typically does not have bit 4 set.
75 * - The overflow bits are set if motion exceeds accumulator range. We use the
76 * maximum range (effectively 9 bits) and do not set the overflow bits.
77 * - Movement in the up/right direction is defined as having positive sign.
78 *
79 *
80 * IntelliMouse PS/2 (ImPS/2) fourth report packet byte:
81 *
82 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
83 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
84 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
85 * | Byte 4 | Z movement delta (two's complement) |
86 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
87 *
88 * - The valid range for Z delta values is only -8/+7, i.e. 4 bits.
89 *
90 * IntelliMouse Explorer (ImEx) fourth report packet byte:
91 *
92 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
93 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
94 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
95 * | Byte 4 | 0 | 0 | Btn 5 | Btn 4 | Z mov't delta (two's complement) |
96 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
97 *
98 * - The Z delta values are in practice only -1/+1; some mice (A4tech?) report
99 * horizontal scrolling as -2/+2.
100 *
101 * IntelliMouse Explorer (ImEx) fourth report packet byte when scrolling:
102 *
103 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
104 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
105 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
106 * | Byte 4 | V | H | Z or W movement delta (two's complement) |
107 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
108 *
109 * - Buttons 4 and 5 are reported as with the regular ImEx protocol, but not when
110 * scrolling. This is a departure from the usual logic because when the mouse
111 * sends scroll events, the state of buttons 4/5 is not reported and the last
112 * reported state should be assumed.
113 *
114 * - When the V bit (bit 7) is set, vertical scroll (Z axis) is being reported.
115 * When the H bit (bit 6) is set, horizontal scroll (W axis) is being reported.
116 * The H and V bits are never set at the same time (also see below). When
117 * the H and V bits are both clear, button 4/5 state is being reported.
118 *
119 * - The Z/W delta is extended to 6 bits. Z (vertical) values are not restricted
120 * to -1/+1, although W (horizontal) values are. Z values of at least -20/+20
121 * can be seen in practice.
122 *
123 * - Horizontal and vertical scroll is mutually exclusive. When the button is
124 * tilted, no vertical scrolling is reported, i.e. horizontal scrolling
125 * has priority over vertical.
126 *
127 * - Positive values indicate down/right direction, negative values up/left.
128 *
129 * - When the scroll button is tilted to engage horizontal scrolling, the mouse
130 * keeps sending events at a rate of 4 or 5 per second as long as the button
131 * is tilted.
132 *
133 * All report formats were verified with a real Microsoft IntelliMouse Explorer 4.0
134 * mouse attached through a PS/2 port.
135 *
136 * The button "accumulator" is necessary to avoid missing brief button presses.
137 * Without it, a very fast mouse button press + release might be lost if it
138 * happened between sending reports. The accumulator latches button presses to
139 * prevent that.
140 *
141 */
142
143
144/*********************************************************************************************************************************
145* Header Files *
146*********************************************************************************************************************************/
147#define LOG_GROUP LOG_GROUP_DEV_KBD
148#include <VBox/vmm/pdmdev.h>
149#include <VBox/err.h>
150#include <iprt/assert.h>
151#include <iprt/uuid.h>
152#include "VBoxDD.h"
153#define IN_PS2M
154#include "DevPS2.h"
155
156
157/*********************************************************************************************************************************
158* Defined Constants And Macros *
159*********************************************************************************************************************************/
160/** @name Auxiliary device commands sent by the system.
161 * @{ */
162#define ACMD_SET_SCALE_11 0xE6 /* Set 1:1 scaling. */
163#define ACMD_SET_SCALE_21 0xE7 /* Set 2:1 scaling. */
164#define ACMD_SET_RES 0xE8 /* Set resolution. */
165#define ACMD_REQ_STATUS 0xE9 /* Get device status. */
166#define ACMD_SET_STREAM 0xEA /* Set stream mode. */
167#define ACMD_READ_REMOTE 0xEB /* Read remote data. */
168#define ACMD_RESET_WRAP 0xEC /* Exit wrap mode. */
169#define ACMD_INVALID_1 0xED
170#define ACMD_SET_WRAP 0xEE /* Set wrap (echo) mode. */
171#define ACMD_INVALID_2 0xEF
172#define ACMD_SET_REMOTE 0xF0 /* Set remote mode. */
173#define ACMD_INVALID_3 0xF1
174#define ACMD_READ_ID 0xF2 /* Read device ID. */
175#define ACMD_SET_SAMP_RATE 0xF3 /* Set sampling rate. */
176#define ACMD_ENABLE 0xF4 /* Enable (streaming mode). */
177#define ACMD_DISABLE 0xF5 /* Disable (streaming mode). */
178#define ACMD_SET_DEFAULT 0xF6 /* Set defaults. */
179#define ACMD_INVALID_4 0xF7
180#define ACMD_INVALID_5 0xF8
181#define ACMD_INVALID_6 0xF9
182#define ACMD_INVALID_7 0xFA
183#define ACMD_INVALID_8 0xFB
184#define ACMD_INVALID_9 0xFC
185#define ACMD_INVALID_10 0xFD
186#define ACMD_RESEND 0xFE /* Resend response. */
187#define ACMD_RESET 0xFF /* Reset device. */
188/** @} */
189
190/** @name Auxiliary device responses sent to the system.
191 * @{ */
192#define ARSP_ID 0x00
193#define ARSP_BAT_OK 0xAA /* Self-test passed. */
194#define ARSP_ACK 0xFA /* Command acknowledged. */
195#define ARSP_ERROR 0xFC /* Bad command. */
196#define ARSP_RESEND 0xFE /* Requesting resend. */
197/** @} */
198
199
200/*********************************************************************************************************************************
201* Test code function declarations *
202*********************************************************************************************************************************/
203#if defined(RT_STRICT) && defined(IN_RING3)
204static void ps2mR3TestAccumulation(void);
205#endif
206
207
208#ifdef IN_RING3
209
210/* Report a change in status down (or is it up?) the driver chain. */
211static void ps2mR3SetDriverState(PPS2M pThis, bool fEnabled)
212{
213 PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
214 if (pDrv)
215 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
216}
217
218/* Reset the pointing device. */
219static void ps2mR3Reset(PPS2M pThis)
220{
221 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_BAT_OK);
222 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, 0);
223 pThis->enmMode = AUX_MODE_STD;
224 pThis->u8CurrCmd = 0;
225
226 /// @todo move to its proper home!
227 ps2mR3SetDriverState(pThis, true);
228}
229
230#endif /* IN_RING3 */
231
232static void ps2mSetRate(PPS2M pThis, uint8_t rate)
233{
234 Assert(rate);
235 pThis->uThrottleDelay = rate ? 1000 / rate : 0;
236 pThis->u8SampleRate = rate;
237 LogFlowFunc(("Sampling rate %u, throttle delay %u ms\n", pThis->u8SampleRate, pThis->uThrottleDelay));
238}
239
240static void ps2mSetDefaults(PPS2M pThis)
241{
242 LogFlowFunc(("Set mouse defaults\n"));
243 /* Standard protocol, reporting disabled, resolution 2, 1:1 scaling. */
244 pThis->enmProtocol = PS2M_PROTO_PS2STD;
245 pThis->u8State = 0;
246 pThis->u8Resolution = 2;
247
248 /* Sample rate 100 reports per second. */
249 ps2mSetRate(pThis, 100);
250
251 /* Event queue, eccumulators, and button status bits are cleared. */
252 PS2CmnClearQueue((GeneriQ *)&pThis->evtQ);
253 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = pThis->iAccumW = pThis->fAccumB = 0;
254}
255
256/* Handle the sampling rate 'knock' sequence which selects protocol. */
257static void ps2mRateProtocolKnock(PPS2M pThis, uint8_t rate)
258{
259 switch (pThis->enmKnockState)
260 {
261 case PS2M_KNOCK_INITIAL:
262 if (rate == 200)
263 pThis->enmKnockState = PS2M_KNOCK_1ST;
264 break;
265 case PS2M_KNOCK_1ST:
266 if (rate == 100)
267 pThis->enmKnockState = PS2M_KNOCK_IMPS2_2ND;
268 else if (rate == 200)
269 pThis->enmKnockState = PS2M_KNOCK_IMEX_2ND;
270 else if (rate == 80)
271 pThis->enmKnockState = PS2M_KNOCK_IMEX_HORZ_2ND;
272 else
273 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
274 break;
275 case PS2M_KNOCK_IMPS2_2ND:
276 if (rate == 80)
277 {
278 pThis->enmProtocol = PS2M_PROTO_IMPS2;
279 LogRelFlow(("PS2M: Switching mouse to ImPS/2 protocol.\n"));
280 }
281 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
282 break;
283 case PS2M_KNOCK_IMEX_2ND:
284 if (rate == 80)
285 {
286 pThis->enmProtocol = PS2M_PROTO_IMEX;
287 LogRelFlow(("PS2M: Switching mouse to ImEx protocol.\n"));
288 }
289 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
290 break;
291 case PS2M_KNOCK_IMEX_HORZ_2ND:
292 if (rate == 40)
293 {
294 pThis->enmProtocol = PS2M_PROTO_IMEX_HORZ;
295 LogRelFlow(("PS2M: Switching mouse ImEx with horizontal scrolling.\n"));
296 }
297 RT_FALL_THRU();
298 default:
299 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
300 }
301}
302
303/* Three-button event mask. */
304#define PS2M_STD_BTN_MASK (RT_BIT(0) | RT_BIT(1) | RT_BIT(2))
305/* ImEx button 4/5 event mask. */
306#define PS2M_IMEX_BTN_MASK (RT_BIT(3) | RT_BIT(4))
307
308/* Report accumulated movement and button presses, then clear the accumulators. */
309static void ps2mReportAccumulatedEvents(PPS2M pThis, GeneriQ *pQueue, bool fAccumBtns)
310{
311 uint32_t fBtnState = fAccumBtns ? pThis->fAccumB : pThis->fCurrB;
312 uint8_t val;
313 int dX, dY, dZ, dW;
314
315 /* Clamp the accumulated delta values to the allowed range. */
316 dX = RT_MIN(RT_MAX(pThis->iAccumX, -255), 255);
317 dY = RT_MIN(RT_MAX(pThis->iAccumY, -255), 255);
318
319 /* Start with the sync bit and buttons 1-3. */
320 val = RT_BIT(3) | (fBtnState & PS2M_STD_BTN_MASK);
321 /* Set the X/Y sign bits. */
322 if (dX < 0)
323 val |= RT_BIT(4);
324 if (dY < 0)
325 val |= RT_BIT(5);
326
327 /* Send the standard 3-byte packet (always the same). */
328 PS2CmnInsertQueue(pQueue, val);
329 PS2CmnInsertQueue(pQueue, dX);
330 PS2CmnInsertQueue(pQueue, dY);
331
332 /* Add fourth byte if an extended protocol is in use. */
333 if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
334 {
335 /* Start out with 4-bit dZ range. */
336 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -8), 7);
337
338 if (pThis->enmProtocol == PS2M_PROTO_IMPS2)
339 {
340 /* NB: Only uses 4-bit dZ range, despite using a full byte. */
341 PS2CmnInsertQueue(pQueue, dZ);
342 pThis->iAccumZ -= dZ;
343 }
344 else if (pThis->enmProtocol == PS2M_PROTO_IMEX)
345 {
346 /* Z value uses 4 bits; buttons 4/5 in bits 4 and 5. */
347 val = (fBtnState & PS2M_IMEX_BTN_MASK) << 1;
348 val |= dZ & 0x0f;
349 pThis->iAccumZ -= dZ;
350 PS2CmnInsertQueue(pQueue, val);
351 }
352 else
353 {
354 Assert((pThis->enmProtocol == PS2M_PROTO_IMEX_HORZ));
355 /* With ImEx + horizontal reporting, prioritize buttons 4/5. */
356 if (pThis->iAccumZ || pThis->iAccumW)
357 {
358 /* ImEx + horizontal reporting Horizontal scroll has
359 * precedence over vertical. Buttons cannot be reported
360 * this way.
361 */
362 if (pThis->iAccumW)
363 {
364 dW = RT_MIN(RT_MAX(pThis->iAccumW, -32), 31);
365 val = (dW & 0x3F) | 0x40;
366 pThis->iAccumW -= dW;
367 }
368 else
369 {
370 Assert(pThis->iAccumZ);
371 /* We can use 6-bit dZ range. Wow! */
372 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -32), 31);
373 val = (dZ & 0x3F) | 0x80;
374 pThis->iAccumZ -= dZ;
375 }
376 }
377 else
378 {
379 /* Just Buttons 4/5 in bits 4 and 5. No scrolling. */
380 val = (fBtnState & PS2M_IMEX_BTN_MASK) << 1;
381 }
382 PS2CmnInsertQueue(pQueue, val);
383 }
384 }
385
386 /* Clear the movement accumulators, but not necessarily button state. */
387 pThis->iAccumX = pThis->iAccumY = 0;
388 /* Clear accumulated button state only when it's being used. */
389 if (fAccumBtns)
390 {
391 pThis->fReportedB = pThis->fCurrB | pThis->fAccumB;
392 pThis->fAccumB = 0;
393 }
394}
395
396
397/* Determine whether a reporting rate is one of the valid ones. */
398bool ps2mIsRateSupported(uint8_t rate)
399{
400 static uint8_t aValidRates[] = { 10, 20, 40, 60, 80, 100, 200 };
401 size_t i;
402 bool fValid = false;
403
404 for (i = 0; i < RT_ELEMENTS(aValidRates); ++i)
405 if (aValidRates[i] == rate)
406 {
407 fValid = true;
408 break;
409 }
410
411 return fValid;
412}
413
414/**
415 * Receive and process a byte sent by the keyboard controller.
416 *
417 * @param pThis The PS/2 auxiliary device instance data.
418 * @param cmd The command (or data) byte.
419 */
420int PS2MByteToAux(PPS2M pThis, uint8_t cmd)
421{
422 uint8_t u8Val;
423 bool fHandled = true;
424
425 LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
426
427 if (pThis->enmMode == AUX_MODE_RESET)
428 /* In reset mode, do not respond at all. */
429 return VINF_SUCCESS;
430
431 /* If there's anything left in the command response queue, trash it. */
432 PS2CmnClearQueue((GeneriQ *)&pThis->cmdQ);
433
434 if (pThis->enmMode == AUX_MODE_WRAP)
435 {
436 /* In wrap mode, bounce most data right back.*/
437 if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
438 ; /* Handle as regular commands. */
439 else
440 {
441 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, cmd);
442 return VINF_SUCCESS;
443 }
444 }
445
446#ifndef IN_RING3
447 /* Reset, Enable, and Set Default commands must be run in R3. */
448 if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
449 return VINF_IOM_R3_IOPORT_WRITE;
450#endif
451
452 switch (cmd)
453 {
454 case ACMD_SET_SCALE_11:
455 pThis->u8State &= ~AUX_STATE_SCALING;
456 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
457 pThis->u8CurrCmd = 0;
458 break;
459 case ACMD_SET_SCALE_21:
460 pThis->u8State |= AUX_STATE_SCALING;
461 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
462 pThis->u8CurrCmd = 0;
463 break;
464 case ACMD_REQ_STATUS:
465 /* Report current status, sample rate, and resolution. */
466 u8Val = (pThis->u8State & AUX_STATE_EXTERNAL) | (pThis->fCurrB & PS2M_STD_BTN_MASK);
467 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
468 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
469 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8Resolution);
470 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8SampleRate);
471 pThis->u8CurrCmd = 0;
472 break;
473 case ACMD_SET_STREAM:
474 pThis->u8State &= ~AUX_STATE_REMOTE;
475 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
476 pThis->u8CurrCmd = 0;
477 break;
478 case ACMD_READ_REMOTE:
479 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
480 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->cmdQ, false);
481 pThis->u8CurrCmd = 0;
482 break;
483 case ACMD_RESET_WRAP:
484 pThis->enmMode = AUX_MODE_STD;
485 /* NB: Stream mode reporting remains disabled! */
486 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
487 pThis->u8CurrCmd = 0;
488 break;
489 case ACMD_SET_WRAP:
490 pThis->enmMode = AUX_MODE_WRAP;
491 pThis->u8State &= ~AUX_STATE_ENABLED;
492 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
493 pThis->u8CurrCmd = 0;
494 break;
495 case ACMD_SET_REMOTE:
496 pThis->u8State |= AUX_STATE_REMOTE;
497 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
498 pThis->u8CurrCmd = 0;
499 break;
500 case ACMD_READ_ID:
501 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
502 /* ImEx + horizontal is protocol 4, just like plain ImEx. */
503 u8Val = pThis->enmProtocol == PS2M_PROTO_IMEX_HORZ ? PS2M_PROTO_IMEX : pThis->enmProtocol;
504 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
505 pThis->u8CurrCmd = 0;
506 break;
507 case ACMD_ENABLE:
508 pThis->u8State |= AUX_STATE_ENABLED;
509#ifdef IN_RING3
510 ps2mR3SetDriverState(pThis, true);
511#else
512 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
513#endif
514 PS2CmnClearQueue((GeneriQ *)&pThis->evtQ);
515 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
516 pThis->u8CurrCmd = 0;
517 break;
518 case ACMD_DISABLE:
519 pThis->u8State &= ~AUX_STATE_ENABLED;
520 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
521 pThis->u8CurrCmd = 0;
522 break;
523 case ACMD_SET_DEFAULT:
524 ps2mSetDefaults(pThis);
525 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
526 pThis->u8CurrCmd = 0;
527 break;
528 case ACMD_RESEND:
529 pThis->u8CurrCmd = 0;
530 break;
531 case ACMD_RESET:
532 ps2mSetDefaults(pThis);
533 /// @todo reset more?
534 pThis->u8CurrCmd = cmd;
535 pThis->enmMode = AUX_MODE_RESET;
536 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
537 if (pThis->fDelayReset)
538 /* Slightly delay reset completion; it might take hundreds of ms. */
539 TMTimerSetMillies(pThis->CTX_SUFF(pDelayTimer), 1);
540 else
541#ifdef IN_RING3
542 ps2mR3Reset(pThis);
543#else
544 AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
545#endif
546 break;
547 /* The following commands need a parameter. */
548 case ACMD_SET_RES:
549 case ACMD_SET_SAMP_RATE:
550 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
551 pThis->u8CurrCmd = cmd;
552 break;
553 default:
554 /* Sending a command instead of a parameter starts the new command. */
555 switch (pThis->u8CurrCmd)
556 {
557 case ACMD_SET_RES:
558 if (cmd < 4) /* Valid resolutions are 0-3. */
559 {
560 pThis->u8Resolution = cmd;
561 pThis->u8State &= ~AUX_STATE_RES_ERR;
562 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
563 pThis->u8CurrCmd = 0;
564 }
565 else
566 {
567 /* Bad resolution. Reply with Resend or Error. */
568 if (pThis->u8State & AUX_STATE_RES_ERR)
569 {
570 pThis->u8State &= ~AUX_STATE_RES_ERR;
571 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
572 pThis->u8CurrCmd = 0;
573 }
574 else
575 {
576 pThis->u8State |= AUX_STATE_RES_ERR;
577 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
578 /* NB: Current command remains unchanged. */
579 }
580 }
581 break;
582 case ACMD_SET_SAMP_RATE:
583 if (ps2mIsRateSupported(cmd))
584 {
585 pThis->u8State &= ~AUX_STATE_RATE_ERR;
586 ps2mSetRate(pThis, cmd);
587 ps2mRateProtocolKnock(pThis, cmd);
588 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
589 pThis->u8CurrCmd = 0;
590 }
591 else
592 {
593 /* Bad rate. Reply with Resend or Error. */
594 if (pThis->u8State & AUX_STATE_RATE_ERR)
595 {
596 pThis->u8State &= ~AUX_STATE_RATE_ERR;
597 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
598 pThis->u8CurrCmd = 0;
599 }
600 else
601 {
602 pThis->u8State |= AUX_STATE_RATE_ERR;
603 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
604 /* NB: Current command remains unchanged. */
605 }
606 }
607 break;
608 default:
609 fHandled = false;
610 }
611 /* Fall through only to handle unrecognized commands. */
612 if (fHandled)
613 break;
614 RT_FALL_THRU();
615
616 case ACMD_INVALID_1:
617 case ACMD_INVALID_2:
618 case ACMD_INVALID_3:
619 case ACMD_INVALID_4:
620 case ACMD_INVALID_5:
621 case ACMD_INVALID_6:
622 case ACMD_INVALID_7:
623 case ACMD_INVALID_8:
624 case ACMD_INVALID_9:
625 case ACMD_INVALID_10:
626 Log(("Unsupported command 0x%02X!\n", cmd));
627 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
628 pThis->u8CurrCmd = 0;
629 break;
630 }
631 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
632 return VINF_SUCCESS;
633}
634
635/**
636 * Send a byte (packet data or command response) to the keyboard controller.
637 *
638 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
639 * @param pThis The PS/2 auxiliary device instance data.
640 * @param pb Where to return the byte we've read.
641 * @remarks Caller must have entered the device critical section.
642 */
643int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
644{
645 int rc;
646
647 AssertPtr(pb);
648
649 /* Anything in the command queue has priority over data
650 * in the event queue. Additionally, packet data are
651 * blocked if a command is currently in progress, even if
652 * the command queue is empty.
653 */
654 /// @todo Probably should flush/not fill queue if stream mode reporting disabled?!
655 rc = PS2CmnRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
656 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
657 rc = PS2CmnRemoveQueue((GeneriQ *)&pThis->evtQ, pb);
658
659 LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
660
661 return rc;
662}
663
664#ifdef IN_RING3
665
666/** Is there any state change to send as events to the guest? */
667static uint32_t ps2mR3HaveEvents(PPS2M pThis)
668{
669 return pThis->iAccumX || pThis->iAccumY || pThis->iAccumZ || pThis->iAccumW
670 || ((pThis->fCurrB | pThis->fAccumB) != pThis->fReportedB);
671}
672
673/**
674 * @callback_function_impl{FNTMTIMERDEV,
675 * Event rate throttling timer to emulate the auxiliary device sampling rate.}
676 */
677static DECLCALLBACK(void) ps2mR3ThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
678{
679 RT_NOREF(pDevIns, pTimer);
680 PPS2M pThis = (PS2M *)pvUser;
681 uint32_t uHaveEvents;
682
683 /* Grab the lock to avoid races with PutEvent(). */
684 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
685 AssertReleaseRC(rc);
686
687 /* If more movement is accumulated, report it and restart the timer. */
688 uHaveEvents = ps2mR3HaveEvents(pThis);
689 LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
690
691 if (uHaveEvents)
692 {
693 /* Report accumulated data, poke the KBC, and start the timer. */
694 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
695 KBCUpdateInterrupts(pThis->pParent);
696 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
697 }
698 else
699 pThis->fThrottleActive = false;
700
701 PDMCritSectLeave(pThis->pCritSectR3);
702}
703
704/**
705 * @callback_function_impl{FNTMTIMERDEV}
706 *
707 * The auxiliary device reset is specified to take up to about 500 milliseconds.
708 * We need to delay sending the result to the host for at least a tiny little
709 * while.
710 */
711static DECLCALLBACK(void) ps2mR3DelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
712{
713 RT_NOREF(pDevIns, pTimer);
714 PPS2M pThis = (PS2M *)pvUser;
715
716 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
717
718 Assert(pThis->u8CurrCmd == ACMD_RESET);
719 ps2mR3Reset(pThis);
720
721 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
722 /* Give the KBC a kick. */
723 KBCUpdateInterrupts(pThis->pParent);
724}
725
726
727/**
728 * Debug device info handler. Prints basic auxiliary device state.
729 *
730 * @param pDevIns Device instance which registered the info.
731 * @param pHlp Callback functions for doing output.
732 * @param pszArgs Argument string. Optional and specific to the handler.
733 */
734static DECLCALLBACK(void) ps2mR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
735{
736 static const char *pcszModes[] = { "normal", "reset", "wrap" };
737 static const char *pcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx", "ImEx+horizontal" };
738 PPS2M pThis = KBDGetPS2MFromDevIns(pDevIns);
739 NOREF(pszArgs);
740
741 Assert(pThis->enmMode <= RT_ELEMENTS(pcszModes));
742 Assert(pThis->enmProtocol <= RT_ELEMENTS(pcszProtocols));
743 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
744 pcszModes[pThis->enmMode],
745 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
746 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
747 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
748 pcszProtocols[pThis->enmProtocol],
749 pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
750 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
751 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
752 pThis->u8SampleRate, 1 << pThis->u8Resolution);
753 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
754 pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
755 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
756 pThis->evtQ.cUsed, pThis->evtQ.cSize);
757}
758
759/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
760
761/**
762 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
763 */
764static DECLCALLBACK(void *) ps2mR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
765{
766 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IBase);
767 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
768 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
769 return NULL;
770}
771
772
773/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
774
775/**
776 * Mouse event handler.
777 *
778 * @returns VBox status code.
779 * @param pThis The PS/2 auxiliary device instance data.
780 * @param dx X direction movement delta.
781 * @param dy Y direction movement delta.
782 * @param dz Z (vertical scroll) movement delta.
783 * @param dw W (horizontal scroll) movement delta.
784 * @param fButtons Depressed button mask.
785 */
786static int ps2mR3PutEventWorker(PPS2M pThis, int32_t dx, int32_t dy, int32_t dz, int32_t dw, uint32_t fButtons)
787{
788 /* Update internal accumulators and button state. Ignore any buttons beyond 5. */
789 pThis->iAccumX += dx;
790 pThis->iAccumY += dy;
791 pThis->iAccumZ += dz;
792 pThis->iAccumW += dw;
793 pThis->fCurrB = fButtons & (PS2M_STD_BTN_MASK | PS2M_IMEX_BTN_MASK);
794 pThis->fAccumB |= pThis->fCurrB;
795
796 /* Ditch accumulated data that can't be reported by the current protocol.
797 * This avoids sending phantom empty reports when un-reportable events
798 * are received.
799 */
800 if (pThis->enmProtocol < PS2M_PROTO_IMEX_HORZ)
801 pThis->iAccumW = 0; /* No horizontal scroll. */
802
803 if (pThis->enmProtocol < PS2M_PROTO_IMEX)
804 {
805 pThis->fAccumB &= PS2M_STD_BTN_MASK; /* Only buttons 1-3. */
806 pThis->fCurrB &= PS2M_STD_BTN_MASK;
807 }
808
809 if (pThis->enmProtocol < PS2M_PROTO_IMPS2)
810 pThis->iAccumZ = 0; /* No vertical scroll. */
811
812 /* Report the event (if any) and start the throttle timer unless it's already running. */
813 if (!pThis->fThrottleActive && ps2mR3HaveEvents(pThis))
814 {
815 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
816 KBCUpdateInterrupts(pThis->pParent);
817 pThis->fThrottleActive = true;
818 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
819 }
820
821 return VINF_SUCCESS;
822}
823
824/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
825
826/**
827 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
828 */
829static DECLCALLBACK(int) ps2mR3MousePort_PutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
830 int32_t dz, int32_t dw, uint32_t fButtons)
831{
832 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IPort);
833 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
834 AssertReleaseRC(rc);
835
836 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
837 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
838 ps2mR3PutEventWorker(pThis, dx, -dy, dz, dw, fButtons);
839
840 PDMCritSectLeave(pThis->pCritSectR3);
841 return VINF_SUCCESS;
842}
843
844/**
845 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
846 */
847static DECLCALLBACK(int) ps2mR3MousePort_PutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
848 int32_t dz, int32_t dw, uint32_t fButtons)
849{
850 AssertFailedReturn(VERR_NOT_SUPPORTED);
851 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
852}
853
854/**
855 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
856 */
857static DECLCALLBACK(int) ps2mR3MousePort_PutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
858 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
859{
860 AssertFailedReturn(VERR_NOT_SUPPORTED);
861 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
862}
863
864
865
866/**
867 * Attach command.
868 *
869 * This is called to let the device attach to a driver for a
870 * specified LUN.
871 *
872 * This is like plugging in the mouse after turning on the
873 * system.
874 *
875 * @returns VBox status code.
876 * @param pThis The PS/2 auxiliary device instance data.
877 * @param pDevIns The device instance.
878 * @param iLUN The logical unit which is being detached.
879 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
880 */
881int PS2MR3Attach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
882{
883 int rc;
884
885 /* The LUN must be 1, i.e. mouse. */
886 Assert(iLUN == 1);
887 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
888 ("PS/2 mouse does not support hotplugging\n"),
889 VERR_INVALID_PARAMETER);
890
891 LogFlowFunc(("iLUN=%d\n", iLUN));
892
893 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Mouse Port");
894 if (RT_SUCCESS(rc))
895 {
896 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
897 if (!pThis->Mouse.pDrv)
898 {
899 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
900 rc = VERR_PDM_MISSING_INTERFACE;
901 }
902 }
903 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
904 {
905 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
906 rc = VINF_SUCCESS;
907 }
908 else
909 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
910
911 return rc;
912}
913
914void PS2MR3SaveState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM)
915{
916 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
917 LogFlowFunc(("Saving PS2M state\n"));
918
919 /* Save the core auxiliary device state. */
920 pHlp->pfnSSMPutU8(pSSM, pThis->u8State);
921 pHlp->pfnSSMPutU8(pSSM, pThis->u8SampleRate);
922 pHlp->pfnSSMPutU8(pSSM, pThis->u8Resolution);
923 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
924 pHlp->pfnSSMPutU8(pSSM, pThis->enmMode);
925 pHlp->pfnSSMPutU8(pSSM, pThis->enmProtocol);
926 pHlp->pfnSSMPutU8(pSSM, pThis->enmKnockState);
927
928 /* Save the command and event queues. */
929 PS2CmnR3SaveQueue(pHlp, pSSM, (GeneriQ *)&pThis->cmdQ);
930 PS2CmnR3SaveQueue(pHlp, pSSM, (GeneriQ *)&pThis->evtQ);
931
932 /* Save the command delay timer. Note that the rate throttling
933 * timer is *not* saved.
934 */
935 TMR3TimerSave(pThis->CTX_SUFF(pDelayTimer), pSSM);
936}
937
938int PS2MR3LoadState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion)
939{
940 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
941 uint8_t u8;
942 int rc;
943
944 NOREF(uVersion);
945 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
946
947 /* Load the basic auxiliary device state. */
948 pHlp->pfnSSMGetU8(pSSM, &pThis->u8State);
949 pHlp->pfnSSMGetU8(pSSM, &pThis->u8SampleRate);
950 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Resolution);
951 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
952 pHlp->pfnSSMGetU8(pSSM, &u8);
953 pThis->enmMode = (PS2M_MODE)u8;
954 pHlp->pfnSSMGetU8(pSSM, &u8);
955 pThis->enmProtocol = (PS2M_PROTO)u8;
956 pHlp->pfnSSMGetU8(pSSM, &u8);
957 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
958
959 /* Load the command and event queues. */
960 rc = PS2CmnR3LoadQueue(pHlp, pSSM, (GeneriQ *)&pThis->cmdQ);
961 AssertRCReturn(rc, rc);
962 rc = PS2CmnR3LoadQueue(pHlp, pSSM, (GeneriQ *)&pThis->evtQ);
963 AssertRCReturn(rc, rc);
964
965 /* Load the command delay timer, just in case. */
966 rc = TMR3TimerLoad(pThis->CTX_SUFF(pDelayTimer), pSSM);
967 AssertRCReturn(rc, rc);
968
969 /* Recalculate the throttling delay. */
970 ps2mSetRate(pThis, pThis->u8SampleRate);
971
972 ps2mR3SetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
973
974 return VINF_SUCCESS;
975}
976
977void PS2MR3FixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
978{
979 LogFlowFunc(("Fixing up old PS2M state version\n"));
980
981 /* Load the basic auxiliary device state. */
982 pThis->u8State = u8State;
983 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
984 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
985
986 /* Recalculate the throttling delay. */
987 ps2mSetRate(pThis, pThis->u8SampleRate);
988
989 ps2mR3SetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
990}
991
992void PS2MR3Reset(PPS2M pThis)
993{
994 LogFlowFunc(("Resetting PS2M\n"));
995
996 pThis->u8CurrCmd = 0;
997
998 /* Clear the queues. */
999 PS2CmnClearQueue((GeneriQ *)&pThis->cmdQ);
1000 ps2mSetDefaults(pThis); /* Also clears event queue. */
1001}
1002
1003void PS2MR3Relocate(PPS2M pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns)
1004{
1005 RT_NOREF(pDevIns, offDelta);
1006 LogFlowFunc(("Relocating PS2M\n"));
1007 pThis->pDelayTimerRC = TMTimerRCPtr(pThis->pDelayTimerR3);
1008 pThis->pThrottleTimerRC = TMTimerRCPtr(pThis->pThrottleTimerR3);
1009}
1010
1011int PS2MR3Construct(PPS2M pThis, PPDMDEVINS pDevIns, PKBDSTATE pParent, unsigned iInstance)
1012{
1013 RT_NOREF(iInstance);
1014
1015 LogFlowFunc(("iInstance=%u\n", iInstance));
1016
1017#ifdef RT_STRICT
1018 ps2mR3TestAccumulation();
1019#endif
1020
1021 pThis->pParent = pParent;
1022
1023 /* Initialize the queues. */
1024 pThis->evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1025 pThis->cmdQ.cSize = AUX_CMD_QUEUE_SIZE;
1026
1027 pThis->Mouse.IBase.pfnQueryInterface = ps2mR3QueryInterface;
1028 pThis->Mouse.IPort.pfnPutEvent = ps2mR3MousePort_PutEvent;
1029 pThis->Mouse.IPort.pfnPutEventAbs = ps2mR3MousePort_PutEventAbs;
1030 pThis->Mouse.IPort.pfnPutEventMultiTouch = ps2mR3MousePort_PutEventMT;
1031
1032 /*
1033 * Initialize the critical section pointer(s).
1034 */
1035 pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
1036
1037 /*
1038 * Create the input rate throttling timer. Does not use virtual time!
1039 */
1040 PTMTIMER pTimer;
1041 int rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_REAL, ps2mR3ThrottleTimer, pThis,
1042 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pTimer);
1043 AssertRCReturn(rc, rc);
1044
1045 pThis->pThrottleTimerR3 = pTimer;
1046 pThis->pThrottleTimerR0 = TMTimerR0Ptr(pTimer);
1047 pThis->pThrottleTimerRC = TMTimerRCPtr(pTimer);
1048
1049 /*
1050 * Create the command delay timer.
1051 */
1052 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mR3DelayTimer, pThis,
1053 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pTimer);
1054 AssertRCReturn(rc, rc);
1055
1056 pThis->pDelayTimerR3 = pTimer;
1057 pThis->pDelayTimerR0 = TMTimerR0Ptr(pTimer);
1058 pThis->pDelayTimerRC = TMTimerRCPtr(pTimer);
1059
1060 /*
1061 * Register debugger info callbacks.
1062 */
1063 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mR3InfoState);
1064
1065 /// @todo Where should we do this?
1066 ps2mR3SetDriverState(pThis, true);
1067 pThis->u8State = 0;
1068 pThis->enmMode = AUX_MODE_STD;
1069
1070 return rc;
1071}
1072
1073#endif
1074
1075#if defined(RT_STRICT) && defined(IN_RING3)
1076/* -=-=-=-=-=- Test code -=-=-=-=-=- */
1077
1078/** Test the event accumulation mechanism which we use to delay events going
1079 * to the guest to one per 10ms (the default PS/2 mouse event rate). This
1080 * test depends on ps2mR3PutEventWorker() not touching the timer if
1081 * This.fThrottleActive is true. */
1082/** @todo if we add any more tests it might be worth using a table of test
1083 * operations and checks. */
1084static void ps2mR3TestAccumulation(void)
1085{
1086 PS2M This;
1087 unsigned i;
1088 int rc;
1089 uint8_t b;
1090
1091 RT_ZERO(This);
1092 This.evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1093 This.u8State = AUX_STATE_ENABLED;
1094 This.fThrottleActive = true;
1095 /* Certain Windows touch pad drivers report a double tap as a press, then
1096 * a release-press-release all within a single 10ms interval. Simulate
1097 * this to check that it is handled right. */
1098 ps2mR3PutEventWorker(&This, 0, 0, 0, 0, 1);
1099 if (ps2mR3HaveEvents(&This))
1100 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1101 ps2mR3PutEventWorker(&This, 0, 0, 0, 0, 0);
1102 if (ps2mR3HaveEvents(&This))
1103 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1104 ps2mR3PutEventWorker(&This, 0, 0, 0, 0, 1);
1105 ps2mR3PutEventWorker(&This, 0, 0, 0, 0, 0);
1106 if (ps2mR3HaveEvents(&This))
1107 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1108 if (ps2mR3HaveEvents(&This))
1109 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1110 for (i = 0; i < 12; ++i)
1111 {
1112 const uint8_t abExpected[] = { 9, 0, 0, 8, 0, 0, 9, 0, 0, 8, 0, 0};
1113
1114 rc = PS2MByteFromAux(&This, &b);
1115 AssertRCSuccess(rc);
1116 Assert(b == abExpected[i]);
1117 }
1118 rc = PS2MByteFromAux(&This, &b);
1119 Assert(rc != VINF_SUCCESS);
1120 /* Button hold down during mouse drags was broken at some point during
1121 * testing fixes for the previous issue. Test that that works. */
1122 ps2mR3PutEventWorker(&This, 0, 0, 0, 0, 1);
1123 if (ps2mR3HaveEvents(&This))
1124 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1125 if (ps2mR3HaveEvents(&This))
1126 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1127 for (i = 0; i < 3; ++i)
1128 {
1129 const uint8_t abExpected[] = { 9, 0, 0 };
1130
1131 rc = PS2MByteFromAux(&This, &b);
1132 AssertRCSuccess(rc);
1133 Assert(b == abExpected[i]);
1134 }
1135 rc = PS2MByteFromAux(&This, &b);
1136 Assert(rc != VINF_SUCCESS);
1137}
1138#endif /* RT_STRICT && IN_RING3 */
1139
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette