VirtualBox

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

Last change on this file since 82781 was 82213, checked in by vboxsync, 5 years ago

doxygen fixes. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.8 KB
Line 
1/* $Id: DevPS2M.cpp 82213 2019-11-26 02:08:48Z 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.virtualbox.org. 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(PPS2MR3 pThisCC, bool fEnabled)
212{
213 PPDMIMOUSECONNECTOR pDrv = pThisCC->Mouse.pDrv;
214 if (pDrv)
215 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
216}
217
218/* Reset the pointing device. */
219static void ps2mR3Reset(PPS2M pThis, PPS2MR3 pThisCC)
220{
221 PS2Q_INSERT(&pThis->cmdQ, ARSP_BAT_OK);
222 PS2Q_INSERT(&pThis->cmdQ, 0);
223 pThis->enmMode = AUX_MODE_STD;
224 pThis->u8CurrCmd = 0;
225
226 /// @todo move to its proper home!
227 ps2mR3SetDriverState(pThisCC, 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 PS2Q_CLEAR(&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, PPS2QHDR pQHdr, size_t cQElements, uint8_t *pbQElements, 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(pQHdr, cQElements, pbQElements, val);
329 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, dX);
330 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, 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(pQHdr, cQElements, pbQElements, 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(pQHdr, cQElements, pbQElements, 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(pQHdr, cQElements, pbQElements, 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 pDevIns The device instance.
418 * @param pThis The PS/2 auxiliary device shared instance data.
419 * @param cmd The command (or data) byte.
420 */
421int PS2MByteToAux(PPDMDEVINS pDevIns, PPS2M pThis, uint8_t cmd)
422{
423 uint8_t u8Val;
424 bool fHandled = true;
425
426 LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
427
428 if (pThis->enmMode == AUX_MODE_RESET)
429 /* In reset mode, do not respond at all. */
430 return VINF_SUCCESS;
431
432 /* If there's anything left in the command response queue, trash it. */
433 PS2Q_CLEAR(&pThis->cmdQ);
434
435 if (pThis->enmMode == AUX_MODE_WRAP)
436 {
437 /* In wrap mode, bounce most data right back.*/
438 if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
439 ; /* Handle as regular commands. */
440 else
441 {
442 PS2Q_INSERT(&pThis->cmdQ, cmd);
443 return VINF_SUCCESS;
444 }
445 }
446
447#ifndef IN_RING3
448 /* Reset, Enable, and Set Default commands must be run in R3. */
449 if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
450 return VINF_IOM_R3_IOPORT_WRITE;
451#endif
452
453 switch (cmd)
454 {
455 case ACMD_SET_SCALE_11:
456 pThis->u8State &= ~AUX_STATE_SCALING;
457 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
458 pThis->u8CurrCmd = 0;
459 break;
460 case ACMD_SET_SCALE_21:
461 pThis->u8State |= AUX_STATE_SCALING;
462 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
463 pThis->u8CurrCmd = 0;
464 break;
465 case ACMD_REQ_STATUS:
466 /* Report current status, sample rate, and resolution. */
467 u8Val = (pThis->u8State & AUX_STATE_EXTERNAL) | (pThis->fCurrB & PS2M_STD_BTN_MASK);
468 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
469 PS2Q_INSERT(&pThis->cmdQ, u8Val);
470 PS2Q_INSERT(&pThis->cmdQ, pThis->u8Resolution);
471 PS2Q_INSERT(&pThis->cmdQ, pThis->u8SampleRate);
472 pThis->u8CurrCmd = 0;
473 break;
474 case ACMD_SET_STREAM:
475 pThis->u8State &= ~AUX_STATE_REMOTE;
476 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
477 pThis->u8CurrCmd = 0;
478 break;
479 case ACMD_READ_REMOTE:
480 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
481 ps2mReportAccumulatedEvents(pThis, &pThis->cmdQ.Hdr, RT_ELEMENTS(pThis->cmdQ.abQueue), pThis->cmdQ.abQueue, false);
482 pThis->u8CurrCmd = 0;
483 break;
484 case ACMD_RESET_WRAP:
485 pThis->enmMode = AUX_MODE_STD;
486 /* NB: Stream mode reporting remains disabled! */
487 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
488 pThis->u8CurrCmd = 0;
489 break;
490 case ACMD_SET_WRAP:
491 pThis->enmMode = AUX_MODE_WRAP;
492 pThis->u8State &= ~AUX_STATE_ENABLED;
493 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
494 pThis->u8CurrCmd = 0;
495 break;
496 case ACMD_SET_REMOTE:
497 pThis->u8State |= AUX_STATE_REMOTE;
498 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
499 pThis->u8CurrCmd = 0;
500 break;
501 case ACMD_READ_ID:
502 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
503 /* ImEx + horizontal is protocol 4, just like plain ImEx. */
504 u8Val = pThis->enmProtocol == PS2M_PROTO_IMEX_HORZ ? PS2M_PROTO_IMEX : pThis->enmProtocol;
505 PS2Q_INSERT(&pThis->cmdQ, u8Val);
506 pThis->u8CurrCmd = 0;
507 break;
508 case ACMD_ENABLE:
509 pThis->u8State |= AUX_STATE_ENABLED;
510#ifdef IN_RING3
511 ps2mR3SetDriverState(&PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux, true);
512#else
513 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
514#endif
515 PS2Q_CLEAR(&pThis->evtQ);
516 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
517 pThis->u8CurrCmd = 0;
518 break;
519 case ACMD_DISABLE:
520 pThis->u8State &= ~AUX_STATE_ENABLED;
521 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
522 pThis->u8CurrCmd = 0;
523 break;
524 case ACMD_SET_DEFAULT:
525 ps2mSetDefaults(pThis);
526 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
527 pThis->u8CurrCmd = 0;
528 break;
529 case ACMD_RESEND:
530 pThis->u8CurrCmd = 0;
531 break;
532 case ACMD_RESET:
533 ps2mSetDefaults(pThis);
534 /// @todo reset more?
535 pThis->u8CurrCmd = cmd;
536 pThis->enmMode = AUX_MODE_RESET;
537 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
538 if (pThis->fDelayReset)
539 /* Slightly delay reset completion; it might take hundreds of ms. */
540 PDMDevHlpTimerSetMillies(pDevIns, pThis->hDelayTimer, 1);
541 else
542#ifdef IN_RING3
543 ps2mR3Reset(pThis, &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux);
544#else
545 AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
546#endif
547 break;
548 /* The following commands need a parameter. */
549 case ACMD_SET_RES:
550 case ACMD_SET_SAMP_RATE:
551 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
552 pThis->u8CurrCmd = cmd;
553 break;
554 default:
555 /* Sending a command instead of a parameter starts the new command. */
556 switch (pThis->u8CurrCmd)
557 {
558 case ACMD_SET_RES:
559 if (cmd < 4) /* Valid resolutions are 0-3. */
560 {
561 pThis->u8Resolution = cmd;
562 pThis->u8State &= ~AUX_STATE_RES_ERR;
563 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
564 pThis->u8CurrCmd = 0;
565 }
566 else
567 {
568 /* Bad resolution. Reply with Resend or Error. */
569 if (pThis->u8State & AUX_STATE_RES_ERR)
570 {
571 pThis->u8State &= ~AUX_STATE_RES_ERR;
572 PS2Q_INSERT(&pThis->cmdQ, ARSP_ERROR);
573 pThis->u8CurrCmd = 0;
574 }
575 else
576 {
577 pThis->u8State |= AUX_STATE_RES_ERR;
578 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
579 /* NB: Current command remains unchanged. */
580 }
581 }
582 break;
583 case ACMD_SET_SAMP_RATE:
584 if (ps2mIsRateSupported(cmd))
585 {
586 pThis->u8State &= ~AUX_STATE_RATE_ERR;
587 ps2mSetRate(pThis, cmd);
588 ps2mRateProtocolKnock(pThis, cmd);
589 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
590 pThis->u8CurrCmd = 0;
591 }
592 else
593 {
594 /* Bad rate. Reply with Resend or Error. */
595 if (pThis->u8State & AUX_STATE_RATE_ERR)
596 {
597 pThis->u8State &= ~AUX_STATE_RATE_ERR;
598 PS2Q_INSERT(&pThis->cmdQ, ARSP_ERROR);
599 pThis->u8CurrCmd = 0;
600 }
601 else
602 {
603 pThis->u8State |= AUX_STATE_RATE_ERR;
604 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
605 /* NB: Current command remains unchanged. */
606 }
607 }
608 break;
609 default:
610 fHandled = false;
611 }
612 /* Fall through only to handle unrecognized commands. */
613 if (fHandled)
614 break;
615 RT_FALL_THRU();
616
617 case ACMD_INVALID_1:
618 case ACMD_INVALID_2:
619 case ACMD_INVALID_3:
620 case ACMD_INVALID_4:
621 case ACMD_INVALID_5:
622 case ACMD_INVALID_6:
623 case ACMD_INVALID_7:
624 case ACMD_INVALID_8:
625 case ACMD_INVALID_9:
626 case ACMD_INVALID_10:
627 Log(("Unsupported command 0x%02X!\n", cmd));
628 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
629 pThis->u8CurrCmd = 0;
630 break;
631 }
632 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
633 return VINF_SUCCESS;
634}
635
636/**
637 * Send a byte (packet data or command response) to the keyboard controller.
638 *
639 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
640 * @param pThis The PS/2 auxiliary device shared instance data.
641 * @param pb Where to return the byte we've read.
642 * @remarks Caller must have entered the device critical section.
643 */
644int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
645{
646 int rc;
647
648 AssertPtr(pb);
649
650 /* Anything in the command queue has priority over data
651 * in the event queue. Additionally, packet data are
652 * blocked if a command is currently in progress, even if
653 * the command queue is empty.
654 */
655 /// @todo Probably should flush/not fill queue if stream mode reporting disabled?!
656 rc = PS2Q_REMOVE(&pThis->cmdQ, pb);
657 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
658 rc = PS2Q_REMOVE(&pThis->evtQ, pb);
659
660 LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
661
662 return rc;
663}
664
665#ifdef IN_RING3
666
667/** Is there any state change to send as events to the guest? */
668static uint32_t ps2mR3HaveEvents(PPS2M pThis)
669{
670 return pThis->iAccumX || pThis->iAccumY || pThis->iAccumZ || pThis->iAccumW
671 || ((pThis->fCurrB | pThis->fAccumB) != pThis->fReportedB);
672}
673
674/**
675 * @callback_method_impl{FNTMTIMERDEV,
676 * Event rate throttling timer to emulate the auxiliary device sampling rate.}
677 */
678static DECLCALLBACK(void) ps2mR3ThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
679{
680 RT_NOREF(pDevIns, pTimer);
681 PPS2M pThis = (PS2M *)pvUser;
682 uint32_t uHaveEvents;
683
684 /* Grab the lock to avoid races with PutEvent(). */
685 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
686 AssertReleaseRC(rc);
687
688 /* If more movement is accumulated, report it and restart the timer. */
689 uHaveEvents = ps2mR3HaveEvents(pThis);
690 LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
691
692 if (uHaveEvents)
693 {
694 /* Report accumulated data, poke the KBC, and start the timer. */
695 ps2mReportAccumulatedEvents(pThis, &pThis->evtQ.Hdr, RT_ELEMENTS(pThis->evtQ.abQueue), pThis->evtQ.abQueue, true);
696 KBCUpdateInterrupts(pDevIns);
697 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, pThis->uThrottleDelay);
698 }
699 else
700 pThis->fThrottleActive = false;
701
702 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
703}
704
705/**
706 * @callback_method_impl{FNTMTIMERDEV}
707 *
708 * The auxiliary device reset is specified to take up to about 500 milliseconds.
709 * We need to delay sending the result to the host for at least a tiny little
710 * while.
711 */
712static DECLCALLBACK(void) ps2mR3DelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
713{
714 PPS2M pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Aux;
715 PPS2MR3 pThisCC = &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux;
716 RT_NOREF(pvUser, pTimer);
717
718 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
719
720 Assert(pThis->u8CurrCmd == ACMD_RESET);
721 ps2mR3Reset(pThis, pThisCC);
722
723 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
724 /* Give the KBC a kick. */
725 KBCUpdateInterrupts(pDevIns);
726}
727
728
729/**
730 * Debug device info handler. Prints basic auxiliary device state.
731 *
732 * @param pDevIns Device instance which registered the info.
733 * @param pHlp Callback functions for doing output.
734 * @param pszArgs Argument string. Optional and specific to the handler.
735 */
736static DECLCALLBACK(void) ps2mR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
737{
738 static const char * const s_apcszModes[] = { "normal", "reset", "wrap" };
739 static const char * const s_apcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx", "ImEx+horizontal" };
740 PKBDSTATE pParent = PDMDEVINS_2_DATA(pDevIns, PKBDSTATE);
741 PPS2M pThis = &pParent->Aux;
742 NOREF(pszArgs);
743
744 Assert(pThis->enmMode < RT_ELEMENTS(s_apcszModes));
745 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
746 s_apcszModes[pThis->enmMode],
747 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
748 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
749 Assert(pThis->enmProtocol < RT_ELEMENTS(s_apcszProtocols));
750 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
751 s_apcszProtocols[pThis->enmProtocol],
752 pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
753 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
754 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
755 pThis->u8SampleRate, 1 << pThis->u8Resolution);
756 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
757 PS2Q_COUNT(&pThis->cmdQ), PS2Q_SIZE(&pThis->cmdQ));
758 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
759 PS2Q_COUNT(&pThis->evtQ), PS2Q_SIZE(&pThis->evtQ));
760}
761
762
763/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
764
765/**
766 * Mouse event handler.
767 *
768 * @returns VBox status code.
769 * @param pDevIns The device instance.
770 * @param pThis The PS/2 auxiliary device shared instance data.
771 * @param dx X direction movement delta.
772 * @param dy Y direction movement delta.
773 * @param dz Z (vertical scroll) movement delta.
774 * @param dw W (horizontal scroll) movement delta.
775 * @param fButtons Depressed button mask.
776 */
777static int ps2mR3PutEventWorker(PPDMDEVINS pDevIns, PPS2M pThis, int32_t dx, int32_t dy, int32_t dz, int32_t dw, uint32_t fButtons)
778{
779 /* Update internal accumulators and button state. Ignore any buttons beyond 5. */
780 pThis->iAccumX += dx;
781 pThis->iAccumY += dy;
782 pThis->iAccumZ += dz;
783 pThis->iAccumW += dw;
784 pThis->fCurrB = fButtons & (PS2M_STD_BTN_MASK | PS2M_IMEX_BTN_MASK);
785 pThis->fAccumB |= pThis->fCurrB;
786
787 /* Ditch accumulated data that can't be reported by the current protocol.
788 * This avoids sending phantom empty reports when un-reportable events
789 * are received.
790 */
791 if (pThis->enmProtocol < PS2M_PROTO_IMEX_HORZ)
792 pThis->iAccumW = 0; /* No horizontal scroll. */
793
794 if (pThis->enmProtocol < PS2M_PROTO_IMEX)
795 {
796 pThis->fAccumB &= PS2M_STD_BTN_MASK; /* Only buttons 1-3. */
797 pThis->fCurrB &= PS2M_STD_BTN_MASK;
798 }
799
800 if (pThis->enmProtocol < PS2M_PROTO_IMPS2)
801 pThis->iAccumZ = 0; /* No vertical scroll. */
802
803 /* Report the event (if any) and start the throttle timer unless it's already running. */
804 if (!pThis->fThrottleActive && ps2mR3HaveEvents(pThis))
805 {
806 ps2mReportAccumulatedEvents(pThis, &pThis->evtQ.Hdr, RT_ELEMENTS(pThis->evtQ.abQueue), pThis->evtQ.abQueue, true);
807 KBCUpdateInterrupts(pDevIns);
808 pThis->fThrottleActive = true;
809 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, pThis->uThrottleDelay);
810 }
811
812 return VINF_SUCCESS;
813}
814
815
816/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
817
818/**
819 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
820 */
821static DECLCALLBACK(int) ps2mR3MousePort_PutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
822 int32_t dz, int32_t dw, uint32_t fButtons)
823{
824 PPS2MR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2MR3, Mouse.IPort);
825 PPDMDEVINS pDevIns = pThisCC->pDevIns;
826 PPS2M pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Aux;
827 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
828 AssertReleaseRC(rc);
829
830 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
831 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
832 ps2mR3PutEventWorker(pDevIns, pThis, dx, -dy, dz, dw, fButtons);
833
834 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
835 return VINF_SUCCESS;
836}
837
838/**
839 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
840 */
841static DECLCALLBACK(int) ps2mR3MousePort_PutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
842 int32_t dz, int32_t dw, uint32_t fButtons)
843{
844 AssertFailedReturn(VERR_NOT_SUPPORTED);
845 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
846}
847
848/**
849 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
850 */
851static DECLCALLBACK(int) ps2mR3MousePort_PutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
852 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
853{
854 AssertFailedReturn(VERR_NOT_SUPPORTED);
855 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
856}
857
858
859/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
860
861/**
862 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
863 */
864static DECLCALLBACK(void *) ps2mR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
865{
866 PPS2MR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2MR3, Mouse.IBase);
867 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->Mouse.IBase);
868 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThisCC->Mouse.IPort);
869 return NULL;
870}
871
872
873/* -=-=-=-=-=- Device management -=-=-=-=-=- */
874
875/**
876 * Attach command.
877 *
878 * This is called to let the device attach to a driver for a
879 * specified LUN.
880 *
881 * This is like plugging in the mouse after turning on the
882 * system.
883 *
884 * @returns VBox status code.
885 * @param pDevIns The device instance.
886 * @param pThisCC The PS/2 auxiliary device instance data for ring-3.
887 * @param iLUN The logical unit which is being detached.
888 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
889 */
890int PS2MR3Attach(PPDMDEVINS pDevIns, PPS2MR3 pThisCC, unsigned iLUN, uint32_t fFlags)
891{
892 int rc;
893
894 /* The LUN must be 1, i.e. mouse. */
895 Assert(iLUN == 1);
896 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
897 ("PS/2 mouse does not support hotplugging\n"),
898 VERR_INVALID_PARAMETER);
899
900 LogFlowFunc(("iLUN=%d\n", iLUN));
901
902 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->Mouse.IBase, &pThisCC->Mouse.pDrvBase, "Mouse Port");
903 if (RT_SUCCESS(rc))
904 {
905 pThisCC->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
906 if (!pThisCC->Mouse.pDrv)
907 {
908 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
909 rc = VERR_PDM_MISSING_INTERFACE;
910 }
911 }
912 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
913 {
914 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
915 rc = VINF_SUCCESS;
916 }
917 else
918 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
919
920 return rc;
921}
922
923void PS2MR3SaveState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM)
924{
925 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
926 LogFlowFunc(("Saving PS2M state\n"));
927
928 /* Save the core auxiliary device state. */
929 pHlp->pfnSSMPutU8(pSSM, pThis->u8State);
930 pHlp->pfnSSMPutU8(pSSM, pThis->u8SampleRate);
931 pHlp->pfnSSMPutU8(pSSM, pThis->u8Resolution);
932 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
933 pHlp->pfnSSMPutU8(pSSM, pThis->enmMode);
934 pHlp->pfnSSMPutU8(pSSM, pThis->enmProtocol);
935 pHlp->pfnSSMPutU8(pSSM, pThis->enmKnockState);
936
937 /* Save the command and event queues. */
938 PS2Q_SAVE(pHlp, pSSM, &pThis->cmdQ);
939 PS2Q_SAVE(pHlp, pSSM, &pThis->evtQ);
940
941 /* Save the command delay timer. Note that the rate throttling
942 * timer is *not* saved.
943 */
944 PDMDevHlpTimerSave(pDevIns, pThis->hDelayTimer, pSSM);
945}
946
947int PS2MR3LoadState(PPDMDEVINS pDevIns, PPS2M pThis, PPS2MR3 pThisCC, PSSMHANDLE pSSM, uint32_t uVersion)
948{
949 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
950 uint8_t u8;
951 int rc;
952
953 NOREF(uVersion);
954 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
955
956 /* Load the basic auxiliary device state. */
957 pHlp->pfnSSMGetU8(pSSM, &pThis->u8State);
958 pHlp->pfnSSMGetU8(pSSM, &pThis->u8SampleRate);
959 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Resolution);
960 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
961 pHlp->pfnSSMGetU8(pSSM, &u8);
962 pThis->enmMode = (PS2M_MODE)u8;
963 pHlp->pfnSSMGetU8(pSSM, &u8);
964 pThis->enmProtocol = (PS2M_PROTO)u8;
965 pHlp->pfnSSMGetU8(pSSM, &u8);
966 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
967
968 /* Load the command and event queues. */
969 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->cmdQ);
970 AssertRCReturn(rc, rc);
971 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->evtQ);
972 AssertRCReturn(rc, rc);
973
974 /* Load the command delay timer, just in case. */
975 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hDelayTimer, pSSM);
976 AssertRCReturn(rc, rc);
977
978 /* Recalculate the throttling delay. */
979 ps2mSetRate(pThis, pThis->u8SampleRate);
980
981 ps2mR3SetDriverState(pThisCC, !!(pThis->u8State & AUX_STATE_ENABLED));
982
983 return VINF_SUCCESS;
984}
985
986void PS2MR3FixupState(PPS2M pThis, PPS2MR3 pThisCC, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
987{
988 LogFlowFunc(("Fixing up old PS2M state version\n"));
989
990 /* Load the basic auxiliary device state. */
991 pThis->u8State = u8State;
992 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
993 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
994
995 /* Recalculate the throttling delay. */
996 ps2mSetRate(pThis, pThis->u8SampleRate);
997
998 ps2mR3SetDriverState(pThisCC, !!(pThis->u8State & AUX_STATE_ENABLED));
999}
1000
1001void PS2MR3Reset(PPS2M pThis)
1002{
1003 LogFlowFunc(("Resetting PS2M\n"));
1004
1005 pThis->u8CurrCmd = 0;
1006
1007 /* Clear the queues. */
1008 PS2Q_CLEAR(&pThis->cmdQ);
1009 ps2mSetDefaults(pThis); /* Also clears event queue. */
1010}
1011
1012int PS2MR3Construct(PPDMDEVINS pDevIns, PPS2M pThis, PPS2MR3 pThisCC)
1013{
1014 LogFlowFunc(("\n"));
1015#ifdef RT_STRICT
1016 ps2mR3TestAccumulation();
1017#endif
1018
1019 /*
1020 * Initialize the state.
1021 */
1022 pThisCC->pDevIns = pDevIns;
1023 pThisCC->Mouse.IBase.pfnQueryInterface = ps2mR3QueryInterface;
1024 pThisCC->Mouse.IPort.pfnPutEvent = ps2mR3MousePort_PutEvent;
1025 pThisCC->Mouse.IPort.pfnPutEventAbs = ps2mR3MousePort_PutEventAbs;
1026 pThisCC->Mouse.IPort.pfnPutEventMultiTouch = ps2mR3MousePort_PutEventMT;
1027
1028 /*
1029 * Create the input rate throttling timer. Does not use virtual time!
1030 */
1031 int rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_REAL, ps2mR3ThrottleTimer, pThis,
1032 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pThis->hThrottleTimer);
1033 AssertRCReturn(rc, rc);
1034
1035 /*
1036 * Create the command delay timer.
1037 */
1038 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mR3DelayTimer, pThis,
1039 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pThis->hDelayTimer);
1040 AssertRCReturn(rc, rc);
1041
1042 /*
1043 * Register debugger info callbacks.
1044 */
1045 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mR3InfoState);
1046
1047 /// @todo Where should we do this?
1048 ps2mR3SetDriverState(pThisCC, true);
1049 pThis->u8State = 0;
1050 pThis->enmMode = AUX_MODE_STD;
1051
1052 return rc;
1053}
1054
1055#endif
1056
1057#if defined(RT_STRICT) && defined(IN_RING3)
1058/* -=-=-=-=-=- Test code -=-=-=-=-=- */
1059
1060/** Test the event accumulation mechanism which we use to delay events going
1061 * to the guest to one per 10ms (the default PS/2 mouse event rate). This
1062 * test depends on ps2mR3PutEventWorker() not touching the timer if
1063 * This.fThrottleActive is true. */
1064/** @todo if we add any more tests it might be worth using a table of test
1065 * operations and checks. */
1066static void ps2mR3TestAccumulation(void)
1067{
1068 PS2M This;
1069 unsigned i;
1070 int rc;
1071 uint8_t b;
1072
1073 RT_ZERO(This);
1074 This.u8State = AUX_STATE_ENABLED;
1075 This.fThrottleActive = true;
1076 /* Certain Windows touch pad drivers report a double tap as a press, then
1077 * a release-press-release all within a single 10ms interval. Simulate
1078 * this to check that it is handled right. */
1079 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1080 if (ps2mR3HaveEvents(&This))
1081 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1082 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1083 if (ps2mR3HaveEvents(&This))
1084 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1085 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1086 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1087 if (ps2mR3HaveEvents(&This))
1088 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1089 if (ps2mR3HaveEvents(&This))
1090 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1091 for (i = 0; i < 12; ++i)
1092 {
1093 const uint8_t abExpected[] = { 9, 0, 0, 8, 0, 0, 9, 0, 0, 8, 0, 0};
1094
1095 rc = PS2MByteFromAux(&This, &b);
1096 AssertRCSuccess(rc);
1097 Assert(b == abExpected[i]);
1098 }
1099 rc = PS2MByteFromAux(&This, &b);
1100 Assert(rc != VINF_SUCCESS);
1101 /* Button hold down during mouse drags was broken at some point during
1102 * testing fixes for the previous issue. Test that that works. */
1103 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1104 if (ps2mR3HaveEvents(&This))
1105 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1106 if (ps2mR3HaveEvents(&This))
1107 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1108 for (i = 0; i < 3; ++i)
1109 {
1110 const uint8_t abExpected[] = { 9, 0, 0 };
1111
1112 rc = PS2MByteFromAux(&This, &b);
1113 AssertRCSuccess(rc);
1114 Assert(b == abExpected[i]);
1115 }
1116 rc = PS2MByteFromAux(&This, &b);
1117 Assert(rc != VINF_SUCCESS);
1118}
1119#endif /* RT_STRICT && IN_RING3 */
1120
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use