VirtualBox

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

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

DevPS2: Converted timers. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.6 KB
Line 
1/* $Id: DevPS2M.cpp 82195 2019-11-25 19:12:32Z 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(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 pDevIns The device instance.
418 * @param pThis The PS/2 auxiliary device 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 PS2CmnClearQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
458 pThis->u8CurrCmd = 0;
459 break;
460 case ACMD_SET_SCALE_21:
461 pThis->u8State |= AUX_STATE_SCALING;
462 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
469 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
470 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8Resolution);
471 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8SampleRate);
472 pThis->u8CurrCmd = 0;
473 break;
474 case ACMD_SET_STREAM:
475 pThis->u8State &= ~AUX_STATE_REMOTE;
476 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
477 pThis->u8CurrCmd = 0;
478 break;
479 case ACMD_READ_REMOTE:
480 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
481 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->cmdQ, 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 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
494 pThis->u8CurrCmd = 0;
495 break;
496 case ACMD_SET_REMOTE:
497 pThis->u8State |= AUX_STATE_REMOTE;
498 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
499 pThis->u8CurrCmd = 0;
500 break;
501 case ACMD_READ_ID:
502 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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(pThis, true);
512#else
513 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
514#endif
515 PS2CmnClearQueue((GeneriQ *)&pThis->evtQ);
516 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
517 pThis->u8CurrCmd = 0;
518 break;
519 case ACMD_DISABLE:
520 pThis->u8State &= ~AUX_STATE_ENABLED;
521 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
522 pThis->u8CurrCmd = 0;
523 break;
524 case ACMD_SET_DEFAULT:
525 ps2mSetDefaults(pThis);
526 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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);
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 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
573 pThis->u8CurrCmd = 0;
574 }
575 else
576 {
577 pThis->u8State |= AUX_STATE_RES_ERR;
578 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
599 pThis->u8CurrCmd = 0;
600 }
601 else
602 {
603 pThis->u8State |= AUX_STATE_RATE_ERR;
604 PS2CmnInsertQueue((GeneriQ *)&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 PS2CmnInsertQueue((GeneriQ *)&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 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 = PS2CmnRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
657 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
658 rc = PS2CmnRemoveQueue((GeneriQ *)&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_function_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 = PDMCritSectEnter(pThis->pCritSectR3, 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, (GeneriQ *)&pThis->evtQ, true);
696 KBCUpdateInterrupts(pDevIns);
697 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, pThis->uThrottleDelay);
698 }
699 else
700 pThis->fThrottleActive = false;
701
702 PDMCritSectLeave(pThis->pCritSectR3);
703}
704
705/**
706 * @callback_function_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 RT_NOREF(pDevIns, pTimer);
715 PPS2M pThis = (PS2M *)pvUser;
716
717 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
718
719 Assert(pThis->u8CurrCmd == ACMD_RESET);
720 ps2mR3Reset(pThis);
721
722 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
723 /* Give the KBC a kick. */
724 KBCUpdateInterrupts(pDevIns);
725}
726
727
728/**
729 * Debug device info handler. Prints basic auxiliary device state.
730 *
731 * @param pDevIns Device instance which registered the info.
732 * @param pHlp Callback functions for doing output.
733 * @param pszArgs Argument string. Optional and specific to the handler.
734 */
735static DECLCALLBACK(void) ps2mR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
736{
737 static const char *pcszModes[] = { "normal", "reset", "wrap" };
738 static const char *pcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx", "ImEx+horizontal" };
739 PPS2M pThis = KBDGetPS2MFromDevIns(pDevIns);
740 NOREF(pszArgs);
741
742 Assert(pThis->enmMode <= RT_ELEMENTS(pcszModes));
743 Assert(pThis->enmProtocol <= RT_ELEMENTS(pcszProtocols));
744 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
745 pcszModes[pThis->enmMode],
746 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
747 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
748 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
749 pcszProtocols[pThis->enmProtocol],
750 pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
751 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
752 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
753 pThis->u8SampleRate, 1 << pThis->u8Resolution);
754 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
755 pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
756 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
757 pThis->evtQ.cUsed, pThis->evtQ.cSize);
758}
759
760/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
761
762/**
763 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
764 */
765static DECLCALLBACK(void *) ps2mR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
766{
767 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IBase);
768 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
769 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
770 return NULL;
771}
772
773
774/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
775
776/**
777 * Mouse event handler.
778 *
779 * @returns VBox status code.
780 * @param pDevIns The device instance.
781 * @param pThis The PS/2 auxiliary device instance data.
782 * @param dx X direction movement delta.
783 * @param dy Y direction movement delta.
784 * @param dz Z (vertical scroll) movement delta.
785 * @param dw W (horizontal scroll) movement delta.
786 * @param fButtons Depressed button mask.
787 */
788static int ps2mR3PutEventWorker(PPDMDEVINS pDevIns, PPS2M pThis, int32_t dx, int32_t dy, int32_t dz, int32_t dw, uint32_t fButtons)
789{
790 /* Update internal accumulators and button state. Ignore any buttons beyond 5. */
791 pThis->iAccumX += dx;
792 pThis->iAccumY += dy;
793 pThis->iAccumZ += dz;
794 pThis->iAccumW += dw;
795 pThis->fCurrB = fButtons & (PS2M_STD_BTN_MASK | PS2M_IMEX_BTN_MASK);
796 pThis->fAccumB |= pThis->fCurrB;
797
798 /* Ditch accumulated data that can't be reported by the current protocol.
799 * This avoids sending phantom empty reports when un-reportable events
800 * are received.
801 */
802 if (pThis->enmProtocol < PS2M_PROTO_IMEX_HORZ)
803 pThis->iAccumW = 0; /* No horizontal scroll. */
804
805 if (pThis->enmProtocol < PS2M_PROTO_IMEX)
806 {
807 pThis->fAccumB &= PS2M_STD_BTN_MASK; /* Only buttons 1-3. */
808 pThis->fCurrB &= PS2M_STD_BTN_MASK;
809 }
810
811 if (pThis->enmProtocol < PS2M_PROTO_IMPS2)
812 pThis->iAccumZ = 0; /* No vertical scroll. */
813
814 /* Report the event (if any) and start the throttle timer unless it's already running. */
815 if (!pThis->fThrottleActive && ps2mR3HaveEvents(pThis))
816 {
817 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
818 KBCUpdateInterrupts(pDevIns);
819 pThis->fThrottleActive = true;
820 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, pThis->uThrottleDelay);
821 }
822
823 return VINF_SUCCESS;
824}
825
826/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
827
828/**
829 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
830 */
831static DECLCALLBACK(int) ps2mR3MousePort_PutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
832 int32_t dz, int32_t dw, uint32_t fButtons)
833{
834 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IPort);
835 PPDMDEVINS pDevIns = pThis->pDevIns;
836 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
837 AssertReleaseRC(rc);
838
839 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
840 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
841 ps2mR3PutEventWorker(pDevIns, pThis, dx, -dy, dz, dw, fButtons);
842
843 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
844 return VINF_SUCCESS;
845}
846
847/**
848 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
849 */
850static DECLCALLBACK(int) ps2mR3MousePort_PutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
851 int32_t dz, int32_t dw, uint32_t fButtons)
852{
853 AssertFailedReturn(VERR_NOT_SUPPORTED);
854 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
855}
856
857/**
858 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
859 */
860static DECLCALLBACK(int) ps2mR3MousePort_PutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
861 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
862{
863 AssertFailedReturn(VERR_NOT_SUPPORTED);
864 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
865}
866
867
868
869/**
870 * Attach command.
871 *
872 * This is called to let the device attach to a driver for a
873 * specified LUN.
874 *
875 * This is like plugging in the mouse after turning on the
876 * system.
877 *
878 * @returns VBox status code.
879 * @param pThis The PS/2 auxiliary device instance data.
880 * @param pDevIns The device instance.
881 * @param iLUN The logical unit which is being detached.
882 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
883 */
884int PS2MR3Attach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
885{
886 int rc;
887
888 /* The LUN must be 1, i.e. mouse. */
889 Assert(iLUN == 1);
890 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
891 ("PS/2 mouse does not support hotplugging\n"),
892 VERR_INVALID_PARAMETER);
893
894 LogFlowFunc(("iLUN=%d\n", iLUN));
895
896 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Mouse Port");
897 if (RT_SUCCESS(rc))
898 {
899 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
900 if (!pThis->Mouse.pDrv)
901 {
902 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
903 rc = VERR_PDM_MISSING_INTERFACE;
904 }
905 }
906 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
907 {
908 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
909 rc = VINF_SUCCESS;
910 }
911 else
912 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
913
914 return rc;
915}
916
917void PS2MR3SaveState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM)
918{
919 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
920 LogFlowFunc(("Saving PS2M state\n"));
921
922 /* Save the core auxiliary device state. */
923 pHlp->pfnSSMPutU8(pSSM, pThis->u8State);
924 pHlp->pfnSSMPutU8(pSSM, pThis->u8SampleRate);
925 pHlp->pfnSSMPutU8(pSSM, pThis->u8Resolution);
926 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
927 pHlp->pfnSSMPutU8(pSSM, pThis->enmMode);
928 pHlp->pfnSSMPutU8(pSSM, pThis->enmProtocol);
929 pHlp->pfnSSMPutU8(pSSM, pThis->enmKnockState);
930
931 /* Save the command and event queues. */
932 PS2CmnR3SaveQueue(pHlp, pSSM, (GeneriQ *)&pThis->cmdQ);
933 PS2CmnR3SaveQueue(pHlp, pSSM, (GeneriQ *)&pThis->evtQ);
934
935 /* Save the command delay timer. Note that the rate throttling
936 * timer is *not* saved.
937 */
938 PDMDevHlpTimerSave(pDevIns, pThis->hDelayTimer, pSSM);
939}
940
941int PS2MR3LoadState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion)
942{
943 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
944 uint8_t u8;
945 int rc;
946
947 NOREF(uVersion);
948 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
949
950 /* Load the basic auxiliary device state. */
951 pHlp->pfnSSMGetU8(pSSM, &pThis->u8State);
952 pHlp->pfnSSMGetU8(pSSM, &pThis->u8SampleRate);
953 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Resolution);
954 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
955 pHlp->pfnSSMGetU8(pSSM, &u8);
956 pThis->enmMode = (PS2M_MODE)u8;
957 pHlp->pfnSSMGetU8(pSSM, &u8);
958 pThis->enmProtocol = (PS2M_PROTO)u8;
959 pHlp->pfnSSMGetU8(pSSM, &u8);
960 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
961
962 /* Load the command and event queues. */
963 rc = PS2CmnR3LoadQueue(pHlp, pSSM, (GeneriQ *)&pThis->cmdQ);
964 AssertRCReturn(rc, rc);
965 rc = PS2CmnR3LoadQueue(pHlp, pSSM, (GeneriQ *)&pThis->evtQ);
966 AssertRCReturn(rc, rc);
967
968 /* Load the command delay timer, just in case. */
969 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hDelayTimer, pSSM);
970 AssertRCReturn(rc, rc);
971
972 /* Recalculate the throttling delay. */
973 ps2mSetRate(pThis, pThis->u8SampleRate);
974
975 ps2mR3SetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
976
977 return VINF_SUCCESS;
978}
979
980void PS2MR3FixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
981{
982 LogFlowFunc(("Fixing up old PS2M state version\n"));
983
984 /* Load the basic auxiliary device state. */
985 pThis->u8State = u8State;
986 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
987 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
988
989 /* Recalculate the throttling delay. */
990 ps2mSetRate(pThis, pThis->u8SampleRate);
991
992 ps2mR3SetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
993}
994
995void PS2MR3Reset(PPS2M pThis)
996{
997 LogFlowFunc(("Resetting PS2M\n"));
998
999 pThis->u8CurrCmd = 0;
1000
1001 /* Clear the queues. */
1002 PS2CmnClearQueue((GeneriQ *)&pThis->cmdQ);
1003 ps2mSetDefaults(pThis); /* Also clears event queue. */
1004}
1005
1006int PS2MR3Construct(PPS2M pThis, PPDMDEVINS pDevIns, PKBDSTATE pParent, unsigned iInstance)
1007{
1008 RT_NOREF(iInstance);
1009
1010 LogFlowFunc(("iInstance=%u\n", iInstance));
1011
1012#ifdef RT_STRICT
1013 ps2mR3TestAccumulation();
1014#endif
1015
1016 pThis->pParent = pParent;
1017 pThis->pDevIns = pDevIns;
1018
1019 /* Initialize the queues. */
1020 pThis->evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1021 pThis->cmdQ.cSize = AUX_CMD_QUEUE_SIZE;
1022
1023 pThis->Mouse.IBase.pfnQueryInterface = ps2mR3QueryInterface;
1024 pThis->Mouse.IPort.pfnPutEvent = ps2mR3MousePort_PutEvent;
1025 pThis->Mouse.IPort.pfnPutEventAbs = ps2mR3MousePort_PutEventAbs;
1026 pThis->Mouse.IPort.pfnPutEventMultiTouch = ps2mR3MousePort_PutEventMT;
1027
1028 /*
1029 * Initialize the critical section pointer(s).
1030 */
1031 pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
1032
1033 /*
1034 * Create the input rate throttling timer. Does not use virtual time!
1035 */
1036 int rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_REAL, ps2mR3ThrottleTimer, pThis,
1037 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pThis->hThrottleTimer);
1038 AssertRCReturn(rc, rc);
1039
1040 /*
1041 * Create the command delay timer.
1042 */
1043 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mR3DelayTimer, pThis,
1044 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pThis->hDelayTimer);
1045 AssertRCReturn(rc, rc);
1046
1047 /*
1048 * Register debugger info callbacks.
1049 */
1050 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mR3InfoState);
1051
1052 /// @todo Where should we do this?
1053 ps2mR3SetDriverState(pThis, true);
1054 pThis->u8State = 0;
1055 pThis->enmMode = AUX_MODE_STD;
1056
1057 return rc;
1058}
1059
1060#endif
1061
1062#if defined(RT_STRICT) && defined(IN_RING3)
1063/* -=-=-=-=-=- Test code -=-=-=-=-=- */
1064
1065/** Test the event accumulation mechanism which we use to delay events going
1066 * to the guest to one per 10ms (the default PS/2 mouse event rate). This
1067 * test depends on ps2mR3PutEventWorker() not touching the timer if
1068 * This.fThrottleActive is true. */
1069/** @todo if we add any more tests it might be worth using a table of test
1070 * operations and checks. */
1071static void ps2mR3TestAccumulation(void)
1072{
1073 PS2M This;
1074 unsigned i;
1075 int rc;
1076 uint8_t b;
1077
1078 RT_ZERO(This);
1079 This.evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1080 This.u8State = AUX_STATE_ENABLED;
1081 This.fThrottleActive = true;
1082 /* Certain Windows touch pad drivers report a double tap as a press, then
1083 * a release-press-release all within a single 10ms interval. Simulate
1084 * this to check that it is handled right. */
1085 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1086 if (ps2mR3HaveEvents(&This))
1087 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1088 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1089 if (ps2mR3HaveEvents(&This))
1090 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1091 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1092 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1093 if (ps2mR3HaveEvents(&This))
1094 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1095 if (ps2mR3HaveEvents(&This))
1096 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1097 for (i = 0; i < 12; ++i)
1098 {
1099 const uint8_t abExpected[] = { 9, 0, 0, 8, 0, 0, 9, 0, 0, 8, 0, 0};
1100
1101 rc = PS2MByteFromAux(&This, &b);
1102 AssertRCSuccess(rc);
1103 Assert(b == abExpected[i]);
1104 }
1105 rc = PS2MByteFromAux(&This, &b);
1106 Assert(rc != VINF_SUCCESS);
1107 /* Button hold down during mouse drags was broken at some point during
1108 * testing fixes for the previous issue. Test that that works. */
1109 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1110 if (ps2mR3HaveEvents(&This))
1111 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1112 if (ps2mR3HaveEvents(&This))
1113 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1114 for (i = 0; i < 3; ++i)
1115 {
1116 const uint8_t abExpected[] = { 9, 0, 0 };
1117
1118 rc = PS2MByteFromAux(&This, &b);
1119 AssertRCSuccess(rc);
1120 Assert(b == abExpected[i]);
1121 }
1122 rc = PS2MByteFromAux(&This, &b);
1123 Assert(rc != VINF_SUCCESS);
1124}
1125#endif /* RT_STRICT && IN_RING3 */
1126
Note: See TracBrowser for help on using the repository browser.

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