VirtualBox

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

Last change on this file was 108993, checked in by vboxsync, 5 weeks ago

DevPS2M: Fixed Parfait warning about switch statement testing for impossible values outside of IN_RING3. [build fix]

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